Skip to content

Commit 45bb063

Browse files
committed
add warning for file extension
1 parent b5b3ce6 commit 45bb063

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/hdmf_zarr/nwb.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Module with Zarr backend for NWB for integration with PyNWB"""
22

3+
import warnings
34
from pathlib import Path
45
from .backend import ZarrIO, SUPPORTED_ZARR_STORES
56

@@ -42,6 +43,10 @@ def __init__(self, **kwargs):
4243
if mode in io_modes_that_create_file or manager is not None or extensions is not None:
4344
load_namespaces = False
4445

46+
if mode in io_modes_that_create_file and not str(path).endswith('.nwb.zarr'):
47+
warnings.warn(f"The file path provided: {path} does not end in '.nwb.zarr'. "
48+
"It is recommended that NWB files using the Zarr backend use the '.nwb.zarr' extension", UserWarning)
49+
4550
if load_namespaces:
4651
tm = get_type_map()
4752
super().load_namespaces(tm, path, storage_options)

tests/unit/test_nwbzarrio.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import shutil
55
from datetime import datetime
66
from dateutil.tz import tzlocal
7+
from pathlib import Path
8+
9+
from hdmf.testing import TestCase
710

811
try:
912
from pynwb import NWBFile
@@ -14,18 +17,11 @@
1417

1518

1619
@unittest.skipIf(not PYNWB_AVAILABLE, "PyNWB not installed")
17-
class TestNWBZarrIO(unittest.TestCase):
20+
class TestNWBZarrIO(TestCase):
1821

1922
def setUp(self):
20-
self.filepath = "test_io.zarr"
21-
22-
def tearDown(self):
23-
if os.path.exists(self.filepath):
24-
shutil.rmtree(self.filepath)
25-
26-
def write_test_file(self):
27-
# Create the NWBFile
28-
nwbfile = NWBFile(
23+
self.filepath = "test_io.nwb.zarr"
24+
self.nwbfile = NWBFile(
2925
session_description="my first synthetic recording",
3026
identifier="EXAMPLE_ID",
3127
session_start_time=datetime.now(tzlocal()),
@@ -35,11 +31,33 @@ def write_test_file(self):
3531
experiment_description="I went on an adventure with thirteen dwarves to reclaim vast treasures.",
3632
session_id="LONELYMTN",
3733
)
38-
3934
# Create a device
40-
nwbfile.create_device(name="array", description="the best array", manufacturer="Probe Company 9000")
35+
self.nwbfile.create_device(name="array", description="the best array", manufacturer="Probe Company 9000")
36+
37+
def tearDown(self):
38+
if os.path.exists(self.filepath):
39+
shutil.rmtree(self.filepath)
40+
41+
def write_test_file(self):
4142
with NWBZarrIO(path=self.filepath, mode="w") as io:
42-
io.write(nwbfile)
43+
io.write(self.nwbfile)
44+
45+
def test_file_extension_warning(self):
46+
"""Test that a warning is raised when the file extension is not .nwb.zarr"""
47+
wrong_filepath = Path(self.filepath).with_suffix('.h5')
48+
49+
msg = (f"The file path provided: {wrong_filepath} does not end in '.nwb.zarr'. "
50+
"It is recommended that NWB files using the Zarr backend use the '.nwb.zarr' extension")
51+
52+
with self.assertWarnsWith(UserWarning, msg):
53+
with NWBZarrIO(path=wrong_filepath, mode="w") as io:
54+
io.write(self.nwbfile)
55+
56+
# should not warn on read or append
57+
with NWBZarrIO(wrong_filepath, 'r') as io:
58+
io.read()
59+
with NWBZarrIO(wrong_filepath, 'a') as io:
60+
io.read()
4361

4462
def test_read_nwb(self):
4563
"""

0 commit comments

Comments
 (0)