def createGraph(self): gph = Graph() # for pline_i, topo in enumerate(self.pline.topologies.values()): pline_i = 0 topo = self.pline.getTopology() for p_id in topo.properties: prop = self.pline.all_properties[p_id] if len(self.pline.topologies) > 1: if self.mc != None: # TODO: review why is this necessary, seems some escaping # mechanism is needed? n_label = "123456789-10.0[]a_-.:*+^/()'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -0.544021110889 x*x " else: n_label = '%r %r %s' % (p_id, pline_i + 1, prop.name) n_id = p_id + pline_i * 10000 else: if self.mc != None: n_label = "123456789-10.0[]a_-.:*+^/()'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -0.544021110889 x*x " else: n_label = '%r %s' % (p_id, prop.name) n_id = p_id label = Text( unicode(n_label), width=85, fill=color(0, 0, 0, 1), fontsize=9) gph.add_node(id=n_id, radius=5 , text=label , fill=self.getNodeColor(prop.type)) # add dependencies directions dpdencies_iterator = topo.connections.dpdencies.getSsIterator() id1, id2 = dpdencies_iterator.next() while id1 != NULL_UID: gph.add_edge(id1 + pline_i * 10000, id2 + pline_i * 10000) id1, id2 = dpdencies_iterator.next() # add dependents directions dpdents_iterator = topo.connections.dpdents.getSsIterator() id_1, id_2 = dpdents_iterator.next() while id_1 != NULL_UID: gph.add_edge(id_1 + pline_i * 10000, id_2 + pline_i * 10000) id_1, id_2 = dpdents_iterator.next() return gph
def set_into_graph(self, root_website, json_dict): print "start : set_into_graph : SpiderModelShowing" if type(root_website) != str or type(json_dict) != dict: raise TypeError("Type Error input is not type match") show_graph = Graph() # graph to draw for netloc_uni in json_dict[root_website]: # use netloc in json_dict netloc = netloc_uni.encode("ascii", "ignore") # change unicode to string for website_uni in json_dict[root_website][ netloc]: # use website in json_dict website = website_uni.encode( "ascii", "ignore") # change unicode to string if self.get_netloc(website) != "": # netloc has word show_graph.add_node( self.get_netloc(website)) # add netloc in draw graph # use child website for child_website_uni in json_dict[root_website][netloc][ website]["website"]: child_website = child_website_uni.encode("ascii", "ignore") # add netloc of child website into draw graph if self.get_netloc(child_website) != "": show_graph.add_node(self.get_netloc(child_website)) # detect netloc of website and child-website is not same if self.get_netloc(website) != self.get_netloc(child_website) \ and self.get_netloc(website) != "" \ and self.get_netloc(child_website) != "": # add edge website to child-website to draw graph show_graph.add_edge(self.get_netloc(website), self.get_netloc(child_website)) print "complete : set_into_graph : SpiderModelShowing" return show_graph
def create_graph(p_node,s): completed_nodes = [] uncomplete_nodes = [] global g g = Graph() p = p_node parent_follower = s.pAll[p_node].follower parent_node_name = s.pAll[p_node].id g.add_node(id=str(parent_node_name),radius = 5,stroke = color(1, 0, 0.25, 1),text = color(1)) print parent_node_name,"--->",parent_follower for i in range(len(parent_follower)): g.add_node(id=str(parent_follower[i]), radius = 5, stroke = color(1), text = color(1)) completed_nodes.append(parent_node_name) # Random edges. for i in range(len(parent_follower)): node1 = str(parent_node_name) node2 = str(parent_follower[i]) uncomplete_nodes.append(parent_follower[i]) g.add_edge(node1, node2, length = 500.0, weight = random(), stroke = color(1, 0, 0.25, 1)) print "-"*50 print "Completed_Nodes ",completed_nodes print "Uncomplete_Nodes ",uncomplete_nodes ##New Round while len(uncomplete_nodes) <> 0: node1 = uncomplete_nodes[0] changed_node = node_convert(node1,s) follower_list = s.pAll[changed_node].follower for i in follower_list: if i not in completed_nodes: node_1 = str(node1) node2 = str(i) print node_1,"--->",i uncomplete_nodes.append(i) g.add_node(id=str(i),radius = 5,stroke = color(1, 0, 0.25, 1),text = color(1)) g.add_edge(node_1,node2, length = 50.0, stroke = color(1), weight = random()) completed_nodes.append(node1) del uncomplete_nodes[0] ## New Round ## for I in range(len(parent_follower)): ## ## p_node = s.pAll[p].follower[I] ## parent_name = p_node ## ## p_node = node_convert(p_node,s) ## p_links = s.pAll[p_node].follower ## ## p_node = parent_name #change ## ## print parent_name,"-->",p_links ## ## r = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([0,51,255]) #rd.randint(10,200) ## green = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([255,128,255]) #rd.randint(0,100) ## b = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([128,0,255]) #rd.randint(0,200) ## ## if len(p_links) <> 0: ## ## for i in range(len(p_links)): ## node1 = str(parent_name) ## ## bul = True ## if p_links[i] not in s.pAll[p].follower: #Not a parent follower ## ## for j in range (I): #Loop to check another sibling's follower ## ## sibling = s.pAll[p].follower[j] ## sibling = node_convert(sibling,s) ## ## if p_links[i] in s.pAll[sibling].follower: ## ## bul = True #Controlling Parameter ## ## if bul: ## g.add_node(id=str(p_links[i]),radius = 5,stroke = color(r, green, b, 1),text = color(1)) ## node2 = str(p_links[i]) ## g.add_edge(node1, node2, ## length = 50.0, ## weight = random(), ## stroke = color(r, green, b, 1)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = 5#node.radius + node.radius*node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1: node.edges[0].length = 0.5 g.prune(depth=0) # Remove orphaned nodes with no connections. g.distance = 25 # Overall spacing between nodes. g.layout.force = 0.01 # Strength of the attractive & repulsive force. g.layout.repulsion = 5 # Repulsion radius. dragged = None
class KeywordGraphGUI(): """Draw and interact with the graph. """ def __init__(self, title): self.edges_data = {} self.nodes = [] self.canvas = Canvas(width=1000, height=600, name=title, resizable=True) self.g = Graph() self.selected_node = None self.clicked = None self.translate_x = 500 self.translate_y = 300 self.dragging = False self.ctrl_pressed = False self.first_node = None self.selected_edge = None def add_node(self, name, root=False): if name in self.nodes: return self.nodes.append(name) self.g.add_node(id=name, radius=10, root=root, fill=Color(1, 0.7, 0.0, 1), fontsize=12) def add_edge(self, n1, n2, data=None, stroke=Color(0, 0, 0, 0.3), strokewidth=2, *args, **kwargs): self.add_node(n1) self.add_node(n2) self.edges_data[(n1, n2)] = data self.g.add_edge(n1, n2, stroke=stroke, strokewidth=strokewidth, *args, **kwargs) def _draw(self): canvas.clear() background(1) translate(self.translate_x, self.translate_y) # With directed=True, edges have an arrowhead indicating the direction of the connection. # With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes. # With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold. # This requires some extra calculations. self.g.draw(weighted=0.5, directed=True) # self.g.update(iterations=10) dx = self.canvas.mouse.x - self.translate_x # Undo translate() dy = self.canvas.mouse.y - self.translate_y if self.canvas.mouse.pressed and self.selected_node is None: current_node = self.g.node_at(dx, dy) if self.canvas.mouse.dragged and current_node is not None: self.selected_node = current_node if current_node is None and self.canvas.mouse.dragged: delta_x = self.canvas.mouse.dx delta_y = self.canvas.mouse.dy # if delta_x > 1 or delta_y > 1: self.translate_x += delta_x self.translate_y += delta_y if current_node is not None and not self.canvas.mouse.dragged and self.ctrl_pressed: if self.first_node is None: self.first_node = current_node elif current_node.id != self.first_node.id: edge = self.g.edge(self.first_node.id, current_node.id) if edge is None: edge = self.g.edge(current_node.id, self.first_node.id) if edge is not None: if self.selected_edge is not None: self.selected_edge.stroke = (0, 0, 0, 0.3) self.selected_edge = edge self.selected_edge.stroke = Color(1, 0, 0, 0.6) if not self.canvas.mouse.pressed: self.selected_node = None self.dragging = False # check hover hovering_node = self.g.node_at(dx, dy) if hovering_node is not None and not self.ctrl_pressed and self.selected_edge is not None: node_id = hovering_node.id if self.selected_edge.node1.id == node_id or self.selected_edge.node2.id == node_id: edge = (self.selected_edge.node1.id, self.selected_edge.node2.id) if self.selected_edge.node1.id == node_id: data = self.edges_data[edge][0] else: data = self.edges_data[edge][1] push() self._draw_node_data(hovering_node, data) pop() if self.selected_node is not None: self.selected_node.x = dx self.selected_node.y = dy def _draw_node_data(self, node, data): lines = [Text(t, width=300, fontname="Droid Sans Mono") for t in data if len(t) < 30] if len(lines) <= 0: return lines = lines[:20] cur_height = 0.0 for l in lines: cur_height += textheight(l) + 5 box_width = 300 box_height = cur_height box = rect(node.x - box_width - 5, node.y - box_height - 5, box_width + 5, box_height + 5, fill=Color(0.8, 0.8, 0.8, 1)) cur_height = 0.0 for l in lines: cur_height += textheight(l) + 5 l.draw(node.x - 300, node.y - cur_height) def _on_key_press(self, keys): if keys.pressed and (keys.char is None or keys.char == ''): if keys.modifiers is not None and len(keys.modifiers) > 0 and keys.modifiers[0] == 'ctrl': self.ctrl_pressed = True def _on_key_release(self, keys): if keys.char is None or keys.char == '': if keys.modifiers is not None and len(keys.modifiers) > 0 and keys.modifiers[0] == 'ctrl': self.ctrl_pressed = False self.first_node = None def start(self, iterations=70, distance=30, force=0.01, repulsion_radius=30): """Starts the GUI """ self.g.distance = distance # Overall spacing between nodes. self.g.layout.force = force # Strength of the attractive & repulsive force. self.g.layout.repulsion = repulsion_radius # Repulsion radius. self.g.update(iterations=iterations) self.canvas.on_key_press = self._on_key_press self.canvas.on_key_release = self._on_key_release self.canvas.draw = self._draw self.canvas.run()
from nodebox.graphics import * from nodebox.graphics.physics import Node, Edge, Graph # Create a graph with randomly connected nodes. # Nodes and edges can be styled with fill, stroke, strokewidth parameters. # Each node displays its id as a text label, stored as a Text object in Node.text. # To hide the node label, set the text parameter to None. abo = list("ATGGCCGAGGTGTTGCGGACGCTGGCCGGAAAACCAAAATGCCACGCACTTCGACCTATGATCCTTTTCCTAATAATGCTTGTCTTGGTCTTGTTTGGTTACGGGGTCCTAAGCCCCAGAAGTCTAATGCCAGGAAGCCTGGAACGGGGGTTC") g = Graph() # bases as nodes index = 1 for b in abo: g.add_node(id=index, radius = 6, stroke = color(0), text = color(.3,.6,.9)) index += 1 # Random edges. for n in range(0,len(abo)-1): node1 = g.nodes[n] node1.text = Text(abo[n], font="Droid Serif", fontsize=5, fontweight=BOLD) node2 = g.nodes[n+1] g.add_edge(node1, node2, length = 1.0, weight = 1.0, stroke = color(.7,.1,.1)) #g.prune(depth=0) # Remove orphaned nodes with no connections. g.distance = 5 # Overall spacing between nodes. g.layout.force = 0.01 # Strength of the attractive & repulsive force.
class GUI(): '''Draw and interact with the damn thing. ''' drag = [] def __init__(self): '''rawgraph is an instance of the RawGraph class The GUI allows to interact with it, i.e. view it and change it. ''' #Layer.__init__(self) # unknown thing, might be necessary. #Buttons self.nodes = [] self.g = Graph() self.dragged =None self.clicked = None self.drawInitial() self.start() def add_node(self, name, root = False): self.nodes.append(name) self.g.add_node(id=name, radius = 5, root = root) def add_edge(self,n1,n2,*args,**kwargs): self.g.add_edge(n1, n2, *args,**kwargs) def close_canvas(self): """Close the canvas """ canvas.clear() canvas.stop() def draw(self): canvas.clear() background(0,0,0,1.0) translate(500, 500) # With directed=True, edges have an arrowhead indicating the direction of the connection. # With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes. # With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold. # This requires some extra calculations. self.g.draw(weighted=False, directed=False) self.g.update(iterations=5) dx = canvas.mouse.x - 500 # Undo translate(). dy = canvas.mouse.y - 500 global dragged global drag temp = None if canvas.mouse.pressed: if self.g.node_at(dx,dy): self.dragged = self.g.node_at(dx, dy) self.drag.append(self.dragged) self.dragged.changeColor() canvas.update() else: if self.drag: for temp in self.drag: temp.resetColor() canvas.update() else: pass if canvas._keys.pressed : self.on_key_press(self.keys) # Make it interactive! # When the mouse is pressed, remember on which node. # Drag this node around when the mouse is moved. dx = canvas.mouse.x - 500 # Undo translate(). dy = canvas.mouse.y - 500 global dragged if not canvas.mouse.pressed: self.dragged = None if self.dragged: self.dragged.x = dx self.dragged.y = dy def deleteNode(self): global delNode temp= None for temp in nodeArray: delNode=delNode+1 self.g.DeleteNode(temp) nodeArray.remove(temp) if delNode == 50: delNode=0 break def textFunction(self, *args, **kwargs): global cNeut global cPos global cVPos global cNeg global cVNeg tot=cNeut+cPos+cVPos+cNeg+cVNeg sxt0="Total Tweets Parsed : "+str(tot) sxt1="Neutral Tweets : "+str(cNeut) sxt2="Positive Tweets : "+str(cPos) sxt3="Very Positive Tweets : "+str(cVPos) sxt4="Negative Tweets : "+str(cNeg) sxt5="Very Negative Tweets : "+str(cVNeg) label = pyglet.text.Label(sxt0,font_name='Times New Roman', font_size=25,x=1000, y=135,anchor_x='left', anchor_y='center',color=(0,191,255,255)) label3 = pyglet.text.Label(sxt3,font_name='Times New Roman', font_size=15,x=1005, y=65,anchor_x='left', anchor_y='center',color=(0,130,0,255)) label2 = pyglet.text.Label(sxt2,font_name='Times New Roman', font_size=15,x=1005, y=85,anchor_x='left', anchor_y='center',color=(255,255,0,255)) label1 = pyglet.text.Label(sxt1,font_name='Times New Roman', font_size=15,x=1005, y=105,anchor_x='left', anchor_y='center',color=(220,220,220,255)) label4 = pyglet.text.Label(sxt4,font_name='Times New Roman', font_size=15,x=1005, y=45,anchor_x='left', anchor_y='center',color=(255,165,0,255)) label5 = pyglet.text.Label(sxt5,font_name='Times New Roman', font_size=15,x=1005, y=25,anchor_x='left', anchor_y='center',color=(255,0,0,255)) label.draw() label3.draw() label2.draw() label1.draw() label4.draw() label5.draw() def updateFunction(self, *args, **kwargs): global nodeArray global csv_f global cNeut global cPos global cVPos global cNeg global cVNeg global const global updRcNeut global updRcPos global updRcVPos global updRcNeg global updRcVNeg for i in range(5): #print "ankky" try: row = csv_f.next() tweet = row[2] PsTweet = row[1] print tweet, scoreUpdate #print tweet sTweet=scoreUpdate(tweet) if len(nodeArray)==500: self.deleteNode() if sTweet == 0: print "Neutral" self.g.add_node(tweet,text=False,fill=(0.86,0.86,0.86,1)) self.add_edge("Neutral", tweet, length=2,stroke = (0.8,0.75,0.69,1)) if updRcNeut > const +1: updRcNeut = 0 else: updRcNeut = updRcNeut + 1 cNeut=cNeut+1 nodeArray.append(tweet) elif sTweet < -3: cVNeg=cVNeg+1 self.g.add_node(tweet,text=False,fill=(1,0,0,1)) self.add_edge("Highly Negative",tweet, length=2*(abs(sTweet)),stroke = (0.8,0.75,0.69,1)) if updRcVNeg > const +1: updRcVNeg = 0 else: updRcVNeg = updRcVNeg + 1 nodeArray.append(tweet) elif sTweet < 0 and sTweet >= -3: cNeg=cNeg+1 self.g.add_node(tweet,text=False,fill=(1,0.65,0,1)) self.add_edge("Negative", tweet, length=2*abs(sTweet),stroke = (0.8,0.75,0.69,1)) if updRcNeg > const +1: updRcNeg = 0 else: updRcNeg = updRcNeg + 1 nodeArray.append(tweet) elif sTweet > 3: cVPos=cVPos+1 self.g.add_node(tweet,text=False,fill=(0,0.5,0,1)) self.add_edge("Highly Positive", tweet, length=2*abs(sTweet),stroke = (0.8,0.75,0.69,1)) if updRcVPos > const +1: updRcVPos = 0 else: updRcVPos = updRcVPos + 1 nodeArray.append(tweet) else: cPos=cPos+1 self.g.add_node(tweet,text=False,fill=(1,1,0,1)) self.add_edge("Positive", tweet, length=2*abs(sTweet),stroke = (0.8,0.75,0.69,1)) if updRcPos > const +1: updRcPos = 0 else: updRcPos = updRcPos + 1 nodeArray.append(tweet) except StopIteration: print "Still waiting for tweets to be fetched!" pass def updateRadii(self): global updRcNeut global updRcPos global updRcVPos global updRcNeg global updRcVNeg self.g.tempNode = self.g.get_node("Highly Positive") self.g.tempNode.makeItBig(Alpha = updRcVPos/25) self.g.tempNode = self.g.get_node("Positive") self.g.tempNode.makeItBig(Alpha = updRcPos/25) self.g.tempNode = self.g.get_node("Neutral") self.g.tempNode.makeItBig(Alpha = updRcNeut/25) self.g.tempNode = self.g.get_node("Negative") self.g.tempNode.makeItBig(Alpha = updRcNeg/25) self.g.tempNode = self.g.get_node("Highly Negative") self.g.tempNode.makeItBig(Alpha = updRcVNeg/25) def drawInitial(self): self.g.add_node("Highly Positive",radius=10, fill=(0,0.5,0,1),text=False) self.g.add_node("Positive",radius=10, fill=(1,1,0,1),text=False) self.g.add_node("Neutral",radius=10, fill=(0.86,0.86,0.86,1),text=False) self.g.add_node("Negative",radius=10, fill=(1,0.65,0,1),text=False) self.g.add_node("Highly Negative",radius=10, fill=(1,0,0,1),text=False) self.g.add_edge("Highly Positive","Positive",length=10, stroke = (0.8,0.75,0.69,1)) self.g.add_edge("Positive","Neutral",length=10, stroke = (0.8,0.75,0.69,1)) self.g.add_edge("Neutral","Negative",length=10, stroke = (0.8,0.75,0.69,1)) self.g.add_edge("Negative","Highly Negative",length=10, stroke = (0.8,0.75,0.69,1)) def start(self, distance = 15, force = 0.01, repulsion_radius=5): """Starts the GUI """ #self.g.prune(depth=0) # Remove orphaned nodes with no connections. self.g.distance = distance # Overall spacing between nodes. self.g.layout.force = force # Strength of the attractive & repulsive force. self.g.layout.repulsion = repulsion_radius # Repulsion radius. canvas.size = 1000, 1000 canvas._set_fullscreen(True) canvas.draw = self.draw sleep(10) pyglet.clock.schedule_interval(self.updateFunction, 2) pyglet.clock.schedule_interval(self.textFunction, 0.1) canvas.run()
def create_graph(p_node, s): global g g = Graph() p = p_node temp = s.pAll[p_node].follower g.add_node(id=str(p_node), radius=5, stroke=color(1, 0, 0.25, 1), text=color(1)) for i in range(len(temp)): #100 g.add_node(id=str(temp[i]), radius=5, stroke=color(1), text=color(1)) # Random edges. p_links = s.pAll[p_node].follower for i in range(len(p_links)): node1 = str(p_node) #choice(g.nodes) node2 = str(p_links[i]) #choice(g.nodes) g.add_edge(node1, node2, length=500.0, weight=random(), stroke=color(1, 0, 0.25, 1)) #New Round for I in range(len(temp)): p_node = s.pAll[p].follower[I] #0 p_links = s.pAll[p_node].follower #print [x * 0.01 for x in range(0,100)] r = rd.choice([x * 0.01 for x in range(0, 100) ]) #rd.choice([0,51,255]) #rd.randint(10,200) green = rd.choice([x * 0.01 for x in range(0, 100) ]) #rd.choice([255,128,255]) #rd.randint(0,100) b = rd.choice([x * 0.01 for x in range(0, 100) ]) #rd.choice([128,0,255]) #rd.randint(0,200) #print r,green,b for i in range(len(p_links)): node1 = str(p_node) #choice(g.nodes) bul = True if p_links[i] not in s.pAll[p].follower: #Not a parent follower for j in range(I): #Loop to check another sibling's follower sibling = s.pAll[p].follower[j] if p_links[i] in s.pAll[sibling].follower: bul = False if bul: g.add_node(id=str(p_links[i]), radius=5, stroke=color(r, green, b, 1), text=color(1)) node2 = str(p_links[i]) #choice(g.nodes) g.add_edge(node1, node2, length=50.0, weight=random(), stroke=color(r, green, b, 1)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = 5 #node.radius + node.radius*node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1: node.edges[0].length = 0.5 g.prune(depth=0) # Remove orphaned nodes with no connections. g.distance = 15 # Overall spacing between nodes. g.layout.force = 0.01 # Strength of the attractive & repulsive force. g.layout.repulsion = 5 # Repulsion radius. dragged = None
# Nodes and edges can be styled with fill, stroke, strokewidth parameters. # Each node displays its id as a text label, stored as a Text object in Node.text. # To hide the node label, set the text parameter to None. ##g handles the graphics of graph drawing g = Graph() #G2 handles the loading and saving of the graph G2 = nx.Graph() #load the graph from saved file G2 = nx.read_adjlist(fileN) #plot loaded nodes for drug in G2.nodes(): classId = su.parseClass(drug) g.add_node(id=su.parseName(drug), id2 = classId, radius=8, stroke=color(0), fill=colors[classId - 1], text=color(0)) #plot edges for ed in G2.edges(): node1 = su.parseName(ed[0]) node2 = su.parseName(ed[1]) g.add_edge(node1, node2, length=20.0, weight=1.0, stroke=color(0)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = node.radius + node.radius*node.weight*0.3 # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1:
# Add the upper directory (where the nodebox module is) to the search path. import os, sys; sys.path.insert(0, os.path.join("..","..")) from nodebox.graphics import * from nodebox.graphics.physics import Node, Edge, Graph # Create a graph with randomly connected nodes. # Nodes and edges can be styled with fill, stroke, strokewidth parameters. # Each node displays its id as a text label, stored as a Text object in Node.text. # To hide the node label, set the text parameter to None. g = Graph() # Random nodes. for i in range(100): g.add_node(id=str(i+1), radius = 5, stroke = color(1), text = color(1)) # Random edges. for i in range(1,100): node1 = 0#choice(g.nodes) node2 = str(i)#choice(g.nodes) g.add_edge(node1, node2, length = 200.0, weight = random(), stroke = color(1, 0, 0.25, 0.75)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = node.radius + node.radius*node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection.
# Add the upper directory (where the nodebox module is) to the search path. import os, sys sys.path.insert(0, os.path.join("..", "..")) from nodebox.graphics import * from nodebox.graphics.physics import Node, Edge, Graph # Create a graph with randomly connected nodes. # Nodes and edges can be styled with fill, stroke, strokewidth parameters. # Each node displays its id as a text label, stored as a Text object in Node.text. # To hide the node label, set the text parameter to None. g = Graph() # Random nodes. for i in range(50): g.add_node(id=str(i + 1), radius=5, stroke=color(0), text=color(0)) # Random edges. for i in range(75): node1 = choice(g.nodes) node2 = choice(g.nodes) g.add_edge(node1, node2, length=1.0, weight=random(), stroke=color(0)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = node.radius + node.radius * node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1: node.edges[0].length *= 0.1 g.prune(depth=0) # Remove orphaned nodes with no connections.
def create_graph(p_node, s): completed_nodes = [] uncomplete_nodes = [] global g g = Graph() p = p_node temp = s.pAll[p_node].follower g.add_node(id=str(p_node), radius=5, stroke=color(1, 0, 0.25, 1), text=color(1)) ## print "-"*50 ## print "parent_node ",p_node ## print "parent follower ",temp for i in range(len(temp)): #100 g.add_node(id=str(temp[i]), radius=5, stroke=color(1), text=color(1)) completed_nodes.append(p_node) # Random edges. p_links = s.pAll[p_node].follower for i in range(len(p_links)): node1 = str(p_node) #choice(g.nodes) node2 = str(p_links[i]) #choice(g.nodes) uncomplete_nodes.append(p_links[i]) g.add_edge(node1, node2, length=500.0, weight=random(), stroke=color(1, 0, 0.25, 1)) ## print "-"*50 ## print "Completed_Nodes ",completed_nodes ## print "Uncomplete_Nodes ",uncomplete_nodes while len(uncomplete_nodes) <> 0: node1 = uncomplete_nodes[0] follower_list = s.pAll[node1].follower ## print "node1 which became parent ",node1 for i in follower_list: if i not in completed_nodes: node_1 = str(node1) node2 = str(i) print node_1, "--->", i uncomplete_nodes.append(i) g.add_node(id=str(i), radius=5, stroke=color(1, 0, 0.25, 1), text=color(1)) g.add_edge(node_1, node2, length=50.0, stroke=color(1), weight=random()) completed_nodes.append(node1) del uncomplete_nodes[0] ## print "Completed_Nodes ",completed_nodes ## print "Uncomplete_Nodes ",uncomplete_nodes #New Round ## for I in range(len(temp)): ## ## p_node = s.pAll[p].follower[I] #0 ## p_links = s.pAll[p_node].follower ## ## print "Sibling Node ",p_node ## print "Sibling Follower ",p_links ## ## #print [x * 0.01 for x in range(0,100)] ## r = rd.choice([x * 0.01 for x in range(0,100)])#rd.choice([0,51,255]) #rd.randint(10,200) ## green = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([255,128,255]) #rd.randint(0,100) ## b = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([128,0,255]) #rd.randint(0,200) ## ## #print r,green,b ## ## for i in range(len(p_links)): ## node1 = str(p_node)#choice(g.nodes) ## ## bul = True ## if p_links[i] not in s.pAll[p].follower: #Not a parent follower ## ## for j in range (I): #Loop to check another sibling's follower ## ## sibling = s.pAll[p].follower[j] ## ## if p_links[i] in s.pAll[sibling].follower: ## ## bul = False ## ## if bul: ## g.add_node(id=str(p_links[i]),radius = 5,stroke = color(r, green, b, 1),text = color(1)) ## node2 = str(p_links[i])#choice(g.nodes) ## g.add_edge(node1, node2, ## length = 50.0, ## weight = random(), ## stroke = color(r, green, b, 1)) ## # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = 5 #node.radius + node.radius*node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1: node.edges[0].length = 0.5 g.prune(depth=0) # Remove orphaned nodes with no connections. g.distance = 15 # Overall spacing between nodes. g.layout.force = 0.01 # Strength of the attractive & repulsive force. g.layout.repulsion = 5 # Repulsion radius. dragged = None
def create_graph(p_node,s): global g g = Graph() p = p_node temp = s.pAll[p_node].follower g.add_node(id=str(p_node),radius = 5,stroke = color(1, 0, 0.25, 1),text = color(1)) for i in range(len(temp)):#100 g.add_node(id=str(temp[i]), radius = 5, stroke = color(1), text = color(1)) # Random edges. p_links = s.pAll[p_node].follower for i in range(len(p_links)): node1 = str(p_node)#choice(g.nodes) node2 = str(p_links[i])#choice(g.nodes) g.add_edge(node1, node2, length = 500.0, weight = random(), stroke = color(1, 0, 0.25, 1)) #New Round for I in range(len(temp)): p_node = s.pAll[p].follower[I] #0 p_links = s.pAll[p_node].follower #print [x * 0.01 for x in range(0,100)] r = rd.choice([x * 0.01 for x in range(0,100)])#rd.choice([0,51,255]) #rd.randint(10,200) green = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([255,128,255]) #rd.randint(0,100) b = rd.choice([x * 0.01 for x in range(0,100)]) #rd.choice([128,0,255]) #rd.randint(0,200) #print r,green,b for i in range(len(p_links)): node1 = str(p_node)#choice(g.nodes) bul = True if p_links[i] not in s.pAll[p].follower: #Not a parent follower for j in range (I): #Loop to check another sibling's follower sibling = s.pAll[p].follower[j] if p_links[i] in s.pAll[sibling].follower: bul = False if bul: g.add_node(id=str(p_links[i]),radius = 5,stroke = color(r, green, b, 1),text = color(1)) node2 = str(p_links[i])#choice(g.nodes) g.add_edge(node1, node2, length = 50.0, weight = random(), stroke = color(r, green, b, 1)) # Two handy tricks to prettify the layout: # 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger. for node in g.nodes: node.radius = 5#node.radius + node.radius*node.weight # 2) Nodes with only one connection ("leaf" nodes) have a shorter connection. for node in g.nodes: if len(node.edges) == 1: node.edges[0].length = 0.5 g.prune(depth=0) # Remove orphaned nodes with no connections. g.distance = 15 # Overall spacing between nodes. g.layout.force = 0.01 # Strength of the attractive & repulsive force. g.layout.repulsion = 5 # Repulsion radius. dragged = None
class GUI(): """Draw and interact with the damn thing. """ def __init__(self): """rawgraph is an instance of the RawGraph class The GUI allows to interact with it, i.e. view it and change it. """ #Layer.__init__(self) # unknown thing, might be necessary. #Buttons self.EXPLORE_F = "Explore Further" self.EXPLORE_D = "Explore Deeper" self.DELETE = "Delete" self.CLOSE = "CLOSE" self.BUTTONS = False #Are buttons showing? self.CBUTTON = False #Is Close button showing? self.nodes = [] self.g = Graph() self.dragged =None self.clicked = None def add_node(self, name, root = False): self.nodes.append(name) self.g.add_node(id=name, radius = 5, root = root) def add_edge(self,n1,n2,*args,**kwargs): self.g.add_edge(n1, n2, *args,**kwargs) def explore_further(self,node_name): """action of explore further button """ pass def explore_deeper(self,node_name): """action of explore deeper button """ pass def delete_node(self,node_name): """action of delete button """ pass def close_canvas(self): """Close the canvas """ canvas.clear() canvas.stop() def add_close(self): """Add close button """ self.g.add_node(self.CLOSE, radius = 10, fill=(1,0,0,0.2)) self.g.add_edge(self.rawg.root, self.CLOSE, length=0.6) self.CBUTTON=True def remove_close(self): """Remove the close button """ try: self.g.remove(self.g.node(self.CLOSE)) except: pass self.CBUTTON=False def add_buttons(self,node_name): """Add the buttons to change the graph """ self.g.add_node(self.EXPLORE_F, radius = 6, fill=(0,1,0,0.5)) self.g.add_node(self.EXPLORE_D, radius = 6, fill=(0,0,1,0.5)) self.g.add_node(self.DELETE,radius=6, fill=(1,0,0,0.5)) self.g.add_edge(node_name, self.EXPLORE_F, length=0.2) self.g.add_edge(node_name, self.EXPLORE_D, length=0.2) self.g.add_edge(node_name, self.DELETE, length=0.2) self.BUTTONS = True def remove_buttons(self): """Remove the buttons to change the graph """ try: self.g.remove(self.g.node(self.DELETE)) self.g.remove(self.g.node(self.EXPLORE_D)) self.g.remove(self.g.node(self.EXPLORE_F)) except: pass self.BUTTONS=False def draw(self): canvas.clear() background(1) translate(500, 500) # With directed=True, edges have an arrowhead indicating the direction of the connection. # With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes. # With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold. # This requires some extra calculations. self.g.draw(weighted=0.5, directed=False) self.g.update(iterations=10) # Make it interactive! # When the mouse is pressed, remember on which node. # Drag this node around when the mouse is moved. dx = canvas.mouse.x - 500 # Undo translate(). dy = canvas.mouse.y - 500 #global dragged if canvas.mouse.pressed and not self.dragged: self.dragged = self.g.node_at(dx, dy) old_clicked = self.clicked try: self.clicked = self.dragged.id except: self.clicked = None if self.clicked != None: if self.clicked == self.DELETE: if old_clicked != None: self.delete_node(old_clicked) self.remove_buttons() elif self.clicked == self.EXPLORE_D: if old_clicked != None: self.explore_deeper(old_clicked) self.remove_buttons() elif self.clicked == self.EXPLORE_F: if old_clicked != None: self.explore_further(old_clicked) self.remove_buttons() elif self.clicked == self.CLOSE: self.remove_buttons() self.remove_close() self.close_canvas() else: self.remove_buttons() self.remove_close() self.add_buttons(self.clicked) else: if self.BUTTONS: self.remove_buttons() elif self.CBUTTON: self.remove_close() else: self.remove_buttons() self.add_close() if not canvas.mouse.pressed: self.dragged = None if self.dragged: self.dragged.x = dx self.dragged.y = dy def start(self, distance = 30, force = 0.01, repulsion_radius=30): """Starts the GUI """ #self.g.prune(depth=0) # Remove orphaned nodes with no connections. self.g.distance = distance # Overall spacing between nodes. self.g.layout.force = force # Strength of the attractive & repulsive force. self.g.layout.repulsion = repulsion_radius # Repulsion radius. canvas.draw = self.draw #canvas.size = 1000, 700 canvas.fullscreen = True canvas.run()
def test_total(self): root_website = "http://www.meawnam.com" file_data = open(os.getcwd() + "\\other\\test_total.html", "r+") html_code = file_data.read() file_data.close() data_str_html = self.spider.get_html_code_to_datastr( root_website, html_code) self.assertEqual( data_str_html, "GAMEBOY [GOOGLE](http://www.google.com) " "[electric](http://www.electric.com) " "[spotlight](http://www.spotlight.com)") content_html = self.spider.get_content_from_datastr(data_str_html) self.assertEqual(content_html, "GAMEBOY") weblink_html = self.spider.get_weblink_from_datastr(data_str_html) self.assertEqual(weblink_html, "GOOGLE electric spotlight") website_list = self.spider.get_website_from_datastr(data_str_html) self.assertListEqual(website_list, [ "http://www.google.com", "http://www.electric.com", "http://www.spotlight.com" ]) dict_json = { root_website: { self.spider.get_netloc(root_website): { root_website: { "content": content_html + " " + weblink_html, "website": website_list } } } } content_dict = {root_website: content_html + " " + weblink_html} website_dict = {root_website: website_list} self.assertDictEqual( self.spider.get_json_string_for_deep(root_website, website_dict, content_dict), dict_json) graph = Graph() graph.add_node("www.meawnam.com") graph.add_node("www.google.com") graph.add_node("www.electric.com") graph.add_node("www.spotlight.com") graph.add_edge("www.meawnam.com", "www.google.com") graph.add_edge("www.meawnam.com", "www.electric.com") graph.add_edge("www.meawnam.com", "www.electric.com") graph.add_edge("www.meawnam.com", "www.spotlight.com") self.assertEqual(graph, self.spider.set_into_graph(root_website, dict_json)) dict_n_used = { "www.meawnam.com": 0, "www.google.com": 1, "www.electric.com": 1, "www.spotlight.com": 1 } self.assertEqual(dict_n_used, self.spider.set_n_used(root_website, dict_json)) save_file = open(os.getcwd() + "\\other\\test_index_total.json", "w+") save_file.write(json.dumps(dict_json, indent=4, sort_keys=True)) save_file.close() file_list = [os.getcwd() + "\\other\\test_index_total.json"] index_dict = self.spider.indexing({}, file_list) my_indexing = { "gameboy": { "http://www.meawnam.com": { "used": 0, "word": 1 } }, "google": { "http://www.meawnam.com": { "used": 0, "word": 1 } }, "electric": { "http://www.meawnam.com": { "used": 0, "word": 1 } }, "spotlight": { "http://www.meawnam.com": { "used": 0, "word": 1 } } } self.assertDictEqual(index_dict, my_indexing) ranking_dict = self.spider.ranking(index_dict) my_ranking = { "gameboy": [("http://www.meawnam.com", { "used": 0, "word": 1 })], "google": [("http://www.meawnam.com", { "used": 0, "word": 1 })], "electric": [("http://www.meawnam.com", { "used": 0, "word": 1 })], "spotlight": [("http://www.meawnam.com", { "used": 0, "word": 1 })] } self.assertDictEqual(ranking_dict, my_ranking)