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

Subject age timedelta #1590

Merged
merged 8 commits into from
Nov 10, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Upcoming

### Enhancements and minor changes
- `Subject.age` can be input as a `timedelta`. @bendichter [#1590](https://github.com/NeurodataWithoutBorders/pynwb/pull/1590)

### Documentation and tutorial enhancements:
- Adjusted [ecephys tutorial](https://pynwb.readthedocs.io/en/stable/tutorials/domain/ecephys.html) to create fake data with proper dimensions @bendichter [#1581](https://github.com/NeurodataWithoutBorders/pynwb/pull/1581)
- Refactored testing documentation, including addition of section on ``pynwb.testing.mock`` submodule. @bendichter
Expand Down
57 changes: 33 additions & 24 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
from dateutil.tz import tzlocal
from collections.abc import Iterable
from warnings import warn
Expand Down Expand Up @@ -64,29 +64,35 @@ class Subject(NWBContainer):
'strain'
)

@docval({'name': 'age', 'type': str,
'doc': ('The age of the subject. The ISO 8601 Duration format is recommended, e.g., "P90D" for '
'90 days old.'), 'default': None},
{'name': 'description', 'type': str,
'doc': 'A description of the subject, e.g., "mouse A10".', 'default': None},
{'name': 'genotype', 'type': str,
'doc': 'The genotype of the subject, e.g., "Sst-IRES-Cre/wt;Ai32(RCL-ChR2(H134R)_EYFP)/wt".',
'default': None},
{'name': 'sex', 'type': str,
'doc': ('The sex of the subject. Using "F" (female), "M" (male), "U" (unknown), or "O" (other) '
'is recommended.'), 'default': None},
{'name': 'species', 'type': str,
'doc': ('The species of the subject. The formal latin binomal name is recommended, e.g., "Mus musculus"'),
'default': None},
{'name': 'subject_id', 'type': str, 'doc': 'A unique identifier for the subject, e.g., "A10"',
'default': None},
{'name': 'weight', 'type': (float, str),
'doc': ('The weight of the subject, including units. Using kilograms is recommended. e.g., "0.02 kg". '
'If a float is provided, then the weight will be stored as "[value] kg".'),
'default': None},
{'name': 'date_of_birth', 'type': datetime, 'default': None,
'doc': 'The datetime of the date of birth. May be supplied instead of age.'},
{'name': 'strain', 'type': str, 'doc': 'The strain of the subject, e.g., "C57BL/6J"', 'default': None})
@docval(
{
"name": "age",
"type": (str, timedelta),
"doc": 'The age of the subject. The ISO 8601 Duration format is recommended, e.g., "P90D" for 90 days old.'
'A timedelta will automatically be converted to The ISO 8601 Duration format.',
"default": None,
},
{'name': 'description', 'type': str,
'doc': 'A description of the subject, e.g., "mouse A10".', 'default': None},
{'name': 'genotype', 'type': str,
'doc': 'The genotype of the subject, e.g., "Sst-IRES-Cre/wt;Ai32(RCL-ChR2(H134R)_EYFP)/wt".',
'default': None},
{'name': 'sex', 'type': str,
'doc': ('The sex of the subject. Using "F" (female), "M" (male), "U" (unknown), or "O" (other) '
'is recommended.'), 'default': None},
{'name': 'species', 'type': str,
'doc': 'The species of the subject. The formal latin binomal name is recommended, e.g., "Mus musculus"',
'default': None},
{'name': 'subject_id', 'type': str, 'doc': 'A unique identifier for the subject, e.g., "A10"',
'default': None},
{'name': 'weight', 'type': (float, str),
'doc': ('The weight of the subject, including units. Using kilograms is recommended. e.g., "0.02 kg". '
'If a float is provided, then the weight will be stored as "[value] kg".'),
'default': None},
{'name': 'date_of_birth', 'type': datetime, 'default': None,
'doc': 'The datetime of the date of birth. May be supplied instead of age.'},
{'name': 'strain', 'type': str, 'doc': 'The strain of the subject, e.g., "C57BL/6J"', 'default': None},
)
def __init__(self, **kwargs):
keys_to_set = ("age",
"description",
Expand All @@ -105,6 +111,9 @@ def __init__(self, **kwargs):
if isinstance(weight, float):
args_to_set['weight'] = str(weight) + ' kg'

if isinstance(args_to_set["age"], timedelta):
args_to_set["age"] = pd.Timedelta(args_to_set["age"]).isoformat()

date_of_birth = args_to_set['date_of_birth']
if date_of_birth and date_of_birth.tzinfo is None:
args_to_set['date_of_birth'] = _add_missing_timezone(date_of_birth)
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd

from datetime import datetime
from datetime import datetime, timedelta
from dateutil.tz import tzlocal, tzutc

from pynwb import NWBFile, TimeSeries, NWBHDF5IO
Expand Down Expand Up @@ -479,6 +479,14 @@ def test_weight_float(self):
)
self.assertEqual(subject.weight, '2.3 kg')

def test_subject_age_duration(self):
subject = Subject(
subject_id='RAT123',
age=timedelta(seconds=99999)
)

self.assertEqual(subject.age, "P1DT3H46M39S")


class TestCacheSpec(TestCase):

Expand Down