def test_stimulus_file_from_json(stimulus_file_fixture):
    stim_pkl_path, stim_pkl_data = stimulus_file_fixture

    # Basic test case
    input_json_dict = {"behavior_stimulus_file": str(stim_pkl_path)}
    stimulus_file = StimulusFile.from_json(input_json_dict)
    assert stimulus_file.data == stim_pkl_data

    # Now test caching by deleting the stimulus_file
    stim_pkl_path.unlink()
    stimulus_file_cached = StimulusFile.from_json(input_json_dict)
    assert stimulus_file_cached.data == stim_pkl_data
Beispiel #2
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)
Beispiel #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)
Beispiel #4
0
 def from_json(cls, dict_repr: dict) -> "StimulusTimestamps":
     if 'sync_file' in dict_repr:
         sync_file = SyncFile.from_json(dict_repr=dict_repr)
         return cls.from_sync_file(sync_file=sync_file)
     else:
         stim_file = StimulusFile.from_json(dict_repr=dict_repr)
         return cls.from_stimulus_file(stimulus_file=stim_file)
Beispiel #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)
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
    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,
        )
Beispiel #11
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())
Beispiel #12
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)
 def setup_class(cls):
     dir = Path(__file__).parent.parent.parent.resolve()
     test_data_dir = dir / 'test_data'
     sf_path = test_data_dir / 'stimulus_file.pkl'
     cls.stimulus_file = StimulusFile.from_json(
         dict_repr={'behavior_stimulus_file': str(sf_path)})