Skip to content

Commit

Permalink
Change Age to dynamic per session.
Browse files Browse the repository at this point in the history
Update unittest values.
  • Loading branch information
morriscb committed Jan 21, 2023
1 parent 005d3f4 commit 93f3402
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 93f3402

Please sign in to comment.