-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewardConditioning_old.m
806 lines (581 loc) · 38 KB
/
RewardConditioning_old.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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
%% HG 2021
function RewardConditioning_old()
% SETUP
% You will need:
% - A Bpod.
% > Port#1: Lickport, DI/O
% > Port#2: Pole (LED channel)
global BpodSystem S;
%% Define parameters
S = BpodSystem.ProtocolSettings; % Load settings chosen in launch manager into current workspace as a struct called S
if isempty(fieldnames(S)) % If settings file was an empty struct, populate struct with default settings
S.GUI.WaterValveTime = 0.05; % in sec
S.GUI.PreSamplePeriod = 0.5; % in sec
S.GUI.SamplePeriod = 0.05; % in sec
S.GUI.Delay = 0.2; % in sec
S.GUI.AnswerPeriod = 1.5; % in sec
S.GUI.ConsumptionPeriod = 1.5; % in sec
S.GUI.StopLickingPeriod = 0.5; % in sec
S.GUI.TimeOut = 4; % in sec
S.GUIPanels.Behaviour= {'WaterValveTime', 'PreSamplePeriod', 'SamplePeriod', 'Delay', 'AnswerPeriod',...
'ConsumptionPeriod', 'StopLickingPeriod', 'TimeOut'};
%S.GUI.NoGoPosition = 7e4;
S.GUI.Position = 2e4;
S.GUI.MotorMoveTime = 2;
S.GUI.APMotorPosition = 1.5;
S.GUI.LateralPolePosition = 1e5;
S.GUIPanels.PolePositions = {'Position', 'MotorMoveTime', 'APMotorPosition', 'LateralPolePosition'};
S.GUIMeta.ProtocolType.Style = 'popupmenu'; % protocol type selection
S.GUIMeta.ProtocolType.String = {'Water_Valve_Calibration', 'Licking', ... case 1,2
'Pole_Delay_Reward', 'Pole_Trace_Delay_Reward', ... case 3,4
'Pole_Delay_Response', 'Pole_Trace_Delay_Response', ... case 5,6
'Pole_Nolick_Delay_Reward', 'Pole_Nolick_Delay_Reward' }; % case 7,8
S.GUI.ProtocolType = 3;
S.GUIPanels.Protocol= {'ProtocolType'};
end
% Initialize parameter GUI plugin
BpodParameterGUI('init', S);
% Sync the protocol selections
p = cellfun(@(x) strcmp(x,'ProtocolType'),BpodSystem.GUIData.ParameterGUI.ParamNames);
set(BpodSystem.GUIHandles.ParameterGUI.Params(p),'callback',{@manualChangeProtocol, S})
% Initiate motor
initiateZaberMotor;
% Setup manual motor inputs
p = cellfun(@(x) strcmp(x,'APMotorPosition'),BpodSystem.GUIData.ParameterGUI.ParamNames);
set(BpodSystem.GUIHandles.ParameterGUI.Params(p),'callback',{@manualMoveZaberMotor,'1'})
p = cellfun(@(x) strcmp(x,'LateralPolePosition'),BpodSystem.GUIData.ParameterGUI.ParamNames);
set(BpodSystem.GUIHandles.ParameterGUI.Params(p),'callback',{@manualMoveZaberMotor,'2'})
% Move motors to current values from config file
p = cellfun(@(x) strcmp(x,'APMotorPosition'),BpodSystem.GUIData.ParameterGUI.ParamNames);
anterior_pole_position = get(BpodSystem.GUIHandles.ParameterGUI.Params(p),'String');
move_absolute(motors,str2double(anterior_pole_position),1);
p = cellfun(@(x) strcmp(x,'LateralPolePosition'),BpodSystem.GUIData.ParameterGUI.ParamNames);
lateral_pole_position = get(BpodSystem.GUIHandles.ParameterGUI.Params(p),'String');
move_absolute(motors,str2double(lateral_pole_position),2);
%% Define trials
MaxTrials = 9999;
TrialTypes = [];
BpodSystem.Data.TrialTypes = []; % The trial type of each trial completed will be added here.
BpodSystem.Data.TrialOutcomes = []; % The trial outcomes
%% Initialise plots
BpodSystem.ProtocolFigures.PerfOutcomePlotFig = figure('Position', [150 800 1600 200], ...
'name','Outcome plot','numbertitle','off', 'MenuBar', 'none', 'Resize', 'off', 'Color', [1 1 1]);
BpodSystem.GUIHandles.PerfOutcomePlot = axes('Position', [.15 .2 .8 .7], 'FontSize', 11);
uicontrol('Style', 'text', 'String', 'nDisplay: ','Position',[20 170 100 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
BpodSystem.GUIHandles.DisplayNTrials = uicontrol('Style','edit','string','100','Position',[125 170 40 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
uicontrol('Style', 'text', 'String', 'hit % (all): ','Position',[20 140 100 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
BpodSystem.GUIHandles.hitpct = uicontrol('Style','text','string','0','Position',[125 140 40 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
uicontrol('Style', 'text', 'String', 'hit % (40): ','Position',[20 120 100 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
BpodSystem.GUIHandles.hitpctrecent = uicontrol('Style','text','string','0','Position',[125 120 40 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
uicontrol('Style', 'text', 'String', 'Trials: ','Position',[20 40 100 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
BpodSystem.GUIHandles.numtrials = uicontrol('Style','text','string','0','Position',[125 40 40 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
uicontrol('Style', 'text', 'String', 'Rewards: ','Position',[20 20 100 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
BpodSystem.GUIHandles.numRewards = uicontrol('Style','text','string','0','Position',[125 20 40 18], ...
'HorizontalAlignment', 'left', 'BackgroundColor', [1 1 1], 'FontSize', 11);
PerfOutcomePlot(BpodSystem.GUIHandles.PerfOutcomePlot,1,'init',0);
% Pause the protocol before starting
BpodSystem.Status.Pause = 1;
HandlePauseCondition;
% Define outputs
io.WaterOutput = {'ValveState',2^0}; % Valve 1 open
io.PoleOutput = {'PWM2',255}; % Behavioural port 2, LED pin
io.AcqTrig = {'BNC1', 1};
io.Bitcode = {'BNC2', 1};
io.CameraTrig = {'WireState', 1};
%% Main trial loop
% Move motor into position
moveZaberMotors(1);
for currentTrial = 1 : MaxTrials
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin
disp(['Starting trial ',num2str(currentTrial)])
TrialTypes(currentTrial) = 1; % only one kind of trial
PerfOutcomePlot(BpodSystem.GUIHandles.PerfOutcomePlot,currentTrial,'next_trial',TrialTypes(currentTrial), BpodSystem.GUIHandles.DisplayNTrials);
sma = NewStateMatrix(); % Assemble state matrix
% Build state matrix depending on the protocol type
switch S.GUI.ProtocolType
%%
case 1 % Water_Valve_Calibration
%---------------------------------------------------------
% Water_Valve_Calibration when sensor is touched, release 50 drops every 0.5 seconds
ndrops = 50; interval = 0.5;
sma = AddState(sma, 'Name', 'TrigTrialStart', ...
'Timer', interval, ...
'StateChangeConditions', {'Port1In', 'Drop1_Openvalve', 'Tup', 'TrialEnd'}, ...
'OutputActions', {});
for i_drop = 1:ndrops-1
sma = AddState(sma, 'Name', ['Drop',num2str(i_drop),'_Openvalve'], ...
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', ['Drop',num2str(i_drop),'_Closevalve']}, ...
'OutputActions', io.WaterOutput);
sma = AddState(sma, 'Name', ['Drop',num2str(i_drop),'_Closevalve'], ...
'Timer', interval, ...
'StateChangeConditions', {'Tup', ['Drop',num2str(i_drop+1),'_Openvalve']}, ...
'OutputActions', {});
end
sma = AddState(sma, 'Name', ['Drop',num2str(ndrops),'_Openvalve'], ...
'Timer', S.GUI.WaterValveTime,...
'StateChangeConditions', {'Tup', 'TrialEnd'}, ...
'OutputActions', io.WaterOutput);
sma = AddState(sma, 'Name', 'TrialEnd', ...
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 2 % Licking
%---------------------------------------------------------
% Each lick is rewarded with reward
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'WaitForLick', ...
'Timer', 300,...
'StateChangeConditions', {'Port1In', 'Reward', 'Tup', 'NoResponse'}, ...
'OutputActions', {});
sma = AddState(sma, 'Name', 'Reward', ...
'Timer', S.GUI.WaterValveTime,...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', io.WaterOutput);
sma = AddState(sma, 'Name', 'NoResponse', ...
'Timer', 0.05,...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 3 % 'Pole_Delay_Reward'
%---------------------------------------------------------
% Stim-Pole comes in, Reward is delivered after Delay
% No punishment and no reqt for licking
% Pole stays in throughout
LickAction = ''; % licking changes nothing
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ...
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole still in.. keep waiting
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Tup', 'Reward'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.PoleOutput io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % pole out and trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 4 % 'Pole_Trace_Delay_Reward'
%---------------------------------------------------------
% Stim-Pole comes in, stays only for sample period
% Reward is delivered after Delay
% No punishment and no reqt for licking
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ...
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole out and wait..
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Tup', 'Reward'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 5 % 'Pole_Delay_Response'
%---------------------------------------------------------
% operant conditioning
% reward is delivered after delay only if there is licking
% in answer period
% only go trials
LickAction = 'Reward';
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ... % pre-sample
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole in
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Tup', 'AnswerPeriod'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'AnswerPeriod', ... % pole still in and wait for response
'Timer', S.GUI.AnswerPeriod, ...
'StateChangeConditions', {'Port1In', LickAction, 'Tup', 'NoResponse'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.PoleOutput io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'NoResponse', ... % no response
'Timer', 0.002, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 6 % 'Pole_Trace_Delay_Response'
%---------------------------------------------------------
% operant conditioning
% reward is delivered after delay only if there is licking
% in answer period
% only go trials
LickAction = 'Reward';
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ... % pre-sample
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole out and wait
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Tup', 'AnswerPeriod'}, ...
'OutputActions', [ io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'AnswerPeriod', ... % and wait for response
'Timer', S.GUI.AnswerPeriod, ...
'StateChangeConditions', {'Port1In', LickAction, 'Tup', 'NoResponse'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'NoResponse', ... % no response
'Timer', 0.002, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 7 % Pole_Nolick_delay_Reward',
%---------------------------------------------------------
% prevent anticipatory licking (< 3 licks in 50 ms) ->
% restarts delay for fewer licks, else no reward
LickAction = 'Reward';
EarlyLickAction = 'StopLicking'; % no reward for early lick
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ...
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in, deliver reward
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole in
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Port1In', 'Delay_lick2', 'Tup', 'Reward'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay_lick2', ... % pole in
'Timer', 0.025, ...
'StateChangeConditions', {'Port1In', 'Delay_lick3', 'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay_lick3', ... % pole in
'Timer', 0.025, ...
'StateChangeConditions', {'Port1In', EarlyLickAction, 'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.PoleOutput io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % pole out and trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
%%
case 8 % Pole_Nolick_delay_Response
%---------------------------------------------------------
% timed response (delay operant conditioning)
LickAction = 'Reward';
EarlyLickAction = 'StopLicking'; % no reward for early lick
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
sma = AddState(sma, 'Name', 'TrigTrialStart', ...
'Timer', S.GUI.PreSamplePeriod, ...
'StateChangeCondition',{'Tup', 'SamplePeriod'}, ...
'OutputActions', [io.AcqTrig io.CameraTrig]);
% Add bitcode here
sma = AddBitcode(sma, currentTrial, io.Bitcode, [io.AcqTrig io.CameraTrig], 'SamplePeriod');
sma = AddState(sma, 'Name', 'SamplePeriod', ... % pole in, deliver reward
'Timer', S.GUI.SamplePeriod, ...
'StateChangeConditions', {'Tup', 'Delay'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Delay', ... % pole in
'Timer', S.GUI.Delay, ...
'StateChangeConditions', {'Port1In', EarlyLickAction, 'Tup', 'AnswerPeriod'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'AnswerPeriod', ... % wait for response
'Timer', S.GUI.AnswerPeriod, ...
'StateChangeConditions', {'Port1In', LickAction, 'Tup', 'NoResponse'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'Reward', ... % turn on water
'Timer', S.GUI.WaterValveTime, ...
'StateChangeConditions', {'Tup', 'RewardConsumption'}, ...
'OutputActions', [io.PoleOutput io.WaterOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'RewardConsumption', ... % reward consumption
'Timer', S.GUI.ConsumptionPeriod, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'NoResponse', ... % no response
'Timer', 0.002, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLicking', ... % stop licking before advancing to next trial
'Timer', S.GUI.StopLickingPeriod, ...
'StateChangeConditions', {'Port1In', 'StopLickingReturn', 'Tup', 'TrialEnd'}, ...
'OutputActions', [io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'StopLickingReturn', ... % return to stop licking
'Timer', 0.01, ...
'StateChangeConditions', {'Tup', 'StopLicking'}, ...
'OutputActions',[io.PoleOutput io.AcqTrig io.CameraTrig]);
sma = AddState(sma, 'Name', 'TrialEnd', ... % pole out and trial end
'Timer', 0.05, ...
'StateChangeConditions', {'Tup', 'exit'}, ...
'OutputActions', {});
end
%%
SendStateMatrix(sma);
RawEvents = RunStateMatrix; % this step takes a long time and variable (seem to wait for GUI to update, which takes a long time)
if ~isempty(fieldnames(RawEvents)) % If trial data was returned
BpodSystem.Data = AddTrialEvents(BpodSystem.Data,RawEvents); % Computes trial events from raw data
BpodSystem.Data.TrialSettings(currentTrial) = S; % Add the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Add the trial type of the current trial to data
if S.GUI.ProtocolType > 2
UpdatePerfOutcomePlot(TrialTypes, BpodSystem.Data);
end
SaveBpodSessionData; % Saves the field BpodSystem.Data to the current data file
BpodSystem.ProtocolSettings = S;
SaveProtocolSettings(S); % SaveBpodProtocolSettings;
end
% Pause the protocol before starting if in Water-Valve-Calibration
if S.GUI.ProtocolType == 1
BpodSystem.Status.Pause = 1;
end
HandlePauseCondition; % Checks to see if the protocol is paused. If so, waits until user resumes.
if BpodSystem.Status.BeingUsed == 0
return
end
end
end
%%
function UpdatePerfOutcomePlot(TrialTypes, Data)
global BpodSystem
Outcomes = zeros(1,Data.nTrials);
for x = 1:Data.nTrials
if Data.TrialSettings(x).GUI.ProtocolType > 2
% reward outcome = 1
if ~isnan(Data.RawEvents.Trial{x}.States.Reward(1))
Outcomes(x) = 1; % correct
BpodSystem.Data.TrialOutcomes(x) = 1;
else
Outcomes(x) = 0; % no reward
BpodSystem.Data.TrialOutcomes(x) = 0;
end
end
% no response outcome = 2
if Data.TrialSettings(x).GUI.ProtocolType==5 || Data.TrialSettings(x).GUI.ProtocolType==6 || Data.TrialSettings(x).GUI.ProtocolType==8
% no response
if ~isnan(Data.RawEvents.Trial{x}.States.NoResponse(1))
Outcomes(x) = 2; % no reward
BpodSystem.Data.TrialOutcomes(x) = 2;
end
end
end
PerfOutcomePlot(BpodSystem.GUIHandles.PerfOutcomePlot,Data.nTrials,'update', ...
TrialTypes, BpodSystem.GUIHandles.DisplayNTrials, Outcomes);
end
%%
function PerfOutcomePlot(ax, Ntrials, action, varargin)
global BpodSystem
sz = 10;
switch action
case 'update'
types = varargin{1};
displayHand = varargin{2};
outcomes = varargin{3};
Ndisplay = str2double(get(displayHand, 'String'));
toPlot = false(1, Ntrials);
ind1 = max(1, Ntrials-Ndisplay+1);
ind2 = Ntrials;
toPlot(ind1:ind2) = true;
miss = (outcomes == 0);
hit = (outcomes == 1);
noresponse = (outcomes == 2);
hold(ax, 'off');
xdat = find(toPlot&hit);
plot(ax, xdat, outcomes(xdat), 'go', 'MarkerSize', sz); hold(ax, 'on');
xdat = find(toPlot&miss);
plot(ax, xdat, outcomes(xdat), 'ro', 'MarkerSize', sz);
xdat = find(toPlot&noresponse);
plot(ax, xdat, outcomes(xdat), 'kx', 'MarkerSize', sz);
hitpct = 100.*sum(hit)./Ntrials;
ind40 = max(1, Ntrials-40+1):Ntrials;
hitpctrecent = 100.*sum(hit(ind40))./numel(ind40);
set(BpodSystem.GUIHandles.hitpct, 'String', num2str(hitpct));
set(BpodSystem.GUIHandles.hitpctrecent, 'String', num2str(hitpctrecent));
set(BpodSystem.GUIHandles.numtrials, 'String', num2str(Ntrials));
xlim(ax, [ind1 ind1+Ndisplay-1+5]);
ylim(ax, [0 3]);
case 'next_trial'
currentType = varargin{1};
displayHand = varargin{2};
Ndisplay = str2double(get(displayHand, 'String'));
ind1 = max(1, Ntrials-Ndisplay+1);
% ind2 = Ntrials;
hold(ax, 'on');
plot(ax, Ntrials, currentType+1, 'ko', 'MarkerSize', sz);
xlim(ax, [ind1 ind1+Ndisplay-1+5]);
end
set(ax, 'YTick', [0 1 2], 'YTickLabel', {'EarlyLick'; 'Go', 'NoResp'});
end
%%
% function UpdateTotalRewardDisplay(RewardAmount, currentTrial)
% % If Rewarded based on the state data, update the TotalRewardDisplay
% global BpodSystem
%
% if ~isnan(BpodSystem.Data.RawEvents.Trial{currentTrial}.States.Reward(1))
%
% TotalRewardDisplay('add', RewardAmount);
%
% end
%
% end
%% Assemble Bitcode
function state_matrix = AddBitcode(state_matrix_in, trial_num, bitcodeCH_output, otherCH_output, next_state_name)
state_matrix = state_matrix_in;
bit_time = 0.002; % bit time
gap_time = 0.008; % gap (inter-bit) time
num_bits = 10; % 2^10 = 1024 possible trial nums
x = double(dec2binvec(trial_num)');
if length(x) < num_bits
x = [x; zeros([num_bits-length(x) 1])];
end
x = double(x); % x is now 10-bit vector giving trial num.
for i_bit = 1:num_bits
if x(i_bit)==1
output_bitONState = [bitcodeCH_output otherCH_output];
else
output_bitONState = otherCH_output;
end
state_matrix = AddState(state_matrix, 'Name', ['Bitcode_bit',num2str(i_bit),'_ON'], ... % incorrect response
'Timer', bit_time,...
'StateChangeConditions', {'Tup', ['Bitcode_bit',num2str(i_bit),'_OFF']},...
'OutputActions', output_bitONState);
if i_bit < num_bits
state_matrix = AddState(state_matrix, 'Name', ['Bitcode_bit',num2str(i_bit),'_OFF'], ... % incorrect response
'Timer', gap_time,...
'StateChangeConditions', {'Tup', ['Bitcode_bit',num2str(i_bit+1),'_ON']},...
'OutputActions', otherCH_output);
else
state_matrix = AddState(state_matrix, 'Name', ['Bitcode_bit',num2str(i_bit),'_OFF'], ... % incorrect response
'Timer', gap_time,...
'StateChangeConditions', {'Tup', next_state_name},...
'OutputActions', otherCH_output);
end
end
end