-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimations.py
114 lines (81 loc) · 3.12 KB
/
animations.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
102
103
104
105
106
107
108
109
110
111
112
113
from celluloid import Camera
from testfunctions import Function2D, ShafferF62D
from dataclasses import dataclass
import matplotlib as mat
import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
import typing as t
class BadData(Exception):
def __init__(self):
super().__init__('Bad Data.')
@dataclass
class Datapoints:
xcoords: t.List[NDArray[np.float32]]
ycoords: t.List[NDArray[np.float32]]
axis: mat.axis.Axis
colour: str = '#2164b0'
alpha: float = 0.6
@classmethod
def fromListCoords(cls, l: t.List[t.List[NDArray[np.float32]]],
axis: mat.axis.Axis,
colour: str ='#2164b0',
alpha: float = 0.6):
xcoords = []
ycoords = []
for li in l:
n = len(li)
x = np.full(n, 0.0, dtype='float32')
y = np.full(n, 0.0, dtype='float32')
for j, (a, b) in enumerate(li):
x[j] = a
y[j] = b
xcoords.append(x)
ycoords.append(y)
return cls(xcoords, ycoords, axis, colour, alpha)
class ScatterAnimation:
# every a in args:
# a = (x, y, c, a)
# x, y is lists of lists where sublists contain x, y coordinates
# len(x) = len(y) < numframes
#
# c is the colour of the plotted points: (rgb_string, alpha)
# a is the axis to plot
def __init__(self, numframes: int = 100, fig: mat.figure.Figure = None, data: t.List[Datapoints] = None) -> None:
if(data is None):
raise BadData()
for d in data:
if ((len(d.xcoords) != len(d.ycoords)) or (len(d.ycoords) < numframes)):
raise BadData()
self.data = data
self.numframes: int = numframes
self.camera = Camera(fig)
def createAnimation(self) -> mat.animation.ArtistAnimation:
for i in range(self.numframes):
for d in self.data:
d.axis.scatter(x = d.xcoords[i], y = d.ycoords[i], c = d.colour, alpha = d.alpha)
self.camera.snap()
anim = self.camera.animate()
return anim
def createFunctionAnimation(funcObj: Function2D, axis: mat.axis.Axis, filesavepath: str, sa: ScatterAnimation):
anim = sa.createAnimation()
funcObj.show(axis)
anim.save(filesavepath)
if __name__ == '__main__':
fig, ax1 = plt.subplots(nrows = 1, ncols = 1, figsize=(12, 8), dpi = 80)
sf = ShafferF62D(xshift = np.float32(30.0), yshift = np.float32(-30.0))
numframes = 10
x1 = []
y1 = []
x2 = []
y2 = []
rng = np.random.default_rng()
for j in range(numframes):
x1.append(rng.uniform(low = -100, high = 100, size = 10))
y1.append(rng.uniform(low = -100, high = 100, size = 10))
x2.append(rng.uniform(low = -100, high = 100, size = 10))
y2.append(rng.uniform(low = -100, high = 100, size = 10))
d1 = Datapoints(x1, y1, ax1)
d2 = Datapoints(x2, y2, ax1, '#25ba6b')
sa = ScatterAnimation(numframes, fig, [d1, d2])
createFunctionAnimation(sf, ax1, './images/hello2.gif', sa)