예제 #1
0
 def __init__(self, spider_model):
     print "start : init : SpiderView"
     self.spider_model = spider_model  # get data from spider model
     self.show_graph = Graph()  # graph to draw
     self.dict_used = {}
     self.index_panel = Panel("Indexing", fixed=False, modal=True)
     self.search_panel = Panel("Search..", x=450, fixed=False, modal=True)
     self.result_text = Text("")  # set text to show result
     self.update_complete_text = Text("")  # set text to update status
     self.update_state = 0  # State of update
     self.index_website = ""
     self.deep = 0
     self.graph_website = ""
     self.website_field = ""
     print "complete : init : SpiderView"
예제 #2
0
 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
예제 #3
0
    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()
예제 #4
0
    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
예제 #5
0
    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 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
예제 #7
0
# 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.
예제 #8
0
파일: 07-graph.py 프로젝트: msarch/py
# 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.
예제 #9
0
# parse command line arguments #
################################
parser = argparse.ArgumentParser(description='prissoner dilema network simulation')
parser.add_argument('--db_url', default='sqlite:///db.sqlite', help='DB URL, default: sqlite:///db.sqlite')
# parser.add_argument('--mode', required=True, choices=['init','walk'])
args = parser.parse_args()


####################
# database connect #
####################
engine  = create_engine(args.db_url)
do.Session = sessionmaker(bind=engine)

# network as global
g = Graph()



