def __init__(self, nodeIndex, itsHMM): self.itsHMM = itsHMM self.index = nodeIndex # The node index in the underlying graph self.id = ValidatingString("%s" % nodeIndex) self.state_class = PopupableInt() self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.label = ValidatingString("<none>") self.order = DefaultedInt() self.order.setDefault(1, 0) self.emissions = [] self.initial = Probability("0.0") self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1:'None', 0:'0', 1:'1', 2:'2'} name2code = {'None':-1, '0':0, '1':1, '2':2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)
def fromDiscreteState(self, id, pi, B, label, order, tiedto=None, background=False): """ Convert from ghmm.DiscreteEmissionHMM to the member attributes """ self.id = id self.index = self.itsHMM.G.AddVertex() self.initial = typed_assign(self.initial, float(pi)) self.label = type(self.label)(label) self.order = DefaultedInt(order) self.order = typed_assign(self.order, int(order)) self.order.useDefault = 0 self.emissions = B if tiedto is None: self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, tiedto) self.tiedto.useDefault = 0 if background is True: self.background = PopupableInt(-1) self.background.setPopup( self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)
def __init__(self): xmlutil.HMMState.__init__(self) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) self.state_class = PopupableInt(-1) self.state_class.setPopup(self.itsHMM.hmmClass.code2name, self.itsHMM.hmmClass.name2code, 10)
def __init__(self, nodeIndex, itsHMM): self.initial = Probability("0.0") self.label = ValidatingString("None") self.itsHMM = itsHMM # print type(self.label) self.index = nodeIndex # The node index in the underlying graph self.id = DefaultedInt() # identification by the canvas, not always the same self.state_class = PopupableInt(-1) self.state_class.setPopup(itsHMM.hmmClass.code2name, itsHMM.hmmClass.name2code, 10) # XXX self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.order = DefaultedInt() self.order.setDefault(1, 0) # the default state order is 0 self.emissions = [] self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1:'None', 0:'0', 1:'1', 2:'2'} name2code = {'None':-1, '0':0, '1':1, '2':2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) self.editableAttr = ['label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id'] self.xmlAttr = self.editableAttr + ['ngeom', 'emissions'] # pair HMM stuff self.alphabet_id = ValidatingInt(0) self.offsetX = ValidatingInt(1) self.offsetY = ValidatingInt(0) self.kclasses = ValidatingInt(1) self.transitionFunction = ValidatingInt(-1)
def __init__(self, nodeIndex, itsHMM): self.itsHMM = itsHMM self.index = nodeIndex # The node index in the underlying graph self.id = ValidatingString("%s" % nodeIndex) self.state_class = PopupableInt() self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.label = ValidatingString("<none>") self.order = DefaultedInt() self.order.setDefault(1, 0) self.emissions = [] self.initial = Probability("0.0") self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1: 'None', 0: '0', 1: '1', 2: '2'} name2code = {'None': -1, '0': 0, '1': 1, '2': 2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)
def __init__(self, nodeIndex, itsHMM): self.initial = Probability("0.0") self.label = ValidatingString("None") self.itsHMM = itsHMM # print type(self.label) self.index = nodeIndex # The node index in the underlying graph self.id = DefaultedInt( ) # identification by the canvas, not always the same self.state_class = PopupableInt(-1) self.state_class.setPopup(itsHMM.hmmClass.code2name, itsHMM.hmmClass.name2code, 10) # XXX self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.order = DefaultedInt() self.order.setDefault(1, 0) # the default state order is 0 self.emissions = [] self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1: 'None', 0: '0', 1: '1', 2: '2'} name2code = {'None': -1, '0': 0, '1': 1, '2': 2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) self.editableAttr = [ 'label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id' ] self.xmlAttr = self.editableAttr + ['ngeom', 'emissions'] # pair HMM stuff self.alphabet_id = ValidatingInt(0) self.offsetX = ValidatingInt(1) self.offsetY = ValidatingInt(0) self.kclasses = ValidatingInt(1) self.transitionFunction = ValidatingInt(-1)
def fromDiscreteState( self, id, pi, B, label, order, tiedto=None, background=False ): """ Convert from ghmm.DiscreteEmissionHMM to the member attributes """ self.id = id self.index = self.itsHMM.G.AddVertex() self.initial = typed_assign(self.initial, float(pi)) self.label = type(self.label)(label) self.order = DefaultedInt(order) self.order = typed_assign(self.order, int(order)) self.order.useDefault = 0 self.emissions = B if tiedto is None: self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, tiedto) self.tiedto.useDefault = 0 if background is True: self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)
class HMMState: def __init__(self, nodeIndex, itsHMM): self.initial = Probability("0.0") self.label = ValidatingString("None") self.itsHMM = itsHMM # print type(self.label) self.index = nodeIndex # The node index in the underlying graph self.id = DefaultedInt( ) # identification by the canvas, not always the same self.state_class = PopupableInt(-1) self.state_class.setPopup(itsHMM.hmmClass.code2name, itsHMM.hmmClass.name2code, 10) # XXX self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.order = DefaultedInt() self.order.setDefault(1, 0) # the default state order is 0 self.emissions = [] self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1: 'None', 0: '0', 1: '1', 2: '2'} name2code = {'None': -1, '0': 0, '1': 1, '2': 2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) self.editableAttr = [ 'label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id' ] self.xmlAttr = self.editableAttr + ['ngeom', 'emissions'] # pair HMM stuff self.alphabet_id = ValidatingInt(0) self.offsetX = ValidatingInt(1) self.offsetY = ValidatingInt(0) self.kclasses = ValidatingInt(1) self.transitionFunction = ValidatingInt(-1) editableAttr = [ 'label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id' ] xmlAttr = editableAttr + ['ngeom', 'emissions'] # ['id', 'state_class', 'label', 'order', 'initial', 'tiedto', 'reading_frame', 'duration', 'background'] # # Set_<Attribute> Methods: for integration with class editobj.Editor.set_value() # the name of the method must be in the form of set_<attr name>(self,value). # Otherwise, EditObj cannot propogate new values. (See the base method editobj.EntryEditor.set_value()) # When necessary, we also update the Graph here. # def set_label(self, value): # Get the id2index from the Graph oldv = self.itsHMM.id2index[self.id] self.label = typed_assign(self.label, value) # We only show the label out of the editable items self.itsHMM.G.labeling[oldv] = "%s\n%s" % (self.id, self.label ) # XXX Hack Aaaargh! self.itsHMM.itsEditor.UpdateVertexLabel(oldv, 0) def set_initial(self, value): self.initial = typed_assign(self.initial, value) def set_offsetX(self, value): self.offsetX = typed_assign(self.offsetX, value) def set_offsetY(self, value): self.offsetY = typed_assign(self.offsetY, value) def set_alphabet_id(self, value): self.alphabet_id = typed_assign(self.alphabet_id, value) def fromDOM(self, XMLNode): self.id = typed_assign(self.id, int( XMLNode.attributes['id'].nodeValue)) # state's id self.index = self.itsHMM.G.AddVertex() datas = XMLNode.getElementsByTagName("data") for data in datas: dataKey = data.attributes['key'].nodeValue dataValue = data.firstChild.nodeValue #print dataValue # if dataValue == None: # use default Value # self.state_class = typed_assign(self.state_class, int(0)) # else: # self.state_class = typed_assign(self.state_class, int(dataValue)) if dataKey == 'class': self.state_class = typed_assign(self.state_class, int(dataValue)) elif dataKey == 'label': self.label = type(self.label)(dataValue.encode( 'ascii', 'replace')) elif dataKey == 'order': if dataValue == None: # use default value self.order = typed_assign(self.order, self.order.defaultValue) self.order.useDefault = 1 else: self.order = typed_assign(self.order, int(dataValue)) self.order.useDefault = 0 elif dataKey == 'initial': self.initial = typed_assign(self.initial, float(dataValue)) elif dataKey == 'tiedto': if dataValue == None: # use default value self.tiedto = typed_assign(self.tiedto, self.tiedto.defaultValue) self.tiedto.useDefault = 1 else: self.tiedto = typed_assign( self.tiedto, dataValue.encode('ascii', 'replace')) self.tiedto.useDefault = 0 elif dataKey == 'reading-frame': self.reading_frame = typed_assign(self.reading_frame, int(dataValue)) elif dataKey == 'background': self.background = typed_assign( self.background, self.itsHMM.backgroundDistributions.name2code[dataValue]) elif dataKey == 'duration': self.duration = typed_assign(self.duration, int(dataValue)) self.duration.useDefault = 0 elif dataKey == 'ngeom': # We only use pos pos = XMLNode.getElementsByTagName('pos')[ 0] # Just one pos ... self.pos = Point2D(float(pos.attributes['x'].nodeValue), float(pos.attributes['y'].nodeValue)) elif dataKey == 'emissions': # collect all strings from childnodes dataValue = "" for child in data.childNodes: dataValue += child.nodeValue self.emissions = listFromCSV(dataValue, types.FloatType) #print self.emissions elif dataKey == 'alphabet_id': self.alphabet_id = ValidatingInt(dataValue) elif dataKey == 'offset_x': self.offsetX = ValidatingInt(dataValue) elif dataKey == 'offset_y': self.offsetY = ValidatingInt(dataValue) elif dataKey == 'kclasses': self.kclasses = ValidatingInt(dataValue) elif dataKey == 'transitionfunction': self.transitionFunction = ValidatingInt(dataValue) else: print "HMMState.fromDOM: unknown key %s of value %s" % ( dataKey, dataValue) def toDOM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("node") node.setAttribute('id', "%s" % self.id) # Mandatory elems writeData(XMLDoc, node, 'label', self.label) writeData(XMLDoc, node, 'class', self.state_class) writeData(XMLDoc, node, 'initial', self.initial / initial_sum) pos_elem = XMLDoc.createElement("pos") try: pos = self.itsHMM.G.embedding[self.index] pos_elem.setAttribute('x', "%s" % pos.x) pos_elem.setAttribute('y', "%s" % pos.y) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) except: pos_elem.setAttribute('x', "%s" % 100) pos_elem.setAttribute('y', "%s" % 50) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) writeData(XMLDoc, node, 'offset_x', self.offsetX) writeData(XMLDoc, node, 'offset_y', self.offsetY) writeData(XMLDoc, node, 'alphabet_id', self.alphabet_id) writeData(XMLDoc, node, 'kclasses', self.kclasses) writeData(XMLDoc, node, 'transitionfunction', self.transitionFunction) if not self.order.useDefault: writeData(XMLDoc, node, 'order', self.order) if self.reading_frame != -1: writeData(XMLDoc, node, 'reading-frame', self.reading_frame) if self.background != -1: writeData( XMLDoc, node, 'background', self.itsHMM.backgroundDistributions.code2name[self.background]) if not self.duration.useDefault: writeData(XMLDoc, node, 'duration', self.duration) if not self.tiedto == '': writeData(XMLDoc, node, 'tiedto', self.tiedto) self.emissions = self.itsHMM.state[self.itsHMM.id2index[int( self.tiedto)]].emissions # XXX if self.order.useDefault: order = 0 else: order = self.order # XXX Produce uniform emission probs, if we dont have the correct number of # parameters size = self.itsHMM.hmmAlphabets[self.alphabet_id].size()**(order + 1) if (self.itsHMM.modelType == "pairHMM"): if (self.offsetX != 0 and self.offsetY != 0): size = size**2 if len(self.emissions) != size: print "in state %s len(emissions) = %i size should be %i" % ( self.id, len(self.emissions), size) tmp = [1.0 / self.itsHMM.hmmAlphabets[self.alphabet_id].size() ] * self.itsHMM.hmmAlphabet.size() if order == 0: self.emissions = tmp else: self.emissions = tmp * self.itsHMM.hmmAlphabets[ self.alphabet_id].size()**order if order > 0: writeData( XMLDoc, node, 'emissions', csvFromList(self.emissions, self.itsHMM.hmmAlphabets[self.alphabet_id].size())) else: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions)) XMLNode.appendChild(node) def toGHMM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("state") node.setAttribute('id', "%s" % self.id) writeXMLTextNode(XMLDoc, node, 'initial', self.initial / initial_sum) # ignore order writeXMLTextNode(XMLDoc, node, 'emission', string.join(map(str, self.emissions), '\n')) XMLNode.appendChild(node) def fromDiscreteState(self, id, pi, B, label, order, tiedto=None, background=False): """ Convert from ghmm.DiscreteEmissionHMM to the member attributes """ self.id = id self.index = self.itsHMM.G.AddVertex() self.initial = typed_assign(self.initial, float(pi)) self.label = type(self.label)(label) self.order = DefaultedInt(order) self.order = typed_assign(self.order, int(order)) self.order.useDefault = 0 self.emissions = B if tiedto is None: self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, tiedto) self.tiedto.useDefault = 0 if background is True: self.background = PopupableInt(-1) self.background.setPopup( self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)
class HMMState: def __init__(self, nodeIndex, itsHMM): self.itsHMM = itsHMM self.index = nodeIndex # The node index in the underlying graph self.id = ValidatingString("%s" % nodeIndex) self.state_class = PopupableInt() self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.label = ValidatingString("<none>") self.order = DefaultedInt() self.order.setDefault(1, 0) self.emissions = [] self.initial = Probability("0.0") self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1:'None', 0:'0', 1:'1', 2:'2'} name2code = {'None':-1, '0':0, '1':1, '2':2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) editableAttr = ['id', 'state_class', 'label', 'order', 'initial', 'tiedto', 'reading_frame', 'duration', 'background'] xmlAttr = editableAttr + ['ngeom', 'emissions'] def fromDOM(self, XMLNode): self.id = ValidatingString(XMLNode.attributes['id'].nodeValue.encode('ascii', 'replace')) self.index = self.itsHMM.G.AddVertex() datas = XMLNode.getElementsByTagName("data") for data in datas: dataKey = data.attributes['key'].nodeValue dataValue = data.firstChild.nodeValue if dataKey == 'class': self.state_class = typed_assign(self.state_class, int(dataValue)) elif dataKey == 'label': self.label = type(self.label)(dataValue.encode('ascii', 'replace')) elif dataKey == 'order': if dataValue == None: # Use default value self.order = typed_assign(self.order, self.order.defaultValue) self.order.useDefault = 1 else: self.order = typed_assign(self.order, int(dataValue)) self.order.useDefault = 0 elif dataKey == 'initial': self.initial = typed_assign(self.initial, float(dataValue)) elif dataKey == 'tiedto': if dataValue == None: # Use default value self.tiedto = typed_assign(self.tiedto, self.tiedto.defaultValue) self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, dataValue.encode('ascii', 'replace')) self.tiedto.useDefault = 0 elif dataKey == 'reading-frame': self.reading_frame = typed_assign(self.reading_frame, int(dataValue)) elif dataKey == 'background': self.background = typed_assign(self.background, self.itsHMM.backgroundDistributions.name2code[dataValue]) elif dataKey == 'duration': self.duration = typed_assign(self.duration, int(dataValue)) self.duration.useDefault = 0 elif dataKey == 'ngeom': # We only use pos pos = XMLNode.getElementsByTagName('pos')[0] # Just one pos ... self.pos = Point2D(float(pos.attributes['x'].nodeValue), float(pos.attributes['y'].nodeValue)) elif dataKey == 'emissions': # collect all strings from childnodes dataValue = "" for child in data.childNodes: dataValue += child.nodeValue self.emissions = listFromCSV(dataValue, types.FloatType) #print self.emissions else: print "HMMState.fromDOM: unknown key %s of value %s" % (dataKey, dataValue) def toDOM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("node") node.setAttribute('id', "%s" % self.id) # Mandatory elems writeData(XMLDoc, node, 'label', self.label) writeData(XMLDoc, node, 'class', self.state_class) writeData(XMLDoc, node, 'initial', self.initial / initial_sum) pos = self.itsHMM.G.embedding[self.index] pos_elem = XMLDoc.createElement("pos") pos_elem.setAttribute('x', "%s" % pos.x) pos_elem.setAttribute('y', "%s" % pos.y) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) if not self.order.useDefault: writeData(XMLDoc, node, 'order', self.order) if self.reading_frame != -1: writeData(XMLDoc, node, 'reading-frame', self.reading_frame) if self.background != -1: writeData(XMLDoc, node, 'background', self.itsHMM.backgroundDistributions.code2name[self.background]) if not self.duration.useDefault: writeData(XMLDoc, node, 'duration', self.duration) if not self.tiedto == '': writeData(XMLDoc, node, 'tiedto', self.tiedto) else: if self.order.useDefault: order = 0 else: order = self.order # XXX Produce uniform emission probs, if we dont have the correct number of # parameters size = self.itsHMM.hmmAlphabet.size()**(order+1) if len(self.emissions) != size: tmp = [1.0/self.itsHMM.hmmAlphabet.size()] * self.itsHMM.hmmAlphabet.size() if order == 0: self.emissions = tmp else: self.emissions = tmp * self.itsHMM.hmmAlphabet.size()**order if order > 0: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions, self.itsHMM.hmmAlphabet.size())) else: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions)) XMLNode.appendChild(node)
class HMMState: def __init__(self, nodeIndex, itsHMM): self.itsHMM = itsHMM self.index = nodeIndex # The node index in the underlying graph self.id = ValidatingString("%s" % nodeIndex) self.state_class = PopupableInt() self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.label = ValidatingString("<none>") self.order = DefaultedInt() self.order.setDefault(1, 0) self.emissions = [] self.initial = Probability("0.0") self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1: 'None', 0: '0', 1: '1', 2: '2'} name2code = {'None': -1, '0': 0, '1': 1, '2': 2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) editableAttr = [ 'id', 'state_class', 'label', 'order', 'initial', 'tiedto', 'reading_frame', 'duration', 'background' ] xmlAttr = editableAttr + ['ngeom', 'emissions'] def fromDOM(self, XMLNode): self.id = ValidatingString(XMLNode.attributes['id'].nodeValue.encode( 'ascii', 'replace')) self.index = self.itsHMM.G.AddVertex() datas = XMLNode.getElementsByTagName("data") for data in datas: dataKey = data.attributes['key'].nodeValue dataValue = data.firstChild.nodeValue if dataKey == 'class': self.state_class = typed_assign(self.state_class, int(dataValue)) elif dataKey == 'label': self.label = type(self.label)(dataValue.encode( 'ascii', 'replace')) elif dataKey == 'order': if dataValue == None: # Use default value self.order = typed_assign(self.order, self.order.defaultValue) self.order.useDefault = 1 else: self.order = typed_assign(self.order, int(dataValue)) self.order.useDefault = 0 elif dataKey == 'initial': self.initial = typed_assign(self.initial, float(dataValue)) elif dataKey == 'tiedto': if dataValue == None: # Use default value self.tiedto = typed_assign(self.tiedto, self.tiedto.defaultValue) self.tiedto.useDefault = 1 else: self.tiedto = typed_assign( self.tiedto, dataValue.encode('ascii', 'replace')) self.tiedto.useDefault = 0 elif dataKey == 'reading-frame': self.reading_frame = typed_assign(self.reading_frame, int(dataValue)) elif dataKey == 'background': self.background = typed_assign( self.background, self.itsHMM.backgroundDistributions.name2code[dataValue]) elif dataKey == 'duration': self.duration = typed_assign(self.duration, int(dataValue)) self.duration.useDefault = 0 elif dataKey == 'ngeom': # We only use pos pos = XMLNode.getElementsByTagName('pos')[ 0] # Just one pos ... self.pos = Point2D(float(pos.attributes['x'].nodeValue), float(pos.attributes['y'].nodeValue)) elif dataKey == 'emissions': # collect all strings from childnodes dataValue = "" for child in data.childNodes: dataValue += child.nodeValue self.emissions = listFromCSV(dataValue, types.FloatType) #print self.emissions else: print "HMMState.fromDOM: unknown key %s of value %s" % ( dataKey, dataValue) def toDOM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("node") node.setAttribute('id', "%s" % self.id) # Mandatory elems writeData(XMLDoc, node, 'label', self.label) writeData(XMLDoc, node, 'class', self.state_class) writeData(XMLDoc, node, 'initial', self.initial / initial_sum) pos = self.itsHMM.G.embedding[self.index] pos_elem = XMLDoc.createElement("pos") pos_elem.setAttribute('x', "%s" % pos.x) pos_elem.setAttribute('y', "%s" % pos.y) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) if not self.order.useDefault: writeData(XMLDoc, node, 'order', self.order) if self.reading_frame != -1: writeData(XMLDoc, node, 'reading-frame', self.reading_frame) if self.background != -1: writeData( XMLDoc, node, 'background', self.itsHMM.backgroundDistributions.code2name[self.background]) if not self.duration.useDefault: writeData(XMLDoc, node, 'duration', self.duration) if not self.tiedto == '': writeData(XMLDoc, node, 'tiedto', self.tiedto) else: if self.order.useDefault: order = 0 else: order = self.order # XXX Produce uniform emission probs, if we dont have the correct number of # parameters size = self.itsHMM.hmmAlphabet.size()**(order + 1) if len(self.emissions) != size: tmp = [1.0 / self.itsHMM.hmmAlphabet.size() ] * self.itsHMM.hmmAlphabet.size() if order == 0: self.emissions = tmp else: self.emissions = tmp * self.itsHMM.hmmAlphabet.size( )**order if order > 0: writeData( XMLDoc, node, 'emissions', csvFromList(self.emissions, self.itsHMM.hmmAlphabet.size())) else: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions)) XMLNode.appendChild(node)
class HMMState: def __init__(self, nodeIndex, itsHMM): self.initial = Probability("0.0") self.label = ValidatingString("None") self.itsHMM = itsHMM # print type(self.label) self.index = nodeIndex # The node index in the underlying graph self.id = DefaultedInt() # identification by the canvas, not always the same self.state_class = PopupableInt(-1) self.state_class.setPopup(itsHMM.hmmClass.code2name, itsHMM.hmmClass.name2code, 10) # XXX self.state_class.setPopup(itsHMM.hmmClass.name, itsHMM.hmmClass.name2code, 10) self.order = DefaultedInt() self.order.setDefault(1, 0) # the default state order is 0 self.emissions = [] self.tiedto = DefaultedString() self.tiedto.setDefault(1, '') self.desc = self.id self.reading_frame = PopupableInt(-1) code2name = {-1:'None', 0:'0', 1:'1', 2:'2'} name2code = {'None':-1, '0':0, '1':1, '2':2} self.reading_frame.setPopup(code2name, name2code, 4) self.duration = DefaultedInt() self.duration.setDefault(1, 0) self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10) self.editableAttr = ['label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id'] self.xmlAttr = self.editableAttr + ['ngeom', 'emissions'] # pair HMM stuff self.alphabet_id = ValidatingInt(0) self.offsetX = ValidatingInt(1) self.offsetY = ValidatingInt(0) self.kclasses = ValidatingInt(1) self.transitionFunction = ValidatingInt(-1) editableAttr = ['label', 'state_class', 'initial', 'order', 'background', 'offsetX', 'offsetY', 'alphabet_id'] xmlAttr = editableAttr + ['ngeom', 'emissions'] # ['id', 'state_class', 'label', 'order', 'initial', 'tiedto', 'reading_frame', 'duration', 'background'] # # Set_<Attribute> Methods: for integration with class editobj.Editor.set_value() # the name of the method must be in the form of set_<attr name>(self,value). # Otherwise, EditObj cannot propogate new values. (See the base method editobj.EntryEditor.set_value()) # When necessary, we also update the Graph here. # def set_label(self, value): # Get the id2index from the Graph oldv = self.itsHMM.id2index[self.id] self.label = typed_assign(self.label, value) # We only show the label out of the editable items self.itsHMM.G.labeling[oldv] = "%s\n%s" % (self.id, self.label) # XXX Hack Aaaargh! self.itsHMM.itsEditor.UpdateVertexLabel(oldv, 0) def set_initial(self, value): self.initial = typed_assign(self.initial, value) def set_offsetX(self, value): self.offsetX = typed_assign(self.offsetX, value) def set_offsetY(self, value): self.offsetY = typed_assign(self.offsetY, value) def set_alphabet_id(self, value): self.alphabet_id = typed_assign(self.alphabet_id, value) def fromDOM(self, XMLNode): self.id = typed_assign(self.id, int(XMLNode.attributes['id'].nodeValue)) # state's id self.index = self.itsHMM.G.AddVertex() datas = XMLNode.getElementsByTagName("data") for data in datas: dataKey = data.attributes['key'].nodeValue dataValue = data.firstChild.nodeValue #print dataValue # if dataValue == None: # use default Value # self.state_class = typed_assign(self.state_class, int(0)) # else: # self.state_class = typed_assign(self.state_class, int(dataValue)) if dataKey == 'class': self.state_class = typed_assign(self.state_class, int(dataValue)) elif dataKey == 'label': self.label = type(self.label)(dataValue.encode('ascii', 'replace')) elif dataKey == 'order': if dataValue == None: # use default value self.order = typed_assign(self.order, self.order.defaultValue) self.order.useDefault = 1 else: self.order = typed_assign(self.order, int(dataValue)) self.order.useDefault = 0 elif dataKey == 'initial': self.initial = typed_assign(self.initial, float(dataValue)) elif dataKey == 'tiedto': if dataValue == None: # use default value self.tiedto = typed_assign(self.tiedto, self.tiedto.defaultValue) self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, dataValue.encode('ascii', 'replace')) self.tiedto.useDefault = 0 elif dataKey == 'reading-frame': self.reading_frame = typed_assign(self.reading_frame, int(dataValue)) elif dataKey == 'background': self.background = typed_assign(self.background, self.itsHMM.backgroundDistributions.name2code[dataValue]) elif dataKey == 'duration': self.duration = typed_assign(self.duration, int(dataValue)) self.duration.useDefault = 0 elif dataKey == 'ngeom': # We only use pos pos = XMLNode.getElementsByTagName('pos')[0] # Just one pos ... self.pos = Point2D(float(pos.attributes['x'].nodeValue), float(pos.attributes['y'].nodeValue)) elif dataKey == 'emissions': # collect all strings from childnodes dataValue = "" for child in data.childNodes: dataValue += child.nodeValue self.emissions = listFromCSV(dataValue, types.FloatType) #print self.emissions elif dataKey == 'alphabet_id': self.alphabet_id = ValidatingInt(dataValue) elif dataKey == 'offset_x': self.offsetX = ValidatingInt(dataValue) elif dataKey == 'offset_y': self.offsetY = ValidatingInt(dataValue) elif dataKey == 'kclasses': self.kclasses = ValidatingInt(dataValue) elif dataKey == 'transitionfunction': self.transitionFunction = ValidatingInt(dataValue) else: print "HMMState.fromDOM: unknown key %s of value %s" % (dataKey, dataValue) def toDOM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("node") node.setAttribute('id', "%s" % self.id) # Mandatory elems writeData(XMLDoc, node, 'label', self.label) writeData(XMLDoc, node, 'class', self.state_class) writeData(XMLDoc, node, 'initial', self.initial / initial_sum) pos_elem = XMLDoc.createElement("pos") try: pos = self.itsHMM.G.embedding[self.index] pos_elem.setAttribute('x', "%s" % pos.x) pos_elem.setAttribute('y', "%s" % pos.y) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) except: pos_elem.setAttribute('x', "%s" % 100) pos_elem.setAttribute('y', "%s" % 50) writeXMLData(XMLDoc, node, 'ngeom', pos_elem) writeData(XMLDoc, node, 'offset_x', self.offsetX) writeData(XMLDoc, node, 'offset_y', self.offsetY) writeData(XMLDoc, node, 'alphabet_id', self.alphabet_id) writeData(XMLDoc, node, 'kclasses', self.kclasses) writeData(XMLDoc, node, 'transitionfunction', self.transitionFunction) if not self.order.useDefault: writeData(XMLDoc, node, 'order', self.order) if self.reading_frame != -1: writeData(XMLDoc, node, 'reading-frame', self.reading_frame) if self.background != -1: writeData(XMLDoc, node, 'background', self.itsHMM.backgroundDistributions.code2name[self.background]) if not self.duration.useDefault: writeData(XMLDoc, node, 'duration', self.duration) if not self.tiedto == '': writeData(XMLDoc, node, 'tiedto', self.tiedto) self.emissions = self.itsHMM.state[self.itsHMM.id2index[int(self.tiedto)]].emissions # XXX if self.order.useDefault: order = 0 else: order = self.order # XXX Produce uniform emission probs, if we dont have the correct number of # parameters size = self.itsHMM.hmmAlphabets[self.alphabet_id].size()**(order+1) if (self.itsHMM.modelType == "pairHMM"): if (self.offsetX != 0 and self.offsetY != 0): size = size**2 if len(self.emissions) != size: print "in state %s len(emissions) = %i size should be %i" % (self.id, len(self.emissions), size) tmp = [1.0/self.itsHMM.hmmAlphabets[self.alphabet_id].size()] * self.itsHMM.hmmAlphabet.size() if order == 0: self.emissions = tmp else: self.emissions = tmp * self.itsHMM.hmmAlphabets[self.alphabet_id].size()**order if order > 0: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions, self.itsHMM.hmmAlphabets[self.alphabet_id].size())) else: writeData(XMLDoc, node, 'emissions', csvFromList(self.emissions)) XMLNode.appendChild(node) def toGHMM(self, XMLDoc, XMLNode, initial_sum): node = XMLDoc.createElement("state") node.setAttribute('id', "%s" % self.id) writeXMLTextNode(XMLDoc, node, 'initial', self.initial / initial_sum) # ignore order writeXMLTextNode(XMLDoc, node, 'emission', string.join(map(str,self.emissions),'\n')) XMLNode.appendChild(node) def fromDiscreteState( self, id, pi, B, label, order, tiedto=None, background=False ): """ Convert from ghmm.DiscreteEmissionHMM to the member attributes """ self.id = id self.index = self.itsHMM.G.AddVertex() self.initial = typed_assign(self.initial, float(pi)) self.label = type(self.label)(label) self.order = DefaultedInt(order) self.order = typed_assign(self.order, int(order)) self.order.useDefault = 0 self.emissions = B if tiedto is None: self.tiedto.useDefault = 1 else: self.tiedto = typed_assign(self.tiedto, tiedto) self.tiedto.useDefault = 0 if background is True: self.background = PopupableInt(-1) self.background.setPopup(self.itsHMM.backgroundDistributions.code2name, self.itsHMM.backgroundDistributions.name2code, 10)