Ejemplo n.º 1
0
def test_stimulus_timestamps_from_json(
    monkeypatch, dict_repr, has_pkl, has_sync
):
    mock_stimulus_file = create_autospec(StimulusFile)
    mock_sync_file = create_autospec(SyncFile)

    mock_get_behavior_stimulus_timestamps = create_autospec(
        get_behavior_stimulus_timestamps
    )
    mock_get_ophys_stimulus_timestamps = create_autospec(
        get_ophys_stimulus_timestamps
    )

    with monkeypatch.context() as m:
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps.StimulusFile",
            mock_stimulus_file
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps.SyncFile",
            mock_sync_file
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps"
            ".get_behavior_stimulus_timestamps",
            mock_get_behavior_stimulus_timestamps
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps"
            ".get_ophys_stimulus_timestamps",
            mock_get_ophys_stimulus_timestamps
        )
        mock_stimulus_file_instance = mock_stimulus_file.from_json(dict_repr)
        ts_from_stim = StimulusTimestamps.from_stimulus_file(
            stimulus_file=mock_stimulus_file_instance)

        if has_pkl and has_sync:
            mock_sync_file_instance = mock_sync_file.from_json(dict_repr)
            ts_from_sync = StimulusTimestamps.from_sync_file(
                sync_file=mock_sync_file_instance)

    if has_pkl and has_sync:
        mock_get_ophys_stimulus_timestamps.assert_called_once_with(
            sync_path=mock_sync_file_instance.filepath
        )
        assert ts_from_sync._sync_file == mock_sync_file_instance
    else:
        assert ts_from_stim._stimulus_file == mock_stimulus_file_instance
        mock_get_behavior_stimulus_timestamps.assert_called_once_with(
            stimulus_pkl=mock_stimulus_file_instance.data
        )
Ejemplo n.º 2
0
def test_stimulus_timestamps_nwb_roundtrip(
    nwbfile, data_object_roundtrip_fixture, roundtrip, stimulus_timestamps_data
):
    stimulus_timestamps = StimulusTimestamps(
        timestamps=stimulus_timestamps_data
    )
    nwbfile = stimulus_timestamps.to_nwb(nwbfile)

    if roundtrip:
        obt = data_object_roundtrip_fixture(nwbfile, StimulusTimestamps)
    else:
        obt = StimulusTimestamps.from_nwb(nwbfile)

    assert np.allclose(obt.value, stimulus_timestamps_data)
Ejemplo n.º 3
0
    def test_from_stimulus_file2(self, tmpdir):
        """
        Test that Licks.from_stimulus_file returns a dataframe
        of licks whose timestamps are based on their frame number
        with respect to the stimulus_timestamps
        """
        stimulus_filepath = self._create_test_stimulus_file(
            lick_events=[12, 15, 90, 136], tmpdir=tmpdir)
        stimulus_file = StimulusFile.from_json(
            dict_repr={'behavior_stimulus_file': str(stimulus_filepath)})
        timestamps = StimulusTimestamps(timestamps=np.arange(0, 2.0, 0.01))
        licks = Licks.from_stimulus_file(stimulus_file=stimulus_file,
                                         stimulus_timestamps=timestamps)

        expected_dict = {
            'timestamps': [0.12, 0.15, 0.90, 1.36],
            'frame': [12, 15, 90, 136]
        }
        expected_df = pd.DataFrame(expected_dict)
        assert expected_df.columns.equals(licks.value.columns)
        np.testing.assert_array_almost_equal(
            expected_df.timestamps.to_numpy(),
            licks.value['timestamps'].to_numpy(),
            decimal=10)
        np.testing.assert_array_almost_equal(expected_df.frame.to_numpy(),
                                             licks.value['frame'].to_numpy(),
                                             decimal=10)
Ejemplo n.º 4
0
    def test_get_licks_excess(self, tmpdir):
        """
        Test that Licks.from_stimulus_file
        in the case where
        there is an extra frame at the end of the trial log and the mouse
        licked on that frame

        https://github.com/AllenInstitute/visual_behavior_analysis/blob
        /master/visual_behavior/translator/foraging2/extract.py#L640-L647
        """
        stimulus_filepath = self._create_test_stimulus_file(
            lick_events=[12, 15, 90, 136, 200],  # len(timestamps) == 200,
            tmpdir=tmpdir)
        stimulus_file = StimulusFile.from_json(
            dict_repr={'behavior_stimulus_file': str(stimulus_filepath)})
        timestamps = StimulusTimestamps(timestamps=np.arange(0, 2.0, 0.01))
        licks = Licks.from_stimulus_file(stimulus_file=stimulus_file,
                                         stimulus_timestamps=timestamps)

        expected_dict = {
            'timestamps': [0.12, 0.15, 0.90, 1.36],
            'frame': [12, 15, 90, 136]
        }
        expected_df = pd.DataFrame(expected_dict)
        assert expected_df.columns.equals(licks.value.columns)
        np.testing.assert_array_almost_equal(
            expected_df.timestamps.to_numpy(),
            licks.value['timestamps'].to_numpy(),
            decimal=10)
        np.testing.assert_array_almost_equal(expected_df.frame.to_numpy(),
                                             licks.value['frame'].to_numpy(),
                                             decimal=10)
Ejemplo n.º 5
0
    def from_json(cls,
                  session_data: dict,
                  monitor_delay: Optional[float] = None) \
            -> "BehaviorSession":
        """

        Parameters
        ----------
        session_data
            Dict of input data necessary to construct a session
        monitor_delay
            Monitor delay. If not provided, will use an estimate.
            To provide this value, see for example
            allensdk.brain_observatory.behavior.data_objects.stimuli.util.
            calculate_monitor_delay

        Returns
        -------
        `BehaviorSession` instance

        """
        behavior_session_id = BehaviorSessionId.from_json(
            dict_repr=session_data)
        stimulus_file = StimulusFile.from_json(dict_repr=session_data)
        stimulus_timestamps = StimulusTimestamps.from_json(
            dict_repr=session_data)
        running_acquisition = RunningAcquisition.from_json(
            dict_repr=session_data)
        raw_running_speed = RunningSpeed.from_json(dict_repr=session_data,
                                                   filtered=False)
        running_speed = RunningSpeed.from_json(dict_repr=session_data)
        metadata = BehaviorMetadata.from_json(dict_repr=session_data)

        if monitor_delay is None:
            monitor_delay = cls._get_monitor_delay()

        licks, rewards, stimuli, task_parameters, trials = \
            cls._read_data_from_stimulus_file(
                stimulus_file=stimulus_file,
                stimulus_timestamps=stimulus_timestamps,
                trial_monitor_delay=monitor_delay
            )
        date_of_acquisition = DateOfAcquisition.from_json(
            dict_repr=session_data)\
            .validate(
            stimulus_file=stimulus_file,
            behavior_session_id=behavior_session_id.value)

        return BehaviorSession(behavior_session_id=behavior_session_id,
                               stimulus_timestamps=stimulus_timestamps,
                               running_acquisition=running_acquisition,
                               raw_running_speed=raw_running_speed,
                               running_speed=running_speed,
                               metadata=metadata,
                               licks=licks,
                               rewards=rewards,
                               stimuli=stimuli,
                               task_parameters=task_parameters,
                               trials=trials,
                               date_of_acquisition=date_of_acquisition)
Ejemplo n.º 6
0
    def from_nwb(cls, nwbfile: NWBFile, **kwargs) -> "BehaviorSession":
        behavior_session_id = BehaviorSessionId.from_nwb(nwbfile)
        stimulus_timestamps = StimulusTimestamps.from_nwb(nwbfile)
        running_acquisition = RunningAcquisition.from_nwb(nwbfile)
        raw_running_speed = RunningSpeed.from_nwb(nwbfile, filtered=False)
        running_speed = RunningSpeed.from_nwb(nwbfile)
        metadata = BehaviorMetadata.from_nwb(nwbfile)
        licks = Licks.from_nwb(nwbfile=nwbfile)
        rewards = Rewards.from_nwb(nwbfile=nwbfile)
        stimuli = Stimuli.from_nwb(nwbfile=nwbfile)
        task_parameters = TaskParameters.from_nwb(nwbfile=nwbfile)
        trials = TrialTable.from_nwb(nwbfile=nwbfile)
        date_of_acquisition = DateOfAcquisition.from_nwb(nwbfile=nwbfile)

        return BehaviorSession(behavior_session_id=behavior_session_id,
                               stimulus_timestamps=stimulus_timestamps,
                               running_acquisition=running_acquisition,
                               raw_running_speed=raw_running_speed,
                               running_speed=running_speed,
                               metadata=metadata,
                               licks=licks,
                               rewards=rewards,
                               stimuli=stimuli,
                               task_parameters=task_parameters,
                               trials=trials,
                               date_of_acquisition=date_of_acquisition)
Ejemplo n.º 7
0
 def test_from_stimulus_file(self):
     stimulus_file = StimulusFile.from_lims(
         behavior_session_id=self.behavior_session_id, db=self.dbconn)
     timestamps = StimulusTimestamps.from_stimulus_file(
         stimulus_file=stimulus_file)
     rewards = Rewards.from_stimulus_file(stimulus_file=stimulus_file,
                                          stimulus_timestamps=timestamps)
     assert rewards == self.expected
Ejemplo n.º 8
0
    def setup_class(cls):
        dir = Path(__file__).parent.resolve()
        test_data_dir = dir / 'test_data'

        stimulus_file = StimulusFile(filepath=test_data_dir /
                                     'behavior_stimulus_file.pkl')
        ts = StimulusTimestamps.from_stimulus_file(stimulus_file=stimulus_file)
        cls.licks = Licks.from_stimulus_file(stimulus_file=stimulus_file,
                                             stimulus_timestamps=ts)
Ejemplo n.º 9
0
    def setup_method(self, method):
        self.nwbfile = pynwb.NWBFile(session_description='asession',
                                     identifier='1234',
                                     session_start_time=datetime.now())

        # Need to write stimulus timestamps first
        bsf = StimulusFile(filepath=self.test_data_dir /
                           'behavior_stimulus_file.pkl')
        ts = StimulusTimestamps.from_stimulus_file(stimulus_file=bsf)
        ts.to_nwb(nwbfile=self.nwbfile)
Ejemplo n.º 10
0
def test_stimulus_timestamps_from_json2():
    dir = Path(__file__).parent.parent.resolve()
    test_data_dir = dir / 'test_data'
    sf_path = test_data_dir / 'stimulus_file.pkl'

    sf = StimulusFile.from_json(
        dict_repr={'behavior_stimulus_file': str(sf_path)})
    stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
        stimulus_file=sf)
    expected = np.array([0.016 * i for i in range(11)])
    assert np.allclose(expected, stimulus_timestamps.value)
Ejemplo n.º 11
0
    def test_get_licks_failure(self, tmpdir):
        stimulus_filepath = self._create_test_stimulus_file(
            lick_events=[12, 15, 90, 136, 201],  # len(timestamps) == 200,
            tmpdir=tmpdir)
        stimulus_file = StimulusFile.from_json(
            dict_repr={'behavior_stimulus_file': str(stimulus_filepath)})
        timestamps = StimulusTimestamps(timestamps=np.arange(0, 2.0, 0.01))

        with pytest.raises(IndexError):
            Licks.from_stimulus_file(stimulus_file=stimulus_file,
                                     stimulus_timestamps=timestamps)
Ejemplo n.º 12
0
 def test_from_stimulus_file(self):
     stimulus_file = StimulusFile.from_lims(
         behavior_session_id=self.behavior_session_id, db=self.dbconn)
     stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
         stimulus_file=stimulus_file)
     stimuli = Stimuli.from_stimulus_file(
         stimulus_file=stimulus_file,
         stimulus_timestamps=stimulus_timestamps,
         limit_to_images=['im065'])
     assert stimuli.presentations == self.expected_presentations
     assert stimuli.templates == self.expected_templates
Ejemplo n.º 13
0
def test_stimulus_timestamps_to_json(
    stimulus_file, stimulus_file_to_json_ret,
    sync_file, sync_file_to_json_ret, raises, expected
):
    if stimulus_file is not None:
        stimulus_file.to_json.return_value = stimulus_file_to_json_ret
    if sync_file is not None:
        sync_file.to_json.return_value = sync_file_to_json_ret

    stimulus_timestamps = StimulusTimestamps(
        timestamps=None,
        stimulus_file=stimulus_file,
        sync_file=sync_file
    )

    if raises:
        with pytest.raises(RuntimeError, match=raises):
            _ = stimulus_timestamps.to_json()
    else:
        obt = stimulus_timestamps.to_json()
        assert obt == expected
