Commit 1a26b05a authored by Ines Filipa Fernandes Ramos's avatar Ines Filipa Fernandes Ramos
Browse files

commit

parent 2d498bcb
%% 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:code id: tags:
``` python
#Save notebook session
#dill.dump_session('Natural_images_dataset_session_09122020.db')
```
%% Cell type:code id: tags:
``` python
#Restore notebook session
#dill.load_session('Natural_images_dataset_session_09122020.db')
```
%% 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, cmap='gray')
ax[0].set_title("Image")
ax[1].imshow(rf, vmin=-1, vmax=1, cmap="gray")
ax[1].set_title("RGC RF")
ax[2].imshow(Img_barHat, cmap=cm.Greys_r)
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(64, 36, 50, 30, -1, plot=True)
```
%% Output
%% Cell type:code id: tags:
``` python
#Generate the response of one RGC
RGC_response(rf=rf, image=ds_imgs, plot=True)
```
%% Output
155
%% Cell type:markdown id: tags:
##### RGCs response generation to natural images dataset:
%% Cell type:code id: tags:
``` python
#Retrieve image sets from evaluation data set of lurz2020 #5993 images randomly selected as train, validation or test
from lurz2020.datasets.mouse_loaders import static_loaders
paths = './RGC_sim/static27012021/data/images'
images = []
for n in range(5993):
x = np.load(paths+'/'+str(n)+'.npy')
images.append(x)
images = np.vstack(images)
```
%% Cell type:code id: tags:
``` python
#Generate receptive fields of several RGCs #2304 RGCs - haf ON/half OFF
rf_ON = []
rf_OFF = []
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)
else:
rf = RF(image.shape[1], image.shape[0], width_center, height_center, -1, plot=False)
rf_OFF.append(rf)
i+=1
```
%% Cell type:code id: tags:
``` python
#Generate responses of simulated RGCs to the image set from evaluation data set of lurz2020
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('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/data/responses/'+str(i)+'.npy', responses)
if (i % 1000) == 0:
print(i)
i+=1
print("--- %s seconds ---" % (time.time() - start_time))
```
%% Output
0
1000
2000
3000
4000
5000
--- 6485.6336970329285 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('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/data/pupil_center/'+str(j)+'.npy', pupil_center)
```
%% Cell type:code id: tags:
``` python
#Generate data - behavior npy files -> use include_behavior= False in static_loaders
#Array with list of three values - pupil dilation, temporal derivative and absolute running speed - [99.2678426 , 6.66429682, 0. ]
```
%% 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('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/unit_ids.npy', unit_ids)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/animal_ids.npy', animal_ids)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/area.npy', area)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/layer.npy', layer)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/scan_idx.npy', scan_idx)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/neurons/sessions.npy', sessions)
```
%% Cell type:code id: tags:
``` python
#Generate metadata - trials - animal_id npy array
animal_id = np.repeat(1, len(responses))
#Generate metadata - trials - scan_idx npy array
scan_idx = np.repeat(14, len(responses))
#Generate metadata - trials - session npy array
session = np.repeat(6, len(responses))
# save numpy arrays as npy arrays
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/trials/animal_id.npy', animal_id)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/trials/scan_idx.npy', scan_idx)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/trials/session.npy', session)
```
%% Cell type:code id: tags:
``` python
#Generate metadata - statistics - pupil_center - all
pupil_center_max_all = np.array([0.0,0.0])
pupil_center_mean_all = np.array([0.0,0.0])
pupil_center_median_all = np.array([0.0,0.0])
pupil_center_min_all = np.array([0.0,0.0])
pupil_center_std_all = np.array([0.0,0.0])
#Generate metadata - statistics - pupil_center - stimulus_frame
pupil_center_max_sf = np.array([0.0,0.0])
pupil_center_mean_sf = np.array([0.0,0.0])
pupil_center_median_sf = np.array([0.0,0.0])
pupil_center_min_sf = np.array([0.0,0.0])
pupil_center_std_sf = np.array([0.0,0.0])
# save numpy arrays as npy arrays - all
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/all/max.npy', pupil_center_max_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/all/mean.npy', pupil_center_mean_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/all/median.npy', pupil_center_median_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/all/min.npy', pupil_center_min_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/all/std.npy', pupil_center_std_all)
# save numpy arrays as npy arrays - stimulus_frame
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/stimulus_frame/max.npy', pupil_center_max_sf)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/stimulus_frame/mean.npy', pupil_center_mean_sf)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/stimulus_frame/median.npy', pupil_center_median_sf)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/stimulus_frame/min.npy', pupil_center_min_sf)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/pupil_center/stimulus_frame/std.npy', pupil_center_std_sf)
```
%% Cell type:code id: tags:
``` python
#Generate responses of simulated RGCs to the image set from evaluation data set of lurz2020
import time
start_time = time.time()
responses_all = []
for k in range(len(images)):
responses_all.append(np.load('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/data/responses/'+str(k)+'.npy'))
if (k % 1000) == 0:
print(k)
print("--- %s seconds ---" % (time.time() - start_time))
```
%% Output
0
1000
2000
3000
4000
5000
--- 47.54899740219116 seconds ---
%% 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('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/all/max.npy', responses_max_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/all/mean.npy', responses_mean_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/all/median.npy', responses_median_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/all/min.npy', responses_min_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/all/std.npy', responses_std_all)
# save numpy arrays as npy arrays - stimulus_frame
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/stimulus_frame/max.npy', responses_max_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/stimulus_frame/mean.npy', responses_mean_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/stimulus_frame/median.npy', responses_median_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/stimulus_frame/min.npy', responses_min_all)
save('./Lurz_2020_code/notebooks/data/RGC_sim/static27012021/meta/statistics/responses/stimulus_frame/std.npy', responses_std_all)
```
%% Cell type:markdown id: tags:
----------------------------------------------------------------
%% Cell type:markdown id: tags:
##### RGCs response generation to random checkerboard stimuli:
%% Cell type:code id: tags:
``` python
#Generate random checkerboard stim dataset #5993 images randomly selected as train, validation or test
#Generate random checkerboard stim dataset #10 993 images randomly selected as train, validation or test
from numpy import random
paths = 'D://inception_loop/RGC_sim/data/static20022021/data/images'
checkerboard_set = []
for n in range(5993):
for n in range(10993):
checkerboard_image = [random.choice([0.0, 255.0], size=(36, 64))]
checkerboard_set.append(checkerboard_image)
np.save(paths+'/'+str(n)+'.npy', checkerboard_image)
```
%% Cell type:code id: tags:
``` python
checkerboard_set[0][0].shape
```
%% Output
(36, 64)
%% Cell type:code id: tags:
``` python
#Generate receptive fields of several RGCs #2304 RGCs - haf ON/half OFF to checkerboard set
rf_ON = []
rf_OFF = []
i = 0
image = checkerboard_set[0]
image = checkerboard_set[0][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)
else:
rf = RF(image.shape[1], image.shape[0], width_center, height_center, -1, plot=False)
rf_OFF.append(rf)
i+=1
```
%% Cell type:code id: tags:
``` python
#Generate responses of simulated RGCs to the image set from evaluation data set of lurz2020
import time
start_time = time.time()
i=0
for image in checkerboard_set:
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('D://inception_loop/RGC_sim/data/static20022021/data/responses/'+str(i)+'.npy', responses)
if (i % 1000) == 0:
print(i)
i+=1
print("--- %s seconds ---" % (time.time() - start_time))
```
%% Output
0
1000
2000
3000
4000
5000
--- 6272.005514383316 seconds ---
6000
7000
8000
9000
10000
--- 10964.050934314728 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(checkerboard_set)):
pupil_center = np.array([0.0,0.0])
# save numpy array as npy file
save('C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021/data/pupil_center/'+str(j)+'.npy', pupil_center)
save('D://inception_loop/RGC_sim/data/static20022021/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
path = 'C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021'
path = 'D://inception_loop/RGC_sim/data/static20022021/'
save(path+'/meta/neurons/unit_ids.npy', unit_ids)
save(path+'/meta/neurons/animal_ids.npy', animal_ids)
save(path+'/meta/neurons/area.npy', area)
save(path+'/meta/neurons/layer.npy', layer)
save(path+'/meta/neurons/scan_idx.npy', scan_idx)
save(path+'/meta/neurons/sessions.npy', sessions)
```
%% Cell type:code id: tags:
``` python
#Generate metadata - trials - animal_id npy array
animal_id = np.repeat(1, len(responses))
#Generate metadata - trials - condition_hash npy array
condition_hash = np.repeat(" ", len(responses))
#Generate metadata - trials - frame_image_class npy array
frame_image_class = np.repeat("imagenet", len(responses))
#Generate metadata - trials - frame_image_id npy array
frame_image_id = np.arrange(0, len(responses), 1)
#Generate metadata - trials - frame_last_flip npy array
frame_last_flip = np.random.randint(11000, 30000, size=(len(responses)))
#Generate metadata - trials - frame_pre_blank_period npy array
frame_pre_blank_period = np.random.uniform(0.3, 0.5, size=(len(responses)))
#Generate metadata - trials - frame_presentation_time npy array
frame_presentation_time = np.repeat(0.5, len(responses))
#Generate metadata - trials - frame_trial_ts npy array
frame_trial_ts = np.repeat("Timestamp('2021-02-23 17:53:43')", len(responses))
#Generate metadata - trials - scan_idx npy array
scan_idx = np.repeat(14, len(responses))
#Generate metadata - trials - tiers npy array
tiers = np.repeat(14, len(responses))
#Generate metadata - trials - session npy array
session = np.repeat(6, len(responses))
# save numpy arrays as npy arrays
path = 'C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021'
save(path+'/meta/trials/animal_id.npy', animal_id)
save(path+'/meta/trials/scan_idx.npy', scan_idx)
save(path+'/meta/trials/session.npy', session)
```
%% Cell type:code id: tags:
``` python
#Generate metadata - statistics - pupil_center - all
pupil_center_max_all = np.array([0.0,0.0])
pupil_center_mean_all = np.array([0.0,0.0])
pupil_center_median_all = np.array([0.0,0.0])
pupil_center_min_all = np.array([0.0,0.0])
pupil_center_std_all = np.array([0.0,0.0])
#Generate metadata - statistics - pupil_center - stimulus_frame
pupil_center_max_sf = np.array([0.0,0.0])
pupil_center_mean_sf = np.array([0.0,0.0])
pupil_center_median_sf = np.array([0.0,0.0])
pupil_center_min_sf = np.array([0.0,0.0])
pupil_center_std_sf = np.array([0.0,0.0])
# save numpy arrays as npy arrays - all
path = 'C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021'
save(path+'/meta/statistics/pupil_center/all/max.npy', pupil_center_max_all)
save(path+'/meta/statistics/pupil_center/all/mean.npy', pupil_center_mean_all)
save(path+'/meta/statistics/pupil_center/all/median.npy', pupil_center_median_all)
save(path+'/meta/statistics/pupil_center/all/min.npy', pupil_center_min_all)
save(path+'/meta/statistics/pupil_center/all/std.npy', pupil_center_std_all)
# save numpy arrays as npy arrays - stimulus_frame
save(path+'/meta/statistics/pupil_center/stimulus_frame/max.npy', pupil_center_max_sf)
save(path+'/meta/statistics/pupil_center/stimulus_frame/mean.npy', pupil_center_mean_sf)
save(path+'/meta/statistics/pupil_center/stimulus_frame/median.npy', pupil_center_median_sf)
save(path+'/meta/statistics/pupil_center/stimulus_frame/min.npy', pupil_center_min_sf)
save(path+'/meta/statistics/pupil_center/stimulus_frame/std.npy', pupil_center_std_sf)
```
%% Cell type:code id: tags:
``` python
#Generate metadata - statistics - responses - all
responses_max_all = np.max(responses, axis=0)
responses_mean_all = np.mean(responses, axis=0)
responses_median_all = np.median(responses, axis=0)
responses_min_all = np.min(responses, axis=0)
responses_std_all = np.std(responses, axis=0)
#Generate metadata - statistics - responses - stimulus_frame
# save numpy arrays as npy arrays - all
path = 'C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021'
save(path+'/meta/statistics/responses/all/max.npy', responses_max_all)
save(path+'/meta/statistics/responses/all/mean.npy', responses_mean_all)
save(path+'/meta/statistics/responses/all/median.npy', responses_median_all)
save(path+'/meta/statistics/responses/all/min.npy', responses_min_all)
save(path+'/meta/statistics/responses/all/std.npy', responses_std_all)
# save numpy arrays as npy arrays - stimulus_frame
save(path+'/meta/statistics/responses/stimulus_frame/max.npy', responses_max_all)
save(path+'/meta/statistics/responses/stimulus_frame/mean.npy', responses_mean_all)
save(path+'/meta/statistics/responses/stimulus_frame/median.npy', responses_median_all)
save(path+'/meta/statistics/responses/stimulus_frame/min.npy', responses_min_all)
save(path+'/meta/statistics/responses/stimulus_frame/std.npy', responses_std_all)
```
%% Cell type:code id: tags:
``` python
images = np.vstack(checkerboard_set)
#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
path = 'C://Users/Asus/Desktop/Intership EMBL/Python docs/Inception_loop/Lurz_2020_code/notebooks/data/RGC_sim/static20022021'
save(path+'/meta/statistics/images/all/max.npy', images_max_all)
save(path+'/meta/statistics/images/all/mean.npy', images_mean_all)
save(path+'/meta/statistics/images/all/median.npy', images_median_all)
save(path+'/meta/statistics/images/all/min.npy', images_min_all)
save(path+'/meta/statistics/images/all/std.npy', images_std_all)
# save numpy arrays as npy arrays - stimulus_frame
save(path+'/meta/statistics/images/stimulus_frame/max.npy', images_max_all)
save(path+'/meta/statistics/images/stimulus_frame/mean.npy', images_mean_all)
save(path+'/meta/statistics/images/stimulus_frame/median.npy', images_median_all)
save(path+'/meta/statistics/images/stimulus_frame/min.npy', images_min_all)
save(path+'/meta/statistics/images/stimulus_frame/std.npy', images_std_all)
```
%% Cell type:markdown id: tags:
------------------------------------------
......
%% Cell type:markdown id: tags:
 
# Demo Notebook with simulated RGCs data
 
%% Cell type:code id: tags:
 
``` python
%matplotlib inline
%load_ext autoreload
%autoreload 2
%load_ext memory_profiler
```
 
%% Cell type:code id: tags:
 
``` python
import torch
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict
import neuralpredictors as neur
from neuralpredictors.data.datasets import StaticImageSet, FileTreeDataset
import MEI
import matplotlib as mpl
```
 
%% Output
 
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
 
%% Cell type:markdown id: tags:
 
# Build the dataloaders
 
%% Cell type:markdown id: tags:
 
The dataloaders object is a dictionary of 3 dictionaries: train, validation and test. Each of them contains the respective data from all datasets combined that were specified in paths. Here we only provide one dataset. While the responses are normalized, we exclude the input images from normalization. The following config was used in the paper (all arguments not in the config have the default value of the function).
 
%% Cell type:code id: tags:
 
``` python
#Use dataloaders with generated RGC data
from lurz2020.datasets.mouse_loaders import static_loaders
 
paths = ['D://inception_loop/RGC_sim/data/static27012021']
 
dataset_config = {'paths': paths,
'batch_size': 64,
'seed': 1,
'cuda': True,
'normalize': True,
'exclude': "images"}
 
dataloaders_RGCs = static_loaders(**dataset_config)
dat = FileTreeDataset('D://inception_loop/RGC_sim/data/static27012021', "images", "responses")
```
 
%% Cell type:code id: tags:
``` python
x= np.load("D://inception_loop/RGC_sim/data/static20022021/meta/trials/scan_idx.npy")
x
```
%% Output
array([14, 14, 14, ..., 14, 14, 14])
%% Cell type:markdown id: tags:
 
### Look at the data
 
%% Cell type:code id: tags:
 
``` python
tier = 'train'
dataset_name = '27012021'
 
images, responses = [], []
for x, y in dataloaders_RGCs[tier][dataset_name]:
images.append(x.squeeze().cpu().data.numpy())
responses.append(y.squeeze().cpu().data.numpy())
 
images = np.vstack(images)
responses = np.vstack(responses)
 
print('The \"{}\" set of dataset \"{}\" contains the responses of {} RGC neurons to {} images'.format(tier, dataset_name, responses.shape[1], responses.shape[0]))
```
 
%% Output
 
The "train" set of dataset "27012021" contains the responses of 2304 RGC neurons to 4472 images
 
%% Cell type:code id: tags:
 
``` python
# show some example images and the neural responses
n_images = 5
max_response = responses[:n_images].max()
 
for i in range(n_images):
fig, axs = plt.subplots(1, 2, figsize=(15,4))
axs[0].imshow(images[i])
axs[1].plot(responses[i])
axs[1].set_xlabel('neurons')
axs[1].set_ylabel('responses')
axs[1].set_ylim([0, max_response])
plt.show()
```
 
%% Output
 
 
 
 
 
 
%% Cell type:markdown id: tags:
 
# Build the model, transfer core, train and evaluate performance - 4 instances
 
%% Cell type:markdown id: tags:
 
Get 4 instances of the model for MEI generation:
 
%% Cell type:code id: tags:
 
``` python
%%time
%%memit
from lurz2020.models.models import se2d_fullgaussian2d
from lurz2020.training.trainers import standard_trainer as trainer
from lurz2020.utility.measures import get_correlations, get_fraction_oracles
 
#Generate 4 instances of the same model with different seeds, for MEI generation
n_seeds = 4
 
models = []
train_correlation_models = []
validation_correlation_models = []
test_correlation_models = []
fraction_oracle = []
 
#Model config
model_config = {'init_mu_range': 0.55,
'init_sigma': 0.4,
'input_kern': 15,
'hidden_kern': 13,
'gamma_input': 1.0,
'grid_mean_predictor': None,
'gamma_readout': 2.439}
 
 
#Change trainer config to not track and print the training progress
trainer_config = {'track_training': False,
'detach_core': True}
 
for i in range(n_seeds):
 
model = se2d_fullgaussian2d(**model_config, dataloaders=dataloaders_RGCs, seed=i)
#Load the weights of the transfer core
transfer_model = torch.load('D://inception_loop/original_code/Lurz_2020_code/notebooks/models/transfer_model.pth.tar')
model.load_state_dict(transfer_model, strict=False)
#Run training
score, output, model_state = trainer(model=model, dataloaders=dataloaders_RGCs, seed=1, **trainer_config)
#Get performance of model
train_correlation_models.append(get_correlations(model, dataloaders_RGCs["train"], device='cuda', as_dict=False, per_neuron=False))
validation_correlation_models.append(get_correlations(model, dataloaders_RGCs["validation"], device='cuda', as_dict=False, per_neuron=False))
test_correlation_models.append(get_correlations(model, dataloaders_RGCs["test"], device='cuda', as_dict=False, per_neuron=False))
 
oracle_dataloader = static_loaders(**dataset_config, return_test_sampler=True, tier='test')
fraction_oracle.append(get_fraction_oracles(model=model, dataloaders=oracle_dataloader, device='cuda')[0])
 
print('-----------------------------------------')
print(f'Model instance #{i}')
print('Correlation (train set): {0:.3f}'.format(train_correlation_models[i]))
print('Correlation (validation set): {0:.3f}'.format(validation_correlation_models[i]))
print('Correlation (test set): {0:.3f}'.format(test_correlation_models[i]))
print('-----------------------------------------')
print('Fraction oracle (test set): {0:.3f}'.format(fraction_oracle[i]))
 
models.append(model)
#Save model state for loading later
torch.save(model_state, 'D://inception_loop/RGC_sim/models/model_'+str(i)+'.pth')
```
 
%% Output
 
Epoch 1: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 27.60it/s]
 
[001|00/05] ---> 0.23407305777072906
 
Epoch 2: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 27.78it/s]
 
[002|00/05] ---> 0.29668185114860535
 
Epoch 3: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.23it/s]
 
[003|00/05] ---> 0.3375687599182129
 
Epoch 4: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 25.36it/s]
 
[004|00/05] ---> 0.3720092177391052
 
Epoch 5: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 26.04it/s]
 
[005|00/05] ---> 0.40209096670150757
 
Epoch 6: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.78it/s]
 
[006|00/05] ---> 0.4222107529640198
 
Epoch 7: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.95it/s]
 
[007|00/05] ---> 0.43884357810020447
 
Epoch 8: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.75it/s]
 
[008|00/05] ---> 0.4509202241897583
 
Epoch 9: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.42it/s]
 
[009|00/05] ---> 0.4619576036930084
 
Epoch 10: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.35it/s]
 
[010|00/05] ---> 0.4768645465373993
 
Epoch 11: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.02it/s]
 
[011|00/05] ---> 0.48412057757377625
 
Epoch 12: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.08it/s]
 
[012|00/05] ---> 0.4915325343608856
 
Epoch 13: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.14it/s]
 
[013|00/05] ---> 0.4976453185081482
 
Epoch 14: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.01it/s]
 
[014|00/05] ---> 0.5054040551185608
 
Epoch 15: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.34it/s]
 
[015|00/05] ---> 0.5070977210998535
 
Epoch 16: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.34it/s]
 
[016|00/05] ---> 0.5132291316986084
 
Epoch 17: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.91it/s]
 
[017|00/05] ---> 0.5208368301391602
 
Epoch 18: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.22it/s]
 
[018|00/05] ---> 0.5226337909698486
 
Epoch 19: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.29it/s]
 
[019|00/05] ---> 0.5245917439460754
 
Epoch 20: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.25it/s]
 
[020|00/05] ---> 0.5302238464355469
 
Epoch 21: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.88it/s]
 
[021|00/05] ---> 0.5344535112380981
 
Epoch 22: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.34it/s]
 
[022|01/05] -/-> 0.5327614545822144
 
Epoch 23: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.09it/s]
 
[023|01/05] ---> 0.5406898856163025
 
Epoch 24: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.22it/s]
 
[024|00/05] ---> 0.5439794063568115
 
Epoch 25: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.77it/s]
 
[025|01/05] -/-> 0.5398200154304504
 
Epoch 26: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.90it/s]
 
[026|01/05] ---> 0.5470251441001892
 
Epoch 27: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.19it/s]
 
[027|01/05] -/-> 0.5431471467018127
 
Epoch 28: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.72it/s]
 
[028|02/05] -/-> 0.5463071465492249
 
Epoch 29: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 27.96it/s]
 
[029|02/05] ---> 0.5533322095870972
 
Epoch 30: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.96it/s]
 
[030|01/05] -/-> 0.5518671274185181
 
Epoch 31: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.34it/s]
 
[031|02/05] -/-> 0.5495620965957642
 
Epoch 32: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.18it/s]
 
[032|02/05] ---> 0.5544109344482422
 
Epoch 33: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.23it/s]
 
[033|00/05] ---> 0.5553478598594666
 
Epoch 34: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.54it/s]
 
[034|00/05] ---> 0.5608093738555908
 
Epoch 35: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.18it/s]
 
[035|01/05] -/-> 0.5579965114593506
 
Epoch 36: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.81it/s]
 
[036|02/05] -/-> 0.5578139424324036
 
Epoch 37: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.05it/s]
 
[037|02/05] ---> 0.5613127946853638
 
Epoch 38: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.87it/s]
 
[038|00/05] ---> 0.5638325810432434
 
Epoch 39: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.71it/s]
 
[039|01/05] -/-> 0.5620004534721375
 
Epoch 40: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.88it/s]
 
[040|01/05] ---> 0.564683198928833
 
Epoch 41: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.26it/s]
 
[041|01/05] -/-> 0.5639308094978333
 
Epoch 42: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.24it/s]
 
[042|02/05] -/-> 0.5626509785652161
 
Epoch 43: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.83it/s]
 
[043|02/05] ---> 0.5658485293388367
 
Epoch 44: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 28.91it/s]
 
[044|00/05] ---> 0.5662789344787598
 
Epoch 45: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.06it/s]
 
[045|01/05] -/-> 0.5660897493362427
 
Epoch 46: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 70/70 [00:02<00:00, 29.26it/s]