Пример #1
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)
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)
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)
Пример #4
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)
		 	g.add_edge_by_label(str(i), str(e))
 	
 	parser = GraphMLParser() 
 	parser.write(g, "myGraph.graphml") 	
Пример #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 readFile(self, filename):
        self.reset()

        try:
            parser = GraphMLParser()
            g = parser.parse(filename)
        except Exception:
            box.showerror("Chyba při zpracování vstupního souboru", "Chybný formát souboru.")
            return

        nodeMap = {}

        try:
            for gnode in g.nodes():
                nodeMap[gnode.id] = self.__add_node(int(gnode['x']), int(gnode['y']))
        except KeyError:
            box.showerror("Chyba při zpracování vstupního souboru", "Uzlum chybi udaje o pozici (atributy x a y).")
            self.reset()
            return

        try:
            for gedge in g.edges():
                start = nodeMap[gedge.node1.id]
                end = nodeMap[gedge.node2.id]
                isCurve = gedge.node1.id == gedge.node2.id
                self.__add_edge(start, end, isCurve)
            self.label.configure(text=os.path.basename(filename))
        except KeyError:
            box.showerror("Chyba při zpracování vstupního souboru",
                          "Soubor obsahuje hrany spojujici neexistujici hrany")
            self.reset()
            return

        self.repaint()
Пример #7
0
    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")
Пример #8
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")
Пример #9
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)
Пример #10
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
Пример #11
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
Пример #12
0
def _graphml2nx(fname):
	g=nx.DiGraph()
	def _attrdict(node):
		attrs=node.attributes()
		return {key:attrs[key].value for key in attrs if key not in attr_blacklist}
	parser=GraphMLParser()
	imported_graph=parser.parse(fname)
	edges=[(edge.node1.id, edge.node2.id) for edge in imported_graph.edges()]
	nodes=[(node.id, _attrdict(node)) for node in imported_graph.nodes()]
	g.add_edges_from(edges)
	g.add_nodes_from(nodes)
	assert(nx.is_tree(g))
	assert(nx.is_directed(g))
	return g
Пример #13
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"))
Пример #14
0
    def writeFile(self, filename):
        g = Graph()

        for i in self.nodes:
            node = self.nodes[i]
            node.name = str(i)
            gnode = g.add_node(i)
            gnode['label'] = i
            gnode['x'] = node.x
            gnode['y'] = node.y

        for i in self.edges:
            edge = self.edges[i]
            edge.name = i

        parser = GraphMLParser()
        parser.write(g, filename)
Пример #15
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)
Пример #16
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
Пример #17
0
    def write(self, ):
        """
        """

        parser = GraphMLParser()
        parser.write(self.graph, "graph.graphml")
Пример #18
0
from pygraphml import GraphMLParser
from Problem import Problem
import searchers.Uninformed as Uninformed, searchers.Informed as Informed

parser = GraphMLParser()


####### Romania problem from the textbook #######
def romania_goal_test(node):
    return node['label'] == 'Bucharest'