Ejemplo n.º 14
0
    def test_from_stimulus_file2(self, tmpdir):
        """
        Test that Rewards.from_stimulus_file returns
        expected results (main nuance is that timestamps should be
        determined by applying the reward frame as an index to
        stimulus_timestamps)
        """
        def _create_dummy_stimulus_file():
            trial_log = [{
                'rewards': [(0.001, -1.0, 4)],
                'trial_params': {
                    'auto_reward': True
                }
            }, {
                'rewards': []
            }, {
                'rewards': [(0.002, -1.0, 10)],
                'trial_params': {
                    'auto_reward': False
                }
            }]
            data = {
                'items': {
                    'behavior': {
                        'trial_log': trial_log
                    }
                },
            }
            tmp_path = tmpdir / 'stimulus_file.pkl'
            with open(tmp_path, 'wb') as f:
                pickle.dump(data, f)
                f.seek(0)

            return tmp_path

        stimulus_filepath = _create_dummy_stimulus_file()
        stimulus_file = StimulusFile.from_json(
            dict_repr={'behavior_stimulus_file': str(stimulus_filepath)})
        timestamps = StimulusTimestamps(timestamps=np.arange(0, 2.0, 0.01))
        rewards = Rewards.from_stimulus_file(stimulus_file=stimulus_file,
                                             stimulus_timestamps=timestamps)

        expected_dict = {
            'volume': [0.001, 0.002],
            'timestamps': [0.04, 0.1],
            'autorewarded': [True, False]
        }
        expected_df = pd.DataFrame(expected_dict)
        expected_df = expected_df
        assert expected_df.equals(rewards.value)
Ejemplo n.º 15
0
    def from_json(cls,
                  dict_repr: dict,
                  filtered: bool = True,
                  zscore_threshold: float = 10.0) -> "RunningSpeed":
        stimulus_file = StimulusFile.from_json(dict_repr)
        stimulus_timestamps = StimulusTimestamps.from_json(dict_repr)

        running_speed = cls._get_running_speed_df(stimulus_file,
                                                  stimulus_timestamps,
                                                  filtered, zscore_threshold)
        return cls(running_speed=running_speed,
                   stimulus_file=stimulus_file,
                   stimulus_timestamps=stimulus_timestamps,
                   filtered=filtered)
Ejemplo n.º 16
0
    def from_json(
        cls,
        dict_repr: dict,
    ) -> "RunningAcquisition":
        stimulus_file = StimulusFile.from_json(dict_repr)
        stimulus_timestamps = StimulusTimestamps.from_json(dict_repr)
        running_acq_df = get_running_df(
            data=stimulus_file.data,
            time=stimulus_timestamps.value,
        )
        running_acq_df.drop("speed", axis=1, inplace=True)

        return cls(
            running_acquisition=running_acq_df,
            stimulus_file=stimulus_file,
            stimulus_timestamps=stimulus_timestamps,
        )
Ejemplo n.º 17
0
    def from_lims(
        cls,
        db: PostgresQueryMixin,
        behavior_session_id: int,
        filtered: bool = True,
        zscore_threshold: float = 10.0,
        stimulus_timestamps: Optional[StimulusTimestamps] = None
    ) -> "RunningSpeed":
        stimulus_file = StimulusFile.from_lims(db, behavior_session_id)
        if stimulus_timestamps is None:
            stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
                stimulus_file=stimulus_file)

        running_speed = cls._get_running_speed_df(stimulus_file,
                                                  stimulus_timestamps,
                                                  filtered, zscore_threshold)
        return cls(running_speed=running_speed,
                   stimulus_file=stimulus_file,
                   stimulus_timestamps=stimulus_timestamps,
                   filtered=filtered)
Ejemplo n.º 18
0
    def test_empty_licks(self, tmpdir):
        """
        Test that Licks.from_stimulus_file in the case where
        there are no licks
        """

        stimulus_filepath = self._create_test_stimulus_file(lick_events=[],
                                                            tmpdir=tmpdir)
        stimulus_file = StimulusFile.from_json(
            dict_repr={'behavior_stimulus_file': str(stimulus_filepath)})
        timestamps = StimulusTimestamps(timestamps=np.arange(0, 2.0, 0.01))
        licks = Licks.from_stimulus_file(stimulus_file=stimulus_file,
                                         stimulus_timestamps=timestamps)

        expected_dict = {'timestamps': [], 'frame': []}
        expected_df = pd.DataFrame(expected_dict)
        assert expected_df.columns.equals(licks.value.columns)
        np.testing.assert_array_equal(expected_df.timestamps.to_numpy(),
                                      licks.value['timestamps'].to_numpy())
        np.testing.assert_array_equal(expected_df.frame.to_numpy(),
                                      licks.value['frame'].to_numpy())
