-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocessing.py
139 lines (97 loc) · 3.9 KB
/
preprocessing.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
"""
Preprocessing utility functions for loading and formatting experimental data
"""
from __future__ import absolute_import
import numpy as np
import os
import h5py
from scipy.stats import zscore
from scipy.special import gamma
from utils import rolling_window, notify, Batch
__all__ = ['datagen', 'loadexpt']
# custom data directories for different machines based on os.uname
datadirs = {
'mbp': os.path.expanduser('~/experiments/data/'),
'lenna': os.path.expanduser('~/experiments/data/'),
'lane.local': os.path.expanduser('~/experiments/data/')
}
def loadexpt(cellidx, filename, method, history, fraction=1., mean_adapt=False, roll=True):
"""
Loads an experiment from disk
Parameters
----------
cellidx : int
Index of the cell to load
filename : string
Name of the hdf5 file to load
method : string
The key in the hdf5 file to load ('train' or 'test')
history : int
Number of samples of history to include in the toeplitz stimulus
fraction : float, optional
Fraction of the experiment to load, must be between 0 and 1. (Default: 1.0)
"""
assert fraction > 0 and fraction <= 1, "Fraction of data to load must be between 0 and 1"
# currently only works with the Oct. 07, 15 experiment
expt = '15-10-07'
with notify('Loading {}ing data'.format(method)):
# load the hdf5 file
f = h5py.File(os.path.join(datadirs[os.uname()[1]], expt, filename + '.h5'), 'r')
# length of the experiment
expt_length = f[method]['time'].size
num_samples = int(np.floor(expt_length * fraction))
# load the stimulus
stim = zscore(np.array(f[method]['stimulus'][:num_samples]).astype('float32'))
# photoreceptor model of mean adaptation
if mean_adapt:
stim = pr_filter(10e-3, stim)
# reshaped stimulus (nsamples, time/channel, space, space)
if roll:
stim_reshaped = np.rollaxis(np.rollaxis(rolling_window(stim, history, axis=0), 2), 3, 1)
else:
stim_reshaped = stim
# get the response for this cell
resp = np.array(f[method]['response/firing_rate_10ms'][cellidx, history:num_samples])
return Batch(stim_reshaped, resp)
def datagen(batchsize, X, y):
"""
Returns a generator that yields batches of data for one pass through the data
Parameters
----------
batchsize : int
X : array_like
y : array_like
"""
# number of samples
nsamples = y.shape[0] #changed this from y.size to account for lstm labels
# compute the number of batches per epoch
num_batches = int(np.floor(float(nsamples) / batchsize))
# reshuffle indices
N = num_batches * batchsize
indices = np.random.choice(N, N, replace=False).reshape(num_batches, batchsize)
# for each batch in this epoch
for inds in indices:
# yield data
yield X[inds, ...], y[inds]
def pr_filter(dt, stim, tau_y=0.033, ny=4., tau_z=0.019, nz=10., alpha=1., beta=0.16, eta=0.23):
"""
Filter the given stimulus using a model of photoreceptor adaptation
Dynamical Adaptation in Photoreceptors (Clark et. al., 2015)
http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003289
"""
# build the two filters
t = np.arange(dt, 0.5, dt)
Ky = dt * _make_filter(t, tau_y, ny)
Kz = eta * Ky + (1 - eta) * dt * _make_filter(t, tau_z, nz)
# filter the stimulus
y = np.zeros_like(stim)
z = np.zeros_like(stim)
T = stim.shape[0]
for row in range(stim.shape[1]):
for col in range(stim.shape[2]):
y[:, row, col] = np.convolve(stim[:,row,col], Ky, mode='full')[:T]
z[:, row, col] = np.convolve(stim[:,row,col], Kz, mode='full')[:T]
# return the filtered stimulus
return (alpha * y) / (1 + beta * z)
def _make_filter(t, tau, n):
return ((t ** n) / (gamma(n+1) * tau ** (n + 1))) * np.exp(-t / tau)