Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unaccounted for delay between 'trials'
#1
Hi Josh,

I have a very simple protocol with just 2 states (see at the bottom), but I seem to be getting a delay of ~0.48s between trials that I can't account for.

e.g. after running 500 trials, I get the data and run the code below:

>>
for trial = 2:SessionData.nTrials
report.delays(trial) = SessionData.TrialStartTimestamp(1,trial) - SessionData.TrialEndTimestamp(1,trial-1);
end
>>

In this case, report.delays(1) will be 0, but report.delays(2) ~0.45 and then increasing steadily with each (trial) reaching ~0.57s

This issue is apparent if I look at the overall task time as well, e.g. if I check the overall task time and compare it with what I would expect from just summing the trial times. 

>>
report.totalTaskTimeBpod = SessionData.TrialEndTimestamp(1,max(SessionData.nTrials)) - SessionData.TrialStartTimestamp(1,1); 
report.totalTaskTimeBpodExpected = (SessionData.TrialEndTimestamp(1,1) - SessionData.TrialStartTimestamp(1,1)) * SessionData.nTrials;
report.timeDiff  = report.totalTaskTimeBpod - report.totalTaskTimeBpodExpected;
%Difference betwen expected and actual time - Ideally zero  
 >>

The difference between the two ends up at ~0.48s [>>>report.timeDiff / SessionData.nTrials]

Is this normal/common? Is this just time taken up by the PC/Matlab to save files etc. or something I'm missing about the state machine/data file structure?

Thank you in advance for your help.
Best wishes,
Raul 

Protocol:
  >>
    sma = NewStateMatrix();
    sma = AddState(sma, 'Name', 'presentStimulus',...
        'Timer', stimDuration,...
        'StateChangeCondition', {'Tup', 'InterTrialInterval'},...
        'OutputActions', {'BNCState',3});
    
    sma = AddState(sma, 'Name', 'InterTrialInterval',...
        'Timer', ITI,...
        'StateChangeConditions', {'Tup', 'exit'},...
        'OutputActions', {});
    
    SendStateMatrix(sma);
    RawEvents = RunStateMatrix;
    if ~isempty(fieldnames(RawEvents))
        %Compute trial events from raw
        BpodSystem.Data                         = AddTrialEvents(BpodSystem.Data, RawEvents);
        
        %Adds the settings used for the current trial to the Data struct
        BpodSystem.Data.TrialSettings(trial)    = S;
        
        %Adds the trial type of the current trial to the Data struct
        BpodSystem.Data.TrialTypes(trial)       = TrialTypes(trial);
        
        %Save the data (see function code for details)
        SaveBpodSessionData;
        
    end
  >>

Configuration:

  Matlab 2019a running on Windows 10 Enterprise
  Bpod r0.5
  System: Intel® Core™ i7-8700 CPU @ 3.20GHz, 16GB RAM
Reply
#2
Hi Raul,

You're looking at the "dead-time" between trials - time it takes MATLAB to run everything in your loop between calls to RunStateMachine().
Given the specs of your machine, I'm surprised it's so high - it should be more like 7-10ms.
If you add 'tic' and 'toc' commands surrounding one line at a time, you can see which line(s) are lagging. SaveBpodSessionData() is a good candidate.

If you want dead-time to be very low (approx. 200 microseconds), you can mod your protocol to use TrialManager (also see example protocol here).
TrialManager frees the MATLAB command line while a trial is running, to do time-costly things in parallel (plotting, saving, preparing the next trial).

-Josh
Reply


Forum Jump: