Skip to content

Commit 941c83e

Browse files
authored
Fix animations (#3)
* allowed dyads to be colored * added sample animation * updated animation notebook * deleted tmax * comment out ffmpeg
1 parent b3d7652 commit 941c83e

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,5 @@ dmypy.json
139139
cython_debug/
140140

141141
Figures/
142-
test.*
142+
test.*
143+
.DS_Store

hypercontagion/visualization/animation.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def contagion_animation(
11-
fig, H, transition_events, pos, node_colors, edge_colors, dt=1, fps=1
11+
fig, H, transition_events, pos, node_colors, edge_colors, dt=1, fps=1, **args
1212
):
1313
"""Generate an animation object of contagion process.
1414
@@ -30,6 +30,8 @@ def contagion_animation(
3030
the timestep at which to take snapshots of the system states
3131
fps : int, default: 1
3232
frames per second in the animation.
33+
**args :
34+
optional xgi draw args
3335
3436
Returns
3537
-------
@@ -58,11 +60,18 @@ def contagion_animation(
5860
node_state[target] = status
5961

6062
node_fc = {n: node_colors[node_state[n]] for n in H.nodes}
61-
edge_fc = {e: edge_colors[edge_state[e]] for e in H.edges}
63+
dyad_color = {
64+
e: edge_colors[edge_state[e]] for e in H.edges.filterby("size", 2)
65+
}
66+
edge_fc = {
67+
e: edge_colors[edge_state[e]] for e in H.edges.filterby("size", 2, "geq")
68+
}
6269

6370
# draw hypergraph
64-
plt.title(f"Time is {interval_time}")
65-
xgi.draw(H, pos=pos, node_fc=node_fc, edge_fc=edge_fc)
71+
xgi.draw(
72+
H, pos=pos, node_fc=node_fc, edge_fc=edge_fc, dyad_color=dyad_color, **args
73+
)
74+
plt.tight_layout()
6675
camera.snap()
6776

6877
return camera.animate(interval=1000 / fps)

tutorials/animation.mp4

131 KB
Binary file not shown.

tutorials/tutorial_3_animations.ipynb

+30-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"import numpy as np\n",
1414
"import random\n",
1515
"import networkx as nx\n",
16-
"from IPython.display import HTML"
16+
"from IPython.display import HTML\n",
17+
"import matplotlib.animation as animation"
1718
]
1819
},
1920
{
@@ -22,12 +23,17 @@
2223
"metadata": {},
2324
"outputs": [],
2425
"source": [
25-
"n = 100\n",
26-
"is_connected = False\n",
27-
"while not is_connected:\n",
28-
" H = xgi.random_hypergraph(n, [0.03, 0.0002, 0.00001])\n",
29-
" is_connected = xgi.is_connected(H)\n",
30-
"pos = xgi.barycenter_spring_layout(H)"
26+
"H = xgi.load_xgi_data(\"diseasome\")\n",
27+
"H.cleanup()"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"xgi.unique_edge_sizes(H)"
3137
]
3238
},
3339
{
@@ -37,10 +43,8 @@
3743
"outputs": [],
3844
"source": [
3945
"gamma = 0.05\n",
40-
"beta2 = 0.1\n",
41-
"beta3 = 0.05\n",
42-
"beta4 = 0.01\n",
43-
"tau = {1: 0, 2: beta2, 3: beta3, 4: beta4}\n",
46+
"beta = 0.05\n",
47+
"tau = {i: beta for i in xgi.unique_edge_sizes(H)}\n",
4448
"rho = 0.1"
4549
]
4650
},
@@ -51,7 +55,7 @@
5155
"outputs": [],
5256
"source": [
5357
"transition_events = hc.discrete_SIR(\n",
54-
" H, tau, gamma, tmin=0, tmax=40, dt=1, rho=rho, return_event_data=True\n",
58+
" H, tau, gamma, tmin=0, dt=1, rho=rho, return_event_data=True\n",
5559
")"
5660
]
5761
},
@@ -70,22 +74,29 @@
7074
"metadata": {},
7175
"outputs": [],
7276
"source": [
73-
"node_colors = {\"S\": \"green\", \"I\": \"red\", \"R\": \"blue\"}\n",
74-
"edge_colors = {\"S\": \"green\", \"I\": \"red\", \"R\": \"blue\", \"OFF\": \"grey\"}\n",
77+
"S_color = \"white\"\n",
78+
"I_color = 'firebrick'\n",
79+
"R_color = 'steelblue'\n",
80+
"\n",
81+
"node_colors = {\"S\": S_color, \"I\": I_color, \"R\": R_color}\n",
82+
"edge_colors = {\"S\": S_color, \"I\": I_color, \"R\": R_color, \"OFF\": \"grey\"}\n",
7583
"fps = 1\n",
7684
"fig = plt.figure(figsize=(10, 10))\n",
77-
"animation = hc.contagion_animation(\n",
78-
" fig, H, transition_events, pos, node_colors, edge_colors, fps=fps\n",
85+
"anim = hc.contagion_animation(\n",
86+
" fig, H, transition_events, pos, node_colors, edge_colors, fps=fps, node_size=10, dyad_lw=1.5\n",
7987
")\n",
80-
"HTML(animation.to_jshtml())"
88+
"# FFwriter = animation.FFMpegWriter(fps=fps)\n",
89+
"# anim.save('animation.mp4', writer = FFwriter)"
8190
]
8291
},
8392
{
8493
"cell_type": "code",
8594
"execution_count": null,
8695
"metadata": {},
8796
"outputs": [],
88-
"source": []
97+
"source": [
98+
"HTML(anim.to_jshtml())"
99+
]
89100
}
90101
],
91102
"metadata": {
@@ -104,7 +115,7 @@
104115
"name": "python",
105116
"nbconvert_exporter": "python",
106117
"pygments_lexer": "ipython3",
107-
"version": "3.10.4"
118+
"version": "3.10.0"
108119
},
109120
"orig_nbformat": 4,
110121
"vscode": {

0 commit comments

Comments
 (0)