-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
301 lines (236 loc) · 9.44 KB
/
utils.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import copy
from imageio import mimsave, imwrite
from tqdm import tqdm
from data import DataIteratorDownload, Logger
data = None
def initialize_data_loader(**kwargs):
global data
data = DataIteratorDownload(
"ricobruland/reddit-2023-rplace-pixel-data", **kwargs)
return data
colors = {}
def string_to_color(hex_color: str):
global colors
if hex_color in colors:
return colors[hex_color]
else:
hex_color_no_hashtag = hex_color.removeprefix('#')
# Convert hex to RGB
rgb = [int(hex_color_no_hashtag[i:i+2], 16) for i in range(0, 6, 2)]
colors[hex_color] = rgb
return rgb
def string_to_coordinates(s: str):
def translate(coord):
return (int(coord[1])+500, int(coord[0])+1000)
l = s.split(",")
match len(l):
case 2:
return [translate(l)]
case 3:
for i in range(3):
l[i] = l[i].removeprefix("{")
l[i] = l[i].removesuffix("}")
l[i] = l[i].strip()
l[i] = l[i].split(":")[1]
center = translate(l[:2])
radius = int(l[2])
# generate all points from the circle
coords = []
for x in range(center[0] - radius, center[0] + radius):
for y in range(center[1] - radius, center[1] + radius):
if (x - center[0])**2 + (y - center[1])**2 <= radius**2:
coords.append((x, y))
return coords
case 4:
left_up_corner = center = translate(l[:2])
right_down_corner = center = translate(l[2:])
# generate all points from the rectangle
coords = []
for x in range(left_up_corner[0], right_down_corner[0]):
for y in range(left_up_corner[1], right_down_corner[1]):
coords.append((x, y))
return coords
case _:
raise ValueError("String did not have proper format")
def binary_search(arr, target):
"""
get the first element in arr that is superior or equal to target
"""
left, right = 0, len(arr) - 1
while left < right:
mid = (left + right) // 2
if arr[mid] == target:
return mid # Element found, return its index
elif arr[mid] < target:
left = mid + 1 # Adjust the search range to the right half
else:
right = mid # Adjust the search range to the left half
return left
def convertImage(image, transform, dtype=np.uint8):
# convert the image to RGB
rezImage = np.zeros((len(image), len(image[0]), 3), dtype=dtype)
for i in range(len(image)):
for j in range(len(image[i])):
rezImage[i][j] = transform(image[i][j])
return rezImage
def visualise(image, transform):
# visualise with plt a matrix of 1000x1000 with colors for each coordinate
image = convertImage(image, transform)
plt.imshow(image)
plt.show()
def summary(startingTimeStamp, EndingTimeStamp, startingData, df, summary_function):
start_index = binary_search(df.timestamp, startingTimeStamp)
end_index = binary_search(df.timestamp, EndingTimeStamp)
df["is_mod"] = False
df = df.iloc[start_index:end_index]
rezData = startingData
for row in tqdm(df.itertuples(), total= end_index - start_index):
l = string_to_coordinates(row.coordinate)
is_mod = len(l) > 1
for coord in l:
x, y = coord
try:
rezData[x][y] = summary_function(
rezData[x][y], row, is_mod)
except Exception as e:
print(x, y)
print(row.coordinate)
raise e
return rezData
TimePassed = pd.Timedelta | int
class DynamicList:
def __init__(self, original_list, func):
self.original_list = original_list
self.func = func
def __len__(self):
return len(self.original_list)
def __getitem__(self, index):
return self.func(self.original_list[index])
def __setitem__(self, index, value):
raise PermissionError("This is a view as such you can't modify it")
def __delitem__(self, key):
raise PermissionError("This is a view as such you can't modify it")
def convert_to_milliseconds(value: TimePassed) -> int:
if isinstance(value, pd.Timedelta):
return int(value / pd.Timedelta(milliseconds=1))
else:
return value
def visualise_at_interval(summary_function,
transforms,
interval: TimePassed,
startingState,
rezfilename,
startingTimeStamp: TimePassed = 0,
EndingTimeStamp: TimePassed = -1,
duration: TimePassed = -1,
rez = None):
# make sure data exists
global data
if not data:
data = initialize_data_loader()
data.dh.clear()
i = 0
# Create a display on which most thing from this function will be printed
dh = Logger(display_id=1)
if not dh:
raise RuntimeError(
"I don't how it happened but it didn't create a display")
# Get startStamps from the data
files_and_timestamp = data.startStamps
# Transform Timedelta to ms
startingTimeStamp = convert_to_milliseconds(startingTimeStamp)
EndingTimeStamp = convert_to_milliseconds(EndingTimeStamp)
duration = convert_to_milliseconds(duration)
interval = convert_to_milliseconds(interval)
# Give actual default value to EndingTimeStamp
if EndingTimeStamp < 0:
EndingTimeStamp = files_and_timestamp[-1][1]
idx = binary_search(DynamicList(files_and_timestamp,
lambda x: x[1]), startingTimeStamp)
if interval > 0:
currentTimeStamp = startingTimeStamp
nextTimeStamp = currentTimeStamp + (duration if duration > 0 else interval)
else :
currentTimeStamp = startingTimeStamp
nextTimeStamp = EndingTimeStamp
_, nextFileTimeStamp = files_and_timestamp[idx]
df = data[idx]
currentState = copy.deepcopy(startingState)
if rez is None:
rez = []
rez.append(startingState)
start_idx = idx
while currentTimeStamp < EndingTimeStamp:
# make the summary
currentState = summary(
currentTimeStamp, nextTimeStamp, currentState, df, summary_function)
if nextTimeStamp > nextFileTimeStamp and idx + 1 < len(files_and_timestamp):
if duration < 0 :
del data.cache[idx]
idx += 1
_, nextFileTimeStamp = files_and_timestamp[idx]
df = data[idx]
else:
if nextTimeStamp > nextFileTimeStamp and duration > 0:
try:
if (nextTimeStamp - nextFileTimeStamp) / duration > 0.5:
break
currentState *= 1 - \
(nextTimeStamp - nextFileTimeStamp) / duration
except Exception as e:
# Catch any exception without specifying a specific type.
print(f"An error occurred: {e}")
i += 1
if duration > 0:
rez.append(currentState)
currentState = copy.deepcopy(startingState)
dh.update(
f"""
just made summary from {pd.Timedelta(milliseconds=currentTimeStamp)} to {pd.Timedelta(milliseconds=nextTimeStamp)}
epochs : {i}
{len(rez)}
"""
)
else:
# save the current state
rez.append(copy.deepcopy(currentState))
dh.update(f"just made summary until {pd.Timedelta(milliseconds=nextTimeStamp)}")
if interval < 0 :
break
# go to the next interval
currentTimeStamp += interval
nextTimeStamp += interval
if duration > 0:
_, nextFileTimeStamp = files_and_timestamp[start_idx]
while nextFileTimeStamp < currentTimeStamp and idx + 1 < len(data):
del data.cache[start_idx]
start_idx += 1
_, nextFileTimeStamp = files_and_timestamp[start_idx]
idx = start_idx
df = data[idx]
# clean files remaining
for x in list(data.cache):
del data.cache[x]
dh.update("start making visualizations")
transform_data_to_image(transforms, rezfilename, rez, interval < 0)
def transform_data_to_image(transforms, rezfilename, rez, total : bool = False):
try:
for name, transform in transforms.items():
one_transform_data_to_image(transform, rezfilename(name), rez, total)
except AttributeError:
one_transform_data_to_image(transforms, rezfilename, rez, total)
def one_transform_data_to_image(transform, rezfilename, rez, total : bool = False) :
# make visualisation
if total :
transformed_rez = transform(rez[-1])
#ssave it as png
imwrite(f"visualisation/{rezfilename}.png" ,transformed_rez)
print(f"results saved on {rezfilename}.png")
else :
transformed_rez = transform(rez)
# save it in a gif
mimsave(f"visualisation/{rezfilename}.gif" ,transformed_rez, duration=3, loop=0)
print(f"results saved on {rezfilename}.gif")