def create_network_from_db():
    session = do.Session()
    # load nodes
    for n in session.query(do.Node).all():
        if n.state:
            fill = color(0.1,0.1,0.7)
        else:
            fill = color(0.7,0.1,0.1)

        g.add_node(id=n.id,
                   radius = n.fitness,
                   stroke = color(0,0,0,0), 
예제 #10
0
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.
예제 #11
0
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
예제 #12
0
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
예제 #13
0
class SpiderView:
    def __init__(self, spider_model):
        print "start : init : SpiderView"
        self.spider_model = spider_model  # get data from spider model
        self.show_graph = Graph()  # graph to draw
        self.dict_used = {}
        self.index_panel = Panel("Indexing", fixed=False, modal=True)
        self.search_panel = Panel("Search..", x=450, fixed=False, modal=True)
        self.result_text = Text("")  # set text to show result
        self.update_complete_text = Text("")  # set text to update status
        self.update_state = 0  # State of update
        self.index_website = ""
        self.deep = 0
        self.graph_website = ""
        self.website_field = ""
        print "complete : init : SpiderView"

    def set_data_showing(self, show_graph, dict_used):
        self.show_graph = show_graph  # graph to draw
        self.dict_used = dict_used
        self.deep = self.get_deep()
        self.graph_website = self.get_root_website()

    # method to set GUI for
    def set_gui(self, my_canvas):
        print "start : set_gui : SpiderView"
        layout = Rows(width=250, height=720)  # set layout
        layout.extend([  # add object in layout
            # add field to insert website
            ("Website", Field(value="", id="web_text", wrap=True)),
            ("Deep",
             Field(value="1", id="deep_text",
                   wrap=True)),  # add field to get deep of searching website
            Button("Update Website",
                   action=update_website_func),  # add button to update website
            Button("Pause", action=pause_func),  # add button to update website
            Button("Show output",
                   action=show_output_func),  # add button to show output
            Button("Indexing", action=indexing_func),  # add button to indexing
            Row(controls=[
                Button("<prev", action=prev_func),
                Button("next>", action=next_func)
            ])
        ])
        self.index_panel.append(layout)  # add layout to index_panel
        self.index_panel.pack()  # show index_panel
        my_canvas.append(self.index_panel)  # add index_panel into canvas

        layout = Rows(width=300, height=300)  # set layout
        layout.extend([  # add object in layout
            # add field to insert website
            ("Search", Field(value="tracer", id="word_text",
                             wrap=True)),  # add field to insert word to search
            Field(id="web_list_text", width=500, height=100,
                  wrap=True),  # add file to show result after search
            Button("Go", action=go_func),  # add button to indexing
        ])
        self.search_panel.append(layout)  # add layout to index_panel
        self.search_panel.pack()  # show index_panel
        my_canvas.append(self.search_panel)  # add index_panel into canvas
        print "complete : set_gui : SpiderView"

    def draw_graph(self, my_canvas):
        # print "start : draw_graph : SpiderView"
        my_canvas.size = 800, 600  # set size of canvas
        my_canvas.clear()  # clear canvas
        offset_x = my_canvas.width / 2  # set offset x of graph
        offset_y = my_canvas.height / 2  # set offset y of graph
        push()  # push matrix
        background(Color(255))  # set background = white
        translate(offset_x, offset_y)  # move graph to offset

        mx = my_canvas.mouse.x - offset_x  # get x of mouse
        my = my_canvas.mouse.y - offset_y  # get y of mouse

        if len(self.show_graph.nodes) > 0:
            root_node_id = self.spider_model.get_netloc(self.graph_website)
            node_deep_list = self.show_graph.node(root_node_id).flatten(
                depth=self.deep)
            for node in self.show_graph.nodes:
                if node not in node_deep_list:
                    self.show_graph.remove(node)
            for node in self.show_graph.nodes:  # get node from graph
                num = self.dict_used[node.id]  # get number of used (netloc)
                node.radius = 8 + int(num * 0.05)  # set radius
                node.stroke = Color(255)  # set node stroke color
                node.fill = Color(0)  # set node color
                if node.id != "":  # detect no word node
                    node.text.fill = None  # set node text = ""
                if node.id == root_node_id:
                    node.text.fill = Color(0)
                    node.fill = Color(255, 0, 0)
                else:
                    node.text.fill = Color(0)
            for edge in self.show_graph.edges:  # get edge from graph
                edge.length = 20  # set edge length
                edge.stroke = Color(0)  # set stroke color
                edge.strokewidth = 2  # set stroke width
            node_choose = self.show_graph.node_at(mx, my)
            if node_choose != None:
                node_choose.fill = Color(0, 255, 0)
                self.draw_result_text(my_canvas, node_choose.id,
                                      num)  # set text of result
            self.show_graph.draw(directed=True)  # draw grpah
            self.show_graph.update()  # update graph
        pop()  # pop matrix
        self.result_text.draw(x=20,
                              y=my_canvas.height - 70)  # draw result text
        self.draw_update_text(my_canvas)  # draw update text
        # print "complete : draw_graph : SpiderView"

    # method that set result text from node (netloc, n_used, n_word)
    def draw_result_text(self, my_canvas, netloc="", n_used=""):
        # print "start : draw_result_text : SpiderView"
        self.result_text.fill = Color(0)  # set color result text
        self.result_text.text = netloc + "\n\nWeb is used : " + str(n_used)
        # print "complete : draw_result_text : SpiderView"

    # method that set update text state
    def draw_update_text(self, my_canvas):
        # print "start : draw_update_text : SpiderView"
        self.update_complete_text.x = my_canvas.width - 250  # set x of update text
        self.update_complete_text.y = my_canvas.height - 70  # set y of update text
        try:
            # print "start : draw_update_text : SpiderView"
            self.update_complete_text.fontsize = 8  # set font size
            self.update_complete_text.fill = Color(0)  # set update text color
            if self.update_state == 0:
                self.update_complete_text.text = "Welcome to Spider"  # set text update_complete
            elif self.update_state == 1:  # spider update complete
                index_website = str(self.index_website)
                self.update_complete_text.text = "Updating....\n" + index_website
            elif self.update_state == 2:  # spider update not complete
                self.update_complete_text.text = "Update Complete"  # set text update_complete
            elif self.update_state == 3:
                self.update_complete_text.text = "Update Pause"  # set text update_complete
            self.update_complete_text.draw()  # draw update text
            # print "complete : draw_update_text : SpiderView"
        except UnicodeError:
            print "Error : draw_update_text : SpiderView"

    # show data after search
    def show_search(self, rank_dict):
        print "start : show_search : SpiderView"
        word = self.search_panel.word_text.value.strip().lower(
        )  # set word to lower
        word_list = word.split()  # get list word from GUI
        if len(word_list) == 1:  # Found only one word
            if word in rank_dict:  # detect word is in word dict
                index = 1  # start index
                str_write_to_panel = ""
                print rank_dict[word]
                for data_pack in rank_dict[
                        word]:  # loop data pack from dict of word
                    website = data_pack[0]  # get website
                    n_used = data_pack[1]["used"]  # get number of used
                    n_word = data_pack[1]["word"]  # get number of word

                    # set data to search panel
                    str_write_to_panel += str(index) + ".) Website : " \
                                                     + str(website.encode("ascii", "ignore")) \
                                                     + "\n\t" + "Used : " + str(n_used) \
                                                     + "\n\t" + "Found : " + str(n_word) + "\n"
                    index += 1  # increase index
                self.search_panel.web_list_text.value = str_write_to_panel
            else:
                # Set datd not found
                self.search_panel.web_list_text.value = "Data Not Found"
        elif len(word_list) > 1:  # Word more than 1
            list_set_website = []  # Set list of website set
            index_set = 0  # Set index of set
            for word in word_list:  # Loop word in word list
                if word in rank_dict:  # Detect word is in list
                    list_set_website.append(set())  # Add set to list
                    for data_pack in rank_dict[
                            word]:  # get data pack from dict of word
                        website = data_pack[0]  # get website
                        list_set_website[index_set].add(
                            website)  # Set website into set
                else:
                    del word_list[word_list.index(
                        word)]  # delete word that not found
                index_set += 1  # add index of set

            if len(word_list) == 0 or len(list_set_website) == 0:
                self.search_panel.web_list_text.value = "Data Not Found"
                return
            else:
                total_set = list_set_website[0]  # get set of first word
                for i in range(1,
                               len(list_set_website)):  # loop to get all set
                    total_set = total_set & list_set_website[
                        i]  # find website is same
                self.search_panel.web_list_text.value = ""  # reset search panel
                index = 1  # start index
                str_write_to_panel = ""
                for data_pack in rank_dict[word_list[0]]:  # get data pack
                    website = data_pack[0]  # get website
                    if website in total_set:  # website in set
                        n_used = data_pack[1]["used"]  # get number of used
                        n_word = data_pack[1]["word"]  # get number of word

                        # show data in search panel
                        str_write_to_panel += str(index) + ".) Website : " + str(website.encode("ascii", "ignore")) \
                                                + "\n\t" + "Used : " + str(n_used) \
                                                + "\n\t" + "Found : " + str(n_word) + "\n"
                        index += 1  # increase index
                self.search_panel.web_list_text.value = str_write_to_panel
        print "complete : show_search : SpiderView"

    @staticmethod
    # method set function for search website
    def set_website_update(update_website):
        print "start : set_website_update : SpiderView"
        global update_website_func
        update_website_func = update_website
        print "complete : set_website_update : SpiderView"

    @staticmethod
    # method that set function show GUI and graph
    def set_show_output(show_output):
        print "start : set_show_output : SpiderView"
        global show_output_func
        show_output_func = show_output
        print "complete : set_show_output : SpiderView"

    @staticmethod
    # method that set function start indexing
    def set_indexing(indexing):
        print "start : set_indexing : SpiderView"
        global indexing_func
        indexing_func = indexing
        print "complete : set_indexing : SpiderView"

    @staticmethod
    # method that set function start indexing
    def set_go(go):
        print "start : set_go : SpiderView"
        global go_func
        go_func = go
        print "complete : set_go : SpiderView"

    @staticmethod
    # method that set function start indexing
    def set_pause(pause):
        print "start : set_pause : SpiderView"
        global pause_func
        pause_func = pause
        print "complete : set_pause : SpiderView"

    @staticmethod
    # method that set function start indexing
    def set_prev(prev):
        print "start : set_prev : SpiderView"
        global prev_func
        prev_func = prev
        print "complete : set_prev : SpiderView"

    @staticmethod
    # method that set function start indexing
    def set_next(next):
        print "start : set_next : SpiderView"
        global next_func
        next_func = next
        print "complete : set_next : SpiderView"

    # method that get root website in field
    def get_root_website(self):
        # print "complete : get_root_website : SpiderView"
        return self.index_panel.web_text.value.encode("ascii", "ignore")

    def set_root_website(self, data):
        # print "complete : get_root_website : SpiderView"
        self.index_panel.web_text.value = data.encode("ascii", "ignore")

    # method that get word in field
    def get_word(self):
        # print "complete : get_word : SpiderView"
        return self.search_panel.word_text.value.encode("ascii", "ignore")

    # method that get deep in field
    def get_deep(self):
        # print "complete : get_deep : SpiderView"
        return int(self.index_panel.deep_text.value)

    # method that return update state
    def get_update_state(self):
        return self.update_state

    # method that set update state
    def set_update_state(self, state):
        self.update_state = state

    def set_index_website(self, website):
        self.index_website = website
예제 #14
0
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()
예제 #15
0
    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)
