Пример #1
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")
Пример #2
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") 	
Пример #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
    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)
Пример #5
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
Пример #6
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"))
Пример #7
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)
Пример #8
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)
            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")
            get_data_from_book_info(book_data, book_info_response, "Person(en)")
            get_data_from_book_info_link(book_data, book_info_response, "Schlagwörter")

            if(len(book_data['Schlagwörter']) > 0):
                for v in book_data.values():             
                    print(v)

                for s in book_data['Schlagwörter']:
                    node = None
                    if s not in nodes:
                        node = graph.add_node (s)
                        nodes[s] = node
                    else:
                        node = nodes[s]

                s1 = book_data['Schlagwörter'][0]
                for s in book_data['Schlagwörter']:
                    if s != s1:
                        edge = graph.add_edge(nodes[s1], nodes[s])
                        edge['label'] = book_data['Titel']
                parser.write(graph, filename)


        query_string = QUERY_FORMAT_STRING_2.format(QUERY,str(currentPosition))
        currentPosition += len(links);
Пример #10
0
    def write(self, ):
        """
        """

        parser = GraphMLParser()
        parser.write(self.graph, "graph.graphml")
Пример #11
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")
Пример #12
0
    def write(self, ):
        """
        """

        parser = GraphMLParser()
        parser.write(self.graph, "graph.graphml")
Пример #13
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")
Пример #14
0
            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")
Пример #15
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')
Пример #16
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")
Пример #17
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])
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")
Пример #19
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)
Пример #20
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")
Пример #21
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()
Пример #22
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)