def get_ophys_frames(dataset: SyncDataset, permissive: bool = False) -> np.ndarray: """ Report the timestamps of each optical physiology video frame Parameters ---------- dataset : describes experiment timing Returns ------- array of timestamps (floating point; seconds; relative to experiment start). permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Notes ----- use rising edge for Scientifica, falling edge for Nikon http://confluence.corp.alleninstitute.org/display/IT/Ophys+Time+Sync This function uses rising edges """ try: return dataset.get_edges("rising", '2p_vsync', "seconds") except KeyError: if not permissive: raise return
def get_synchronized_frame_times(session_sync_file: Path, sync_line_label_keys: Tuple[str, ...]) -> pd.Series: """Get experimental frame times from an experiment session sync file. Parameters ---------- session_sync_file : Path Path to an ephys session sync file. The sync file contains rising/falling edges from a daq system which indicates when certain events occur (so they can be related to each other). sync_line_label_keys : Tuple[str, ...] Line label keys to get times for. See class attributes of allensdk.brain_observatory.sync_dataset.Dataset for a listing of possible keys. Returns ------- pd.Series An array of times when frames for the eye tracking camera were acquired. """ sync_dataset = Dataset(str(session_sync_file)) frame_times = sync_dataset.get_edges( "rising", sync_line_label_keys, units="seconds" ) # Occasionally an extra set of frame times are acquired after the rest of # the signals. We detect and remove these. frame_times = trim_discontiguous_times(frame_times) return pd.Series(frame_times)
def get_trigger(dataset: SyncDataset, permissive: bool = False) -> Optional[np.ndarray]: """ Returns (as a 1-element array) the time at which optical physiology acquisition was started. Parameters ---------- dataset : describes experiment timing permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Returns ------- timestamps (floating point; seconds; relative to experiment start) or None. If None, no timestamps were found in this sync dataset. Notes ----- Ophys frame timestamps can be recorded before acquisition start when experimenters are setting up the recording session. These do not correspond to acquired ophys frames. """ return dataset.get_edges("rising", ["2p_trigger", "acq_trigger"], "seconds", permissive)
def get_raw_stimulus_frames( dataset: SyncDataset, permissive: bool = False ) -> np.ndarray: """ Report the raw timestamps of each stimulus frame. This corresponds to the time at which the psychopy window's flip method returned, but not necessarily to the time at which the stimulus frame was displayed. Parameters ---------- dataset : describes experiment timing permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Returns ------- array of timestamps (floating point; seconds; relative to experiment start). """ try: return dataset.get_edges("falling",'stim_vsync', "seconds") except KeyError: if not permissive: raise return
def main(stimulus_pkl_path, sync_h5_path, output_path, wheel_radius, subject_position, use_median_duration, **kwargs): stim_file = pd.read_pickle(stimulus_pkl_path) sync_dataset = Dataset(sync_h5_path) # Why the rising edge? See Sweepstim.update in camstim. This method does: # 1. updates the stimuli # 2. updates the "items", causing a running speed sample to be acquired # 3. sets the vsync line high # 4. flips the buffer frame_times = sync_dataset.get_edges("rising", Dataset.FRAME_KEYS, units="seconds") # occasionally an extra set of frame times are acquired after the rest of # the signals. We detect and remove these frame_times = sync_utilities.trim_discontiguous_times(frame_times) num_raw_timestamps = len(frame_times) dx_deg = running_from_stim_file(stim_file, "dx", num_raw_timestamps) if num_raw_timestamps != len(dx_deg): raise ValueError( f"found {num_raw_timestamps} rising edges on the vsync line, " f"but only {len(dx_deg)} rotation samples") vsig = running_from_stim_file(stim_file, "vsig", num_raw_timestamps) vin = running_from_stim_file(stim_file, "vin", num_raw_timestamps) velocities = extract_running_speeds( frame_times=frame_times, dx_deg=dx_deg, vsig=vsig, vin=vin, wheel_radius=wheel_radius, subject_position=subject_position, use_median_duration=use_median_duration) raw_data = pd.DataFrame({ "vsig": vsig, "vin": vin, "frame_time": frame_times, "dx": dx_deg }) store = pd.HDFStore(output_path) store.put("running_speed", velocities) store.put("raw_data", raw_data) store.close() return {"output_path": output_path}
def get_lick_times(dataset: SyncDataset, permissive: bool = False) -> Optional[np.ndarray]: """ Report the timestamps of each detected lick Parameters ---------- dataset : describes experiment timing permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Returns ------- array of timestamps (floating point; seconds; relative to experiment start) or None. If None, no lick timestamps were found in this sync dataset. """ return dataset.get_edges("rising", ["lick_times", "lick_sensor"], "seconds", permissive)
def get_eye_tracking(dataset: SyncDataset, permissive: bool = False) -> Optional[np.ndarray]: """ Report the timestamps of each frame of the eye tracking video Parameters ---------- dataset : describes experiment timing permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Returns ------- array of timestamps (floating point; seconds; relative to experiment start) or None. If None, no eye tracking timestamps were found in this sync dataset. """ return dataset.get_edges("rising", ["cam2_exposure", "eye_tracking"], "seconds", permissive)
def get_stim_photodiode(dataset: SyncDataset, permissive: bool = False) -> Optional[List[float]]: """ Report the timestamps of each detected sync square transition (both black -> white and white -> black) in this experiment. Parameters ---------- dataset : describes experiment timing permissive : If True, None will be returned if timestamps are not found. If False, a KeyError will be raised Returns ------- array of timestamps (floating point; seconds; relative to experiment start) or None. If None, no photodiode timestamps were found in this sync dataset. """ return dataset.get_edges("all", ["stim_photodiode", "photodiode"], "seconds", permissive)
def get_synchronized_camera_frame_times(session_sync_file: Path) -> pd.Series: """Get eye tracking camera frame times from an experiment session sync file. Args: session_sync_file (Path): Path to an ephys session sync file. The sync file contains rising/falling edges from a daq system which indicates when certain events occur (so they can be related to each other). Returns: pandas.Series: An array of times when frames for the eye tracking camera were acquired. """ sync_dataset = Dataset(str(session_sync_file)) frame_times = sync_dataset.get_edges("rising", Dataset.EYE_TRACKING_KEYS, units="seconds") # Occasionally an extra set of frame times are acquired after the rest of # the signals. We detect and remove these. frame_times = sync_utilities.trim_discontiguous_times(frame_times) return pd.Series(frame_times)