Exemplo n.º 1
0
def to_pyvis(graph, layout=True):
    """
    This method takes an exisitng Networkx graph and translates
    it to a PyVis graph format that can be accepted by the VisJs
    API in the Jinja2 template.

    Parameters
    ----------
    graph : nx.DiGraph
        NetworkX directed graph.
    layout : bool
        Use hierarchical layout if this is set.

    Returns
    -------
    net : Network
        PyVis Network
    """
    def add_node(node_id):
        attr = nodes[node_id]
        net.add_node(node_id, label=attr['label'], level=attr['level'], color=attr.get('color', None),
                     title=attr['title'])

    edges = graph.edges.data()
    nodes = graph.nodes
    net = Network(height="960px", width="1280px", directed=True, layout=layout)
    for v, u, edge_attr in edges:
        add_node(v)
        add_node(u)
        net.add_edge(v, u, title=edge_attr['label'], color=edge_attr['color'])
    return net
Exemplo n.º 2
0
	def wrapper(jsonGraph, path):
		g = func(jsonGraph,path)
		colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
		G= Network(height="800px", width="70%", directed=True)
		data=[]
		for u,e,v in g[1]:
			data.append(e)

		label_encoder = LabelEncoder()
		integer_encoded = np.array(label_encoder.fit_transform(data), dtype='float')
		i=0
		for src,e,dst in g[1]:
			src_cname_idx = str(src).count('/') if str(src).count('/') < len(cnames) else (len(cnames) - 1)
			src_size_idx = str(src).count('/') if str(src).count('/') < len(sizes) else (len(sizes) - 1)
			dst_cname_idx = str(dst).count('/') if str(dst).count('/') < len(cnames) else (len(cnames) - 1)
			dst_size_idx = str(dst).count('/') if str(dst).count('/') < len(sizes) else (len(sizes) - 1)

			G.add_node(src, title=src, physics=True,color=cnames[src_cname_idx], size=sizes[src_size_idx], shape="dot")
			G.add_node(dst, title=dst, physics=True,color=cnames[dst_cname_idx], size=sizes[dst_size_idx], shape="dot")
			G.add_edge(src,dst,width=0.5, title=data[i],physics=True)
			i+=1

		G.hrepulsion(node_distance=120,
        			 central_gravity=0.0,
        			 spring_length=100,
        			 spring_strength=0.01,
        			 damping=0.09)
		G.show_buttons(filter_=['physics'])
		G.show("test.html")

		# for s,p,o in g[1]:
		# 	print((s,p,o))
		return g
Exemplo n.º 3
0
    def to_interactive_html(self, destination: str):
        from pyvis.network import Network  # type: ignore
        G = Network(height='100%',
                    width='100%',
                    directed=True,
                    bgcolor='#000000')
        G.toggle_stabilization(False)

        all_nodes: Set[Node] = set()
        nodes_to_explore: Set[Node] = set([self])
        while len(nodes_to_explore) > 0:
            all_nodes.update(nodes_to_explore)
            new_nodes_to_explore: Set[Node] = set()
            for node_to_explore in nodes_to_explore:
                connected_nodes = node_to_explore.query.through_edge().results
                connected_nodes = cast(Set[Node], connected_nodes)
                new_nodes_to_explore.update([
                    node for node in connected_nodes if node not in all_nodes
                ])

            nodes_to_explore = new_nodes_to_explore

        for node in all_nodes:
            G.add_node(node.graph_id, label=str(node))

        for node in all_nodes:
            for out in node.outbound:
                G.add_edge(node.graph_id, out.destination.graph_id)
                # links.append((node, out.destination, {'label': out.label}))

        G.show(destination)
Exemplo n.º 4
0
def network_visualization(path='./socnetvis.html', alphabet=False):
    weights = {'best': 4, 'good': 2, 'friend': 1, 'acquaintance': 0.5}
    net = Network('100%', '100%', bgcolor='#222', font_color='white')
    net.barnes_hut()
    for name, node in nodes.items():
        for connection_type in node['connections']:
            for partner in node['connections'][connection_type]:
                net.add_node(name, name, title=name)
                net.add_node(partner, partner, title=partner)
                net.add_edge(name, partner, value=weights[connection_type])
    for net_node in net.nodes:
        node = nodes[net_node['id']]
        net_node['value'] = len(net.get_adj_list()[net_node['id']])
        net_node['title'] += f"<br>{net_node['value']} Connections<br>"
        if node['notes']:
            net_node['title'] += f"<i>{node['notes']}</i><br>"
        for connection_type in node['connections']:
            if node['connections'][connection_type]:
                connections = node['connections'][connection_type]
                net_node['title'] += f"<br>{connection_type.capitalize()} " \
                                     f"({len(connections)})<br>&nbsp&nbsp"
                net_node['title'] += "<br>&nbsp&nbsp".join(
                    sorted(connections) if alphabet else connections) + "<br>"
    if not os.path.isdir(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))
    net.show(path)
Exemplo n.º 5
0
def graph_print():
    dist = loadCompleteDistro()
    matrix = numpy.zeros((len(dist), len(dist)))
    i = 0
    labels = {}
    net = Network()
    k=0
    caster = {"nld": "Niderlandzki", "gle": "Irlandzki", "spa": "Hiszpański", "fra": "Francuski", "eng": "Angielski", "slk": "Słowacki", "fin": "Fiński", "est": "Estoński", "lit": "Litewski", "hrv": "Chorwacki", "por": "Portugalski", "swe": "Szwedzki", "ron": "Romański", "dan": "Duński", "slv": "Słoweński", "bul": "Bułgarski", "ita": "Włoski", "ell": "Grecki", "mlt": "Maltański", "lav": "Łotewski", "ces": "Czeski", "pol": "Polski", "deu": "Niemiecki", "hun": "Węgierski"}

    for lang1 in dist.keys():
        print(lang1)
        net.add_node(k + 1, label=f"{caster[lang1]}")
        k+=1

    xD = []
    for lang1 in dist.keys():
        labels[i] = lang1
        j = 0
        for lang2 in dist.keys():
            if lang1 != lang2:

                xD.append([i+1, j+1,round(KullbackLeibnerIteration(dist[lang1].alain(dist[lang2]), dist[lang2].alain(dist[lang1])),2)])
            else:
                pass
            j += 1
        i += 1

    #G = nx.from_numpy_matrix(matrix)
    #H = nx.relabel_nodes(G, labels)

    #vg = []
    #for u in H.edges.data():
    #    vg.append(u[2]["weight"])
    #
  
    xD.sort(key=lambda x: x[2])
    xDD = xD[:40]
    for x in xDD:
        print(x[2])
        net.add_edge(x[0],x[1],value=1.0/x[2], label=str(x[2]))

    #
    #Z = copy.deepcopy(H)
    #for u in Z.edges.data():
    #    if u[2]["weight"] > median:
    #        H.remove_edge(u[0], u[1])
    #


    #pos = nx.spring_layout(H,k=1, iterations=20)
    #plt.figure(3, figsize=(10, 10))
    #labels = nx.get_edge_attributes(H, 'weight')
    #nx.draw_networkx(H,pos=pos, with_labels=True,node_size=500)
    #nx.draw_networkx_edge_labels(H, pos, edge_labels=labels)
    #plt.show()

    net.save_graph("my_graph.html")
    net.toggle_physics(True)
    net.show_buttons(filter_=['nodes', 'edges', 'physics'])
    net.show('mygraph.html')
Exemplo n.º 6
0
class transaction_graph:
    def __init__(self):
        self.graph = Network(height="750px", width="100%", directed=True)
        with open('layouts\\transaction_graph_layout.json') as f:
            self.options = json.load(f)

    def show_graph(self):
        dirOutput = "output"
        if not os.path.exists(dirOutput):
            os.makedirs("output")
        self.graph.show("output\\transaction_gragh.html")

    def add_transaction(self, input, output, in_time, out_time, amount):

        input_edge = zip(input, in_time, output, out_time, amount)
        for i in input_edge:
            input = i[0]
            label = i[0]
            time_in = i[1]
            output = i[2]
            time_out = i[3]
            weight = i[4]
            self.graph.add_node(input, level=time_in, shape="square")
            self.graph.add_node(output, level=time_out, shape="square")
            self.graph.add_edge(input, output, value=weight, title=weight)

        self.graph.options = self.options
Exemplo n.º 7
0
def visualize(networkx_graph, name):
    if '.html' in name:
        pyvis_graph = Network(height=800, width=800, notebook=True)
        for node, node_attrs in networkx_graph.nodes(data=True):
            pyvis_graph.add_node(node, **node_attrs)

        # for each edge and its attributes in the networkx graph
        for source, target, edge_attrs in networkx_graph.edges(data=True):
            # if value/width not specified directly, and weight is specified, set 'value' to 'weight'
            if not 'value' in edge_attrs and not 'width' in edge_attrs and 'weight' in edge_attrs:
                # place at key 'value' the weight of the edge
                edge_attrs['value'] = edge_attrs['weight']
            # add the edge
            pyvis_graph.add_edge(source, target, **edge_attrs)

        return pyvis_graph.show('docs/' + name)
    elif '.svg' in name:
        # pos = nx.spring_layout(networkx_graph,k=0.25)
        options = {
            "node_color": "blue",
            "node_size": 20,
            "line_color": "grey",
            "linewidths": 1,
            "width": 1,
        }
        nx.draw(networkx_graph, **options)
        plt.savefig(f"docs/{name}")
Exemplo n.º 8
0
    def network_pyvis(self, df):
        G = nx.from_pandas_edgelist(df, edge_attr=True)

        net = Network(height="500px", width="100%", heading='')

        sources = df['source']
        targets = df['target']
        weights = df['weight']

        edge_data = zip(sources, targets, weights)

        for e in edge_data:
            src = e[0]
            dst = e[1]
            w = e[2]

        net.add_node(src, src, title=src)
        net.add_node(dst, dst, title=dst)
        net.add_edge(src, dst, value=w)

        neighbor_map = net.get_adj_list()

        net.from_nx(G)
        net.write_html('tweets/temp.html')
        return net.html
Exemplo n.º 9
0
def plotResult(file_in, file_out):
    got_net = Network(height="100%",
                      width="100%",
                      bgcolor="white",
                      font_color="black")

    got_net.barnes_hut()
    got_data = pd.read_csv(file_in)

    sources = got_data['From']
    targets = got_data['To']
    weights = got_data['Weight']
    x_array = got_data['x']
    y_array = got_data['y']
    highlight = got_data['Highlight']

    edge_data = zip(sources, targets, weights, x_array, y_array, highlight)

    for e in edge_data:
        src = e[0]
        dst = e[1]
        w = e[2]
        x_array = e[3]
        y_array = e[4]
        h = e[5]
        v_color_dst = "#ADFF2F"  #Green color
        image_url = 'cat.png'
        v_shape = 'dot'

        if (dst in ['central_concentrator'] or h == 1):
            image_url = 'cat.png'
            v_shape = 'circle'

        got_net.add_node(src,
                         shape=v_shape,
                         title=src,
                         x=x_array,
                         y=y_array,
                         color="#ADFF2F")  #Green color
        got_net.add_node(dst,
                         shape=v_shape,
                         title=dst,
                         x=x_array,
                         y=y_array,
                         color=v_color_dst)
        got_net.add_edge(src, dst, value=h)

    neighbor_map = got_net.get_adj_list()

    # add neighbor data to node hover data
    for node in got_net.nodes:
        node["title"] += " Neighbors:<br>" + "<br>".join(
            neighbor_map[node["id"]])
        node["value"] = len(neighbor_map[node["id"]])

    got_net.set_options(
        'var options = { "edges": { "arrows": { "middle": { "enabled": true } }, "color": { "inherit": "false"}, "smooth": false},"physics": {"enabled": true, "forceAtlas2Based":{  "gravitationalConstant": -500, "springLength": 100, "avoidOverlap": 1}, "minVelocity": 0.75, "solver": "forceAtlas2Based"}}'
    )

    got_net.show(file_out)
Exemplo n.º 10
0
def getCourseText():
    """
    Visualize courses and their assigned texts.
    """
    coursesAndTexts = g.query("""
        select distinct ?courseName ?textTitle ?authorLast where {
            ?a a ccso:Course .
            ?a ccso:csName ?courseName .
            ?a ccso:hasLM ?t .
            ?t res:resource ?doc .
            ?doc dcterms:title ?textTitle .
            ?doc dcterms:creator ?author .
            ?author foaf:surname ?authorLast .
        } limit 50""")

    for line in coursesAndTexts:
        print(line)

    net = Network(height='750px', width='100%')

    for courseName, textTitle, authorLast in coursesAndTexts:
        net.add_node(courseName, shape='square', title=str(courseName))
        net.add_node(textTitle,
                     shape='circle',
                     label=str(authorLast),
                     title=str(textTitle))
        net.add_edge(courseName, textTitle, title="hasLM")

    net.save_graph('../website/graph-viz.html')
Exemplo n.º 11
0
def build_pyvis(filename):
    header = metadata_header(filename)
    net = Network("95%", "100%", heading=header)
    net.show_buttons()
    nodes, edges = triplet(filename)
    node_weight = count_node_weight(edges)
    for node in nodes:
        group = node["labels"] if node["labels"] else ""
        net.add_node(
            node["alias"],
            label=node["name"],
            group=group,
            size=((sqrt(node_weight[node["alias"]]) + 1) * 5),
        )
    for edge in edges:
        net.add_node(edge["subject"], color="blue")
        net.add_node(edge["object"], color="blue")
        net.add_edge(edge["subject"], edge["object"], title=edge["predicate"])

    net.barnes_hut(
        gravity=-4000,
        central_gravity=0.3,
        spring_strength=0.001,
        damping=0.09,
        overlap=1,
    )
    net.show("index.html")
Exemplo n.º 12
0
def getUniCourseGraph():
    """
    Get university-course graph.
    """
    results = g.query("""
        select distinct ?courseID ?courseName ?instructorFN ?instructorGN ?university where {
            ?courseID a ccso:Course .
            ?courseID ccso:csName ?courseName .
            ?courseID ccso:hasInstructor ?inst .
            ?inst foaf:familyName ?instructorFN .
            ?inst foaf:givenName ?instructorGN .
            ?courseID ccso:offeredBy ?dept .
            ?dept ccso:belongsTo ?uni .
            ?uni ccso:legalName ?university .
        }""")

    visGraph = Network(height='750px',
                       width='100%')  # PyVis-Network, for visualization.
    nxGraph = nx.Graph()  # NetworkX, for analyses

    for courseID, courseName, instLast, instFirst, uni in results:
        instName = f"{instFirst} {instLast}"
        nxGraph.add_node(uni)
        visGraph.add_node(uni, shape='circle', title=uni, mass=2)
        nxGraph.add_node(courseID)
        visGraph.add_node(courseID, shape='box', label=courseName)
        visGraph.add_edge(uni, courseID)
        nxGraph.add_edge(uni, courseID)
    return nxGraph, visGraph
Exemplo n.º 13
0
def generate_graph(arbre):
    got_net = Network(height="100%",
                      width="100%",
                      bgcolor="#222222",
                      font_color="white")
    dico_couleur = {}
    for streamer in arbre.streamers:
        got_net.add_node(streamer.name,
                         value=streamer.poids,
                         shape='image',
                         image=streamer.photo_profil,
                         size=100)
        dico_couleur[streamer.name] = streamer.couleur

    for depart, arrivee in arbre.groupes_migrants:
        for viewer in arbre.groupes_migrants[depart, arrivee]:
            got_net.add_node(viewer, value=1, color=dico_couleur[depart])
            got_net.add_edge(viewer, depart, color=dico_couleur[depart])
            got_net.add_edge(viewer, arrivee, color=dico_couleur[arrivee])
    got_net.show_buttons()
    got_net.set_edge_smooth(smooth_type='dynamic')
    got_net.force_atlas_2based(gravity=-200,
                               central_gravity=0.01,
                               spring_length=1,
                               spring_strength=0.08,
                               damping=0.4,
                               overlap=0)
    got_net.save_graph("./graphs_v1/" + 'streamgame_v1_42' + ".html")
    return
Exemplo n.º 14
0
def api_docs_network():
    ee = get_enriched_endpoints()
    endpoints = [e['endpoint'] for e in ee]
    similarity_matrix = similarity_network([e['document'] for e in ee], weighted=True, enr_endpoints=ee)

    similarity_threshold = 0.7
    colors = get_different_colors(305)
    added_nodes = []
    wordnet_network = nx.Graph()
    vis_network = Network(height="100%", width="70%")
    for iy, y in enumerate(similarity_matrix):
        for ix, x in enumerate(y):
            if x > similarity_threshold and ix != iy:
                y_name = endpoints[iy]['path']
                x_name = endpoints[ix]['path']
                if y_name not in added_nodes:
                    vis_network.add_node(y_name)
                    # vis_network.add_node(y_name,
                    #                      color="#FF0000" if len(get_services_from_table(y_name)) > 0 else "#DDDDDD")
                    added_nodes.append(y_name)
                if x_name not in added_nodes:
                    vis_network.add_node(x_name)
                    # vis_network.add_node(x_name,
                    #                      color="#FF0000" if len(get_services_from_table(y_name)) > 0 else "#DDDDDD")
                    added_nodes.append(x_name)

                # norm_weight = (x - similarity_threshold) / (1 - similarity_threshold)
                vis_network.add_edge(y_name, x_name)
                wordnet_network.add_edge(y_name, x_name, weight=x)

    nx.write_gexf(wordnet_network, "network_outputs/api_wordnet.gexf")
    nx.draw_networkx(wordnet_network, node_size=3, with_labels=False)
    plt.show()
    vis_network.show_buttons(filter_=['physics'])
    vis_network.show("network_outputs/api_wordnet.html")
def evaluate_match_based(video_space, estimator, plot=True, thr=0.4):

    estimator.fit(video_space)
    distances, indexes = estimator.kneighbors(video_space, n_neighbors=20)
    if plot == True:
        plt.hist(distances[:, 1::].ravel(), bins=100)

    results, results_distances = filter_results(distances, indexes, thr)

    net = Network(notebook=True, height='1000px', width='1000px')
    net.barnes_hut()

    for i, l in enumerate(labels):
        net.add_node(str(i), label=str(l))

    for i, d in enumerate(results):
        for j, m in enumerate(d):
            if j != 0:
                net.add_edge(str(i),
                             str(m),
                             weight=str(results_distances[i][j]))

    net.show_buttons(filter_=['physics'])
    net.show('mygraph.html')
    return net
Exemplo n.º 16
0
def plot_bacteria_intraction_network(bacteria, node_list, node_size_list, edge_list, color_list, G_name, folder,
                                     color_by_tax_level=2, directed_G=True, control_color_and_shape=True):
    # create the results folder
    if not os.path.exists(folder):
        os.mkdir(folder)

    nodes_colors, nodes_shapes, group_list, tax_to_color_and_shape_map = \
        get_nodes_colors_by_bacteria_tax_level(bacteria, G_name, taxnomy_level=color_by_tax_level, folder=folder)

    bact_short_names = [shorten_single_bact_name(b) for b in bacteria]
    nodes_with_edges = np.unique(np.array(edge_list).flatten()).tolist()

    net = Network(height="750px", width="100%", bgcolor="#FFFFFF", font_color="black", directed=directed_G)
    #net.barnes_hut(gravity=-120000)
    net.force_atlas_2based()

    # for the nodes: you can use either only the group option the automatically colors the group in different colors
    # shaped like dots - no control, or use color and shape to make it you own, in this case group is irrelevant
    for i, node in enumerate(node_list):
        if node in nodes_with_edges:
            if control_color_and_shape:
                net.add_node(node, label=bact_short_names[i], color=nodes_colors[i], value=node_size_list[i],
                            shape=nodes_shapes[i], group=group_list[i])
            else:
                net.add_node(node, label=bact_short_names[i],  value=node_size_list[i],
                            group=group_list[i])

    # for the edges, the colors are what you decide and send
    for i, (u, v) in enumerate(edge_list):
        net.add_edge(u, v, color=color_list[i])


    net.save_graph(os.path.join(folder, G_name + ".html"))
Exemplo n.º 17
0
def pyvis(height):
    # Load Pyvis network
    got_net = Network(height="750px",
                      width="100%",
                      bgcolor="#222222",
                      font_color="white")
    got_net.barnes_hut()
    got_data = pd.read_csv(
        "https://www.macalester.edu/~abeverid/data/stormofswords.csv")

    sources = got_data['Source']
    targets = got_data['Target']
    weights = got_data['Weight']
    edge_data = zip(sources, targets, weights)

    for e in edge_data:
        src = e[0]
        dst = e[1]
        w = e[2]
        got_net.add_node(src, src, title=src)
        got_net.add_node(dst, dst, title=dst)
        got_net.add_edge(src, dst, value=w)

    neighbor_map = got_net.get_adj_list()
    for node in got_net.nodes:
        node["title"] += " Neighbors:<br>" + "<br>".join(
            neighbor_map[node["id"]])
        node["value"] = len(neighbor_map[node["id"]])

    got_net.write_html("gameofthrones.html")
    return got_net.html, height, "Pyvis Network"
Exemplo n.º 18
0
class show_path:
    def __init__(self):
        self.graph = Network(height="750px", width="100%", directed=True)

        with open('layouts\path_layout.json') as f:
            self.composite_options = json.load(f)

    def show_graph(self):
        dirOutput = "output"
        if not os.path.exists(dirOutput):
            os.makedirs("output")
        self.graph.show("output\path_graph.html")

    def add_path(self, input, amount):

        input_edge = zip(input[0:len(input) - 1], input[1:len(input)], amount)

        for i in input_edge:
            input = i[0]
            output = i[1]
            weight = i[2]
            self.graph.add_node(input)
            self.graph.add_node(output)
            self.graph.add_edge(input, output, value=weight, title=weight)

        self.graph.options = self.composite_options

    def show(self):
        self.show_graph()
def kyoki_word_network():
    got_net = Network(height="1000px",
                      width="95%",
                      bgcolor="#FFFFFF",
                      font_color="black",
                      notebook=True)
    # set the physics layout of the network
    #got_net.barnes_hut()
    got_net.force_atlas_2based()
    got_data = pd.read_csv("kyoki.csv")[:150]
    sources = got_data['first']  #count
    targets = got_data['second']  #first
    weights = got_data['count']  #second
    edge_data = zip(sources, targets, weights)

    for e in edge_data:
        src = e[0]
        dst = e[1]
        w = e[2]

        got_net.add_node(src, src, title=src)
        got_net.add_node(dst, dst, title=dst)
        got_net.add_edge(src, dst, value=w)

    neighbor_map = got_net.get_adj_list()

    #add neighbor data to node hover data
    for node in got_net.nodes:
        node["title"] += " Neighbors:<br>" + "<br>".join(
            neighbor_map[node["id"]])
        node["value"] = len(neighbor_map[node["id"]])

    got_net.show_buttons(filter_=['physics'])
    return got_net
def createNetwork(in_dic, showNodesPanel_bol, showPhysicsPanel_bol):
    """Function to take a dic and to build a network from it. Key = pmid, value = [refs]. We can selected whether we want panel to edit nodes or gravity
in_dic must have format;
[+] pmid_dic = {pmid_int : [[ref1_str, ..., refn_str], title_str]}
[+] in_dic :{19102772: [[12764516, 15825143, 15702043], 'Identification of a novel picornavirus related to cosaviruses in a child with acute diarrhea'], 31622334: [[20536486, 17627978], 'Use of personalised risk-based screening schedules to optimise workload and sojourn time in screening programmes for diabetic retinopathy: A retrospective cohort study']}.
	"""

    #get the network ready
    G = Network(height="100%",
                width="95%",
                bgcolor="#222222",
                font_color="white")
    #G.barnes_hut()

    # add the reference nodes
    for i in in_dic:

        # add a link to the article on pubmed
        myTitle_str = in_dic[i][1]
        # addToMyTitle_str = '<a href="https://pubmed.ncbi.nlm.nih.gov/{}" target="_blank"> Link:{}'.format(i,i)
        addToMyTitle_str = '<a href="https://pubmed.ncbi.nlm.nih.gov/{}" target="_blank">'.format(
            i)
        myTitle_str = addToMyTitle_str + myTitle_str
        #st.write("i = {} and list of refs = {}, title = {}".format(i, in_dic[i][0], myTitle_str))
        # main node
        G.add_node(str(i), color="#dd4b39", value=1,
                   title=myTitle_str)  #, mass = 10)# [1] = title

        # reference nodes
        # are there any refs for the node?
        if len(in_dic[i][0]) != 0:

            for j in in_dic[i][0]:  # [0] = ref nodes
                #st.write("REF Node j :{}".format(j))
                refNode_str = str(j)  # add main node
                #st.write("pmid = <{}>, adding ref node j <<{}>>".format(i, refNode_str))

                # add the ref node; no titles to give so we use the pubmed link instead.
                myTitle = '<a href="https://pubmed.ncbi.nlm.nih.gov/{}" target="_blank"> Link:{}'.format(
                    refNode_str, refNode_str)
                G.add_node(refNode_str, title=myTitle)

                # draw edge between main node and ref node
                G.add_edge(str(i), refNode_str, value=1)
        else:
            # st.write("pmid = <{}> has no references. :-(".format(i))
            pass

    if showNodesPanel_bol == True:
        G.show_buttons(filter_=['nodes'])  #shown below graph in browser
    # G.show_buttons(filter_=['physics']) #shown below graph in browser

    if showPhysicsPanel_bol == True:
        G.show_buttons(filter_=['physics'])  #shown below graph in browser

    phc.checkDataDir(OUTDATADIR)
    plotName_str = OUTDATADIR + "pmidPlot.html"
    # plotName_str = "/tmp/pmidPlot.html"
    showMyPlot(G, plotName_str)  #push out the plot to browser
Exemplo n.º 21
0
class NetDisplay:
    def __init__(self, net, LinGen, name):
        self.net = net
        self.LinGen = LinGen
        self.Grafo = Network(height="600px", width="1000px")
        self.name = name

        self.Grafo.toggle_physics(False)
        self.Grafo.inherit_edge_colors_from(False)

    def nodesPlot(self):
        self.CalcNeuronsYpositions()
        for layer in self.net:
            neurons = layer.ReturnNeurons()
            y_positions = layer.NeuronYPosition
            for neuron, y_pos in zip(neurons, y_positions):
                self.Grafo.add_node(
                    neuron, x=layer.number * 100, y=y_pos * 100)

        i = 0
        for con in self.LinGen:
            if con.enable:
                if con.weight >= 0:
                    color = 'blue'
                else:
                    color = 'red'

                if con.recurrent:
                    dashes = 'true'
                else:
                    dashes = 'false'

                try:
                    self.Grafo.add_edge(
                        con.input, con.output, value=abs(con.weight))
                    self.Grafo.edges[i].update(
                        {'color': color, 'arrows': 'to', 'arrowStrikethrough': 'false',
                         'title': 'IN: ' + str(con.innovation_number), 'dashes': dashes, 'shadow': dashes})
                    i += 1
                except:
                    pass

        self.Grafo.show_buttons()
        self.Grafo.set_edge_smooth('dynamic')
        self.Grafo.show(self.name + ".html")

    def CalcNeuronsYpositions(self):
        for layer in self.net:
            neuronsYposition = []
            size = len(layer.ReturnNeurons())
            if size % 2 == 0:
                for i in range(0, size // 2):
                    neuronsYposition += [i + 0.5, -i - 0.5]
            else:
                neuronsYposition = [0]
                for i in range(1, size // 2 + 1):
                    neuronsYposition += [i, -i]
            layer.NeuronYPosition = neuronsYposition
Exemplo n.º 22
0
 def drawRscRscGraph_advanced(self, RscRscMatrix, weight_threshold, directed, encryption):
     
     rows, cols = np.where(RscRscMatrix > weight_threshold)
     weights = list();
     
     for x in range(len(rows)):
         weights.append(RscRscMatrix[rows[x]][cols[x]])
     
     #got_net = Network(height="750px", width="100%", bgcolor="white", font_color="#3de975", directed=directed)
     got_net = Network(height="750px", width="100%", bgcolor="white", font_color="black", directed=directed)
     ##f57b2b
     # set the physics layout of the network
     got_net.barnes_hut()
   
     edge_data = zip(rows, cols, weights)
     
     for e in edge_data:
         
         src = self.resourceList[e[0]]  # convert ids to labels
         dst = self.resourceList[e[1]]
         w = e[2] 
         
         if(encryption):
             src = Utilities.AES_ECB_Encrypt(self,src.encode('utf-8'))
             dst = Utilities.AES_ECB_Encrypt(self,dst.encode('utf-8'))
             
         # I have to add some options here, there is no parameter
         highlight = {'border': "#3de975", 'background': "#41e9df"}
         #color = {'border': "#000000", 'background': "#123456"}
         got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight})
         got_net.add_node(dst, dst, title=dst , labelHighlightBold=True, color={'highlight': highlight})
         got_net.add_edge(src, dst, value=w, title=w)
     
     neighbor_map = got_net.get_adj_list()
     
     dict = got_net.get_edges()
     
     self.getResourceList()
     
     # add neighbor data to node hover data
     for node in got_net.nodes:
         counter = 0
         if(directed):
             node["title"] = "<h3>" + node["title"] + " Output Links: </h3>"
         else:
             node["title"] = "<h3>" + node["title"] + " Links: </h3>"
         for neighbor in neighbor_map[node["id"]]:
             if(counter % 10 == 0):
                 node["title"] += "<br>::: " + neighbor        
             else:
                 node["title"] += " ::: " + neighbor
             node["value"] = len(neighbor_map[node["id"]])
             counter += 1
     
     got_net.show_buttons(filter_=['nodes', 'edges', 'physics'])
     got_net.show_buttons(filter_=['physics'])
     
     got_net.show("PMSocial.html")
Exemplo n.º 23
0
    def drawActivityResourceGraph_advanced(self, ActivityRscMatrix, weight_threshold, directed, splitted):
        
        rows, cols = np.where(ActivityRscMatrix > weight_threshold)
        weights = list();
        
        for x in range(len(rows)):
            weights.append(ActivityRscMatrix[rows[x]][cols[x]])
        
        got_net = Network(height="750px", width="100%", bgcolor="black", font_color="#f57b2b", directed= directed)
        
        # set the physics layout of the network
        got_net.barnes_hut()
      
        edge_data = zip(rows, cols, weights)
        
        counter = 1
        for e in edge_data:
            src = self.activityList[e[0]]  # convert ids to labels
            dst = self.resourceList[e[1]]
            w = e[2] 
    
            # I have to add some options here, there is no parameter
            highlight = {'border': "#3de975", 'background': "#41e9df"}
            if(splitted):
                got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight},shape='square')
                got_net.add_node(dst+ "__" +str(counter) , dst , title=dst , labelHighlightBold=True, color={'border': "#dd4b39", 'background': "#dd4b39", 'highlight': highlight})
                got_net.add_edge(src, dst+ "__" +str(counter), value=w, title=w)
                counter +=1
            else:
                got_net.add_node(src, src, title=src, labelHighlightBold=True, color={'highlight': highlight},shape='square')
                got_net.add_node(dst , dst, title=dst , labelHighlightBold=True, color={'border': "#dd4b39", 'background': "#dd4b39", 'highlight': highlight})
                got_net.add_edge(src, dst , value=w, title=w)

        neighbor_map = got_net.get_adj_list()
        
        dict = got_net.get_edges()
        
        self.getResourceList()
        
        # add neighbor data to node hover data
        for node in got_net.nodes:
            counter = 0
            if(directed):
                node["title"] = "<h3>" + node["title"] + " Output Links: </h3>"
            else:
                node["title"] = "<h3>" + node["title"] + " Links: </h3>"
            for neighbor in neighbor_map[node["id"]]:
                if(counter % 10 == 0):
                    node["title"] += "<br>::: " + neighbor        
                else:
                    node["title"] += " ::: " + neighbor
                node["value"] = len(neighbor_map[node["id"]])
                counter += 1
        
        got_net.show_buttons(filter_=['nodes', 'edges', 'physics'])
        got_net.show_buttons(filter_=['physics'])
        
        got_net.show("PMSocial.html")
