From dfd5ef3ca400a4364dfabeb15800294235e01214 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:49:14 -0800 Subject: [PATCH 1/4] add warning for non nwb extension --- src/pynwb/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pynwb/__init__.py b/src/pynwb/__init__.py index 1d109abe3..2ac87a3ea 100644 --- a/src/pynwb/__init__.py +++ b/src/pynwb/__init__.py @@ -397,6 +397,10 @@ def __init__(self, **kwargs): if mode in io_modes_that_create_file or manager is not None or extensions is not None: load_namespaces = False + if mode in io_modes_that_create_file and not str(path).endswith('.nwb'): + warn(f"The file path provided: {path} does not end in '.nwb'. " + "It is recommended that NWB files using the HDF5 backend use the '.nwb' extension.", UserWarning) + if load_namespaces: tm = get_type_map() super().load_namespaces(tm, path, file=file_obj, driver=driver, aws_region=aws_region) From f9f46e3277d6ad13d9028490ea0bfaa6ab825e2f Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:49:46 -0800 Subject: [PATCH 2/4] add tests and update test filenames --- tests/integration/hdf5/test_file_copy.py | 4 ++-- tests/integration/hdf5/test_io.py | 22 ++++++++++++++++++++-- tests/unit/test_icephys_metadata_tables.py | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/integration/hdf5/test_file_copy.py b/tests/integration/hdf5/test_file_copy.py index b491586fb..be23f74ca 100644 --- a/tests/integration/hdf5/test_file_copy.py +++ b/tests/integration/hdf5/test_file_copy.py @@ -9,8 +9,8 @@ class TestFileCopy(TestCase): def setUp(self): - self.path1 = "test_a.h5" - self.path2 = "test_b.h5" + self.path1 = "test_a.nwb" + self.path2 = "test_b.nwb" def tearDown(self): if os.path.exists(self.path1): diff --git a/tests/integration/hdf5/test_io.py b/tests/integration/hdf5/test_io.py index e1ce4269b..c0c78284d 100644 --- a/tests/integration/hdf5/test_io.py +++ b/tests/integration/hdf5/test_io.py @@ -313,7 +313,7 @@ def setUp(self): self.nwbfile = NWBFile(session_description='a', identifier='b', session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc())) - self.path = "test_pynwb_io_hdf5_h5dataIO.h5" + self.path = "test_pynwb_io_hdf5_h5dataIO.nwb" def tearDown(self): remove_test_file(self.path) @@ -428,7 +428,7 @@ def setUp(self): self.nwbfile = NWBFile(session_description='a test NWB File', identifier='TEST123', session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc())) - self.path = "test_pynwb_io_nwbhdf5.h5" + self.path = "test_pynwb_io_nwbhdf5.nwb" def tearDown(self): remove_test_file(self.path) @@ -532,6 +532,24 @@ def test_round_trip_with_pathlib_path(self): read_file = io.read() self.assertContainerEqual(read_file, self.nwbfile) + def test_warn_for_nwb_extension(self): + """Creating a file with an extension other than .nwb should raise a warning""" + pathlib_path = Path(self.path).with_suffix('.h5') + + # with self.assertRaises(UserWarning): + with self.assertWarns(UserWarning): + with NWBHDF5IO(pathlib_path, 'w') as io: + io.write(self.nwbfile) + with self.assertWarns(UserWarning): + with NWBHDF5IO(str(pathlib_path), 'w') as io: + io.write(self.nwbfile) + + # should not warn on read or append + with NWBHDF5IO(str(pathlib_path), 'r') as io: + io.read() + with NWBHDF5IO(str(pathlib_path), 'a') as io: + io.read() + def test_can_read_current_nwb_file(self): with NWBHDF5IO(self.path, 'w') as io: io.write(self.nwbfile) diff --git a/tests/unit/test_icephys_metadata_tables.py b/tests/unit/test_icephys_metadata_tables.py index b31ee9215..a357f3288 100644 --- a/tests/unit/test_icephys_metadata_tables.py +++ b/tests/unit/test_icephys_metadata_tables.py @@ -82,7 +82,7 @@ def setUp(self): sweep_number=np.uint64(15) ) self.nwbfile.add_acquisition(self.response) - self.path = 'test_icephys_meta_intracellularrecording.h5' + self.path = 'test_icephys_meta_intracellularrecording.nwb' def tearDown(self): remove_test_file(self.path) @@ -1037,7 +1037,7 @@ class NWBFileTests(TestCase): """ def setUp(self): warnings.simplefilter("always") # Trigger all warnings - self.path = 'test_icephys_meta_intracellularrecording.h5' + self.path = 'test_icephys_meta_intracellularrecording.nwb' def tearDown(self): remove_test_file(self.path) From 2517865f77bb627a053e32fb501f49fb5cb3e5b6 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Thu, 7 Nov 2024 12:04:08 -0800 Subject: [PATCH 3/4] update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16e504659..e52ea2e81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ ### Bug fixes - Fixed bug in how `ElectrodeGroup.__init__` validates its `position` argument. @oruebel [#1770](https://github.com/NeurodataWithoutBorders/pynwb/pull/1770) +### Enhancements and minor changes` +- Added warning when writing files with `NWBHDF5IO` without the `.nwb` extension. @stephprince [#1978](https://github.com/NeurodataWithoutBorders/pynwb/pull/1978) + ## PyNWB 2.8.2 (September 9, 2024) ### Enhancements and minor changes From ccddddcfd77bf7a46e47465cf5d78a59f2569062 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:40:38 -0800 Subject: [PATCH 4/4] Remove comment in tests --- tests/integration/hdf5/test_io.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/hdf5/test_io.py b/tests/integration/hdf5/test_io.py index c0c78284d..a97eb1b63 100644 --- a/tests/integration/hdf5/test_io.py +++ b/tests/integration/hdf5/test_io.py @@ -536,7 +536,6 @@ def test_warn_for_nwb_extension(self): """Creating a file with an extension other than .nwb should raise a warning""" pathlib_path = Path(self.path).with_suffix('.h5') - # with self.assertRaises(UserWarning): with self.assertWarns(UserWarning): with NWBHDF5IO(pathlib_path, 'w') as io: io.write(self.nwbfile) @@ -544,7 +543,7 @@ def test_warn_for_nwb_extension(self): with NWBHDF5IO(str(pathlib_path), 'w') as io: io.write(self.nwbfile) - # should not warn on read or append + # should not warn on read or append with NWBHDF5IO(str(pathlib_path), 'r') as io: io.read() with NWBHDF5IO(str(pathlib_path), 'a') as io: