-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
200 lines (140 loc) · 5.01 KB
/
utils.py
File metadata and controls
200 lines (140 loc) · 5.01 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
'''
Author: Mario Massimo
Date: March 2025
'''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import display, HTML
def animate(birds_positions_per_time_step, birds_velocities_per_time_step, space_length, num_time_steps, save = False):
''' Animates the simulation creating a video/GIF and save it if required
Parameters:
-----------
boid_positions_per_time_step : np.ndarray
Birds positions per each time step
boid_velocities_per_time_step : np.ndarray
Birds positions per each time step
space_length : float
Length of the side of the square containing the birds
num_time_steps : int
Total number of time steps
save : bool, optional
Bool variable to save or not the gif produced, default is False
Returns:
-----------
None
Raises:
TypeError:
If save is not a Bool
'''
if not isinstance(save, bool):
raise TypeError('Save argument must be boolean')
fig, ax = plt.subplots(figsize=(7,7))
velocities_magnitudes = np.linalg.norm(birds_velocities_per_time_step[0], axis=1)
velocities_normalized = birds_velocities_per_time_step[0] / np.reshape(velocities_magnitudes, (-1,1))
scat = ax.quiver(birds_positions_per_time_step[0][:,0],
birds_positions_per_time_step[0][:,1],
velocities_normalized[:,0],
velocities_normalized[:,1], scale=14, scale_units='inches')
ax.set_xlim([0,space_length])
ax.set_ylim([0,space_length])
def update(frame):
scat.set_offsets(birds_positions_per_time_step[frame])
velocities_magnitudes = np.linalg.norm(birds_velocities_per_time_step[frame], axis=1)
velocities_normalized = birds_velocities_per_time_step[frame]/ np.reshape(velocities_magnitudes, (-1,1))
scat.set_UVC(velocities_normalized[:,0],
velocities_normalized[:,1])
return scat,
ani = FuncAnimation(fig, update, frames=num_time_steps, blit=True, interval = 25)
ax.axis('off')
print("Animation finished. Video processing . . .")
display(HTML(ani.to_jshtml()))
plt.show()
if save:
ani.save('flock_simulation.gif', writer="pillow", fps=30)
def isint(string):
''' A function that returns True if the input string
can be casted into an int without ValueError
Parameters:
-----------
string : str
Input string to be casted
Returns:
-----------
True : bool
If the input string can be casted into an int
False : bool
If the input string cannot be casted into an int
'''
try:
int(string)
return True
except ValueError:
return False
def isfloat(string):
''' A function that returns True if the input string
can be casted into a float without ValueError
Parameters:
-----------
string : str
Input string to be casted
Returns:
-----------
True : bool
If the input string can be casted into a float
False : bool
If the input string cannot be casted into a float
'''
try:
float(string)
return True
except ValueError:
return False
def isbool(string):
''' A function that returns True if the input string
is equal to one of the key words meaning a bool variable ('true'/'false' or 'yes'/'no')
Parameters:
-----------
string : str
Input string
Returns:
-----------
True : bool
If the input string is within a list of key words
False : bool
If the input string isn't within a list of key words
'''
return string.lower() in ['true', 'false', 'yes', 'no']
def set_type(string):
''' A function that given a string returns the value
of the string content with the correct type (i.e. an int, a float or a bool)
if the content is not a sentence/a word
Parameters:
-----------
string : str
Input string
Returns:
-----------
int(string) : int
If the input string can be casted into an int
float(string) : float
If the input string can be casted into a float
True : bool
If the input string is within a list of key words meaning boolean True ('true'/'yes')
False : bool
If the input string is within a list of key words meaning boolean False ('false'/'no')
string : str
Otherwise
'''
if isint(string):
return int(string)
elif isfloat(string):
return float(string)
elif isbool(string):
if string.lower() in ['true', 'yes']:
return True
elif string.lower() in ['false', 'no']:
return False
else:
return string