diff --git a/src/pynwb/__init__.py b/src/pynwb/__init__.py index b98b05157..74f5c9834 100644 --- a/src/pynwb/__init__.py +++ b/src/pynwb/__init__.py @@ -258,6 +258,15 @@ def export(self, **kwargs): kwargs['container'] = nwbfile call_docval_func(super().export, kwargs) + 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 296adab0f..22152d050 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -309,10 +309,12 @@ def __init__(self, **kwargs): raise ValueError("'timestamps_reference_time' must be a timezone-aware datetime object.") self.fields['file_create_date'] = getargs('file_create_date', kwargs) + if self.fields['file_create_date'] is None: - self.fields['file_create_date'] = datetime.now(tzlocal()) - if isinstance(self.fields['file_create_date'], datetime): + self.fields['file_create_date'] = [datetime.now(tzlocal())] + elif isinstance(self.fields['file_create_date'], datetime): self.fields['file_create_date'] = [self.fields['file_create_date']] + self.fields['file_create_date'] = list(map(_add_missing_timezone, self.fields['file_create_date'])) fieldnames = [ @@ -749,6 +751,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 8476914f5..568aeb1ca 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -1,4 +1,5 @@ import numpy as np +import os import pandas as pd from datetime import datetime @@ -516,3 +517,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)