class GraphMLExporter(): """ """ def __init__(self, lines): """ """ self.lines = lines self.graph = Graph() self.i = 0 self.createGraph() def createGraph(self): """ """ mul = 100 for line in self.lines: n1 = self.checkNode(str(self.i)) n2 = self.checkNode(str(self.i)) n1['x'] = line.p1.x * mul n1['y'] = line.p1.y * mul n1['z'] = line.p1.z * mul n2['x'] = line.p2.x * mul n2['y'] = line.p2.y * mul n2['z'] = line.p2.z * mul self.graph.add_edge(n1, n2) def checkNode(self, label): """ """ exist = False for n in self.graph.nodes(): if label == n['label']: exist = True node = n if exist: return node else: self.i += 1 return self.graph.add_node(label) def write(self, ): """ """ parser = GraphMLParser() parser.write(self.graph, "graph.graphml")
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
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)
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)
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()
class Property_Graph: def __init__(self): """ Initializes graph parameters. """ self.g = Graph() self.vertex_id = 0 self.edge_id = 0 def add_vertex(self, label, value, property=None): """ Adds a vertex to the graph. @param label: default label or key by which vertex is to be added (vertex will be uniquely by this label and its value) e.g. "name" @param value: value of above label e.g. "product0012" @param property: list of length 2 containing key name of property as first element and value of property as second element """ # check if node with same label already exists, if it does then return that node instance for n in self.g._nodes: if n[label] == value: return n # add new node to graph node = self.g.add_node(str(self.vertex_id)) # add default label and its value node[label] = value # add additional properties if provided if property != None: node[property[0]] = property[1] self.vertex_id += 1 return node def add_edge(self, label, label_value1, label_value2, default_property, property=None): """ Adds edge between two vertices. @param label: label or key value by which vertex will be searched e.g. "name" or "URI" @param label_value1 : value of above label or key of source vertex @param label_value2 : value of above label or key of target vertex @param default_property : default_property of edge (predicate in RDF terms) @param property : additional property, list of length 2 containing key name of property as first element and value of property as second element """ n1 = None n2 = None # Search source and target nodes for n in self.g._nodes: if n[label] == label_value1: n1 = n if n[label] == label_value2: n2 = n # If source or target doesn't exists, then return if n1 == None or n2 == None: return # Add edge edge = self.g.add_edge(n1, n2, directed=True) # Add edge default property edge['property'] = default_property edge['id'] = str(self.edge_id) # Add additional property if provided if property != None: edge[property[0]] = edge[property[1]] self.edge_id += 1 def add_vertices_and_edge(self, label, label_value1, label_value2, default_edge_property,\ edge_property=None, vertex1_property=None, vertex2_property=None): """ Adds two vertices and edge connecting them @param label: default label or key by which vertex is to be added (vertex will be uniquely by this label and its value) e.g. "name" @param label_value1 : value of above label or key of source vertex @param label_value2 : value of above label or key of target vertex @param default_edge_property : default_property of edge (predicate in RDF terms) @param edge_property : additional property, list of length 2 containing key name of property as first element and value of property as second element @param vertex1_property: list of length 2 containing key name of property as first element and value of property as second element @param vertex2_property: list of length 2 containing key name of property as first element and value of property as second element """ n1 = self.add_vertex(label, label_value1, vertex1_property) n2 = self.add_vertex(label, label_value2, vertex2_property) edge = self.g.add_edge(n1, n2, directed=True) edge['label'] = default_edge_property edge['id'] = str(self.edge_id) self.edge_id += 1 if edge_property != None: edge[edge_property[0]] = edge[edge_property[1]] def add_property(self, label, label_value, property, to_edge=False): """ Adds property to a edge or vertex @param label : label or key by which desired edge or vertex will be searched @param label_value : value of above label or key @param property : property to be added, list of length 2 containing key name of property as first element and value of property as second element @param to_edge : If set True, property will be added to edge, default value is False. """ if to_edge: # Yet to be implemented pass else: for n in self.g._nodes: if n[label] == label_value: n[property[0]] = property[1] break 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)
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);
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) z2a['weight'] = 75 o2s = romania.add_edge(oradea, sibiu, directed=False) o2s['weight'] = 151 a2s = romania.add_edge(arad, sibiu, directed=False) a2s['weight'] = 140 a2t = romania.add_edge(arad, timisoara, directed=False) a2t['weight'] = 118 t2l = romania.add_edge(timisoara, lugoj, directed=False)
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 edge = radiation.add_edge(four, three, directed=True) edge['weight'] = 9 edge = radiation.add_edge(six, two, directed=True)
class WorldView(QGraphicsScene): rowHeight = 100 colWidth = 150 def __init__(self,parent = None): super(WorldView,self).__init__(parent) self.setBackgroundBrush(QColor('white')) self.nodes = {'state pre': {}, 'state post': {}, 'action': {}, 'utility': {}, 'observation': {}} self.edgesOut = {} self.edgesIn = {} self.agents = {} self.world = None self.graph = {} self.dirty = False self.center = None self.xml = None def clear(self): super(WorldView,self).clear() for table in self.nodes.values(): table.clear() self.edgesOut.clear() self.edgesIn.clear() self.center = None def displayWorld(self,agents=None): self.clear() self.graph = graph.DependencyGraph(self.world) self.graph.computeGraph(agents) layout = getLayout(self.graph) if self.world.diagram is None: self.world.diagram = diagram.Diagram() # Lay out the pre variable nodes x = self.drawStateNodes(layout['state pre'],self.graph,0,0,'xpre','ypre') # Lay out the action nodes x = self.drawActionNodes(layout['action'],x,0) # Lay out the post variable nodes x = self.drawStateNodes(layout['state post'],self.graph,x,0,'xpost','ypost') # Lay out the utility nodes x = self.drawUtilityNodes(x,0,self.graph,sorted(self.world.agents.keys())) self.colorNodes() # Lay out edges for key,entry in self.graph.items(): if key in self.nodes[entry['type']]: node = self.nodes[entry['type']][key] for child in entry['children']: if child in self.nodes[self.graph[child]['type']]: self.drawEdge(key,child) 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') def getGraphNode(self,label): for node in self.xml.nodes(): if node['label'] == label: return node else: return(self.xml.add_node(label)) def drawStateNodes(self,nodes,graph,x0,y0,xkey,ykey,believer=None,maxRows=10): x = x0 even = True for layer in nodes: y = y0 for key in sorted(layer): if believer: label = beliefKey(believer,key) if self.xml: self.xml.add_node('%sBeliefOf%s' % (believer,makePresent(key))) else: if gtnodes and makePresent(key) not in gtnodes: continue label = key if self.xml: self.xml.add_node(makePresent(key)) variable = self.world.variables[makePresent(key)] if y >= y0+maxRows*self.rowHeight: even = not even if even: y = y0 else: y = y0+50 x += int(0.75*self.colWidth) if not xkey in variable: variable[xkey] = x variable[ykey] = y # Move on to next Y y += self.rowHeight if graph[key]['agent'] != WORLD and graph[key]['agent']: agent = self.world.agents[graph[key]['agent']] if isBinaryKey(key): node = VariableNode(agent,key[len(agent.name)+1:],label, variable[xkey],variable[ykey], 100,50,scene=self) else: node = VariableNode(agent,key[len(agent.name)+3:],label, variable[xkey],variable[ykey], 100,50,scene=self) else: node = VariableNode(None,state2feature(key),key, variable[xkey],variable[ykey], 100,50,scene=self) self.nodes[graph[key]['type']][label] = node x += self.colWidth return x def drawActionNodes(self,nodes,x0,y0,maxRows=10): x = x0 y = y0 for action in sorted(nodes): if gtnodes and str(action) not in gtnodes: continue if self.world.diagram.getX(action) is None: self.setDirty() self.world.diagram.x[action] = x self.world.diagram.y[action] = y # Move on to next Y y += self.rowHeight if y >= maxRows*self.rowHeight: y = y0 x += self.colWidth else: x = max(x,self.world.diagram.getX(action)) y = max(y,self.world.diagram.getY(action)) node = ActionNode(self.world.agents[self.graph[action]['agent']],action,scene=self) self.nodes[self.graph[action]['type']][action] = node if self.xml: self.xml.add_node(str(action)) x += self.colWidth return x def drawUtilityNodes(self,x0,y0,graph,agents): x = x0 y = y0 - self.rowHeight/2 for name in agents: if name in graph: agent = self.world.agents[name] if self.world.diagram.getX(agent.name) is None: self.setDirty() y += self.rowHeight self.world.diagram.x[agent.name] = x self.world.diagram.y[agent.name] = y else: x = self.world.diagram.getX(agent.name) y = self.world.diagram.getY(agent.name) node = UtilityNode(agent,x,y,scene=self) self.nodes[graph[name]['type']][name] = node if self.xml: self.xml.add_node(name) if self.xml: self.xml.add_edge(self.getGraphNode(stateKey(name,'horizon')), self.getGraphNode(name),True) x += self.colWidth return x def drawObservationNodes(self,x0,y0,graph,xkey,ykey,believer=None,maxRows=10): omega = sorted(sum([[stateKey(name,omega) for omega in self.world.agents[name].omega] for name in self.world.agents],[])) x = x0 y = y0 even = True oNodes = {} for key in omega: if gtnodes and makePresent(key) not in gtnodes: continue if believer: label = beliefKey(believer,key) if self.xml: self.xml.add_node('%sBeliefOf%s' % (believer,makePresent(key))) else: label = key if self.xml: oNodes[key] = self.xml.add_node(key) variable = self.world.variables[makePresent(key)] if y >= y0+maxRows*self.rowHeight: even = not even if even: y = y0 else: y = y0+50 x += int(0.75*self.colWidth) if not xkey in variable: variable[xkey] = x variable[ykey] = y # Move on to next Y y += self.rowHeight agent = self.world.agents[state2agent(key)] if isBinaryKey(key): node = VariableNode(agent,key[len(agent.name)+1:],key, variable[xkey],variable[ykey], 100,50,scene=self) else: node = VariableNode(agent,key[len(agent.name)+3:],key, variable[xkey],variable[ykey], 100,50,scene=self) self.nodes['observation'][label] = node x += self.colWidth return x def drawEdge(self,parent,child,graph=None,rect0=None,rect1=None): if self.xml: if isinstance(parent,str): parentVal = makePresent(parent) else: parentVal = str(parent) if isBeliefKey(parentVal): parentVal = '%sBeliefOf%s' % (belief2believer(parentVal), makePresent(belief2key(parentVal))) if isinstance(child,str): childVal = makePresent(child) else: childVal = str(child) if isBeliefKey(childVal): childVal = '%sBeliefOf%s' % (belief2believer(childVal), makePresent(belief2key(childVal))) for nP in self.xml.nodes(): if nP['label'] == str(parentVal): break else: raise RuntimeError('Unable to find GraphML node %s' % (parentVal)) for nC in self.xml.nodes(): if nC['label'] == str(childVal): break else: raise RuntimeError('Unable to find GraphML node %s (link from %s)' % \ (childVal,parentVal)) self.xml.add_edge(nP,nC,True) if graph is None: graph = self.graph if isBeliefKey(parent): node0 = self.nodes[graph[belief2key(parent)]['type']][parent] else: node0 = self.nodes[graph[parent]['type']][parent] if isBeliefKey(child): node1 = self.nodes[graph[belief2key(child)]['type']][child] else: node1 = self.nodes[graph[child]['type']][child] if rect0 is None: rect0 = node0.boundingRect() if rect1 is None: rect1 = node1.boundingRect() if parent == child: # Loop back to self x0 = rect0.x()+rect0.width()/15 y0 = rect0.y()+2*rect0.height()/3 path = QPainterPath(QPointF(x0,y0)) path.arcTo(rect0.x(),rect0.y()+rect0.height()/2,rect0.width(),rect0.height(),145,250) edge = QGraphicsPathItem(path,node0) arrow = drawArrow(QLineF(x0-5,y0+25,x0,y0),edge) elif rect0.y() == rect1.y(): # Same row, so arc x0 = rect0.x()+rect0.width()/2 x1 = rect1.x()+rect1.width()/2 path = QPainterPath(QPointF(x1,rect1.y()+rect1.height()/2)) path.arcTo(x1,rect1.y()+rect1.height()/2,x0-x1,rect1.height(),180,180) edge = QGraphicsPathItem(path) node0.scene().addItem(edge) if x1 < x0: arrow = drawArrow(QLineF(x1+25,rect1.y()+rect1.height()+15, x1-5,rect1.y()+rect1.height()),edge) else: arrow = drawArrow(QLineF(x1-25,rect1.y()+rect1.height()+15, x1+5,rect1.y()+rect1.height()),edge) else: # straight-line link if rect0.x() < rect1.x(): x0 = rect0.right() x1 = rect1.left() else: x0 = rect0.left() x1 = rect1.right() y0 = rect0.y()+rect0.height()/2 y1 = rect1.y()+rect1.height()/2 edge = QGraphicsLineItem(x0,y0,x1,y1) node0.scene().addItem(edge) arrow = drawArrow(edge.line(),edge) edge.setZValue(1.) if not parent in self.edgesOut: self.edgesOut[parent] = {} if child in self.edgesOut[parent]: node0.scene().removeItem(self.edgesOut[parent][child][0]) self.edgesOut[parent][child] = (edge,arrow) if not child in self.edgesIn: self.edgesIn[child] = {} if parent != child: self.edgesIn[child][parent] = (edge,arrow) return edge def highlightEdges(self,center): """ Hide any edges *not* originating or ending at the named node :type center: {str} """ self.center = center for key,table in list(self.edgesOut.items())+list(self.edgesIn.items()): if center is None or key in center: # All edges are important! for edge,arrow in table.values(): edge.show() else: for subkey,(edge,arrow) in table.items(): if center is None or subkey in center: # This edge is important edge.show() else: # This edge is unimportant edge.hide() def boldEdges(self,center): """ Highlight any edges originating or ending at the named node :type center: {str} """ for key,table in self.edgesOut.items()+self.edgesIn.items(): if key in center: # All edges are important! for edge,arrow in table.values(): edge.setPen(QPen(QBrush(QColor('black')),5)) edge.setZValue(2.0) else: for subkey,(edge,arrow) in table.items(): if subkey in center: # This edge is important edge.setPen(QPen(QBrush(QColor('black')),5)) edge.setZValue(2.0) else: # This edge is unimportant edge.setPen(QPen(QColor('black'))) edge.setZValue(1.0) def updateEdges(self,key,rect): self.setDirty() if key in self.edgesOut: for subkey,(edge,arrow) in self.edgesOut[key].items(): if self.center is None or key in self.center or subkey in self.center: if isinstance(edge,QGraphicsLineItem): line = edge.line() line.setP1(QPointF(rect.x()+rect.width(),rect.y()+rect.height()/2)) edge.setLine(line) drawArrow(line,arrow=arrow) elif key != subkey: edge.scene().removeItem(edge) del self.edgesOut[key][subkey] del self.edgesIn[subkey][key] self.drawEdge(key,subkey,rect0=rect) if key in self.edgesIn: for subkey,(edge,arrow) in self.edgesIn[key].items(): if self.center is None or key in self.center or subkey in self.center: if isinstance(edge,QGraphicsLineItem): line = edge.line() line.setP2(QPointF(rect.x(),rect.y()+rect.height()/2)) edge.setLine(line) drawArrow(line,arrow=arrow) elif key != subkey: edge.scene().removeItem(edge) del self.edgesIn[key][subkey] del self.edgesOut[subkey][key] self.drawEdge(subkey,key,rect1=rect) def step(self): self.world.step() self.world.printState() self.colorNodes('likelihood') def colorNodes(self,mode='agent'): cache = None for category,nodes in self.nodes.items(): for node in nodes.values(): color = node.defaultColor if mode == 'agent': if node.agent: color = self.world.diagram.getColor(node.agent.name) elif node.agent is None: color = self.world.diagram.getColor(None) elif mode == 'likelihood': if cache is None: # Pre-compute some outcomes cache = {} outcomes = self.world.step(real=False) for outcome in outcomes: # Update action probabilities for name,distribution in outcome['actions'].items(): if name not in cache: cache[name] = Distribution() for action in distribution.domain(): cache[name].addProb(action,outcome['probability']*distribution[action]) # Update state probabilities for vector in outcome['new'].domain(): for key,value in vector.items(): if key not in cache: cache[key] = Distribution() cache[key].addProb(value,outcome['probability']*outcome['new'][vector]) if category == 'state pre': if node.agent: key = stateKey(node.agent.name,node.feature) else: key = stateKey(None,node.feature) variable = self.world.variables[key] marginal = self.world.getFeature(key) if variable['domain'] is bool: color = dist2color(marginal) else: raise RuntimeError('Unable to display color of %s over domain %s' % (key,variable['domain'])) elif category == 'action': uniform = 1./float(len(cache[node.agent.name])) prob = cache[node.agent.name].getProb(node.action) if prob < uniform: prob = 0.5*prob/uniform else: prob = 0.5*(prob-uniform)/(1.-uniform) + 0.5 distribution = Distribution({True: prob}) distribution[False] = 1.-distribution[True] color = dist2color(distribution) elif category == 'state post': if node.agent: key = stateKey(node.agent.name,node.feature) else: key = stateKey(None,node.feature) key = makePresent(key) if self.world.variables[key]['domain'] is bool: dist = self.world.float2value(key,cache[key]) color = dist2color(dist) node.setBrush(QBrush(color)) def setDirty(self): self.parent().parent().parent().actionSave.setEnabled(True) self.dirty = True def unsetDirty(self): self.parent().parent().parent().actionSave.setEnabled(False) self.dirty = False def minRect(self): """ @return: a rectangle cropped to show only object space """ rect = None for nodes in self.nodes.values(): for node in nodes.values(): if rect is None: rect = node.boundingRect() else: rect = rect.united(node.boundingRect()) return rect def saveImage(self,fname): rect = self.minRect() #self.sceneRect() pix = QImage(rect.width(), rect.height(),QImage.Format_ARGB32) painter = QPainter(pix) self.render(painter,rect) painter.end() pix.save(fname) def saveSubgraphs(self,dirName,onlyConnected=True): previous = self.center for nodeType,nodes in self.nodes.items(): for key,node in nodes.items(): if not onlyConnected or key in self.edgesOut or key in self.edgesIn: center = {key} if nodeType == 'state post': for agent in self.world.agents: subkey = beliefKey(agent,key) if subkey in nodes: center.add(subkey) self.highlightEdges(center) if isinstance(key,ActionSet): self.saveImage(os.path.join(dirName,'%s.png' % (str(key)))) else: self.saveImage(os.path.join(dirName,'%s.png' % (escapeKey(key)))) self.highlightEdges(previous)
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) 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)
from pygraphml import Graph # Create graph 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) g.show()
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) 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():