Ejemplo n.º 19
0
    def from_lims(
        cls,
        db: PostgresQueryMixin,
        behavior_session_id: int,
        ophys_experiment_id: Optional[int] = None,
    ) -> "RunningAcquisition":

        stimulus_file = StimulusFile.from_lims(db, behavior_session_id)
        stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
            stimulus_file=stimulus_file)
        running_acq_df = get_running_df(
            data=stimulus_file.data,
            time=stimulus_timestamps.value,
        )
        running_acq_df.drop("speed", axis=1, inplace=True)

        return cls(
            running_acquisition=running_acq_df,
            stimulus_file=stimulus_file,
            stimulus_timestamps=stimulus_timestamps,
        )
Ejemplo n.º 20
0
def test_stimulus_timestamps_from_json3():
    """
    Test that StimulusTimestamps.from_stimulus_file
    just returns the sum of the intervalsms field in the
    behavior stimulus pickle file, padded with a zero at the
    first timestamp.
    """
    dir = Path(__file__).parent.parent.resolve()
    test_data_dir = dir / 'test_data'
    sf_path = test_data_dir / 'stimulus_file.pkl'

    sf = StimulusFile.from_json(
        dict_repr={'behavior_stimulus_file': str(sf_path)})

    sf._data['items']['behavior']['intervalsms'] = [0.1, 0.2, 0.3, 0.4]

    stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
        stimulus_file=sf)

    expected = np.array([0., 0.0001, 0.0003, 0.0006, 0.001])
    np.testing.assert_array_almost_equal(stimulus_timestamps.value,
                                         expected,
                                         decimal=10)
Ejemplo n.º 21
0
def test_stimulus_timestamps_from_lims(
    monkeypatch, behavior_session_id, ophys_experiment_id
):
    mock_db_conn = create_autospec(PostgresQueryMixin, instance=True)

    mock_stimulus_file = create_autospec(StimulusFile)
    mock_sync_file = create_autospec(SyncFile)

    mock_get_behavior_stimulus_timestamps = create_autospec(
        get_behavior_stimulus_timestamps
    )
    mock_get_ophys_stimulus_timestamps = create_autospec(
        get_ophys_stimulus_timestamps
    )

    with monkeypatch.context() as m:
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps.StimulusFile",
            mock_stimulus_file
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps.SyncFile",
            mock_sync_file
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps"
            ".get_behavior_stimulus_timestamps",
            mock_get_behavior_stimulus_timestamps
        )
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".timestamps.stimulus_timestamps.stimulus_timestamps"
            ".get_ophys_stimulus_timestamps",
            mock_get_ophys_stimulus_timestamps
        )
        mock_stimulus_file_instance = mock_stimulus_file.from_lims(
            mock_db_conn, behavior_session_id
        )
        ts_from_stim = StimulusTimestamps.from_stimulus_file(
            stimulus_file=mock_stimulus_file_instance)
        assert ts_from_stim._stimulus_file == mock_stimulus_file_instance

        if behavior_session_id is not None and ophys_experiment_id is not None:
            mock_sync_file_instance = mock_sync_file.from_lims(
                mock_db_conn, ophys_experiment_id
            )
            ts_from_sync = StimulusTimestamps.from_sync_file(
                sync_file=mock_sync_file_instance)

    if behavior_session_id is not None and ophys_experiment_id is not None:
        mock_get_ophys_stimulus_timestamps.assert_called_once_with(
            sync_path=mock_sync_file_instance.filepath
        )
        assert ts_from_sync._sync_file == mock_sync_file_instance
    else:
        mock_stimulus_file.from_lims.assert_called_with(
            mock_db_conn, behavior_session_id
        )
        mock_get_behavior_stimulus_timestamps.assert_called_once_with(
            stimulus_pkl=mock_stimulus_file_instance.data
        )
