Example #1
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)
Example #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)
Example #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)
Example #4
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)
Example #5
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())
Example #6
0
 def _read_data_from_stimulus_file(cls, stimulus_file: StimulusFile,
                                   stimulus_timestamps: StimulusTimestamps,
                                   trial_monitor_delay: float):
     """Helper method to read data from stimulus file"""
     licks = Licks.from_stimulus_file(
         stimulus_file=stimulus_file,
         stimulus_timestamps=stimulus_timestamps)
     rewards = Rewards.from_stimulus_file(
         stimulus_file=stimulus_file,
         stimulus_timestamps=stimulus_timestamps)
     stimuli = Stimuli.from_stimulus_file(
         stimulus_file=stimulus_file,
         stimulus_timestamps=stimulus_timestamps)
     task_parameters = TaskParameters.from_stimulus_file(
         stimulus_file=stimulus_file)
     trials = TrialTable.from_stimulus_file(
         stimulus_file=stimulus_file,
         stimulus_timestamps=stimulus_timestamps,
         licks=licks,
         rewards=rewards,
         monitor_delay=trial_monitor_delay)
     return licks, rewards, stimuli, task_parameters, trials
Example #7
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