Skip to content

Commit e55de6c

Browse files
authored
Warn if writing a file without the appropriate extension (#1978)
* add warning for non nwb extension * add tests and update test filenames * update CHANGELOG.md * Remove comment in tests
1 parent 2f2a494 commit e55de6c

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- Fixed bug in how `ElectrodeGroup.__init__` validates its `position` argument. @oruebel [#1770](https://github.com/NeurodataWithoutBorders/pynwb/pull/1770)
1010
- Changed `SpatialSeries.reference_frame` from required to optional as specified in the schema. @rly [#1986](https://github.com/NeurodataWithoutBorders/pynwb/pull/1986)
1111

12+
### Enhancements and minor changes`
13+
- Added warning when writing files with `NWBHDF5IO` without the `.nwb` extension. @stephprince [#1978](https://github.com/NeurodataWithoutBorders/pynwb/pull/1978)
14+
1215
## PyNWB 2.8.2 (September 9, 2024)
1316

1417
### Enhancements and minor changes

src/pynwb/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ def __init__(self, **kwargs):
397397
if mode in io_modes_that_create_file or manager is not None or extensions is not None:
398398
load_namespaces = False
399399

400+
if mode in io_modes_that_create_file and not str(path).endswith('.nwb'):
401+
warn(f"The file path provided: {path} does not end in '.nwb'. "
402+
"It is recommended that NWB files using the HDF5 backend use the '.nwb' extension.", UserWarning)
403+
400404
if load_namespaces:
401405
tm = get_type_map()
402406
super().load_namespaces(tm, path, file=file_obj, driver=driver, aws_region=aws_region)

tests/integration/hdf5/test_file_copy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class TestFileCopy(TestCase):
1010

1111
def setUp(self):
12-
self.path1 = "test_a.h5"
13-
self.path2 = "test_b.h5"
12+
self.path1 = "test_a.nwb"
13+
self.path2 = "test_b.nwb"
1414

1515
def tearDown(self):
1616
if os.path.exists(self.path1):

tests/integration/hdf5/test_io.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def setUp(self):
313313
self.nwbfile = NWBFile(session_description='a',
314314
identifier='b',
315315
session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc()))
316-
self.path = "test_pynwb_io_hdf5_h5dataIO.h5"
316+
self.path = "test_pynwb_io_hdf5_h5dataIO.nwb"
317317

318318
def tearDown(self):
319319
remove_test_file(self.path)
@@ -428,7 +428,7 @@ def setUp(self):
428428
self.nwbfile = NWBFile(session_description='a test NWB File',
429429
identifier='TEST123',
430430
session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc()))
431-
self.path = "test_pynwb_io_nwbhdf5.h5"
431+
self.path = "test_pynwb_io_nwbhdf5.nwb"
432432

433433
def tearDown(self):
434434
remove_test_file(self.path)
@@ -532,6 +532,23 @@ def test_round_trip_with_pathlib_path(self):
532532
read_file = io.read()
533533
self.assertContainerEqual(read_file, self.nwbfile)
534534

535+
def test_warn_for_nwb_extension(self):
536+
"""Creating a file with an extension other than .nwb should raise a warning"""
537+
pathlib_path = Path(self.path).with_suffix('.h5')
538+
539+
with self.assertWarns(UserWarning):
540+
with NWBHDF5IO(pathlib_path, 'w') as io:
541+
io.write(self.nwbfile)
542+
with self.assertWarns(UserWarning):
543+
with NWBHDF5IO(str(pathlib_path), 'w') as io:
544+
io.write(self.nwbfile)
545+
546+
# should not warn on read or append
547+
with NWBHDF5IO(str(pathlib_path), 'r') as io:
548+
io.read()
549+
with NWBHDF5IO(str(pathlib_path), 'a') as io:
550+
io.read()
551+
535552
def test_can_read_current_nwb_file(self):
536553
with NWBHDF5IO(self.path, 'w') as io:
537554
io.write(self.nwbfile)

tests/unit/test_icephys_metadata_tables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def setUp(self):
8282
sweep_number=np.uint64(15)
8383
)
8484
self.nwbfile.add_acquisition(self.response)
85-
self.path = 'test_icephys_meta_intracellularrecording.h5'
85+
self.path = 'test_icephys_meta_intracellularrecording.nwb'
8686

8787
def tearDown(self):
8888
remove_test_file(self.path)
@@ -1037,7 +1037,7 @@ class NWBFileTests(TestCase):
10371037
"""
10381038
def setUp(self):
10391039
warnings.simplefilter("always") # Trigger all warnings
1040-
self.path = 'test_icephys_meta_intracellularrecording.h5'
1040+
self.path = 'test_icephys_meta_intracellularrecording.nwb'
10411041

10421042
def tearDown(self):
10431043
remove_test_file(self.path)

0 commit comments

Comments
 (0)