-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtutils.py
62 lines (48 loc) · 1.64 KB
/
tutils.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
"""
Some utility functions for blog post on Turing Patterns.
"""
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
class BaseStateSystem:
"""
Base object for "State System".
We are going to repeatedly visualise systems which are Markovian:
the have a "state", the state evolves in discrete steps, and the next
state only depends on the previous state.
To make things simple, I'm going to use this class as an interface.
"""
def __init__(self):
raise NotImplementedError()
def initialise(self):
raise NotImplementedError()
def initialise_figure(self):
fig, ax = plt.subplots()
return fig, ax
def update(self):
raise NotImplementedError()
def draw(self, ax):
raise NotImplementedError()
def plot_time_evolution(self, filename, n_steps=30):
"""
Creates a gif from the time evolution of a basic state syste.
"""
self.initialise()
fig, ax = self.initialise_figure()
def step(t):
self.update()
self.draw(ax)
anim = animation.FuncAnimation(fig, step, frames=np.arange(n_steps), interval=20)
anim.save(filename=filename, dpi=60, fps=10, writer='imagemagick')
plt.close()
def plot_evolution_outcome(self, filename, n_steps):
"""
Evolves and save the outcome of evolving the system for n_steps
"""
self.initialise()
fig, ax = self.initialise_figure()
for _ in range(n_steps):
self.update()
self.draw(ax)
fig.savefig(filename)
plt.close()