Ejemplo n.º 22
0
    def from_lims(cls, behavior_session_id: int,
                  lims_db: Optional[PostgresQueryMixin] = None,
                  stimulus_timestamps: Optional[StimulusTimestamps] = None,
                  monitor_delay: Optional[float] = None,
                  date_of_acquisition: Optional[DateOfAcquisition] = None) \
            -> "BehaviorSession":
        """

        Parameters
        ----------
        behavior_session_id
            Behavior session id
        lims_db
            Database connection. If not provided will create a new one.
        stimulus_timestamps
            Stimulus timestamps. If not provided, will calculate stimulus
            timestamps from stimulus file.
        monitor_delay
            Monitor delay. If not provided, will use an estimate.
            To provide this value, see for example
            allensdk.brain_observatory.behavior.data_objects.stimuli.util.
            calculate_monitor_delay
        date_of_acquisition
            Date of acquisition. If not provided, will read from
            behavior_sessions table.
        Returns
        -------
        `BehaviorSession` instance
        """
        if lims_db is None:
            lims_db = db_connection_creator(
                fallback_credentials=LIMS_DB_CREDENTIAL_MAP)

        behavior_session_id = BehaviorSessionId(behavior_session_id)
        stimulus_file = StimulusFile.from_lims(
            db=lims_db, behavior_session_id=behavior_session_id.value)
        if stimulus_timestamps is None:
            stimulus_timestamps = StimulusTimestamps.from_stimulus_file(
                stimulus_file=stimulus_file)
        running_acquisition = RunningAcquisition.from_lims(
            lims_db, behavior_session_id.value)
        raw_running_speed = RunningSpeed.from_lims(
            lims_db,
            behavior_session_id.value,
            filtered=False,
            stimulus_timestamps=stimulus_timestamps)
        running_speed = RunningSpeed.from_lims(
            lims_db,
            behavior_session_id.value,
            stimulus_timestamps=stimulus_timestamps)
        behavior_metadata = BehaviorMetadata.from_lims(
            behavior_session_id=behavior_session_id, lims_db=lims_db)

        if monitor_delay is None:
            monitor_delay = cls._get_monitor_delay()

        licks, rewards, stimuli, task_parameters, trials = \
            cls._read_data_from_stimulus_file(
                stimulus_file=stimulus_file,
                stimulus_timestamps=stimulus_timestamps,
                trial_monitor_delay=monitor_delay
            )
        if date_of_acquisition is None:
            date_of_acquisition = DateOfAcquisition.from_lims(
                behavior_session_id=behavior_session_id.value, lims_db=lims_db)
        date_of_acquisition = date_of_acquisition.validate(
            stimulus_file=stimulus_file,
            behavior_session_id=behavior_session_id.value)

        return BehaviorSession(behavior_session_id=behavior_session_id,
                               stimulus_timestamps=stimulus_timestamps,
                               running_acquisition=running_acquisition,
                               raw_running_speed=raw_running_speed,
                               running_speed=running_speed,
                               metadata=behavior_metadata,
                               licks=licks,
                               rewards=rewards,
                               stimuli=stimuli,
                               task_parameters=task_parameters,
                               trials=trials,
                               date_of_acquisition=date_of_acquisition)
