From 9bfff8555a9667f19fa234e0a326293893af5ae3 Mon Sep 17 00:00:00 2001 From: rly Date: Thu, 25 Apr 2024 11:06:34 -0700 Subject: [PATCH] Update tests --- src/pynwb/io/file.py | 2 -- src/pynwb/testing/mock/file.py | 2 +- src/pynwb/testing/testh5io.py | 6 ++-- tests/integration/hdf5/test_base.py | 4 +-- tests/integration/hdf5/test_io.py | 4 +-- .../integration/hdf5/test_modular_storage.py | 2 +- tests/integration/hdf5/test_nwbfile.py | 31 ++++++++++++++++++- tests/unit/test_epoch.py | 2 +- tests/unit/test_extension.py | 4 +-- tests/unit/test_file.py | 7 +++-- tests/unit/test_icephys.py | 12 +++---- tests/unit/test_scratch.py | 2 +- 12 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/pynwb/io/file.py b/src/pynwb/io/file.py index c56319222..15c2c1c06 100644 --- a/src/pynwb/io/file.py +++ b/src/pynwb/io/file.py @@ -16,8 +16,6 @@ def parse_datetime(datestr): :param datestr: str :return: datetime.datetime or datetime.date """ - if datestr is None: - return None if isinstance(datestr, bytes): datestr = datestr.decode("utf-8") dt = datetime.datetime.fromisoformat(datestr) diff --git a/src/pynwb/testing/mock/file.py b/src/pynwb/testing/mock/file.py index 943f86dcb..ee7fc5655 100644 --- a/src/pynwb/testing/mock/file.py +++ b/src/pynwb/testing/mock/file.py @@ -10,7 +10,7 @@ def mock_NWBFile( session_description: str = 'session_description', identifier: Optional[str] = None, - session_start_time: datetime = datetime(1970, 1, 1, tzinfo=tzlocal()), + session_start_time: datetime = datetime(1970, 1, 1), **kwargs ): return NWBFile( diff --git a/src/pynwb/testing/testh5io.py b/src/pynwb/testing/testh5io.py index 7234e79f5..6a5c96547 100644 --- a/src/pynwb/testing/testh5io.py +++ b/src/pynwb/testing/testh5io.py @@ -33,8 +33,8 @@ def getContainer(self, nwbfile): def setUp(self): self.container = self.setUpContainer() - self.start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc()) - self.create_date = datetime(2018, 4, 15, 12, tzinfo=tzlocal()) + self.start_time = datetime(1971, 1, 1, 12) + self.create_date = datetime(2018, 4, 15, 12) self.container_type = self.container.__class__.__name__ self.filename = 'test_%s.nwb' % self.container_type self.export_filename = 'test_export_%s.nwb' % self.container_type @@ -226,7 +226,7 @@ def setUp(self): container_type = self.getContainerType().replace(" ", "_") session_description = 'A file to test writing and reading a %s' % container_type identifier = 'TEST_%s' % container_type - session_start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc()) + session_start_time = datetime(1971, 1, 1, 12) self.nwbfile = NWBFile( session_description=session_description, identifier=identifier, diff --git a/tests/integration/hdf5/test_base.py b/tests/integration/hdf5/test_base.py index 60f8510ff..145f55d0f 100644 --- a/tests/integration/hdf5/test_base.py +++ b/tests/integration/hdf5/test_base.py @@ -34,7 +34,7 @@ def test_timestamps_linking(self): tsa = TimeSeries(name='a', data=np.linspace(0, 1, 1000), timestamps=np.arange(1000.), unit='m') tsb = TimeSeries(name='b', data=np.linspace(0, 1, 1000), timestamps=tsa, unit='m') nwbfile = NWBFile(identifier='foo', - session_start_time=datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal()), + session_start_time=datetime(2017, 5, 1, 12, 0, 0), session_description='bar') nwbfile.add_acquisition(tsa) nwbfile.add_acquisition(tsb) @@ -52,7 +52,7 @@ def test_data_linking(self): tsb = TimeSeries(name='b', data=tsa, timestamps=np.arange(1000.), unit='m') tsc = TimeSeries(name='c', data=tsb, timestamps=np.arange(1000.), unit='m') nwbfile = NWBFile(identifier='foo', - session_start_time=datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal()), + session_start_time=datetime(2017, 5, 1, 12, 0, 0), session_description='bar') nwbfile.add_acquisition(tsa) nwbfile.add_acquisition(tsb) diff --git a/tests/integration/hdf5/test_io.py b/tests/integration/hdf5/test_io.py index d68334c89..da22a0651 100644 --- a/tests/integration/hdf5/test_io.py +++ b/tests/integration/hdf5/test_io.py @@ -245,7 +245,7 @@ class TestAppend(TestCase): def setUp(self): self.nwbfile = NWBFile(session_description='hi', identifier='hi', - session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc())) + session_start_time=datetime(1970, 1, 1, 12)) self.path = "test_append.nwb" def tearDown(self): @@ -312,7 +312,7 @@ class TestH5DataIO(TestCase): def setUp(self): self.nwbfile = NWBFile(session_description='a', identifier='b', - session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc())) + session_start_time=datetime(1970, 1, 1, 12)) self.path = "test_pynwb_io_hdf5_h5dataIO.h5" def tearDown(self): diff --git a/tests/integration/hdf5/test_modular_storage.py b/tests/integration/hdf5/test_modular_storage.py index fba5d02db..bfe71b772 100644 --- a/tests/integration/hdf5/test_modular_storage.py +++ b/tests/integration/hdf5/test_modular_storage.py @@ -19,7 +19,7 @@ def setUp(self): self.link_filename = os.path.join(os.getcwd(), 'test_time_series_modular_link.nwb') # Make the data container file write - self.start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc()) + self.start_time = datetime(1971, 1, 1, 12) self.data = np.arange(2000).reshape((1000, 2)) self.timestamps = np.linspace(0, 1, 1000) # The container before roundtrip diff --git a/tests/integration/hdf5/test_nwbfile.py b/tests/integration/hdf5/test_nwbfile.py index 1bfb35d88..641928598 100644 --- a/tests/integration/hdf5/test_nwbfile.py +++ b/tests/integration/hdf5/test_nwbfile.py @@ -21,7 +21,12 @@ def setUp(self): """ Set up an NWBFile object with an acquisition TimeSeries, analysis TimeSeries, and a processing module """ self.start_time = datetime(1970, 1, 1, 12, tzinfo=tzutc()) self.ref_time = datetime(1979, 1, 1, 0, tzinfo=tzutc()) - self.create_date = datetime(2017, 4, 15, 12, tzinfo=tzlocal()) + # try some dates with/without timezone and time + self.create_date = [ + datetime(2017, 5, 1, 12, tzinfo=tzlocal()), + datetime(2017, 5, 2, 13), + datetime(2017, 5, 2), + ] self.manager = get_manager() self.filename = 'test_nwbfileio.h5' self.nwbfile = NWBFile(session_description='a test NWB File', @@ -327,6 +332,30 @@ def getContainer(self, nwbfile): return nwbfile.subject +class TestSubjectMinimalSetIO(NWBH5IOMixin, TestCase): + + def setUpContainer(self): + """ Return the test Subject """ + return Subject( + age="P90D", + description="An unfortunate rat", + genotype="WT", + sex="M", + species="Rattus norvegicus", + subject_id="RAT123", + weight="2 kg", + strain="my_strain", + ) + + def addContainer(self, nwbfile): + """ Add the test Subject to the given NWBFile """ + nwbfile.subject = self.container + + def getContainer(self, nwbfile): + """ Return the test Subject from the given NWBFile """ + return nwbfile.subject + + class TestEmptySubjectIO(TestSubjectIO): def setUpContainer(self): diff --git a/tests/unit/test_epoch.py b/tests/unit/test_epoch.py index 318ad3943..6cd3f6575 100644 --- a/tests/unit/test_epoch.py +++ b/tests/unit/test_epoch.py @@ -67,7 +67,7 @@ def test_dataframe_roundtrip_drop_ts(self): self.assertEqual(obtained.loc[2, 'foo'], df.loc[2, 'foo']) def test_no_tags(self): - nwbfile = NWBFile("a file with header data", "NB123A", datetime(1970, 1, 1, tzinfo=tz.tzutc())) + nwbfile = NWBFile("a file with header data", "NB123A", datetime(1970, 1, 1)) df = self.get_dataframe() for i, row in df.iterrows(): nwbfile.add_epoch(start_time=row['start_time'], stop_time=row['stop_time']) diff --git a/tests/unit/test_extension.py b/tests/unit/test_extension.py index 7664bbf22..a94268e93 100644 --- a/tests/unit/test_extension.py +++ b/tests/unit/test_extension.py @@ -108,7 +108,7 @@ def __init__(self, **kwargs): super().__init__(**kwargs) self.test_attr = test_attr - nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())) + nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0)) nwbfile.add_lab_meta_data(MyTestMetaData(name='test_name', test_attr=5.)) @@ -128,7 +128,7 @@ def test_lab_meta_auto(self): MyTestMetaData = get_class('MyTestMetaData', self.prefix) - nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())) + nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0)) nwbfile.add_lab_meta_data(MyTestMetaData(name='test_name', test_attr=5.)) diff --git a/tests/unit/test_file.py b/tests/unit/test_file.py index 19b1aa9c9..fc44dfdd8 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -19,9 +19,10 @@ class NWBFileTest(TestCase): def setUp(self): self.start = datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal()) self.ref_time = datetime(1979, 1, 1, 0, tzinfo=tzutc()) + # try some dates with/without timezone and time self.create = [datetime(2017, 5, 1, 12, tzinfo=tzlocal()), - datetime(2017, 5, 2, 13, 0, 0, 1, tzinfo=tzutc()), - datetime(2017, 5, 2, 14, tzinfo=tzutc())] + datetime(2017, 5, 2, 13), + datetime(2017, 5, 2)] self.path = 'nwbfile_test.h5' self.nwbfile = NWBFile(session_description='a test session description for a test NWBFile', identifier='FILE123', @@ -533,7 +534,7 @@ def setUp(self): date_of_birth=datetime(2017, 5, 1, 12, tzinfo=tzlocal()), strain='my_strain', ) - self.start = datetime(2017, 5, 1, 12, tzinfo=tzlocal()) + self.start = datetime(2017, 5, 1, 12) self.path = 'nwbfile_test.h5' self.nwbfile = NWBFile( 'a test session description for a test NWBFile', diff --git a/tests/unit/test_icephys.py b/tests/unit/test_icephys.py index e0e8332f9..860221418 100644 --- a/tests/unit/test_icephys.py +++ b/tests/unit/test_icephys.py @@ -46,7 +46,7 @@ def test_sweep_table_depractation_warn(self): _ = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), + session_start_time=datetime(2017, 4, 3, 11), ic_electrodes=[self.icephys_electrode, ], sweep_table=SweepTable()) @@ -57,14 +57,14 @@ def test_ic_electrodes_parameter_deprecation(self): _ = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), + session_start_time=datetime(2017, 4, 3, 11), ic_electrodes=[self.icephys_electrode, ]) def test_icephys_electrodes_parameter(self): nwbfile = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), + session_start_time=datetime(2017, 4, 3, 11), icephys_electrodes=[self.icephys_electrode, ]) self.assertEqual(nwbfile.get_icephys_electrode('test_iS'), self.icephys_electrode) @@ -73,7 +73,7 @@ def test_add_ic_electrode_deprecation(self): nwbfile = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal())) + session_start_time=datetime(2017, 4, 3, 11)) msg = "NWBFile.add_ic_electrode has been replaced by NWBFile.add_icephys_electrode." with self.assertWarnsWith(DeprecationWarning, msg): @@ -83,7 +83,7 @@ def test_ic_electrodes_attribute_deprecation(self): nwbfile = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), + session_start_time=datetime(2017, 4, 3, 11), icephys_electrodes=[self.icephys_electrode, ]) # make sure NWBFile.ic_electrodes property warns @@ -100,7 +100,7 @@ def test_create_ic_electrode_deprecation(self): nwbfile = NWBFile( session_description='NWBFile icephys test', identifier='NWB123', # required - session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal())) + session_start_time=datetime(2017, 4, 3, 11)) device = Device(name='device_name') msg = "NWBFile.create_ic_electrode has been replaced by NWBFile.create_icephys_electrode." with self.assertWarnsWith(DeprecationWarning, msg): diff --git a/tests/unit/test_scratch.py b/tests/unit/test_scratch.py index 398ae2f78..29ecddbcc 100644 --- a/tests/unit/test_scratch.py +++ b/tests/unit/test_scratch.py @@ -15,7 +15,7 @@ def setUp(self): self.nwbfile = NWBFile( session_description='a file to test writing and reading scratch data', identifier='TEST_scratch', - session_start_time=datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal()) + session_start_time=datetime(2017, 5, 1, 12, 0, 0) ) def test_constructor_list(self):