-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.m
381 lines (359 loc) · 19.5 KB
/
main.m
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
% Iteratively data folder and find unique sessions
files = dir('allData');
total_files = length(files);
subjects = {};
dates = {};
session_ids = {};
for f = 1:total_files
% get the file name
f_name = convertCharsToStrings(files(f).name);
if(strlength(f_name) > 10) % filter out data files
fields = strsplit(f_name, '~'); % split the name to get fields
subject = fields(3);
date = fields(4);
session_id = fields(5);
subjects{end + 1} = char(subject);
dates{end + 1} = char(date);
session_ids{end + 1} = char(session_id);
end
end
% all data files
meta_table = table(subjects', dates', session_ids');
% filter out unique sessions available
unq_meta_table = unique(meta_table, 'stable');
msg = ['Found ', num2str(height(unq_meta_table)), ' sessions'];
disp(msg);
response_prompt = 'Convert all sessions (1) or only one (0): ';
response = input(response_prompt);
if(~response)
disp('Converting only one session data');
unq_meta_table = unq_meta_table(1, :);
else
disp('Converting all sessions data');
end
% loop over iterations
for sess = 1:height(unq_meta_table)
found = 0; % flag to check the particular session files are available
for f = 1:total_files
% get the file name
f_name = convertCharsToStrings(files(f).name);
if(strlength(f_name) > 10) % filter out data files
fields = strsplit(f_name, '~'); % split the name to get fields
subject = fields(3);
date = fields(4);
session_id = fields(5);
identifier = fields(7);
fname_nwb = strjoin(fields(1:6));
fname_nwb = replace(fname_nwb, ' ', '~');
fname_nwb = strcat(fname_nwb, '.nwb');
% if file fields match with req. session details
if((subject == unq_meta_table.Var1(sess) && ...
date == unq_meta_table.Var2(sess) && ...
session_id == unq_meta_table.Var3(sess)) && ~found)
% start converting session data
% initialize nwb file object
nwb_file = initialize_nwb_object(date, session_id);
nwb_file = populate(nwb_file, fields);
nwbExport(nwb_file, fname_nwb);
found = 1;
end
end
end
if(found ~= 0)
msg = sprintf('Processed subject id: %s, date: %s', ...
string(unq_meta_table.Var1(sess)), ...
string(unq_meta_table.Var2(sess)));
else
msg = sprintf('Unable to find subject id: %s date: %s', ...
string(unq_meta_table.Var1(sess)), ...
string(unq_meta_table.Var2(sess)));
end
disp(msg);
end
function f_name = proper_filename(fields, identifier)
f_name = strcat(strjoin(fields(1:6)), identifier);
f_name = replace(f_name, ' ', '~');
f_name = strcat('allData/', f_name);
end
function nwb_file = populate(nwb_file, fields)
% create file prefix string and processing module
file_prefix = strcat('allData/', strjoin(fields(1:6)), '~');
file_prefix = replace(file_prefix, ' ', '~');
behavior_module = types.core.ProcessingModule(...
'description', 'behavior module');
%% Converting Eye data
ts_data_unit = 'arb. unit';
ts_description = ['Features extracted from the ' ...
'video of the right eye.'];
ts_comments = ['The area of the pupil extracted ' ...
'with DeepLabCut. Note that ' ...
'it is relatively very small during the discrimination task ' ...
'and during the passive replay because the three screens are ' ...
'medium-grey at this time and black elsewhere - so the much ' ...
'brighter overall luminance levels lead to relatively ' ...
'constricted pupils.'];
xy_data_unit = ts_data_unit;
xy_description = ['Features extracted from the video ' ...
'of the right eye.'];
xy_comments = ['The 2D position of the center of the pupil ' ...
'in the video frame. This is not registered ' ...
'to degrees visual angle, but ' ...
'could be used to detect saccades or ' ...
'other changes in eye position.'];
reference_frame = '(0, 0) is the top-left';
[pupil_tracking, eye_tracking] = nxpl2nwb.Eye(file_prefix, ...
ts_data_unit, ts_description, ts_comments, ...
reference_frame, ...
xy_data_unit, xy_description, xy_comments);
behavior_module.nwbdatainterface.set('PupilTracking', pupil_tracking);
behavior_module.nwbdatainterface.set('EyeTracking', eye_tracking);
nwb_file.processing.set('behavior', behavior_module);
%% Convert Face energy data
dataunit = 'arb. unit';
description = ['Features extracted from the video of the ' ...
'frontal aspect of the subject, including the ' ...
'subject face and forearms.'];
comments = ['The integrated motion energy across the whole frame' ...
', i.e. sum( (thisFrame-lastFrame)^2 ). ' ...
'Some smoothing is applied before this operation.'];
face_energy = nxpl2nwb.Face(file_prefix, dataunit, ...
description, comments);
behavior_module.nwbdatainterface.set(...
'BehavioralTimeSeries', face_energy);
%% Convert Lick piezo data and add as NWB.acquisition
data_unit = 'V';
description = ['Voltage values from a thin-film piezo ' ...
'connected to the lick spout, so that values ' ...
'are proportional to deflection ' ...
'of the spout and licks can be detected ' ...
'as peaks of the signal.'];
lp_timeseries = nxpl2nwb.LickPiezo(file_prefix, data_unit, description);
nwb_file.acquisition.set('LickPiezo', lp_timeseries);
%% Convert Lick times data
data_unit = 'Unknown';
description = ['Extracted times of licks, ' ...
'from the lickPiezo signal.'];
lick_events = nxpl2nwb.LickTimes(file_prefix, data_unit, ...
description);
behavior_module.nwbdatainterface.set(...
'BehavioralEvents', lick_events);
%% Convert spontaneous intervals
description = ['Intervals of sufficient duration when nothing ' ...
'else is going on (no task or stimulus presentation'];
spontaneous_timeintervals = nxpl2nwb.Spontaneous(file_prefix, ...
description);
nwb_file.intervals.set('spontaneous', spontaneous_timeintervals);
%% Wheel times
data_unit = 'mm';
data_conversion = 0.135;
description = ['The position reading of the rotary encoder attached to ' ...
'the rubber wheel that the mouse pushes left and right ' ...
'with his forelimbs.'];
comments = ['The wheel has radius 31 mm and 1440 ticks per revolution, ' ...
'so multiply by 2*pi*r/tpr=0.135 to convert to millimeters. ' ...
'Positive velocity (increasing numbers) correspond to clockwise ' ...
'turns (if looking at the wheel from behind the mouse), i.e. ' ...
'turns that are in the correct direction for stimuli presented ' ...
'to the left. Likewise negative velocity corresponds to right choices.'];
wheel_timeseries = nxpl2nwb.Wheel(file_prefix, data_unit, data_conversion, ...
description, comments);
nwb_file.acquisition.set('WheelTimes', wheel_timeseries);
%% Wheel moves
description = 'Detected wheel movements.';
comments = ['0 for flinches or otherwise unclassified movements, ' ...
'1 for left/clockwise turns, 2 for right/counter-clockwise ' ...
'turns (where again "left" means "would be the correct ' ...
'direction for a stimulus presented on the left). A detected ' ...
'movement is counted as left or right only if it was ' ...
'sufficient amplitude that it would have registered a correct ' ...
'response (and possibly did), within a minimum amount of time ' ...
'from the start of the movement. Movements failing those ' ...
'criteria are flinch/unclassified type.'];
wheel_moves_behavior = nxpl2nwb.WheelMoves(file_prefix, ...
description, comments);
behavior_module.nwbdatainterface.set(...
'BehavioralEpochs', wheel_moves_behavior);
%% Sparse Noise
data_unit = 'degrees visual angle';
description = ['White squares shown on the screen with randomized ' ...
'positions and timing - see manuscript Methods.'];
comments = ['The altitude (first column) and azimuth (second column) ' ...
'of the square.'];
sparsenoise_timeseries = nxpl2nwb.SparseNoise(file_prefix, ...
data_unit, description, comments);
nwb_file.stimulus_presentation.set(...
'receptive_field_mapping_sparse_noise', sparsenoise_timeseries);
%% Convert Trials data and create NWB TrialTable
description = 'trial table for behavioral trials';
included_desc = ['Importantly, while this ' ...
'variable gives inclusion criteria according ' ...
'to the definition of disengagement ' ...
'(see manuscript Methods), it does ' ...
'not give inclusion criteria based on the ' ...
'time of response, as used ' ...
'for most analyses in the paper.'];
go_cue_desc = ['The goCue is referred to as the ' ...
'auditory tone cue in the manuscript.'];
visual_stimulus_time_desc = ['Times are relative to the same time' ...
'base as every other time in the ' ...
'dataset, not to the start of the trial'];
visual_stimulus_left_desc = ['Proportion contrast. A value of 0.5 ' ...
'means 50% contrast. 0 is a blank ' ...
'screen: no change to any pixel values on ' ...
'that side (completely undetectable).'];
visual_stimulus_right_desc = ['Proportion contrast. A value of 0.5 ' ...
'means 50% contrast. 0 is a blank ' ...
'screen: no change to any pixel values on ' ...
'that side (completely undetectable).'];
response_time_desc = ['Times are relative to the same time' ...
'base as every other time in the ' ...
'dataset, not to the start of the trial'];
response_choice_desc = ['Enumerated type. The response ' ...
'registered at the end of the trial, ' ...
'which determines the feedback ' ...
'according to the contrast condition. ' ...
'Note that in a small percentage of cases ' ...
'(~4%, see manuscript Methods) ' ...
'the initial wheel turn was in the opposite ' ...
'direction. -1 for Right ' ...
'choice (i.e. correct when stimuli are on the ' ...
'right); +1 for left ' ...
'choice; 0 for Nogo choice.'];
feedback_time_desc = ['Times are relative to the same time ' ...
'base as every other time in the dataset, ' ...
'not to the start of the trial.'];
feedback_type_desc = ['Enumerated type. -1 for negative ' ...
'feedback (white noise burst); +1 for ' ...
'positive feedback (water reward delivery).'];
rep_num_desc =['Trials are repeated if they are "easy" ' ...
'trials (high contrast stimuli with large difference ' ...
'between the two sides, or the blank screen condition) and ' ...
'this keeps track of how many times the current ' ...
'trials condition has been repeated.'];
trials = nxpl2nwb.TrialTable(file_prefix, description, ...
included_desc, go_cue_desc, visual_stimulus_time_desc, ...
visual_stimulus_left_desc, visual_stimulus_right_desc, ...
response_time_desc, response_choice_desc, ...
feedback_time_desc, feedback_type_desc, ...
rep_num_desc);
nwb_file.intervals_trials = trials;
%% Convert Passive stimulus data
% passive beeps
pb_data_unit = 'Unknown';
pb_description = ['Auditory tones of the same frequency as ' ...
'the auditory tone cue in the task'];
% passive valve clicks
pc_data_unit = 'Unknown';
pc_description = ['Opening of the reward valve, but with a clamp in place ' ...
'such that no water flows. Therefore the auditory sound of ' ...
'the valve is heard, but no water reward is obtained.'];
% passive visual times
pass_vis = proper_filename(fields(1:6), '~passiveVisual.times.npy');
pass_vis_left = proper_filename(fields(1:6), '~passiveVisual.contrastLeft.npy');
pass_vis_right = proper_filename(fields(1:6), '~passiveVisual.contrastRight.npy');
pvl_data_unit = 'proportion contrast';
pvl_description = ['Gratings of the same size, spatial freq, position, etc ' ...
'as during the discrimination task.'];
pvr_data_unit = 'proportion contrast';
pvr_description = ['Gratings of the same size, spatial freq, position, etc ' ...
'as during the discrimination task.'];
% passive valve clicks
pvc_data_unit = 'Unknown';
pvc_description = ['The sound that accompanies an incorrect response ' ...
' during the discrimination task.'];
[beep_ts, click_ts, pass_l, pass_r, pass_white] = nxpl2nwb.PassiveStim(...
file_prefix, ...
pb_data_unit, pb_description, ...
pc_data_unit, pc_description, ...
pvl_data_unit, pvl_description, ...
pvr_data_unit, pvr_description, ...
pvc_data_unit, pvc_description);
nwb_file.stimulus_presentation.set('passive_beeps', beep_ts);
nwb_file.stimulus_presentation.set('passive_click_times', click_ts);
nwb_file.stimulus_presentation.set('passive_left_contrast', pass_l);
nwb_file.stimulus_presentation.set('passive_right_contrast', pass_r);
nwb_file.stimulus_presentation.set('passive_white_noise', pass_white);
%% Create Electrode table to add neural data
device_desc = 'Probe device';
probe_electrode_desc = 'Neuropixels Phase3A opt3';
probe_location = 'Unknown';
[nwb_file, group_view] = nxpl2nwb.ElectrodeTable(nwb_file, ...
file_prefix, probe_location, ...
device_desc, ...
probe_electrode_desc);
%% Create Units table for clusters and spikes data
description = 'Units table';
electrode_group_desc = 'Electrode group';
electrodes_desc = 'Electrodes';
waveform_mean_desc = 'Waveform mean';
peakchannel_desc = ['The channel number of the location of ' ...
'the peak of the cluster waveform.'];
waveformduration_desc = ['The trough-to-peak duration of ' ...
'the waveform on the peak channel'];
phyannotations_desc = ['0 = noise (these are already excluded and ' ...
'dont appear in this dataset ' ...
'at all); 1 = MUA (i.e. presumed to contain ' ...
'spikes from multiple neurons; ' ...
'these are not analyzed in any analyses in the paper)' ...
'; 2 = Good (manually ' ...
'labeled); 3 = Unsorted. In this ' ...
'dataset Good was applied in a few but ' ...
'not all datasets to included neurons, ' ...
'so in general the neurons with ' ...
'_phy_annotation>=2 are the ones that should be included.'];
clusterdepths_desc = ['The position of the center of mass of the ' ...
'template of the cluster, ' ...
'relative to the probe. The deepest channel ' ...
'on the probe is depth=0, ' ...
'and the most superficial is depth=3820. ' ...
'Units: micro-m'];
samplingrate_desc = 'Sampling rate in Hz';
spikeamps_desc = ['The peak-to-trough amplitude, ' ...
'obtained from the template and ' ...
'template-scaling amplitude returned by Kilosort ' ...
'(not from the raw data).'];
spikedepths_desc = ['The position of the center of mass ' ...
'of the spike on the probe, ' ...
'determined from the principal component features ' ...
'returned by Kilosort. ' ...
'The deepest channel on the probe is depth=0, ' ...
'and the most superficial is depth=3820.'];
nwb_file = nxpl2nwb.ClustersSpikes(nwb_file, group_view, ...
file_prefix, ...
description, electrode_group_desc, electrodes_desc, ...
waveform_mean_desc, peakchannel_desc, waveformduration_desc, ...
phyannotations_desc, clusterdepths_desc, ...
samplingrate_desc, spikeamps_desc, spikedepths_desc);
%%
nwb_file.processing.set('behavior', behavior_module);
end
function nwb_file = initialize_nwb_object(date, session_id)
% intialize nwb object
% add subject information
subject = types.core.Subject('age', 'P77D', ...
'genotype', 'tetO-G6s x CaMK-tTA', ...
'sex', 'F', ...
'species', 'Mus musculus', ...
'description', 'strain: C57Bl6/J');
date = strsplit(string(date), '-');
% create nwb file
nwb_file = NwbFile(...
'session_description', ['Neuropixels recording during visual ' ...
'discrimination in awake mice.'], ...
'general_session_id', convertStringsToChars(session_id), ...
'session_start_time', datetime(str2num(date(1)), str2num(date(2)), ...
str2num(date(3)), 12, 0, 0), ...
'identifier', 'Cori_2016-12-14', ...
'general_institution', 'University College London', ...
'general_lab', 'The Carandini & Harris Lab', ...
'general_subject', subject, ...
'general_experimenter', 'Nick Steinmetz', ...
'general_experiment_description', ['Large-scale Neuropixels recordings' ...
'across brain regions of mice ' ...
'during a head-fixed visual ' ...
'discrimination task. '], ...
'general_related_publications', 'DOI 10.1038/s41586-019-1787-x', ...
'general_keywords', {'Neural coding', 'Neuropixels', 'mouse', ...
'brain-wide', 'vision', 'visual discrimination', ...
'electrophysiology'});
end