Last active
April 19, 2018 01:41
-
-
Save eurekaneuro/5d87b249e6b59ec24a5fa0ff7f16a4d1 to your computer and use it in GitHub Desktop.
Bpod Protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SP_Phase3_CSUS | |
% | |
% Hyun-Jae Pi, June2016 | |
% This protocol has been modified by other contributors. | |
% This protocol will deliver a stimulation followed by water | |
% delivery after the CS (the animal controls the delivery by triggering deliver reward. | |
% The initial delay will be shuffled between 2 and 3. | |
global BpodSystem | |
%% ****************************************** | |
TrialTypeProbs = [1 0] % First probability being Go or NOGO. | |
%% 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.RewardAmount = 10; % amount of reward delivered to the mouse in microliters | |
S.GUI.InitialDelay = 2; | |
S.GUI.ITI = 0.7; | |
S.GUI.WaitForLickDur = 1; | |
S.StimulusDuration = 0.3; | |
S.InitialDelayMean = [2 3]; | |
S.TrialTypeProbs = TrialTypeProbs; | |
S.RewardValveCode =1; | |
S.Timeout = 1.5; | |
end | |
%% Define trial types | |
maxTrials = 5000; | |
for x = 1:maxTrials | |
P = rand; | |
Cutoffs = cumsum(S.TrialTypeProbs); | |
Found = 0; | |
for y = 1:length(S.TrialTypeProbs) | |
if P<Cutoffs(y) && Found == 0 | |
Found = 1; | |
S.TrialTypes(x) = y; | |
end | |
end | |
end | |
%randomises delay | |
for i = 1:ceil(maxTrials / length(S.InitialDelayMean)) %adjust divisor based on no. test frequencies | |
delay(i,:) = Shuffle(S.InitialDelayMean); | |
end | |
initdel = reshape(delay',[1, maxTrials]); | |
S.InitdelTypes = initdel; | |
clear new i delay | |
S.InitdelTypes = initdel(randperm(length(initdel))); | |
BpodSystem.Data.TrialTypes = []; % The trial type of each trial completed will be added here. | |
%% Initialize parameter GUI plugin | |
BpodParameterGUI('init', S); | |
TotalRewardDisplay('init', S); | |
%% Initialize plots | |
% BpodSystem.ProtocolFigures.OutcomePlotFig = | |
figure('Position', [100 300 1000 300],'Name','Outcome plot','numbertitle','off', 'MenuBar', 'none', 'Resize', 'off'); | |
BpodSystem.GUIHandles.OutcomePlot = axes('Position', [.075 .3 .89 .6]); | |
OutcomePlot_SP_Phase3_CSUS(BpodSystem.GUIHandles.OutcomePlot,'init',2-S.TrialTypes); | |
%% Main loop | |
maxTrials = 5000; | |
for currentTrial = 1:maxTrials | |
fprintf(['Trial # ' num2str(currentTrial) ': trial type ' num2str(S.TrialTypes(currentTrial)) '\n']); | |
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin | |
% Backgroud_noise = {'WireState', 2}; | |
switch S.TrialTypes(currentTrial) | |
case 1 | |
StimOutputAction = {'WireState',1}; % {'SoftCode',1,'BNCState',0, 'WireState',2} | |
Offsettime = 0; | |
RewardOutputAction = {'ValveState',1}; % {'BNCState',2, 'WireState',2}; | |
case 2 | |
StimOutputAction = {'BNCState', 1}; % {'SoftCode', 2, 'BNCState', 1, 'WireState',2}; | |
Offsettime = 0; | |
RewardOutputAction = {}; | |
end | |
WaterTime = GetValveTimes(S.GUI.RewardAmount, [1]); | |
% Assemble state matrix | |
sma = NewStateMatrix(); | |
sma = AddState(sma, 'Name', 'InitialDelay', ... | |
'Timer', S.InitdelTypes(currentTrial), ... | |
'StateChangeConditions',{'Tup', 'StartTrial'},... | |
'OutputActions', {}); | |
sma = AddState(sma, 'Name', 'StartTrial', ... | |
'Timer', 0,... | |
'StateChangeConditions',{'Tup','DeliverStimulus'},... | |
'OutputActions', {}); % Send a TTL on BNC output 2 of Bpod for syncing with imaging system ; WireState 1 for initiating �maging system | |
sma = AddState(sma, 'Name', 'DeliverStimulus', ... | |
'Timer', S.StimulusDuration,... | |
'StateChangeConditions', {'Tup','WaitForLick'},... | |
'OutputActions', StimOutputAction); | |
if S.TrialTypes(currentTrial) == 1 | |
sma = AddState(sma, 'Name', 'WaitForLick', ... | |
'Timer',S.GUI.WaitForLickDur,... | |
'StateChangeConditions', {'Tup', 'ITI', 'Port1In', 'DeliverReward'},... | |
'OutputActions', {}); | |
end | |
if S.TrialTypes(currentTrial) == 2 | |
sma = AddState(sma, 'Name', 'WaitForLick', ... | |
'Timer',S.GUI.WaitForLickDur,... | |
'StateChangeConditions', {'Tup', 'ITI', 'Port1In', 'Timeout'},... | |
'OutputActions', {}); | |
end | |
sma = AddState(sma,'Name', 'DeliverReward', ... | |
'Timer',WaterTime, ... | |
'StateChangeConditions',{'Tup', 'ITI', 'Port1In', 'StillDrinking'},... | |
'OutputActions', RewardOutputAction); | |
sma = AddState(sma,'Name', 'Timeout', ... | |
'Timer',S.Timeout, ... | |
'StateChangeConditions',{'Tup', 'ITI', 'Port1In', 'Timeout'},... | |
'OutputActions', {}); | |
sma = AddState(sma, 'Name', 'ITI', ... | |
'Timer',S.GUI.ITI,... | |
'StateChangeConditions', {'Tup', 'exit', 'Port1In','ResetTimerForDrinking'}, ... | |
'OutputActions', {}); | |
sma = AddState(sma,'Name', 'StillDrinking', ... | |
'Timer', 0.5, ... | |
'StateChangeConditions', {'Tup', 'ITI','Port1In','ResetTimerForDrinking'},... | |
'OutputActions', {}); | |
sma = AddState(sma,'Name', 'ResetTimerForDrinking', ... | |
'Timer', 0.5, ... | |
'StateChangeConditions', {'Tup', 'StillDrinking',},... | |
'OutputActions', {}); | |
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends) | |
SendStateMatrix(sma); | |
RawEvents = RunStateMatrix; | |
BpodSystem.Data = AddTrialEvents(BpodSystem.Data,RawEvents); | |
%BpodSystem.Data = BpodNotebook(BpodSystem.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) = S.TrialTypes(currentTrial); % Adds the trial type of the current trial to data | |
BpodSystem.Data.InitDelayTypes(currentTrial) = S.InitdelTypes(currentTrial); % Adds the trial type of the current trial to data | |
UpdateTotalRewardDisplay(S.GUI.RewardAmount, currentTrial); | |
Outcomes = UpdateOutcomePlot(S.TrialTypes, BpodSystem.Data); | |
BpodSystem.Data.TrialOutcome(currentTrial) = Outcomes(currentTrial); | |
SaveBpodSessionData; % Saves the field BpodSystem.Data to the current data file | |
end | |
end | |
%---------------------------------------- /MAIN LOOP | |
%% sub-functions | |
function Outcomes = UpdateOutcomePlot(TrialTypes, Data) | |
global BpodSystem | |
Outcomes = zeros(1,Data.nTrials); | |
for x = 1:Data.nTrials | |
if ~isnan(Data.RawEvents.Trial{x}.States.DeliverReward(1)) | |
Outcomes(x) = 1; | |
% elseif ~isnan(Data.RawEvents.Trial{x}.States.DeliverPunish(1)) | |
% Outcomes(x) = 0; | |
else | |
Outcomes(x) = 3; | |
end | |
end | |
OutcomePlot_SP_Phase3_CSUS(BpodSystem.GUIHandles.OutcomePlot,'update',Data.nTrials+1,2-TrialTypes,Outcomes) | |
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.DeliverReward(1)) | |
TotalRewardDisplay('add', RewardAmount); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment