-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_parameters.m
783 lines (644 loc) · 30.3 KB
/
update_parameters.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
function update_parameters
% UPDATE_PARAMETERS Update parameters at each trial.
global association_flag response_window trial_duration quiet_window lick_threshold...
artifact_window iti camera_flag is_stim is_auditory is_whisker is_light is_opto...
reward_valve_duration aud_reward wh_reward wh_vec aud_vec light_vec ...
light_prestim_delay stim_flag perf lick_flag ...
false_alarm_punish_flag false_alarm_timeout early_lick_punish_flag early_lick_timeout ...
Stim_S Log_S wh_stim_duration aud_stim_duration aud_stim_amp aud_stim_freq Stim_S_SR ScansTobeAcquired ...
Reward_S Reward_S_SR Trigger_S fid_lick_trace mouse_licked_flag reaction_time ...
trial_started_flag trial_number main_pool_size_old opto_stim_proba_old folder_name handles2give...
stim_proba_old aud_stim_proba_old wh_stim_proba_old opto_aud_proba_old opto_wh_proba_old opto_ctrl_proba_old...
light_flag baseline_window camera_vec deliver_reward_flag ...
wh_stim_amp wh_scaling_factor response_window_start response_window_end...
perf_and_save_results_flag reward_delivered_flag update_parameters_flag...
is_reward reward_pool partial_reward_flag reward_proba_old...
light_duration light_freq light_amp light_duty camera_freq SITrigger_vec main_trial_pool...
whisker_trial_counter mouse_rewarded_context context_block context_flag block_id wh_rewarded_context...
pink_noise_player brown_noise_player identical_block_count extra_time Context_S...
WF_S wf_cam_vec LED1_vec LED2_vec opto_vec galv_x galv_y ...
passive_stim_flag_enable passive_stim_flag passive_trial_max passive_iti passive_trial_counter is_passive
outputSingleScan(Trigger_S,[0 0 0])
pause(.5)
outputSingleScan(Trigger_S,[0 0 0])
trial_number = trial_number+1;
%% GENERAL SETTINGS FROM BEHAVIOUR GUI
association_flag=handles2give.association_flag; % 0 detection 1 assosiation
light_flag=handles2give.light_flag;
% Context info flag
context_flag = handles2give.context_flag;
context_block_size = handles2give.context_block_size;
% Camera settings
camera_freq=handles2give.camera_freq; % Hz
camera_duty_cycle=0.5;
trial_duration=handles2give.trial_duration; % ms
% Lick sensor threshold
lick_threshold=handles2give.lick_threshold; %in volts
% Trial timeline settings
min_quiet_window = handles2give.min_quiet_window; % in ms
max_quiet_window = handles2give.max_quiet_window; % in ms
if min_quiet_window > max_quiet_window
set(handles2give.OnlineTextTag,'String','Error: Minimum quiet window should be smaller than maximum quiet window!');
elseif min_quiet_window==max_quiet_window
set(handles2give.OnlineTextTag,'String','Warning: quiet window will be constant.');
quiet_window=min_quiet_window;
else
quiet_window=randsample(min_quiet_window:.1:max_quiet_window,1);
end
response_window=handles2give.response_window; % in ms
artifact_window=handles2give.artifact_window; % in ms
baseline_window=handles2give.baseline_window; % in ms
% Inter-trial interval range
min_iti=handles2give.min_iti; % InterStimInterval in ms
max_iti=handles2give.max_iti; % InterStimInterval in ms
if min_iti > max_iti
set(handles2give.OnlineTextTag,'String','Error: Minimum ITI should be smaller than maximum ITI!');
elseif min_iti==max_iti %constant ITI
set(handles2give.OnlineTextTag,'String','Warning: ITI will be constant.');
iti=min_iti;
else
iti=randsample(min_iti:.1:max_iti,1);
end
% Timeout
false_alarm_punish_flag = handles2give.false_alarm_punish_flag;
false_alarm_timeout=handles2give.false_alarm_timeout; % in ms
early_lick_punish_flag = handles2give.early_lick_punish_flag;
early_lick_timeout=handles2give.early_lick_timeout; % in ms
% Stimuli parameters
aud_stim_amp = handles2give.aud_stim_amp;
aud_stim_duration = handles2give.aud_stim_duration;
aud_stim_freq = handles2give.aud_stim_freq;
wh_stim_amp = handles2give.wh_stim_amp_1; % default amplitude, overwritten at stim. definition
wh_stim_duration = handles2give.wh_stim_duration;
wh_scaling_factor = handles2give.wh_scaling_factor;
aud_stim_weight = handles2give.aud_stim_weight;
wh_stim_weight = get_whisker_weight(handles2give);
no_stim_weight = handles2give.no_stim_weight;
if light_flag % If light stimulus, to be implemented
light_amp = handles2give.light_amp;
light_duration = handles2give.light_duration;
light_prestim_delay = handles2give.light_prestim_delay;
light_freq = handles2give.light_freq;
light_duty = handles2give.light_duty;
light_stim_weight = handles2give.light_stim_weight;
end
% Passive stimulation
passive_stim_flag_enable = handles2give.passive_stim_flag_enable;
passive_stim_flag = handles2give.passive_stim_flag;
passive_trial_max = handles2give.passive_trial_max;
passive_iti = handles2give.passive_iti;
is_passive = 0;
if passive_stim_flag_enable
contexts = {'active', 'passive'};
if passive_stim_flag
if passive_trial_counter < passive_trial_max
is_passive = 1;
association_flag = 1; % to exclude from performance and plots
trial_duration = 1000;
quiet_window = 0;
iti = passive_iti;
passive_trial_counter = passive_trial_counter + 1;
context_block = contexts(is_passive+1)
disp(['Passive trial ' num2str(passive_trial_counter)]);
else
is_passive = 0;
association_flag = 0;
passive_stim_flag=0;
context_block = contexts(is_passive+1)
set(handles2give.PassiveOnToggleButton,'Value',0);
handles2give.passive_stim_flag = 0; % this will be reset when toggle button pressed again
disp('End of passive stimulation.')
end
else
context_block = contexts(is_passive+1)
passive_trial_counter = 0; % keep counter
end
else
is_passive=0;
end
%% Compute stimulus probability
stim_proba = (aud_stim_weight + wh_stim_weight)/(aud_stim_weight + wh_stim_weight + no_stim_weight);
%stim_proba = compute_stim_proba(handles2give);
if isempty(stim_proba_old)
stim_proba_old = stim_proba;
end
aud_stim_proba = (aud_stim_weight)/(aud_stim_weight + wh_stim_weight + no_stim_weight);
if isempty(aud_stim_proba_old)
aud_stim_proba_old = aud_stim_proba;
end
wh_stim_proba = (wh_stim_weight)/(aud_stim_weight + wh_stim_weight + no_stim_weight);
if isempty(wh_stim_proba_old)
wh_stim_proba_old = wh_stim_proba;
end
%% Compute opto-stimulus probability
if handles2give.opto_session
global Opto_info
opto_stim_proba = Opto_info.nostim_proba; % proportion of light trials
if isempty(opto_stim_proba_old)
opto_stim_proba_old = opto_stim_proba;
end
opto_aud_proba = Opto_info.aud_proba; % proportion of light trials
if isempty(opto_aud_proba_old)
opto_aud_proba_old = opto_aud_proba;
end
opto_wh_proba = Opto_info.wh_proba; % proportion of light trials
if isempty(opto_wh_proba_old)
opto_wh_proba_old = opto_wh_proba;
end
opto_ctrl_proba = Opto_info.ctrl_proba;
if isempty(opto_ctrl_proba_old)
opto_ctrl_proba_old = opto_ctrl_proba;
end
else
opto_stim_proba = 0;
opto_aud_proba = 0;
opto_wh_proba = 0;
opto_ctrl_proba = 0;
end
%% Define new pool of stimuli
if trial_number > 1
results=readtable(strcat(folder_name, '\results.csv'));
n_completed_trials=sum(results.perf~=6);
else
n_completed_trials=0;
end
% Size of pool (i.e. trial block) to get trials from
% If context is not used:
main_pool_size = aud_stim_weight + wh_stim_weight + no_stim_weight; %in an non-light task
if isempty(main_pool_size_old)
main_pool_size_old = main_pool_size;
end
stim_light_list=[900,901,902,903,904,905,906]; % code for stimuli: stim, aud, wh, opto_stim, opto_aud, opto_wh, opto_ctrl(tbd)
% --- Creation of pseudo-random pool of trials (= block) ---
% Create new trial pool when current pool finished, or, when change in parameters
if mod(n_completed_trials, main_pool_size)==0 || main_pool_size_old ~= main_pool_size||...
stim_proba_old ~= stim_proba || aud_stim_proba_old ~= aud_stim_proba|| wh_stim_proba_old ~= wh_stim_proba ||...
opto_stim_proba_old ~= opto_stim_proba || opto_aud_proba_old ~= opto_aud_proba || opto_wh_proba_old ~= opto_wh_proba ||...
opto_ctrl_proba_old ~= opto_ctrl_proba
% Save old probabilities and pool size
opto_aud_proba_old = opto_aud_proba;
opto_stim_proba_old = opto_stim_proba;
opto_wh_proba_old = opto_wh_proba;
opto_ctrl_proba_old = opto_ctrl_proba; % TBD: proba of trials stimulating in control location vs. no stimulation
aud_stim_proba_old = aud_stim_proba;
wh_stim_proba_old = wh_stim_proba;
stim_proba_old = stim_proba;
main_pool_size_old = main_pool_size;
% Stim. probability and trial pool when opto-stimulus
if handles2give.opto_session
no_stim_opto_proba =(1-stim_proba)*opto_stim_proba;
no_stim_no_opto_proba = (1-stim_proba)*(1-opto_stim_proba);
aud_stim_opto = aud_stim_proba*opto_aud_proba;
aud_stim_no_opto = aud_stim_proba*(1-opto_aud_proba);
wh_stim_opto = wh_stim_proba*opto_wh_proba;
wh_stim_no_opto = wh_stim_proba*(1-opto_wh_proba);
main_trial_pool=[stim_light_list(1)*ones(1,round(round(no_stim_no_opto_proba*main_pool_size*100)/100)),...
stim_light_list(2)*ones(1,round(round(aud_stim_no_opto*main_pool_size*100)/100)),...
stim_light_list(3)*ones(1,round(round(wh_stim_no_opto*main_pool_size*100)/100)),...
stim_light_list(4)*ones(1,round(round(no_stim_opto_proba*main_pool_size*100)/100)),...
stim_light_list(5)*ones(1,round(round(aud_stim_opto*main_pool_size*100)/100)),...
stim_light_list(6)*ones(1,round(round(wh_stim_opto*main_pool_size*100)/100)),...
];
% Stim. probability and trial pool when no opto-stimulus
else
main_trial_pool=[stim_light_list(1)*ones(1,round(round(((1-stim_proba)*main_pool_size)*100)/100)) ,...
stim_light_list(2)*ones(1,round(round(aud_stim_proba*main_pool_size*100)/100)),...
stim_light_list(3)*ones(1,round(round(wh_stim_proba*main_pool_size*100)/100)),...
];
end
%Randomize occurrence of trials in pool
main_trial_pool=main_trial_pool(randperm(numel(main_trial_pool)));
if passive_stim_flag_enable
contexts = {'active', 'passive'};
else
context_block = {'NA'};
end
% if context task
if context_flag
contexts = {'pink', 'brown'};
if trial_number==1
identical_block_count = 1;
[pink_noise_player, brown_noise_player] = create_context_background_noise(handles2give.bckg_noise_directory);
mouse_rewarded_context = get_or_determine_mouse_rewarded_context(handles2give.context_table_directory, handles2give.mouse_name, contexts);
block_id = randi([1, size(contexts, 2)], 1);
context_block = contexts(block_id);
outputSingleScan(Context_S, [0])
if handles2give.context_sound_on
play_context_background(context_block, pink_noise_player, brown_noise_player, Context_S)
else
outputSingleScan(Context_S, [1])
end
else
old_block_id = block_id;
block_id = randi([1, size(contexts, 2)], 1);
if block_id == old_block_id
identical_block_count = identical_block_count + 1;
else
identical_block_count = 1;
end
if identical_block_count > context_block_size % We don't want more than 2 consecutive context A or B block
new_block_id = block_id;
while new_block_id == block_id
new_block_id = randi([1, size(contexts, 2)], 1);
end
block_id = new_block_id;
identical_block_count = 1;
end
context_block = contexts(block_id);
outputSingleScan(Context_S, [0])
if handles2give.context_sound_on
play_context_background(context_block, pink_noise_player, brown_noise_player, Context_S)
else
outputSingleScan(Context_S, [1])
end
end
wh_rewarded_context = strcmp(context_block, mouse_rewarded_context);
else
block_id = 1;
end
end
% --- End of trial pool creation ---
% Select next trial
%trial_type = main_trial_pool(mod(n_completed_trials,main_pool_size)+1); %0 noLight 1 Light
if is_passive
main_trial_pool_passive = main_trial_pool((main_trial_pool~=900) & (main_trial_pool~=903)); % remove no stim trials
main_pool_passive_size = wh_stim_weight + aud_stim_weight;
% trial_type = main_trial_pool_passive(mod(n_completed_trials,main_pool_passive_size)+1); %0 noLight 1 Light
trial_type = randsample(main_trial_pool_passive, 1);
switch trial_type
case stim_light_list(2) % AUDITORY TRIAL
is_stim=1;
is_auditory=1;
is_whisker=0;
is_light=0;
is_opto=0;
case stim_light_list(3) % WHISKER TRIAL
is_stim=1;
is_auditory=0;
is_whisker=1;
is_light=0;
is_opto=0;
whisker_trial_counter=whisker_trial_counter+1; %for proba. reward pool indexing
case stim_light_list(5) % LIGHT AUDITORY TRIAL
is_stim=1;
is_auditory=1;
is_whisker=0;
is_light=0;
is_opto=1;
case stim_light_list(6) % LIGHT WHISKER TRIAL
is_stim=1;
is_auditory=0;
is_whisker=1;
is_light=0;
is_opto=1;
whisker_trial_counter=whisker_trial_counter+1; %for proba. reward pool indexing
end
else
trial_type = main_trial_pool(mod(n_completed_trials,main_pool_size)+1); %0 noLight 1 Light
switch trial_type
case stim_light_list(1) % NO STIM TRIAL
is_stim=0;
is_auditory=0;
is_whisker=0;
is_light=0;
is_opto=0;
case stim_light_list(2) % AUDITORY TRIAL
is_stim=1;
is_auditory=1;
is_whisker=0;
is_light=0;
is_opto=0;
case stim_light_list(3) % WHISKER TRIAL
is_stim=1;
is_auditory=0;
is_whisker=1;
is_light=0;
is_opto=0;
whisker_trial_counter=whisker_trial_counter+1; %for proba. reward pool indexing
case stim_light_list(4) % LIGHT NO STIM TRIAL
is_stim=0;
is_auditory=0;
is_whisker=0;
is_light=0;
is_opto=1;
case stim_light_list(5) % LIGHT AUDITORY TRIAL
is_stim=1;
is_auditory=1;
is_whisker=0;
is_light=0;
is_opto=1;
case stim_light_list(6) % LIGHT WHISKER TRIAL
is_stim=1;
is_auditory=0;
is_whisker=1;
is_light=0;
is_opto=1;
whisker_trial_counter=whisker_trial_counter+1; %for proba. reward pool indexing
end
end
% Set light parameters to zero if not a light trial
if ~is_light || ~light_flag
is_light=0;
light_prestim_delay=0;
light_amp=0;
light_duration=0;
light_freq=0;
end
%% Reward Settings
reward_valve_duration=handles2give.reward_valve_duration; % duration valve open in milliseconds
if handles2give.reward_delay_flag
reward_delay_time=handles2give.reward_delay_time; % delay in milisecond for delivering reward after stim (if Association=1)
else
reward_delay_time = 0;
end
aud_reward = handles2give.aud_reward; % is auditory rewarded
if context_flag
if wh_rewarded_context
wh_reward=1;
else
wh_reward=0;
end
else
wh_reward = handles2give.wh_reward; % is whisker rewarded
end
partial_reward_flag=handles2give.partial_reward_flag;
n_pool_partial = 10;
% PROBABILISTIC reward delivery for whisker
if partial_reward_flag && not(association_flag)
reward_proba=handles2give.reward_proba; % proportion of rewarded whisker hits
if isempty('reward_proba_old')
reward_proba_old = reward_proba;
end
% Update pool of rewarded and unrewarded trials (1s and 0s) -> this
% could be deleted
if all(mod(whisker_trial_counter, n_pool_partial)==1)|| all(reward_proba_old ~= reward_proba) %check if reward_proba has changed
reward_proba_old = reward_proba;
reward_pool=[zeros(1,round((1-reward_proba)*n_pool_partial)) ones(1,round(reward_proba*n_pool_partial))]; % zero for no stim, one for Reward
reward_pool=reward_pool(randperm(numel(reward_pool)));
end
% Set reward flag per trial type
if is_whisker
is_reward = double(rand(1)<reward_proba);
elseif is_auditory
is_reward=aud_reward;
end
% CONSTANT reward delivery (also for association trials)
elseif not(partial_reward_flag) || association_flag
is_reward=1;
end
% Define reward vector
rew_vec_amp = 5; %volt
reward_vec = [zeros(1,reward_delay_time) rew_vec_amp*ones(1,reward_valve_duration*Reward_S_SR/1000) zeros(1,Reward_S_SR/2)];
if ~is_reward || ~is_stim
reward_vec=zeros(1,numel(reward_vec));
end
%% Get trial duration
trial_duration = max(trial_duration, (light_prestim_delay + artifact_window + baseline_window + max(response_window, (light_duration-light_prestim_delay))) /1000);
%% Define stimulus parameters and vectors
% Catch trials, set stimulus vectors to 0
if ~is_stim
aud_stim_duration = 0;
aud_stim_amp = 0;
aud_stim_freq = 0;
wh_stim_duration = 0;
wh_stim_amp = 0;
wh_vec = zeros(1,(trial_duration)*(Stim_S_SR/1000));
aud_vec = zeros(1,(trial_duration)*(Stim_S_SR/1000));
% Stimulus trial parameters and vectors
else
% Sinusoidal auditory stimulus
if is_auditory
aud_vec = aud_stim_amp*[zeros(1,(baseline_window)*Stim_S_SR/1000)...
sin(linspace(0, aud_stim_duration* aud_stim_freq*2*pi/1000, round(aud_stim_duration*Stim_S_SR/1000))) 0];
aud_vec=[aud_vec zeros(1,trial_duration*Stim_S_SR/1000-length(aud_vec))];
% Set whisker vec to zero
wh_stim_duration = 0;
wh_stim_amp = 0;
wh_vec = zeros(1,(trial_duration)*(Stim_S_SR/1000));
% Biphasic cosine whisker stimulus
elseif is_whisker
% Set auditory vector to 0
aud_stim_duration = 0;
aud_stim_amp = 0;
aud_stim_freq = 0;
aud_vec=zeros(1,(trial_duration)*(Stim_S_SR/1000));
wh_stim_duration_up = wh_stim_duration/2*Stim_S_SR/1000;
wh_stim_duration_down = wh_stim_duration/2*Stim_S_SR/1000;
impulse_up = tukeywin(wh_stim_duration_up,1);
impulse_up = impulse_up(1:end-1);
impulse_down = -tukeywin(wh_stim_duration_down,1);
impulse_down = impulse_down(2:end);
impulse = [impulse_up' wh_scaling_factor*impulse_down'];
wh_stim_amp = get_whisker_stim_amp(handles2give);
wh_vec = wh_stim_amp * [zeros(1,baseline_window*Stim_S_SR/1000) impulse];
wh_vec = [wh_vec zeros(1,trial_duration*Stim_S_SR/1000 - numel(wh_vec))];
end
end
% If optogenetics, get opto-stimulus
if handles2give.opto_session
global opto_gui Opto_S variables_to_save_opto voltage_x voltage_y bregma_x bregma_y opto_count
if is_opto
[ML, AP, grid_no, count] = get_next_grid;
power = opto_gui.amplitude;
[opto_vec, galv_x, galv_y] = load_opto_vec(ML,AP);
opto_count = opto_count+1;
else
[opto_vec, galv_x, galv_y] = load_opto_vec(5,-5);
AP = -5;
ML = 5;
power = -1;
grid_no = nan;
count = trial_number - opto_count;
opto_gui.plot_grid
scatter(opto_gui.UIAxes, -5, 5, 'filled', 'MarkerEdgeColor','#DC143C', 'MarkerFaceColor', '#DC143C');
text(opto_gui.UIAxes, 1, 6.5, {['No opto trial: ' num2str(count) '/' num2str(trial_number)]})
end
% Get optogenetic data to save
variables_to_save_opto = {trial_number is_opto is_stim is_auditory is_whisker context_block opto_gui.baseline*1000 power opto_gui.frequency...
opto_gui.duration opto_gui.pulse_width grid_no count AP ML ...
voltage_x voltage_y bregma_x bregma_y};
end
%% Light define vector - To update
if is_light
% disp(['Light' num2str(is_light)])
% time_vec_light = 1/Stim_S_SR : 1/Stim_S_SR : (light_duration/1000);
%
% light_pulse_train=[ones(1,round(Stim_S_SR*(light_duty/light_freq))) zeros(1,round(Stim_S_SR*((1-light_duty)/light_freq)))];
% light_pulse_train=repmat(light_pulse_train,1,light_duration*light_freq/1000);
%
% switch light_stim_shape
% case 'sin'
% light_vec=light_amp/2+light_amp/2*[-ones(1,baseline_window*Stim_S_SR/1000) -ones(1,(light_prestim_delay)*Stim_S_SR/1000) -cos(2*pi*light_freq*time_vec_light)];
% light_vec=[light_vec zeros(1,(trial_duration)*(Stim_S_SR/1000)-numel(light_vec))];
%
% light_vec_shadow= [zeros(1,baseline_window*Stim_S_SR/1000) zeros(1,(light_prestim_delay)*Stim_S_SR/1000) [ones(1,1*length(light_pulse_train)-ramp_down_duration*Stim_S_SR/1000) fliplr(linspace(0,1,ramp_down_duration*Stim_S_SR/1000))]];
% light_vec_shadow =[light_vec_shadow zeros(1,(trial_duration)*(Stim_S_SR/1000)-numel(light_vec_shadow))];
%
% light_vec=light_vec.*light_vec_shadow;
%
% otherwise
% light_vec=light_amp*[(zeros(1,(baseline_window - light_prestim_delay)*Stim_S_SR/1000)) light_pulse_train];
% light_vec=[light_vec zeros(1,(trial_duration)*(Stim_S_SR/1000)-numel(light_vec))];
% %
% light_vec_shadow= [zeros(1,(baseline_window-light_prestim_delay)*Stim_S_SR/1000) [ones(1,1*length(light_pulse_train)-ramp_down_duration*Stim_S_SR/1000) fliplr(linspace(0,1,ramp_down_duration*Stim_S_SR/1000))]];
% light_vec_shadow =[light_vec_shadow zeros(1,(trial_duration)*(Stim_S_SR/1000)-numel(light_vec_shadow))];
%
% light_vec=light_vec.*light_vec_shadow;
%
% end
% No light, set light vector to 0
else
light_vec=zeros(1,(trial_duration)*(Stim_S_SR/1000));
end
%% For Video Filming - Make pulse train for Trigger_S Camera
camera_vec = [ones(1, Stim_S_SR*(camera_duty_cycle/camera_freq)) zeros(1,Stim_S_SR*((1-camera_duty_cycle)/camera_freq))];
camera_vec = repmat(camera_vec, 1, trial_duration*camera_freq/1000);
%% For WF imaging - Load vectors in single trial imaging
if handles2give.wf_session
global WF_FileInfo
if ~WF_FileInfo.RecordingContinuous
wf_imaging
if ~handles2give.opto_session
WF_S.start()
WF_S.write([wf_cam_vec; LED1_vec; LED2_vec;]')
end
end
end
%% Plotting the whisker/auditory stim and camera vector signals
% Set plotting params
acolor = [0 0.4470 0.7410];
acolor_str = '0 0.4470 0.7410';
if wh_reward
wcolor = [0.4660 0.6740 0.1880]';
wcolor_str = '0.4660 0.6740 0.1880';
else
wcolor = [0.6350 0.0780 0.1840];
wcolor_str = '0.6350 0.0780 0.1840';
end
%% Online performance for plotting
if trial_number>1
% Load results data
results=readtable(strcat(folder_name, '\results.csv'));
% Compute performance and display on GUI
[aud_hit_rate, wh_hit_rate, fa_rate, stim_trial_number, aud_stim_number, wh_stim_number] = compute_performance(results);
set(handles2give.PerformanceText1Tag, 'String', ...
['AHR=' num2str(aud_hit_rate) ', ' ...
' WHR=' num2str(wh_hit_rate) ', ' ...
' FAR=' num2str(fa_rate) ', ' ...
' Stim=' num2str(stim_trial_number) ', '...
' AudStim=' num2str(aud_stim_number) ', '...
' WhStim=' num2str(wh_stim_number)], ...
'FontWeight', 'Bold');
% Make performance plot
perf_win_size=handles2give.last_recent_trials;
plot_performance(results, perf_win_size);
% Compute approx. reward volume obtained and display on GUI
volume_per_reward = 5; % in microliter <- THIS MUST BE CALIBRATED
[aud_tot_volume, wh_tot_volume, asso_tot_volume] = compute_reward_volume(results, volume_per_reward);
if wh_reward
wh_tot_volume = wh_tot_volume;
else
wh_tot_volume = 0;
end
set(handles2give.PerformanceText2Tag, 'String', ...
['Reward: Auditory=' num2str(aud_tot_volume) 'uL, ' ...
' Whisker=' num2str(wh_tot_volume) 'uL, ' ...
' Total=' num2str(aud_tot_volume+wh_tot_volume) 'uL, ' ...
' (Asso.=' num2str(asso_tot_volume) 'uL)'], ...
'FontWeight', 'Bold');
end
%% Printing out the next trial specs
trial_titles={'No stimulus', ...
['Amp=' num2str(wh_stim_amp) ', ' 'Duration=' num2str(wh_stim_duration)], ...
['Amp=' num2str(aud_stim_amp) ', ' 'Duration=' num2str(aud_stim_duration) ', ' 'Frequency=' num2str(aud_stim_freq)]};
if is_passive
association_titles={'', ''};
reward_titles={'Passive', 'Passive'};
else
association_titles={'', ' Association'};
reward_titles={'Not rewarded', 'Rewarded'};
end
if is_opto
opto_titles={'Opto OFF', 'Opto ON'};
else
opto_titles={'', ''};
end
if is_stim
if is_auditory
set(handles2give.TrialTimeLineTextTag,'String', ['Next trial: Auditory. ' char(trial_titles(is_stim+2)) ' '...
char(association_titles(association_flag+1)) ' ' char(reward_titles(aud_reward+1)) ' ' char(opto_titles(is_opto+1))],'ForegroundColor',acolor);
else
if is_reward==1 && wh_reward==1 % =1 always when no partial rewards
reward_title = 'Rewarded';
wcolor = [0.4660 0.6740 0.1880]';
else
reward_title = 'Not rewarded';
wcolor = [0.6350 0.0780 0.1840];
end
if is_passive
reward_title = 'Passive';
end
set(handles2give.TrialTimeLineTextTag,'String',['Next trial: Whisker. ' char(trial_titles(is_stim+1)) ' '...
char(association_titles(association_flag+1)) ' ' char(reward_title) ' ' char(opto_titles(is_opto+1))],'ForegroundColor', wcolor);
end
else
set(handles2give.TrialTimeLineTextTag,'String',['Next trial: ' char(trial_titles(is_stim+1)) ' '...
char(association_titles(association_flag+1)) ' ' char(opto_titles(is_opto+1))], 'ForegroundColor','k');
end
%% Parameters are updated: now send signal vectors and triggers
SITrigger_vec=[ones(1,numel(wh_vec)-2) 0 0]; % ScanImage trigger vector
queueOutputData(Stim_S,[wh_vec; aud_vec; camera_vec; SITrigger_vec]')
while Stim_S.IsRunning
disp('Here') %?????
end
Stim_S.startBackground()
outputSingleScan(Trigger_S,[0 0 0])
if handles2give.opto_session && ~handles2give.wf_session
try
queueOutputData(Opto_S, [opto_vec; galv_x; galv_y]')
pause(.1)
catch
disp(['Error preloading Opto_S coords ap ml: ' num2str(AP) ' ' num2str(ML)])
disp(Opto_S)
end
Opto_S.startBackground();
elseif handles2give.opto_session && handles2give.wf_session
try
queueOutputData(Opto_S, [opto_vec(1:end-1); galv_x(1:end-1); galv_y(1:end-1); wf_cam_vec]')
pause(.1)
catch
disp(['Error preloading Opto_S coords ap ml: ' num2str(AP) ' ' num2str(ML)])
disp(Opto_S)
end
Opto_S.startBackground();
end
% Queue reward vector
if ~Reward_S.ScansQueued
queueOutputData(Reward_S, reward_vec')
while Reward_S.IsRunning
disp('Here 2')
end
Reward_S.startBackground();
end
%% Define response window boundaries
response_window_start = (artifact_window + baseline_window)/1000;
response_window_end = (artifact_window + baseline_window + response_window)/1000;
%% Update current trial flags
handles2give.ReportPause=1; %not currently paused
trial_started_flag=0;
stim_flag=1;
deliver_reward_flag=0;
perf_and_save_results_flag=0;
mouse_licked_flag=0;
reward_delivered_flag=0;
update_parameters_flag=0;
% Reset variables
reaction_time=0;
% Give user extra time to start video and 2P acquisition before 1st trial start.
% Added to iti condition for trial start in main_control.
if trial_number == 1
extra_time = 10;
else
extra_time = 0;
end
end