-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstimupy_test.py
197 lines (163 loc) · 5.13 KB
/
stimupy_test.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
import stimupy
import matplotlib.pyplot as plt
from stimupy.utils import plot_stim
import numpy as np
from numpy.random import default_rng
import h5py
import hdf5plugin
import matplotlib
import matplotlib.animation
from stimupy.components.waves import bessel
from stimupy.stimuli.waves import square_radial
from stimupy.stimuli.cornsweets import cornsweet
from stimupy.components import shapes
import math
# %%
stim_bull = shapes.rectangle(
visual_size=(80, 80), ppd=10, rectangle_size=(4, 2), rectangle_position=(1, 3)
)
fig, ax = plt.subplots(1, 1, figsize=(7, 7))
ax.imshow(np.roll(stim_bull["img"], 1, axis=1), cmap="Greys")
fig.show()
# %%
stacked_patterns = np.zeros((10, 800, 800), dtype=np.uint8)
for frame in range(10):
stacked_patterns[frame, :, :] = np.roll(stim_bull["img"], frame, axis=1)
stacked_patterns = stacked_patterns * 255
# %%
with h5py.File("./stimuli/moving_bar_test.h5", "w") as f:
f.create_dataset(
"Noise",
data=stacked_patterns,
dtype="uint8",
compression=hdf5plugin.Blosc(
cname="blosclz", clevel=9, shuffle=hdf5plugin.Blosc.NOSHUFFLE
),
)
f.create_dataset(name="Frame_Rate", data=1, dtype="uint8")
f.create_dataset(name="Checkerboard_Size", data=200, dtype="uint64")
f.create_dataset(name="Shuffle", data=False, dtype="bool")
# %%
frames = 3000
stacked_patterns = np.zeros((frames, 800, 800), dtype=np.uint8)
radii_array = np.zeros((frames, 5), dtype=float)
for i in range(frames):
# Create 5 random floats between 0 and 15
while True:
rng = default_rng()
radii = np.sort(rng.uniform(0.01, 20, 6))
radii = radii[1:][np.diff(radii) > 0.1]
if len(radii) > 1:
radii_array[i, : len(radii)] = radii
break
radii_array = np.sort(radii_array, axis=1)
# %%
# np.save("radii_array.npy", radii_array)
# %%
stimulus = np.zeros((frames, 400, 400), dtype=float)
for i, rad_frame in enumerate(radii_array):
if np.random.choice([True, False]):
stim_gen = stimupy.bullseyes.circular_generalized(
visual_size=(40, 40),
ppd=10,
radii=rad_frame,
intensity_target=0,
intensity_background=0.5,
intensity_rings=(1, 0),
)
else:
stim_gen = stimupy.bullseyes.circular_generalized(
visual_size=(40, 40),
ppd=10,
radii=rad_frame,
intensity_target=1,
intensity_background=0.5,
intensity_rings=(0, 1),
)
stimulus[i, :, :] = stim_gen["img"]
# %%
np.save("bullseye_raw.npy", stimulus)
# %%
pattern_mean = np.mean(stimulus, axis=0)
fig, ax = plt.subplots(1, 1, figsize=(7, 7))
im = ax.imshow(pattern_mean, cmap="Greys", vmin=0, vmax=1)
plt.colorbar(im)
fig.show()
# %%
stimulus = stimulus * 255
# %%
cX = 200
cY = 400
stacked_patterns = np.zeros((frames, 800, 800), dtype=np.uint8)
for i in range(frames):
stacked_patterns[i, cX - 200 : cX + 200, cY - 200 : cY + 200] = stimulus[i]
# %%
def kernel_mov(ker):
n_frames, h, w = ker.shape
fig, ax = plt.subplots(figsize=(7, 7))
im = ax.imshow(ker[0], cmap="Greys_r")
def update(frame):
im.set_array(ker[frame])
return [im]
animation = matplotlib.animation.FuncAnimation(
fig, update, frames=range(n_frames), blit=True
)
return animation
# %%
ani = kernel_mov(stacked_patterns)
ani.save("moving_bar_test.mp4", writer="ffmpeg", fps=10)
# %%
with h5py.File("./stimuli/ring_noise.h5", "w") as f:
f.create_dataset(
"Noise",
data=stacked_patterns,
dtype="uint8",
compression=hdf5plugin.Blosc(
cname="blosclz", clevel=9, shuffle=hdf5plugin.Blosc.NOSHUFFLE
),
)
f.create_dataset(name="Frame_Rate", data=10, dtype="uint8")
f.create_dataset(name="Checkerboard_Size", data=200, dtype="uint64")
f.create_dataset(name="Shuffle", data=False, dtype="bool")
# %%
frames = 3000
stacked_patterns = np.zeros((frames, 600, 600), dtype=float)
# %%
for i in range(frames):
# Create a random frequency float
radius = np.random.uniform(0.01, 2)
disc = shapes.disc(
visual_size=(5, 5),
ppd=10,
radius=radius,
intensity_disc=1,
intensity_background=0,
)
nr = int(600 * 600 / (radius * 10 * math.pi) * 0.5 / 10)
# if np.random.choice([True, False]):
# stim_gen["img"] = np.logical_not(stim_gen["img"]).astype(np.uint8)
for pos in range(nr):
pos_x = np.random.randint(50, 550)
pos_y = np.random.randint(50, 550)
stacked_patterns[
i,
pos_x - 25 : pos_x + 25,
pos_y - 25 : pos_y + 25,
] = np.logical_or(
stacked_patterns[
i,
pos_x - 25 : pos_x + 25,
pos_y - 25 : pos_y + 25,
],
disc["img"],
)
stacked_patterns = (stacked_patterns * 255).astype(np.uint8)
# %%
ani = kernel_mov(stacked_patterns)
ani.save("ring_noise.mp4", writer="ffmpeg", fps=1)
# %%
pattern_mean = np.mean(stacked_patterns, axis=0)
fig, ax = plt.subplots(1, 1, figsize=(7, 7))
im = ax.imshow(pattern_mean, cmap="Greys")
plt.colorbar(im)
fig.show()