示例#1
0
    def setup_class(cls):
        cls.ophys_experiment_id = 994278291

        dir = Path(__file__).parent.resolve()
        test_data_dir = dir / 'test_data'

        cls.expected_max = Projections._from_filepath(filepath=str(
            test_data_dir / 'max_projection.png'),
                                                      pixel_size=.78125)

        cls.expected_avg = Projections._from_filepath(filepath=str(
            test_data_dir / 'avg_projection.png'),
                                                      pixel_size=.78125)
示例#2
0
    def setup_class(cls):
        dir = Path(__file__).parent.resolve()
        test_data_dir = dir / 'test_data'
        with open(test_data_dir / 'test_input.json') as f:
            dict_repr = json.load(f)
        dict_repr = dict_repr['session_data']
        dict_repr['max_projection_file'] = test_data_dir / \
            dict_repr['max_projection_file']
        dict_repr['average_intensity_projection_image_file'] = \
            test_data_dir / \
            dict_repr['average_intensity_projection_image_file']

        cls.expected_max = Projections._from_filepath(filepath=str(
            test_data_dir / 'max_projection.png'),
                                                      pixel_size=.78125)

        cls.expected_avg = Projections._from_filepath(filepath=str(
            test_data_dir / 'avg_projection.png'),
                                                      pixel_size=.78125)

        cls.dict_repr = dict_repr
示例#3
0
 def setup_class(cls):
     tj = TestJson()
     tj.setup_class()
     cls.projections = Projections.from_json(dict_repr=tj.dict_repr)
示例#4
0
    def test_from_json(self):
        projections = Projections.from_json(dict_repr=self.dict_repr)

        assert projections.max_projection == self.expected_max
        assert projections.avg_projection == self.expected_avg
示例#5
0
    def test_from_lims(self):
        projections = Projections.from_lims(
            ophys_experiment_id=self.ophys_experiment_id, lims_db=self.dbconn)

        assert projections.max_projection == self.expected_max
        assert projections.avg_projection == self.expected_avg
    def from_json(cls,
                  session_data: dict,
                  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
        ----------
        session_data
        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_json(
                dict_repr=session_data)
            return cls._is_multi_plane_session(
                imaging_plane_group_meta=imaging_plane_group_meta)

        def _get_motion_correction():
            rigid_motion_transform_file = RigidMotionTransformFile.from_json(
                dict_repr=session_data)
            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_json(
                dict_repr=session_data)
            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

        sync_file = SyncFile.from_json(dict_repr=session_data)
        is_multiplane_session = _is_multi_plane_session()
        meta = BehaviorOphysMetadata.from_json(
            dict_repr=session_data, is_multiplane=is_multiplane_session)
        monitor_delay = calculate_monitor_delay(
            sync_file=sync_file, equipment=meta.behavior_metadata.equipment)
        behavior_session = BehaviorSession.from_json(
            session_data=session_data, monitor_delay=monitor_delay)

        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_json(dict_repr=session_data)
        cell_specimens = CellSpecimens.from_json(
            dict_repr=session_data,
            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_json(
                dict_repr=session_data)

        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=behavior_session._date_of_acquisition)
    def from_nwb(cls,
                 nwbfile: NWBFile,
                 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) -> "BehaviorOphysExperiment":
        """

        Parameters
        ----------
        nwbfile
        eye_tracking_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
        eye_tracking_dilation_frames : int, optional
            Determines the number of adjacent frames that will be marked
            as 'likely_blink' when performing blink detection for
            `eye_tracking` data, by default 2
        events_filter_scale : float, optional
            Stdev of halfnorm distribution used to convolve ophys events with
            a 1d causal half-gaussian filter to smooth it for visualization,
            by default 2.0
        events_filter_n_time_steps : int, optional
            Number of time steps to use for convolution of ophys events
        exclude_invalid_rois
            Whether to exclude invalid rois
        """
        def _is_multi_plane_session():
            imaging_plane_group_meta = ImagingPlaneGroup.from_nwb(
                nwbfile=nwbfile)
            return cls._is_multi_plane_session(
                imaging_plane_group_meta=imaging_plane_group_meta)

        behavior_session = BehaviorSession.from_nwb(nwbfile=nwbfile)
        projections = Projections.from_nwb(nwbfile=nwbfile)
        cell_specimens = CellSpecimens.from_nwb(
            nwbfile=nwbfile,
            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)
        eye_tracking_rig_geometry = EyeTrackingRigGeometry.from_nwb(
            nwbfile=nwbfile)
        eye_tracking_table = EyeTrackingTable.from_nwb(
            nwbfile=nwbfile,
            z_threshold=eye_tracking_z_threshold,
            dilation_frames=eye_tracking_dilation_frames)
        motion_correction = MotionCorrection.from_nwb(nwbfile=nwbfile)
        is_multiplane_session = _is_multi_plane_session()
        metadata = BehaviorOphysMetadata.from_nwb(
            nwbfile=nwbfile, is_multiplane=is_multiplane_session)
        if is_multiplane_session:
            ophys_timestamps = OphysTimestampsMultiplane.from_nwb(
                nwbfile=nwbfile)
        else:
            ophys_timestamps = OphysTimestamps.from_nwb(nwbfile=nwbfile)
        date_of_acquisition = DateOfAcquisitionOphys.from_nwb(nwbfile=nwbfile)

        return BehaviorOphysExperiment(
            behavior_session=behavior_session,
            cell_specimens=cell_specimens,
            eye_tracking_rig_geometry=eye_tracking_rig_geometry,
            eye_tracking_table=eye_tracking_table,
            motion_correction=motion_correction,
            metadata=metadata,
            ophys_timestamps=ophys_timestamps,
            projections=projections,
            date_of_acquisition=date_of_acquisition)
    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)