diff --git a/src/pynwb/__init__.py b/src/pynwb/__init__.py index ec6e9a186a..d6cb43349d 100644 --- a/src/pynwb/__init__.py +++ b/src/pynwb/__init__.py @@ -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 diff --git a/src/pynwb/file.py b/src/pynwb/file.py index e44d688a2d..5bc46f0c20 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -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): """ diff --git a/tests/unit/test_file.py b/tests/unit/test_file.py index 57f7b285a2..8adb8701a3 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -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)