From c8c8b8e22d6f80e73fb20de85df699670e899ba8 Mon Sep 17 00:00:00 2001 From: "Christopher B. Morrison" Date: Fri, 13 Jan 2023 13:44:16 -0800 Subject: [PATCH] Change Age to dynamic per session. Update unittest values. --- .../metadata/subject_metadata/age.py | 33 ++++++++++-- .../metadata/test_behavior_ophys_metadata.py | 52 ++++++++----------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/allensdk/brain_observatory/behavior/data_objects/metadata/subject_metadata/age.py b/allensdk/brain_observatory/behavior/data_objects/metadata/subject_metadata/age.py index ef23d1d269..1db29bdfa2 100644 --- a/allensdk/brain_observatory/behavior/data_objects/metadata/subject_metadata/age.py +++ b/allensdk/brain_observatory/behavior/data_objects/metadata/subject_metadata/age.py @@ -1,18 +1,24 @@ +from datetime import datetime import re import warnings from typing import Optional +import pytz from pynwb import NWBFile from allensdk.core import DataObject from allensdk.core import \ JsonReadableInterface, LimsReadableInterface, NwbReadableInterface from allensdk.internal.api import PostgresQueryMixin +from allensdk.brain_observatory.behavior.data_objects.metadata\ + .behavior_metadata.date_of_acquisition import \ + DateOfAcquisition +from allensdk.brain_observatory.behavior.data_files import BehaviorStimulusFile class Age(DataObject, JsonReadableInterface, LimsReadableInterface, NwbReadableInterface): - """Age of animal (in days)""" + """Age (in days) of animal at the time the behavior session was taken.""" def __init__(self, age: int): super().__init__(name="age_in_days", value=age) @@ -25,17 +31,34 @@ def from_json(cls, dict_repr: dict) -> "Age": @classmethod def from_lims(cls, behavior_session_id: int, lims_db: PostgresQueryMixin) -> "Age": + # TODO PSB-17: Need to likewise grab the daq from the stimulus + # file for now as the data for daq in LIMS needs to be + # updated. + date_of_acquisition = DateOfAcquisition.from_stimulus_file( + BehaviorStimulusFile.from_lims( + db=lims_db, + behavior_session_id=behavior_session_id).validate() + ).value + query = f""" - SELECT a.name AS age + SELECT d.date_of_birth AS date_of_birth FROM behavior_sessions bs JOIN donors d ON d.id = bs.donor_id - JOIN ages a ON a.id = d.age_id WHERE bs.id = {behavior_session_id}; """ - age = lims_db.fetchone(query, strict=True) - age = cls._age_code_to_days(age=age) + date_of_birth = cls._check_timezone( + lims_db.fetchone(query, strict=True)) + + age = (date_of_acquisition - date_of_birth).days return cls(age=age) + @classmethod + def _check_timezone(cls, input_date: datetime) -> datetime: + if input_date.tzinfo is None: + # Add UTC tzinfo if not already set + input_date = pytz.utc.localize(input_date) + return input_date + @classmethod def from_nwb(cls, nwbfile: NWBFile) -> "Age": age = cls._age_code_to_days(age=nwbfile.subject.age) diff --git a/allensdk/test/brain_observatory/behavior/data_objects/metadata/test_behavior_ophys_metadata.py b/allensdk/test/brain_observatory/behavior/data_objects/metadata/test_behavior_ophys_metadata.py index c93b6f010d..5348958b12 100644 --- a/allensdk/test/brain_observatory/behavior/data_objects/metadata/test_behavior_ophys_metadata.py +++ b/allensdk/test/brain_observatory/behavior/data_objects/metadata/test_behavior_ophys_metadata.py @@ -129,41 +129,31 @@ def test_from_lims(self, meso): assert isinstance(bom.ophys_metadata, MultiplaneMetadata) assert bom.ophys_metadata.imaging_depth == 150 assert bom.ophys_metadata.targeted_imaging_depth == 150 - assert bom.behavior_metadata.session_type == "OPHYS_1_images_A" - assert ( - bom.behavior_metadata.subject_metadata.reporter_line - == "Ai148(TIT2L-GC6f-ICL-tTA2)" - ) - assert bom.behavior_metadata.subject_metadata.driver_line == [ - "Sst-IRES-Cre" - ] - assert bom.behavior_metadata.subject_metadata.mouse_id == "457841" - assert ( - bom.behavior_metadata.subject_metadata.full_genotype - == "Sst-IRES-Cre/wt;Ai148(TIT2L-GC6f-ICL-tTA2)/wt" - ) - assert bom.behavior_metadata.subject_metadata.age_in_days == 233 - assert bom.behavior_metadata.subject_metadata.sex == "F" + + assert bom.behavior_metadata.session_type == 'OPHYS_1_images_A' + assert bom.behavior_metadata.subject_metadata.reporter_line == \ + 'Ai148(TIT2L-GC6f-ICL-tTA2)' + assert bom.behavior_metadata.subject_metadata.driver_line == \ + ['Sst-IRES-Cre'] + assert bom.behavior_metadata.subject_metadata.mouse_id == 457841 + assert bom.behavior_metadata.subject_metadata.full_genotype == \ + 'Sst-IRES-Cre/wt;Ai148(TIT2L-GC6f-ICL-tTA2)/wt' + assert bom.behavior_metadata.subject_metadata.age_in_days == 206 + assert bom.behavior_metadata.subject_metadata.sex == 'F' else: assert isinstance(bom.ophys_metadata, OphysExperimentMetadata) assert bom.ophys_metadata.imaging_depth == 175 assert bom.ophys_metadata.targeted_imaging_depth == 175 - assert bom.behavior_metadata.session_type == "OPHYS_4_images_A" - assert ( - bom.behavior_metadata.subject_metadata.reporter_line - == "Ai93(TITL-GCaMP6f)" - ) - assert bom.behavior_metadata.subject_metadata.driver_line == [ - "Camk2a-tTA", - "Slc17a7-IRES2-Cre", - ] - assert bom.behavior_metadata.subject_metadata.mouse_id == "491060" - assert ( - bom.behavior_metadata.subject_metadata.full_genotype - == "Slc17a7-IRES2-Cre/wt;Camk2a-tTA/wt;Ai93(TITL-GCaMP6f)/wt" - ) - assert bom.behavior_metadata.subject_metadata.age_in_days == 130 - assert bom.behavior_metadata.subject_metadata.sex == "M" + assert bom.behavior_metadata.session_type == 'OPHYS_4_images_A' + assert bom.behavior_metadata.subject_metadata.reporter_line == \ + 'Ai93(TITL-GCaMP6f)' + assert bom.behavior_metadata.subject_metadata.driver_line == \ + ['Camk2a-tTA', 'Slc17a7-IRES2-Cre'] + assert bom.behavior_metadata.subject_metadata.mouse_id == 491060 + assert bom.behavior_metadata.subject_metadata.full_genotype == \ + 'Slc17a7-IRES2-Cre/wt;Camk2a-tTA/wt;Ai93(TITL-GCaMP6f)/wt' + assert bom.behavior_metadata.subject_metadata.age_in_days == 120 + assert bom.behavior_metadata.subject_metadata.sex == 'M' class TestJson(TestBOM):