-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_automata.py
185 lines (150 loc) · 5.24 KB
/
matrix_automata.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
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
from typing import Protocol
import numpy as np
from glumpy import app, gloo, gl, key
from rules import CallableRuleset, basic_convolution, fast_inv_gaussian_activation, checkerboard_intervetion
app.use('qt5')
class AutomataRuleset(Protocol):
def convolution(self, field: np.ndarray) -> np.ndarray:
...
def activation(self, field: np.ndarray) -> np.ndarray:
...
def __call__(self, field: np.ndarray) -> np.ndarray:
...
def intervention(self, field: np.ndarray, x: int, y: int) -> None:
...
class Buffer(Protocol):
def get_smoothed(self) -> np.ndarray:
...
def append(self, field: np.ndarray) -> None:
...
class MeanFrameBuffer:
def __init__(self, size):
self.size = size
self._buffer = []
def get_smoothed(self):
return np.mean(np.stack(self._buffer), axis=0)
def append(self, field):
self._buffer.append(field)
if len(self._buffer) > self.size:
self._buffer.pop(0)
class EveryNthBuffer:
def __init__(self, size):
self.size = size
self.state = 0
def get_smoothed(self):
return self._buffer
def append(self, field):
if self.state == 0:
self._buffer = field
self.state += 1
if self.state >= self.size:
self.state = 0
class OneFrameFakeBuffer:
'it just feels morally wrong to do real computations for a 1-frame buffer'
def __init__(self):
self._buffer = None
def get_smoothed(self):
return self._buffer
def append(self, field):
self._buffer = field
class AutomataDisplay:
def __init__(
self,
rules:AutomataRuleset,
field_size:'tuple[int,int]|float',
display_size:'tuple[int, int]',
color:'tuple[np.int8, np.int8, np.int8]',
buffer:Buffer=None,
fullscreen=True,
):
self.window = app.Window(*display_size, fullscreen=fullscreen)
disx, disy = self.window.get_size()
if isinstance(field_size, (int, float)):
field_size = (int(disy * field_size), int(disx * field_size))
self.field = np.random.default_rng(42).uniform(0, 1, size=field_size).astype('float32')
self.rules = rules
if buffer is None:
self.buffer = OneFrameFakeBuffer()
else:
self.buffer = buffer
self.quad = gloo.Program(
vertex='''
attribute vec2 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
v_texcoord = texcoord;
}
''',
fragment='''
uniform vec3 color;
uniform sampler2D texture;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = vec4(color * texture2D(texture, v_texcoord).r, 1);
}
''',
count=4,
)
self.quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.quad['texcoord'] = [
(0, 1), (0, 0), (1, 1), (1, 0)] # fmt: skip
self.quad['texture'] = self.field
self.quad['color'] = np.array(color) / 255
self.pause = False
self.total_frames = 0
self.window.event(self.on_draw)
self.window.event(self.on_key_press)
self.window.event(self.on_mouse_drag)
self.window.event(self.on_mouse_press)
def on_draw(self, dt):
if not self.pause:
self.total_frames += 1
self.field = self.rules(self.field)
self.buffer.append(self.field)
self.quad['texture'] = self.buffer.get_smoothed()
self.window.clear()
self.quad.draw(gl.GL_TRIANGLE_STRIP)
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE:
print(f'[i] Quitting. FPS was {self.window.fps:.2f}. '
f'Simultaion ran for {self.total_frames} frames.'
)
print(f'{np.mean(self.field) = }')
app.quit()
elif symbol == key.SPACE:
self.pause = not self.pause
def mouse_input(self, posx, posy):
disx, disy = self.window.get_size()
fieldy, fieldx = self.field.shape
x = int(posx / disx * fieldx)
y = int(posy / disy * fieldy)
try:
self.rules.intervention(self.field, y, x)
except (ValueError, IndexError):
pass
def on_mouse_drag(self, x, y, dx, dy, button):
self.mouse_input(x, y)
def on_mouse_press(self, x, y, button):
self.mouse_input(x, y)
def __call__(self, framerate=0):
app.run(framerate=framerate)
if __name__ == '__main__':
AutomataDisplay(
rules=CallableRuleset(
np.array([[-0.88885623, 0.49463819, -0.88885623],
[ 0.49463819, -0.74555808, 0.49463819],
[-0.88885623, 0.49463819, -0.88885623]]),
basic_convolution,
fast_inv_gaussian_activation,
checkerboard_intervetion
),
display_size=(1, 1),
field_size=1,
color=(100, 180, 100),
fullscreen=True,
# buffer=EveryNthBuffer(4),
)()