📄 objecthmm.py
字号:
def ReadCState(self, cmodel, cstate, i): State.ReadCState(self, cmodel, cstate, i) self.silent = ValidatingBool(cmodel.getSilent(i)) class BackgroundState(State): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) self.init() def init(self): self.background = PopupableInt() self.background.setPopup(self.itsHMM.backgroundDistributions.val2pop) def update(self): if len(self.itsHMM.backgroundDistributions.names()) > 0: self.editableAttr['background'] = "Background distribution" self.background.setPopup(self.itsHMM.backgroundDistributions.val2pop) def ReadCState(self, cmodel, cstate, i): State.ReadCState(self, cmodel, cstate, i) self.update() self.background = PopupableInt(cmodel.getBackgroundID(i)+1)class LabeledState(State): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) self.init() def init(self): self.label = PopupableInt() self.label.setPopup(self.itsHMM.label_alphabet.name) def update(self): if len(self.itsHMM.label_alphabet.names()) > 0: self.editableAttr['label'] = "State label" self.label.setPopup(self.itsHMM.label_alphabet.name) def ReadCState(self, cmodel, cstate, i): State.ReadCState(self, cmodel, cstate, i) self.update self.label = PopupableInt(cmodel.getStateLabel(i))class TiedState(State): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) self.init() def init(self): self.tiedto = PopupableInt(0) self.tiedto.setPopup(self.itsHMM.tie_groups.val2pop) def update(self): if len(self.itsHMM.tie_groups.names()) > 0: self.editableAttr['tiedto'] = "Emissions tied to state" self.tiedto.setPopup(self.itsHMM.tie_groups.val2pop) def editEmissions(self, master): if self.tiedto > 0: self.itsHMM.tie_groups.editEmissions(master, self.tiedto) else: State.editEmissions(self, master) def ReadCState(self, cmodel, cstate, i): State.ReadCState(self, cmodel, cstate, i) tied = cmodel.getTiedTo(i) if tied == i: self.itsHMM.tie_groups.add("tiedto_state%d"%tied, self.itsHMM.alphabet) self.update() self.tiedto = PopupableInt(self.itsHMM.tie_groups.name2code["tiedto_state%d"%tied])class SilentBackgroundState(SilentState, BackgroundState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) BackgroundState.init(self) def update(self): BackgroundState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class SilentLabeledState(SilentState, LabeledState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) LabeledState.init(self) def update(self): LabeledState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) LabeledState.ReadCState(self, cmodel, cstate, i)class SilentTiedState(SilentState, TiedState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) TiedState.init(self) def update(self): TiedState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i)class LabeledBackgroundState(LabeledState, BackgroundState): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) LabeledState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) LabeledState.update(self) def ReadCState(self, cmodel, cstate, i): LabeledState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class LabeledTiedState(LabeledState, TiedState): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) LabeledState.init(self) TiedState.init(self) def update(self): LabeledState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate, i): LabeledState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i)class TiedBackgroundState(TiedState, BackgroundState): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) TiedState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate): TiedState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class SilentLabeledBackgroundState(SilentState, LabeledState, BackgroundState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) LabeledState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) LabeledState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) LabeledState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class SilentLabeledTiedState(SilentState, LabeledState, TiedState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) LabeledState.init(self) TiedState.init(self) def update(self): LabeledState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) LabeledState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i)class SilentTiedBackgroundState(SilentState, TiedState, BackgroundState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) TiedState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate): SilentState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class LabeledTiedBackgroundState(LabeledState, TiedState, BackgroundState): def __init__(self, emission=Emission(), hmm=None): State.__init__(self, emission, hmm) LabeledState.init(self) TiedState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) LabeledState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate, i): LabeledState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class SilentLabeledTiedBackgroundState(SilentState, LabeledState, TiedState, BackgroundState): def __init__(self, emission=Emission(), hmm=None, silent=ValidatingBool(False)): State.__init__(self, emission, hmm) SilentState.init(self, silent) LabeledState.init(self) TiedState.init(self) BackgroundState.init(self) def update(self): BackgroundState.update(self) LabeledState.update(self) TiedState.update(self) def ReadCState(self, cmodel, cstate, i): SilentState.ReadCState(self, cmodel, cstate, i) LabeledState.ReadCState(self, cmodel, cstate, i) TiedState.ReadCState(self, cmodel, cstate, i) BackgroundState.ReadCState(self, cmodel, cstate, i)class Transition(EdgeObject): def __init__(self, tail, head): EdgeObject.__init__(self, tail, head) #self.weight = Probability() self.editableAttr = {'weight':"Weight"} def __str__(self): return "Transition from %d to %d with probability %f" % (self.tail.id, self.head.id, self.GetWeight()) def __setattr__(self, name, value): if name is "p": self.SetWeight(value) else: self.__dict__[name] = value #object.__setattr__(self, name, value) def __getattr__(self, name): if name is "p": return self.GetWeight() else: return self.__dict__[name] #return object.__getattr__(self, name) def GetWeight(self): return self.GetEdgeWeight(0) def SetWeight(self, value): self.SetEdgeWeight(0, value) def edit(self, parent, attributes = None): if attributes == None: editBox = EditObjectAttributesDialog(parent, self, self.editableAttr) else: editableAttr = {} for attr in attributes: editableAttr[attr] = self.editableAttr[attr] editBox = EditObjectAttributesDialog(parent, self, editableAttr) def ReadCTransition(self, state, cos, i): assert (cos == 1) self.weight = state.getOutProb(i)class SwitchedTransition(Transition): def __init__(self, tail, head, noTransitionClasses=2): Transition.__init__(self, tail, head) self.weight = [Probability()] * noTransitionClasses self.noClasses = noTransitionClasses def GetEdgeWeight(self,i): if i < self.noClasses: return self.weight[i] def SetEdgeWeight(self,i,value): if i < self.noClasses: self.weight[i] = value def ReadCTransition(self, state, cos, i): self.weight = ghmmhelper.double_array2list(state.out_a[i], cos)class ObjectHMM(ObjectGraph): """ """ def __init__(self, stateClass, transitionClass, emissionClass=Emission(), alphabet=None, etype=0): ObjectGraph.__init__(self, stateClass, transitionClass) self.simple = 0 self.euclidian = 0 self.directed = 1 self.modelType = 0 self.alphabet = alphabet self.emissionClass = emissionClass self.etype = etype self.name2id = defaultdict(list) self.vertices_ids = {0:'untied'} # editable attributes per EditPropertiesDialog # common properties: self.desc = "New HMM properties" self.editableAttr = {'name':"Name"} self.alphatype = PopupableInt() self.alphatype_val2pop = {0:"binary", 1:"dice", 2:"DNA", 3:"amino acids", 4:"custom"} self.alphatype.setPopup(self.alphatype_val2pop) self.labels = ValidatingBool() self.background = ValidatingBool() self.maxOrder = DefaultedInt(0) self.maxOrder.setDefault(1, 0) self.name = ValidatingString("") self.silent = ValidatingBool() self.switching = DefaultedInt(1) self.switching.setDefault(1, 1) self.tied = ValidatingBool() # discrete emissions only properties if etype == 0: self.editableAttr['tied'] = "Tied emissions" self.editableAttr['silent'] = "Silent states" self.editableAttr['maxOrder'] = "Higher order emissions" self.editableAttr['background'] = "Background distributions" self.editableAttr['labels'] = "State labels" self.editableAttr['alphatype'] = "Alphabet" # continuous emissions only properties elif etype == 1: self.editableAttr['switching'] = "No. of transition Classes" else: tkMessageBox.showerror("HMMEd", "invalid model type") def __str__(self): string = "HMM with %d states" % len(self.vertices) for v in self.vertices.values(): string += '\n' + str(v) for e in self.edges.values(): string += '\n' + str(e) return string def AddVertex(self): """ Add an isolated vertex. Returns the id of the new vertex """ if self.alphabet is not None: e = self.emissionClass(self.alphabet) else: e = self.emissionClass() v = self.vertexClass(e, self) v.id = self.GetNextVertexID() self.vertices[v.id] = v self.vertices_ids[v.id] = str(v.id) vname = str(v.id) v.name = vname
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -