forked from nelsocs/dzcnapyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutrients.py
executable file
·54 lines (43 loc) · 1.65 KB
/
nutrients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Create and visualize the network of foods and nutrients
"""
import networkx as nx
import matplotlib.pyplot as plt
import dzcnapy_plotlib as dzcnapy
import csv
with open("/home/chrisnelson/move/flowlogs-accept.log") as infile:
csv_reader = csv.reader(infile)
G = nx.Graph(csv_reader)
print(G.nodes())
loops = list(G.selfloop_edges())
G.remove_edges_from(loops)
print(loops)
mapping = {node: node.title() for node in G if isinstance(node, str)}
nx.relabel_nodes(G, mapping, copy=False)
print(G.nodes())
nutrients = set(("172.31.27.238","52.15.159.198","54.194.229.79","91.189.88.152","91.189.91.157","91.189.92.19","91.189.92.20","91.189.92.40","91.189.92.41"))
nutrient_dict = {node: (node in nutrients) for node in G}
nx.set_node_attributes(G, nutrient_dict, "nutrient")
# Prepare for drawing
colors = ["yellow" if n[1]["nutrient"] else "pink" for n in
G.nodes(data=True)]
dzcnapy.medium_attrs["node_color"] = colors
# Draw four layouts in four subplots
_, plot = plt.subplots(2, 2)
subplots = plot.reshape(1, 4)[0]
layouts = (nx.random_layout, nx.circular_layout, nx.spring_layout,
nx.spectral_layout)
titles = ("Random", "Circular", "Force-Directed", "Spectral")
for plot, layout, title in zip(subplots, layouts, titles):
pos = layout(G)
nx.draw_networkx(G, pos=pos, ax=plot, with_labels=False,
**dzcnapy.medium_attrs)
plot.set_title(title)
dzcnapy.set_extent(pos, plot)
dzcnapy.plot("nutrients")
from networkx.drawing.nx_agraph import graphviz_layout
_, plot = plt.subplots()
pos = graphviz_layout(G)
nx.draw_networkx(G, pos, **dzcnapy.attrs)
dzcnapy.set_extent(pos, plot)
dzcnapy.plot("nutrients-graphviz")