Skip to content

Commit

Permalink
add warning for file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Feb 21, 2025
1 parent b5b3ce6 commit 45bb063
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/hdmf_zarr/nwb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module with Zarr backend for NWB for integration with PyNWB"""

import warnings
from pathlib import Path
from .backend import ZarrIO, SUPPORTED_ZARR_STORES

Expand Down Expand Up @@ -42,6 +43,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.zarr'):
warnings.warn(f"The file path provided: {path} does not end in '.nwb.zarr'. "
"It is recommended that NWB files using the Zarr backend use the '.nwb.zarr' extension", UserWarning)

if load_namespaces:
tm = get_type_map()
super().load_namespaces(tm, path, storage_options)
Expand Down
44 changes: 31 additions & 13 deletions tests/unit/test_nwbzarrio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import shutil
from datetime import datetime
from dateutil.tz import tzlocal
from pathlib import Path

from hdmf.testing import TestCase

try:
from pynwb import NWBFile
Expand All @@ -14,18 +17,11 @@


@unittest.skipIf(not PYNWB_AVAILABLE, "PyNWB not installed")
class TestNWBZarrIO(unittest.TestCase):
class TestNWBZarrIO(TestCase):

def setUp(self):
self.filepath = "test_io.zarr"

def tearDown(self):
if os.path.exists(self.filepath):
shutil.rmtree(self.filepath)

def write_test_file(self):
# Create the NWBFile
nwbfile = NWBFile(
self.filepath = "test_io.nwb.zarr"
self.nwbfile = NWBFile(
session_description="my first synthetic recording",
identifier="EXAMPLE_ID",
session_start_time=datetime.now(tzlocal()),
Expand All @@ -35,11 +31,33 @@ def write_test_file(self):
experiment_description="I went on an adventure with thirteen dwarves to reclaim vast treasures.",
session_id="LONELYMTN",
)

# Create a device
nwbfile.create_device(name="array", description="the best array", manufacturer="Probe Company 9000")
self.nwbfile.create_device(name="array", description="the best array", manufacturer="Probe Company 9000")

def tearDown(self):
if os.path.exists(self.filepath):
shutil.rmtree(self.filepath)

def write_test_file(self):
with NWBZarrIO(path=self.filepath, mode="w") as io:
io.write(nwbfile)
io.write(self.nwbfile)

def test_file_extension_warning(self):
"""Test that a warning is raised when the file extension is not .nwb.zarr"""
wrong_filepath = Path(self.filepath).with_suffix('.h5')

msg = (f"The file path provided: {wrong_filepath} does not end in '.nwb.zarr'. "
"It is recommended that NWB files using the Zarr backend use the '.nwb.zarr' extension")

with self.assertWarnsWith(UserWarning, msg):
with NWBZarrIO(path=wrong_filepath, mode="w") as io:
io.write(self.nwbfile)

# should not warn on read or append
with NWBZarrIO(wrong_filepath, 'r') as io:
io.read()
with NWBZarrIO(wrong_filepath, 'a') as io:
io.read()

def test_read_nwb(self):
"""
Expand Down

0 comments on commit 45bb063

Please sign in to comment.