Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
hamming_avg = float(line[6])
for neighbor in line[7].rstrip().split(','):
if len(neighbor) < 1:
continue
fields = neighbor.split('_')
neighbor_id = int(fields[0])
left = int(fields[1])
right = int(fields[2])
dist = int(fields[3])
G.add_edge(node_id, neighbor_id, color=colors[dist],weight=(left+right)/5)
plt.figure(figsize=(30,30))
nx.draw(G)#, pos, edges=edges, edge_color=colors, width=weights)
plt.savefig(out_file+"_basic.pdf")
edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]
plt.figure(figsize=(30,30))
pos = nx.spring_layout(G)
nx.draw(G, pos, edges=edges, edge_color=colors, width=weights, alpha=0.1, node_size=25)
plt.savefig(out_file+"_spring.pdf")
plt.figure(figsize=(18,18))
pos = nx.spectral_layout(G)
nx.draw(G, pos, edges=edges, edge_color=colors, width=weights)
plt.savefig(out_file+"_spectral.pdf")
# extract nodes from graph
nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])
# create networkx graph
G=nx.Graph()
# add nodes
for node in nodes:
G.add_node(node)
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
# draw graph
nx.draw(G, ax=subplot)
# show graph
return graph
def DrawGraph(G, egocentric_network_edge_list, egocentric_network_node_list, vert):
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels = True, node_color = 'blue', alpha = 0.8) #with_labels=true is to show the node number in the output graph
nx.draw_networkx_edges(G, pos, edgelist = egocentric_network_edge_list , width = 2.5, alpha = 0.8, edge_color = 'red')
nx.draw_networkx_nodes(G,pos, nodelist = egocentric_network_node_list, node_color = 'red', alpha = 0.5)
nx.draw_networkx_nodes(G,pos,nodelist=[vert],node_color='green',node_size=500,alpha=0.8)
return pos
def plot_graph(G):
"""
Plots a networkx graph.
Parameters
----------
G:
The networkx graph of interest.
"""
weights = np.real([*nx.get_edge_attributes(G, 'weight').values()])
pos = nx.shell_layout(G)
nx.draw(G, pos, node_color='#A0CBE2', with_labels=True, edge_color=weights,
width=4, edge_cmap=plt.cm.Blues)
plt.show()
def view(self):
import matplotlib.pyplot as plt
networkx.draw(self)
plt.show()
G = build_karate_club_graph()
print('We have %d nodes.' % G.number_of_nodes())
print('We have %d edges.' % G.number_of_edges())
###############################################################################
# Visualize the graph by converting it to a `networkx
# `_ graph:
import networkx as nx
# Since the actual graph is undirected, we convert it for visualization
# purpose.
nx_G = G.to_networkx().to_undirected()
# Kamada-Kawaii layout usually looks pretty for arbitrary graphs
pos = nx.kamada_kawai_layout(nx_G)
nx.draw(nx_G, pos, with_labels=True, node_color=[[.7, .7, .7]])
###############################################################################
# Step 2: Assign features to nodes or edges
# --------------------------------------------
# Graph neural networks associate features with nodes and edges for training.
# For our classification example, we assign each node an input feature as a one-hot vector:
# node :math:`v_i`'s feature vector is :math:`[0,\ldots,1,\dots,0]`,
# where the :math:`i^{th}` position is one.
#
# In DGL, you can add features for all nodes at once, using a feature tensor that
# batches node features along the first dimension. The code below adds the one-hot
# feature for all nodes:
import torch
G.ndata['feat'] = torch.eye(34)
if self.experiment.epoch > 0:
# Get a list of the colors to use for each edge.
edge_colors = []
tmatrix = self.experiment.data['population']['transitions']
for f in range(self.max_types):
for t in range(self.max_types):
if tmatrix[f][t] > tmatrix[t][f]:
edge_colors.append(self.colors[f])
wt = tmatrix[f][t] - tmatrix[t][f]
graph.add_edge(f, t, weight=wt)
# Make the figure
plt.figure()
nx.draw(graph, with_labels=False, pos=nx.circular_layout(graph),
edge_color=edge_colors, node_color=self.colors, node_size=node_sizes, labels=self.node_labels)
if self.display_epoch:
plt.text(0, -0.05, "Epoch: %d" % (self.experiment.epoch),
horizontalalignment='left',
size='small')
filename = "%s-%06d.%s" % (self.filename, self.experiment.epoch, self.format)
data_file = self.datafile_path(filename)
plt.savefig(data_file, transparent=self.transparent)
plt.close()
def draw(self):
from networkx.drawing.nx_agraph import graphviz_layout
pos = graphviz_layout(self, prog='dot')
nx.draw(self, pos, with_labels=True)
# Create a BA model graph
n = 1000
m = 2
G = nx.generators.barabasi_albert_graph(n, m)
# find node with largest degree
node_and_degree = G.degree()
(largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
# Create ego graph of main hub
hub_ego = nx.ego_graph(G, largest_hub)
# Draw graph
pos = nx.spring_layout(hub_ego)
nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
# Draw ego as large and red
options = {"node_size": 300, "node_color": "r"}
nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
plt.show()