def draw_network_interactive(dists_pd, GP, query_result, highlight, q): G = nx.Graph() for idx in range(len(dists_pd)): G.add_edge(dists_pd.iloc[idx,0], dists_pd.iloc[idx,1], weight=dists_pd.iloc[idx,2], quantile=dists_pd.iloc[idx,3]) width=min(900, int(np.sqrt(len(G.nodes)))*150) if width < 400: width=400 Gi = nx.Graph([(hit,hit) for hit in [highlight] if hit in G.nodes()]) highlight = hvnx.draw(Gi, GP, node_color="blue", node_size=3000, with_labels=False, width=width, height=width) Gi = nx.Graph([(hit,hit) for hit, rep in zip(query_result["Lead gene name"], query_result["Common lists"]) if rep==1 and hit in G.nodes()]) labels0 = hvnx.draw(Gi, GP, node_color="white", node_size=2500, with_labels=True, width=width, height=width) Gi = nx.Graph([(hit,hit) for hit, rep in zip(query_result["Lead gene name"], query_result["Common lists"]) if rep==2 and hit in G.nodes()]) labels1 = hvnx.draw(Gi, GP, node_color="lightgrey", node_size=2500, with_labels=True, width=width, height=width) Gi = nx.Graph([(hit,hit) for hit, rep in zip(query_result["Lead gene name"], query_result["Common lists"]) if rep==3 and hit in G.nodes()]) labels2 = hvnx.draw(Gi, GP, node_color="orange", node_size=2500, with_labels=True, width=width, height=width) if "Query" in query_result.columns: Gi = nx.Graph([(hit,hit) for hit in np.unique(query_result["Query"]) if hit in G.nodes()]) labels3 = hvnx.draw(Gi, GP, node_color="red", node_size=2500, with_labels=True, width=width, height=width) else: Gi = nx.Graph([(hit,hit) for hit in [query_result["Lead gene name"][0]] if hit in G.nodes()]) labels3 = hvnx.draw(Gi, GP, node_color="red", node_size=2500, with_labels=True, width=width, height=width) # .opts(tools=[HoverTool(tooltips=[('index', '@index_hover')])]) # edges edge_styles = [{"edge_width": 8, "edge_color": "darkred", "style": "solid", "alpha": 1}, # 0.1% {"edge_width": 8, "edge_color": "darkred", "style": "solid", "alpha": 1}, # 0.5% {"edge_width": 8, "edge_color": "darkred", "style": "solid", "alpha": 1}, # 1% {"edge_width": 6, "edge_color": "red", "style": "solid", "alpha": 1}, # 2.5% {"edge_width": 6, "edge_color": "red", "style": "solid", "alpha": 1}, # 5% {"edge_width": 4, "edge_color": "darkorange", "style": "solid", "alpha": 1}, # 10% {"edge_width": 3, "edge_color": "#ababab", "style": "solid", "alpha": 1}, # 25% {"edge_width": 2, "edge_color": "lightgrey", "style": "solid", "alpha": 1}, # 50% {"edge_width": 2, "edge_color": "lightgrey", "style": "dotted", "alpha": 1}] # 100% edges = [] for q_cat, style in zip(q.index[1:], edge_styles): edgelist = [(u, v) for (u, v, d) in G.edges(data=True) if d['quantile']==float(q_cat)] if len(edgelist) == 0: continue Gi = nx.Graph(edgelist) edge = hvnx.draw(Gi, GP, node_size=2000, with_labels=False, width=width, height=width, **style) edges.append(edge) nw = hv.Overlay(edges[::-1]) * highlight * labels0 * labels1 * labels2 * labels3 return nw
def draw(self, **kwargs: Dict) -> Overlay: """Draws the graph and returns the hvplot object. Parameters ---------- **kwargs : Dict[str, Any] keyword arguments given to the hvplot.networkx.draw method Returns ------- Overlay HoloViews Overlay representing the correlation graph """ return hvnx.draw(self.graph, pos=self.pos, edge_width="weight", node_color="cluster_corr", labels="name", colorbar=True, **kwargs)
def draw_graph_hv(G): pos = nx.fruchterman_reingold_layout(G) edges = G.edges() colors = [G[u][v]['color'] for u, v in edges] weights = [G[u][v]['weight'] for u, v in edges] options = { 'node_size': 2000, 'node_color': '#A0CBE2', 'edge_width': weights, 'edge_color': colors, 'width': 1800, 'height': 900, 'with_labels': True, 'alpha': 0.8 } g = hvnx.draw(G, pos, **options) renderer = hv.renderer('bokeh') renderer.save(g, 'graph.html') plot = renderer.get_plot(g).state output_file("graph.html") save(plot, 'graph.html') show(plot)
def connections(neuron='BAGL', connstyle=CONN_STYLE): '''Prepare graph for plotting''' G = nx.DiGraph() syn_in = dfs.index[dfs[neuron] > 0].tolist() intens_in = dfs.loc[dfs[neuron] > 0, neuron] syni = [(pre, neuron, {}) for pre in syn_in] G.add_edges_from(syni) gaps_ = dfg.index[dfg[neuron] > 0].tolist() intens_g = 0.1 * dfg.loc[dfg[neuron] > 0, neuron] gaps = [(neuron, k, {}) for k in gaps_] + [(k, neuron, {}) for k in gaps_] G.add_edges_from(gaps) syn_out = dfs.T.index[dfs.T[neuron] > 0].tolist() intens_out = dfs.T.loc[dfs.T[neuron] > 0, neuron] syno = [(neuron, post, {}) for post in syn_out] G.add_edges_from(syno) G.remove_node(neuron) pos = nx.layout.circular_layout(G, scale=2) G.add_node(neuron) pos[neuron] = np.array([0, 0]) def draw_nodes(shape='o', category=inters, col='k'): hvnx.draw_networkx_nodes( G, pos, node_color=col, nodelist=[n for n in G.nodes if n in category], node_size=NODE_SIZE, alpha=0.9) draw_nodes(SENSOR_SHAPE, sensors, SENSOR_COLOR) draw_nodes(INTER_SHAPE, inters, INTER_COLOR) draw_nodes(MOTOR_SHAPE, motors, MOTOR_COLOR) nx.draw_networkx_labels(G, pos, font_color='w', font_weight='bold') nx.draw_networkx_edges(G, pos, arrowstyle=style, edgelist=syni, edge_color='g', connectionstyle=connstyle, arrowsize=10, alpha=0.7, width=intens_in, node_size=NODE_SIZE) nx.draw_networkx_edges(G, pos, arrowstyle=style, edgelist=syno, edge_color='r', connectionstyle=connstyle, arrowsize=10, alpha=0.5, width=intens_out, node_size=NODE_SIZE) nx.draw_networkx_edges(G, pos, arrowstyle=styleg, edgelist=gaps, edge_color='Gold', arrowsize=10, alpha=0.8, width=np.hstack((intens_g, intens_g)), node_size=NODE_SIZE) #plt.axis('off') hvnx.draw(G, pos) hvnx.show()
from ndlib.viz.bokeh.DiffusionPrevalence import DiffusionPrevalence import matplotlib as plot import selenium import hvplot.networkx as hvnx from itertools import compress import pandas as pd import random from ndlib.viz.mpl.TrendComparison import DiffusionTrendComparison #%% PREPROCESSING: # Load graphs and remove isolated nodes to only study the main # connected component. # LOAD GRAPH FOR MAIN NETWORK (SCENARIO 1) raw_net = nx.read_graphml('budapest_large.graphml') raw = hvnx.draw(raw_net, node_color = 'lightblue') print('Properties raw_net: '+ str(len(raw_net.nodes())) + ' and ' + str(len(raw_net.edges()))) # Remove isolated nodes main_net = raw_net main_net.remove_nodes_from(list(nx.isolates(main_net))) processed = hvnx.draw(main_net, node_color = 'lightblue') print('Properties main_net: '+ str(len(main_net.nodes())) +' and '+ str(len(main_net.edges()))) # LOAD NETWORK SCENARIO 2 deg_net = nx.read_graphml('budapest_large.graphml') deg_net.remove_nodes_from(list(nx.isolates(deg_net))) # LOAD NETWORK SCENARIO 3
node_size_list = list(node_sizes.values()) number_of_names_to_display = 10 add_labels = sorted(node_sizes, key=node_sizes.get, reverse=True)[:number_of_names_to_display] label_dict = {name: name if name in add_labels else "" for name in K.nodes} # In[9]: hvnx.draw( I, pos, height=800, width=800, node_size=node_size_list, arrowhead_length=0.01, with_labels=True, labels=label_dict, edge_width='weight', edge_alpha=0.7, edge_color='brown', node_color='eigenvector_centrality', node_cmap=plt.cm.plasma, ) # # Cliques # Cliques are sets of nodes that are all connected to each other. That is, if we have four nodes, A, B, C, and D, all possible connections exist: # > A --- B</br> # > A --- C</br> # > A --- D</br> # > B --- C</br> # > B --- D</br>
def test_nodes_are_not_sorted(self): plot = hvnx.draw(self.g) assert all(self.nodes == plot.nodes.dimension_values(2))
for period in enumerate(grphdict.keys()): n = period[0] pn = period[1] periodgraph, pos = period_graph(pn) labels = {node: node for node in periodgraph.nodes() if node in commonnames[pn]} start = periods[pn]['start'] end = periods[pn]['end'] chart.title = f"REMP network {start}-{end}" chart = hvnx.draw(G=periodgraph, pos=pos, with_labels=True, labels=labels, node_size=hv.dim('centrality')*200, node_color='entity_type', edge_color='community', cmap='accent', edge_cmap='category20', node_tooltip=['name'], #node_label='name', font_color="black", #font_size='11', width=800, height=600, ) #chart.configure_view(width=800, height=600,) chart.Overlay.opts(title=f"REMP network {start}-{end}") if not charts: charts = chart else: charts = charts + chart