Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
0
def test_stimulus_file_from_lims(stimulus_file_fixture, behavior_session_id):
    stim_pkl_path, stim_pkl_data = stimulus_file_fixture

    mock_db_conn = create_autospec(PostgresQueryMixin, instance=True)

    # Basic test case
    mock_db_conn.fetchone.return_value = str(stim_pkl_path)
    stimulus_file = StimulusFile.from_lims(mock_db_conn, behavior_session_id)
    assert stimulus_file.data == stim_pkl_data

    # Now test caching by deleting stimulus_file and also asserting db
    # `fetchone` called only once
    stim_pkl_path.unlink()
    stimfile_cached = StimulusFile.from_lims(mock_db_conn, behavior_session_id)
    assert stimfile_cached.data == stim_pkl_data

    query = STIMULUS_FILE_QUERY_TEMPLATE.format(
        behavior_session_id=behavior_session_id)

    mock_db_conn.fetchone.assert_called_once_with(query, strict=True)
Exemplo n.º 4
0
    def from_lims(
            cls,
            db: PostgresQueryMixin,
            behavior_session_id: int,
            ophys_experiment_id: Optional[int] = None) -> "StimulusTimestamps":
        stimulus_file = StimulusFile.from_lims(db, behavior_session_id)

        if ophys_experiment_id:
            sync_file = SyncFile.from_lims(
                db=db, ophys_experiment_id=ophys_experiment_id)
            return cls.from_sync_file(sync_file=sync_file)
        else:
            return cls.from_stimulus_file(stimulus_file=stimulus_file)
Exemplo n.º 5
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)
Exemplo n.º 6
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,
        )
Exemplo n.º 7
0
 def _get_stimulus_data():
     lims_db = db_connection_creator(
         fallback_credentials=LIMS_DB_CREDENTIAL_MAP)
     stimulus_file = StimulusFile.from_lims(
         db=lims_db, behavior_session_id=behavior_experiment_id)
     return stimulus_file.data
Exemplo n.º 8
0
 def test_from_stimulus_file(self):
     stimulus_file = StimulusFile.from_lims(
         behavior_session_id=self.behavior_session_id, db=self.dbconn)
     tp = TaskParameters.from_stimulus_file(stimulus_file=stimulus_file)
     assert tp == self.expected
Exemplo n.º 9
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)