-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkerflickerplusanalyzer.py
333 lines (272 loc) · 12.2 KB
/
checkerflickerplusanalyzer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 8 11:22:39 2017
@author: ycan
"""
import datetime
import os
import sys
import warnings
import numpy as np
from randpy import randpy
import analysis_scripts as asc
import iofuncs as iof
import miscfuncs as msc
import matplotlib.pyplot as plt
import plotfuncs as plf
from pathlib import PureWindowsPath
def runfreezemask(total_frames, runfr, frofr, refresh_rate):
mask = np.array([], dtype='bool')
nr_required = np.ceil(total_frames
/ refresh_rate
/ ((runfr+frofr)/refresh_rate))
nr_required = np.int(nr_required)
for i in range(nr_required):
for bo, j in zip([True, False], [runfr, frofr]):
mask = np.append(mask, [bo]*j)
mask = mask[:total_frames]
return mask
def checkerflickerplusanalyzer(exp_name, stimulusnr, clusterstoanalyze=None,
frametimingsfraction=None, cutoff=4):
"""
Analyzes checkerflicker-like data, typically interspersed
stimuli in between chunks of checkerflicker.
e.g. checkerflickerplusmovie, frozennoise
Parameters:
----------
exp_name:
Experiment name.
stimulusnr:
Number of the stimulus to be analyzed.
clusterstoanalyze:
Number of clusters should be analyzed. Default is None.
First N cells will be analyzed if this parameter is given.
In case of long recordings it might make sense to first
look at a subset of cells before starting to analyze
the whole dataset.
frametimingsfraction:
Fraction of the recording to analyze. Should be a number
between 0 and 1. e.g. 0.3 will analyze the first 30% of
the whole recording.
cutoff:
Worst rating that is wanted for the analysis. Default
is 4. The source of this value is manual rating of each
cluster.
"""
exp_dir = iof.exp_dir_fixer(exp_name)
stimname = iof.getstimname(exp_dir, stimulusnr)
exp_name = os.path.split(exp_dir)[-1]
clusters, metadata = asc.read_spikesheet(exp_dir, cutoff=cutoff)
# Check that the inputs are as expected.
if clusterstoanalyze:
if clusterstoanalyze > len(clusters[:, 0]):
warnings.warn('clusterstoanalyze is larger '
'than number of clusters in dataset. '
'All cells will be included.')
clusterstoanalyze = None
if frametimingsfraction:
if not 0 < frametimingsfraction < 1:
raise ValueError('Invalid input for frametimingsfraction: {}. '
'It should be a number between 0 and 1'
''.format(frametimingsfraction))
scr_width = metadata['screen_width']
scr_height = metadata['screen_height']
refresh_rate = metadata['refresh_rate']
parameters = asc.read_parameters(exp_dir, stimulusnr)
stx_h = parameters['stixelheight']
stx_w = parameters['stixelwidth']
# Check whether any parameters are given for margins, calculate
# screen dimensions.
marginkeys = ['tmargin', 'bmargin', 'rmargin', 'lmargin']
margins = []
for key in marginkeys:
margins.append(parameters.get(key, 0))
# Subtract bottom and top from vertical dimension; left and right
# from horizontal dimension
scr_width = scr_width-sum(margins[2:])
scr_height = scr_height-sum(margins[:2])
nblinks = parameters['Nblinks']
bw = parameters.get('blackwhite', False)
# Gaussian stimuli are not supported yet, we need to ensure we
# have a black and white stimulus
if bw is not True:
raise ValueError('Gaussian stimuli are not supported yet!')
seed = parameters.get('seed', -1000)
sx, sy = scr_height/stx_h, scr_width/stx_w
# Make sure that the number of stimulus pixels are integers
# Rounding down is also possible but might require
# other considerations.
if sx % 1 == 0 and sy % 1 == 0:
sx, sy = int(sx), int(sy)
else:
raise ValueError('sx and sy must be integers')
filter_length, frametimings = asc.ft_nblinks(exp_dir, stimulusnr)
if parameters['stimulus_type'] in ['FrozenNoise',
'checkerflickerplusmovie']:
runfr = parameters['RunningFrames']
frofr = parameters['FrozenFrames']
# To generate the frozen noise, a second seed is used.
# The default value of this is -10000 as per StimulateOpenGL
secondseed = parameters.get('secondseed', -10000)
if parameters['stimulus_type'] == 'checkerflickerplusmovie':
mblinks = parameters['Nblinksmovie']
# Retrivee the number of frames (files) from parameters['path']
ipath = PureWindowsPath(parameters['path']).as_posix()
repldict = iof.config('stimuli_path_replace')
for needle, repl in repldict.items():
ipath = ipath.replace(needle, repl)
ipath = os.path.normpath(ipath) # Windows compatiblity
moviefr = len([name for name in os.listdir(ipath)
if os.path.isfile(os.path.join(ipath, name))
and name.lower().endswith('.raw')])
noiselen = (runfr+frofr) * nblinks
movielen = moviefr * mblinks
triallen = noiselen + movielen
ft_on, ft_off = asc.readframetimes(exp_dir, stimulusnr,
returnoffsets=True)
frametimings = np.empty(ft_on.shape[0]*2, dtype=float)
frametimings[::2] = ft_on
frametimings[1::2] = ft_off
import math
ntrials = math.floor(frametimings.size/triallen)
trials = np.zeros((ntrials, runfr + frofr + moviefr))
for t in range(ntrials):
frange = frametimings[t*triallen:(t+1)*triallen]
trials[t, :runfr+frofr] = frange[:noiselen][::nblinks]
trials[t, runfr+frofr:] = frange[noiselen:][::mblinks]
frametimings = trials.ravel()
filter_length = np.int(np.round(.666*refresh_rate/nblinks))
# Add frozen movie to frozen noise (for masking)
frofr += moviefr
savefname = str(stimulusnr)+'_data'
if clusterstoanalyze:
clusters = clusters[:clusterstoanalyze, :]
print('Analyzing first %s cells' % clusterstoanalyze)
savefname += '_'+str(clusterstoanalyze)+'cells'
if frametimingsfraction:
frametimingsindex = int(len(frametimings)*frametimingsfraction)
frametimings = frametimings[:frametimingsindex]
print('Analyzing first {}% of'
' the recording'.format(frametimingsfraction*100))
savefname += '_'+str(frametimingsfraction).replace('.', '')+'fraction'
frame_duration = np.average(np.ediff1d(frametimings))
total_frames = frametimings.shape[0]
all_spiketimes = []
# Store spike triggered averages in a list containing correct shaped
# arrays
stas = []
for i in range(len(clusters[:, 0])):
spiketimes = asc.read_raster(exp_dir, stimulusnr,
clusters[i, 0], clusters[i, 1])
spikes = asc.binspikes(spiketimes, frametimings)
all_spiketimes.append(spikes)
stas.append(np.zeros((sx, sy, filter_length)))
# Separate out the repeated parts
all_spiketimes = np.array(all_spiketimes)
mask = runfreezemask(total_frames, runfr, frofr, refresh_rate)
repeated_spiketimes = all_spiketimes[:, ~mask]
run_spiketimes = all_spiketimes[:, mask]
# We need to cut down the total_frames by the same amount
# as spiketimes
total_run_frames = run_spiketimes.shape[1]
# To be able to use the same code as checkerflicker analyzer,
# convert to list again.
run_spiketimes = list(run_spiketimes)
# Empirically determined to be best for 32GB RAM
desired_chunk_size = 21600000
# Length of the chunks (specified in number of frames)
chunklength = int(desired_chunk_size/(sx*sy))
chunksize = chunklength*sx*sy
nrofchunks = int(np.ceil(total_run_frames/chunklength))
print(f'\nAnalyzing {stimname}.\nTotal chunks: {nrofchunks}')
time = startime = datetime.datetime.now()
timedeltas = []
quals = np.zeros(len(stas))
frame_counter = 0
for i in range(nrofchunks):
randnrs, seed = randpy.ranb(seed, chunksize)
# Reshape and change 0's to -1's
stimulus = np.reshape(randnrs, (sx, sy, chunklength), order='F')*2-1
del randnrs
# Range of indices we are interested in for the current chunk
if (i+1)*chunklength < total_run_frames:
chunkind = slice(i*chunklength, (i+1)*chunklength)
chunkend = chunklength
else:
chunkind = slice(i*chunklength, None)
chunkend = total_run_frames - i*chunklength
for k in range(filter_length, chunkend-filter_length+1):
stim_small = stimulus[:, :, k-filter_length+1:k+1][:, :, ::-1]
for j in range(clusters.shape[0]):
spikes = run_spiketimes[j][chunkind]
if spikes[k] != 0:
stas[j] += spikes[k]*stim_small
qual = np.array([])
for c in range(clusters.shape[0]):
qual = np.append(qual, asc.staquality(stas[c]))
quals = np.vstack((quals, qual))
# Draw progress bar
width = 50 # Number of characters
prog = i/(nrofchunks-1)
bar_complete = int(prog*width)
bar_noncomplete = width-bar_complete
timedeltas.append(msc.timediff(time)) # Calculate running avg
avgelapsed = np.mean(timedeltas)
elapsed = np.sum(timedeltas)
etc = startime + elapsed + avgelapsed*(nrofchunks-i)
sys.stdout.flush()
sys.stdout.write('\r{}{} |{:4.1f}% ETC: {}'.format('█'*bar_complete,
'-'*bar_noncomplete,
prog*100, etc.strftime("%a %X")))
time = datetime.datetime.now()
sys.stdout.write('\n')
# Remove the first row which is full of random nrs.
quals = quals[1:, :]
max_inds = []
spikenrs = np.array([spikearr.sum() for spikearr in run_spiketimes])
for i in range(clusters.shape[0]):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '.*true_divide*.')
stas[i] = stas[i]/spikenrs[i]
# Find the pixel with largest absolute value
max_i = np.squeeze(np.where(np.abs(stas[i])
== np.max(np.abs(stas[i]))))
# If there are multiple pixels with largest value,
# take the first one.
if max_i.shape != (3,):
try:
max_i = max_i[:, 0]
# If max_i cannot be found just set it to zeros.
except IndexError:
max_i = np.array([0, 0, 0])
max_inds.append(max_i)
print(f'Completed. Total elapsed time: {msc.timediff(startime)}\n'+
f'Finished on {datetime.datetime.now().strftime("%A %X")}')
savepath = os.path.join(exp_dir, 'data_analysis', stimname)
if not os.path.isdir(savepath):
os.makedirs(savepath, exist_ok=True)
savepath = os.path.join(savepath, savefname)
keystosave = ['clusters', 'frametimings', 'mask',
'repeated_spiketimes', 'run_spiketimes',
'frame_duration', 'max_inds', 'nblinks', 'stas',
'stx_h', 'stx_w', 'total_run_frames', 'sx', 'sy',
'filter_length', 'stimname', 'exp_name', 'spikenrs',
'clusterstoanalyze', 'frametimingsfraction', 'cutoff',
'quals', 'nrofchunks', 'chunklength']
datadict = {}
for key in keystosave:
datadict[key] = locals()[key]
np.savez(savepath, **datadict)
t = (np.arange(nrofchunks)*chunklength*frame_duration)/refresh_rate
qmax = np.max(quals, axis=0)
qualsn = quals/qmax[np.newaxis, :]
ax = plt.subplot(111)
ax.plot(t, qualsn, alpha=0.3)
plt.ylabel('Z-score of center pixel (normalized)')
plt.xlabel('Minutes of stimulus analyzed')
plt.ylim([0, 1])
plf.spineless(ax, 'tr')
plt.title(f'Recording duration optimization\n{exp_name}\n {savefname}')
plt.savefig(savepath+'.svg', format='svg')
plt.close()