def test_stimulus_timestamps_from_json( monkeypatch, dict_repr, has_pkl, has_sync ): mock_stimulus_file = create_autospec(StimulusFile) mock_sync_file = create_autospec(SyncFile) mock_get_behavior_stimulus_timestamps = create_autospec( get_behavior_stimulus_timestamps ) mock_get_ophys_stimulus_timestamps = create_autospec( get_ophys_stimulus_timestamps ) with monkeypatch.context() as m: m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps.StimulusFile", mock_stimulus_file ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps.SyncFile", mock_sync_file ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps" ".get_behavior_stimulus_timestamps", mock_get_behavior_stimulus_timestamps ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps" ".get_ophys_stimulus_timestamps", mock_get_ophys_stimulus_timestamps ) mock_stimulus_file_instance = mock_stimulus_file.from_json(dict_repr) ts_from_stim = StimulusTimestamps.from_stimulus_file( stimulus_file=mock_stimulus_file_instance) if has_pkl and has_sync: mock_sync_file_instance = mock_sync_file.from_json(dict_repr) ts_from_sync = StimulusTimestamps.from_sync_file( sync_file=mock_sync_file_instance) if has_pkl and has_sync: mock_get_ophys_stimulus_timestamps.assert_called_once_with( sync_path=mock_sync_file_instance.filepath ) assert ts_from_sync._sync_file == mock_sync_file_instance else: assert ts_from_stim._stimulus_file == mock_stimulus_file_instance mock_get_behavior_stimulus_timestamps.assert_called_once_with( stimulus_pkl=mock_stimulus_file_instance.data )
def test_stimulus_timestamps_from_lims( monkeypatch, behavior_session_id, ophys_experiment_id ): mock_db_conn = create_autospec(PostgresQueryMixin, instance=True) mock_stimulus_file = create_autospec(StimulusFile) mock_sync_file = create_autospec(SyncFile) mock_get_behavior_stimulus_timestamps = create_autospec( get_behavior_stimulus_timestamps ) mock_get_ophys_stimulus_timestamps = create_autospec( get_ophys_stimulus_timestamps ) with monkeypatch.context() as m: m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps.StimulusFile", mock_stimulus_file ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps.SyncFile", mock_sync_file ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps" ".get_behavior_stimulus_timestamps", mock_get_behavior_stimulus_timestamps ) m.setattr( "allensdk.brain_observatory.behavior.data_objects" ".timestamps.stimulus_timestamps.stimulus_timestamps" ".get_ophys_stimulus_timestamps", mock_get_ophys_stimulus_timestamps ) mock_stimulus_file_instance = mock_stimulus_file.from_lims( mock_db_conn, behavior_session_id ) ts_from_stim = StimulusTimestamps.from_stimulus_file( stimulus_file=mock_stimulus_file_instance) assert ts_from_stim._stimulus_file == mock_stimulus_file_instance if behavior_session_id is not None and ophys_experiment_id is not None: mock_sync_file_instance = mock_sync_file.from_lims( mock_db_conn, ophys_experiment_id ) ts_from_sync = StimulusTimestamps.from_sync_file( sync_file=mock_sync_file_instance) if behavior_session_id is not None and ophys_experiment_id is not None: mock_get_ophys_stimulus_timestamps.assert_called_once_with( sync_path=mock_sync_file_instance.filepath ) assert ts_from_sync._sync_file == mock_sync_file_instance else: mock_stimulus_file.from_lims.assert_called_with( mock_db_conn, behavior_session_id ) mock_get_behavior_stimulus_timestamps.assert_called_once_with( stimulus_pkl=mock_stimulus_file_instance.data )
def from_lims(cls, ophys_experiment_id: int, eye_tracking_z_threshold: float = 3.0, eye_tracking_dilation_frames: int = 2, events_filter_scale: float = 2.0, events_filter_n_time_steps: int = 20, exclude_invalid_rois=True, skip_eye_tracking=False) -> \ "BehaviorOphysExperiment": """ Parameters ---------- ophys_experiment_id eye_tracking_z_threshold See `BehaviorOphysExperiment.from_nwb` eye_tracking_dilation_frames See `BehaviorOphysExperiment.from_nwb` events_filter_scale See `BehaviorOphysExperiment.from_nwb` events_filter_n_time_steps See `BehaviorOphysExperiment.from_nwb` exclude_invalid_rois Whether to exclude invalid rois skip_eye_tracking Used to skip returning eye tracking data """ def _is_multi_plane_session(): imaging_plane_group_meta = ImagingPlaneGroup.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db) return cls._is_multi_plane_session( imaging_plane_group_meta=imaging_plane_group_meta) def _get_motion_correction(): rigid_motion_transform_file = RigidMotionTransformFile.from_lims( ophys_experiment_id=ophys_experiment_id, db=lims_db) return MotionCorrection.from_data_file( rigid_motion_transform_file=rigid_motion_transform_file) def _get_eye_tracking_table(sync_file: SyncFile): eye_tracking_file = EyeTrackingFile.from_lims( db=lims_db, ophys_experiment_id=ophys_experiment_id) eye_tracking_table = EyeTrackingTable.from_data_file( data_file=eye_tracking_file, sync_file=sync_file, z_threshold=eye_tracking_z_threshold, dilation_frames=eye_tracking_dilation_frames) return eye_tracking_table lims_db = db_connection_creator( fallback_credentials=LIMS_DB_CREDENTIAL_MAP) sync_file = SyncFile.from_lims(db=lims_db, ophys_experiment_id=ophys_experiment_id) stimulus_timestamps = StimulusTimestamps.from_sync_file( sync_file=sync_file) behavior_session_id = BehaviorSessionId.from_lims( db=lims_db, ophys_experiment_id=ophys_experiment_id) is_multiplane_session = _is_multi_plane_session() meta = BehaviorOphysMetadata.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db, is_multiplane=is_multiplane_session) monitor_delay = calculate_monitor_delay( sync_file=sync_file, equipment=meta.behavior_metadata.equipment) date_of_acquisition = DateOfAcquisitionOphys.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db) behavior_session = BehaviorSession.from_lims( lims_db=lims_db, behavior_session_id=behavior_session_id.value, stimulus_timestamps=stimulus_timestamps, monitor_delay=monitor_delay, date_of_acquisition=date_of_acquisition) if is_multiplane_session: ophys_timestamps = OphysTimestampsMultiplane.from_sync_file( sync_file=sync_file, group_count=meta.ophys_metadata.imaging_plane_group_count, plane_group=meta.ophys_metadata.imaging_plane_group) else: ophys_timestamps = OphysTimestamps.from_sync_file( sync_file=sync_file) projections = Projections.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db) cell_specimens = CellSpecimens.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db, ophys_timestamps=ophys_timestamps, segmentation_mask_image_spacing=projections.max_projection.spacing, events_params=EventsParams( filter_scale=events_filter_scale, filter_n_time_steps=events_filter_n_time_steps), exclude_invalid_rois=exclude_invalid_rois) motion_correction = _get_motion_correction() if skip_eye_tracking: eye_tracking_table = None eye_tracking_rig_geometry = None else: eye_tracking_table = _get_eye_tracking_table(sync_file=sync_file) eye_tracking_rig_geometry = EyeTrackingRigGeometry.from_lims( ophys_experiment_id=ophys_experiment_id, lims_db=lims_db) return BehaviorOphysExperiment( behavior_session=behavior_session, cell_specimens=cell_specimens, ophys_timestamps=ophys_timestamps, metadata=meta, projections=projections, motion_correction=motion_correction, eye_tracking_table=eye_tracking_table, eye_tracking_rig_geometry=eye_tracking_rig_geometry, date_of_acquisition=date_of_acquisition)