Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up Experiment dependencies inside Stimulus #72

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
8 changes: 7 additions & 1 deletion stytra/stimulation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from copy import deepcopy
import warnings

from PyQt5.QtCore import pyqtSignal, QTimer, QObject
from stytra.stimulation.stimuli import Pause, DynamicStimulus
Expand Down Expand Up @@ -112,7 +113,12 @@ def update_protocol(self):

# pass experiment to stimuli for calibrator and asset folders:
for stimulus in self.stimuli:
stimulus.initialise_external(self.experiment)
try:
stimulus.initialise_external(self.experiment, self.experiment.calibrator)
except TypeError:
stimulus.initialise_external(self.experiment)
warnings.warn("Warning: 'initialise_external' will require a calibrator input from the new update!", FutureWarning)
warnings.warn("Warning: 'initialise_external' will require a calibrator input from the new update!", DeprecationWarning)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIX 1


if self.dynamic_log is None:
self.dynamic_log = DynamicLog(self.stimuli, experiment=self.experiment)
Expand Down
20 changes: 10 additions & 10 deletions stytra/stimulation/stimuli/conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def get_dynamic_state(self):
state.update(self.active.get_dynamic_state())
return state

def initialise_external(self, experiment):
super().initialise_external(experiment)
self.active.initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)
self.active.initialise_external(experiment, calibrator)

def get_state(self):
state = super().get_state()
Expand Down Expand Up @@ -157,10 +157,10 @@ def get_dynamic_state(self):
state.update(self._stim_on.get_dynamic_state())
return state

def initialise_external(self, experiment):
super().initialise_external(experiment)
self._stim_on.initialise_external(experiment)
self._stim_off.initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)
self._stim_on.initialise_external(experiment, calibrator)
self._stim_off.initialise_external(experiment, calibrator)