Ejemplo n.º 23
0
    def from_lims(cls,
                  ophys_experiment_id: int,
                  eye_tracking_z_threshold: float = 3.0,
                  eye_tracking_dilation_frames: int = 2,
                  events_filter_scale: float = 2.0,
                  events_filter_n_time_steps: int = 20,
                  exclude_invalid_rois=True,
                  skip_eye_tracking=False) -> \
            "BehaviorOphysExperiment":
        """
        Parameters
        ----------
        ophys_experiment_id
        eye_tracking_z_threshold
            See `BehaviorOphysExperiment.from_nwb`
        eye_tracking_dilation_frames
            See `BehaviorOphysExperiment.from_nwb`
        events_filter_scale
            See `BehaviorOphysExperiment.from_nwb`
        events_filter_n_time_steps
            See `BehaviorOphysExperiment.from_nwb`
        exclude_invalid_rois
            Whether to exclude invalid rois
        skip_eye_tracking
            Used to skip returning eye tracking data
        """
        def _is_multi_plane_session():
            imaging_plane_group_meta = ImagingPlaneGroup.from_lims(
                ophys_experiment_id=ophys_experiment_id, lims_db=lims_db)
            return cls._is_multi_plane_session(
                imaging_plane_group_meta=imaging_plane_group_meta)

        def _get_motion_correction():
            rigid_motion_transform_file = RigidMotionTransformFile.from_lims(
                ophys_experiment_id=ophys_experiment_id, db=lims_db)
            return MotionCorrection.from_data_file(
                rigid_motion_transform_file=rigid_motion_transform_file)

        def _get_eye_tracking_table(sync_file: SyncFile):
            eye_tracking_file = EyeTrackingFile.from_lims(
                db=lims_db, ophys_experiment_id=ophys_experiment_id)
            eye_tracking_table = EyeTrackingTable.from_data_file(
                data_file=eye_tracking_file,
                sync_file=sync_file,
                z_threshold=eye_tracking_z_threshold,
                dilation_frames=eye_tracking_dilation_frames)
            return eye_tracking_table

        lims_db = db_connection_creator(
            fallback_credentials=LIMS_DB_CREDENTIAL_MAP)
        sync_file = SyncFile.from_lims(db=lims_db,
                                       ophys_experiment_id=ophys_experiment_id)
        stimulus_timestamps = StimulusTimestamps.from_sync_file(
            sync_file=sync_file)
        behavior_session_id = BehaviorSessionId.from_lims(
            db=lims_db, ophys_experiment_id=ophys_experiment_id)
        is_multiplane_session = _is_multi_plane_session()
        meta = BehaviorOphysMetadata.from_lims(
            ophys_experiment_id=ophys_experiment_id,
            lims_db=lims_db,
            is_multiplane=is_multiplane_session)
        monitor_delay = calculate_monitor_delay(
            sync_file=sync_file, equipment=meta.behavior_metadata.equipment)
        date_of_acquisition = DateOfAcquisitionOphys.from_lims(
            ophys_experiment_id=ophys_experiment_id, lims_db=lims_db)
        behavior_session = BehaviorSession.from_lims(
            lims_db=lims_db,
            behavior_session_id=behavior_session_id.value,
            stimulus_timestamps=stimulus_timestamps,
            monitor_delay=monitor_delay,
            date_of_acquisition=date_of_acquisition)
        if is_multiplane_session:
            ophys_timestamps = OphysTimestampsMultiplane.from_sync_file(
                sync_file=sync_file,
                group_count=meta.ophys_metadata.imaging_plane_group_count,
                plane_group=meta.ophys_metadata.imaging_plane_group)
        else:
            ophys_timestamps = OphysTimestamps.from_sync_file(
                sync_file=sync_file)

        projections = Projections.from_lims(
            ophys_experiment_id=ophys_experiment_id, lims_db=lims_db)
        cell_specimens = CellSpecimens.from_lims(
            ophys_experiment_id=ophys_experiment_id,
            lims_db=lims_db,
            ophys_timestamps=ophys_timestamps,
            segmentation_mask_image_spacing=projections.max_projection.spacing,
            events_params=EventsParams(
                filter_scale=events_filter_scale,
                filter_n_time_steps=events_filter_n_time_steps),
            exclude_invalid_rois=exclude_invalid_rois)
        motion_correction = _get_motion_correction()
        if skip_eye_tracking:
            eye_tracking_table = None
            eye_tracking_rig_geometry = None
        else:
            eye_tracking_table = _get_eye_tracking_table(sync_file=sync_file)
            eye_tracking_rig_geometry = EyeTrackingRigGeometry.from_lims(
                ophys_experiment_id=ophys_experiment_id, lims_db=lims_db)

        return BehaviorOphysExperiment(
            behavior_session=behavior_session,
            cell_specimens=cell_specimens,
            ophys_timestamps=ophys_timestamps,
            metadata=meta,
            projections=projections,
            motion_correction=motion_correction,
            eye_tracking_table=eye_tracking_table,
            eye_tracking_rig_geometry=eye_tracking_rig_geometry,
            date_of_acquisition=date_of_acquisition)
Ejemplo n.º 24
0
 def test_from_stimulus_file(self):
     st = StimulusTimestamps.from_stimulus_file(
         stimulus_file=self.stimulus_file)
     licks = Licks.from_stimulus_file(stimulus_file=self.stimulus_file,
                                      stimulus_timestamps=st)
     assert licks == self.expected