-
Notifications
You must be signed in to change notification settings - Fork 5
/
runOnlineRF.m
556 lines (467 loc) · 14.6 KB
/
runOnlineRF.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
%% starting function
function varargout = runOnlineRF(varargin)
% runOnlineRF Online Receptive Field mapping
%
% Version 1.0 [2019-04-02]
% Created for TDT NeuroNexus recordings by Jorrit Montijn
% Version 1.0.1 [2019-04-11]
% Improved high-pass filtering and rewrote for GPU processing
% Version 1.0.2 [2019-05-01]
% Stepwise data loading to reduce memory load
% Version 1.0.3 [2019-05-10]
% ENV-support and bug fixes
% Version 2.0.0a [2020-11-19]
% Neuropixels support with SpikeGLX
% Version 2.0.0b [2020-11-24]
% Attempted bug fix
% Version 2.0.1 [2020-11-25]
% Bug fixes
% Added optional smoothing
% Added scatter plot of RF per channel
% Version 2.0.2 [2020-11-26]
% Transformed all to use StreamCore module
% Version 2.0.3 [2021-01-15]
% Small bug fixes & transfer to parallel processing of data
% streaming and analysis
%set tags
%#ok<*INUSL>
%#ok<*INUSD>
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @runOnlineRF_OpeningFcn, ...
'gui_OutputFcn', @runOnlineRM_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
%% these are functions that don't do anything, but are required by matlab
function ptrListSelectMetric_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditHighpassFreq_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrListSelectChannel_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrListSelectProbe_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditDownsample_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrButtonOldFig_Callback(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrButtonNewFig_Callback(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditHighpassFreq_Callback(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrListSelectDataProcessing_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditChannelMin_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditChannelMax_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditHostSGL_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
function ptrEditStimSyncNI_CreateFcn(hObject, eventdata, handles),end %#ok<DEFNU>
%% opening function; initializes output
function runOnlineRF_OpeningFcn(hObject, eventdata, handles, varargin)
%opening actions
%define globals
global sFig;
global sRM;
%set closing function
set(hObject,'DeleteFcn','SC_DeleteFcn')
% set rainbow logo
I = imread('LogoRFmapper.jpg');
axes(handles.ptrAxesLogo);
imshow(I);
drawnow;
% set default output
handles.output = hObject;
guidata(hObject, handles);
%set default values
sRM = struct;
sRM = RM_populateStructure(sRM);
%populate figure
boolInit = true;
sFig = SC_populateFigure(handles,boolInit);
% set timer to query whether there is a data update every second
objMainTimer = timer();
objMainTimer.Period = 1;
objMainTimer.StartDelay = 1;
objMainTimer.ExecutionMode = 'fixedSpacing';
objMainTimer.TimerFcn = @RM_main;
sFig.objMainTimer = objMainTimer;
start(objMainTimer);
% set timer to update plots
objDrawTimer = timer();
objDrawTimer.Period = 1;
objDrawTimer.StartDelay = 1;
objDrawTimer.ExecutionMode = 'fixedSpacing';
objDrawTimer.TimerFcn = @RM_redraw;
sFig.objDrawTimer = objDrawTimer;
start(objDrawTimer);
%lock
set(sFig.ptrEditHighpassFreq,'UserData','lock');
set(sFig.ptrEditDownsample,'UserData','lock');
set(sFig.ptrEditChannelMin,'UserData','lock');
set(sFig.ptrEditChannelMax,'UserData','lock');
set(sFig.ptrButtonScatterYes,'UserData','lock')
set(sFig.ptrButtonScatterNo,'UserData','lock')
set(sFig.ptrButtonNewFig,'UserData','lock')
set(sFig.ptrButtonOldFig,'UserData','lock')
set(sFig.ptrButtonClearAll,'UserData','lock')
set(sFig.ptrListSelectProbe,'UserData','lock');
set(sFig.ptrListSelectDataProcessing,'UserData','lock');
set(sFig.ptrListSelectMetric,'UserData','lock');
set(sFig.ptrListSelectChannel,'UserData','lock')
% Update handles structure
guidata(hObject, handles);
%check if default host is online
ptrEditHostSGL_Callback([], [], handles);
end
%% defines output variables
function varargout = runOnlineRM_OutputFcn(hObject, eventdata, handles)
%output
varargout{1} = handles.output;
end
%% change in scatter plot
function ptrPanelScatterPlot_SelectionChangedFcn(hObject, eventdata, handles) %#ok<DEFNU>
%selection is automatically queried by drawing function,
%so no other action is required other than redrawing
%lock GUI
SC_lock(handles);
%redraw
RM_redraw(1);
%unlock GUI
SC_unlock(handles);
end
%% change in target figure
function ptrPanelPlotIn_SelectionChangedFcn(hObject, eventdata, handles) %#ok<DEFNU>
%selection is automatically queried by drawing function,
%so no other action is required other than redrawing
%lock GUI
SC_lock(handles);
%redraw
RM_redraw(1);
%unlock GUI
SC_unlock(handles);
end
%% select which image to display as background
function ptrListSelectMetric_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%selected image is automatically queried by drawing function; so no
%other action is required other than redrawing
%lock GUI
SC_lock(handles);
%redraw
RM_redraw(1);
%unlock GUI
SC_unlock(handles);
end
%% this function initializes everything
function ptrEditHostSGL_Callback(hObject, eventdata, handles)
%This function lets the user select an SGL host
% get globals
global sFig;
global sRM;
%lock GUI
SC_lock(handles);
%clear data
set(sFig.ptrTextChanNumIM, 'string', '...');
set(sFig.ptrTextRecording, 'string', '...');
set(sFig.ptrListSelectProbe, 'string', {''});
%connect to host
sRM.strHostSGL = get(sFig.ptrEditHostSGL,'String');
% try connection
try
%suppress warnings
cellText = {};
cellText{1} = ['Attempting to connect to host at ' sRM.strHostSGL];
SC_updateTextInformation(cellText);
sWarn = warning('off');
sRM.hSGL = SpikeGL(sRM.strHostSGL);
warning(sWarn);
SC_updateTextInformation('Success!');
catch ME
%unlock GUI
SC_unlock(handles);
if strcmp(ME.identifier,'ChkConn:ConnectFail')
SC_updateTextInformation({['Cannot connect to host at ' sRM.strHostSGL]});
return;
else
%disp error message
cellText = {};
cellText{1} = '<< ERROR >>';
cellText{2} = ME.identifier;
cellText{3} = ME.message;
SC_updateTextInformation(cellText);
rethrow(ME);
end
end
%retrieve channels to save; if settings are unvalidated, this will give an error
try
warning('off','CalinsNetMex:connectionClosed');
vecSaveChans = GetSaveChans(sRM.hSGL, 0); %#ok<NASGU>
warning('on','CalinsNetMex:connectionClosed');
catch ME
%unlock GUI
SC_unlock(handles);
%disp error message
cellText = {};
cellText{1} = '<< ERROR >>';
cellText{2} = ME.identifier;
cellText{3} = ME.message;
SC_updateTextInformation(cellText);
warning('on','CalinsNetMex:connectionClosed');
if contains(ME.message,'Run parameters never validated.')
%we know what this is; no need to panic
cellText{4} = '';
cellText{5} = 'Please verify your settings in SpikeGLX';
SC_updateTextInformation(cellText);
return;
else
rethrow(ME);
end
end
%initialize connection with SGL
[sFig,sRM] = SC_initSGL(sFig,sRM);
%unlock GUI
SC_unlock(handles);
%check if both data path and stim path is set
if isfield(sRM,'boolInitSGL') && ~isempty(sRM.boolInitSGL) && sRM.boolInitSGL && ...
isfield(sRM,'strSourcePathLog') && ~isempty(sRM.strSourcePathLog)
[sFig,sRM] = SC_initialize(sFig,sRM);
end
end
function ptrButtonChooseSourceStim_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%This function lets the user select a stim log path
%get globals
global sFig;
global sRM;
%lock GUI
SC_lock(handles);
%switch path
try
oldPath = cd(sRM.metaData.strSourcePathLog);
catch
oldPath = cd();
end
%get file
strSourcePathLog = uigetdir('Select stim log path');
%back to old path
cd(oldPath);
if isempty(strSourcePathLog) || isscalar(strSourcePathLog),SC_unlock(handles);return;end
if strcmpi(strSourcePathLog(end),filesep)
strSourcePathLog(end) = [];
end
sRM.strSourcePathLog = strSourcePathLog;
%fill recording/block data
set(sFig.ptrTextStimPath, 'string', strSourcePathLog);
%unlock GUI
SC_unlock(handles);
%check if connection is active and stim path is set
if isfield(sRM,'boolInitSGL') && ~isempty(sRM.boolInitSGL) && sRM.boolInitSGL && ...
isfield(sRM,'strSourcePathLog') && ~isempty(sRM.strSourcePathLog)
[sFig,sRM] = SC_initialize(sFig,sRM);
end
end
function ptrListSelectProbe_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%get globals
global sFig;
global sRM;
%lock GUI
SC_lock(handles);
% update maps
[sFig,sRM] = SC_initSGL(sFig,sRM);
%unlock GUI
SC_unlock(handles);
end
function ptrListSelectChannel_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%lock GUI
SC_lock(handles);
% update maps
RM_redraw(1);
%unlock GUI
SC_unlock(handles);
end
function ptrListSelectDataProcessing_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%lock GUI
SC_lock(handles);
% update maps
RM_redraw(1);
%unlock GUI
SC_unlock(handles);
end
function ptrEditDownsample_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%get globals
global sFig;
global sRM;
%downsample
dblSampFreqIM = sRM.dblSampFreqIM;
dblSampFreqNI = sRM.dblSampFreqNI;
dblSubSampleToReq = str2double(get(sFig.ptrEditDownsample,'String'));
sRM.intSubSampleFactorIM = round(dblSubSampleToReq*dblSampFreqIM);
if isnan(sRM.intSubSampleFactorIM),sRM.intSubSampleFactorIM=0;end
sRM.dblSubSampleTo = sRM.intSubSampleFactorIM/dblSampFreqIM;
if isnan(sRM.dblSubSampleTo),sRM.dblSubSampleTo=0;end
sRM.dblSubSampleFactorNI = dblSubSampleTo/dblSampFreqNI;
set(sFig.ptrEditDownsample,'String',sprintf('%.3f',dblSubSampleTo));
set(sFig.ptrTextDownsampleFactor,'String',num2str(intSubSampleFactorIM));
end
function ptrPanicButton_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%get global
global sFig;
%unlock busy & GUI
sFig.boolIsBusy = false;
sFig.boolIsDrawing = false;
SC_unlock(handles);
%restart main timer
stop(sFig.objMainTimer);
objTimer = timer();
objTimer.Period = 1;
objTimer.StartDelay = 1;
objTimer.ExecutionMode = 'fixedSpacing';
objTimer.TimerFcn = @RM_main;
sFig.objMainTimer = objTimer;
start(objTimer);
%restart draw timer
stop(sFig.objMainTimer);
objTimer = timer();
objTimer.Period = 1;
objTimer.StartDelay = 1;
objTimer.ExecutionMode = 'fixedSpacing';
objTimer.TimerFcn = @RM_redraw;
sFig.objDrawTimer = objTimer;
start(objTimer);
%update text
SC_updateTextInformation({''});
end
function ptrButtonClearAll_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%define globals
global sFig;
global sRM;
%stop timer
stop(sFig.objMainTimer);
stop(sFig.objDrawTimer);
%clear data and reset to defaults
sRM = struct;
sRM = RM_populateStructure(sRM);
sFig = SC_populateFigure(handles,false,sFig);
% set timer to query whether there is a data update every second
objTimer = timer();
objTimer.Period = 1;
objTimer.StartDelay = 1;
objTimer.ExecutionMode = 'fixedSpacing';
objTimer.TimerFcn = @RM_main;
sFig.objMainTimer = objTimer;
start(objTimer);
% set timer to redraw
objDrawTimer = timer();
objDrawTimer.Period = 1;
objDrawTimer.StartDelay = 1;
objDrawTimer.ExecutionMode = 'fixedSpacing';
objDrawTimer.TimerFcn = @RM_redraw;
sFig.objDrawTimer = objDrawTimer;
start(objDrawTimer);
%update text
SC_updateTextInformation({''});
%check if default host is online
ptrEditHostSGL_Callback([], [], handles);
end
function ptrButtonClearAndRecompute_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%define global
global sRM;
global sFig;
%save initialization parameters
IsInitialized = sRM.IsInitialized;
UseGPU = sRM.UseGPU;
%clear rest
sRM = struct;
sRM = RM_populateStructure(sRM);
sRM.IsInitialized = IsInitialized;
sRM.UseGPU = UseGPU;
%reload data if initialized
if IsInitialized
%lock gui
SC_lock(handles);
SC_updateTextInformation({'Data cleared, re-processing data...'});
%connect to host
sRM.strHostSGL = get(sFig.ptrEditHostSGL,'String');
sRM.hSGL = SpikeGL(sRM.strHostSGL);
%re-establish connection
[sFig,sRM] = SC_initSGL(sFig,sRM);
%reinitialize
[sFig,sRM] = SC_initialize(sFig,sRM);
%run main
RM_main();
end
end
function ptrEditChannelMin_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%define globals
global sRM;
global sFig;
%lock gui
SC_lock(handles);
%get data
intMinChan = str2double(get(hObject,'String'));
strMsg = '';
%check range
if intMinChan < 1
strMsg = strcat(strMsg,sprintf('%d is out of range; ',intMinChan));
intMinChan = 1;
end
if intMinChan > numel(sRM.vecUseChans)
strMsg = strcat(strMsg,sprintf('%d is out of range; ',intMinChan));
intMinChan = numel(sRM.vecUseChans);
end
strMsg = strcat(strMsg,sprintf('Min chan set to %d',intMinChan));
%assign to global
sRM.intMinChan = intMinChan;
set(hObject,'String',num2str(intMinChan));
%update msg
SC_updateTextInformation({strMsg});
%unlock gui
SC_unlock(handles);
end
function ptrEditChannelMax_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%define globals
global sRM;
global sFig;
%lock gui
SC_lock(handles);
%get data
intMaxChan = str2double(get(hObject,'String'));
strMsg = '';
%check range
if intMaxChan < 1
strMsg = strcat(strMsg,sprintf('%d is out of range; ',intMaxChan));
intMaxChan = 1;
end
if intMaxChan > numel(sRM.vecUseChans)
strMsg = strcat(strMsg,sprintf('%d is out of range; ',intMaxChan));
intMaxChan = numel(sRM.vecUseChans);
end
strMsg = strcat(strMsg,sprintf('Max chan set to %d',intMaxChan));
%assign to global
sRM.intMaxChan = intMaxChan;
set(hObject,'String',num2str(intMaxChan));
%update msg
SC_updateTextInformation({strMsg});
%unlock gui
SC_unlock(handles);
end
function ptrEditStimSyncNI_Callback(hObject, eventdata, handles) %#ok<DEFNU>
%get globals
global sRM;
%lock GUI
SC_lock(handles);
%get channel
intStimSyncChanNI = str2double(get(hObject,'String'));
%check if channel lies within range of NI channels
vecSaveChans = GetSaveChans(sRM.hSGL, -1);
if ~ismember(intStimSyncChanNI,vecSaveChans)
cellText = {'<< WARNING >>','',sprintf('Sync channel %d is out of NI channel range',intStimSyncChanNI)};
else
cellText = {sprintf('Changing stim sync channel to %d',intStimSyncChanNI)};
end
SC_updateTextInformation(cellText);
%assign new channel ID
sRM.intStimSyncChanNI = intStimSyncChanNI;
%unlock GUI
SC_unlock(handles);
end