diff --git a/src/spikeinterface/core/tests/test_zarrextractors.py b/src/spikeinterface/core/tests/test_zarrextractors.py index e8c76018de..b0b69d1313 100644 --- a/src/spikeinterface/core/tests/test_zarrextractors.py +++ b/src/spikeinterface/core/tests/test_zarrextractors.py @@ -186,3 +186,12 @@ def test_ZarrSortingExtractor_lazy_search(tmp_path): test_ZarrSortingExtractor(tmp_path) test_ZarrSampleIndexSearch(tmp_path) test_ZarrSortingExtractor_lazy_search(tmp_path) + + +def test_zarr_path_with_hash(tmp_path): + # zarr 3 parses str paths as URLs, truncating them at '#' + folder = tmp_path / "Record Node 103#Probe.zarr" + sorting = generate_sorting(durations=[2], num_units=3) + ZarrSortingExtractor.write_sorting(sorting, folder) + loaded = ZarrSortingExtractor(folder) + assert loaded.get_num_units() == 3 diff --git a/src/spikeinterface/core/zarr_tools.py b/src/spikeinterface/core/zarr_tools.py index a12c4ceec2..9bffd3e3bc 100644 --- a/src/spikeinterface/core/zarr_tools.py +++ b/src/spikeinterface/core/zarr_tools.py @@ -537,3 +537,15 @@ def build_codec_pipeline(filters=None, compressors=None): codec_kwargs["serializer"] = serializers[0] if len(serializers) == 1 else "auto" codec_kwargs["compressors"] = byte_compressors return codec_kwargs + + +def local_zarr_store(path): + """ + Return a zarr LocalStore for local paths (remote paths are returned as str). + Passing a plain str to zarr 3 parses it as a URL, so characters like '#' truncate the path. + """ + from .core_tools import is_path_remote + + if is_path_remote(str(path)): + return str(path) + return zarr.storage.LocalStore(path) diff --git a/src/spikeinterface/core/zarrextractors.py b/src/spikeinterface/core/zarrextractors.py index 9f06b80e3e..002a5c756a 100644 --- a/src/spikeinterface/core/zarrextractors.py +++ b/src/spikeinterface/core/zarrextractors.py @@ -17,7 +17,7 @@ is_path_remote, ) from .job_tools import split_job_kwargs, fix_job_kwargs -from .zarr_tools import iterate_zarr_group, adjust_chunks_shards_and_job_kwargs, build_codec_pipeline +from .zarr_tools import local_zarr_store, iterate_zarr_group, adjust_chunks_shards_and_job_kwargs, build_codec_pipeline from .time_series_tools import _write_time_series_to_zarr # The spike vector uses the numcodecs Delta filter (not in the zarr v3 spec) because it greatly @@ -97,7 +97,7 @@ def super_zarr_open(folder_path: str | Path, mode: str = "r", storage_options: d raise ValueError(f"Folder {folder_path} does not exist") for use_consolidated in use_consolidated_options: try: - root = zarr.open(str(folder_path), mode=mode, use_consolidated=use_consolidated) + root = zarr.open(local_zarr_store(folder_path), mode=mode, use_consolidated=use_consolidated) break except Exception as e: exception = e @@ -266,7 +266,7 @@ def write_recording( **kwargs, ): folder_path = create_zarr_path_for_write(folder_path, overwrite=overwrite) - zarr_root = zarr.open(str(folder_path), mode="w", storage_options=storage_options) + zarr_root = zarr.open(local_zarr_store(folder_path), mode="w", storage_options=storage_options) zarr_root.attrs["zarr_class_info"] = retrieve_importing_provenance(ZarrRecordingExtractor) add_recording_to_zarr_group(recording, zarr_root, **kwargs) return ZarrRecordingExtractor(folder_path, storage_options=storage_options) @@ -676,7 +676,7 @@ def write_sorting( Write a sorting extractor to zarr format. """ folder_path = create_zarr_path_for_write(folder_path, overwrite=overwrite) - zarr_root = zarr.open(str(folder_path), mode="w", storage_options=storage_options) + zarr_root = zarr.open(local_zarr_store(folder_path), mode="w", storage_options=storage_options) zarr_root.attrs["zarr_class_info"] = retrieve_importing_provenance(ZarrSortingExtractor) add_sorting_to_zarr_group(sorting, zarr_root, relative_to=relative_to, **kwargs) return ZarrSortingExtractor(folder_path, storage_options=storage_options)