def test_process_eye_tracking_data_raises_on_sync_error(eye_tracking_df, frame_times): """ Test that an error is raised when the number of sync timestamps exceeds the number of eye tracking frames by more than 15 """ with pytest.raises(RuntimeError, match='Error! The number of sync file'): process_eye_tracking_data(eye_tracking_df, frame_times)
def from_data_file(cls, data_file: EyeTrackingFile, sync_file: SyncFile, z_threshold: float = 3.0, dilation_frames: int = 2) -> "EyeTrackingTable": """ Parameters ---------- data_file sync_file z_threshold : float, optional See EyeTracking.from_lims dilation_frames : int, optional See EyeTracking.from_lims """ cls._logger.info(f"Getting eye_tracking_data with " f"'z_threshold={z_threshold}', " f"'dilation_frames={dilation_frames}'") sync_path = Path(sync_file.filepath) frame_times = sync_utilities.get_synchronized_frame_times( session_sync_file=sync_path, sync_line_label_keys=Dataset.EYE_TRACKING_KEYS, trim_after_spike=False) eye_tracking_data = process_eye_tracking_data(data_file.data, frame_times, z_threshold, dilation_frames) return EyeTrackingTable(eye_tracking=eye_tracking_data)
def test_process_eye_tracking_data_truncation(eye_tracking_df, frame_times): """ Test that the array of sync times is truncated when the number of raw sync timestamps exceeds the numer of eye tracking frames by <= 15 """ df = process_eye_tracking_data(eye_tracking_df, frame_times) np.testing.assert_array_almost_equal(df.timestamps.to_numpy(), np.array([0.0, 0.1]), decimal=10)
def get_eye_tracking(self, z_threshold: float = 3.0, dilation_frames: int = 2) -> Optional[pd.DataFrame]: """Gets corneal, eye, and pupil ellipse fit data Parameters ---------- z_threshold : float, optional The z-threshold when determining which frames likely contain outliers for eye or pupil areas. Influences which frames are considered 'likely blinks'. By default 3.0 dilation_frames : int, optional Determines the number of additional adjacent frames to mark as 'likely_blink', by default 2. Returns ------- Optional[pd.DataFrame] *_area *_center_x *_center_y *_height *_phi *_width likely_blink where "*" can be "corneal", "pupil" or "eye" Will return None if class attr _skip_eye_tracking is True. """ if self._skip_eye_tracking: return None self.logger.info(f"Getting eye_tracking_data with " f"'z_threshold={z_threshold}', " f"'dilation_frames={dilation_frames}'") filepath = Path(self.extractor.get_eye_tracking_filepath()) sync_path = Path(self.extractor.get_sync_file()) eye_tracking_data = load_eye_tracking_hdf(filepath) frame_times = sync_utilities.get_synchronized_frame_times( session_sync_file=sync_path, sync_line_label_keys=Dataset.EYE_TRACKING_KEYS, trim_after_spike=False) eye_tracking_data = process_eye_tracking_data(eye_tracking_data, frame_times, z_threshold, dilation_frames) return eye_tracking_data
def get_eye_tracking(self, z_threshold: float = 3.0, dilation_frames: int = 2): logger = logging.getLogger("BehaviorOphysLimsApi") logger.info(f"Getting eye_tracking_data with " f"'z_threshold={z_threshold}', " f"'dilation_frames={dilation_frames}'") filepath = Path(self.get_eye_tracking_filepath()) sync_path = Path(self.get_sync_file()) eye_tracking_data = load_eye_tracking_hdf(filepath) frame_times = sync_utilities.get_synchronized_frame_times( session_sync_file=sync_path, sync_line_label_keys=Dataset.EYE_TRACKING_KEYS) eye_tracking_data = process_eye_tracking_data(eye_tracking_data, frame_times, z_threshold, dilation_frames) return eye_tracking_data
len(e) s.line_labels() s.line_labels len(e.index) fts.shape len(e.index) - fts.shape len(e.index) - fts.shape[0] e = sdk_eye.load_eye_tracking_hdf(r"\\allen\programs\braintv\production\visualbehavior\prod0\specimen_1035469407\ecephys_session_1055240613\eye_tracking\1055240613_ellipse.h5") s = sync_dataset(r"\\allen\programs\braintv\production\visualbehavior\prod0\specimen_1035469407\ecephys_session_1055240613\1055324726\1055240613_533539_20201007.sync") fts = s.get_rising_edges('eye_frame_received') fts.shape len(e) e = e.iloc[1:] len(e) fts = pd.Series(fts) e_p = sdk_eye.process_eye_tracking_data(e, fts) e_p.keys() plt.plot(e_p['time'], e_p['pupil_area']) plt.plot(e_p['time'], e_p['pupil_center_x']) pa = e_p['pupil_area'].loc[~e_p['likely_blink']] e_p['likely_blink'].head() np.sum(e_p['likely_blink']) len(e_p) pa = e_p['pupil_area'].loc[!e_p['likely_blink']] pa = e_p['pupil_area'].loc[e_p['likely_blink']] plt.plot(e_p['time'], e_p['pupil_area']) plt.plot(e_p['time'], e_p['likely_blink']*100) plt.plot(e_p['time'], e_p['likely_blink']*100000) pa = e_p['pupil_area'].values pa[e_p['likely_blink']] = np.nan pa[e_p['likely_blink'].values] = np.nan
def test_process_eye_tracking_data(eye_tracking_df, frame_times, expected): obtained = process_eye_tracking_data(eye_tracking_df, frame_times) pd.testing.assert_frame_equal(obtained, expected)
def test_process_eye_tracking_data(eye_tracking_df, frame_times, expected): obtained = process_eye_tracking_data(eye_tracking_df, frame_times) assert expected.equals(obtained)
def test_process_eye_tracking_data_raises_on_sync_error( eye_tracking_df, frame_times): with pytest.raises(RuntimeError, match='Error! The number of sync file'): _ = process_eye_tracking_data(eye_tracking_df, frame_times)