def get_state(self):
state = super().get_state()
Expand Down Expand Up @@ -271,7 +271,7 @@ def __init__(self, stimulus, *args, centering_stimulus=None, margin=45, **kwargs

def check_condition_on(self):
y, x, theta = self._experiment.estimator.get_position()
scale = self._experiment.calibrator.mm_px ** 2
scale = self._calibrator.mm_px ** 2
return (
x > 0 and ((x - self.xc) ** 2 + (y - self.yc) ** 2) <= self.margin / scale
)
Expand Down Expand Up @@ -324,14 +324,14 @@ def __init__(

def check_condition_on(self):
y, x, theta = self._experiment.estimator.get_position()
scale = self._experiment.calibrator.mm_px ** 2
scale = self._calibrator.mm_px ** 2
return (not np.isnan(x)) and (
(x - self.xc) ** 2 + (y - self.yc) ** 2 <= self.margin_in / scale
)

def check_condition_off(self):
y, x, theta = self._experiment.estimator.get_position()
scale = self._experiment.calibrator.mm_px ** 2
scale = self._calibrator.mm_px ** 2
return np.isnan(x) or (
(x - self.xc) ** 2 + (y - self.yc) ** 2 > self.margin_out / scale
)
Expand Down
2 changes: 1 addition & 1 deletion stytra/stimulation/stimuli/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
pulse_dur_str = str(pulse_dur_ms).zfill(3)
self.mex = str("shock" + amp_dac + pulse_dur_str)

def initialise_external(self, experiment):
def initialise_external(self, experiment, calibrator):
"""

Parameters
Expand Down
21 changes: 17 additions & 4 deletions stytra/stimulation/stimuli/generic_stimuli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import datetime
import warnings


class Stimulus:
Expand Down Expand Up @@ -66,6 +67,7 @@ def __init__(self, duration=0.0):
self._elapsed = 0.0 # time from the beginning of the stimulus
self.name = "undefined"
self._experiment = None
self._calibrator = None
self.real_time_start = None
self.real_time_stop = None

Expand Down Expand Up @@ -111,7 +113,7 @@ def stop(self):
"""
pass

def initialise_external(self, experiment):
def initialise_external(self, experiment, calibrator = -999):
fedem-p marked this conversation as resolved.
Show resolved Hide resolved
""" Make a reference to the Experiment class inside the Stimulus.
This is required to access from inside the Stimulus class to the
Calibrator, the Pyboard, the asset directories with movies or the motor
Expand All @@ -130,7 +132,18 @@ def initialise_external(self, experiment):
None

"""

if calibrator == -999:
self._calibrator = self._experiment.calibrator
warnings.warn("Warning: 'initialise_external' will require a calibrator input from the new update!", FutureWarning)
warnings.warn("Warning: 'initialise_external' will require a calibrator input from the new update!", DeprecationWarning)
else:
self._calibrator = calibrator


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix 2

self._experiment = experiment




class DynamicStimulus(Stimulus):
Expand Down Expand Up @@ -289,10 +302,10 @@ def update(self):
s.update()
s._elapsed = self._elapsed

def initialise_external(self, experiment):
super().initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)
for s in self._stim_list:
s.initialise_external(experiment)
s.initialise_external(experiment, calibrator)

@property
def dynamic_parameter_names(self):
Expand Down
4 changes: 2 additions & 2 deletions stytra/stimulation/stimuli/kinematograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def get_dimensions(self):
-------
number of dots to display and the displacement amount in pixel coordinates
"""
if self._experiment.calibrator is not None:
mm_px = self._experiment.calibrator.mm_px
if self._calibrator is not None:
mm_px = self._calibrator.mm_px
else:
mm_px = 1

Expand Down
28 changes: 14 additions & 14 deletions stytra/stimulation/stimuli/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ def get_tile_ranges(self, imw, imh, w, h, tr: QTransform):
return range(x_start, x_end + 1), range(y_start, y_end + 1)

def paint(self, p, w, h):
if self._experiment.calibrator is not None:
mm_px = self._experiment.calibrator.mm_px
if self._calibrator is not None:
mm_px = self._calibrator.mm_px
else:
mm_px = 1

Expand Down Expand Up @@ -396,8 +396,8 @@ def __init__(self, *args, background, background_name=None, **kwargs):
self.background_name = "array {}x{}".format(*self._background.shape)
self._qbackground = None

def initialise_external(self, experiment):
super().initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)

# Get background image from folder:
if isinstance(self._background, str):
Expand Down Expand Up @@ -468,7 +468,7 @@ def __init__(
def create_pattern(self):
l = max(
2,
int(self.grating_period / (max(self._experiment.calibrator.mm_px, 0.0001))),
int(self.grating_period / (max(self._calibrator.mm_px, 0.0001))),
)
if self.wave_shape == "square":
self._pattern = np.ones((l, 3), np.uint8) * self.color_1
Expand All @@ -483,8 +483,8 @@ def create_pattern(self):
+ (1 - w[:, None]) * np.array(self.color_2)[None, :]
).astype(np.uint8)

def initialise_external(self, experiment):
super().initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)
self.create_pattern()
# Get background image from folder:
self._qbackground = qimage2ndarray.array2qimage(self._pattern[None, :, :])
Expand Down Expand Up @@ -532,7 +532,7 @@ def get_unit_dims(self, w, h):
#TODO what does this thing define?
"""
return (
int(self.grating_period / (max(self._experiment.calibrator.mm_px, 0.0001))),
int(self.grating_period / (max(self._calibrator.mm_px, 0.0001))),
self.barheight,
)

Expand All @@ -547,7 +547,7 @@ def draw_block(self, p, point, w, h):
point.y(),
int(
self.grating_period
/ (2 * max(self._experiment.calibrator.mm_px, 0.0001))
/ (2 * max(self._calibrator.mm_px, 0.0001))
),
self.barheight,
)
Expand Down Expand Up @@ -654,7 +654,7 @@ def update(self):

def paint(self, p, w, h):
x, y = (
(np.arange(d) - d / 2) * self._experiment.calibrator.mm_px for d in (w, h)
(np.arange(d) - d / 2) * self._calibrator.mm_px for d in (w, h)
)
self.image = np.round(
np.sin(
Expand Down Expand Up @@ -751,8 +751,8 @@ def create_pattern(self, side_len=500):
self._pattern = W * self.color_1 + (1 - W) * self.color_2
self._qbackground = qimage2ndarray.array2qimage(self._pattern)

def initialise_external(self, experiment):
super().initialise_external(experiment)
def initialise_external(self, experiment, calibrator):
super().initialise_external(experiment, calibrator)
self.create_pattern()

def draw_block(self, p, point, w, h):
Expand Down Expand Up @@ -933,8 +933,8 @@ def __init__(
def paint(self, p, w, h):
super().paint(p, w, h)

if self._experiment.calibrator is not None:
mm_px = self._experiment.calibrator.mm_px
if self._calibrator is not None:
mm_px = self._calibrator.mm_px
else:
mm_px = 1

Expand Down