示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
 def SetEmbedding(self, v, x, y):
     self.vertices[v].embedding = Point2D(x, y)
示例#5
0
    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)
示例#6
0
 def SetEmbedding(self, v, x, y):
     self.embedding[v] = Point2D(x, y)