-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMIAcqnvs_v2.asv
368 lines (321 loc) · 15.7 KB
/
BMIAcqnvs_v2.asv
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
function out = BMIAcqnvs_v2(vals,varargin)
%{
Scanimage needs a linear function script style to run between frames.
For this reason the code needs to be executed line by line. Besides
each frame the function gets executed and returns 0, which means,
variables that are not persistent will be lost. This function needs to
be as light as possible. Scanimage will not execute the function if it
does not have time enough for it. Avoid large computations, saving to
memory, large matrices copying and anything that requires computational
power, and therefore time, from the CPU
Inputs: vals is the activity of the neurons selected on scanimage
expt_str --> buffer:
0) normal_bmi
flagBMI = true; (use self-generated hits to give waterer reward, no stim)
flagwater = true; flagstim = False
0) dstim_bmi
flagBMI = true; (use self-generated hits to send D1/D2 stim)
flagwater = false; flagstim = true;
3) block_bmi
flagBMI = true; (use self-generated hits to give water reward block D1/D2)
flagwater = true; flagstim = true;
%}
%% To initialize the recording:
% this is only to initialize. History keeps most of the persisten vars
global history
global data
% this is required to clean all the persistent variables if need be
flush = 0;
% history.index = 1;
if flush
vars = whos;
vars = vars([vars.persistent]);
varName = {vars.name};
clear(varName{:});
out = [];
clear a;
clear history;
disp('flushiiiing')
return
end
%%
%**********************************************************
%**************** PARAMETERS ****************************
%**********************************************************
%% BMI parameters
% parameters for the function that do not change (and we may need in each
% iteration. --> GIVEN BY SETTINGS (task/calibration/etc)
out = 0; %required output to ScanImage
%% needed variables
% Define persistent variables that will remain in each iteration of this
% function
persistent a flags counters task_settings T decoder units back_2_base block_val fb_mapping
%%
%*********************************************************************
%****************** INITIALIZE ***********************************
%*********************************************************************
% initialize arduino for playing sound (if not initialized already).
if ~isa(a, 'arduino')
a = arduino('COM15', 'Uno');
disp('starting arduino')
end
if history.index==1 %if this is the first time it runs this program (after flush)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% bring the settings from base
task_settings = evalin('base','task_settings');
T = evalin('base','calibration_results.T');
decoder = evalin('base','calibration_results.decoder');
units = evalin('base','calibration_settings.units');
back_2_base = evalin('base','calibration_settings.params.back2base_alpha')*T;
block_val = evalin('base','task_settings.params.block_at_alpha')*T;
fb_mapping = evalin('base','calibration_results.fb_mapping');
%global parameters hSI;
counters.do_not_update_buffer = task_settings.params.initial_count;
counters.update_buffer = 0;
counters.miss = 0;
counters.after_motion = 0;
counters.trial = 1;
counters.back_2_base = 0;
counters.baseline_full = task_settings.base_frames;
counters.tim = 0;
counters.tim_frame = 0;
flags.back_2_baseline = false;
flags.new_trial = true;
flags.base_buffer_full = false;
flags.init_baseline = true;
flags.motion = false;
flags.stim_done = false;
data.cursor = single(nan(1, task_settings.expected_length_experiment)); %define a very long vector for cursor
data.frequency = single(nan(1, task_settings.expected_length_experiment));
data.hits = single(nan(1, task_settings.params.length_trials));
data.miss = single(nan(1, task_settings.params.length_trials));
data.reward = single(nan(1, task_settings.params.length_trials));
data.stims = single(nan(1, task_settings.params.length_trials));
data.trial_end = single(nan(1, task_settings.params.length_trials));
data.trial_start = single(nan(1, task_settings.params.length_trials));
data.time_vector = single(nan(1, task_settings.expected_length_experiment));
history.baseval = single(ones(units,1).*vals);
history.buffer = single(nan(units, task_settings.moving_average_frames)); %define a windows buffer
history.last_volume = 0; %careful with this it may create problems
history.nZ=evalin('base','hSI.hFastZ.numFramesPerVolume');
history.number_hits = 0;
history.number_miss = 0;
history.number_rewards = 0;
history.number_trials = 0;
history.number_stims = 0;
end
%% some parameters depending on settinggs
% init_frame_base = task_settings.params.initial_count + 1;
% experiment FLAGS
switch lower(task_settings.experiment) %make it case insensitive. all lower cases
case 'normal_bmi'
flags.BMI = true;
flags.water = true;
flags.stim_time = false;
flags.stim_target = false;
flags.random_stim = false;
case 'dstim_bmi'
flags.BMI = true;
flags.water = false;
flags.stim_time = false;
flags.stim_target = true;
flags.random_stim = false;
case 'block_bmi_reward'
flags.BMI = true;
flags.water = true;
flags.stim_time = false;
flags.stim_target = true;
flags.random_stim = false;
case 'block_bmi'
flags.BMI = true;
flags.water = true;
flags.stim_time = true;
flags.stim_target = false;
flags.random_stim = false;
case 'random_stim'
flags.BMI = true;
flags.water = true;
flags.stim_time = false;
flags.stim_target = false;
flags.random_stim = true;
end
%% Prepare the stim !!!TODO!!!
%%
%************************************************************************
%*************************** RUN ********************************
%************************************************************************
% acquire the actual frame
this_frame = evalin('base','hSI.hScan2D.hAcq.hFpga.AcqStatusAcquiredFrames');
if isempty(history.nZ)
history.nZ = 1;
end
this_volume = floor(this_frame/history.nZ);
%if we've completed a new volume, update history
% store nans on frames that we've skipped so we know we skipped
% them
if this_volume > history.last_volume % if this is a new volume
counters.tim_frame = cputime;
% handle ******* MOTION***********
% because we don't want to stim or reward or update buffer if there is motion
mot = evalin('base', 'hSI.hMotionManager.motionCorrectionVector');
if ~isempty(mot)
motion = sqrt(mot(1)^2 + mot(2)^2 + mot(3)^2);
else
motion = 0;
end
if motion > task_settings.params.motion_thresh
flags.motion = true;
counters.after_motion = 0;
else
if counters.after_motion >= task_settings.motion_relaxation_frames
flags.motion = false;
else
counters.after_motion = counters.motion + 1;
end
end
% obtain the frame
steps = this_volume - history.last_volume;
history.last_volume = this_volume;
history.index = history.index + steps;
if counters.do_not_update_buffer == 0 && ~flags.motion
%update frame
% variable to hold nans in unseen frames
placeholder = nan(numel(vals),steps-1);
mVals = [placeholder vals];
% update buffer of activity history
if steps < task_settings.moving_average_frames
history.buffer(:, 1: end-steps) = history.buffer(:, steps+1:end);
history.buffer(:,end-steps+1:end) = mVals;
else
history.buffer = mVals(:, end-task_settings.moving_average_frames+1:end);
end
signal = single(nanmean(history.buffer, 2));
% update dynamic baseline. baseline may be seeded if another BMI was run before
if flags.init_baseline && ~isnan(sum(sum(task_settings.base_val_seed)))
flags.base_buffer_full = true;
history.baseval = task_settings.base_val_seed;
disp('baseBuffer seeded!');
elseif ~ flags.base_buffer_full && counters.baseline_full > 0
history.baseval = (history.baseval*(single(history.index) - 1) + signal)./single(history.index);
counters.baseline_full = counters.baseline_full - 1;
if counters.baseline_full == 0
disp('baseline full')
flags.base_buffer_full = true;
end
else
history.baseval = (history.baseval*(task_settings.base_frames - 1) + signal)./task_settings.base_frames;
end
% baseline has been initiated one way or another
if flags.init_baseline
flags.init_baseline = false;
end
if flags.base_buffer_full % only after it finishes with baseline it will start
% calculation of DFF
dff = (signal - history.baseval) ./ history.baseval;
data.cursor(history.index) = decoder * dff;
% obtain frequency
freq = cursor_to_audio(data.cursor(history.index), fb_mapping, task_settings.mice_settings.target_low);
data.frequency(history.index) = freq;
end
if counters.update_buffer == 0 && flags.base_buffer_full
% Is it a new trial?
if flags.new_trial && ~flags.back_2_baseline
history.number_trials = history.number_trials + 1;
data.trial_start(history.number_trials) = history.index;
flags.new_trial = false;
counters.tim = cputime;
disp('New Trial!')
if flags.random_stim
flags.stim_done = false;
time_stim = rand *(task_settings.params.duration_trial/2 - 1) + 1;
end
end
if flags.back_2_baseline
if data.cursor(history.index) <= back_2_base
counters.back_2_base = counters.back_2_base + 1;
end
if counters.back_2_base >= task_settings.params.back_2_base_frame_thresh
flags.back_2_baseline = false;
counters.back_2_base = 0;
disp('back to baseline')
end
else
a.playTone("D11", freq, 1);
data.cursor(history.index)
if data.cursor(history.index) >= T %if it hit the target
if flags.BMI
disp('target hit')
data.trial_end(history.number_trials) = history.index;
history.number_hits = history.number_hits + 1;
data.hits(history.number_hits) = history.index;
if flags.water
history.number_rewards = history.number_rewards + 1;
data.rewards(history.number_rewards) = history.index;
% give water reward
a.writeDigitalPin("D10", 1); pause(task_settings.params.water_time); a.writeDigitalPin("D10",0)
disp(['Trial: ', num2str(history.number_trials), 'Water rewards: ', num2str(history.number_rewards)]);
elseif flags.stim_target
history.number_stims = history.number_stims + 1;
data.stims(history.number_stims) = history.index;
% send stim!! TODO
a.writeDigitalPin("D11", 1); pause(task_settings.params.stim_pulse); a.writeDigitalPin("D11",0)
disp(['Trial: ', num2str(history.number_trials), 'Stim rewards: ', num2str(history.number_stims)]);
end
counters.update_buffer = task_settings.relaxation_frames;
flags.back_2_baseline = true;
flags.new_trial = true;
else
% do nothing for now. Available to implement later
end
elseif (cputime - counters.tim) > task_settings.params.trial_max_time
disp('Timeout')
data.trial_end(history.number_trials) = history.index;
history.number_miss = history.number_miss + 1;
data.miss(history.number_miss) = history.index;
a.playTone("D11", 0, task_settings.params.timeout);
flags.new_trial = true;
counters.update_buffer = task_settings.timeout_frames;
end
if ~flags.new_trial
if flags.random_stim && ~ flags.stim_done
if (cputime - counters.tim) > time_stim
disp('random stim')
%TODO!!! send stim
a.writeDigitalPin("D11", 1); pause(task_settings.params.stim_pulse); a.writeDigitalPin("D11",0)
history.number_stims = history.number_stims + 1;
data.stims(history.number_stims) = history.index;
flags.stim_done = true;
end
end
end
if flags.stim_time
% sends stim if the cursor is approaching the target
if data.cursor(history.index) >= block_val
disp('timed stim')
a.writeDigitalPin("D11", 1); pause(task_settings.params.stim_pulse); a.writeDigitalPin("D11",0)
%TODO send stim
if ~flags.stim_done
history.number_stims = history.number_stims + 1;
data.stims(history.number_stims) = history.index;
flags.stim_done = true;
end
else
flags.stim_done = false;
end
end
end
else
if counters.update_buffer>0
counters.update_buffer = counters.update_buffer - 1;
end
end
else
if counters.do_not_update_buffer>0
counters.do_not_update_buffer = counters.do_not_update_buffer - 1;
end
end
data.time_vector(history.index) = cputime - counters.tim_frame;
else
% do nothing (for now)
end
end