Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket/2545/dev #2604

Merged
merged 6 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions allensdk/brain_observatory/behavior/behavior_ophys_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,22 @@
import pandas as pd
from pynwb import NWBFile

from allensdk.brain_observatory.behavior.behavior_session import (
BehaviorSession)
from allensdk.brain_observatory.behavior.behavior_session import BehaviorSession # NOQA
from allensdk.brain_observatory.behavior.data_files import SyncFile
from allensdk.brain_observatory.behavior.data_files\
.rigid_motion_transform_file import \
RigidMotionTransformFile
from allensdk.brain_observatory.behavior.data_objects import \
BehaviorSessionId
from allensdk.brain_observatory.behavior.data_objects.cell_specimens \
.cell_specimens import \
CellSpecimens, EventsParams
from allensdk.brain_observatory.behavior.data_objects.metadata \
.behavior_metadata.date_of_acquisition import \
DateOfAcquisitionOphys, DateOfAcquisition
from allensdk.brain_observatory.behavior.data_objects.metadata\
.behavior_ophys_metadata import \
BehaviorOphysMetadata
from allensdk.brain_observatory.behavior.data_objects.metadata\
.ophys_experiment_metadata.multi_plane_metadata.imaging_plane_group \
import \
ImagingPlaneGroup
from allensdk.brain_observatory.behavior.data_objects.metadata\
.ophys_experiment_metadata.multi_plane_metadata.multi_plane_metadata \
import \
MultiplaneMetadata
from allensdk.brain_observatory.behavior.data_objects.motion_correction \
import \
MotionCorrection
from allensdk.brain_observatory.behavior.data_objects.projections import \
Projections
from allensdk.brain_observatory.behavior.data_objects.stimuli.util import \
calculate_monitor_delay
from allensdk.brain_observatory.behavior.data_objects.timestamps \
.ophys_timestamps import \
OphysTimestamps, OphysTimestampsMultiplane
from allensdk.brain_observatory.behavior.data_files.rigid_motion_transform_file import RigidMotionTransformFile # NOQA
from allensdk.brain_observatory.behavior.data_objects import BehaviorSessionId
from allensdk.brain_observatory.behavior.data_objects.cell_specimens.cell_specimens import CellSpecimens, EventsParams # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.behavior_metadata.date_of_acquisition import DateOfAcquisition, DateOfAcquisitionOphys # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.behavior_ophys_metadata import BehaviorOphysMetadata # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.multi_plane_metadata.imaging_plane_group import ImagingPlaneGroup # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.multi_plane_metadata.multi_plane_metadata import MultiplaneMetadata # NOQA
from allensdk.brain_observatory.behavior.data_objects.motion_correction import MotionCorrection # NOQA
from allensdk.brain_observatory.behavior.data_objects.projections import Projections # NOQA
from allensdk.brain_observatory.behavior.data_objects.stimuli.util import calculate_monitor_delay # NOQA
from allensdk.brain_observatory.behavior.data_objects.timestamps.ophys_timestamps import OphysTimestamps, OphysTimestampsMultiplane # NOQA
from allensdk.brain_observatory.behavior.image_api import Image
from allensdk.core.auth_config import LIMS_DB_CREDENTIAL_MAP
from allensdk.deprecated import legacy
from allensdk.brain_observatory.behavior.image_api import Image
from allensdk.internal.api import db_connection_creator


