-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagery.py
280 lines (230 loc) · 7.55 KB
/
imagery.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import itertools
from random import random, choice
from math import sqrt
from PIL import Image
import numpy as np
# TODO fix before publicaiton: run analysis seems to have a bug for lavender?! Square lengths are non-constant!? One bug identified, either fix or exclude lavender from the story—leaning towards exclusion, because the absence of zeros spoils the "burst" narrative? But solve the bug first; bugs are always bad even when they "don't matter"
# have to substitute where I already used LAVENDER as a label in story
# also double-check possible label lengths 10/13/16 &c.
WIDTH = LENGTH = 473
# https://stackoverflow.com/a/14382692
def cartesian_to_barycentric(q, p0, p1, p2):
d = 0.5 * (
-p1[1] * p2[0]
+ p0[1] * (-p1[0] + p2[0])
+ p0[0] * (p1[1] - p2[1])
+ p1[0] * p2[1]
)
s = (
1
/ (2 * d)
* (
p0[1] * p2[0]
- p0[0] * p2[1]
+ (p2[1] - p0[1]) * q[0]
+ (p0[0] - p2[0]) * q[1]
)
)
t = (
1
/ (2 * d)
* (
p0[0] * p1[1]
- p0[1] * p1[0]
+ (p0[1] - p1[1]) * q[0]
+ (p1[0] - p0[0]) * q[1]
)
)
return s, t
color_map = {
"RED": (200, 0, 0),
"GREEN": (0, 200, 0),
"BLUE": (0, 0, 200),
"TEAL": (0, 150, 150),
"YELLOW": (240, 240, 0),
# "LAVENDER": (150, 100, 200),
}
def read_label(label):
return ''.join(chr(c) for c in label)
def triangle_points():
return [(157*random(), 157*random()), (315+157*random(), 157+157*random()), (157*random(), 315+157*random())]
def in_triangle(q, points):
s, t = cartesian_to_barycentric(q, *points)
return s > 0 and t > 0 and 1 - s - t > 0
def noise(color):
return [c + 10*random() for c in color]
def triangle_pixels(color):
points = triangle_points()
return [[noise(color) if in_triangle((x, y), points) else noise((0, 0, 0)) for x in range(WIDTH)] for y in range(LENGTH)]
def symmetric_params():
return [(150 + 100*random())/2, 236 + 100*(random() - 0.5), 236 + 100*(random() - 0.5)]
def square_pixels(color):
halflength, x_center, y_center = symmetric_params()
pixels = []
for x in range(WIDTH):
row = []
for y in range(LENGTH):
if abs(x_center - x) < halflength and abs(y_center - y) < halflength:
row.append(noise(color))
else:
row.append(noise((0, 0, 0)))
pixels.append(row)
return pixels
def circle_pixels(color):
radius, x_center, y_center = symmetric_params()
pixels = []
for x in range(WIDTH):
row = []
for y in range(LENGTH):
if sqrt((x - x_center)**2 + (y - y_center)**2) < radius:
row.append(noise(color))
else:
row.append(noise((0, 0, 0)))
pixels.append(row)
return pixels
def data_array(pixels):
data = []
for row in pixels:
for pixel in row:
for channel in pixel:
data.append(int(channel))
return data
def count_burst_lengths(data):
bursts = []
counter = 0
previous = None
for datum in data:
if datum >= 240:
counter += 1
else:
# consecutive "ordinary" numbers mean the burst is over
if counter and previous and previous < 240:
bursts.append(counter)
counter = 0
previous = datum
return bursts
def high_runs(data):
runs = []
high_counter = 0
low_counter = 0
for datum in data:
if datum > 100:
high_counter += 1
low_counter = 0
if datum < 100:
low_counter += 1
if low_counter > 3 and high_counter > 1:
runs.append(high_counter)
low_counter = high_counter = 0
return runs
def run_derivative(run_sequence):
diffs = []
previous = run_sequence[0]
for run in run_sequence[1:]:
diffs.append(run - previous)
previous = run
return diffs
import collections
from itertools import chain, repeat
import operator
def convolve(signal, kernel):
# See: https://betterexplained.com/articles/intuitive-convolution/
# convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
# convolve(data, [1, -1]) --> 1st finite difference (1st derivative)
# convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
kernel = tuple(kernel)[::-1]
n = len(kernel)
window = collections.deque([0], maxlen=n) * n
for x in chain(signal, repeat(0, n-1)):
window.append(x)
yield round(sum(map(operator.mul, kernel, window)), 2)
def smoothed(data):
smoothed = []
return list(convolve(data, [0.25, 0.25, 0.25, 0.25]))
def find_burst(seq):
for i, el in enumerate(seq):
if el > 100:
return i
def do_shape(color_name, shape_name):
color = color_map[color_name]
if shape_name == "TRIANGLE":
pixels = triangle_pixels(color)
elif shape_name == "SQUARE":
pixels = square_pixels(color)
elif shape_name == "CIRCLE":
pixels = circle_pixels(color)
label = [ord(c) for c in "{} {}".format(color_name, shape_name)]
data = data_array(pixels)
run_analysis = high_runs(data)
diffs = run_derivative(run_analysis)
array = np.array(pixels, dtype=np.uint8)
shape_image = Image.fromarray(array)
shape_image.save("{}_{}.png".format(color_name.lower(), shape_name.lower()))
return data, run_analysis, diffs, label
def random_shape_spec(no_square=False):
if no_square:
shape_names = ["TRIANGLE", "CIRCLE"]
else:
shape_names = ["TRIANGLE", "SQUARE", "CIRCLE"]
color_name = choice(list(color_map.keys()))
shape_name = choice(shape_names)
return color_name, shape_name
def do_random_shape(no_square=False):
color_name, shape_name = random_shape_spec(no_square=no_square)
return do_shape(color_name, shape_name)
def do_answer_key_table_entry(color_name, shape_name):
data, run_analysis, diffs, label = do_shape(color_name, shape_name)
i = find_burst(data) + 1
if shape_name in ["TRIANGLE", "CIRCLE"]:
lengths = "increasing, then decreasing"
else:
lengths = "constant"
if color_name in ["GREEN", "BLUE"]:
pattern_essence = "RED"
else:
pattern_essence = color_name
pattern = ', '.join(str(c) for c in color_map[pattern_essence])
if pattern == (0, 150, 150):
pattern = (150, 150, 0)
print_label = ', '.join(str(c) for c in label)
return "<tr><td>{lengths}</td><td>{pattern}</td><td>{i}</td><td>{print_label}</td></tr>".format(**locals())
def do_random_answer_key_table_entry():
color_name, shape_name = random_shape_spec()
return do_answer_key_table_entry(color_name, shape_name)
p = [('GREEN', 'CIRCLE'),
('BLUE', 'SQUARE'),
('YELLOW', 'CIRCLE'),
('TEAL', 'SQUARE'),
('RED', 'SQUARE'),
('YELLOW', 'SQUARE'),
('BLUE', 'CIRCLE'),
('RED', 'TRIANGLE'),
('BLUE', 'TRIANGLE'),
('GREEN', 'TRIANGLE'),
('YELLOW', 'TRIANGLE'),
('TEAL', 'TRIANGLE'),
('GREEN', 'SQUARE'),
('RED', 'CIRCLE'),
('TEAL', 'CIRCLE')]
def confirm_index(color_name, shape_name):
data, run_analysis, diffs, label = do_shape(color_name, shape_name)
i = find_burst(data) + 1
return (color_name, i%3)
# In [13]: color_index = [confirm_index(c, s) for c, s in p]
# In [15]: sorted(color_index)
# Out[15]:
# [('BLUE', 0),
# ('BLUE', 0),
# ('BLUE', 0),
# ('GREEN', 2),
# ('GREEN', 2),
# ('GREEN', 2),
# ('RED', 1),
# ('RED', 1),
# ('RED', 1),
# ('TEAL', 2),
# ('TEAL', 2),
# ('TEAL', 2),
# ('YELLOW', 1),
# ('YELLOW', 1),
# ('YELLOW', 1)]