예제 #1
0
def test_frame_time_offset():
    mock_data = {
        "items": {
            "behavior": {
                "trial_log":
                    [
                        {
                            "events":
                                [
                                    ["trial_start", 2, 1],
                                    ["trial_end", 3, 2],
                                ],
                        },
                        {
                            "events":
                                [
                                    ["trial_start", 4, 3],
                                    ["trial_start", 5, 4],
                                ],
                        },
                    ],
                },
            },
        }
    actual = frame_time_offset(mock_data)
    assert 1. == actual
예제 #2
0
    def get_rewards(self) -> pd.DataFrame:
        """Get reward data from pkl file, based on pkl file timestamps
        (not sync file).

        :returns: pd.DataFrame -- A dataframe containing timestamps of
            delivered rewards.
        """
        data = self._behavior_stimulus_file()
        offset = frame_time_offset(data)
        # No sync timestamps to rebase on, but do need to align to
        # trial events, so add the offset as the "rebase" function
        return get_rewards(data, lambda x: x + offset)
예제 #3
0
 def get_licks(self):
     behavior_stimulus_file = self.get_behavior_stimulus_file()
     data = pd.read_pickle(behavior_stimulus_file)
     rebase_function = self.get_stimulus_rebase_function()
     # Get licks from pickle file (need to add an offset to align with
     # the trial_log time stream)
     lick_frames = (
         data["items"]["behavior"]["lick_sensors"][0]["lick_events"])
     vsyncs = data["items"]["behavior"]["intervalsms"]
     vsync_times_raw = np.hstack(
         (0, vsyncs)).cumsum() / 1000.0  # cumulative time
     vsync_offset = frame_time_offset(data)
     vsync_times = vsync_times_raw + vsync_offset
     lick_times = [vsync_times[frame] for frame in lick_frames]
     # Align pickle data with sync time stream
     return pd.DataFrame({"time": list(map(rebase_function, lick_times))})
예제 #4
0
    def get_stimulus_timestamps(self) -> np.ndarray:
        """Get stimulus timestamps (vsyncs) from pkl file. Align to the
        (frame, time) points in the trial events.

        NOTE: Located with behavior_session_id. Does not use the sync_file
        which requires ophys_session_id.

        Returns
        -------
        np.ndarray
            Timestamps associated with stimulus presentations on the monitor
            that do no account for monitor delay.
        """
        data = self._behavior_stimulus_file()
        vsyncs = data["items"]["behavior"]["intervalsms"]
        cum_sum = np.hstack((0, vsyncs)).cumsum() / 1000.0  # cumulative time
        offset = frame_time_offset(data)
        return cum_sum + offset
예제 #5
0
    def get_licks(self) -> pd.DataFrame:
        """Get lick data from pkl file.
        This function assumes that the first sensor in the list of
        lick_sensors is the desired lick sensor. If this changes we need
        to update to get the proper line.

        Since licks can occur outside of a trial context, the lick times
        are extracted from the vsyncs and the frame number in `lick_events`.
        Since we don't have a timestamp for when in "experiment time" the
        vsync stream starts (from self.get_stimulus_timestamps), we compute
        it by fitting a linear regression (frame number x time) for the
        `start_trial` and `end_trial` events in the `trial_log`, to true
        up these time streams.

        :returns: pd.DataFrame -- A dataframe containing lick timestamps
        """
        # Get licks from pickle file instead of sync
        data = self._behavior_stimulus_file()
        offset = frame_time_offset(data)
        stimulus_timestamps = self.get_stimulus_timestamps() + offset
        lick_frames = (data["items"]["behavior"]["lick_sensors"][0]
                       ["lick_events"])
        lick_times = [stimulus_timestamps[frame] for frame in lick_frames]
        return pd.DataFrame({"time": lick_times})