-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
56 lines (43 loc) · 1.74 KB
/
utils.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
"""
Functions shared by both ``benchmarks.py`` and ``detection.py``.
"""
from matplotlib import pyplot as plt, animation as anim
TEXT = "t"
ANIMATION = "a"
def get_out_mode():
"""Let the user decide how to display the output."""
mode = input("\nHow do you want the output to be displayed? Type \"t\" for text-only, \"a\" for animation: ")
while mode != TEXT and mode != ANIMATION:
mode = input("Please type either \"t\" or \"a\": ")
return mode
def gen_data(swarm):
"""Generate data for the animation step."""
while swarm.has_not_converged():
swarm.update_swarm()
x = swarm.positions[:, 0]
y = swarm.positions[:, 1]
z = swarm.get_f_values()
count = swarm.epoch_count
yield x, y, z, count
def animate(fig, update, gen_data, fps):
"""
Create and show an animation.
:param fig: matplotlib.pyplot.Figure object on which to perform the animation.
:param update: function performing the animation step.
:param gen_data: function generating the data for the animation step.
:param fps: frames per second.
:return: matplotlib.animation.FuncAnimation object.
"""
animation = anim.FuncAnimation(fig, update, gen_data, interval=1e3/fps, blit=True, repeat=False, save_count=1500)
plt.show()
return animation
def save(animation, name, fps, ext=".gif"):
"""
Save animation as either a .gif or .mp4 file.
:param animation: matplotlib.animation.FuncAnimation object.
:param name: name of the saved file.
:param fps: frames per second.
:param ext: extension of the saved file, either .gif or .mp4.
"""
writer = anim.PillowWriter(fps) if ext == ".gif" else anim.FFMpegWriter(fps)
animation.save("images/" + name + ext, writer)