Expand Down Expand Up @@ -426,6 +404,8 @@ def metadata(self):
'field_of_view_width':
self._metadata.ophys_metadata.field_of_view_shape.width,
'imaging_depth': self._metadata.ophys_metadata.imaging_depth,
'average_container_depth':
self._metadata.ophys_metadata.average_container_depth,
'imaging_plane_group':
self._metadata.ophys_metadata.imaging_plane_group
if isinstance(self._metadata.ophys_metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

from pynwb import NWBFile

from allensdk.core import DataObject
from allensdk.brain_observatory.behavior.data_objects import BehaviorSessionId
from allensdk.core import \
JsonReadableInterface, NwbReadableInterface, \
LimsReadableInterface
from allensdk.core import \
NwbWritableInterface
from allensdk.brain_observatory.behavior.data_objects.metadata\
.behavior_metadata.behavior_metadata import \
BehaviorMetadata
from allensdk.brain_observatory.behavior.data_objects.metadata\
.ophys_experiment_metadata.multi_plane_metadata\
.multi_plane_metadata import \
MultiplaneMetadata
from allensdk.brain_observatory.behavior.data_objects.metadata\
.ophys_experiment_metadata.ophys_experiment_metadata import \
OphysExperimentMetadata
from allensdk.brain_observatory.behavior.schemas import \
OphysBehaviorMetadataSchema
from allensdk.brain_observatory.behavior.data_objects.metadata.behavior_metadata.behavior_metadata import BehaviorMetadata # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.multi_plane_metadata.multi_plane_metadata import MultiplaneMetadata # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.ophys_experiment_metadata import OphysExperimentMetadata # NOQA
from allensdk.brain_observatory.behavior.schemas import OphysBehaviorMetadataSchema # NOQA
from allensdk.brain_observatory.nwb import load_pynwb_extension
from allensdk.core import DataObject, JsonReadableInterface, LimsReadableInterface, NwbReadableInterface, NwbWritableInterface # NOQA
from allensdk.internal.api import PostgresQueryMixin


Expand Down Expand Up @@ -158,6 +145,7 @@ def to_nwb(self, nwbfile: NWBFile) -> NWBFile:
session_type=behavior_meta.session_type,
equipment_name=behavior_meta.equipment.value,
imaging_depth=ophys_meta.imaging_depth,
average_container_depth=ophys_meta.average_container_depth,
behavior_session_uuid=str(behavior_meta.behavior_session_uuid),
behavior_session_id=behavior_meta.behavior_session_id
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from pynwb import NWBFile

from allensdk.core import DataObject, JsonReadableInterface, LimsReadableInterface, NwbReadableInterface # NOQA
from allensdk.internal.api import PostgresQueryMixin


class AverageContainerDepth(
DataObject,
LimsReadableInterface,
NwbReadableInterface,
JsonReadableInterface,
):
mikejhuang marked this conversation as resolved.
Show resolved Hide resolved
"""Data object loads and stores the average `imaging_depth`s
(microns) across experiments in the container that an experiment is
associated with.
"""
def __init__(self, average_container_depth: int):
super().__init__(
name="average_container_depth", value=average_container_depth
)

@classmethod
def from_lims(
cls, ophys_experiment_id: int, lims_db: PostgresQueryMixin
) -> "AverageContainerDepth":
query_container_id = """
SELECT visual_behavior_experiment_container_id
FROM ophys_experiments_visual_behavior_experiment_containers
WHERE ophys_experiment_id = {}
""".format(
ophys_experiment_id
)

container_id = lims_db.fetchone(query_container_id, strict=True)

query_depths = """
SELECT AVG(imd.depth)
FROM ophys_experiments_visual_behavior_experiment_containers ec
JOIN ophys_experiments oe ON oe.id = ec.ophys_experiment_id
LEFT JOIN imaging_depths imd ON imd.id = oe.imaging_depth_id
WHERE ec.visual_behavior_experiment_container_id = {};
""".format(
container_id
)

average_container_depth = int(lims_db.fetchone(query_depths))
return cls(average_container_depth=average_container_depth)

@classmethod
def from_json(cls, dict_repr: dict) -> "AverageContainerDepth":
# TODO remove all of the from_json loading and validation step
# ticket 2607
return cls(average_container_depth=dict_repr["targeted_depth"])

@classmethod
def from_nwb(cls, nwbfile: NWBFile) -> "AverageContainerDepth":
metadata = nwbfile.lab_meta_data["metadata"]
return cls(average_container_depth=metadata.average_container_depth)
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@

class ImagingDepth(DataObject, LimsReadableInterface, NwbReadableInterface,
JsonReadableInterface):
"""Data object loads and stores the imaging_depth (microns) for an
experiments. This is the calculated difference between measured
z-depths of the surface and imaging_depth.
"""
def __init__(self, imaging_depth: int):
super().__init__(name='imaging_depth', value=imaging_depth)

@classmethod
def from_lims(cls, ophys_experiment_id: int,
lims_db: PostgresQueryMixin) -> "ImagingDepth":
query = """
SELECT id.depth
SELECT imd.depth
FROM ophys_experiments oe
JOIN ophys_sessions os ON oe.ophys_session_id = os.id
LEFT JOIN imaging_depths id ON id.id = oe.imaging_depth_id
LEFT JOIN imaging_depths imd ON imd.id = oe.imaging_depth_id
WHERE oe.id = {};
""".format(ophys_experiment_id)
imaging_depth = lims_db.fetchone(query, strict=True)
return cls(imaging_depth=imaging_depth)

@classmethod
def from_json(cls, dict_repr: dict) -> "ImagingDepth":
# TODO remove all of the from_json loading and validation step
# ticket 2607
return cls(imaging_depth=dict_repr['targeted_depth'])

@classmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
from pynwb import NWBFile

from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.ophys_container_id import \
OphysContainerId
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.field_of_view_shape import \
FieldOfViewShape
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.imaging_depth import \
ImagingDepth
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.multi_plane_metadata \
.imaging_plane_group import \
ImagingPlaneGroup
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.ophys_experiment_metadata import \
OphysExperimentMetadata
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.ophys_session_id import \
OphysSessionId
from allensdk.brain_observatory.behavior.data_objects.metadata \
.ophys_experiment_metadata.project_code import \
ProjectCode
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.field_of_view_shape import FieldOfViewShape # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.imaging_depth import ImagingDepth # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.multi_plane_metadata.imaging_plane_group import ImagingPlaneGroup # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.ophys_container_id import OphysContainerId # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.ophys_experiment_metadata import OphysExperimentMetadata # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.ophys_session_id import OphysSessionId # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.project_code import ProjectCode # NOQA
from allensdk.brain_observatory.behavior.data_objects.metadata.ophys_experiment_metadata.average_container_depth import AverageContainerDepth # NOQA
from allensdk.internal.api import PostgresQueryMixin


Expand All @@ -32,6 +18,7 @@ def __init__(self,
ophys_container_id: OphysContainerId,
field_of_view_shape: FieldOfViewShape,
imaging_depth: ImagingDepth,
average_container_depth: AverageContainerDepth,
imaging_plane_group: ImagingPlaneGroup,
project_code: ProjectCode):
super().__init__(
Expand All @@ -40,6 +27,7 @@ def __init__(self,
ophys_container_id=ophys_container_id,
field_of_view_shape=field_of_view_shape,
imaging_depth=imaging_depth,
average_container_depth=average_container_depth,
project_code=project_code
)
self._imaging_plane_group = imaging_plane_group
Expand All @@ -55,9 +43,10 @@ def from_lims(
return cls(
ophys_experiment_id=ophys_experiment_metadata.ophys_experiment_id,
ophys_session_id=ophys_experiment_metadata._ophys_session_id,
ophys_container_id=ophys_experiment_metadata._ophys_container_id, # noqa E501
ophys_container_id=ophys_experiment_metadata._ophys_container_id,
field_of_view_shape=ophys_experiment_metadata._field_of_view_shape,
imaging_depth=ophys_experiment_metadata._imaging_depth,
average_container_depth=ophys_experiment_metadata._average_container_depth, # noqa E501
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does black really not split this line and the others like it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's been a ticket open for this on the black repository since 2018 and it hasn't been resolved yet.
psf/black#510

A black collaborator acknowledged the issue needs to be fixed in 2020
psf/black#1582 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, if you fix it by hand, does black just revert it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it gets reverted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, guess we'll live with it.

project_code=ophys_experiment_metadata._project_code,
imaging_plane_group=imaging_plane_group
)
Expand All @@ -69,9 +58,10 @@ def from_json(cls, dict_repr: dict) -> "MultiplaneMetadata":
return cls(
ophys_experiment_id=ophys_experiment_metadata.ophys_experiment_id,
ophys_session_id=ophys_experiment_metadata._ophys_session_id,
ophys_container_id=ophys_experiment_metadata._ophys_container_id, # noqa E501
ophys_container_id=ophys_experiment_metadata._ophys_container_id,
field_of_view_shape=ophys_experiment_metadata._field_of_view_shape,
imaging_depth=ophys_experiment_metadata._imaging_depth,
average_container_depth=ophys_experiment_metadata._average_container_depth, # noqa E501
project_code=ophys_experiment_metadata._project_code,
imaging_plane_group=imaging_plane_group
)
Expand All @@ -83,9 +73,10 @@ def from_nwb(cls, nwbfile: NWBFile) -> "MultiplaneMetadata":
return cls(
ophys_experiment_id=ophys_experiment_metadata.ophys_experiment_id,
ophys_session_id=ophys_experiment_metadata._ophys_session_id,
ophys_container_id=ophys_experiment_metadata._ophys_container_id, # noqa E501
ophys_container_id=ophys_experiment_metadata._ophys_container_id,
field_of_view_shape=ophys_experiment_metadata._field_of_view_shape,
imaging_depth=ophys_experiment_metadata._imaging_depth,
average_container_depth=ophys_experiment_metadata._average_container_depth, # noqa E501
project_code=ophys_experiment_metadata._project_code,
imaging_plane_group=imaging_plane_group
)
Expand Down
Loading