def test_running_acquisition_nwb_roundtrip(nwbfile,
                                           data_object_roundtrip_fixture,
                                           roundtrip, running_acq_data):
    running_acq = RunningAcquisition(running_acquisition=running_acq_data)
    nwbfile = running_acq.to_nwb(nwbfile)

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

    pd.testing.assert_frame_equal(obt.value, running_acq_data, check_like=True)
Пример #2
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)
Пример #3
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)
def test_running_acquisition_to_json(stimulus_file, stimulus_file_to_json_ret,
                                     stimulus_timestamps,
                                     stimulus_timestamps_to_json_ret, raises,
                                     expected):
    if stimulus_file is not None:
        stimulus_file.to_json.return_value = stimulus_file_to_json_ret
    if stimulus_timestamps is not None:
        stimulus_timestamps.to_json.return_value = (
            stimulus_timestamps_to_json_ret)

    running_acq = RunningAcquisition(running_acquisition=None,
                                     stimulus_file=stimulus_file,
                                     stimulus_timestamps=stimulus_timestamps)

    if raises:
        with pytest.raises(RuntimeError, match=raises):
            _ = running_acq.to_json()
    else:
        obt = running_acq.to_json()
        assert obt == expected
def test_running_acquisition_from_lims(monkeypatch, behavior_session_id,
                                       ophys_experiment_id,
                                       returned_running_acq_df,
                                       expected_running_acq_df):
    mock_db_conn = create_autospec(PostgresQueryMixin, instance=True)

    mock_stimulus_file = create_autospec(StimulusFile)
    mock_stimulus_timestamps = create_autospec(StimulusTimestamps)
    mock_get_running_df = create_autospec(get_running_df)

    mock_get_running_df.return_value = returned_running_acq_df

    with monkeypatch.context() as m:
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.StimulusFile",
            mock_stimulus_file)
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.StimulusTimestamps",
            mock_stimulus_timestamps)
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.get_running_df",
            mock_get_running_df)
        obt = RunningAcquisition.from_lims(mock_db_conn, behavior_session_id,
                                           ophys_experiment_id)

    mock_stimulus_file.from_lims.assert_called_once_with(
        mock_db_conn, behavior_session_id)
    mock_stimulus_file_instance = mock_stimulus_file.from_lims(
        mock_db_conn, behavior_session_id)
    assert obt._stimulus_file == mock_stimulus_file_instance

    mock_stimulus_timestamps.from_stimulus_file.assert_called_once_with(
        mock_stimulus_file_instance)
    mock_stimulus_timestamps_instance = mock_stimulus_timestamps.\
        from_stimulus_file(stimulus_file=mock_stimulus_file)
    assert obt._stimulus_timestamps == mock_stimulus_timestamps_instance

    mock_get_running_df.assert_called_once_with(
        data=mock_stimulus_file_instance.data,
        time=mock_stimulus_timestamps_instance.value,
    )

    pd.testing.assert_frame_equal(obt.value,
                                  expected_running_acq_df,
                                  check_like=True)
def test_running_acquisition_from_json(monkeypatch, dict_repr,
                                       returned_running_acq_df,
                                       expected_running_acq_df):
    mock_stimulus_file = create_autospec(StimulusFile)
    mock_stimulus_timestamps = create_autospec(StimulusTimestamps)
    mock_get_running_df = create_autospec(get_running_df)

    mock_get_running_df.return_value = returned_running_acq_df

    with monkeypatch.context() as m:
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.StimulusFile",
            mock_stimulus_file)
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.StimulusTimestamps",
            mock_stimulus_timestamps)
        m.setattr(
            "allensdk.brain_observatory.behavior.data_objects"
            ".running_speed.running_acquisition.get_running_df",
            mock_get_running_df)
        obt = RunningAcquisition.from_json(dict_repr)

    mock_stimulus_file.from_json.assert_called_once_with(dict_repr)
    mock_stimulus_file_instance = mock_stimulus_file.from_json(dict_repr)
    assert obt._stimulus_file == mock_stimulus_file_instance

    mock_stimulus_timestamps.from_json.assert_called_once_with(dict_repr)
    mock_stimulus_timestamps_instance = mock_stimulus_timestamps.from_json(
        dict_repr)
    assert obt._stimulus_timestamps == mock_stimulus_timestamps_instance

    mock_get_running_df.assert_called_once_with(
        data=mock_stimulus_file_instance.data,
        time=mock_stimulus_timestamps_instance.value,
    )

    pd.testing.assert_frame_equal(obt.value, expected_running_acq_df)
Пример #7
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)