Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ines Filipa Fernandes Ramos
inception_loop_asari_lab
Commits
b79030c4
Commit
b79030c4
authored
Mar 26, 2021
by
Ines Filipa Fernandes Ramos
Browse files
changes to train with big dataset
parent
c5b21465
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
RGC_simulations/RGC_sim_big_dataset.ipynb
View file @
b79030c4
%% Cell type:markdown id: tags:
# Simple RGCs simulation
%% Cell type:code id: tags:
```
python
import
numpy
as
np
,
array
import
matplotlib.pyplot
as
plt
import
matplotlib.gridspec
as
gridspec
from
scipy
import
signal
import
matplotlib.image
as
mpimg
import
matplotlib.cm
as
cm
import
math
from
mpl_toolkits.mplot3d
import
axes3d
import
torch
from
collections
import
OrderedDict
import
neuralpredictors
as
neur
from
neuralpredictors.data.datasets
import
StaticImageSet
,
FileTreeDataset
from
numpy
import
save
```
%% Cell type:markdown id: tags:
##### Simple RGCs simulation:
%% Cell type:code id: tags:
```
python
_default_2Dgaussian_p
=
(
1
,
1
,
1
,
0
,
0
,
0
,
0
)
def
gaussian_2D
(
xz
,
sigma_x
,
sigma_z
,
amp
,
theta
,
x0
,
z0
,
y0
):
"""Two dimensional Gaussian function
params:
- xz: meshgrid of x and z coordinates at which to evaluate the points
- sigma_x: width of the gaussian
- sigma_z: height of the gaussian
- amp: amplitude of the gaussian
- theta: angle of the gaussian (in radian)
- x0: shift in x of the gaussian
- z0: shift in z of the gaussian
- y0: shift in y of the gaussian
"""
(
x
,
z
)
=
xz
x0
,
z0
=
float
(
x0
),
float
(
z0
)
a
=
(
np
.
cos
(
theta
)
**
2
)
/
(
2
*
sigma_x
**
2
)
+
(
np
.
sin
(
theta
)
**
2
)
/
(
2
*
sigma_z
**
2
)
b
=
-
(
np
.
sin
(
2
*
theta
))
/
(
4
*
sigma_x
**
2
)
+
(
np
.
sin
(
2
*
theta
))
/
(
4
*
sigma_z
**
2
)
c
=
(
np
.
sin
(
theta
)
**
2
)
/
(
2
*
sigma_x
**
2
)
+
(
np
.
cos
(
theta
)
**
2
)
/
(
2
*
sigma_z
**
2
)
g
=
amp
*
np
.
exp
(
-
(
a
*
((
x
-
x0
)
**
2
)
+
2
*
b
*
(
x
-
x0
)
*
(
z
-
z0
)
+
c
*
((
z
-
z0
)
**
2
)))
+
y0
return
g
.
ravel
()
def
mexicanHat
(
xz
,
sigma_x_1
,
sigma_z_1
,
amp_1
,
theta_1
,
x0_1
,
z0_1
,
sigma_x_2
,
sigma_z_2
,
amp_2
,
theta_2
,
x0_2
,
z0_2
,
y0
):
"""Sum of two 2D Gaussian function. For the params, see `gaussian_2D`.
However, both share the y0 parameter."""
return
(
gaussian_2D
(
xz
,
sigma_x_1
,
sigma_z_1
,
amp_1
,
theta_1
,
x0_1
,
z0_1
,
0
)
+
gaussian_2D
(
xz
,
sigma_x_2
,
sigma_z_2
,
amp_2
,
theta_2
,
x0_2
,
z0_2
,
0
)
+
y0
)
def
ELU
(
r
):
if
r
>
0
:
return
r
+
1
else
:
return
np
.
exp
(
r
)
+
1
def
RF
(
vis_field_width
,
vis_field_height
,
x_rf_center
,
z_rf_center
,
polarity
,
plot
=
False
):
x
,
y
=
np
.
meshgrid
(
np
.
linspace
(
0
,
vis_field_width
,
vis_field_width
),
np
.
linspace
(
0
,
vis_field_height
,
vis_field_height
))
if
polarity
==
1
:
sigma_x_1
,
sigma_z_1
,
amp_1
,
theta_1
,
x0_1
,
z0_1
=
2
,
2
,
1
,
0
,
x_rf_center
,
z_rf_center
sigma_x_2
,
sigma_z_2
,
amp_2
,
theta_2
,
x0_2
,
z0_2
,
y0
=
3
,
3
,
-
0.5
,
0
,
x_rf_center
,
z_rf_center
,
0
else
:
sigma_x_1
,
sigma_z_1
,
amp_1
,
theta_1
,
x0_1
,
z0_1
=
2
,
2
,
-
1
,
0
,
x_rf_center
,
z_rf_center
sigma_x_2
,
sigma_z_2
,
amp_2
,
theta_2
,
x0_2
,
z0_2
,
y0
=
3
,
3
,
0.5
,
0
,
x_rf_center
,
z_rf_center
,
0
z
=
mexicanHat
((
x
,
y
),
sigma_x_1
,
sigma_z_1
,
amp_1
,
theta_1
,
x0_1
,
z0_1
,
sigma_x_2
,
sigma_z_2
,
amp_2
,
theta_2
,
x0_2
,
z0_2
,
y0
).
reshape
(
vis_field_height
,
vis_field_width
)
if
plot
==
True
:
fig
=
plt
.
figure
(
figsize
=
(
5
,
4
))
ax
=
fig
.
add_subplot
(
111
,
projection
=
'3d'
)
ax
.
plot_wireframe
(
x
,
y
,
z
,
rstride
=
3
,
cstride
=
3
,
label
=
f
"x_rf_center=
{
x_rf_center
}
z_rf_center=
{
z_rf_center
}
\n
amp_center=
{
amp_1
}
amp_surround=
{
amp_2
}
"
)
_
=
ax
.
legend
()
return
z
def
RGC_response
(
rf
,
image
,
plot
=
False
,
seed
=
None
):
Img_barHat
=
image
*
rf
if
plot
==
True
:
fig
,
ax
=
plt
.
subplots
(
3
,
figsize
=
(
7
,
7
))
ax
[
0
].
imshow
(
image
)
ax
[
0
].
set_title
(
"Image"
)
ax
[
1
].
imshow
(
rf
)
ax
[
1
].
set_title
(
"RGC RF"
)
ax
[
2
].
imshow
(
Img_barHat
)
ax
[
2
].
set_title
(
"RGC Response"
)
plt
.
tight_layout
()
if
seed
is
not
None
:
np
.
random
.
seed
(
seed
)
g
=
ELU
(
sum
(
Img_barHat
.
ravel
()))
spikes
=
np
.
random
.
poisson
(
lam
=
g
,
size
=
None
)
return
spikes
```
%% Cell type:code id: tags:
```
python
#Generate the receptive field of one RGC
rf
=
RF
(
vis_field_width
=
64
,
vis_field_height
=
36
,
x_rf_center
=
30
,
z_rf_center
=
30
,
polarity
=
1
,
plot
=
True
)
```
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
#Generate the response of one RGC
x
=
np
.
load
(
'D://inception_loop/RGC_sim_data/data/static23032021_vanhateren_images/data/images/'
+
str
(
10
)
+
'.npy'
)[
0
]
RGC_response
(
rf
=
rf
,
image
=
x
,
plot
=
True
)
```
%%%% Output: execute_result
0
%%%% Output: display_data

%% Cell type:markdown id: tags:
# Generate simulated RGCs responses to big dataset (lurz + vanhateren)
%% Cell type:markdown id: tags:
##### RGCs response generation to lurz + vanhateren dataset:
%% Cell type:code id: tags:
```
python
#Retrieve image sets from evaluation data set of lurz2020 #5993 + vanhateren image set #2624 added as training data
#Total of
paths
=
'D://inception_loop/RGC_sim_data/data/static23032021_vanhateren_images'
images
=
[]
for
n
in
range
(
8617
):
x
=
np
.
load
(
paths
+
'/data/images/'
+
str
(
n
)
+
'.npy'
)
#
x_padded = np.pad(x[0], pad_width=20, mode='constant',
#
constant_values=0)
#
np.save(paths+'/data/images/'+str(n)+'.npy', [x_padded])
images
.
append
(
x
)
x_padded
=
np
.
pad
(
x
[
0
],
pad_width
=
20
,
mode
=
'constant'
,
constant_values
=
0
)
np
.
save
(
paths
+
'/data/images/'
+
str
(
n
)
+
'.npy'
,
[
x_padded
])
#
images.append(x)
images
=
np
.
vstack
(
images
)
#
images = np.vstack(images)
```
%% Cell type:code id: tags:
```
python
#Generate receptive fields of several RGCs #2304 RGCs - haf ON/half OFFù
import
random
rf_ON
=
[]
rf_ON_center_coord
=
[]
rf_OFF
=
[]
rf_OFF_center_coord
=
[]
i
=
0
image
=
images
[
0
]
for
width_center
in
range
(
image
.
shape
[
1
]):
for
height_center
in
range
(
image
.
shape
[
0
]):
if
(
i
%
2
)
==
0
:
rf
=
RF
(
image
.
shape
[
1
],
image
.
shape
[
0
],
width_center
,
height_center
,
1
,
plot
=
False
)
rf_ON
.
append
(
rf
)
save
(
paths
+
'/RFs/'
+
str
(
i
)
+
'.npy'
,
rf
)
else
:
rf
=
RF
(
image
.
shape
[
1
],
image
.
shape
[
0
],
width_center
,
height_center
,
-
1
,
plot
=
False
)
rf_OFF
.
append
(
rf
)
save
(
paths
+
'/RFs/'
+
str
(
i
)
+
'.npy'
,
rf
)
i
+=
1
```
%% Cell type:code id: tags:
```
python
#Generate responses of simulated RGCs to the image set
import
time
start_time
=
time
.
time
()
i
=
0
for
image
in
images
:
responses
=
[]
for
rfon
,
rfoff
in
zip
(
rf_ON
,
rf_OFF
):
rgc_on_response
=
RGC_response
(
rf
=
rfon
,
image
=
image
,
plot
=
False
)
responses
.
append
(
rgc_on_response
)
rgc_off_response
=
RGC_response
(
rf
=
rfoff
,
image
=
image
,
plot
=
False
)
responses
.
append
(
rgc_off_response
)
# save numpy array as npy file
save
(
paths
+
'/data/responses/'
+
str
(
i
)
+
'.npy'
,
responses
)
if
(
i
%
1000
)
==
0
:
print
(
i
)
i
+=
1
print
(
"--- %s seconds ---"
%
(
time
.
time
()
-
start_time
))
```
%%%% Output: stream
0
1000
2000
3000
4000
5000
6000
7000
8000
--- 7405.442238330841 seconds ---
%% Cell type:code id: tags:
```
python
#Retrieve responses of simulated RGCs to the image set
import
time
start_time
=
time
.
time
()
responses_all
=
[]
for
k
in
range
(
len
(
images
)):
responses_all
.
append
(
np
.
load
(
paths
+
'/data/responses/'
+
str
(
k
)
+
'.npy'
))
if
(
k
%
1000
)
==
0
:
print
(
k
)
print
(
"--- %s seconds ---"
%
(
time
.
time
()
-
start_time
))
```
%%%% Output: stream
0
1000
2000
3000
4000
5000
6000
7000
8000
--- 9.932857990264893 seconds ---
%% Cell type:code id: tags:
```
python
#Generate data - pupil_center npy files
#Array with list of two values- coordinates of pupil center - [759.87785056, 472.71767702]
for
j
in
range
(
len
(
images
)):
pupil_center
=
np
.
array
([
0.0
,
0.0
])
# save numpy array as npy file
save
(
paths
+
'/data/pupil_center/'
+
str
(
j
)
+
'.npy'
,
pupil_center
)
```
%% Cell type:code id: tags:
```
python
#Generate metadata - neurons - unit_ids npy array
unit_ids
=
np
.
array
(
range
(
1
,
len
(
responses
)
+
1
))
#Generate metadata - neurons - animal_ids npy array
animal_ids
=
np
.
repeat
(
1
,
len
(
responses
))
#Generate metadata - neurons - area npy array
area
=
[
'retina'
]
*
len
(
responses
)
#Generate metadata - neurons - layer npy array
layer
=
[
'RGC'
]
*
len
(
responses
)
#Generate metadata - neurons - scan_idx npy array
scan_idx
=
np
.
repeat
(
14
,
len
(
responses
))
#Generate metadata - neurons - sessions npy array
sessions
=
np
.
repeat
(
6
,
len
(
responses
))
# save numpy arrays as npy arrays
save
(
paths
+
'/meta/neurons/unit_ids.npy'
,
unit_ids
)
save
(
paths
+
'/meta/neurons/animal_ids.npy'
,
animal_ids
)
save
(
paths
+
'/meta/neurons/area.npy'
,
area
)
save
(
paths
+
'/meta/neurons/layer.npy'
,
layer
)
save
(
paths
+
'/meta/neurons/scan_idx.npy'
,
scan_idx
)
save
(
paths
+
'/meta/neurons/sessions.npy'
,
sessions
)
```
%% Cell type:code id: tags:
```
python
#Generate metadata - statistics - responses - all
responses_max_all
=
np
.
max
(
responses_all
,
axis
=
0
)
responses_mean_all
=
np
.
mean
(
responses_all
,
axis
=
0
)
responses_median_all
=
np
.
median
(
responses_all
,
axis
=
0
)
responses_min_all
=
np
.
min
(
responses_all
,
axis
=
0
)
responses_std_all
=
np
.
std
(
responses_all
,
axis
=
0
)
#Generate metadata - statistics - responses - stimulus_frame
# save numpy arrays as npy arrays - all
save
(
paths
+
'/meta/statistics/responses/all/max.npy'
,
responses_max_all
)
save
(
paths
+
'/meta/statistics/responses/all/mean.npy'
,
responses_mean_all
)
save
(
paths
+
'/meta/statistics/responses/all/median.npy'
,
responses_median_all
)
save
(
paths
+
'/meta/statistics/responses/all/min.npy'
,
responses_min_all
)
save
(
paths
+
'/meta/statistics/responses/all/std.npy'
,
responses_std_all
)
# save numpy arrays as npy arrays - stimulus_frame
save
(
paths
+
'/meta/statistics/responses/stimulus_frame/max.npy'
,
responses_max_all
)
save
(
paths
+
'/meta/statistics/responses/stimulus_frame/mean.npy'
,
responses_mean_all
)
save
(
paths
+
'/meta/statistics/responses/stimulus_frame/median.npy'
,
responses_median_all
)
save
(
paths
+
'/meta/statistics/responses/stimulus_frame/min.npy'
,
responses_min_all
)
save
(
paths
+
'/meta/statistics/responses/stimulus_frame/std.npy'
,
responses_std_all
)
```
%% Cell type:code id: tags:
```
python
#Generate metadata - statistics - responses - all
images_max_all
=
np
.
max
(
images
)
images_mean_all
=
np
.
mean
(
images
)
images_median_all
=
np
.
median
(
images
)
images_min_all
=
np
.
min
(
images
)
images_std_all
=
np
.
std
(
images
)
#Generate metadata - statistics - responses - stimulus_frame
# save numpy arrays as npy arrays - all
save
(
paths
+
'/meta/statistics/images/all/mean.npy'
,
images_mean_all
)
save
(
paths
+
'/meta/statistics/images/all/median.npy'
,
images_median_all
)
save
(
paths
+
'/meta/statistics/images/all/min.npy'
,
images_min_all
)
save
(
paths
+
'/meta/statistics/images/all/std.npy'
,
images_std_all
)
# save numpy arrays as npy arrays - stimulus_frame
save
(
paths
+
'/meta/statistics/images/stimulus_frame/max.npy'
,
images_max_all
)
save
(
paths
+
'/meta/statistics/images/stimulus_frame/mean.npy'
,
images_mean_all
)
save
(
paths
+
'/meta/statistics/images/stimulus_frame/median.npy'
,
images_median_all
)
save
(
paths
+
'/meta/statistics/images/stimulus_frame/min.npy'
,
images_min_all
)
save
(
paths
+
'/meta/statistics/images/stimulus_frame/std.npy'
,
images_std_all
)
```
%% Cell type:code id: tags:
```
python
#Generate metadata - trials - animal_id npy array
animal_id
=
np
.
repeat
(
1
,
len
(
responses_all
))
#Generate metadata - trials - condition_hash npy array
condition_hash
=
np
.
repeat
(
" "
,
len
(
responses_all
))
#Generate metadata - trials - frame_image_class npy array
frame_image_class
=
np
.
repeat
(
"imagenet"
,
len
(
responses_all
))
#Generate metadata - trials - frame_image_id npy array
frame_image_id
=
np
.
load
(
paths
+
'/meta/trials/frame_image_id.npy'
)
frame_image_id_new
=
np
.
concatenate
((
frame_image_id
,
np
.
arange
(
max
(
frame_image_id
)
+
1
,
max
(
frame_image_id
)
+
1
+
len
(
responses_all
)
-
len
(
frame_image_id
),
1
)))
#Generate metadata - trials - frame_last_flip npy array
frame_last_flip
=
np
.
random
.
randint
(
11000
,
30000
,
size
=
(
len
(
responses_all
)))
#Generate metadata - trials - frame_pre_blank_period npy array
frame_pre_blank_period
=
np
.
random
.
uniform
(
0.3
,
0.5
,
size
=
(
len
(
responses_all
)))
#Generate metadata - trials - frame_presentation_time npy array
frame_presentation_time
=
np
.
repeat
(
0.5
,
len
(
responses_all
))
#Generate metadata - trials - frame_trial_ts npy array
frame_trial_ts
=
np
.
repeat
(
"Timestamp('2021-03-26 13:30:43')"
,
len
(
responses_all
))
#Generate metadata - trials - scan_idx npy array
scan_idx
=
np
.
repeat
(
14
,
len
(
responses_all
))
#Generate metadata - trials - tiers npy array
tiers
=
np
.
load
(
paths
+
'/meta/trials/tiers.npy'
)
tiers_new
=
np
.
concatenate
((
tiers
,
np
.
repeat
(
"train"
,
len
(
responses_all
)
-
len
(
tiers
))))
#Generate metadata - trials - session npy array
session
=
np
.
repeat
(
6
,
len
(
responses_all
))
#Generate metadata - trials - trial_idx npy array
trial_idx
=
np
.
repeat
(
0
,
len
(
responses
))
# save numpy arrays as npy arrays
save
(
paths
+
'/meta/trials/animal_id.npy'
,
animal_id
)
save
(
paths
+
'/meta/trials/condition_hash.npy'
,
condition_hash
)
save
(
paths
+
'/meta/trials/frame_image_class.npy'
,
frame_image_class
)
save
(
paths
+
'/meta/trials/frame_image_id.npy'
,
frame_image_id_new
)
save
(
paths
+
'/meta/trials/frame_last_flip.npy'
,
frame_last_flip
)
save
(
paths
+
'/meta/trials/frame_pre_blank_period.npy'
,
frame_pre_blank_period
)
save
(
paths
+
'/meta/trials/frame_presentation_time.npy'
,
frame_presentation_time
)
save
(
paths
+
'/meta/trials/frame_trial_ts.npy'
,
frame_trial_ts
)
save
(
paths
+
'/meta/trials/scan_idx.npy'
,
scan_idx
)
save
(
paths
+
'/meta/trials/tiers.npy'
,
tiers_new
)
save
(
paths
+
'/meta/trials/session.npy'
,
session
)
save
(
paths
+
'/meta/trials/trial_idx.npy'
,
trial_idx
)
```
%% Cell type:code id: tags:
```
python
frame_image_id
=
np
.
load
(
paths
+
'/meta/trials/frame_image_id.npy'
)
frame_image_id_new
=
np
.
concatenate
((
frame_image_id
,
np
.
arange
(
max
(
frame_image_id
)
+
1
,
max
(
frame_image_id
)
+
1
+
len
(
responses_all
)
-
len
(
frame_image_id
),
1
)))
```
%% Cell type:code id: tags:
```
python
tiers
=
np
.
load
(
paths
+
'/meta/trials/tiers.npy'
)
tiers_new
=
np
.
concatenate
((
tiers
,
np
.
repeat
(
"train"
,
len
(
responses_all
)
-
len
(
tiers
))))
```
%% Cell type:code id: tags:
```
python
len
(
tiers_new
)
```
%%%% Output: execute_result
8617
%% Cell type:code id: tags:
```
python
```
...
...
RGC_simulations/RGC_sim_small_dataset.ipynb
View file @
b79030c4
This diff is collapsed.
Click to expand it.
RGC_simulations/example_notebook_RGC_sim.ipynb
View file @
b79030c4
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment