Skip to content

Updates to allensdk for MultiscopeSignalNoise and TaskTrainedNetworksMultiscope data #2278

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

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c22d044
changed computation of stimulus frame rate in behavior metadata to us…
mabuice Nov 11, 2021
4679ba3
fixed missing reference to SyncFile in stimulus_frame_rate
mabuice Nov 11, 2021
adf57d8
fixed bug with keyword argument for SyncFile
mabuice Nov 11, 2021
7af0205
added method to get sync_file for a session id
mabuice Nov 11, 2021
6fb29a0
updated sync_file
mabuice Nov 11, 2021
fc9351a
updated sync_file
mabuice Nov 11, 2021
bf605c5
updated sync_file
mabuice Nov 11, 2021
8016b9a
updated sync_file
mabuice Nov 11, 2021
05dd50e
updated sync_file
mabuice Nov 11, 2021
6a41f66
added VisualCodingOphysExperiment and necessary changes for non-behav…
mabuice Nov 12, 2021
9eb0d28
bug fixes for VisualCodingExperiment
mabuice Nov 12, 2021
32e8b19
added VisualCodingSessionType
mabuice Nov 12, 2021
79cb6e0
added VisualCodingSession
mabuice Nov 12, 2021
8b04062
bug fixes
mabuice Nov 12, 2021
3f8930d
Merge
mabuice Nov 12, 2021
3aeb4b4
bug fix in visualcoding_session
mabuice Nov 12, 2021
0833626
bug fix
mabuice Nov 12, 2021
12f49d6
added try-except to running_processing to look for 'foraging' key
mabuice Nov 12, 2021
8a566a5
added another try-except to running_processing to look for 'foraging'…
mabuice Nov 12, 2021
b3ad81b
removed computation of behavioral task data
mabuice Nov 12, 2021
926918f
removed events from visual_coding_ophys_experiment
mabuice Nov 13, 2021
b8ace79
bug fix for events in cell_specimens
mabuice Nov 13, 2021
9f3a1f2
bug fix for meta data with visualcoding_ophys_experiment
mabuice Nov 16, 2021
776e89a
bug fix for meta data with visualcoding_session
mabuice Nov 16, 2021
f01e2a1
removed incomplete stimuli call from visualcoding_session
mabuice Nov 16, 2021
572430f
replaced return value for behavior_session_uuid in visualcoding_metad…
mabuice Nov 16, 2021
70980f5
updated imaging_plane and reporter line to use ophys session instead …
mabuice Nov 17, 2021
4184d31
changed running processing to use ophys_session
mabuice Nov 17, 2021
e53be43
fixed bug with get_ophys_frames in sync with key for get_edges
mabuice Dec 1, 2021
ed3be02
updated sync __init__ file for key name changes for TaskTrainedNetwor…
mabuice Dec 1, 2021
ed2e7ca
updated sync __init__ file for get_eye_tracking and get_behavior_moni…
mabuice Dec 1, 2021
ea6dde7
updated get_raw_stimulus_frames for sync info due to key name change …
mabuice Dec 1, 2021
7d72027
updated keys for mesoscope in sync_data and __init__
mabuice Dec 1, 2021
f1ec9c3
trying "stim_running" for acquisition key to check for a bug
mabuice Dec 2, 2021
0337dcb
added try, except for get_trigger and removed stim_running
mabuice Dec 2, 2021
12d2bed
adding a temporary fix to truncate extra eye tracking frames until we…
mabuice Dec 2, 2021
e0cb0a4
added stim_running to trigger keys for sync
mabuice Dec 6, 2021
e1476b2
added stim_running to acquiring keys in time_sync
mabuice Dec 6, 2021
d884eea
added 'foraging' key for get_behavior_stimulus_timestamps
mabuice Dec 7, 2021
47f1842
added stimulus presentations for dense movies for visualcoding
mabuice Dec 7, 2021
2b6e0b6
bug fix for stimuli in visualcoding_session and changed keys for dens…
mabuice Dec 7, 2021
471687b
added property for visualcoding_session to return stimulus_presentati…
mabuice Dec 8, 2021
2f7715d
added events to visualcoding_ophys_experiment
mabuice Jan 11, 2022
40d9761
updated paths for stimulus template files
mabuice Jan 11, 2022
1813509
bug fix in densemovie_template
mabuice Jan 12, 2022
d614294
added MESO.2 to util.py
mabuice Feb 24, 2023
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
30 changes: 30 additions & 0 deletions allensdk/brain_observatory/behavior/data_files/stimulus_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,31 @@
"""


STIMULUS_FILE_SESSION_QUERY_TEMPLATE = """
SELECT
wkf.storage_directory || wkf.filename AS stim_file
FROM
well_known_files wkf
WHERE
wkf.attachable_id = {ophys_session_id}
AND wkf.attachable_type = 'OphysSession'
AND wkf.well_known_file_type_id IN (
SELECT id
FROM well_known_file_types
WHERE 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_cache_key_ophys(cls, db, ophys_session_id: int):
return hashkey(ophys_session_id)


class StimulusFile(DataFile):
"""A DataFile which contains methods for accessing and loading visual
Expand Down Expand Up @@ -68,6 +86,18 @@ def from_lims(
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)

@classmethod
@cached(cache=LRUCache(maxsize=10), key=from_lims_cache_key_ophys)
def from_lims_for_ophys_session(
cls, db: PostgresQueryMixin,
ophys_session_id: Union[int, str]
) -> "StimulusFile":
query = STIMULUS_FILE_SESSION_QUERY_TEMPLATE.format(
ophys_session_id=ophys_session_id
)
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)

@staticmethod
def load_data(filepath: Union[str, Path]) -> dict:
filepath = safe_system_path(file_name=filepath)
Expand Down
30 changes: 30 additions & 0 deletions allensdk/brain_observatory/behavior/data_files/sync_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
AND oe.id = {ophys_experiment_id};
"""

SYNC_FILE_SESSION_QUERY_TEMPLATE = """
SELECT wkf.storage_directory || wkf.filename AS sync_file
FROM ophys_experiments oe
JOIN ophys_sessions os ON oe.ophys_session_id = os.id
JOIN well_known_files wkf ON wkf.attachable_id = os.id
JOIN well_known_file_types wkft
ON wkft.id = wkf.well_known_file_type_id
WHERE wkf.attachable_type = 'OphysSession'
AND wkft.name = 'OphysRigSync'
AND os.id = {ophys_session_id};
"""



def from_json_cache_key(cls, dict_repr: dict):
return hashkey(json.dumps(dict_repr))
Expand All @@ -33,6 +46,10 @@ def from_lims_cache_key(cls, db, ophys_experiment_id: int):
return hashkey(ophys_experiment_id)


def from_lims_cache_key_session(cls, db, ophys_session_id: int):
return hashkey(ophys_session_id)


class SyncFile(DataFile):
"""A DataFile which contains methods for accessing and loading visual
behavior stimulus *.pkl files.
Expand Down Expand Up @@ -65,6 +82,19 @@ def from_lims(
filepath = db.fetchone(query, strict=True)
return cls(filepath=filepath)


@classmethod
@cached(cache=LRUCache(maxsize=10), key=from_lims_cache_key_session)
def from_lims_for_ophys_session(
cls, db: PostgresQueryMixin,
ophys_session_id: Union[int, str]
) -> "SyncFile":
query = SYNC_FILE_SESSION_QUERY_TEMPLATE.format(
ophys_session_id=ophys_session_id
)
filepath = db.fetchall(query, strict=True)[0]
return cls(filepath=filepath)

@staticmethod
def load_data(filepath: Union[str, Path]) -> dict:
filepath = safe_system_path(file_name=filepath)
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\
.behavior_metadata.visualcoding_session_id import VisualCodingSessionId
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 @@ -181,9 +181,10 @@ def __init__(self,
# there seem to be cases where cell_specimen_table contains rois not in
# events
# See ie https://app.zenhub.com/workspaces/allensdk-10-5c17f74db59cfb36f158db8c/issues/alleninstitute/allensdk/2139 # noqa
events.filter_and_reorder(
roi_ids=cell_specimen_table['cell_roi_id'].values,
raise_if_rois_missing=False)
if events is not None:
events.filter_and_reorder(
roi_ids=cell_specimen_table['cell_roi_id'].values,
raise_if_rois_missing=False)

self._meta = meta
self._cell_specimen_table = cell_specimen_table
Expand Down Expand Up @@ -235,7 +236,8 @@ def from_lims(cls, ophys_experiment_id: int,
ophys_timestamps: OphysTimestamps,
segmentation_mask_image_spacing: Tuple,
exclude_invalid_rois=True,
events_params: Optional[EventsParams] = None) \
events_params: Optional[EventsParams] = None,
include_events: bool = True) \
-> "CellSpecimens":
def _get_ophys_cell_segmentation_run_id() -> int:
"""Get the ophys cell segmentation run id associated with an
Expand Down Expand Up @@ -297,7 +299,10 @@ def _get_events():
ophys_timestamps=ophys_timestamps)
dff_traces = _get_dff_traces()
corrected_fluorescence_traces = _get_corrected_fluorescence_traces()
events = _get_events()
if include_events:
events = _get_events()
else:
events = None

