portugueslab / stytra

A modular package to control stimulation and track behaviour
http://www.portugueslab.com/stytra/
GNU General Public License v3.0
41 stars 26 forks source link

Clean-up Experiment dependencies inside Stimulus #72

Open fedem-p opened 2 years ago

fedem-p commented 2 years ago

With this PR the protocol runner passes the calibrator object from the method initialise_external(). To avoid problems with existing custom stimuli which may have rewritten the method I added two warnings.

More specifically I expect two cases to throw errors:

  1. if the protocol runner calls a custom stimulus where the initialise_external() function doesn't accept the calibrator input.
  2. if the function initialise_external() is used without passing the calibrator input.

To fix 1:

in the protocol runner there's a try{} except{} block that tries to feed the calibrator object to the stimulus initialise_external() function. If this doesn't work then the protocol runner raises a warning and then calls the function without the calibrator input.

To fix 2:

the initialise_external() has an optional argument calibrator which has default value -999, if the default value is detected then the self._calibrator is initialized with the self._experiment and a warning is given to the user. else the self._calibrator is initialized with the calibrator input

Finally:

In all the others default stimuli the _calibrator object is used instead of _experiment.calibrator

fedem-p commented 2 years ago

Update: I have created a Dataclass called EnvironmentState which olds different attributes:

The last two are still unused.

The way it works is the same as before: the protocol runner passes an _environment_state instance to the stimulus through the initialise_external method. Here it gets initialized as a stimulus variable (if the input is not an instance of the EnvironmentState class the variable is initialized as a copy of the experiment object)

in the rest of the stimulus files the calls to calibrator and estimator go through the _environment_state variable