コード例 #1
0
def test_graph_io():
    g = create_graph()

    fname = tempfile.mktemp()
    parser = GraphMLParser()
    parser.write(g, fname)

    with open(fname) as f:
        print(f.read())

    parser = GraphMLParser()
    g = parser.parse(fname)

    assert len(g.nodes()) == 5
    assert len(g.edges()) == 4
コード例 #2
0
def leeGraphML(file):
    '''
    Lee un grafo en formato graphML usando la libreria pygraphMl, 
    y lo devuelve como lista con pesos
    '''

    parser = GraphMLParser()
    g = parser.parse(file)

    V = []
    E = []

    for node in g.nodes():
        V.append(node['label'])

    for e in g.edges():
        source = e.node1
        target = e.node2
        try:
            peso = float(e['d1'])
        except:
            peso = None
        E.append((source['label'], target['label'], peso))

    return (V, E)
コード例 #3
0
def createGraphML():
    global g
    g = Graph()
    c.execute('select uid from userdata')
    dataList = c.fetchall()
    gnodes = []
    edges = []
    for i in dataList:
        i = str(i)
        i = i.replace("(", "").replace(",)", "").replace("L", "")
        i = int(i)
        c.execute('select distinct low from graphdata where high=?', (i, ))
        relate = c.fetchall()
        if not i in gnodes:
            g.add_node(i)
            gnodes.append(i)

        for e in relate:
            e = str(e)
            e = e.replace("(", "").replace(",)", "").replace("L", "")
            e = int(e)
            if not e in gnodes:
                g.add_node(e)
                gnodes.append(e)

# 		 	edges.append(i)
# 		 	edges.append(e)
#1 		 	if edges2.count(e) > 1:
            g.add_edge_by_label(str(i), str(e))

    parser = GraphMLParser()
    parser.write(g, "myGraph.graphml")
コード例 #4
0
ファイル: converter.py プロジェクト: joanPlepi/rdf_to_pg
    def __write_graphML(self):
        """Writes the .graphml file.
        
        Uses class Graph from pygraphml library to create the graph. 
        Also it uses class GraphMLParser from pygraphml library to write the file.
        """
        gr = Graph()

        # Adding nodes in the graph with properties.
        for sub in self.nodes_prop.keys():
            n = gr.add_node(sub)
            for pair_prop in self.nodes_prop[sub]:
                n[pair_prop[0]] = pair_prop[1]

        # Adding nodes in the graph without properties.
        for node in self.nodes.values():
            if node not in self.nodes_prop.keys():
                gr.add_node(node)

        # Checking the relations between nodes and creating respective edges.
        for relation in self.relations:
            source = self.nodes[relation[0]]
            target = self.nodes[relation[2]]
            edge = gr.add_edge_by_label(source, target)
            edge.set_directed(True)
            edge['model'] = relation[1]

        # Writting the file.
        parser = GraphMLParser()
        file_name = self.file_name.split(".")
        file_name = file_name[0]

        parser.write(gr, file_name + ".graphml")
        print("File  " + file_name + ".graphml is succesfully written")
コード例 #5
0
    def fromGraph(cls, rs, args):
        session = Session()
        file, cpu = args
        parser = GraphMLParser()

        g = parser.parse(os.path.join(DATA_FOLDER, file))
        nodes = [Node(name=str(n.id), cpu_capacity=cpu) for n in g.nodes()]
        nodes_from_g = {str(n.id): n for n in g.nodes()}
        session.add_all(nodes)
        session.flush()

        edges = [Edge
                 (node_1=session.query(Node).filter(Node.name == str(e.node1.id)).one(),
                  node_2=session.query(Node).filter(Node.name == str(e.node2.id)).one(),
                  bandwidth=float(e.attributes()["d42"].value),
                  delay=get_delay(nodes_from_g[str(e.node1.id)], nodes_from_g[str(e.node2.id)])
                  )

                 for e in g.edges() if
                 "d42" in e.attributes()
                 and isOK(nodes_from_g[str(e.node1.id)], nodes_from_g[str(e.node2.id)])
                 ]
        session.add_all(edges)
        session.flush()

        # filter out nodes for which we have edges
        valid_nodes = list(set([e.node_1.name for e in edges] + [e.node_2.name for e in edges]))
        nodes = list(set([n for n in nodes if str(n.name) in valid_nodes]))

        session.add_all(nodes)
        session.flush()
        session.add_all(edges)
        session.flush()

        return cls(edges, nodes)
コード例 #6
0
    def save_graph(self, file_name):
        """
        Save graph to .graphml file

        @param file_name : name of the file to which graph will be saved
        """
        parser = GraphMLParser()
        parser.write(self.g, file_name)
コード例 #7
0
def readGraphMLFile(graphMLFile):
    graphMLParser = GraphMLParser()
    try:
        graphNet = graphMLParser.parse(graphMLFile)
    except xml.parsers.expat.ExpatError as e:
        print("%s is not a GraphML file\n" % graphMLFile)
        print("Error message is %s" % str(e))
        sys.exit(-1)
    return graphNet
コード例 #8
0
 def from_graphml(fname: str) -> Graph:
     parser = GraphMLParser()
     gml = parser.parse(fname)
     g = Graph()
     for node in gml._nodes:
         g.adj[Node(id=node.id)]
     for edge in gml._edges:
         g.adj[Node(id=edge.node1.id)].append(Node(id=edge.node2.id))
     return g
コード例 #9
0
def generate_chain(screen_name, store_graph):

    fh = open("downloaded/%s/tweets.txt" % screen_name, "r")

    chain = {}

    g = Graph()
    nodes = {}

    def generate_trigram(words):
        if len(words) < 3:
            return
        for i in range(len(words) - 2):
            yield (words[i], words[i + 1], words[i + 2])

            if ((words[i], words[i + 1]) in nodes):
                if ((words[i + 2]) in nodes):
                    g.add_edge(nodes[(words[i], words[i + 1])],
                               nodes[(words[i + 2])])
                else:
                    nodes[(words[i + 2])] = g.add_node(words[i + 2])
                    g.add_edge(nodes[(words[i], words[i + 1])],
                               nodes[(words[i + 2])])
            else:
                nodes[(words[i],
                       words[i + 1])] = g.add_node(words[i] + words[i + 1])
                if ((words[i + 2]) in nodes):
                    g.add_edge(nodes[(words[i], words[i + 1])],
                               nodes[(words[i + 2])])
                else:
                    nodes[(words[i + 2])] = g.add_node(words[i + 2])
                    g.add_edge(nodes[(words[i], words[i + 1])],
                               nodes[(words[i + 2])])

    for line in fh.readlines():
        words = line.split()
        for word1, word2, word3 in generate_trigram(words):
            key = (word1, word2)
            if key in chain:
                chain[key].append(word3)
            else:
                chain[key] = [word3]

    if (store_graph):
        parser = GraphMLParser()
        parser.write(g, "downloaded/%s/graph.graphml" % screen_name)

    pickle.dump(chain, open("downloaded/%s/chain.p" % screen_name, "wb"))
コード例 #10
0
def writeToGraphml(pages, fileName):
    graph = Graph()
    nodes = []
    # creating nodes
    for page in pages:
        node = graph.add_node(page.id)
        node['title'] = page.title
        node['url'] = page.url
        if not page.mainCategory is None:
            node['main_category'] = page.mainCategory.title
        nodes.append(node)
    # creating edges
    for page in pages:
        # if there are edges
        if page.linksTo:
            for pageId in page.linksTo:
                e = graph.add_edge(nodes[page.id], nodes[pageId])
                e.set_directed(True)
    parser = GraphMLParser()
    parser.write(graph, fileName)
コード例 #11
0
from pygraphml.GraphMLParser import *
from pygraphml.Graph import *
from pygraphml.Node import *
from pygraphml.Edge import *

import sys

# parser = GraphMLParser()
# g = parser.parse(sys.argv[1])

# root = g.set_root_by_attribute("RootNode")
# #print g.root()

# for n in g.DFS_prefix():
#     print n

# #g.show(True)

g = Graph()

n1 = g.add_node("salut")
n2 = g.add_node("coucou")
n1['positionX'] = 555

g.add_edge(n1, n2)

parser = GraphMLParser()
parser.write(g, "ttest.graphml")

#g.show()
コード例 #12
0
ファイル: GraphMLExporter.py プロジェクト: hadim/lsysdrawer
    def write(self, ):
        """
        """

        parser = GraphMLParser()
        parser.write(self.graph, "graph.graphml")
コード例 #13
0
def open_file(file):
    parser = GraphMLParser()
    graph = parser.parse(file)
    #graph.show()
    return graph
