示例#1
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")
示例#2
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")
示例#3
0
 def __init__(self):
     """
     Initializes graph parameters.
     """
     self.g = Graph()
     self.vertex_id = 0
     self.edge_id = 0
示例#4
0
    def __init__(self, lines):
        """
        """

        self.lines = lines
        self.graph = Graph()
        self.i = 0

        self.createGraph()
示例#5
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
def loadgraph(addr):
    gphml = Graph()
    gmatrix = loadmat(addr)["A"].toarray()
    for i in range(len(gmatrix)):
        n = gphml.add_node()
        n.id = str(i)
    for i in range(len(gmatrix)):
        for j in range(i + 1, len(gmatrix)):
            if gmatrix[i, j] == 1:
                gphml.add_edge_by_id(str(i), str(j))
    return gphml
示例#7
0
def create_graph():
    Item.ID = 0
    g = Graph()

    n1 = g.add_node("A")
    n2 = g.add_node("B")
    n3 = g.add_node("C")
    n4 = g.add_node("D")
    n5 = g.add_node("E")

    g.add_edge(n1, n3)
    g.add_edge(n2, n3)
    g.add_edge(n3, n4)
    g.add_edge(n3, n5)
    return g
示例#8
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"))
示例#9
0
def do_harvest(query, iterations):
    book_data = {}
    currentPosition = 0
    query_string = QUERY_FORMAT_STRING.format(query)

    graph = Graph()
    parser = JSONParser.JSONParser()

    # map for collecting nodes
    nodes = {}
    while (iterations > len(nodes)):
        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")
            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
                    node = graph.add_node(s)
                    nodes[s] = node

                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']

        query_string = QUERY_FORMAT_STRING_2.format(query,
                                                    str(currentPosition))
        currentPosition += len(links)
        iterations -= 1
    return parser.tostring(graph)
示例#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
def generate_graph(similarity, attr):
	from pygraphml import Graph
	items = similarity.items()
	labels = np.array([artist for artist, obj in items])

	network = np.zeros((labels.size, labels.size))

	g = Graph()

	for artist in labels:
		g.add_node(artist)

	for artist_id, x in enumerate(items):
		network[artist_id, artist_id] = 1
		artist, obj = x

		if attr == "emotion":
			for idx, score in enumerate(obj["emotion_sim"]):
				if network[artist_id, idx] == 0 and network[idx, artist_id] == 0:
					edge = g.add_edge_by_label(labels[artist_id], labels[idx])
					if score == 0:
						edge["weight"] = 0.001
					else:
						edge["weight"] = score
					network[artist_id, idx] = 1
					network[idx, artist_id] = 1
		elif attr == "topic":
			for idx, score in enumerate(obj["topic_sim"]):
				if network[artist_id, idx] == 0 and network[idx, artist_id] == 0:
					edge = g.add_edge_by_label(labels[artist_id], labels[idx])
					if score == 0:
						edge["weight"] = 0.001
					else:	
						edge["weight"] = score
					network[artist_id, idx] = 1
					network[idx, artist_id] = 1

	return g
示例#12
0
def generate_graph_with_clusters(summary, attr):
	from pygraphml import Graph
	items = summary.items()
	labels = np.array([artist for artist, obj in items])

	g = Graph()

	for artist in labels:
		g.add_node(artist)

	if attr == "emotion":
		for category in EMOTION_CATEGORIES:
			g.add_node(category)
	elif attr == "topic":
		for category in TOPIC_CATEGORIES:
			g.add_node(category)

	for artist_id, x in enumerate(items):
		arist, obj = x

		if attr == "emotion":
			for idx, score in enumerate(obj["emotions"]):
				edge = g.add_edge_by_label(EMOTION_CATEGORIES[idx], labels[artist_id])
				if score == 0:
					edge["weight"] = 0.001 # Set to very small value
				else:
					edge["weight"] = score

		elif attr == "topic":
			for idx, score in enumerate(obj["topics"]):
				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
示例#13
0
# binary tree with a "deep and cheap" left half and an expensive but shallow right half

from pygraphml import Graph, GraphMLParser

test2 = Graph()

a = test2.add_node("A")
b = test2.add_node("B")
c = test2.add_node("C")
d = test2.add_node("D")
e = test2.add_node("E")
f = test2.add_node("F")
g = test2.add_node("G")
h = test2.add_node("H")
i = test2.add_node("I")
j = test2.add_node("J")
k = test2.add_node("K")
l = test2.add_node("L")
m = test2.add_node("M")
n = test2.add_node("N")
o = test2.add_node("O")
p = test2.add_node("P")
q = test2.add_node("Q")
r = test2.add_node("R")
s = test2.add_node("S")

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)
示例#14
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()
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)
示例#16
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')
示例#17
0
# basic binary tree with all edge weights = 1

from pygraphml import Graph, GraphMLParser

test1 = Graph()

a = test1.add_node("A")
b = test1.add_node("B")
c = test1.add_node("C")
d = test1.add_node("D")
e = test1.add_node("E")
f = test1.add_node("F")
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)
示例#18
0
# Radiation grid from CSCI 6550 HW 1

from pygraphml import Graph, GraphMLParser

radiation = Graph()

two = radiation.add_node("2")
three = radiation.add_node("3")
four = radiation.add_node("4")
six = radiation.add_node("6")
seven = radiation.add_node("7")
nine = radiation.add_node("9")
ten = radiation.add_node("10")
eleven = radiation.add_node("11")
twelve = radiation.add_node("12")
thirteen = radiation.add_node("13")
fourteen = radiation.add_node("14")
fifteen = radiation.add_node("15")

edge = radiation.add_edge(two, three, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(two, six, directed=True)
edge['weight'] = 11

edge = radiation.add_edge(three, two, directed=True)
edge['weight'] = 9
edge = radiation.add_edge(three, four, directed=True)
edge['weight'] = 10
edge = radiation.add_edge(three, seven, directed=True)
edge['weight'] = 11
示例#19
0
# Toy problem from the textbook: Romanian road map

from pygraphml import Graph, GraphMLParser

romania = Graph()

oradea = romania.add_node("Oradea")
zerind = romania.add_node("Zerind")
arad = romania.add_node("Arad")
sibiu = romania.add_node("Sibiu")
fagaras = romania.add_node("Fagaras")
timisoara = romania.add_node("Timisoara")
rv = romania.add_node("Rimnicu Vilcea")
lugoj = romania.add_node("Lugoj")
pitesti = romania.add_node("Pitesti")
mehadia = romania.add_node("Mehadia")
drobeta = romania.add_node("Drobeta")
craiova = romania.add_node("Craiova")
bucharest = romania.add_node("Bucharest")
giurgiu = romania.add_node("Giurgiu")
urziceni = romania.add_node("Urziceni")
neamt = romania.add_node("Neamt")
iasi = romania.add_node("Iasi")
vaslui = romania.add_node("Vaslui")
hirsova = romania.add_node("Hirsova")
eforie = romania.add_node("Eforie")

o2z = romania.add_edge(oradea, zerind, directed=False)
o2z['weight'] = 71

z2a = romania.add_edge(zerind, arad, directed=False)