예제 #16
0
fileN = "ragam_data_modal.txt"
#Save to:
fileN2 = "Ragas.txt"
#Each node class gets assigned a color
#E.g. Broad classes (1) are black, drugs (2) red, uses (3) green, side effects (4) blue
colors = [Color(0,0,0), Color(255,0,0), Color(0, 255, 0), Color(0, 0, 255), Color(0, 255, 255), Color(255, 255,0)]
width = 1200
height = 700
window_width = 1500
window_height = 700

# 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])
예제 #17
0
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph

import random as rd

# 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 =  None
g = Graph()

# Random nodes.


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
예제 #18
0
 def __init__(self, weighted, *args, **kwargs):
     Graph.__init__(self, *args, **kwargs)
     self.weighted = weighted
예제 #19
0
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()
예제 #20
0
 def draw(self, *args, **kwargs):
     Graph.draw(self, weighted=self.weighted, directed=True)
예제 #21
0
파일: view.py 프로젝트: 2od/literature
import networkx as nx
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph
import argparse

parser = argparse.ArgumentParser(description='Interactive view of network')
parser.add_argument('--pickle', type=argparse.FileType('r'), required=True )
args   = parser.parse_args()

h = nx.read_gpickle( args.pickle )

g = Graph()

for e in h.edges():
    g.add_edge(e[0], e[1], 
               length = 0.2, 
               weight = 0.3, 
               stroke = color(.4,.4,.5))

