-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation_IM.py
82 lines (70 loc) · 3.28 KB
/
animation_IM.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The movements of the infectious "super mosquitoes" (IM) are shown here in a
video.
"""
#==============================================================================
# Load modules
#==============================================================================
import os
import glob
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
import Model_Szenario as MS
#==============================================================================
# Get testregion from the last modelrun
#==============================================================================
testregion = MS.testregion
supermosquitoes = MS.superagents
#==============================================================================
# Read the spatial outputs (numpy arrays) from the last modelrun,
# read them in the order like they were dumped by using the time stamp of the
# file
#==============================================================================
DaylyInfs_365 = []
for array in sorted(glob.glob('DaylyInf_{}_*.npy'.format(testregion)),
key=os.path.getmtime):
print(array)#check order of reading the files
DaylyInfs_365.append(np.load(array))
#==============================================================================
# Remove days before the infectious period starts to make the video shorter
#==============================================================================
DaylyInfs = DaylyInfs_365.copy()
daycount = 0
while np.sum(DaylyInfs[0])==0:
DaylyInfs.pop(0)
daycount += 1
#print(daycount, " removed")
print("Start date of the infectious period: ",daycount)
#==============================================================================
# Remove days after the infectious period ended to make the video shorter:
#==============================================================================
while np.sum(DaylyInfs[-1])==0:
DaylyInfs.pop(-1)
#print("removed last item")
print("Period with infectious mosquitoes: ",len(DaylyInfs))
#==============================================================================
# Animate the infectious mosquitoes
# -> Uncommend "ani.save..." to save the animation (this may take a minute)
#==============================================================================
maxI = np.max(DaylyInfs)
cmap = "inferno"
fig, ax = plt.subplots(figsize=(10,10),dpi=150)
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
im = ax.imshow(DaylyInfs[0],cmap=cmap,vmin=0,vmax=maxI) # axesimage object
def updatefig(i):
im.set_array(DaylyInfs[i]) # set data in the axesimage object
ax.set_title("Day {}".format(daycount+i+1))
return [im] # return artists set
plt.colorbar(im, cax=cax, orientation='vertical', ticks=range(maxI+1))
ani = animation.FuncAnimation(fig, updatefig, frames = len(DaylyInfs),
interval=300, repeat = False,
blit= False,
cache_frame_data = True)
#ani.save('Infectious_{}_super{}.mp4'.format(testregion, supermosquitoes),
# fps=2.5, bitrate= -1, dpi=300)
plt.show()