cortex-lab / Rigbox

A MATLAB toolbox for running behavioral neuroscience experiments and managing data
GNU General Public License v3.0
33 stars 16 forks source link

Visual stimuli can't appear before expStart event #44

Closed k1o0 closed 4 years ago

k1o0 commented 6 years ago

When using a pre-delay it is possible for various signals to update before the expStart event is updated as parameter signals acquire their values at an undefined time. This doesn't cause errors unless a visual stimulus is presented, in which case MATLAB crashed. Example:

% In this example a stimulus appears when the subject holds the wheel for a period equal to p.stimDelay. 
% If the rig pre-delay is longer than the p.stimDelay parameter shown here then MATLAB will crash.  
% This can be avoided by having the relevant signals only update at evts.newTrial.
stimDelay = p.stimDelay %.at(evts.newTrial); % at(evts.newTrial) fix for rig pre-delay 
stimulusOn = sig.quiescenceWatch(stimDelay , t, wheel, floor(p.encoderRes/100));
stimulusOff = stimulusOn .delay(10);
evts.endTrial = stimulusOff.delay(0.5);

stimulus = vis.grating(t, 'sinusoid', 'gaussian'); % create a Gabor grating
% When show is true, the stimulus is visible
stimulus.show = stimulusOn.to(stimulusOff);

To do: find the cause of the crash and prevent it from happening, or at least throw an informative error.

k1o0 commented 4 years ago

Adding to this mystery, it seems the problem only occurs when the stimulus depends on an input signal (e.g. inputs.wheel) before events.expStart updates. Observations:

  1. The input signals are independent of events.expStart and update as soon as we enter the main loop after run is called.
  2. The crash doesn't happen if the the stimulus is not shown before events.expStart updates. That is, if the show property is false.
  3. The crash doesn't happen if the stimulus property that depends on an input signal doesn't update until after events.expStart takes a value.
  4. No crash occurs if the stimulus property relies on the t signal, which also updates before events.expStart.
  5. The visual stimuli and the rendering framework do not appear to rely on events.expStart at all for anything.
k1o0 commented 4 years ago

The only visible difference between t and e.g. inputs.wheel is that one is an origin signal and the other is a subscriptable signal...

k1o0 commented 4 years ago

https://github.com/cortex-lab/signals/commit/276cd6f45a3ffa426f6a722b08fe86bd473aa30d provides a robust fix for this. In the future this should be fixed in mexnet here (c.f. old code).