Exemplo n.º 24
0
def interactiveGraphExtended(G):
    # plot interactive graph using pyvis, with degree, no of node labeled, size depends on nodes' degree
    nt = Network(height="750px", width="100%", bgcolor="#222222", font_color="white")
    nt.barnes_hut(spring_strength=0.006)
    for node in G:
        nt.add_node(node, label=G.degree(node), title="I am node "+str(node), value=G.degree(node))
    for edge in G.edges:
        nt.add_edge(int(edge[0]), int(edge[1]), color='white')
    nt.show("nx.html")
Exemplo n.º 25
0
class address_graph:
    def __init__(self):
        self.graph = Network(height="750px", width="100%", directed=True)
        with open('layouts\\cluster_layout.json') as f:
            self.composite_options = json.load(f)

    def show_graph(self):
        dirOutput = "output"
        if not os.path.exists(dirOutput):
            os.makedirs("output")
        self.graph.show("output\\cluster_gragh.html")

    def add_address_graph(self, input, output, amount):

        input_edge = zip(input, output, amount)
        for i in input_edge:
            input = i[0]
            output = i[1]
            weight = i[2]
            self.graph.add_node(input)
            self.graph.add_node(output)
            self.graph.add_edge(input, output, value=weight, title=weight)
        self.graph.options = self.composite_options

    def colors(self, n):
        ret = []
        r = int(random.random() * 256)
        g = int(random.random() * 256)
        b = int(random.random() * 256)
        step = 256 / n
        for i in range(n):
            r += step
            g += step
            b += step
            r = int(r) % 256
            g = int(g) % 256
            b = int(b) % 256
            ret.append((r, g, b))
        return ret

    def cluster_addresses(self, address, cluster):

        color_num = max(cluster)
        print(color_num)
        colors = self.colors(4)

        for i in self.graph.nodes:
            try:
                idx = address.index(i["id"])
                value = cluster[address.index(i["id"])]
                print(i, "      id           ", value)
                print(str(colors[value - 1]))
                i["color"] = "rgb" + str(colors[value - 1])
            except Exception:
                pass
Exemplo n.º 26
0
def generate_network_plot(species,
                          path_to_template=template,
                          path_to_reactions=path):
    net = Network(directed=True)
    with open(path_to_reactions, 'r') as f:
        reactions_data = json.load(f)

    contained_reactions = {}

    for r in reactions_data['camp-data'][0]['reactions']:
        if 'reactants' in r:
            tmp = reactions_data['camp-data'][0]['reactions']
            if species in r['reactants']:
                contained_reactions.update({tmp.index(r): {}})
            if species in r['products']:
                contained_reactions.update({tmp.index(r): {}})

    logging.debug(contained_reactions)
    nodes = {}
    edges = []

    if contained_reactions:
        for i in contained_reactions:
            tmp = reactions_data['camp-data'][0]['reactions']
            reac_data = tmp[i]['reactants']
            prod_data = tmp[i]['products']
            reactants = [x for x in reac_data]
            products = [x for x in prod_data]
            first = '+'.join(reactants)
            second = '+'.join(products)
            name = first + '->' + second
            net.add_node(name,
                         label=name,
                         color='green',
                         borderWidthSelected=3)
            for j in reactants:
                nodes.update({j: {}})
                edges.append([j, name])
            for k in products:
                nodes.update({k: {}})
                edges.append([name, k])
    else:
        net.add_node(
            species,
            label=species,
        )
    if species in nodes:
        nodes.pop(species)
    net.add_node(species, label=species, color='blue', size=50)
    for n in nodes:
        net.add_node(n, label=n, borderWidthSelected=3, size=40)
    for e in edges:
        net.add_edge(e[0], e[1])
    net.force_atlas_2based(gravity=-100, overlap=1)
    net.show(str(path_to_template))
Exemplo n.º 27
0
def dynamic_netShow(gnetdata, filename, node_size=50, node_community='all', html_size=["500px", "1800px"],
                    bgcolor="#222222", font_color="white", **kwarg):
    """
    create a GRN html
    ------------------------------------------
    :param gnetdata: Gnetdata object
    :param filename: str, save as filename
    :param node_size: int, default 50.
    :param node_community: str or list, default all.
    :param html_size: list, default ["1200px", "1600px"]
    :param font_color: string, default white
    :param bgcolor: string, default #222222
    :return: None
    """
    assert 'graph' in gnetdata.NetAttrs.keys(), 'graph is empty!'
    assert 'communities' in gnetdata.NetAttrs.keys(), 'node communities is empty!'

    node_group = gnetdata.NetAttrs['communities']
    graph = gnetdata.NetAttrs['graph']

    net = Network(html_size[0], html_size[1], bgcolor=bgcolor, font_color=font_color, **kwarg)
    edge_data = zip(list(graph.edges), list(graph[x[0]][x[1]]['weight'] for x in graph.edges))
    colors = sns.color_palette().as_hex() + sns.color_palette('Paired', 100).as_hex()

    for e in edge_data:
        src = e[0][0]
        dst = e[0][1]
        w = e[1]

        if node_community == 'all':
            src_color = int(node_group[node_group['node'] == src]['group'])
            dst_color = int(node_group[node_group['node'] == dst]['group'])
        else:
            sub_node_group = node_group[node_group.group.isin(node_community)]
            src_color = int(sub_node_group[sub_node_group['node'] == src]['group']) if src in list(
                sub_node_group.node) else 'grey'
            dst_color = int(sub_node_group[sub_node_group['node'] == dst]['group']) if dst in list(
                sub_node_group.node) else 'grey'

        net.add_node(src, src, title=src, color='grey' if src_color == 'grey' else colors[src_color])
        net.add_node(dst, dst, title=dst, color='grey' if dst_color == 'grey' else colors[dst_color])
        net.add_edge(src, dst, value=w)

    neighbor_map = net.get_adj_list()

    # add neighbor data to node hover data
    for node in net.nodes:
        node["title"] += " Neighbors:<br>" + "<br>".join(neighbor_map[node["id"]])
        node["value"] = len(neighbor_map[node["id"]])
        node['size'] = node_size

    net.show_buttons(filter_='physics')
    net.show(filename)

    return None
Exemplo n.º 28
0
    def show(self, file="state_graph.html", physics=True):
        from pyvis.network import Network

        net = Network()
        for (i, j), weight in self.edges.items():
            net.add_node(i)
            net.add_node(j)
            net.add_edge(i, j, value=weight)

        net.toggle_physics(physics)
        net.show(file)
Exemplo n.º 29
0
def main():
    net = Network(height=600, width=900)
    net.barnes_hut()
    dbName = "pcapDb"

    if (not os.path.exists(dbName)):
        conn = sqlite3.connect(dbName)
        conn.execute(
            """create table packetdata (src text,dst text,len integer);""")

    else:
        conn = sqlite3.connect(dbName)

    cursor = conn.cursor()

    header_struct = struct.Struct('!I')
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(("localhost", 9888))
    sock.listen(1)
    q = queue.Queue()
    reciever, _ = sock.accept()
    t = threading.Thread(target=display, args=(q, net))
    # t.start()
    while (True):
        data_len = recvall(reciever, header_struct.size)
        (data_len, ) = header_struct.unpack(data_len)
        data = recvall(reciever, data_len)
        data = pickle.loads(data)
        # print(data)
        nodes = set()
        for i in data:
            if (i is None):
                continue
            src, dst, length = i
            # print(type(i))
            cursor.execute(
                "insert into packetdata (src,dst,len) values(?,?,?)",
                (src, dst, length))
            nodes.add(src)
            nodes.add(dst)
        weights = []
        # print("Here")
        for i in nodes:
            la = "IP: " + i
            net.add_node(i, label=i, title=la)

        for i in data:
            if (i is None):
                continue
            src, dst, weight = i
            net.add_edge(src, dst, value=weight)
        net.write_html("index.html")
        time.sleep(4)
Exemplo n.º 30
0
    def new_draw(self):
        """
        draw the ARN using the class attributes

        Args:
            nothing

        Returns:
            a local generated web page

        """
        got_net = Network(height="750px",
                          width="100%",
                          bgcolor="#222222",
                          font_color="white")
        got_net.heading = "Nucleotids"
        # set the physics layout of the network
        got_net.barnes_hut()
        cp = 0
        for nuk in self.list_nucleotide:
            for tuples in nuk.tup:
                if tuples.relation in self.grps.keys():
                    relation_type = self.grps.get(tuples.relation)
                else:
                    relation_type = 0
                relation_type = str(nuk.paired_type)
                got_net.add_node(tuples.tup[0],
                                 label="(" + str(tuples.tup[0]) + ")" +
                                 " -> " + str(tuples.tup[1]) + " : " +
                                 str(relation_type),
                                 group=relation_type)
                got_net.add_node(tuples.tup[1],
                                 label="(" + str(tuples.tup[1]) + ")" +
                                 " -> " + str(tuples.tup[0]) + " : " +
                                 str(relation_type),
                                 group=relation_type)
                got_net.add_edge(tuples.tup[0], tuples.tup[1])
        cp += 1

        # got_net.add_node(src, src, title=src)
        # got_net.add_node(dst, dst, title=dst)
        # got_net.add_edge(src, dst, value=w)

        # neighbor_map = got_net.get_adj_list()

        # add neighbor data to node hover data
        '''for node in got_net.nodes:
            node["title"] += " Neighbors:<br>" + \
                "<br>".join(neighbor_map[node["id"]])
            node["value"] = len(neighbor_map[node["id"]])'''

        got_net.show("networkRNA.html")