Skip to content

Commit

Permalink
src/pynwb/file.py: Add new entry to file_create_date on non-read-only…
Browse files Browse the repository at this point in the history
… load

The file_create_date entry holds according to [1]

  A record of the date the file was created and of subsequent modifications.

But until now we never added additional entries to file_create_date.

We now do that when the file is not opened read-only.

[1]: https://nwb-schema.readthedocs.io/en/latest/format.html#nwb-n-file
  • Loading branch information
t-b committed Jan 30, 2020
1 parent f26740f commit 9137f2a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ def __init__(self, **kwargs):
manager = get_manager()
super(NWBHDF5IO, self).__init__(path, manager=manager, mode=mode, file=file_obj, comm=comm)

def read(self, **kwargs):

nwbfile = super().read(**kwargs)

if self.mode != 'r':
nwbfile._appendModificationEntry()

return nwbfile


from . import io as __io # noqa: F401,E402
from .core import NWBContainer, NWBData # noqa: F401,E402
Expand Down
7 changes: 7 additions & 0 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,13 @@ def copy(self):

return NWBFile(**kwargs)

def _appendModificationEntry(self):
"""
Append an entry with the current timestamp to the file_create_date array
"""

self.fields['file_create_date'].append(datetime.now(tzlocal()))


def _add_missing_timezone(date):
"""
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,46 @@ def test_reftime_tzaware(self):
'TEST124',
self.start_time,
timestamps_reference_time=self.ref_time_notz)


class TestFileCreateDateArray(TestCase):

def setUp(self):
self.path = 'unittest_file_create_date.nwb'

def tearDown(self):
if os.path.exists(self.path):
os.remove(self.path)

def test_simple(self):
file_create_date = datetime.now(tzlocal())
nwbfile_init = NWBFile(' ', ' ',
datetime.now(tzlocal()),
file_create_date=file_create_date,
institution='Rixdorf University, Berlin')

self.assertEqual(nwbfile_init.file_create_date, [file_create_date])
self.assertEqual(len(nwbfile_init.file_create_date), 1)

with NWBHDF5IO(self.path, 'w') as io:
io.write(nwbfile_init)

with NWBHDF5IO(self.path, 'r') as reader:
nwbfile = reader.read()

# no change as it was opened read-only
self.assertEqual(len(nwbfile.file_create_date), 1)

with NWBHDF5IO(self.path, 'r+') as writer:
nwbfile = writer.read()

# added one more entry as opened read/write
self.assertEqual(len(nwbfile.file_create_date), 2)

writer.write(nwbfile)

with NWBHDF5IO(self.path, 'r') as reader:
nwbfile = reader.read()

# reopen again to check that it has still two entries
self.assertEqual(len(nwbfile.file_create_date), 2)

0 comments on commit 9137f2a

Please sign in to comment.