-
Notifications
You must be signed in to change notification settings - Fork 19
/
sc_loadmuadata.m
539 lines (376 loc) · 19.8 KB
/
sc_loadmuadata.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
function [features,mua]=sc_loadmuadata(muafile, dofeatures,s_opt)
skipsetup=0;
muafile
[ignore,ignoreb,muafile_ext] = fileparts(muafile);
muafile_ext = muafile_ext(2:end);
switch muafile_ext
case 'spikes' % open ephys data format
[data, timestamps, info] = load_open_ephys_data_faster(muafile);
% remove end of file?
% cutoff_t=3.9100e+03;
% cutoff=min(find(timestamps>cutoff_t));
% data=data(1:cutoff,:,:);
% timestamps=timestamps(1:cutoff);
features.chnumstr = info.header.electrode;
features.sourcechannel= str2num(info.header.electrode(end-1:end));
if numel( features.sourcechannel)==0
features.sourcechannel= str2num(info.header.electrode(end));
end;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
mua.Nspikes = numel(timestamps);
% mua.ts=timestamps./info.header.sampleRate;
mua.ts=timestamps;
[pathstr, name, ext] = fileparts(muafile);
mua.fname=[name,ext];
mua.opt=[];
mua.header=info.header;
mua.ncontacts = info.header.num_channels;
mua.val2volt=1; % alreaddy done in load script
% flatten waveforms for display
mua.waveforms=reshape(data,size(data,1),size(data,2)*size(data,3),1);
mua.ts_spike=linspace(-.5,3.5,40* mua.ncontacts); % neuralynx saves 32 samples at 30303Hz, so its a 1.056ms window
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
case 'mat'
load(muafile);
if ~ exist('mua', 'var') % no mua var in there, try doreas format
if exist('times_all', 'var') % marker for doreas mat format
if dofeatures
if s_opt.auto_number==0
prompt = {['source channel nr for file ',muafile]};
dlg_title = 'channel nr';
num_lines = 1;
def = {''};
features.chnumstr = inputdlg(prompt,dlg_title,num_lines,def);
features.sourcechannel= str2num(features.chnumstr{1});
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
else
%do it automatically
[ignore,n,ignoreb]=fileparts(muafile)
disp('automatically detecting ch number for');
disp(muafile);
nind=find(ismember(n, '0':'9'));
if numel(nind)>2 || numel(nind)==0
error('for automatic channel numbering make sure the mua filenames contain just one number between 1 and 99 ');
end;
ch=str2num(n(nind));
disp(['-> ch ',num2str(ch)]);
features.chnumstr = ch;
features.sourcechannel= ch;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
end;
end;
% load doreas format
mua.opt=[];
mua.fname=muafile;
mua.Nspikes = numel(times_all);
% concatenate all waveforms
mua.ncontacts = size(waveforms,2);
mua.waveforms = (reshape(waveforms,size(waveforms,1)*size(waveforms,2),size(waveforms,3) ))';
mua.ts = times_all;
mua.ts_spike=[1:size( mua.waveforms,2)];
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
end;
else % file has a variable mua in it, probably jakobs own format
if exist('features', 'var') %marker for simple_clust output format
if numel(mua.ts)<size(mua.waveforms,1)
warning('fewer spikes than waveforms, truncating!');
mua.waveforms = mua.waveforms(1:numel(mua.ts),:);
end;
% we just loaded previous simple_clust data
% in theory there should be nothing left to do here?
skipsetup=1;
features.skipsetup=1; % so the main program doesnt try to run scripts like auto noise rejection again
else
% parse jakobs ad hoc format here
% for now we only deal with simple electrodes and ones
% extracted from laminar recordings
if size(mua.waveforms,2)==3% extracted from laminar, 3 contacts!
mua.ncontacts = 3;
% reformat wavewforms, flatten for display
%D= (squeeze(reshape(mua.waveforms,1,128,size(mua.waveforms,3))));
D=[squeeze(mua.waveforms(:,1,:))',squeeze(mua.waveforms(:,2,:))',squeeze(mua.waveforms(:,3,:))'];
mua.waveforms=D;
mua.ts_spike=linspace(-.5,2.5,93); % we do 31 samples at 30303Hz, so its a 1.056ms window
if dofeatures
features=sc_mua2features(mua,s_opt);
sourcechannel=mua.sourcechannel;
features.sourcechannel=sourcechannel;
end;
elseif size(mua.waveforms,2)==4 %tetrode recording
mua.ncontacts = 4;
D=[squeeze(mua.waveforms(:,1,:))',squeeze(mua.waveforms(:,2,:))',squeeze(mua.waveforms(:,3,:))',squeeze(mua.waveforms(:,4,:))'];
mua.waveforms=D;
mua.ts_spike=linspace(-.5,2.5,93); % we do 31 samples at 30303Hz, so its a 1.056ms window
mua.ts_spike=linspace(0,4,size(mua.waveforms,2));
if dofeatures
features=sc_mua2features(mua,s_opt);
sourcechannel=mua.sourcechannel;
features.sourcechannel=sourcechannel;
end;
else
mua.ncontacts = 1;
if dofeatures
features=sc_mua2features(mua,s_opt);
sourcechannel=mua.sourcechannel;;
features.sourcechannel=sourcechannel;
end;
end;
end;
end;
case 'nse'
if dofeatures
if s_opt.auto_number==0
prompt = {['source channel nr for file ',muafile]};
dlg_title = 'channel nr';
num_lines = 1;
def = {''};
features.chnumstr = inputdlg(prompt,dlg_title,num_lines,def);
features.sourcechannel= str2num(features.chnumstr{1});
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
else
%do it automatically
[ignore,n,ignoreb]=fileparts(muafile)
disp('automatically detecting ch number for');
disp(muafile);
nind=find(ismember(n, '0':'9'));
if numel(nind)>2 || numel(nind)==0
error('for automatic channel numbering make sure the mua filenames contain just one number between 1 and 99 ');
end;
ch=str2num(n(nind));
disp(['-> ch ',num2str(ch)]);
features.chnumstr = ch;
features.sourcechannel= ch;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
end;
end;
cdata = read_cheetah_data(muafile);
%mua = load_neuralynx_mua(muafile);
clear mua;
mua=[];
mua.Nspikes = size(cdata.ts,1);
mua.waveforms=cdata.waveforms;
mua.ts=cdata.ts;
%identify bits2volt in header
vstart=strfind(cdata.header,'ADBitVolts');
mua.val2volt=str2num(cdata.header(vstart+10:vstart+24));
[pathstr, name, ext] = fileparts(muafile);
mua.fname=[name,ext];
mua.opt=[];
mua.header=cdata.header;
mua.ncontacts = size(mua.waveforms,1);
% flatten waveforms for display
D= (squeeze(reshape(mua.waveforms,1,32,size(mua.waveforms,3))));
mua.waveforms=[D(1:end,:)]';
mua.ts_spike=linspace(-.5,0.5,32); % neuralynx saves 32 samples at 30303Hz, so its a 1.056ms window
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
case 'nst'
if dofeatures
if s_opt.auto_number==0
prompt = {['source channel nr for file ',muafile]};
dlg_title = 'channel nr';
num_lines = 1;
def = {''};
features.chnumstr = inputdlg(prompt,dlg_title,num_lines,def);
features.sourcechannel= str2num(features.chnumstr{1});
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
else
%do it automatically
[ignore,n,ignoreb]=fileparts(muafile)
disp('automatically detecting ch number for');
disp(muafile);
nind=find(ismember(n, '0':'9'));
if numel(nind)>2 || numel(nind)==0
error('for automatic channel numbering make sure the mua filenames contain just one number between 1 and 99 ');
end;
ch=str2num(n(nind));
disp(['-> ch ',num2str(ch)]);
features.chnumstr = ch;
features.sourcechannel= ch;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
end;
end;
cdata = read_cheetah_data(muafile);
%mua = load_neuralynx_mua(muafile);
clear mua;
mua=[];
mua.Nspikes = size(cdata.ts,1);
mua.waveforms=cdata.waveforms;
mua.ts=cdata.ts;
[pathstr, name, ext] = fileparts(muafile);
mua.fname=[name,ext];
mua.opt=[];
mua.header=cdata.header;
%identify bits2volt in header
vstart=strfind(cdata.header,'ADBitVolts');
mua.val2volt=str2num(cdata.header(vstart+10:vstart+24));
mua.ncontacts = size(mua.waveforms,1);
% flatten waveforms for display
D= (squeeze(reshape(mua.waveforms,1,64,size(mua.waveforms,3))));
mua.waveforms=[D(1:2:end,:);D(2:2:end,:)]';
mua.ts_spike=linspace(-.5,1.5,64); % neuralynx saves 32 samples at 30303Hz, so its a 1.056ms window
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
case 'ntt'
if dofeatures
if s_opt.auto_number==0
prompt = {['source channel nr for file ',muafile]};
dlg_title = 'channel nr';
num_lines = 1;
def = {''};
features.chnumstr = inputdlg(prompt,dlg_title,num_lines,def);
features.sourcechannel= str2num(features.chnumstr{1});
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
else
%do it automatically
[ignore,n,ignoreb]=fileparts(muafile)
disp('automatically detecting ch number for');
disp(muafile);
nind=find(ismember(n, '0':'9'));
if numel(nind)>2 || numel(nind)==0
error('for automatic channel numbering make sure the mua filenames contain just one number between 1 and 99 ');
end;
ch=str2num(n(nind));
disp(['-> ch ',num2str(ch)]);
features.chnumstr = ch;
features.sourcechannel= ch;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
end;
end;
cdata = read_cheetah_data(muafile);
%mua = load_neuralynx_mua(muafile);
clear mua;
mua=[];
mua.Nspikes = size(cdata.ts,1);
mua.waveforms=cdata.waveforms;
mua.ts=cdata.ts;
[pathstr, name, ext] = fileparts(muafile);
mua.fname=[name,ext];
mua.opt=[];
mua.header=cdata.header;
mua.ncontacts = size(mua.waveforms,1);
%identify bits2volt in header
vstart=strfind(cdata.header,'ADBitVolts');
mua.val2volt=str2num(cdata.header(vstart+10:vstart+24));
% flatten waveforms for display
D= (squeeze(reshape(mua.waveforms,1,128,size(mua.waveforms,3))));
mua.waveforms=[D(1:4:end,:);D(2:4:end,:);D(3:4:end,:);D(4:4:end,:)]';
mua.ts_spike=linspace(-.5,3.5,128); % neuralynx saves 32 samples at 30303Hz, so its a 1.056ms window
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
case 'wf'
if dofeatures
if s_opt.auto_number==0
prompt = {['source channel nr for file ',muafile]};
dlg_title = 'channel nr';
num_lines = 1;
def = {''};
features.chnumstr = inputdlg(prompt,dlg_title,num_lines,def);
features.sourcechannel= str2num(features.chnumstr{1});
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
else
%do it automatically
[ignore,n,ignoreb]=fileparts(muafile);
disp('automatically detecting ch number for');
disp(muafile);
chstr = regexpi(n, '_Ch(\d)', 'tokens');
if isempty(chstr),
ch = 0;
else
%We'll take the first channel as our channel
ch = str2num(chstr{1}{1});
end
disp(['-> ch ',num2str(ch)]);
features.chnumstr = ch;
features.sourcechannel= ch;
sourcechannel=features.sourcechannel; % just so we dont overwrite it in 'features=sc_mua2features(mua,s_opt);'
end;
end;
% load tim's format
mua.opt = LoadSpikeWF(muafile, [], 6);
mua.fname=muafile;
mua.Nspikes = LoadSpikeWF(muafile, [], 5);
%Load waveforms
[mua.ts, wv] = LoadSpikeWF(muafile, [1 mua.Nspikes], 4);
% concatenate all waveforms
mua.ncontacts = size(wv,2);
%Copy waveforms over
size(wv)
mua.waveforms = zeros(size(wv, 1), size(wv, 2)*size(wv, 3));
for i = 1:size(wv, 1),
mua.waveforms(i, :) = reshape(squeeze(wv(i, :, :))', [1 size(wv, 2)*size(wv, 3)]);
end
size(mua.waveforms)
mua.ts_spike = linspace(mua.opt.SpikeExtract_WFRange(1), mua.opt.SpikeExtract_WFRange(2), mua.opt.NumPointsInWF)./mua.opt.SampleFrequency;
if dofeatures
features=sc_mua2features(mua,s_opt);
features.sourcechannel=sourcechannel;
end;
otherwise
error('unrecognized file format');
end;
if ~dofeatures
skipsetup=1;
features=[]; % in this case just return the mua
end;
if ~skipsetup
%% config and setup
features.clusters=ones(size(features.id));
features.clusters_undo=features.clusters;
features.clustervisible=ones(1,12);
if ~isfield(s_opt,'skipevery_wf_display') % bkwrds comp.
s_opt.skipevery_wf_display=8; % if it's not specified, use a pretty conservative number so that things look the same
end;
%features.clusters(1:100)=2;
features.labelcategories = {' ','noise','neg','unit','unit_{FS}','unit_{RS}','unit_{huge}','mua big','mua wide','mua thin','mua small','negative','artefact',};
features.clusterfstrs={'k.','b.','r.','g.','c.','r.','k.','r.','b.'};
features.colors=[.7 .7 .7; 1 0 0; 0 1 0; 0 0 1; 1 .7 0; 1 .2 1; 0 1 1; 1 .5 0; .5 1 0; 1 0 .5];
features.Nclusters=2;
features.imagesize=100;
features.waveformscale=0.0001;
mua.waveforms( find(abs(mua.waveforms) > 10000) ) =0;
mua.waveforms( isnan(mua.waveforms) ) =0;
% find appropriate scale for plotting waveforms
features.waveformscale=0.1 ./ quantile(mua.waveforms(1:100:end)-mean(mua.waveforms(1:100:end)),.95);
features.range=zeros(size(features.data,1),2); % for x/y range display
features.zoomrange=zeros(size(features.data,1),2); % where do we display right now
features.numextrafeaatures=0;
features.highlight = 0;
features.clusterimages=ones(features.imagesize,features.imagesize,12);
features.selected=0;
features.plotsize=0;
features.timeselectwidth=200;
features.timeselection=0;
features.Ndisplay=25000;
for i=1:1
features.isioptions(1).tmax=10;
features.isioptions(1).nbins=50;
end;
features.plotgroup=1;
features.highlight_multiple(1:10)=1;
features.clusterlabels(1:10)=1;
features.nlabels=numel(features.labelcategories );
% preprocess features to fit 1 1 -1 -1 box
features=sc_scale_features(features);
features=sc_addnoisetoquantiledfeatures(features);
% laod icons
%features.eye_icon_o=imread('eye.png');
%features.eye_icon_x=imread('eye_closed.png');
features.timevisible=ones(1,numel(features.ts));
features.randperm = randperm(numel(features.ts));
features.featureselects=[2 3];
features=sc_updateclusterimages(features,mua, s_opt);
end;
run=1;