forked from JorritMontijn/Acquipix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunOptoStim.m
395 lines (341 loc) · 12.7 KB
/
RunOptoStim.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
%function structEP = RunDriftingGratings
%% suppress m-lint warnings
%#ok<*MCCD,*NASGU,*ASGLU,*CTCH>
clearvars -except sStimPresets sExpMeta sStimParamsSettings;
%% define variables
fprintf('Starting %s [%s]\n',mfilename,getTime);
intStimSet = 1;% 1=0:15:359, reps20; 2=[0 5 90 95], reps 400 with noise
boolUseSGL = true;
boolUseNI = true;
boolDebug = false;
strHostAddress = '192.87.10.238'; %default host address
if exist('sExpMeta','var')
%defaults
objDaqOut = [];
%expand structure
if isfield(sExpMeta,'strHostAddress'),strHostAddress=sExpMeta.strHostAddress;end
if isfield(sExpMeta,'objDaqOut'),objDaqOut=sExpMeta.objDaqOut;end
if isfield(sExpMeta,'boolUseSGL'),boolUseSGL=sExpMeta.boolUseSGL;end
if isfield(sExpMeta,'boolUseNI'),boolUseNI=sExpMeta.boolUseNI;end
else
sExpMeta = [];
end
%% query user input for recording name
if exist('sStimParamsSettings','var') && isfield(sStimParamsSettings,'strRecording')
strRecording = sStimParamsSettings.strRecording;
else
strRecording = input('Recording name (e.g., MouseX): ', 's');
end
%% input params
fprintf('Loading settings...\n');
if ~exist('sStimParamsSettings','var') || isempty(sStimParamsSettings)
%parameters
sStimParamsSettings.strStimType = 'OptoStim';
sStimParamsSettings.strHostAddress = strHostAddress;
sStimParamsSettings.strOutputPath = 'F:\DataRaw\Exp'; %appends date
sStimParamsSettings.strTempObjectPath = 'D:\Tempdata\';%X:\JorritMontijn\ or F:\Data\Temp\
sStimParamsSettings.dblPulseVoltage = 3;%volts
sStimParamsSettings.dblSamplingRate = 10000;%Hz
sStimParamsSettings.intUseDaqDevice = 1; %ID of DAQ device
sStimParamsSettings.strPortOut = 'ao1'; %output port of DAQ device
elseif ~strcmpi(sStimParamsSettings.strStimType,'OptoStim')
error([mfilename ':WrongStimType'],sprintf('You''re trying to run "%s", but the stim parameter structure is meant for "%s"',mfilename',sStimParamsSettings.strStimType));
else
% evaluate and assign pre-defined values to structure
cellFields = fieldnames(sStimParamsSettings);
for intField=1:numel(cellFields)
try
sStimParamsSettings.(cellFields{intField}) = eval(sStimParamsSettings.(cellFields{intField}));
catch
sStimParamsSettings.(cellFields{intField}) = sStimParamsSettings.(cellFields{intField});
end
end
end
%% set output locations for logs
strOutputPath = sStimParamsSettings.strOutputPath;
strTempObjectPath = sStimParamsSettings.strTempObjectPath;
strThisFilePath = mfilename('fullpath');
[strFilename,strLogDir,strTempDir,strTexDir] = RE_assertPaths(strOutputPath,strRecording,strTempObjectPath,strThisFilePath);
fprintf('Saving output in directory %s; loading textures from %s\n',strLogDir,strTexDir);
%% initialize connection with SpikeGLX
if boolUseSGL
%check if data are supplied
if exist('sExpMeta','var') && isfield(sExpMeta,'hSGL') && isfield(sExpMeta,'strRunName') && isfield(sExpMeta,'sParamsSGL')
%get data
hSGL = sExpMeta.hSGL;
strRunName = sExpMeta.strRunName;
sParamsSGL = sExpMeta.sParamsSGL;
%start recording
intOutFlag = StartRecordingSGL(hSGL);
else
%start connection
fprintf('Opening SpikeGLX connection & starting recording "%s" [%s]...\n',strRecording,getTime);
[hSGL,strRunName,sParamsSGL] = InitSGL(strRecording,strHostAddress);
end
fprintf('SGL saving to "%s", matlab saving to "%s.mat" [%s]...\n',strRunName,strFilename,getTime);
%retrieve some parameters
intStreamNI = -1;
dblSampRateNI_rec = GetSampleRate(hSGL, intStreamNI);
intStreamIM = 0; %needs to be adjusted if there are more than one probe
dblSampRateIM_rec = GetSampleRate(hSGL, intStreamIM);
%% check disk space available
%strDataDirSGL = GetDataDir(hSGL);
%jFileObj = java.io.File(strDataDirSGL);
%dblFreeGB = (jFileObj.getFreeSpace)/(1024^3);
%if dblFreeGB < 100,warning([mfilename ':LowDiskSpace'],'Low disk space available (%.0fGB) for Neuropixels data (dir: %s)',dblFreeGB,strDataDirSGL);end
else
sParamsSGL = struct;
end
%% build structEP
%load presets
if ~exist('sStimPresets','var') || ~strcmp(sStimPresets.strExpType,mfilename)
sStimPresets = loadStimPreset(intStimSet,mfilename);
end
% evaluate and assign pre-defined values to structure
structEP = struct; %structureElectroPhysiology
cellFieldsSP = fieldnames(sStimPresets);
for intField=1:numel(cellFieldsSP)
try
structEP.(cellFieldsSP{intField}) = eval(sStimPresets.(cellFieldsSP{intField}));
catch
structEP.(cellFieldsSP{intField}) = sStimPresets.(cellFieldsSP{intField});
end
end
structEP.intStimTypes = numel(sStimPresets.vecPulseITI);
%% combine data & pre-allocate
%combine
sStimParams = catstruct(sStimParamsSettings,sStimPresets);
%extract
cellFields = fieldnames(sStimParams);
for intField=1:numel(cellFields)
eval([(cellFields{intField}) ' = sStimParams.(cellFields{intField});']);
end
%build pres vectors
cellPulseData = cell(1,intTrialNum);
cellPulseITI = cell(1,intTrialNum);
cellPulseDur = cell(1,intTrialNum);
vecPulseVolt = nan(1,intTrialNum);
vecStimOnNI = nan(1,intTrialNum);
vecStimOffNI = nan(1,intTrialNum);
for intTrial=1:intTrialNum
%shuffle order
vecRand = randperm(numel(vecPulseITI));
vecShuffITI = vecPulseITI(vecRand);
vecShuffDur = vecPulseDur(vecRand);
vecData = logical([]);
for intPulseType=1:numel(vecShuffITI)
vecOnePulse = cat(1,true(round(vecShuffDur(intPulseType)*dblSamplingRate),1),false(round(vecShuffITI(intPulseType)*dblSamplingRate),1));
vecPulses = repmat(vecOnePulse,[intRepsPerPulse 1]);
vecWait = false(round(dblSamplingRate*dblPulseWaitSignal),1);
vecData = cat(1,vecData,vecPulses,vecWait);
end
vecPulseVolt(intTrial) = dblPulseVoltage;
cellPulseData{intTrial} = vecData;
cellPulseITI{intTrial} = vecShuffITI;
cellPulseDur{intTrial} = vecShuffDur;
end
%% initialize NI I/O box
if boolUseNI && sStimParamsSettings.intUseDaqDevice > 0
%% setup connection
%query connected devices
objDevice = daq.getDevices;
strCard = objDevice.Model;
strID = objDevice.ID;
%create connection
objDAQOut = daq.createSession(objDevice(sStimParamsSettings.intUseDaqDevice).Vendor.ID);
%set variables
objDAQOut.IsContinuous = true;
objDAQOut.Rate=round(dblSamplingRate); %1ms precision
objDAQOut.NotifyWhenScansQueuedBelow = 100;
%add picospritzer output channels
%[chOut0,dblIdx0] = addAnalogOutputChannel(objDAQOut, strID, 'ao0', 'Voltage');
%add opto LED output channels
[chOut1,dblIdx1] = addAnalogOutputChannel(objDAQOut, strID, strPortOut, 'Voltage');
%% set spritzer off
dblStartT = 0.1;
%queueOutputData(objDAQOut,repmat([0 0],[ceil(objDAQOut.Rate*dblStartT) 1]));
queueOutputData(objDAQOut,zeros([ceil(objDAQOut.Rate*dblStartT) 1]));
startBackground(objDAQOut);
pause(dblStartT);
else
objDAQOut = struct;
end
%% assign to structure
structEP.strRecording = strRecording;
structEP.strFilename = strFilename;
structEP.dblPrePostWait = dblPrePostWait;%secs
structEP.dblSamplingRate = dblSamplingRate;%Hz
structEP.intRepsPerPulse = intRepsPerPulse;%count
structEP.intTrialNum = intTrialNum;%count
structEP.dblPulseWait = dblPulseWait;%secs, at least ~0.2s
structEP.vecPulseITI = vecPulseITI;%secs
structEP.vecPulseDur = vecPulseDur;%secs
structEP.dblPulseWaitSignal = dblPulseWaitSignal;
structEP.dblPulseWaitPause = dblPulseWaitPause;
structEP.sStimParams = sStimParams;
structEP.sParamsSGL = sParamsSGL;
structEP.objDAQOut = objDAQOut;
%timestamp start
structEP.strStartDate = getDate();
structEP.strStartTime = getTime();
try
%% check escape
if CheckEsc(),error([mfilename ':EscapePressed'],'Esc pressed; exiting');end
%% start pre-wait
hTicExpStart = tic;
fprintf('Experiment started; initial wait of %.1fs [%s]\n',dblPrePostWait,getTime);
while toc(hTicExpStart) < dblPrePostWait
if CheckEsc(),error([mfilename ':EscapePressed'],'Esc pressed; exiting');end
pause(1/1000);
end
warning('off','CalinsNetMex:connectionClosed');
%% run stimuli
for intTrial = 1:intTrialNum
%timestamp
hTicTrial = tic;
%save current data
vecPulseVolt_Temp = vecPulseVolt(1:(intTrial-1));
cellPulseData_Temp = cellPulseData(1:(intTrial-1));
cellPulseITI_Temp = cellPulseITI(1:(intTrial-1));
cellPulseDur_Temp = cellPulseDur(1:(intTrial-1));
vecStimOnNI_Temp = vecStimOnNI(1:(intTrial-1));
vecStimOffNI_Temp = vecStimOffNI(1:(intTrial-1));
save(fullfile(strTempDir,[strFilename '_Temp']),...
'vecPulseVolt_Temp','cellPulseData_Temp','cellPulseITI_Temp','cellPulseDur_Temp','vecStimOnNI_Temp','vecStimOffNI_Temp');
%get new pulse data
matData = vecPulseVolt(intTrial)*double(cellPulseData{intTrial});
%msg
fprintf('Trial %d/%d [%s]\n',intTrial,intTrialNum,getTime);
%check for escape in-between pulse runs
if CheckEsc(),error([mfilename ':EscapePressed'],'Esc pressed; exiting');end
%prep stimulus
if boolUseNI && sStimParamsSettings.intUseDaqDevice > 0
stop(objDAQOut);
%extend
if size(matData,2) == 1
%matData = repmat(matData,[1 2]);
end
%prep
stop(objDAQOut);
queueOutputData(objDAQOut,matData);
prepare(objDAQOut);
end
%wait
while toc(hTicTrial) < (dblPulseWaitPause*0.9)
pause((dblPulseWaitPause - toc(hTicTrial))*0.3);
end
while toc(hTicTrial) < dblPulseWaitPause
%do nothing
end
%start stimulus
fprintf('\b; stim started at %.3fs\n',toc(hTicTrial));
if boolUseNI && sStimParamsSettings.intUseDaqDevice > 0,startBackground(objDAQOut);end
%log NI timestamp
if boolUseSGL
dblStimOnNI = GetScanCount(hSGL, intStreamNI)/dblSampRateNI_rec;
else
dblStimOnNI = nan;
end
%wait
if boolUseNI && sStimParamsSettings.intUseDaqDevice > 0
dblTimeout = (size(matData,1)/dblSamplingRate)*1.5;
wait(objDAQOut,dblTimeout);
end
%log NI timestamp
if boolUseSGL
dblStimOffNI = GetScanCount(hSGL, intStreamNI)/dblSampRateNI_rec;
else
dblStimOffNI = nan;
end
%log timestamps
vecStimOnNI(intTrial) = dblStimOnNI;
vecStimOffNI(intTrial) = dblStimOffNI;
%msg
fprintf('\b; trial finished at %.3fs [%s]\n',toc(hTicTrial),getTime);
end
warning('on','CalinsNetMex:connectionClosed');
%save data
structEP.vecPulseVolt = vecPulseVolt(1:intTrial);
structEP.cellPulseData = cellPulseData(1:intTrial);
structEP.cellPulseITI = cellPulseITI(1:intTrial);
structEP.cellPulseDur = cellPulseDur(1:intTrial);
structEP.vecStimOnNI = vecStimOnNI(1:intTrial);
structEP.vecStimOffNI = vecStimOffNI(1:intTrial);
if exist('dblSampRateNI_rec','var')
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL','dblSampRateNI_rec','dblSampRateIM_rec');
else
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL');
end
%% end-wait
hTicExpStop = tic;
fprintf('Starting final wait of %.1fs [%s]\n',dblPrePostWait,getTime);
while toc(hTicExpStart) < dblPrePostWait
if CheckEsc(),error([mfilename ':EscapePressed'],'Esc pressed; exiting');end
pause(1/1000);
end
%closing remark
fprintf('\nExperiment is finished at [%s], closing down and cleaning up...\n',getTime);
%end recording
if boolUseSGL,CloseSGL(hSGL);end
%close Daq IO
if sStimParamsSettings.intUseDaqDevice > 0
try
closeDaqOutput(objDAQOut);
if boolDaqIn
closeDaqInput(objDAQIn);
end
catch
end
end
catch ME
%% check if escape
if strcmp(ME.identifier,'RunOptoStim:EscapePressed')
fprintf('\nEscape pressed at [%s], closing down and cleaning up...\n',getTime);
%save data
structEP.vecPulseVolt = vecPulseVolt(1:intTrial);
structEP.cellPulseData = cellPulseData(1:intTrial);
structEP.cellPulseITI = cellPulseITI(1:intTrial);
structEP.cellPulseDur = cellPulseDur(1:intTrial);
structEP.vecStimOnNI = vecStimOnNI(1:intTrial);
structEP.vecStimOffNI = vecStimOffNI(1:intTrial);
if exist('dblSampRateNI_rec' ,'var')
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL','dblSampRateNI_rec','dblSampRateIM_rec');
else
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL');
end
%clean up
fprintf('\nExperiment is finished at [%s], closing down and cleaning up...\n',getTime);
%% close Daq IO
if boolUseNI && ~(exist('sExpMeta','var') && isfield(sExpMeta,'objDaqOut'))
try
closeDaqOutput(objDaqOut);
catch
end
end
else
%% catch me and throw me
fprintf('\n\n\nError occurred! Trying to save data and clean up...\n\n\n');
%save data
structEP.vecPulseVolt = vecPulseVolt(1:intTrial);
structEP.cellPulseData = cellPulseData(1:intTrial);
structEP.cellPulseITI = cellPulseITI(1:intTrial);
structEP.cellPulseDur = cellPulseDur(1:intTrial);
structEP.vecStimOnNI = vecStimOnNI(1:intTrial);
structEP.vecStimOffNI = vecStimOffNI(1:intTrial);
if exist('dblSampRateNI_rec','var')
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL','dblSampRateNI_rec','dblSampRateIM_rec');
else
save(fullfile(strLogDir,strFilename), 'structEP','sParamsSGL');
end
%% close Daq IO
if boolUseNI && ~(exist('sExpMeta','var') && isfield(sExpMeta,'objDaqOut'))
try
closeDaqOutput(objDaqOut);
catch
end
end
%% show error
rethrow(ME);
end
end
%end