return CellSpecimens(
cell_specimen_table=cell_specimen_table, meta=meta,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from pynwb import NWBFile

from allensdk.brain_observatory.behavior.data_files import StimulusFile
from allensdk.brain_observatory.behavior.data_files import StimulusFile, SyncFile
from allensdk.brain_observatory.behavior.data_objects import DataObject, \
BehaviorSessionId
from allensdk.brain_observatory.behavior.data_objects.base \
Expand Down Expand Up @@ -211,8 +211,11 @@ def from_lims(

stimulus_file = StimulusFile.from_lims(
db=lims_db, behavior_session_id=behavior_session_id.value)
stimulus_frame_rate = StimulusFrameRate.from_stimulus_file(
stimulus_file=stimulus_file)
# stimulus_frame_rate = StimulusFrameRate.from_stimulus_file(
# stimulus_file=stimulus_file)
sync_file = SyncFile.from_lims_for_session(db=lims_db, behavior_session_id=behavior_session_id.value)
stimulus_frame_rate = StimulusFrameRate.from_sync_file(
sync_file=sync_file)
session_type = SessionType.from_stimulus_file(
stimulus_file=stimulus_file)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ def from_lims(
datetime=experiment_date)
return cls(date_of_acquisition=experiment_date)

@classmethod
def from_lims_for_ophys_session(
cls, ophys_session_id: int,
lims_db: PostgresQueryMixin) -> "DateOfAcquisition":
query = """
SELECT os.date_of_acquisition
FROM ophys_sessions os
WHERE os.id = {};
""".format(ophys_session_id)

experiment_date = lims_db.fetchone(query, strict=True)
experiment_date = cls._postprocess_lims_datetime(
datetime=experiment_date)
return cls(date_of_acquisition=experiment_date)


@classmethod
def from_nwb(cls, nwbfile: NWBFile) -> "DateOfAcquisition":
return cls(date_of_acquisition=nwbfile.session_start_time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ def from_lims(cls, behavior_session_id: int,
equipment_name = lims_db.fetchone(query, strict=True)
return cls(equipment_name=equipment_name)

@classmethod
def from_lims_for_ophys_session(cls, ophys_session_id: int,
lims_db: PostgresQueryMixin) -> "Equipment":
query = f"""
SELECT e.name AS device_name
FROM ophys_sessions os
JOIN equipment e ON e.id = os.equipment_id
WHERE os.id = {ophys_session_id};
"""
equipment_name = lims_db.fetchone(query, strict=True)
return cls(equipment_name=equipment_name)

@classmethod
def from_nwb(cls, nwbfile: NWBFile) -> "Equipment":
metadata = nwbfile.lab_meta_data['metadata']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ def from_lims(cls, behavior_session_id: int,
foraging_id = lims_db.fetchone(query, strict=True)
foraging_id = uuid.UUID(foraging_id)
return cls(foraging_id=foraging_id)

@classmethod
def from_lims_for_ophys_session(cls, ophys_session_id: int,
lims_db: PostgresQueryMixin) -> "ForagingId":
query = f"""
SELECT
foraging_id
FROM
ophys_sessions
WHERE
ophys_sessions.id = {ophys_session_id};
"""
foraging_id = lims_db.fetchone(query, strict=True)
foraging_id = uuid.UUID(foraging_id)
return cls(foraging_id=foraging_id)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pynwb import NWBFile

from allensdk.brain_observatory.behavior.data_files import StimulusFile
from allensdk.brain_observatory.behavior.data_files import StimulusFile, SyncFile
from allensdk.brain_observatory.behavior.data_objects import DataObject
from allensdk.brain_observatory.behavior.data_objects.base \
.readable_interfaces import \
Expand All @@ -27,6 +27,15 @@ def from_stimulus_file(
frame_rate = calc_frame_rate(timestamps=stimulus_timestamps.value)
return cls(stimulus_frame_rate=frame_rate)

@classmethod
def from_sync_file(
cls,
sync_file: SyncFile) -> "StimulusFrameRate":
stimulus_timestamps = StimulusTimestamps.from_sync_file(
sync_file=sync_file)
frame_rate = calc_frame_rate(timestamps=stimulus_timestamps.value)
return cls(stimulus_frame_rate=frame_rate)

@classmethod
def from_nwb(cls, nwbfile: NWBFile) -> "StimulusFrameRate":
metadata = nwbfile.lab_meta_data['metadata']
Expand Down
Loading