-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCuedOutcome_Sensors.m
233 lines (210 loc) · 9.44 KB
/
CuedOutcome_Sensors.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
function CuedOutcome_Sensors
%Functions used in this protocol:
%"CuedReward_Phase": specify the phase of the training
%"WeightedRandomTrials" : generate random trials sequence
%"Online_LickPlot" : initialize and update online lick and outcome plot
%"Online_LickEvents" : extract the data for the online lick plot
%"Online_NidaqPlot" : initialize and update online nidaq plot
%"Online_NidaqEvents" : extract the data for the online nidaq plot
global BpodSystem nidaq S
%% Define parameters
S = BpodSystem.ProtocolSettings; % Load settings chosen in launch manager into current workspace as a struct called S
ParamPC=BpodParam_PCdep();
if isempty(fieldnames(S)) % If settings file was an empty struct, populate struct with default settings
CuedOutcome_Sensors_TaskParameters(ParamPC);
end
% Initialize parameter GUI plugin and Pause
BpodParameterGUI('init', S);
BpodSystem.Pause=1;
HandlePauseCondition;
S = BpodParameterGUI('sync', S);
%% Outcome
S.SmallRew = GetValveTimes(S.GUI.SmallReward, S.GUI.RewardValve);
S.InterRew = GetValveTimes(S.GUI.InterReward, S.GUI.RewardValve);
S.LargeRew = GetValveTimes(S.GUI.LargeReward, S.GUI.RewardValve);
%% Define stimuli and send to sound server
CueA=SoundGenerator(S.GUI.SoundSamplingRate,S.GUI.CueA,1,1,S.GUI.CueDuration,0);
CueB=SoundGenerator(S.GUI.SoundSamplingRate,S.GUI.CueB,1,1,S.GUI.CueDuration,0);
CueC=SoundGenerator(S.GUI.SoundSamplingRate,S.GUI.CueC,1,1,S.GUI.CueDuration,0);
CueD=SoundGenerator(S.GUI.SoundSamplingRate,S.GUI.CueD,1,1,S.GUI.CueDuration,0);
NoCue=zeros(1,S.GUI.CueDuration*S.GUI.SoundSamplingRate);
PsychToolboxSoundServer('init');
PsychToolboxSoundServer('Load', 1, CueA);
PsychToolboxSoundServer('Load', 2, CueB);
PsychToolboxSoundServer('Load', 3, CueC);
PsychToolboxSoundServer('Load', 4, CueD);
PsychToolboxSoundServer('Load', 5, NoCue);
BpodSystem.SoftCodeHandlerFunction = 'SoftCodeHandler_PlaySound';
%% Define trial types parameters, trial sequence and Initialize plots
[S.TrialsNames, S.TrialsMatrix]=CuedOutcome_Sensors_Phase(S,S.Names.Phase{S.GUI.Phase});
TrialSequence=WeightedRandomTrials(S.TrialsMatrix(:,2)', S.GUI.MaxTrials);
S.NumTrialTypes=max(TrialSequence);
FigLick=Online_LickPlot('ini',TrialSequence);
%% NIDAQ Initialization amd Plots
if S.GUI.Photometry || S.GUI.Wheel
if (S.GUI.DbleFibers+S.GUI.Isobestic405+S.GUI.RedChannel)*S.GUI.Photometry >1
disp('Error - Incorrect photometry recording parameters')
return
end
Nidaq_photometry('ini',ParamPC);
end
if S.GUI.Photometry
FigNidaq1=Online_NidaqPlot('ini','470');
if S.GUI.DbleFibers || S.GUI.Isobestic405 || S.GUI.RedChannel
FigNidaq2=Online_NidaqPlot('ini','channel2');
end
end
if S.GUI.Wheel
FigWheel=Online_WheelPlot('ini');
end
%% Main trial loop
BpodSystem.Data.TrialTypes = []; % The trial type of each trial completed will be added here.
for currentTrial = 1:S.GUI.MaxTrials
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin
%% Initialize current trial parameters
S.Cue = S.TrialsMatrix(TrialSequence(currentTrial),3);
S.Delay = S.TrialsMatrix(TrialSequence(currentTrial),4)+(S.GUI.DelayIncrement*(currentTrial-1));
S.Valve = S.TrialsMatrix(TrialSequence(currentTrial),5);
S.Outcome = S.TrialsMatrix(TrialSequence(currentTrial),6);
if S.GUI.Phase==3 % L3-SecondaryCue
S.Cue = 3;
S.ExtraDelay = S.TrialsMatrix(TrialSequence(currentTrial),4);
S.ExtraCueDuration = S.GUI.CueDuration;
S.ExtraCue = S.TrialsMatrix(TrialSequence(currentTrial),3);
else
S.ExtraDelay = 0;
S.ExtraCueDuration = 0;
S.ExtraCue = 0;
end
S.ITI = 100;
while S.ITI > 6 * S.GUI.ITI
S.ITI = exprnd(S.GUI.ITI);
end
%% Assemble State matrix
sma = NewStateMatrix();
sma = AddState(sma,'Name', 'ITI',...
'Timer',S.ITI,...
'StateChangeConditions', {'Tup', 'PreState'},...
'OutputActions',{});
%Pre task states
sma = AddState(sma, 'Name','PreState',...
'Timer',S.GUI.PreCue,...
'StateChangeConditions',{'Tup','CueDelivery'},...
'OutputActions',{'BNCState',1});
%Cue
sma=AddState(sma,'Name', 'CueDelivery',...
'Timer',S.GUI.CueDuration,...
'StateChangeConditions',{'Tup', 'Delay'},...
'OutputActions', {'SoftCode',S.Cue});
%Delay
sma=AddState(sma,'Name', 'Delay',...
'Timer',S.Delay,...
'StateChangeConditions',{'Tup', 'ExtraCueDelivery'},...
'OutputActions',{});
%Extra Cue for L3-SecondaryCue
sma=AddState(sma,'Name', 'ExtraCueDelivery',...
'Timer',S.ExtraCueDuration,...
'StateChangeConditions', {'Tup', 'ExtraDelay'},...
'OutputActions', {'SoftCode',S.ExtraCue});
%Extra Delay for L3-SecondaryCue
sma=AddState(sma,'Name', 'ExtraDelay',...
'Timer',S.ExtraDelay,...
'StateChangeConditions',{'Tup', 'Outcome'},...
'OutputActions',{});
%Reward
sma=AddState(sma,'Name', 'Outcome',...
'Timer',S.Outcome,...
'StateChangeConditions', {'Tup', 'PostOutcome'},...
'OutputActions', {'ValveState', S.Valve});
%Post task states
sma=AddState(sma,'Name', 'PostOutcome',...
'Timer',S.GUI.PostOutcome,...
'StateChangeConditions',{'Tup', 'exit'},...
'OutputActions',{});
SendStateMatrix(sma);
%% NIDAQ Get nidaq ready to start
if S.GUI.Photometry || S.GUI.Wheel
Nidaq_photometry('WaitToStart');
end
RawEvents = RunStateMatrix;
%% NIDAQ Stop acquisition and save data in bpod structure
if S.GUI.Photometry || S.GUI.Wheel
Nidaq_photometry('Stop');
[PhotoData,WheelData,Photo2Data]=Nidaq_photometry('Save');
if S.GUI.Photometry
BpodSystem.Data.NidaqData{currentTrial}=PhotoData;
if S.GUI.DbleFibers || S.GUI.RedChannel
BpodSystem.Data.Nidaq2Data{currentTrial}=Photo2Data;
end
end
if S.GUI.Wheel
BpodSystem.Data.NidaqWheelData{currentTrial}=WheelData;
end
end
%% Save
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; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialSequence(currentTrial); % Adds the trial type of the current trial to data
SaveBpodSessionData; % Saves the field BpodSystem.Data to the current data file
end
%% PLOT - extract events from BpodSystem.data and update figures
try
[currentOutcome, currentLickEvents]=Online_LickEvents(S.Names.StateToZero{S.GUI.StateToZero});
FigLick=Online_LickPlot('update',[],FigLick,currentOutcome,currentLickEvents);
if S.GUI.Photometry
[currentNidaq1, rawNidaq1]=Online_NidaqDemod(PhotoData(:,1),nidaq.LED1,S.GUI.LED1_Freq,S.GUI.LED1_Amp,S.Names.StateToZero{S.GUI.StateToZero});
currentNidaq1=CuedOutcome_Sensors_VariableITIAVG(currentNidaq1);
FigNidaq1=Online_NidaqPlot('update',[],FigNidaq1,currentNidaq1,rawNidaq1);
if S.GUI.Isobestic405 || S.GUI.DbleFibers || S.GUI.RedChannel
if S.GUI.Isobestic405
[currentNidaq2, rawNidaq2]=Online_NidaqDemod(PhotoData(:,1),nidaq.LED2,S.GUI.LED2_Freq,S.GUI.LED2_Amp,S.Names.StateToZero{S.GUI.StateToZero});
elseif S.GUI.RedChannel
[currentNidaq2, rawNidaq2]=Online_NidaqDemod(Photo2Data(:,1),nidaq.LED2,S.GUI.LED2_Freq,S.GUI.LED2_Amp,S.Names.StateToZero{S.GUI.StateToZero});
elseif S.GUI.DbleFibers
[currentNidaq2, rawNidaq2]=Online_NidaqDemod(Photo2Data(:,1),nidaq.LED2,S.GUI.LED1b_Freq,S.GUI.LED1b_Amp,S.Names.StateToZero{S.GUI.StateToZero});
end
currentNidaq2=CuedOutcome_Sensors_VariableITIAVG(currentNidaq2);
FigNidaq2=Online_NidaqPlot('update',[],FigNidaq2,currentNidaq2,rawNidaq2);
end
end
if S.GUI.Wheel
FigWheel=Online_WheelPlot('update',FigWheel,WheelData,S.Names.StateToZero{S.GUI.StateToZero},currentTrial,currentLickEvents);
end
catch
disp('Oups, something went wrong with the online analysis... May be you closed a plot ?')
end
%% Photometry QC
if currentTrial==1 && S.GUI.Photometry
thismax=max(PhotoData(S.GUI.NidaqSamplingRate:S.GUI.NidaqSamplingRate*2,1))
if thismax>4 || thismax<0.5
disp('WARNING - Something is wrong with fiber #1 - run check-up! - unpause to ignore')
BpodSystem.Pause=1;
HandlePauseCondition;
end
if S.GUI.DbleFibers
thismax=max(Photo2Data(S.GUI.NidaqSamplingRate:S.GUI.NidaqSamplingRate*2,1))
if thismax>4 || thismax<0.5
disp('WARNING - Something is wrong with fiber #2 - run check-up! - unpause to ignore')
BpodSystem.Pause=1;
HandlePauseCondition;
end
end
end
%% End of trial
HandlePauseCondition; % Checks to see if the protocol is paused. If so, waits until user resumes.
if BpodSystem.BeingUsed == 0
return
end
end
%% Add killer script + sarah's behavior QC code here + Quick Analysis
try
ChannelNames={'BLA - Sensor','TdTomato','VS - Sensor'};
YPhoto=[S.GUI.NidaqMin S.GUI.NidaqMax];
Analysis=Analysis_Photometry_Launcher_PostRec(BpodSystem,ChannelNames,YPhoto);
% Figure handle is in Analysis.Figure.PostRec
AP_Sensors_Evernote(Analysis,FigLick.water)
catch
disp('Post recording analysis failed - check whether analysis pipeline is present')
end
end