-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
101 lines (72 loc) · 2.81 KB
/
example.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd
from drawing import chord_diagram
def networkx_drawing_example(graph, img_path):
fig, ax = plt.subplots(figsize=(6, 6))
nx.draw_circular(graph, ax=ax)
ax.set_title("Graph drawn with networkx")
fig.savefig(img_path)
def simple_example(graph, img_path):
fig, ax = plt.subplots(figsize=(6, 6))
ax = chord_diagram(graph, ax)
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_title("A simple graph drawn better")
fig.savefig(img_path)
def cells_network_example(graph, img_path):
fig, ax = plt.subplots(figsize=(6, 6))
chord_diagram(graph, ax)
ax.set_title(img_path)
fig.savefig("images/2_cells_network.png")
def not_circular_layout_example(img_path):
from drawing import draw_graph_edges
graph = nx.Graph()
# Create a random graph with self-loops
graph.add_nodes_from(range(10))
while len(graph.edges) < 30:
node_i, node_j = np.random.choice(graph.nodes, 2)
graph.add_edge(node_i, node_j)
graph.edges[(node_i, node_j)]["weight"] = np.random.randint(1, 5)
pos = nx.kamada_kawai_layout(graph)
fig, ax = plt.subplots(figsize=(6, 6))
nx.draw_networkx_nodes(graph, pos, ax=ax)
ax = draw_graph_edges(graph, pos, ax)
ax.set_title("Cells communication network")
fig.savefig(img_path)
def cells_communication_graph():
cell_types = ("RPS expressing", "B", "CD4 T", "CD14 Monocytes", "NK",
"CD8 T", "FCGR3A Monocytes", "Dendritic", "Megakaryocytes")
communication_network = np.array(
[
(1, 0, 1, 1, 1, 1, 1, 1, 0),
(1, 0, 1, 2, 1, 1, 2, 2, 0),
(0, 0, 1, 1, 1, 1, 1, 1, 0),
(0, 0, 0, 3, 0, 0, 3, 3, 0),
(1, 0, 1, 3, 1, 1, 3, 3, 0),
(0, 0, 0, 1, 0, 0, 1, 1, 0),
(0, 1, 0, 3, 0, 0, 3, 3, 1),
(0, 0, 0, 3, 0, 0, 3, 3, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 1)
]
)
df = pd.DataFrame(communication_network, index=cell_types, columns=cell_types)
return nx.DiGraph(df)
if __name__ == "__main__":
np.random.seed(42)
graph = nx.DiGraph(
np.array([
[1, 2, 1, 3, 5],
[1, 0, 3, 0, 0],
[1, 1, 3, 0, 1],
[0, 0, 2, 0, 1],
[1, 1, 1, 1, 1]
])
)
networkx_drawing_example(graph, img_path="images/0_simple_graph_networkx.png")
simple_example(graph, img_path="images/1_simple_graph.png")
cells_communication_graph = cells_communication_graph()
cells_network_example(cells_communication_graph, img_path="Cells communication network")
networkx_drawing_example(cells_communication_graph, img_path="images/3_nx_cells_network.png")
not_circular_layout_example(img_path="images/4_not_circular_layout.png")