-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoStim.m
170 lines (147 loc) · 7.07 KB
/
PhotoStim.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
function PhotoStim
% PhotoStim w/ multiple frequencies for optogenetic tagging
% Written by Torben Ott 11/2017
global BpodSystem
global TaskParameters
TaskParameters = BpodSystem.ProtocolSettings;
if isempty(fieldnames(TaskParameters))
TaskParameters.GUI.NTrials = 160;
TaskParameters.GUI.StimFreq.Freq = [5, 10, 20, 40, 80]';
TaskParameters.GUI.StimFreq.Active = ones(size(TaskParameters.GUI.StimFreq.Freq,1),1);
TaskParameters.GUIMeta.StimFreq.Style = 'table';
TaskParameters.GUIMeta.StimFreq.String = 'Stim Freq';
TaskParameters.GUIMeta.OdorTable.ColumnLabel = {'Freq','Active'};
TaskParameters.GUI.PulsePalTriggerChannel = 2;
TaskParameters.GUI.PulsePalOutputChannels = 3;
TaskParameters.GUI.BpodTriggerChannel = 2;
TaskParameters.GUI.ITI = 1;
TaskParameters.GUIPanels.GeneralParams = {'NTrials','ITI','BpodTriggerChannel','PulsePalTriggerChannel','PulsePalOutputChannels'};
TaskParameters.GUIPanels.StimFreqTable ={'StimFreq'};
TaskParameters.GUI.NPulses = 10;
TaskParameters.GUI.PulseDuration_ms = 1;
TaskParameters.GUIPanels.TrainParams = {'NPulses','PulseDuration_ms'};
end
BpodParameterGUI('init', TaskParameters);
%% Initialize plots
BpodSystem.ProtocolFigures.OutcomePlotFig = figure('Position', [400 600 1000 200],'Name','Outcome plot','numbertitle','off', 'MenuBar', 'none', 'Resize', 'off');
BpodSystem.GUIHandles.OutcomePlot.HandleOutcome = axes('Position', [.075 .3 .89 .6]);
OutcomePlot_PhotoStim(BpodSystem.GUIHandles.OutcomePlot,'init');
% load default PulsePal stimulus train matrix
load('LightTrain.mat')
ParameterMatrixDefault = ParameterMatrix;
%% Main trial loop
iTrial = 1;
while iTrial <= TaskParameters.GUI.NTrials
TaskParameters = BpodParameterGUI('sync', TaskParameters); % Sync parameters with BpodParameterGUI plugin
%uodate current trial fields
ActiveFreqIdx = find(logical(TaskParameters.GUI.StimFreq.Active));
idx = mod(iTrial,length(ActiveFreqIdx));
if idx == 0
idx=length(ActiveFreqIdx);
end
StimFreq = TaskParameters.GUI.StimFreq.Freq(ActiveFreqIdx(idx));
BpodSystem.Data.StimFreq(iTrial) = StimFreq;
BpodSystem.Data.StimFreqIdx(iTrial) = ActiveFreqIdx(idx);
BpodSystem.Data.iTrial(iTrial) = iTrial;
%Program PulsePal
ParameterMatrix = ParameterMatrixDefault;
OutputChannels = [1:4] .* ismember('1234',num2str(TaskParameters.GUI.PulsePalOutputChannels));
OutputChannels = OutputChannels(OutputChannels>0);
%TriggerChannel
if TaskParameters.GUI.PulsePalTriggerChannel == 1
ParameterMatrix(13,OutputChannels+1) = {0};
ParameterMatrix(14,OutputChannels+1) = {1};
elseif TaskParameters.GUI.PulsePalTriggerChannel == 2
ParameterMatrix(13,OutputChannels+1) = {0};
ParameterMatrix(14,OutputChannels+1) = {1};
else
error('Unknown trigger channel')
end
%Inter-pulse interval
ParameterMatrix(8,OutputChannels+1)={1./StimFreq - TaskParameters.GUI.PulseDuration_ms/1000};
%Burst Duration
ParameterMatrix(9,OutputChannels+1)={1./StimFreq*TaskParameters.GUI.NPulses};
%stimulus train duration
ParameterMatrix(11,OutputChannels+1)={1./StimFreq*TaskParameters.GUI.NPulses};
%single pulse duration
ParameterMatrix(5,OutputChannels+1)={TaskParameters.GUI.PulseDuration_ms/1000};
ProgramPulsePal(ParameterMatrix);
%Build state matrix
sma = NewStateMatrix();
if BpodSystem.Data.StimFreqIdx(iTrial)==1
sma = AddState(sma, 'Name', 'DeliverStimulus', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LightTrain_1'},...
'OutputActions', {});
elseif BpodSystem.Data.StimFreqIdx(iTrial)==2
sma = AddState(sma, 'Name', 'DeliverStimulus', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LightTrain_2'},...
'OutputActions', {});
elseif BpodSystem.Data.StimFreqIdx(iTrial)==3
sma = AddState(sma, 'Name', 'DeliverStimulus', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LightTrain_3'},...
'OutputActions', {});
elseif BpodSystem.Data.StimFreqIdx(iTrial)==4
sma = AddState(sma, 'Name', 'DeliverStimulus', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LightTrain_4'},...
'OutputActions', {});
elseif BpodSystem.Data.StimFreqIdx(iTrial)==5
sma = AddState(sma, 'Name', 'DeliverStimulus', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LightTrain_5'},...
'OutputActions', {});
else
errror('Unknown freq stimulus when builiding state matrix.')
end
sma = AddState(sma, 'Name', 'LightTrain_1', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'Delay'},...
'OutputActions', {'BNCState',TaskParameters.GUI.BpodTriggerChannel});
sma = AddState(sma, 'Name', 'LightTrain_2', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'Delay'},...
'OutputActions', {'BNCState',TaskParameters.GUI.BpodTriggerChannel});
sma = AddState(sma, 'Name', 'LightTrain_3', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'Delay'},...
'OutputActions', {'BNCState',TaskParameters.GUI.BpodTriggerChannel});
sma = AddState(sma, 'Name', 'LightTrain_4', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'Delay'},...
'OutputActions', {'BNCState',TaskParameters.GUI.BpodTriggerChannel});
sma = AddState(sma, 'Name', 'LightTrain_5', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'Delay'},...
'OutputActions', {'BNCState',TaskParameters.GUI.BpodTriggerChannel});
%delay state of length of 1 trial
sma = AddState(sma, 'Name', 'Delay', ...
'Timer', 1./StimFreq*TaskParameters.GUI.NPulses,...
'StateChangeConditions', {'Tup', 'ITI'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'ITI', ...
'Timer', TaskParameters.GUI.ITI,...
'StateChangeConditions', {'Tup', 'exit'},...
'OutputActions', {});
SendStateMatrix(sma);
RawEvents = RunStateMatrix;
if ~isempty(fieldnames(RawEvents)) % If trial data was returned
BpodSystem.Data = AddTrialEvents(BpodSystem.Data,RawEvents); % Computes trial events from raw data
% BpodSystem.Data = BpodNotebook(BpodSystem.Data); % Sync with Bpod notebook plugin
BpodSystem.Data.TrialSettings(iTrial) = TaskParameters; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
if ~isnan(BpodSystem.Data.RawEvents.Trial{iTrial}.States.DeliverStimulus(1))
BpodSystem.Data.Outcomes(iTrial) = 1;
else
BpodSystem.Data.Outcomes(iTrial) = 0;
end
SaveBpodSessionData; % Saves the field BpodSystem.Data to the current data file
end
HandlePauseCondition; % Checks to see if the protocol is paused. If so, waits until user resumes.
if BpodSystem.BeingUsed == 0
return
end
OutcomePlot_PhotoStim(BpodSystem.GUIHandles.OutcomePlot,'update');
iTrial = iTrial+1;
end