-
Notifications
You must be signed in to change notification settings - Fork 2
/
evolution.py
108 lines (81 loc) · 3.1 KB
/
evolution.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
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from progressbar import ProgressBar
from array2gif import write_gif
"""
Coupled Map Lattice Evolution plotting functionality
- Takes any CML as input
- Handles visualization separately from CML simulation code
"""
class Evolution:
def __init__(self, cml, colormap='plasma'):
self.cml = cml
self.colormap = colormap
def time_evolution(self, iterations, modulus=1, save_figure=False, save_data=False, show_figure=True):
# generate evolution data
data = np.array(self.cml.get_lattice())
bar = ProgressBar(max_value=(iterations-1))
for i in range(iterations - 1):
new_data = self.cml.update()
if i % modulus == 0:
data = np.vstack((data, new_data))
bar.update(i)
"""
for nice blurring
from scipy.ndimage.filters import gaussian_filter
data = gaussian_filter(data, sigma=7)
"""
if save_data:
np.save("time_evolution.npy", data)
fig = plt.figure(figsize=(6, 3.2))
ax = fig.add_subplot(111)
ax.set_title('Lattice Evolution')
plt.imshow(data, cmap=self.colormap)
ax.set_aspect('equal')
# we want the lattice "evolving upward"
plt.gca().invert_yaxis()
cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
if save_figure:
plt.savefig("time_evolution.png")
if show_figure:
plt.show()
return data
def alpha_evolution(self, steps=100, time_iterations=100, max=2.0, min=1.5, save_figure=True, save_data=False):
# First, generate all data
data_array = []
rgb_data_array = []
for index, al in enumerate(np.linspace(min, max, steps)):
print("Evolution {} out of {}...".format(index, steps))
# Update CML's alpha value
self.cml.coupling.map_obj.set_alpha(al)
data = self.time_evolution(time_iterations, 1, False, False, False)
# For now: artificial RGB for array2gif
g_to_rgb = lambda x: [x, x, x]
rgb_data = []
for row in data:
rgb_row = []
for val in row:
rgb_row.append(g_to_rgb(val))
rgb_data.append(rgb_row)
rgb_data = np.array(rgb_data)
rgb_data = (rgb_data + 1.0) * 255.0 / 2.0
rgb_data = rgb_data.astype(int)
data_array.append(data)
rgb_data_array.append(rgb_data)
if save_figure:
write_gif(rgb_data_array, "alpha_evolution.gif", fps=10)
if save_data:
np.save("alpha_evolution.npy", np.array(data_array))
return np.array(data_array)
"""
cml = CML(dim=100, coupling=TwoNeighbor(strength=0.2, map_obj=KanekoLogistic(alpha=1.47)))
ev = Evolution(cml)
ev.plot_time_evolution(10)
"""