for n in g.nodes:
    n.text = Text(n.id, font="Droid Mono", fontsize=7, fill = color(.9,.9,.9)) 
    
g.distance         = 32   # Overall spacing between nodes.
g.layout.force     = 0.009 # Strength of the attractive & repulsive force.
g.layout.repulsion = 12   # Repulsion radius.

dragged = None
def draw(canvas):
    
    canvas.clear()
    background(color(0.22,.22,.36))
예제 #22
0
import networkx as nx
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph
import argparse

parser = argparse.ArgumentParser(description='Interactive view of network')
parser.add_argument('--pickle', type=argparse.FileType('r'), required=True)
args = parser.parse_args()

h = nx.read_gpickle(args.pickle)

g = Graph()

for e in h.edges():
    g.add_edge(e[0], e[1], length=0.2, weight=0.3, stroke=color(.4, .4, .5))

for n in g.nodes:
    n.text = Text(n.id, font="Droid Mono", fontsize=7, fill=color(.9, .9, .9))

g.distance = 32  # Overall spacing between nodes.
g.layout.force = 0.009  # Strength of the attractive & repulsive force.
g.layout.repulsion = 12  # Repulsion radius.

dragged = None


def draw(canvas):

    canvas.clear()
    background(color(0.22, .22, .36))
    translate(250, 250)
예제 #23
0
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()
예제 #24
0
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
예제 #25
0
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
# Data Science is a field in computer science that is dedicated to analyzing patterns in raw data using
# techniques like Artificial Intelligence (AI), Machine Learning (ML), mathematical functions, and
# statistical algorithms.
# Pattern is a web mining module for the Python programming language.
# It has tools for data mining (Google, Twitter and Wikipedia API, a web crawler, a HTML DOM parser), natural
# language processing (part-of-speech taggers, n-gram search, sentiment analysis, WordNet), machine learning
# (vector space model, clustering, SVM), network analysis and <canvas> visualization.
# Network visualization.
# The functions in the pattern.graph module are identical to those in the graph module in NodeBox for OpenGL.
# We can combine the two implementations to visualize the network, using a force-directed algorithm to place
# the nodes on a 2D canvas. 

from nodebox.graphics import * 
from nodebox.graphics.physics import Graph as NodeBoxGraph 
 
g1 = g.copy(nodes=halo(g['rocket'])) 
g2 = NodeBoxGraph() 

for e in g1.edges: 
    g2.add_edge(e.node1.id, e.node2.id) 
 
def draw(canvas): 
    canvas.clear() 
    translate(canvas.width / 2, canvas.height / 2) 

    g2.update() 
    g2.draw() 
 
canvas.draw = draw 
canvas.run()