romania_sld_table = {
    'Arad':366,
    'Bucharest':0,
    'Craiova':160,
    'Drobeta':242,
    'Eforie':161,
    'Fagaras':176,
    'Giurgiu':77,
    'Hirsova':151,
    'Iasi':226,
    'Lugoj':244,

    'Mehadia': 241,
    'Neamt': 234,
    'Oradea': 380,
    'Pitesti': 100,
    'Rimnicu Vilcea': 193,
    'Sibiu': 253,
    'Timisoara': 329,
    'Urziceni': 80,
Пример #19
0
def open_file(file):
    parser = GraphMLParser()
    graph = parser.parse(file)
    #graph.show()
    return graph
Пример #20
0
from pygraphml import GraphMLParser
import sys

reload(sys)
sys.setdefaultencoding("latin-1")

file = "mln.graphml"

parser = GraphMLParser()

g = parser.parse(file)

friends = []
friends_with_no_count = []
nodes = g.nodes()
count = 0
for node in nodes:
    uid = node['uid']
    friend_count = node.attr.get('friend_count', None)
    if friend_count is not None:
        friends.append(int(friend_count.value))
    else:
        friends_with_no_count.append(node['Label'])
    count += 1

friends.append(len(friends))


facebook = open("facebook_friends_count.csv", "w")
facebook.write("NUM,COUNT\n")
num=1
Пример #21
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')
Пример #22
0
edge['weight'] = 11
edge = radiation.add_edge(eleven, ten, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(eleven, twelve, directed=True)
edge['weight'] = 10
edge = radiation.add_edge(eleven, fifteen, directed=True)
edge['weight'] = 8

edge = radiation.add_edge(twelve, eleven, directed=True)
edge['weight'] = 9

edge = radiation.add_edge(thirteen, nine, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(thirteen, fourteen, directed=True)
edge['weight'] = 7

edge = radiation.add_edge(fourteen, ten, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(fourteen, thirteen, directed=True)
edge['weight'] = 7
edge = radiation.add_edge(fourteen, fifteen, directed=True)
edge['weight'] = 8

edge = radiation.add_edge(fifteen, eleven, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(fifteen, fourteen, directed=True)
edge['weight'] = 7

parser = GraphMLParser()
parser.write(radiation, "radiation.graphml")
Пример #23
0
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)
def get_data_from_book_info (book_data, response, field_name):
    tree = html.fromstring(response.content)
    field_data = tree.xpath('//td/strong[text() = "{}"]/../../td/text()'.format(field_name))
    field_string = ""
    for field in field_data:
        field_string = field_string + field.replace("\n","").replace("\r","").replace("\t","")

    book_data[field_name] = field_string

if __name__ == "__main__":
    book_data = {}
    currentPosition = 0;
    query_string = QUERY_FORMAT_STRING.format(QUERY);
    file_handles = {};
    graph = Graph();
    parser = GraphMLParser()
    filename = "book_data_" + QUERY + ".graphml"
    # map for collecting nodes
    nodes = {}
    while(True):
        page = requests.get(query_string)
        tree = html.fromstring(page.content)

        links = tree.xpath('//table[@id="searchresult"]//a/@href')

        if(len(links) == 0):
            break;

        for link in links:
            book_info_response = requests.get(BASE_URL_DNB + link)
            get_data_from_book_info(book_data, book_info_response, "Titel")
Пример #25
0
test2.add_edge(a, b, directed=True)
test2.add_edge(b, c, directed=True)
test2.add_edge(b, d, directed=True)
test2.add_edge(c, e, directed=True)
test2.add_edge(c, f, directed=True)
test2.add_edge(d, g, directed=True)
test2.add_edge(d, h, directed=True)
test2.add_edge(f, i, directed=True)
test2.add_edge(f, j, directed=True)
test2.add_edge(g, k, directed=True)
test2.add_edge(g, l, directed=True)

for edge in test2.edges():
    edge['weight'] = 1

test2.add_edge(a, m, directed=True)
test2.add_edge(m, n, directed=True)
test2.add_edge(m, o, directed=True)
test2.add_edge(n, p, directed=True)
test2.add_edge(n, q, directed=True)
test2.add_edge(o, r, directed=True)
test2.add_edge(o, s, directed=True)

for edge in test2.edges():
    if edge.child()['label'] in ['M', 'N', 'O', 'P', 'Q', 'R', 'S']:
        edge['weight'] = 3

parser = GraphMLParser()
parser.write(test2, "test2.graphml")
Пример #26
0
from pygraphml import Graph
from pygraphml import GraphMLParser

# data sets are from http://www.graphdrawing.org/data.html
parser = GraphMLParser()
g = parser.parse('./random-dag/g.400.12.graphml')

with open('example.lgf', 'w') as fp:
    fp.write('@nodes\nlabel\n')
    for n in g.nodes():
        fp.write(n['label'][1:] + '\n')
    fp.write('@arcs\n\t\tlabel\n')
    counter = 0
    for n in g.nodes():
        for nn in g.children(n):
            fp.write(n['label'][1:] + '\t' + nn['label'][1:] + '\t' +
                     str(counter) + '\n')
            counter += 1
    fp.write('@end\n')
Пример #27
0

def postProcessGraph(g, lines):
    # Add manual fixes here.
    pass


# Actually process the file and output the graph.
if len(sys.argv) < 3:
    print('Expected input and output file names')
else:
    with (open(sys.argv[1], 'r')) as f:
        lines = f.readlines()
        lines = removeDuplicateHeadings(lines)
        lines = fixHeadings(lines)
        lines = removeSectionTitles(lines)
        lines = removePageNumbers(lines)
        lines = removeWhiteSpace(lines)
        lines = squishParagraphs(lines)
        lines = moveSmallParagraphs(lines)
        lines = removeBlankLines(lines)

        g = Graph()
        addNodes(g)
        setStars(lines)
        addEdges(g, lines)
        postProcessGraph(g, lines)

        parser = GraphMLParser()
        parser.write(g, sys.argv[2])
Пример #28
0
g = test1.add_node("G")
h = test1.add_node("H")
i = test1.add_node("I")
j = test1.add_node("J")
k = test1.add_node("K")
l = test1.add_node("L")
m = test1.add_node("M")
n = test1.add_node("N")
o = test1.add_node("O")

test1.add_edge(a, b, directed=True)
test1.add_edge(a, c, directed=True)
test1.add_edge(b, d, directed=True)
test1.add_edge(b, e, directed=True)
test1.add_edge(c, f, directed=True)
test1.add_edge(c, g, directed=True)
test1.add_edge(d, h, directed=True)
test1.add_edge(d, i, directed=True)
test1.add_edge(e, j, directed=True)
test1.add_edge(e, k, directed=True)
test1.add_edge(f, l, directed=True)
test1.add_edge(f, m, directed=True)
test1.add_edge(g, n, directed=True)
test1.add_edge(g, o, directed=True)

for edge in test1.edges():
    edge['weight'] = 1

parser = GraphMLParser()
parser.write(test1, "test1.graphml")
Пример #29
0
f2b = romania.add_edge(fagaras, bucharest, directed=False)
f2b['weight'] = 211

p2b = romania.add_edge(pitesti, bucharest, directed=False)
p2b['weight'] = 101

b2g = romania.add_edge(bucharest, giurgiu, directed=False)
b2g['weight'] = 90

b2u = romania.add_edge(bucharest, urziceni, directed=False)
b2u['weight'] = 85

u2h = romania.add_edge(urziceni, hirsova, directed=False)
u2h['weight'] = 98

h2e = romania.add_edge(hirsova, eforie, directed=False)
h2e['weight'] = 86

u2v = romania.add_edge(urziceni, vaslui, directed=False)
u2v['weight'] = 142

v2i = romania.add_edge(vaslui, iasi, directed=False)
v2i['weight'] = 92

i2n = romania.add_edge(iasi, neamt, directed=False)
i2n['weight'] = 87

parser = GraphMLParser()
parser.write(romania, "romania.graphml")
Пример #30
0
    def write(self, ):
        """
        """

        parser = GraphMLParser()
        parser.write(self.graph, "graph.graphml")
from pygraphml import Graph
from pygraphml import GraphMLParser

parser = GraphMLParser()
g = parser.parse(
    'rendered_55days_inconsistency_graph_with_AggregationAndLabel.graphml')
nodes = g.BFS()
for node in nodes:
    node['r'] = node['r']
    node['g'] = node['g']
    node['b'] = node['b']

parser.write(g, "myGraph.graphml")
Пример #32
0
            as_type = AS["type"]
            newAlloc = Allocation(Id, pos, size, pagetype, as_type)
            Allocations[Id] = newAlloc
            Nodes[Id] = newAlloc.add_to_graph(g)

        elif "val" in j:
            val = j["val"]
            Id = int(val["id"])
            allocation_id = int(val["allocation_id"])
            size = int(val["size"])
            pos = int(val["pos"])
            newValue = Value(Id, pos, size, allocation_id)
            Values[Id] = newValue

## Use dependences to determine connection between allocations
with open(args[0], 'r') as f:
    for line in f:
        j = json.loads(line)

        if "dep" in j:
            dep = j["dep"]
            srcValId = int(dep["src_id"])
            dstValId = int(dep["dst_id"])
            srcAllocId = Values[srcValId].alloc_id
            dstAllocId = Values[dstValId].alloc_id
            weight = Allocations[srcAllocId].size
            newEdge = DirectedEdge(srcAllocId, dstAllocId, weight)
            newEdge.add_to_graph(g, Nodes)

parser = GraphMLParser()
parser.write(g, "cprof.graphml")
Пример #33
0
				edge = g.add_edge_by_label(TOPIC_CATEGORIES[idx], labels[artist_id])
				if score == 0:
					edge["weight"] = 0.001 # Set to very small value
				else:
					edge["weight"] = score

	return g

if __name__ == "__main__":
	with open(INFILE, "r") as inf:
		songs = json.load(inf)

	summary = summarize_songs(songs)
	similarity = calculate_similarity(summary)
	emotion_graph = generate_graph(similarity, "emotion")
	topic_graph = generate_graph(similarity, "topic")
	emotion_graph_with_clusters = generate_graph_with_clusters(summary, "emotion")
	topic_graph_with_clusters = generate_graph_with_clusters(summary, "topic")

	from pygraphml import GraphMLParser
	parser = GraphMLParser()
	parser.write(emotion_graph, OUTEMOTIONS)
	parser.write(topic_graph, OUTTOPICS)

	# Need to manually change
	# <key attr.name="weight" attr.type="string" id="weight"/>
	# to
	# <key attr.name="weight" attr.type="double" for="edge" id="weight"/>
	# in output files
	parser.write(emotion_graph_with_clusters, OUTEMOTIONSCENTERS)
	parser.write(topic_graph_with_clusters, OUTTOPICCENTERS)
Пример #34
0
from pygraphml import Graph
from pygraphml import GraphMLParser
import numpy
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
parser=GraphMLParser()
g=parser.parse("mln.graphml")

#g.show()
datafile=open("facebook.data","w")
dropfile=open("facebook_dropped.data","w")
nodes = g.nodes()
friendCounts=[]
for node in nodes:
	try:
		count=node['friend_count']
    		friendCounts.append(int(count)) 
	except Exception, e:
		dropfile.write(str(node)+"\n")

friendCounts.append(len(nodes)) 
friendCounts=sorted(friendCounts)
datafile.write("Friend_count\t"+str(len(nodes))+"\n")
datafile.write("std\t"+str(numpy.std(friendCounts))+"\n")
datafile.write("mean\t"+str(numpy.mean(friendCounts))+"\n")
datafile.write("median\t"+str(numpy.median(friendCounts))+"\n")
for fcount in friendCounts:
	datafile.write(str(fcount)+"\n")
datafile.close()
dropfile.close()
Пример #35
0
    allginfo = []
    CALSlm = []
    ALSlm = []
    allpms = []
    alllms = []
    #alllsPn = []
    #lllsPm = []
    #allpmlengths = []
    #allpmdiverse = []
    #alllsPnOnPm = []
    #alltermWithoutConst = []
    ginfos = ["nodes", "paths", "edges"]
    #alllinks = []
    #get the graph

    parser = GraphMLParser()
    gname = "bridge2"  #"Bics"#"BeyondTheNetwork"#"Cogentco"#"bridge2"#"BeyondTheNetwork"#"Getnet"
    g = parser.parse("./archive/{}.graphml".format(gname))
    """
    #pmlengths = ["top 1 traversal", "avg"]
    gname="AS20965"#"AS8717"#"AS20965"#"AS8717"
    g = ipM.loadgraph("./MatlabData/CAIDA_{}.mat".format(gname))
    """
    expinfo["init"] = init
    expinfo["t"] = t
    expinfo["tmax"] = tmax
    expinfo["numberMonitors"] = numberMonitors
    expinfo["loops"] = loops
    expinfo["cond"] = cond
    #expinfo["numberPathsBtw2M"]=numberPathsBtw2M
    expinfo["gname"] = gname
Пример #36
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()
Пример #37
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)