コード例 #14
0
    def displayGroundTruth(self,agent=WORLD,x0=0,y0=0,maxRows=10,recursive=False,selfCycle=False):
        if agent == WORLD:
            self.clear()
            if __graph__:
                self.xml = Graph()
            else:
                self.xml = None
        
        x = x0
        y = y0
        if agent == WORLD:
            if not self.graph:
                self.graph = graph.DependencyGraph(self.world)
                self.graph.computeGraph()
            g = self.graph
            state = self.world.state
        else:
            g = self.graph = graph.DependencyGraph(self.world)
            state = self.world.agents[agent].getBelief()
            assert len(state) == 1
            g.computeGraph(state=next(iter(state.values())),belief=True)
        layout = getLayout(g)
        if agent == WORLD:
            # Lay out the action nodes
            x = self.drawActionNodes(layout['action'],x,y,maxRows)
            xPostAction = x
            believer = None
            xkey = 'xpost'
            ykey = 'ypost'
        else:
            believer = agent
            xkey = beliefKey(believer,'xpost')
            ykey = beliefKey(believer,'ypost')
        # Lay out the post variable nodes
        x = self.drawStateNodes(layout['state post'],g,x,y,xkey,ykey,believer,maxRows)
        # Lay out the observation nodes
        if agent == WORLD:
            x = self.drawObservationNodes(x,0,self.graph,xkey,ykey)
        # Lay out the utility nodes
        if agent == WORLD:
            if recursive:
                uNodes = [a.name for a in self.world.agents.values() \
                          if a.getAttribute('beliefs','%s0' % (a.name)) is True]
            else:
                uNodes = self.world.agents.keys()
        else:
            uNodes = [agent]
        x = self.drawUtilityNodes(x,y,g,uNodes)
        if agent == WORLD:
            # Draw links from utility back to actions
            for name in self.world.agents:
                if recursive and \
                   self.world.agents[name].getAttribute('beliefs','%s0' % (name)) is not True:
                    y += (maxRows+1) * self.rowHeight
                    self.displayGroundTruth(name,xPostAction,y,maxRows=maxRows,recursive=recursive)
                if name in g:
                    actions = self.world.agents[name].actions
                    for action in actions:
                        if action in g:
                            if gtnodes and str(action) not in gtnodes:
                                continue
                            self.drawEdge(name,action,g)
            self.colorNodes()
        # Draw links, reusing post nodes as pre nodes
        for key,entry in g.items():
            if isStateKey(key) or isBinaryKey(key):
                if not isFuture(key):
                    key = makeFuture(key)
                if agent != WORLD:
                    key = beliefKey(agent,key)
            elif agent != WORLD:
                continue
            if gtnodes:
                if (isFuture(key) and makePresent(key) not in gtnodes) or (not isFuture(key) and str(key) not in gtnodes):
                    if not isBeliefKey(key):
                        continue
            for child in entry['children']:
                if agent != WORLD and child in self.world.agents and not child in uNodes:
                    continue
                if (isStateKey(child) or isBinaryKey(child)) and agent != WORLD:
                    if isBinaryKey(child) or state2agent(child) == WORLD or \
                       state2feature(child) not in self.world.agents[state2agent(child)].omega:
                        child = beliefKey(agent,child)
                elif agent != WORLD and not child in uNodes:
                    continue
                if child in self.world.agents and not child in uNodes:
                    continue
                if gtnodes and makePresent(child) not in gtnodes:
                    continue
                if selfCycle or key != child:
                    self.drawEdge(key,child,g)
        x += self.colWidth
        if recursive:
            rect = QRectF(-self.colWidth/2,y0-self.rowHeight/2,
                          x,(float(maxRows)+.5)*self.rowHeight)
            self.agents[agent] = {'box': QGraphicsRectItem(rect)}
            self.agents[agent]['box'].setPen(QPen(QBrush(QColor('black')),3))
            self.agents[agent]['box'].setZValue(0.)
            if agent != WORLD:
                self.agents[agent]['text'] = QGraphicsTextItem(self.agents[agent]['box'])
                doc = QTextDocument(agent,self.agents[agent]['text'])
                self.agents[agent]['text'].setPos(rect.x(),rect.y())
                self.agents[agent]['text'].setTextWidth(rect.width())
                self.agents[agent]['text'].setDocument(doc)
            if agent != WORLD:
                color = self.world.diagram.getColor(agent)
                color.setAlpha(128)
                self.agents[agent]['box'].setBrush(QBrush(QColor(color)))
            self.addItem(self.agents[agent]['box'])

        if agent == WORLD:
            for observer in self.world.agents.values():
                if observer.O is not True:
                    for omega,table in observer.O.items():
                        if gtnodes and omega not in gtnodes:
                            continue
                        if self.xml:
                            for oNode in self.xml.nodes():
                                if oNode['label'] == omega:
                                    break
                            else:
                                raise ValueError('Unable to find node for %s' % (omega))
                        for action,tree in table.items():
                            if action is not None:
                                if self.xml and (len(gtnodes) == 0 or str(action) in gtnodes):
                                    for aNode in self.xml.nodes():
                                        if aNode['label'] == str(action):
                                            break
                                    else:
                                        raise ValueError('Unable to find node for %s' % (action))
                                    self.xml.add_edge(aNode,oNode,True)
                            for key in tree.getKeysIn():
                                if key != CONSTANT:
                                    if self.xml:
                                        for sNode in self.xml.nodes():
                                            if sNode['label'] == key:
                                                break
                                        else:
                                            raise ValueError('Unable to find node for %s' % (key))
                                        self.xml.add_edge(sNode,oNode,True)
                                        label = '%sBeliefOf%s' % (observer.name,key)
                                        bNode = self.getGraphNode(label)
                                        self.xml.add_edge(oNode,bNode,True)
                                    if recursive:
                                        belief = beliefKey(observer.name,makeFuture(key))
                                        self.drawEdge(omega,belief)
            for name in self.world.agents:
                # Draw links from non-belief reward components
                model = '%s0' % (name)
                R = self.world.agents[name].getReward(model)
                if R:
                    for parent in R.getKeysIn() - set([CONSTANT]):
                        if beliefKey(name,makeFuture(parent) not in self.nodes['state post']):
                            # Use real variable
                            self.drawEdge(makeFuture(parent),name,g)
            parser = GraphMLParser()
            parser.write(self.xml,'/tmp/GroundTruth-USC.graphml')
