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)
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)
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)
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 setup_class(cls): dir = Path(__file__).parent.resolve() test_data_dir = dir / 'test_data' cls.stimulus_file = StimulusFile(filepath=test_data_dir / 'behavior_stimulus_file.pkl') expected = pd.read_pickle(str(test_data_dir / 'licks.pkl')) cls.expected = Licks(licks=expected)
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)
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())
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
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