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

[Bug]: date of birth date time round trip #1859

Closed
3 tasks done
bendichter opened this issue Mar 12, 2024 · 2 comments · Fixed by #1886
Closed
3 tasks done

[Bug]: date of birth date time round trip #1859

bendichter opened this issue Mar 12, 2024 · 2 comments · Fixed by #1886
Assignees
Labels
category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users
Milestone

Comments

@bendichter
Copy link
Contributor

bendichter commented Mar 12, 2024

What happened?

After adding date as an allowable type for Subject.date_of_birth:

{'name': 'date_of_birth', 'type': (datetime, date), 'default': None,

a Subject date_of_birth can be set with a date object. This can be written to an NWB file which stores the date correctly. However, on read, the date_of_birth is converted to a datetime.

Steps to Reproduce

from pynwb.file import Subject
from pynwb import NWBHDF5IO
from datetime import date, datetime
from pynwb.testing.mock.file import mock_NWBFile
from h5py import File

# A Subject date_of_birth can be set with a date object
subject = Subject(subject_id="001", date_of_birth=date(2022, 1, 1))
assert isinstance(subject.date_of_birth, date)
print("Value in Subject object:", subject.date_of_birth)

# This can be written to an NWB file
nwbfile = mock_NWBFile(subject=subject)

with NWBHDF5IO("test_dob_date", "w") as io:
    io.write(nwbfile)
    
# The resulting NWB file stores the date correctly
with File("test_dob_date") as file:
    print("Value in NWB file:", file["general/subject/date_of_birth"][()])
    
    
# However, on read, the date of birth is converted to a datetime
with NWBHDF5IO("test_dob_date", "r") as io:
    read_nwbfile = io.read()
    
print("Value in read NWBFile object:", read_nwbfile.subject.date_of_birth)

assert isinstance(read_nwbfile.subject.date_of_birth, datetime)

assert read_nwbfile.subject.date_of_birth == nwbfile.subject.date_of_birth

Traceback

No response

Operating System

macOS

Python Executable

Conda

Python Version

3.9

Package Versions

No response

Code of Conduct

@bendichter
Copy link
Contributor Author

bendichter commented Mar 12, 2024

I think this escaped me in hdmf-dev/hdmf#874 because a datetime isa date:

from datetime import date, datetime

isinstance(datetime.now(), date)  # this is True, which I did not expect.

@bendichter
Copy link
Contributor Author

I'm not able to find in HDMF where the iso date format string is mapped to the datetime type. @rly , do you know where this would be?

@stephprince stephprince assigned stephprince and rly and unassigned stephprince Apr 11, 2024
@stephprince stephprince added category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users labels Apr 11, 2024
@stephprince stephprince added this to the Next Release milestone Apr 11, 2024
@rly rly closed this as completed in #1886 Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: bug errors in the code or code behavior priority: medium non-critical problem and/or affecting only a small set of NWB users
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants