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

Feature non behavior ophys nwb #2664

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
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
Binary file added __pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/deprecated.cpython-37.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion allensdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def one(x):
return x
if xlen != 1:
raise OneResultExpectedError("Expected length one result, received: "
f"{x} results from queryr")
f"{x} results from query")
if isinstance(x, set):
return list(x)[0]
else:
Expand Down
130 changes: 0 additions & 130 deletions allensdk/api/cloud_cache/README.md

This file was deleted.

Empty file modified allensdk/api/warehouse_cache/cache.py
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Any, Union
from pathlib import Path

from allensdk.internal.core.lims_utilities import safe_system_path


class DataFile(abc.ABC):
"""An abstract class that prototypes methods for accessing internal
Expand All @@ -15,7 +17,7 @@ class DataFile(abc.ABC):
"""

def __init__(self, filepath: Union[str, Path]): # pragma: no cover
self._filepath: str = str(filepath)
self._filepath: str = safe_system_path(str(filepath))
self._data = self.load_data(filepath=self._filepath)

@property
Expand Down
4 changes: 3 additions & 1 deletion allensdk/brain_observatory/behavior/data_files/demix_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def load_data(filepath: Union[str, Path]) -> pd.DataFrame:
with h5py.File(filepath, 'r') as in_file:
traces = in_file['data'][()]
roi_id = in_file['roi_names'][()]
idx = pd.Index(roi_id, name='cell_roi_id', dtype=int)
roi_id = roi_id.astype(str)
roi_id = roi_id.astype(int)
idx = pd.Index(roi_id, name='cell_roi_id')
return pd.DataFrame({'corrected_fluorescence': list(traces)},
index=idx)
5 changes: 4 additions & 1 deletion allensdk/brain_observatory/behavior/data_files/dff_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ def from_lims(
AND oe.id = {};
""".format(ophys_experiment_id)
filepath = db.fetchone(query, strict=True)
print(filepath)
return cls(filepath=filepath)

@staticmethod
def load_data(filepath: Union[str, Path]) -> pd.DataFrame:
with h5py.File(filepath, 'r') as raw_file:
traces = np.asarray(raw_file['data'], dtype=np.float64)
roi_names = np.asarray(raw_file['roi_names'])
idx = pd.Index(roi_names, name='cell_roi_id', dtype=int)
roi_names = roi_names.astype(str)
roi_names = roi_names.astype(int)
idx = pd.Index(roi_names, name='cell_roi_id')
return pd.DataFrame({'dff': [x for x in traces]}, index=idx)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
load_eye_tracking_hdf
from allensdk.internal.api import PostgresQueryMixin
from allensdk.internal.core.lims_utilities import safe_system_path
from allensdk.brain_observatory.behavior.data_files import DataFile
from allensdk.internal.core import DataFile


class EyeTrackingFile(DataFile):
Expand All @@ -29,16 +29,17 @@ def to_json(self) -> Dict[str, str]:
@classmethod
def from_lims(
cls, db: PostgresQueryMixin,
ophys_experiment_id: Union[int, str]
behavior_session_id: Union[int, str]
) -> "EyeTrackingFile":
query = f"""
SELECT wkf.storage_directory || wkf.filename AS eye_tracking_file
FROM ophys_experiments oe
LEFT JOIN well_known_files wkf ON wkf.attachable_id = oe.ophys_session_id
FROM behavior_sessions bs
JOIN ophys_sessions os ON os.id = bs.ophys_session_id
LEFT JOIN well_known_files wkf ON wkf.attachable_id = os.id
JOIN well_known_file_types wkft ON wkf.well_known_file_type_id = wkft.id
WHERE wkf.attachable_type = 'OphysSession'
AND wkft.name = 'EyeTracking Ellipses'
AND oe.id = {ophys_experiment_id};
AND bs.id = {behavior_session_id};
""" # noqa E501
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)
Expand All @@ -47,4 +48,4 @@ def from_lims(
def load_data(filepath: Union[str, Path]) -> pd.DataFrame:
filepath = safe_system_path(file_name=filepath)
# TODO move the contents of this function here
return load_eye_tracking_hdf(filepath)
return load_eye_tracking_hdf(filepath)
34 changes: 32 additions & 2 deletions allensdk/brain_observatory/behavior/data_files/stimulus_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from allensdk.brain_observatory.behavior.data_files import DataFile

# Query returns path to StimulusPickle file for given behavior session
STIMULUS_FILE_QUERY_TEMPLATE = """
BEHAVIOR_STIMULUS_FILE_QUERY_TEMPLATE = """
SELECT
wkf.storage_directory || wkf.filename AS stim_file
FROM
Expand All @@ -27,13 +27,31 @@
"""


NO_BEHAVIOR_STIMULUS_FILE_QUERY_TEMPLATE = """
SELECT
wkf.storage_directory || wkf.filename AS stim_file
FROM
well_known_files wkf
JOIN
well_known_file_types wkft
ON wkf.well_known_file_type_id = wkft.id
WHERE
wkf.attachable_id = {behavior_session_id}
AND wkft.name = 'StimulusPickle'
"""



def from_json_cache_key(cls, dict_repr: dict):
return hashkey(json.dumps(dict_repr))


def from_lims_cache_key(cls, db, behavior_session_id: int):
return hashkey(behavior_session_id)

def from_lims_nb_cache_key(cls, db, ophys_experiment_id: int):
return hashkey(ophys_experiment_id)


class StimulusFile(DataFile):
"""A DataFile which contains methods for accessing and loading visual
Expand Down Expand Up @@ -62,11 +80,23 @@ def from_lims(
cls, db: PostgresQueryMixin,
behavior_session_id: Union[int, str]
) -> "StimulusFile":
query = STIMULUS_FILE_QUERY_TEMPLATE.format(
query = NO_BEHAVIOR_STIMULUS_FILE_QUERY_TEMPLATE.format(
behavior_session_id=behavior_session_id
)
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)

@classmethod
@cached(cache=LRUCache(maxsize=10), key=from_lims_nb_cache_key)
def no_behavior_from_lims(
cls, db: PostgresQueryMixin,
ophys_experiment_id: Union[int, str]
) -> "StimulusFile":
query = NO_BEHAVIOR_STIMULUS_FILE_QUERY_TEMPLATE.format(
experiment_id = ophys_experiment_id
)
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)

@staticmethod
def load_data(filepath: Union[str, Path]) -> dict:
Expand Down
2 changes: 2 additions & 0 deletions allensdk/brain_observatory/behavior/data_objects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from allensdk.brain_observatory.behavior.data_objects.base._data_object_abc import DataObject # noqa: E501, F401
from allensdk.brain_observatory.behavior.data_objects.metadata\
.behavior_metadata.behavior_session_id import BehaviorSessionId # noqa: E501, F401
from allensdk.brain_observatory.behavior.data_objects.metadata\
.ophys_experiment_metadata.ophys_session_id import OphysSessionId
from allensdk.brain_observatory.behavior.data_objects.timestamps\
.stimulus_timestamps.stimulus_timestamps import StimulusTimestamps # noqa: E501, F401
from allensdk.brain_observatory.behavior.data_objects.running_speed.running_speed import RunningSpeed # noqa: E501, F401
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(self,
roi_ids=cell_specimen_table['cell_roi_id'].values,
raise_if_rois_missing=False)


self._meta = meta
self._cell_specimen_table = cell_specimen_table
self._dff_traces = dff_traces
Expand Down Expand Up @@ -299,6 +300,7 @@ def _get_events():
corrected_fluorescence_traces = _get_corrected_fluorescence_traces()
events = _get_events()


return CellSpecimens(
cell_specimen_table=cell_specimen_table, meta=meta,
dff_traces=dff_traces,
Expand Down Expand Up @@ -525,7 +527,6 @@ def _get_segmentation_mask_image(self, spacing: tuple) -> Image:
metadata
"""
mask_data = np.sum(self.roi_masks['roi_mask']).astype(int)

mask_image = Image(
data=mask_data,
spacing=spacing,
Expand Down Expand Up @@ -578,6 +579,9 @@ def _validate_traces(
for traces in (dff_traces, corrected_fluorescence_traces):
# validate traces contain expected roi ids
if not np.in1d(traces.value.index, cell_roi_ids).all():
# print(traces.value.index)
# print("roi table")
# print(cell_roi_ids)
raise RuntimeError(f"{traces.name} contains ROI IDs that "
f"are not in "
f"cell_specimen_table.cell_roi_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class MultiplaneMetadata(OphysExperimentMetadata):
def __init__(self,
ophys_experiment_id: int,
ophys_session_id: OphysSessionId,
experiment_container_id: ExperimentContainerId,
#experiment_container_id: ExperimentContainerId,
field_of_view_shape: FieldOfViewShape,
imaging_depth: ImagingDepth,
imaging_plane_group: ImagingPlaneGroup,
project_code: ProjectCode):
super().__init__(
ophys_experiment_id=ophys_experiment_id,
ophys_session_id=ophys_session_id,
experiment_container_id=experiment_container_id,
#experiment_container_id=experiment_container_id,
field_of_view_shape=field_of_view_shape,
imaging_depth=imaging_depth,
project_code=project_code
Expand All @@ -55,7 +55,7 @@ def from_lims(
return cls(
ophys_experiment_id=ophys_experiment_metadata.ophys_experiment_id,
ophys_session_id=ophys_experiment_metadata._ophys_session_id,
experiment_container_id=ophys_experiment_metadata._experiment_container_id, # noqa E501
#experiment_container_id=ophys_experiment_metadata._experiment_container_id, # noqa E501
field_of_view_shape=ophys_experiment_metadata._field_of_view_shape,
imaging_depth=ophys_experiment_metadata._imaging_depth,
project_code=ophys_experiment_metadata._project_code,
Expand Down
Loading