コード例 #15
0
ファイル: test.py プロジェクト: rvinowise/lang_style
from pygraphml import GraphMLParser

from lxml import etree

if (__name__ == "__main__"):
    gp = GraphMLParser()
    g = gp.parse("weather.graphml")
    node0 = g.get_node("in the middle", "NodeLabel")
    group = node0.container_node[0]
    g.set_root(node0)

    nodes = g.BFS(direction="contained_nodes")
    for node in nodes:
        print(node.attr.get("NodeLabel"))

    node1 = g.get_node("n0", "id")

    test = etree.tostring(g.xml, encoding=str)

    gp.write(g)
コード例 #16
0
def read_graphml(filename, color_map=[]):
    parser = GraphMLParser()
    g = parser.parse(filename)
    
    g.set_root('n0')
    
    print dir(g)
    id = '%s_%s'%(filename.split('.')[0],filename.split('.')[1])
    net = Network(id=id)
    net.notes = "NeuroMLlite conversion of %s from https://www.neurodata.io/project/connectomes/"%filename
    #net.parameters = {}

    dummy_cell = Cell(id='testcell', pynn_cell='IF_cond_alpha')
    dummy_cell.parameters = { "tau_refrac":5, "i_offset":0 }
    net.cells.append(dummy_cell)

    net.synapses.append(Synapse(id='ampa', 
                                pynn_receptor_type='excitatory', 
                                pynn_synapse_type='cond_alpha', 
                                parameters={'e_rev':0, 'tau_syn':2}))

    for node in g.nodes():
        
        info = ''
        for a in node.attributes():
            info+=node.attr[a].value+'; '
        if len(info)>2: info = info[:-4]
            
        color = '%s %s %s'%(random.random(),random.random(),random.random()) \
                    if not info in color_map else color_map[info]
                    
        print('> Node: %s (%s), color: %s'%(node.id, info, color))
        p0 = Population(id=node.id, 
                        size=1, 
                        component=dummy_cell.id, 
                        properties={'color':color})

        net.populations.append(p0)
        
    for edge in g.edges():
        #print dir(edge)
        #print edge.attributes()
        src = edge.node1.id
        tgt = edge.node2.id
        weight = float(str(edge.attr['e_weight'].value)) if 'e_weight' in edge.attr else 1
        
        #print('>> Edge from %s -> %s, weight %s'%(src, tgt, weight))
        
        net.projections.append(Projection(id='proj_%s_%s'%(src,tgt),
                                          presynaptic=src, 
                                          postsynaptic=tgt,
                                          synapse='ampa',
                                          weight=weight,
                                          random_connectivity=RandomConnectivity(probability=1)))

    #g.show()
    
    #print(net)
    
    #print(net.to_json())
    new_file = net.to_json_file('%s.json'%net.id)
    
    duration=1000
    dt = 0.1
    
    sim = Simulation(id='Sim%s'%net.id,
                     network=new_file,
                     duration=duration,
                     dt=dt,
                     recordRates={'all':'*'})
    
    check_to_generate_or_run(sys.argv, sim)