def ChangeEdgeWeights(self): if self.G == None: return n = self.edgeWeightVar.get() k = self.G.edgeWeights.keys() if self.G.edgeWeights[0].QInteger(): initialWeight = 0 else: initialWeight = 0.0 if n == 1 or n == 2: if 2 in k: del (self.G.edgeWeights[2]) else: if 2 not in k: self.G.edgeWeights[2] = EdgeWeight(self.G, initialWeight) if self.G.edgeWeights[0].QInteger(): self.G.edgeWeights[2].Integerize() if n == 1: if 1 in k: del (self.G.edgeWeights[1]) else: if 1 not in k: self.G.edgeWeights[1] = EdgeWeight(self.G, initialWeight) if self.G.edgeWeights[0].QInteger(): self.G.edgeWeights[1].Integerize()
def Clear(self): """ Delete all vertices and edges from the subgraph. """ self.vertices = [] self.adjLists = {} self.invAdjLists = {} # Inverse Adjazenzlisten self.size = 0 self.totalWeight = 0 self.edgeWeights = {} # Dictionary of edge labellings self.edgeWeights[0] = EdgeWeight(self) self.vertexWeights.clear() # None by default self.highVertexID = 0 # INTERNAL
def OpenDotGraph(fileName): """ Reads in a graph from file fileName. File-format is supposed to be dot (*.dot) used in """ G = Graph.Graph() G.directed = 1 E = VertexLabeling() W = EdgeWeight(G) L = VertexLabeling() VLabel = VertexLabeling() ELabel = EdgeLabeling() import re file = open(fileName, 'r') lines = file.readlines() file.close() dot2graph = {} for l in lines[3:]: items = string.split(l) if len(items) < 2: break if items[1] != '->': v = G.AddVertex() dot_v = int(items[0]) L[v] = "%d" % dot_v dot2graph[dot_v] = v m = re.search('label=("[^"]+")', l) VLabel[v] = m.group(1)[1:-1] m = re.search('pos="(\d+),(\d+)"', l) x = int(m.group(1)) y = int(m.group(2)) E[v] = Point2D(x,y) else: m = re.search('(\d+) -> (\d+)', l) v = dot2graph[int(m.group(1))] w = dot2graph[int(m.group(2))] m = re.search('label=("[^"]+")', l) #print l #print v,w,m.group(1) G.AddEdge(v,w) weight = float(m.group(1)[1:-1]) W[(v,w)] = weight ELabel[(v,w)] = "%0.2f" % weight G.embedding = E G.labeling = L G.nrEdgeWeights = 1 G.edgeWeights[0] = W G.vertexAnnotation = VLabel G.edgeAnnotation = ELabel return G
def __init__(self, vertexClass, edgeClass): self.vertexClass = vertexClass self.edgeClass = edgeClass self.vertices = {} self.edges = {} self.highVertexID = 0 # INTERNAL self.simple = 1 self.euclidian = 1 self.directed = 0 self.edgeWeights = {} # Dictionary of edge labellings self.edgeWeights[0] = EdgeWeight(self) self.vertexWeights = {} # None by default self.size = 0 self.edgeWidth = None self.vertexAnnotation = None self.edgeAnnotation = None self.properties = {}
def __init__(self): self.simple = 1 self.euclidian = 1 self.directed = 0 self.vertices = [] self.adjLists = {} self.invAdjLists = {} # Inverse Adjazenzlisten self.highVertexID = 0 # INTERNAL self.embedding = VertexLabeling() # 2D-Positions self.labeling = VertexLabeling() # Names of vertices self.edgeWeights = {} # Dictionary of edge labellings self.edgeWeights[0] = EdgeWeight(self) self.vertexWeights = {} # None by default self.size = 0 self.edgeWidth = None self.vertexAnnotation = None self.edgeAnnotation = None self.properties = {}
def OpenGMLGraph(fileName): """ Reads in a graph from file fileName. File-format is supposed to be GML (*.gml) """ G = Graph.Graph() G.directed = 0 E = VertexLabeling() W = EdgeWeight(G) L = VertexLabeling() VLabel = VertexLabeling() ELabel = EdgeLabeling() file = open(fileName, 'r') g = ParseGML(file) file.close() if g[0][0] != 'graph': log.error("Serious format error in %s. first key is not graph" % fileName) return else: l = g[0][1] for i in xrange(len(l)): key = l[i][0] value = l[i][1] if key == 'node': d = PairListToDictionary(value) v = G.AddVertex() try: VLabel[v] = eval(d['label']) P = PairListToDictionary(d['graphics']) E[v] = Point2D(eval(P['x']), eval(P['y'])) except: d = None P = None elif key == 'edge': d = PairListToDictionary(value) try: s = eval(d['source']) t = eval(d['target']) G.AddEdge(s,t) ELabel[(s,t)] = eval(d['label']) W[(s,t)] = 0 except: d = None elif key == 'directed': G.directed = 1 for v in G.vertices: L[v] = v G.embedding = E G.labeling = L G.nrEdgeWeights = 1 G.edgeWeights[0] = W G.vertexAnnotation = VLabel G.edgeAnnotation = ELabel return G
def OpenCATBoxGraph(_file): """ Reads in a graph from file fileName. File-format is supposed to be from old CATBOX++ (*.cat) """ G = Graph.Graph() E = VertexLabeling() W = EdgeWeight(G) L = VertexLabeling() # get file from name or file object graphFile=None if type(_file) in types.StringTypes: graphFile = open(_file, 'r') elif type(_file)==types.FileType or issubclass(_file.__class__,StringIO.StringIO): graphFile=_file else: raise Exception("got wrong argument") lineNr = 1 firstVertexLineNr = -1 lastVertexLineNr = -1 firstEdgeLineNr = -1 lastEdgeLineNr = -1 intWeights = 0 while 1: line = graphFile.readline() if not line: break if lineNr == 2: # Read directed and euclidian splitLine = split(line[:-1],';') G.directed = eval(split(splitLine[0],':')[1]) G.simple = eval(split(splitLine[1],':')[1]) G.euclidian = eval(split(splitLine[2],':')[1]) intWeights = eval(split(splitLine[3],':')[1]) nrOfEdgeWeights = eval(split(splitLine[4],':')[1]) nrOfVertexWeights = eval(split(splitLine[5],':')[1]) for i in xrange(nrOfEdgeWeights): G.edgeWeights[i] = EdgeWeight(G) for i in xrange(nrOfVertexWeights): G.vertexWeights[i] = VertexWeight(G) if lineNr == 5: # Read nr of vertices nrOfVertices = eval(split(line[:-2],':')[1]) # Strip of "\n" and ; firstVertexLineNr = lineNr + 1 lastVertexLineNr = lineNr + nrOfVertices if firstVertexLineNr <= lineNr and lineNr <= lastVertexLineNr: splitLine = split(line[:-1],';') v = G.AddVertex() x = eval(split(splitLine[1],':')[1]) y = eval(split(splitLine[2],':')[1]) for i in xrange(nrOfVertexWeights): w = eval(split(splitLine[3+i],':')[1]) G.vertexWeights[i][v] = w E[v] = Point2D(x,y) if lineNr == lastVertexLineNr + 1: # Read Nr of edges nrOfEdges = eval(split(line[:-2],':')[1]) # Strip of "\n" and ; firstEdgeLineNr = lineNr + 1 lastEdgeLineNr = lineNr + nrOfEdges if firstEdgeLineNr <= lineNr and lineNr <= lastEdgeLineNr: splitLine = split(line[:-1],';') h = eval(split(splitLine[0],':')[1]) t = eval(split(splitLine[1],':')[1]) G.AddEdge(t,h,False) for i in xrange(nrOfEdgeWeights): G.edgeWeights[i][(t,h)] = eval(split(splitLine[3+i],':')[1]) lineNr = lineNr + 1 graphFile.close() for v in G.vertices: L[v] = v G.embedding = E G.labeling = L if intWeights: G.Integerize('all') for i in xrange(nrOfVertexWeights): G.vertexWeights[i].Integerize() return G
def fromDOM(self, XMLNode): # self.hmmClass.fromDOM(XMLNode.getElementsByTagName("hmm:class")[0]) for tag in XMLNode.getElementsByTagName("hmm:class"): self.hmmClass.fromDOM(tag) nameNodes = XMLNode.getElementsByTagName("hmm:name") if (len(nameNodes) > 0): self.modelType = nameNodes[0].firstChild.nodeValue # model type node modelTypeNodes = XMLNode.getElementsByTagName("hmm:modeltype") if (len(modelTypeNodes) > 0): self.modelType = modelTypeNodes[0].firstChild.nodeValue if (self.modelType == "pairHMM"): alphabetNodes = XMLNode.getElementsByTagName("hmm:alphabet") for alphabetNode in alphabetNodes: alphabet = DiscreteHMMAlphabet() alphabet.fromDOM(alphabetNode) self.hmmAlphabets[alphabet.id] = alphabet transitionFunctionNodes = XMLNode.getElementsByTagName( "hmm:transitionfunction") for transitionFunctionNode in transitionFunctionNodes: transitionFunction = TransitionFunction() transitionFunction.fromDom(transitionFunctionNode) self.transitionFunctions[ transitionFunction.id] = transitionFunction else: # If it is no pair hmm One "hmm:alphabet" XML element self.hmmAlphabets[0] = DiscreteHMMAlphabet() self.hmmAlphabets[0].fromDOM( XMLNode.getElementsByTagName("hmm:alphabet")[0]) self.backgroundDistributions.fromDOM(XMLNode) nodes = XMLNode.getElementsByTagName("node") for n in nodes: state = HMMState(-1, self) state.fromDOM(n) self.state[state.index] = state # key must be string self.id2index[state.id] = state.index self.G.embedding[state.index] = state.pos self.G.labeling[state.index] = "%s\n%s" % (state.id, state.label ) # XXX Hack Aaaargh! edges = XMLNode.getElementsByTagName("edge") # nr_classes = int(self.hmmClass.high()-self.hmmClass.low())+1 nr_classes = 1 # search in all states for the maximal kclasses for s in self.state.values(): if (s.kclasses > nr_classes): nr_classes = s.kclasses for i in range(nr_classes): self.G.edgeWeights[i] = EdgeWeight(self.G) for edge in edges: i = self.id2index[int(edge.attributes['source'].nodeValue)] j = self.id2index[int(edge.attributes['target'].nodeValue)] source = self.state[i] datas = edge.getElementsByTagName("data") for data in datas: dataKey = data.attributes['key'].nodeValue # dataValue = data.firstChild.nodeValue if dataKey == 'prob': #p = float(dataValue) # collect all strings from childnodes dataValue = "" for child in data.childNodes: dataValue += child.nodeValue p = listFromCSV(dataValue, types.FloatType) self.G.AddEdge(i, j) if len(p) == 1: # only one class for cl in range(source.kclasses - 1): p.append(0.0) for cl in range(source.kclasses): self.G.edgeWeights[cl][(i, j)] = p[cl]