From 59e80753792122247edf42fea323dbb225f43ecf Mon Sep 17 00:00:00 2001 From: Ryan Ly Date: Sun, 25 Jun 2023 16:45:45 -0700 Subject: [PATCH] Adjust test to not count warnings on read (#1698) * Show uncaught warning correctly * Adjust test to not check for warnings * Update changelog * Fix flake8 --- CHANGELOG.md | 2 ++ src/pynwb/testing/testh5io.py | 4 +-- tests/unit/test_epoch_legacy.py | 61 +++++++++++++++------------------ 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24cd0985e..74ccf5080 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ [#1690](https://github.com/NeurodataWithoutBorders/pynwb/pull/1690) - Update `requirements-doc.txt` to resolve Python 3.7 incompatibility. @rly [#1694](https://github.com/NeurodataWithoutBorders/pynwb/pull/1694) +- Fixed test battery to show and check for warnings appropriately. @rly + [#1698](https://github.com/NeurodataWithoutBorders/pynwb/pull/1698) ## PyNWB 2.3.2 (April 10, 2023) diff --git a/src/pynwb/testing/testh5io.py b/src/pynwb/testing/testh5io.py index 08626f943..b45407bfb 100644 --- a/src/pynwb/testing/testh5io.py +++ b/src/pynwb/testing/testh5io.py @@ -107,7 +107,7 @@ def roundtripContainer(self, cache_spec=False): BrokenLinkWarning)): raise Exception('%s: %s' % (w.category.__name__, w.message)) else: - warnings.warn(w.message, w.category) + warnings.showwarning(w.message, w.category, w.filename, w.lineno, w.file, w.line) try: return self.getContainer(self.read_nwbfile) @@ -141,7 +141,7 @@ def roundtripExportContainer(self, cache_spec=False): BrokenLinkWarning)): raise Exception('%s: %s' % (w.category.__name__, w.message)) else: - warnings.warn(w.message, w.category) + warnings.showwarning(w.message, w.category, w.filename, w.lineno, w.file, w.line) try: return self.getContainer(self.read_exported_nwbfile) diff --git a/tests/unit/test_epoch_legacy.py b/tests/unit/test_epoch_legacy.py index d204c94e3..1f6c50f38 100644 --- a/tests/unit/test_epoch_legacy.py +++ b/tests/unit/test_epoch_legacy.py @@ -3,7 +3,6 @@ from pynwb import NWBFile, NWBHDF5IO from pynwb.base import TimeSeries, TimeSeriesReference, TimeSeriesReferenceVectorData import numpy as np -import warnings import h5py @@ -66,41 +65,35 @@ def test_legacy_format(self): nwbfile = NWBFile(description, identifier, self.start_time, file_create_date=self.create_date) self.addContainer(nwbfile) - with warnings.catch_warnings(record=True) as ws: - # write the file - with NWBHDF5IO(self.filename, mode='w') as write_io: - write_io.write(nwbfile, cache_spec=False) - # Modify the HDF5 file to look like NWB 2.4 and earlier. This simply means - # modifying the neurodata_type on the TimeIntervals.timeseries column - with h5py.File(self.filename, mode='a') as infile: - infile['/intervals/epochs/timeseries'].attrs['neurodata_type'] = 'VectorData' - infile.attrs['nwb_version'] = '2.3.0' - # Make sure we didn't have warnings - self.assertEqual(len(ws), 0) + # write the file + with NWBHDF5IO(self.filename, mode='w') as write_io: + write_io.write(nwbfile, cache_spec=False) + # Modify the HDF5 file to look like NWB 2.4 and earlier. This simply means + # modifying the neurodata_type on the TimeIntervals.timeseries column + with h5py.File(self.filename, mode='a') as infile: + infile['/intervals/epochs/timeseries'].attrs['neurodata_type'] = 'VectorData' + infile.attrs['nwb_version'] = '2.3.0' # Read the file back - with warnings.catch_warnings(record=True) as ws: - self.reader = NWBHDF5IO(self.filename, mode='r') - self.read_nwbfile = self.reader.read() + self.reader = NWBHDF5IO(self.filename, mode='r') + self.read_nwbfile = self.reader.read() - # Test that the VectorData column for timeseries has been converted to TimeSeriesReferenceVectorData - self.assertIsInstance(self.read_nwbfile.epochs.timeseries, TimeSeriesReferenceVectorData) + # Test that the VectorData column for timeseries has been converted to TimeSeriesReferenceVectorData + self.assertIsInstance(self.read_nwbfile.epochs.timeseries, TimeSeriesReferenceVectorData) - # Test that slicing into epochs.timeseries works as expected - re = self.read_nwbfile.epochs.timeseries[0] - self.assertIsInstance(re, TimeSeriesReference) - self.assertTupleEqual((re[0], re[1], re[2].object_id), (0, 5, nwbfile.get_acquisition('a').object_id)) + # Test that slicing into epochs.timeseries works as expected + re = self.read_nwbfile.epochs.timeseries[0] + self.assertIsInstance(re, TimeSeriesReference) + self.assertTupleEqual((re[0], re[1], re[2].object_id), (0, 5, nwbfile.get_acquisition('a').object_id)) - # Test that slicing into epochs works as expected - re = self.read_nwbfile.epochs[0:1] - self.assertListEqual(re.columns.tolist(), ['start_time', 'stop_time', 'temperature', 'tags', 'timeseries']) - for i in re.loc[0, 'timeseries']: - self.assertIsInstance(i, TimeSeriesReference) - self.assertTupleEqual( - (re.loc[0, 'timeseries'][0][0], re.loc[0, 'timeseries'][0][1], re.loc[0, 'timeseries'][0][2].object_id), - (0, 5, nwbfile.get_acquisition('a').object_id)) - self.assertTupleEqual( - (re.loc[0, 'timeseries'][1][0], re.loc[0, 'timeseries'][1][1], re.loc[0, 'timeseries'][1][2].object_id), - (0, 3, nwbfile.get_acquisition('b').object_id)) - # Make sure we didn't have warnings - self.assertEqual(len(ws), 0) + # Test that slicing into epochs works as expected + re = self.read_nwbfile.epochs[0:1] + self.assertListEqual(re.columns.tolist(), ['start_time', 'stop_time', 'temperature', 'tags', 'timeseries']) + for i in re.loc[0, 'timeseries']: + self.assertIsInstance(i, TimeSeriesReference) + self.assertTupleEqual( + (re.loc[0, 'timeseries'][0][0], re.loc[0, 'timeseries'][0][1], re.loc[0, 'timeseries'][0][2].object_id), + (0, 5, nwbfile.get_acquisition('a').object_id)) + self.assertTupleEqual( + (re.loc[0, 'timeseries'][1][0], re.loc[0, 'timeseries'][1][1], re.loc[0, 'timeseries'][1][2].object_id), + (0, 3, nwbfile.get_acquisition('b').object_id))