diff --git a/.github/workflows/deepinterpolation.yml b/.github/workflows/deepinterpolation.yml index 6ad13a4b4e..5b0cf1e7a0 100644 --- a/.github/workflows/deepinterpolation.yml +++ b/.github/workflows/deepinterpolation.yml @@ -20,10 +20,10 @@ jobs: - uses: actions/checkout@v6 - uses: actions/setup-python@v6 with: - python-version: "3.10" + python-version: "3.12" - uses: astral-sh/setup-uv@v7 with: - python-version: "3.10" + python-version: "3.12" enable-cache: false - name: Get changed files id: changed-files diff --git a/conftest.py b/conftest.py index e4a08c170a..6c4bfbef3a 100644 --- a/conftest.py +++ b/conftest.py @@ -47,16 +47,10 @@ def pytest_collection_modifyitems(config, items): This function marks (in the pytest sense) the tests according to their name and file_path location Marking them in turn allows the tests to be run by using the pytest -m marker_name option. """ - - from spikeinterface.core.core_tools import _is_zarr_write_supported - rootdir = Path(config.rootdir) modules_location = rootdir / "src" / "spikeinterface" for item in items: # TODO: remove once writing to zarr is supported with zarr>=3 - if item.get_closest_marker("requires_zarr_write") and not _is_zarr_write_supported(): - item.add_marker(pytest.mark.skip(reason="Writing to zarr is not supported yet with zarr>=3")) - if config.getoption("--mp-context") is not None and item.name == "test_global_job_kwargs": item.add_marker(pytest.mark.skip(reason="--mp-context changes the default job kwargs")) diff --git a/doc/modules/core.rst b/doc/modules/core.rst index 297f22e945..93ca94df56 100644 --- a/doc/modules/core.rst +++ b/doc/modules/core.rst @@ -644,8 +644,8 @@ used when creating a :py:class:`~spikeinterface.core.SortingAnalyzer` which caus .. _save_load: -Saving, loading, and compression --------------------------------- +Saving and loading +------------------ The Base SpikeInterface objects (:py:class:`~spikeinterface.core.BaseRecording`, :py:class:`~spikeinterface.core.BaseSorting`, and @@ -678,22 +678,42 @@ This saving/loading features enables us to store SpikeInterface objects efficien # save sorting to NPZ sorting_saved = sorting.save(folder="sorting") +Compression +----------- + **NOTE:** the Zarr format by default applies data compression with :code:`Blosc.Zstandard` codec with BIT shuffling. Any other Zarr-compatible compressors and filters can be applied using the :code:`compressor` and :code:`filters` -arguments. For example, in this case we apply `LZMA `_ -and use a `Delta `_ filter: +arguments. For example, in this case we apply ``LZMA `_ +and use a `Delta `_ filter: .. code-block:: python - from numcodecs import LZMA, Delta + from zarr.codecs import LZMA, Delta - compressor = LZMA() + compressors = [LZMA()] filters = [Delta(dtype="int16")] - recording_custom_comp = recording.save(folder="recording", format="zarr", - compressor=compressor, filters=filters, - **job_kwargs) + recording_custom_comp = recording.save( + folder="recording", + format="zarr", + compressor=compressors, + filters=filters, + **job_kwargs + ) + +Zarr stores each dataset as a grid of **chunks**: the unit of compression and of reading, so a chunk is always +decompressed as a whole. When saving a recording, the chunk size along time is set by the job parameters +(e.g. :code:`chunk_duration="1s"`). Zarr v3 also supports **shards**, which group many chunks in a single file/object. +Chunks are still compressed and read independently, but sharding drastically reduces the number of files, which is +useful on cluster file systems and cloud storage. A shard must be a multiple of the chunk size, which is set with +:code:`shard_factor` (shard size = :code:`shard_factor` x chunk size). For example, 1 s chunks and 20 s shards: + +.. code-block:: python + + job_kwargs = dict(n_jobs=8, chunk_duration="1s") + recording_sharded = recording.save(folder="recording", format="zarr", + shard_factor=20, **job_kwargs) Parallel processing and job_kwargs diff --git a/pyproject.toml b/pyproject.toml index aba26a0e5e..4265be1622 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,8 +24,7 @@ dependencies = [ "numpy>=2.3.2;python_version>='3.14'", "threadpoolctl>=3.0.0", "tqdm", - "zarr>=2.18,<3;python_version<'3.14'", - "zarr>=3;python_version>='3.14'", # Zarr writing is not yet supported with zarr 3 + "zarr>=3,<4", # for release we need pypi, so this needs to be commented "neo @ git+https://github.com/NeuralEnsemble/python-neo.git", "probeinterface @ git+https://github.com/SpikeInterface/probeinterface.git", @@ -33,8 +32,6 @@ dependencies = [ # "probeinterface>=0.4.0", "packaging", "pydantic", - "numcodecs<0.16.0;python_version<'3.14'", # For supporting zarr < 3 - "numcodecs>=0.16.5;python_version>='3.14'", ] [build-system] @@ -69,7 +66,7 @@ changelog = "https://spikeinterface.readthedocs.io/en/latest/whatisnew.html" extractors = [ "MEArec>=1.8", "pynwb>=2.6.0", - "hdmf-zarr>=0.11.0", + "hdmf-zarr>=0.14.0; python_version>='3.12'", "pyedflib>=0.1.30", "lxml", # lxml for neuroscope "scipy", @@ -85,9 +82,9 @@ streaming_extractors = [ "fsspec", "aiohttp", "requests", - "hdmf-zarr>=0.11.0", + "hdmf-zarr>=0.14.0; python_version>='3.12'", "remfile", - "s3fs" + "s3fs>=2025.7.0" ] @@ -367,7 +364,6 @@ markers = [ "widgets", "sortingcomponents", "streaming_extractors: extractors that require streaming such as ross and fsspec", - "requires_zarr_write: tests that write to zarr, skipped with zarr>=3 where writing is not supported yet", ] filterwarnings =[ 'ignore:.*distutils Version classes are deprecated.*:DeprecationWarning', diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index 1e525885e3..dbecd82bbe 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -874,7 +874,6 @@ def save_to_zarr( folder=None, overwrite=False, storage_options=None, - channel_chunk_size=None, verbose=True, **save_kwargs, ): @@ -895,76 +894,6 @@ def save_to_zarr( # we keep the default format for recording and sorting like in old version return self.save(folder=folder, format="zarr", verbose=verbose, **save_kwargs) - # """ - # Save extractor to zarr. - - # Parameters - # ---------- - # name: str or None, default: None - # Name of the subfolder in get_global_tmp_folder() - # If "name" is given, "folder" must be None - # folder: str, Path, or None, default: None - # The folder used to save the zarr output. If the folder does not have a ".zarr" suffix, - # it will be automatically appended - # overwrite: bool, default: False - # If True, the folder is removed if it already exists - # storage_options: dict or None, default: None - # Storage options for zarr `store`. E.g., if "s3://" or "gcs://" they can provide authentication methods, etc. - # For cloud storage locations, this should not be None (in case of default values, use an empty dict) - # channel_chunk_size: int or None, default: None - # Channels per chunk (only for BaseRecording) - # compressor: numcodecs.Codec or None, default: None - # Global compressor. If None, Blosc-zstd, level 5, with bit shuffle is used - # filters: list[numcodecs.Codec] or None, default: None - # Global filters for zarr (global) - # compressor_by_dataset: dict or None, default: None - # Optional compressor per dataset: - # - traces - # - times - # If None, the global compressor is used - # filters_by_dataset: dict or None, default: None - # Optional filters per dataset: - # - traces - # - times - # If None, the global filters are used - # verbose: bool, default: True - # If True, the output is verbose - # auto_cast_uint: bool, default: True - # If True, unsigned integers are cast to signed integers to avoid issues with zarr (only for BaseRecording) - - # Returns - # ------- - # cached: ZarrExtractor - # Saved copy of the extractor. - # """ - # from .zarrextractors import read_zarr - - # save_kwargs.pop("format", None) - - # if folder is None: - # cache_folder = get_global_tmp_folder() - # if name is None: - # name = "".join(random.choices(string.ascii_uppercase + string.digits, k=8)) - # zarr_path = (cache_folder / name).with_suffix(".zarr") - # if verbose: - # print(f"Saving to zarr_path={zarr_path}") - # else: - # if storage_options is None: # save locally (not cloud storage) - # folder = clean_zarr_folder_name(folder) - # if folder.is_dir() and overwrite: - # shutil.rmtree(folder) - # zarr_path = folder - - # if not is_path_remote(zarr_path): - # assert not zarr_path.exists(), f"Path {zarr_path} already exists, choose another name" - # save_kwargs["zarr_path"] = zarr_path - # save_kwargs["storage_options"] = storage_options - # save_kwargs["channel_chunk_size"] = channel_chunk_size - # cached = self._save(format="zarr", verbose=verbose, **save_kwargs) - # cached = read_zarr(zarr_path) - - # return cached - def _load_extractor_from_dict(dic) -> "BaseExtractor": """ diff --git a/src/spikeinterface/core/baserecording.py b/src/spikeinterface/core/baserecording.py index 772c026ccc..ed7c37dae0 100644 --- a/src/spikeinterface/core/baserecording.py +++ b/src/spikeinterface/core/baserecording.py @@ -5,10 +5,11 @@ import numpy as np from probeinterface import read_probeinterface, write_probeinterface +from .base import BaseExtractor from .time_series import TimeSeriesSegment, TimeSeries from .baserecordingsnippets import BaseRecordingSnippets from .core_tools import convert_bytes_to_str, convert_seconds_to_str -from .job_tools import split_job_kwargs +from .job_tools import split_job_kwargs, _shared_job_kwargs_doc class BaseRecording(BaseRecordingSnippets, TimeSeries): @@ -346,9 +347,23 @@ def save(self, format="binary", verbose: bool = False, **save_kwargs): For cloud storage locations, this should not be None (in case of default values, use an empty dict) - channel_chunk_size: int or None, default: None Channels per chunk (only for BaseRecording) - - compressor: numcodecs.Codec or None, default: None - Global compressor. If None, Blosc-zstd, level 5, with bit shuffle is used - - filters: list[numcodecs.Codec] or None, default: None + - chunks: tuple | None, default: None + # TOOD: modify this + Chunks for the traces dataset. If None, chunking is applied to the time dimension only and it is + determined by the job_kwargs chunks ("chunk_size" or "chunk_duration"). + If `chunks` is not None, it needs to be a tuple of length 2 with the chunk size for the time and channel + dimensions respectively and `channel_chunk_size` should not be specified. + - shard_factor: int | tuple[int] | None, default: None + If integer, the shard size will be set to chunk_size * shard_factor in the first dimension (time). + If tuple, the shard_factor to be applied to each dimension. Note that `shard_factor` cannot + be specified together with `shards`. + - shards: tuple | None, default: None + Number of shard size. If None, no sharding is done. Note that shards dimensions need to be larger than + chunk dimensions (if chunks is not None) and that sharding is only done on the first dimension. + - compressors: BytesBytesCodec | ArrayBytesCodec | list of them, or None, default: None + Global compressors. Only one ArrayBytesCodec (serializer) is supported. + If None, Blosc-zstd, level 5, with bit shuffle is used. + - filters: ArrayArrayCodec | list[ArrayArrayCodec] or None, default: None Global filters for zarr (global) - compressor_by_dataset: dict or None, default: None Optional compressor per dataset: @@ -364,6 +379,7 @@ def save(self, format="binary", verbose: bool = False, **save_kwargs): - times If None, the global filters are used + * "memory" format: - sharedmem : bool, default: True If True, the recording is saved in shared memory. If False, it is saved as diff --git a/src/spikeinterface/core/basesorting.py b/src/spikeinterface/core/basesorting.py index fdd15e08e6..84257592ef 100644 --- a/src/spikeinterface/core/basesorting.py +++ b/src/spikeinterface/core/basesorting.py @@ -590,8 +590,17 @@ def save(self, format="numpy_folder", **save_kwargs): - storage_options: dict or None, default: None Storage options for zarr `store`. E.g., if "s3://" or "gcs://" they can provide authentication methods, etc. For cloud storage locations, this should not be None (in case of default values, use an empty dict) - - compressor: numcodecs.Codec or None, default: None - Global compressor. If None, Blosc-zstd, level 5, with bit shuffle is used + - target_chunk_size_bytes: int or None, default: 10 * 1024 * 1024 (10 MB) + The target chunk size for each spike field in the zarr arrays. + If explicitly set to None, no chunking is used. + - compressors: zarr compressors or str or None, default: "default" + The compressor used for each spike field in the zarr arrays. + If "default", the SpikeInterface default zarr compressor is used (Blosc-zstd, level 5, bitshuffle). + - filters: list of zarr filters or None, default: None + The list of zarr filters to apply to each spike field in the zarr arrays. + If None, a Delta filter is applied. + - shard_factor: int or None, default: None + If given, a shard will be shard_factor * chunk size. * "memory" format: - sharedmem : bool, default: True If True, the recording is saved in shared memory. If False, it is saved as diff --git a/src/spikeinterface/core/core_tools.py b/src/spikeinterface/core/core_tools.py index 58d53dca91..519771144c 100644 --- a/src/spikeinterface/core/core_tools.py +++ b/src/spikeinterface/core/core_tools.py @@ -763,27 +763,6 @@ def measure_memory_allocation(measure_in_process: bool = True) -> float: return memory -def _is_zarr_write_supported() -> bool: - """ - Whether writing to zarr is supported with the installed zarr, which is not yet the case for zarr>=3. - """ - return int(zarr.__version__.split(".")[0]) < 3 - - -def _check_zarr_write_is_supported() -> None: - """ - Raise an informative error when writing to zarr with zarr>=3, which is not supported yet. - - zarr>=3 is what gets installed on Python 3.14, where reading existing zarr folders works but writing does not. - """ - if not _is_zarr_write_supported(): - raise NotImplementedError( - f"Writing to zarr is not supported yet with zarr {zarr.__version__}, which is the version installed " - "on Python 3.14. Use Python 3.13 or lower to save in zarr format, or save in another format such as " - "'binary_folder'." - ) - - def is_path_remote(path: str | Path) -> bool: """ Returns True if the path is a remote path (e.g., s3:// or gcs://). diff --git a/src/spikeinterface/core/loading.py b/src/spikeinterface/core/loading.py index 9c06e95300..8cc679d95e 100644 --- a/src/spikeinterface/core/loading.py +++ b/src/spikeinterface/core/loading.py @@ -270,11 +270,15 @@ def _guess_object_from_zarr(zarr_folder): return _guess_object_from_dict(spikeinterface_info) # here it is the old fashion and a bit ambiguous - if "templates_array" in zarr_root.keys(): + # get_zarr_group_keys is used to be compatible with both zarr v2 and zarr v3 groups + from .zarr_tools import get_zarr_group_keys + + root_keys = get_zarr_group_keys(zarr_root) + if "templates_array" in root_keys: return "Templates" - elif "channel_ids" in zarr_root.keys() and "unit_ids" not in zarr_root.keys(): + elif "channel_ids" in root_keys and "unit_ids" not in root_keys: return "Recording" - elif "unit_ids" in zarr_root.keys() and "channel_ids" not in zarr_root.keys(): + elif "unit_ids" in root_keys and "channel_ids" not in root_keys: return "Sorting" diff --git a/src/spikeinterface/core/node_pipeline.py b/src/spikeinterface/core/node_pipeline.py index c66d68a2e4..56f43cf3bf 100644 --- a/src/spikeinterface/core/node_pipeline.py +++ b/src/spikeinterface/core/node_pipeline.py @@ -12,7 +12,23 @@ from spikeinterface.core import BaseRecording, get_chunk_with_margin from spikeinterface.core.job_tools import TimeSeriesChunkExecutor, fix_job_kwargs, _shared_job_kwargs_doc from spikeinterface.core import get_channel_distances -from spikeinterface.core.core_tools import ms_to_samples, samples_to_ms, _check_zarr_write_is_supported +from spikeinterface.core.core_tools import ms_to_samples, samples_to_ms + +# Ignore Zarr consolidated metadata warning +warnings.filterwarnings( + "ignore", + message="Consolidated metadata is currently not part", + category=UserWarning, +) + +compressors_type = ( + zarr.abc.codec.BytesBytesCodec + | zarr.abc.codec.ArrayBytesCodec + | list[zarr.abc.codec.BytesBytesCodec | zarr.abc.codec.ArrayBytesCodec] + | str + | None +) +filters_type = zarr.abc.codec.ArrayArrayCodec | list[zarr.abc.codec.ArrayArrayCodec] | None class PipelineNode: @@ -1076,10 +1092,13 @@ class GatherToZarr: Note that in both modes the `dest` must contain a ".zarr" suffix. names : list of str | None Names of the outputs. Can be None when `dest` is a list of explicit destinations. - compressor : numcodecs codec | "default" | None, default: "default" - The compressor used for every array. If "default", the SpikeInterface default + compressors : zarr.abc.codec.BytesBytesCodec | zarr.abc.codec.ArrayBytesCodec | list of them | str, or None, default: "default" + The compressors used for every array. If "default", the SpikeInterface default zarr compressor is used (Blosc-zstd, level 5, bitshuffle). If None, no compression. Ignored for destinations that are already a ``zarr.Array`` (they keep their own compressor). + filters : zarr.abc.codec.ArrayArrayCodec | list[zarr.abc.codec.ArrayArrayCodec] or None, default: None + The filters used for every array. If None, no filters are applied. + Ignored for destinations that are already a ``zarr.Array`` (they keep their own filters). zarr_target_chunk_bytes : int | dict[str, int], default: 10485760 (10 MiB) Target (uncompressed) size in bytes of one zarr chunk, used to compute the number of rows per chunk. An int applies the same target to every array, a dict sets it per array @@ -1091,17 +1110,16 @@ def __init__( self, dest: str | Path | list[str | Path | zarr.Array] | None = None, names: list[str] | None = None, - compressor: "numcodecs.abc.Codec | str | None" = "default", + compressors: compressors_type = "default", + filters: filters_type = None, zarr_target_chunk_bytes: int | dict[str, int] = 10 * 1024 * 1024, ): - import zarr - from spikeinterface.core.zarrextractors import get_default_zarr_compressor + from spikeinterface.core.zarr_tools import build_codec_pipeline - _check_zarr_write_is_supported() - if compressor == "default": - compressor = get_default_zarr_compressor() - self.compressor = compressor + if compressors == "default": + compressors = get_default_zarr_compressor() + self.codec_kwargs = build_codec_pipeline(filters=filters, compressors=compressors) if isinstance(dest, (list, tuple)): # append to explicit destinations (paths inside a store or pre-created zarr.Arrays) @@ -1183,13 +1201,13 @@ def __call__(self, res): # pick the number of rows per chunk to target ~zarr_target_chunk_bytes per chunk row_nbytes = int(np.prod(trailing_shape, dtype="int64")) * buf.dtype.itemsize chunk0 = max(1, self.zarr_target_chunk_bytes[name] // max(1, row_nbytes)) - self.arrays[i_name] = root.create_dataset( + self.arrays[i_name] = root.create_array( name=internal_path, shape=(0,) + trailing_shape, chunks=(chunk0,) + trailing_shape, dtype=buf.dtype, - compressor=self.compressor, overwrite=True, + **self.codec_kwargs, ) self.arrays[i_name].append(buf, axis=0) diff --git a/src/spikeinterface/core/sortinganalyzer.py b/src/spikeinterface/core/sortinganalyzer.py index bd03f0b55e..34309c5b29 100644 --- a/src/spikeinterface/core/sortinganalyzer.py +++ b/src/spikeinterface/core/sortinganalyzer.py @@ -27,7 +27,6 @@ retrieve_importing_provenance, is_path_remote, clean_zarr_folder_name, - _check_zarr_write_is_supported, ) from .sorting_tools import ( generate_unit_ids_for_merge_group, @@ -41,9 +40,22 @@ from .sparsity import ChannelSparsity, estimate_sparsity from .sortingfolder import NumpyFolderSorting from .zarrextractors import get_default_zarr_compressor, ZarrSortingExtractor, super_zarr_open -from .zarr_tools import write_json_array, write_pickle_array, read_encoded_array +from .zarr_tools import ( + iterate_zarr_group, + get_zarr_attr_or_legacy_object, + write_json_array, + write_pickle_array, + read_encoded_array, +) from .node_pipeline import run_node_pipeline +# Ignore Zarr consolidated metadata warning +warnings.filterwarnings( + "ignore", + message="Consolidated metadata is currently not part", + category=UserWarning, +) + # Typing hints PeakSignType = Literal["both", "neg", "pos"] PeakModeType = Literal["extremum", "at_index", "peak_to_peak"] @@ -1015,10 +1027,9 @@ def load_from_binary_folder( def _get_zarr_root(self, mode="r+"): assert mode in ("r+", "a", "r"), "mode must be 'r+', 'a' or 'r'" - if mode != "r": - _check_zarr_write_is_supported() storage_options = self._backend_options.get("storage_options", {}) + zarr_root = super_zarr_open(self.folder, mode=mode, storage_options=storage_options) return zarr_root @@ -1038,7 +1049,6 @@ def create_zarr( ) -> "SortingAnalyzer": from .zarrextractors import add_sorting_to_zarr_group, ZarrSortingExtractor - _check_zarr_write_is_supported() is_remote = is_path_remote(folder) if not is_remote: folder = clean_zarr_folder_name(folder) @@ -1050,8 +1060,13 @@ def create_zarr( storage_options = backend_options.get("storage_options", {}) saving_options = backend_options.get("saving_options", {}) + if not is_remote: + storage_options_kwargs = {} + else: + storage_options_kwargs = {"storage_options": storage_options} + # Create zarr root group (and subgroups) - zarr_root = zarr.open(folder, mode="w", storage_options=storage_options) + zarr_root = zarr.open(folder, mode="w", **storage_options_kwargs) sorting_group = zarr_root.create_group("sorting") # for sorting output recording_info_group = zarr_root.create_group("recording_info") # rec_attributes and probe group zarr_root.create_group("extensions") # used later @@ -1071,14 +1086,14 @@ def create_zarr( # Save sparsity if sparsity is not None: - zarr_root.create_dataset("sparsity_mask", data=sparsity.mask, **saving_options) + zarr_root.create_array("sparsity_mask", data=sparsity.mask, **saving_options) # Dump recording provenance if recording is not None: rec_dict = recording.to_dict(relative_to=relative_to, recursive=True) if recording.check_serializability("json"): - compressor = saving_options.get("compressor", get_default_zarr_compressor()) - write_json_array(zarr_root, "recording", check_json(rec_dict), compressor=compressor) + compressors = saving_options.get("compressors", get_default_zarr_compressor()) + write_json_array(zarr_root, "recording", check_json(rec_dict), compressors=compressors) else: warnings.warn( "The recording is not JSON-serializable, so it is not saved in the zarr SortingAnalyzer, which " @@ -1137,6 +1152,10 @@ def load_from_zarr( backend_options = {} if backend_options is None else backend_options storage_options = backend_options.get("storage_options", {}) + if not is_path_remote(str(folder)): + storage_options_kwargs = {} + else: + storage_options_kwargs = storage_options # Open root group zarr_root = super_zarr_open(str(folder), mode="r", storage_options=storage_options) @@ -1147,7 +1166,7 @@ def load_from_zarr( si_info = zarr_root.attrs["spikeinterface_info"] if parse(si_info["version"]) < parse("0.101.1"): try: - zarr_root_a = zarr.open(str(folder), mode="a", storage_options=storage_options) + zarr_root_a = zarr.open(str(folder), mode="a", **storage_options_kwargs) zarr.consolidate_metadata(zarr_root_a.store) except: warnings.warn( @@ -1183,9 +1202,13 @@ def load_from_zarr( # Load recording (if available) if recording is None: - rec_field = zarr_root.get("recording") - if rec_field is not None: - rec_dict = read_encoded_array(rec_field) + try: + rec_field = zarr_root.get("recording") + rec_dict = read_encoded_array(rec_field) if rec_field is not None else None + except Exception: + # legacy zarr v2 object array, which zarr v3 cannot open natively + rec_dict = get_zarr_attr_or_legacy_object(zarr_root, "recording") + if rec_dict is not None: try: recording = load(rec_dict, base_folder=folder) except: @@ -1297,7 +1320,7 @@ def set_sorting_property( if key in zarr_root["sorting"]["properties"]: zarr_root["sorting"]["properties"][key][:] = prop_values else: - zarr_root["sorting"]["properties"].create_dataset(name=key, data=prop_values, compressor=None) + zarr_root["sorting"]["properties"].create_array(name=key, data=prop_values, compressors=None) # IMPORTANT: we need to re-consolidate the zarr store! zarr.consolidate_metadata(zarr_root.store) @@ -2170,12 +2193,15 @@ def get_sorting_provenance(self): elif self.format == "zarr": zarr_root = self._get_zarr_root(mode="r") sorting_provenance = None - sort_dict = zarr_root["sorting"].attrs.get("provenance", None) + # In zarr v3, sorting_provenance is stored in attributes (in zarr v2 it was an object array) + sort_dict = zarr_root["sorting"].attrs.get("provenance") if sort_dict is None: # Legacy - if "sorting_provenance" in zarr_root.keys(): - sort_dict = zarr_root["sorting_provenance"][0] + sort_dict = get_zarr_attr_or_legacy_object(zarr_root, "sorting_provenance") if sort_dict is not None: + # try-except here is because it's not required to be able + # to load the sorting provenance, as the user might have deleted + # the original sorting folder try: sorting_provenance = load(sort_dict, base_folder=self.folder) except: @@ -2621,10 +2647,16 @@ def get_saved_extension_names(self): elif self.format == "zarr": zarr_root = self._get_zarr_root(mode="r") - if "extensions" in zarr_root.keys(): + # Avoid iterating zarr_root.keys() because legacy v2 stores may contain + # object-dtype arrays (e.g. "recording", "sorting_provenance") that zarr v3 + # cannot parse, causing ValueError on enumeration. + try: extension_group = zarr_root["extensions"] - for extension_name in extension_group.keys(): - if "params" in extension_group[extension_name].attrs.keys(): + except KeyError: + extension_group = None + if extension_group is not None: + for extension_name, extension in iterate_zarr_group(extension_group): + if "params" in extension.attrs.keys(): saved_extension_names.append(extension_name) else: @@ -3367,8 +3399,7 @@ def load_data(self, lazy=False): self.set_data(ext_data_name, ext_data) elif self.format == "zarr": extension_group = self._get_zarr_extension_group(mode="r") - for ext_data_name in extension_group.keys(): - ext_data_ = extension_group[ext_data_name] + for ext_data_name, ext_data_ in iterate_zarr_group(extension_group): if "encoding" in ext_data_.attrs: if lazy and ext_data_.attrs["encoding"] == "pickle": continue @@ -3381,9 +3412,9 @@ def load_data(self, lazy=False): index = ext_data_["index"] ext_data = pd.DataFrame(index=index) - for col in ext_data_.keys(): + for col, col_data in iterate_zarr_group(ext_data_): if col != "index": - ext_data.loc[:, col] = ext_data_[col][:] + ext_data.loc[:, col] = col_data[:] ext_data = ext_data.convert_dtypes() elif "object" in ext_data_.attrs: # legacy pickle object array (spikeinterface<=0.105) @@ -3488,9 +3519,10 @@ def run(self, save=True, **kwargs): self._save_data() if self.format == "zarr": - zarr.consolidate_metadata(self.sorting_analyzer._get_zarr_root().store) + zarr.consolidate_metadata(self.sorting_analyzer._get_zarr_root(mode="r+").store) def save(self): + self._reset_extension_folder() self._save_params() self._save_importing_provenance() self._save_run_info() @@ -3498,7 +3530,7 @@ def save(self): if self.format == "zarr": - zarr.consolidate_metadata(self.sorting_analyzer._get_zarr_root().store) + zarr.consolidate_metadata(self.sorting_analyzer._get_zarr_root(mode="r+").store) def _save_data(self): if self.format == "memory": @@ -3545,8 +3577,11 @@ def _save_data(self): extension_group = self._get_zarr_extension_group(mode="r+") # if compression is not externally given, we use the default - if "compressor" not in saving_options: - saving_options["compressor"] = get_default_zarr_compressor() + if "compressors" not in saving_options and "compressor" not in saving_options: + saving_options["compressors"] = get_default_zarr_compressor() + if "compressor" in saving_options: + saving_options["compressors"] = [saving_options["compressor"]] + del saving_options["compressor"] for ext_data_name, ext_data in self.data.items(): if isinstance(ext_data, zarr.Array): @@ -3557,31 +3592,31 @@ def _save_data(self): del extension_group[ext_data_name] if isinstance(ext_data, (dict, list)): write_json_array( - extension_group, ext_data_name, check_json(ext_data), compressor=saving_options["compressor"] + extension_group, ext_data_name, check_json(ext_data), compressors=saving_options["compressors"] ) elif isinstance(ext_data, np.ndarray): # only save the array if the dataset does not already exist, since it is created directly # by the run_node_pipeline() function in the case of nodepipeline extensions if ext_data_name not in extension_group: - extension_group.create_dataset(name=ext_data_name, data=ext_data, **saving_options) + extension_group.create_array(name=ext_data_name, data=ext_data, **saving_options) elif HAS_PANDAS and isinstance(ext_data, pd.DataFrame): df_group = extension_group.create_group(ext_data_name) # first we save the index indices = ext_data.index.to_numpy() if indices.dtype.kind == "O": indices = indices.astype(str) - df_group.create_dataset(name="index", data=indices) + df_group.create_array(name="index", data=indices) for col in ext_data.columns: col_data = ext_data[col].to_numpy() if col_data.dtype.kind == "O": col_data = col_data.astype(str) - df_group.create_dataset(name=col, data=col_data) + df_group.create_array(name=col, data=col_data) df_group.attrs["dataframe"] = True else: # any object try: write_pickle_array( - extension_group, ext_data_name, ext_data, compressor=saving_options["compressor"] + extension_group, ext_data_name, ext_data, compressors=saving_options["compressors"] ) except: raise Exception(f"Could not save {ext_data_name} as extension data") @@ -3678,8 +3713,6 @@ def set_params(self, save=True, **params): def _save_params(self): params_to_save = self.params.copy() - self._reset_extension_folder() - # TODO make sparsity local Result specific # if "sparsity" in params_to_save and params_to_save["sparsity"] is not None: # assert isinstance( diff --git a/src/spikeinterface/core/template.py b/src/spikeinterface/core/template.py index 0f0922e10b..a170dd4e02 100644 --- a/src/spikeinterface/core/template.py +++ b/src/spikeinterface/core/template.py @@ -321,22 +321,20 @@ def add_templates_to_zarr_group(self, zarr_group: "zarr.Group") -> None: to optimize read/write operations for individual units. """ - from .core_tools import _check_zarr_write_is_supported - - _check_zarr_write_is_supported() - # Saves one chunk per unit - arrays_chunk = (1, None, None) - zarr_group.create_dataset("templates_array", data=self.templates_array, chunks=arrays_chunk) - zarr_group.create_dataset("channel_ids", data=self.channel_ids) - zarr_group.create_dataset("unit_ids", data=self.unit_ids) + # In zarr v3, chunks must be a full tuple with actual dimensions + num_units, num_samples, num_channels = self.templates_array.shape + arrays_chunk = (1, num_samples, num_channels) + zarr_group.create_array("templates_array", data=self.templates_array, chunks=arrays_chunk) + zarr_group.create_array("channel_ids", data=self.channel_ids) + zarr_group.create_array("unit_ids", data=self.unit_ids) zarr_group.attrs["sampling_frequency"] = self.sampling_frequency zarr_group.attrs["nbefore"] = self.nbefore zarr_group.attrs["is_in_uV"] = self.is_in_uV if self.sparsity_mask is not None: - zarr_group.create_dataset("sparsity_mask", data=self.sparsity_mask) + zarr_group.create_array("sparsity_mask", data=self.sparsity_mask) if self.probe is not None: probe_group = zarr_group.create_group("probe") @@ -357,9 +355,6 @@ def to_zarr(self, folder_path: str | Path) -> None: """ import zarr - from .core_tools import _check_zarr_write_is_supported - - _check_zarr_write_is_supported() zarr_group = zarr.open_group(folder_path, mode="w") self.add_templates_to_zarr_group(zarr_group) diff --git a/src/spikeinterface/core/tests/test_analyzer_extension_core.py b/src/spikeinterface/core/tests/test_analyzer_extension_core.py index 8436d7c7a3..6423ce11ce 100644 --- a/src/spikeinterface/core/tests/test_analyzer_extension_core.py +++ b/src/spikeinterface/core/tests/test_analyzer_extension_core.py @@ -11,9 +11,8 @@ from spikeinterface.core.sortinganalyzer import _extension_children, _get_children_dependencies import numpy as np -from spikeinterface.core.core_tools import _is_zarr_write_supported -analyzer_formats = ("memory", "binary_folder", "zarr") if _is_zarr_write_supported() else ("memory", "binary_folder") +analyzer_formats = ("memory", "binary_folder", "zarr") def get_sorting_analyzer(cache_folder, format="memory", sparse=True): @@ -75,9 +74,7 @@ def _check_result_extension(sorting_analyzer, extension_name, cache_folder): # print(k, arr.shape) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize( "sparse", [ @@ -96,7 +93,7 @@ def test_ComputeRandomSpikes(format, sparse, create_cache_folder): print("Checking results") _check_result_extension(sorting_analyzer, "random_spikes", cache_folder) - print("Delering extension") + print("Deleting extension") sorting_analyzer.delete_extension("random_spikes") print("Re-computing random spikes") @@ -107,9 +104,7 @@ def test_ComputeRandomSpikes(format, sparse, create_cache_folder): _check_result_extension(sorting_analyzer, "random_spikes", cache_folder) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize("sparse", [True, False]) def test_ComputeWaveforms(format, sparse, create_cache_folder): cache_folder = create_cache_folder @@ -122,7 +117,6 @@ def test_ComputeWaveforms(format, sparse, create_cache_folder): _check_result_extension(sorting_analyzer, "waveforms", cache_folder) -@pytest.mark.requires_zarr_write @pytest.mark.parametrize("sparse", [False, True]) def test_ComputeWaveforms_consistent_across_formats(create_cache_folder, sparse): # Computing waveforms (and templates) on memory / binary_folder / zarr analyzers with the same @@ -178,9 +172,7 @@ def test_ComputeWaveforms_consistent_across_formats(create_cache_folder, sparse) assert np.array_equal(zarr_waveforms, wfs_mem) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize("sparse", [True, False]) def test_ComputeTemplates(format, sparse, create_cache_folder): cache_folder = create_cache_folder @@ -269,9 +261,7 @@ def test_ComputeTemplates(format, sparse, create_cache_folder): _check_result_extension(sorting_analyzer, "templates", cache_folder) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize("sparse", [True, False]) def test_ComputeNoiseLevels(format, sparse, create_cache_folder): cache_folder = create_cache_folder diff --git a/src/spikeinterface/core/tests/test_baserecording.py b/src/spikeinterface/core/tests/test_baserecording.py index 8fd38ce4bf..c20850a3e1 100644 --- a/src/spikeinterface/core/tests/test_baserecording.py +++ b/src/spikeinterface/core/tests/test_baserecording.py @@ -24,7 +24,6 @@ from spikeinterface.core.testing import check_recordings_equal from spikeinterface.core import generate_recording -from spikeinterface.core.core_tools import _is_zarr_write_supported def test_BaseRecording(create_cache_folder): @@ -365,34 +364,32 @@ def test_BaseRecording(create_cache_folder): rec_2d = rec_3d.planarize(axes="zy") assert np.allclose(rec_2d.get_channel_locations(), locations_3d[:, [2, 1]]) - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - # test save to zarr - compressor = get_default_zarr_compressor() - rec_zarr = rec2.save(format="zarr", folder=cache_folder / "recording", compressor=compressor) - rec_zarr_loaded = load(cache_folder / "recording.zarr") - # annotations is False because Zarr adds compression ratios - check_recordings_equal(rec2, rec_zarr, return_in_uV=False, check_annotations=False, check_properties=True) - check_recordings_equal( - rec_zarr, rec_zarr_loaded, return_in_uV=False, check_annotations=False, check_properties=True - ) - for annotation_name in rec2.get_annotation_keys(): - assert rec2.get_annotation(annotation_name) == rec_zarr.get_annotation(annotation_name) - assert rec2.get_annotation(annotation_name) == rec_zarr_loaded.get_annotation(annotation_name) - - rec_zarr2 = rec2.save( - format="zarr", folder=cache_folder / "recording_channel_chunk", compressor=compressor, channel_chunk_size=2 - ) - rec_zarr2_loaded = load(cache_folder / "recording_channel_chunk.zarr") - - # annotations is False because Zarr adds compression ratios - check_recordings_equal(rec2, rec_zarr2, return_in_uV=False, check_annotations=False, check_properties=True) - check_recordings_equal( - rec_zarr2, rec_zarr2_loaded, return_in_uV=False, check_annotations=False, check_properties=True - ) - for annotation_name in rec2.get_annotation_keys(): - assert rec2.get_annotation(annotation_name) == rec_zarr2.get_annotation(annotation_name) - assert rec2.get_annotation(annotation_name) == rec_zarr2_loaded.get_annotation(annotation_name) + # test save to zarr + compressor = get_default_zarr_compressor() + rec_zarr = rec2.save(format="zarr", folder=cache_folder / "recording", compressors=compressor) + rec_zarr_loaded = load(cache_folder / "recording.zarr") + # annotations is False because Zarr adds compression ratios + check_recordings_equal(rec2, rec_zarr, return_in_uV=False, check_annotations=False, check_properties=True) + check_recordings_equal( + rec_zarr, rec_zarr_loaded, return_in_uV=False, check_annotations=False, check_properties=True + ) + for annotation_name in rec2.get_annotation_keys(): + assert rec2.get_annotation(annotation_name) == rec_zarr.get_annotation(annotation_name) + assert rec2.get_annotation(annotation_name) == rec_zarr_loaded.get_annotation(annotation_name) + + rec_zarr2 = rec2.save( + format="zarr", folder=cache_folder / "recording_channel_chunk", compressors=compressor, channel_chunk_size=2 + ) + rec_zarr2_loaded = load(cache_folder / "recording_channel_chunk.zarr") + + # annotations is False because Zarr adds compression ratios + check_recordings_equal(rec2, rec_zarr2, return_in_uV=False, check_annotations=False, check_properties=True) + check_recordings_equal( + rec_zarr2, rec_zarr2_loaded, return_in_uV=False, check_annotations=False, check_properties=True + ) + for annotation_name in rec2.get_annotation_keys(): + assert rec2.get_annotation(annotation_name) == rec_zarr2.get_annotation(annotation_name) + assert rec2.get_annotation(annotation_name) == rec_zarr2_loaded.get_annotation(annotation_name) def test_json_pickle_equivalence(create_cache_folder): diff --git a/src/spikeinterface/core/tests/test_basesorting.py b/src/spikeinterface/core/tests/test_basesorting.py index ec7c2cefb6..09c82a9ecb 100644 --- a/src/spikeinterface/core/tests/test_basesorting.py +++ b/src/spikeinterface/core/tests/test_basesorting.py @@ -25,13 +25,37 @@ from spikeinterface.core.base import BaseExtractor, minimum_spike_dtype, unit_period_dtype from spikeinterface.core.basesorting import LEXSORT_UNIT_COMPACT from spikeinterface.core.testing import check_sorted_arrays_equal, check_sortings_equal -from spikeinterface.core.core_tools import _is_zarr_write_supported + + +def _make_sorting_with_shuffled_ties(num_units, num_segments, seed=42): + """Build a NumpySorting whose cotemporal spikes are in arbitrary unit_index order. + + A spike vector is only guaranteed to be segment-blocked and sample_index-ascending within each + segment; the unit_index order among spikes sharing a sample_index is unspecified (see #4606). + Building via `NumpySorting.from_unit_dict` happens to produce unit-ascending ties, so it + can't test the shuffled tie case. + """ + rng = np.random.default_rng(seed) + num_spikes = 2_000 + + # A sample range far smaller than num_spikes, so cotemporal spikes are abundant -- including + # repeats of the same (segment, sample, unit), the tie that np.lexsort itself cannot break. + spikes = np.empty(num_spikes, dtype=minimum_spike_dtype) + spikes["sample_index"] = rng.integers(0, 200, size=num_spikes) + spikes["unit_index"] = rng.integers(0, num_units, size=num_spikes) + spikes["segment_index"] = rng.integers(0, num_segments, size=num_spikes) + + # Order by segment then sample, breaking ties randomly rather than by unit_index. + spikes = spikes[np.lexsort((rng.random(num_spikes), spikes["sample_index"], spikes["segment_index"]))] + + sorting = NumpySorting(spikes, 30_000.0, np.arange(num_units)) + assert sorting.get_num_segments() == num_segments + return sorting def test_BaseSorting(create_cache_folder): - cache_folder = create_cache_folder num_seg = 2 - file_path = cache_folder / "test_BaseSorting.npz" + file_path = create_cache_folder / "test_BaseSorting.npz" file_path.parent.mkdir(exist_ok=True) create_sorting_npz(num_seg, file_path) @@ -58,28 +82,28 @@ def test_BaseSorting(create_cache_folder): check_sortings_equal(sorting, sorting3, check_annotations=True, check_properties=True) # dump/load json - sorting.dump_to_json(cache_folder / "test_BaseSorting.json") - sorting2 = load(cache_folder / "test_BaseSorting.json") - sorting3 = load(cache_folder / "test_BaseSorting.json") + sorting.dump_to_json(create_cache_folder / "test_BaseSorting.json") + sorting2 = load(create_cache_folder / "test_BaseSorting.json") + sorting3 = load(create_cache_folder / "test_BaseSorting.json") check_sortings_equal(sorting, sorting2, check_annotations=True, check_properties=False) check_sortings_equal(sorting, sorting3, check_annotations=True, check_properties=False) # dump/load pickle - sorting.dump_to_pickle(cache_folder / "test_BaseSorting.pkl") - sorting2 = load(cache_folder / "test_BaseSorting.pkl") - sorting3 = load(cache_folder / "test_BaseSorting.pkl") + sorting.dump_to_pickle(create_cache_folder / "test_BaseSorting.pkl") + sorting2 = load(create_cache_folder / "test_BaseSorting.pkl") + sorting3 = load(create_cache_folder / "test_BaseSorting.pkl") check_sortings_equal(sorting, sorting2, check_annotations=True, check_properties=True) check_sortings_equal(sorting, sorting3, check_annotations=True, check_properties=True) # cache old format : npz_folder - folder = cache_folder / "simple_sorting_npz_folder" + folder = create_cache_folder / "simple_sorting_npz_folder" sorting.set_property("test", np.ones(len(sorting.unit_ids))) sorting.save(folder=folder, format="npz_folder") sorting2 = load(folder) assert isinstance(sorting2, NpzFolderSorting) # cache new format : binary - folder = cache_folder / "simple_sorting_binary" + folder = create_cache_folder / "simple_sorting_binary" sorting.set_property("test", np.ones(len(sorting.unit_ids))) sorting.save(folder=folder, format="numpy_folder") sorting2 = load(folder) @@ -144,44 +168,46 @@ def test_BaseSorting(create_cache_folder): del sorting6 del sorting5 - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - # test save to zarr - # compressor = get_default_zarr_compressor() - sorting_zarr = sorting.save(format="zarr", folder=cache_folder / "sorting.zarr") - sorting_zarr_loaded = load(cache_folder / "sorting.zarr") - # annotations is False because Zarr adds compression ratios - check_sortings_equal(sorting, sorting_zarr, check_annotations=False, check_properties=True) - check_sortings_equal(sorting_zarr, sorting_zarr_loaded, check_annotations=False, check_properties=True) - for annotation_name in sorting.get_annotation_keys(): - assert sorting.get_annotation(annotation_name) == sorting_zarr.get_annotation(annotation_name) - assert sorting.get_annotation(annotation_name) == sorting_zarr_loaded.get_annotation(annotation_name) - - -def _make_sorting_with_shuffled_ties(num_units, num_segments, seed=42): - """Build a NumpySorting whose cotemporal spikes are in arbitrary unit_index order. - - A spike vector is only guaranteed to be segment-blocked and sample_index-ascending within each - segment; the unit_index order among spikes sharing a sample_index is unspecified (see #4606). - Building via `NumpySorting.from_unit_dict` happens to produce unit-ascending ties, so it - can't test the shuffled tie case. - """ - rng = np.random.default_rng(seed) - num_spikes = 2_000 - - # A sample range far smaller than num_spikes, so cotemporal spikes are abundant -- including - # repeats of the same (segment, sample, unit), the tie that np.lexsort itself cannot break. - spikes = np.empty(num_spikes, dtype=minimum_spike_dtype) - spikes["sample_index"] = rng.integers(0, 200, size=num_spikes) - spikes["unit_index"] = rng.integers(0, num_units, size=num_spikes) - spikes["segment_index"] = rng.integers(0, num_segments, size=num_spikes) - - # Order by segment then sample, breaking ties randomly rather than by unit_index. - spikes = spikes[np.lexsort((rng.random(num_spikes), spikes["sample_index"], spikes["segment_index"]))] - - sorting = NumpySorting(spikes, 30_000.0, np.arange(num_units)) - assert sorting.get_num_segments() == num_segments - return sorting + # test save to zarr + # compressor = get_default_zarr_compressor() + sorting_zarr = sorting.save(format="zarr", folder=create_cache_folder / "sorting.zarr") + sorting_zarr_loaded = load(create_cache_folder / "sorting.zarr") + # annotations is False because Zarr adds compression ratios + check_sortings_equal(sorting, sorting_zarr, check_annotations=False, check_properties=True) + check_sortings_equal(sorting_zarr, sorting_zarr_loaded, check_annotations=False, check_properties=True) + for annotation_name in sorting.get_annotation_keys(): + assert sorting.get_annotation(annotation_name) == sorting_zarr.get_annotation(annotation_name) + assert sorting.get_annotation(annotation_name) == sorting_zarr_loaded.get_annotation(annotation_name) + + +def test_zarr_save_with_sharding(create_cache_folder): + sorting = generate_sorting(durations=[600, 600]) + # 1 MB target chunk size for Zarr + zarr_target_bytes = 1 * 1024 * 1024 + shard_factor = 3 + sorting_zarr = sorting.save( + format="zarr", + folder=create_cache_folder / "sorting_sharded.zarr", + target_chunk_size_bytes=zarr_target_bytes, + shard_factor=shard_factor, + ) + sorting_zarr_loaded = load(create_cache_folder / "sorting_sharded.zarr") + check_sortings_equal(sorting, sorting_zarr, check_annotations=False, check_properties=True) + check_sortings_equal(sorting_zarr, sorting_zarr_loaded, check_annotations=False, check_properties=True) + + # check that chunks and shards are correctly set + spikes_group = sorting_zarr._root["spikes"] + for field in spikes_group: + if field not in ("segment_slices", "sample_index_chunk_firsts"): + array = spikes_group[field] + assert array.chunks is not None + assert array.shards is not None + assert array.shards[0] == array.chunks[0] * shard_factor + assert array.chunks[0] * array.dtype.itemsize <= zarr_target_bytes + print(f"Field: {field}, Chunks: {array.chunks}, Shards: {array.shards}") + print( + f"Field: {field}, Chunk size in bytes: {array.chunks[0] * array.dtype.itemsize} - Target: {zarr_target_bytes}" + ) @pytest.mark.parametrize("use_numba", [True, False], ids=["numba", "numpy"]) @@ -418,9 +444,9 @@ def test_select_periods(): from pathlib import Path with tempfile.TemporaryDirectory() as tmpdirname: - cache_folder = Path(tmpdirname) + create_cache_folder = Path(tmpdirname) - test_BaseSorting(cache_folder) + test_BaseSorting(create_cache_folder) test_npy_sorting() test_empty_sorting() test_select_periods() diff --git a/src/spikeinterface/core/tests/test_loading.py b/src/spikeinterface/core/tests/test_loading.py index 364c5bd910..8d57a3412f 100644 --- a/src/spikeinterface/core/tests/test_loading.py +++ b/src/spikeinterface/core/tests/test_loading.py @@ -90,7 +90,7 @@ def generate_motion_object(): return motion -@pytest.mark.parametrize("output_format", ["binary", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) +@pytest.mark.parametrize("output_format", ["binary", "zarr"]) def test_load_binary_recording(generate_recording_sorting, tmp_path, output_format): rec, _ = generate_recording_sorting _ = rec.save(folder=tmp_path / "test_recording", format=output_format, overwrite=True) @@ -103,7 +103,7 @@ def test_load_binary_recording(generate_recording_sorting, tmp_path, output_form check_recordings_equal(rec, rec_loaded) -@pytest.mark.parametrize("output_format", ["numpy_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) +@pytest.mark.parametrize("output_format", ["numpy_folder", "zarr"]) def test_load_binary_sorting(generate_recording_sorting, tmp_path, output_format): _, sort = generate_recording_sorting _ = sort.save(folder=tmp_path / "test_sorting", format=output_format, overwrite=True) @@ -141,9 +141,7 @@ def test_load_ext_extractors(generate_recording_sorting, tmp_path, extension): check_sortings_equal(sort, sort_loaded, check_properties=False) -@pytest.mark.parametrize( - "output_format", ["binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("output_format", ["binary_folder", "zarr"]) def test_load_sorting_analyzer(generate_sorting_analyzer, tmp_path, output_format): analyzer = generate_sorting_analyzer _ = analyzer.save_as(folder=tmp_path / "analyzer", format=output_format) @@ -160,7 +158,6 @@ def test_load_sorting_analyzer(generate_sorting_analyzer, tmp_path, output_forma assert ext in analyzer_loaded.extensions -@pytest.mark.requires_zarr_write def test_load_templates(tmp_path, generate_templates_object): templates = generate_templates_object templates_dict = templates.to_dict() diff --git a/src/spikeinterface/core/tests/test_node_pipeline.py b/src/spikeinterface/core/tests/test_node_pipeline.py index 533966d703..a721fc3367 100644 --- a/src/spikeinterface/core/tests/test_node_pipeline.py +++ b/src/spikeinterface/core/tests/test_node_pipeline.py @@ -16,7 +16,6 @@ ExtractDenseWaveforms, sorting_to_peaks, ) -from spikeinterface.core.core_tools import _is_zarr_write_supported class AmplitudeExtractionNode(PipelineNode): @@ -181,36 +180,34 @@ def test_run_node_pipeline(cache_folder_creation): assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms2) assert np.array_equal(denoised_waveforms_rms2, denoised_waveforms_rms3) - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - # gather zarr mode - import zarr - - zarr_folder = cache_folder / f"pipeline_folder_{loop}.zarr" - if zarr_folder.is_dir(): - shutil.rmtree(zarr_folder) - output = run_node_pipeline( - recording, - nodes, - job_kwargs, - gather_mode="zarr", - dest=zarr_folder, - names=["amplitudes", "waveforms_rms", "denoised_waveforms_rms"], - ) - amplitudes_z, waveforms_rms_z, denoised_waveforms_rms_z = output - - # values must match the memory gather - assert np.array_equal(amplitudes, amplitudes_z[:]) - assert np.array_equal(waveforms_rms, waveforms_rms_z[:]) - assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms_z[:]) - - # arrays must be persisted on disk and re-openable - zarr_root = zarr.open(str(zarr_folder), mode="r") - for name in ("amplitudes", "waveforms_rms", "denoised_waveforms_rms"): - assert name in zarr_root - assert np.array_equal(amplitudes, zarr_root["amplitudes"][:]) - assert np.array_equal(waveforms_rms, zarr_root["waveforms_rms"][:]) - assert np.array_equal(denoised_waveforms_rms, zarr_root["denoised_waveforms_rms"][:]) + # gather zarr mode + import zarr + + zarr_folder = cache_folder / f"pipeline_folder_{loop}.zarr" + if zarr_folder.is_dir(): + shutil.rmtree(zarr_folder) + output = run_node_pipeline( + recording, + nodes, + job_kwargs, + gather_mode="zarr", + dest=zarr_folder, + names=["amplitudes", "waveforms_rms", "denoised_waveforms_rms"], + ) + amplitudes_z, waveforms_rms_z, denoised_waveforms_rms_z = output + + # values must match the memory gather + assert np.array_equal(amplitudes, amplitudes_z[:]) + assert np.array_equal(waveforms_rms, waveforms_rms_z[:]) + assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms_z[:]) + + # arrays must be persisted on disk and re-openable + zarr_root = zarr.open(str(zarr_folder), mode="r") + for name in ("amplitudes", "waveforms_rms", "denoised_waveforms_rms"): + assert name in zarr_root + assert np.array_equal(amplitudes, zarr_root["amplitudes"][:]) + assert np.array_equal(waveforms_rms, zarr_root["waveforms_rms"][:]) + assert np.array_equal(denoised_waveforms_rms, zarr_root["denoised_waveforms_rms"][:]) # gather npy mode with an explicit list of file paths (final location) npy_files_folder = cache_folder / f"pipeline_npy_files_{loop}" @@ -235,38 +232,36 @@ def test_run_node_pipeline(cache_folder_creation): assert np.array_equal(waveforms_rms, waveforms_rms_f) assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms_f) - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - # gather zarr mode with an explicit list of dataset paths, created on the fly - # inside an existing store (final location, e.g. an analyzer extension group) - datasets_store = cache_folder / f"pipeline_zarr_datasets_{loop}.zarr" - if datasets_store.is_dir(): - shutil.rmtree(datasets_store) - # pre-existing store that must not be wiped - root = zarr.open(str(datasets_store), mode="w") - root.attrs["preexisting"] = True - dataset_paths = [ - datasets_store / "extensions" / "amplitudes", - datasets_store / "extensions" / "waveforms_rms", - datasets_store / "extensions" / "denoised_waveforms_rms", - ] - output = run_node_pipeline( - recording, - nodes, - job_kwargs, - gather_mode="zarr", - dest=dataset_paths, - ) - amplitudes_d, waveforms_rms_d, denoised_waveforms_rms_d = output - assert np.array_equal(amplitudes, amplitudes_d[:]) - assert np.array_equal(waveforms_rms, waveforms_rms_d[:]) - assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms_d[:]) - # data must be persisted at the passed final location and the store not wiped - root_reopen = zarr.open(str(datasets_store), mode="r") - assert root_reopen.attrs.get("preexisting", False) - assert np.array_equal(amplitudes, root_reopen["extensions"]["amplitudes"][:]) - assert np.array_equal(waveforms_rms, root_reopen["extensions"]["waveforms_rms"][:]) - assert np.array_equal(denoised_waveforms_rms, root_reopen["extensions"]["denoised_waveforms_rms"][:]) + # gather zarr mode with an explicit list of dataset paths, created on the fly + # inside an existing store (final location, e.g. an analyzer extension group) + datasets_store = cache_folder / f"pipeline_zarr_datasets_{loop}.zarr" + if datasets_store.is_dir(): + shutil.rmtree(datasets_store) + # pre-existing store that must not be wiped + root = zarr.open(str(datasets_store), mode="w") + root.attrs["preexisting"] = True + dataset_paths = [ + datasets_store / "extensions" / "amplitudes", + datasets_store / "extensions" / "waveforms_rms", + datasets_store / "extensions" / "denoised_waveforms_rms", + ] + output = run_node_pipeline( + recording, + nodes, + job_kwargs, + gather_mode="zarr", + dest=dataset_paths, + ) + amplitudes_d, waveforms_rms_d, denoised_waveforms_rms_d = output + assert np.array_equal(amplitudes, amplitudes_d[:]) + assert np.array_equal(waveforms_rms, waveforms_rms_d[:]) + assert np.array_equal(denoised_waveforms_rms, denoised_waveforms_rms_d[:]) + # data must be persisted at the passed final location and the store not wiped + root_reopen = zarr.open(str(datasets_store), mode="r") + assert root_reopen.attrs.get("preexisting", False) + assert np.array_equal(amplitudes, root_reopen["extensions"]["amplitudes"][:]) + assert np.array_equal(waveforms_rms, root_reopen["extensions"]["waveforms_rms"][:]) + assert np.array_equal(denoised_waveforms_rms, root_reopen["extensions"]["denoised_waveforms_rms"][:]) # Test pickle mechanism for node in nodes: @@ -276,7 +271,6 @@ def test_run_node_pipeline(cache_folder_creation): unpickled_node = pickle.loads(pickled_node) -@pytest.mark.requires_zarr_write def test_gather_to_zarr_chunking(tmp_path): # the zarr chunk size along the first axis must be picked from a byte target (not from the # size of the first gathered buffer), so it stays sensible for billions of spikes and never @@ -325,7 +319,6 @@ def test_gather_to_zarr_chunking(tmp_path): assert np.array_equal(waveforms[:], waveforms2[:]) -@pytest.mark.requires_zarr_write def test_gather_to_zarr_chunk_bytes_per_name(tmp_path): # `zarr_target_chunk_bytes` can also be a dict to use a different byte target per array recording, sorting = generate_ground_truth_recording(num_channels=8, num_units=5, durations=[20.0], seed=7) diff --git a/src/spikeinterface/core/tests/test_sortinganalyzer.py b/src/spikeinterface/core/tests/test_sortinganalyzer.py index 527e053184..9372210400 100644 --- a/src/spikeinterface/core/tests/test_sortinganalyzer.py +++ b/src/spikeinterface/core/tests/test_sortinganalyzer.py @@ -20,15 +20,23 @@ AnalyzerExtension, _sort_extensions_by_dependency, ) +from spikeinterface.core.zarr_tools import check_compressors_match from spikeinterface.core.analyzer_extension_core import BaseSpikeVectorExtension from spikeinterface.core.base import minimum_spike_dtype # to test basespikevectorextension with node pipeline from spikeinterface.core.node_pipeline import SpikeRetriever from spikeinterface.core.tests.test_node_pipeline import AmplitudeExtractionNode -from spikeinterface.core.core_tools import _is_zarr_write_supported -analyzer_formats = ("memory", "binary_folder", "zarr") if _is_zarr_write_supported() else ("memory", "binary_folder") +# Ignore Zarr consolidated metadata warning +warnings.filterwarnings( + "ignore", + message="Consolidated metadata is currently not part", + category=UserWarning, +) + + +analyzer_formats = ("memory", "binary_folder", "zarr") def get_dataset(): @@ -95,6 +103,26 @@ def test_SortingAnalyzer_memory(tmp_path, dataset): _check_sorting_analyzers(sorting_analyzer, sorting2, cache_folder=tmp_path) +@pytest.mark.parametrize("format", ["binary_folder", "zarr"]) +def test_provenance_saved(tmp_path, dataset, format): + recording, sorting = dataset + recording = recording.save(folder=tmp_path / "recording_binary") + sorting = sorting.save(folder=tmp_path / "sorting_npz") + folder = tmp_path / f"analyzer_{format}" if format == "binary_folder" else tmp_path / "analyzer.zarr" + create_sorting_analyzer(sorting, recording, format=format, folder=folder, sparse=False) + + loaded = load_sorting_analyzer(folder) + assert loaded.has_recording() + assert np.array_equal(loaded.recording.channel_ids, recording.channel_ids) + assert np.array_equal(loaded.recording.get_traces(end_frame=100), recording.get_traces(end_frame=100)) + + sorting_provenance = loaded.get_sorting_provenance() + assert sorting_provenance is not None + assert np.array_equal(sorting_provenance.unit_ids, sorting.unit_ids) + for unit_id in sorting.unit_ids: + assert np.array_equal(sorting_provenance.get_unit_spike_train(unit_id), sorting.get_unit_spike_train(unit_id)) + + def test_SortingAnalyzer_binary_folder(tmp_path, dataset): recording, sorting = dataset recording = recording.save(folder=tmp_path / "recording_binary") @@ -142,11 +170,13 @@ def test_SortingAnalyzer_binary_folder(tmp_path, dataset): assert "number" in sorting_analyzer_reloded.sorting.get_property_keys() -@pytest.mark.requires_zarr_write def test_SortingAnalyzer_zarr(tmp_path, dataset): recording, sorting = dataset recording = recording.save(folder=tmp_path / "recording_zarr") + # make recording JSON serializable + recording = recording.save(folder=tmp_path / "recording_for_zarr", overwrite=True) + folder = tmp_path / "test_SortingAnalyzer_zarr.zarr" default_compressor = get_default_zarr_compressor() @@ -158,13 +188,12 @@ def test_SortingAnalyzer_zarr(tmp_path, dataset): _check_sorting_analyzers(sorting_analyzer, sorting, cache_folder=tmp_path) # check that compression is applied - assert ( - sorting_analyzer._get_zarr_root()["extensions"]["random_spikes"]["random_spikes_indices"].compressor.codec_id - == default_compressor.codec_id + check_compressors_match( + default_compressor, + sorting_analyzer._get_zarr_root()["extensions"]["random_spikes"]["random_spikes_indices"].compressors[0], ) - assert ( - sorting_analyzer._get_zarr_root()["extensions"]["templates"]["average"].compressor.codec_id - == default_compressor.codec_id + check_compressors_match( + default_compressor, sorting_analyzer._get_zarr_root()["extensions"]["templates"]["average"].compressors[0] ) # test select_units see https://github.com/SpikeInterface/spikeinterface/issues/3041 @@ -185,35 +214,33 @@ def test_SortingAnalyzer_zarr(tmp_path, dataset): sparsity=None, return_in_uV=False, overwrite=True, - backend_options={"saving_options": {"compressor": None}}, + backend_options={"saving_options": {"compressors": None}}, ) - print(sorting_analyzer_no_compression._backend_options) sorting_analyzer_no_compression.compute(["random_spikes", "templates"]) assert ( - sorting_analyzer_no_compression._get_zarr_root()["extensions"]["random_spikes"][ - "random_spikes_indices" - ].compressor - is None + len( + sorting_analyzer_no_compression._get_zarr_root()["extensions"]["random_spikes"][ + "random_spikes_indices" + ].compressors + ) + == 0 ) - assert sorting_analyzer_no_compression._get_zarr_root()["extensions"]["templates"]["average"].compressor is None + assert len(sorting_analyzer_no_compression._get_zarr_root()["extensions"]["templates"]["average"].compressors) == 0 # test a different compressor - from numcodecs import LZMA + from zarr.codecs import LZMA lzma_compressor = LZMA() folder = tmp_path / "test_SortingAnalyzer_zarr_lzma.zarr" sorting_analyzer_lzma = sorting_analyzer_no_compression.save_as( - format="zarr", folder=folder, backend_options={"saving_options": {"compressor": lzma_compressor}} + format="zarr", folder=folder, backend_options={"saving_options": {"compressors": lzma_compressor}} ) - assert ( - sorting_analyzer_lzma._get_zarr_root()["extensions"]["random_spikes"][ - "random_spikes_indices" - ].compressor.codec_id - == LZMA.codec_id + check_compressors_match( + lzma_compressor, + sorting_analyzer_lzma._get_zarr_root()["extensions"]["random_spikes"]["random_spikes_indices"].compressors[0], ) - assert ( - sorting_analyzer_lzma._get_zarr_root()["extensions"]["templates"]["average"].compressor.codec_id - == LZMA.codec_id + check_compressors_match( + lzma_compressor, sorting_analyzer_lzma._get_zarr_root()["extensions"]["templates"]["average"].compressors[0] ) # test set_sorting_property @@ -314,22 +341,20 @@ def test_load_without_runtime_info(tmp_path, dataset): with pytest.warns(UserWarning): sorting_analyzer = load_sorting_analyzer(folder, format="auto") - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - # zarr - folder = tmp_path / "test_SortingAnalyzer_run_info.zarr" - sorting_analyzer = create_sorting_analyzer( - sorting, recording, format="zarr", folder=folder, sparse=False, sparsity=None - ) - sorting_analyzer.compute(extensions) - # remove run_info from attrs to mimic a previous version of spikeinterface - root = sorting_analyzer._get_zarr_root(mode="r+") - for ext in extensions: - del root["extensions"][ext].attrs["run_info"] - zarr.consolidate_metadata(root.store) - # should raise a warning for missing run_info - with pytest.warns(UserWarning): - sorting_analyzer = load_sorting_analyzer(folder, format="auto") + # zarr + folder = tmp_path / "test_SortingAnalyzer_run_info.zarr" + sorting_analyzer = create_sorting_analyzer( + sorting, recording, format="zarr", folder=folder, sparse=False, sparsity=None + ) + sorting_analyzer.compute(extensions) + # remove run_info from attrs to mimic a previous version of spikeinterface + root = sorting_analyzer._get_zarr_root(mode="r+") + for ext in extensions: + del root["extensions"][ext].attrs["run_info"] + zarr.consolidate_metadata(root.store) + # should raise a warning for missing run_info + with pytest.warns(UserWarning): + sorting_analyzer = load_sorting_analyzer(folder, format="auto") def test_SortingAnalyzer_tmp_recording(dataset): @@ -373,7 +398,7 @@ def test_SortingAnalyzer_interleaved_probegroup(dataset): assert np.array_equal(recording.get_channel_locations(), sorting_analyzer.get_channel_locations()) -@pytest.mark.parametrize("format", ["binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) +@pytest.mark.parametrize("format", ["binary_folder", "zarr"]) def test_load_in_lazy_mode(tmp_path, dataset, format): recording, sorting = dataset @@ -891,9 +916,7 @@ def _compute_reference_pipeline_data(dataset): return analyzer.get_extension("dummy_pipeline").get_data() -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize("lazy", [True, False]) def test_compute_pipeline_extension_gather_to_disk_lazy(tmp_path, dataset, format, lazy): """ @@ -958,7 +981,7 @@ def test_compute_pipeline_extension_gather_to_disk_lazy(tmp_path, dataset, forma assert np.array_equal(load_sorting_analyzer(folder).get_extension("dummy_pipeline").get_data(), amp_ref) -@pytest.mark.parametrize("format", ["binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) +@pytest.mark.parametrize("format", ["binary_folder", "zarr"]) def test_compute_pipeline_extension_save_false(tmp_path, dataset, format): """ With save=False on a disk-backed analyzer, node-pipeline extensions are computed in memory @@ -980,9 +1003,7 @@ def test_compute_pipeline_extension_save_false(tmp_path, dataset, format): assert not analyzer_reloaded.has_extension("dummy_pipeline") -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) @pytest.mark.parametrize("lazy", [True, False]) def test_compute_one_pipeline_extension_gather_to_disk(tmp_path, dataset, format, lazy): """ diff --git a/src/spikeinterface/core/tests/test_template_class.py b/src/spikeinterface/core/tests/test_template_class.py index 20f88817cb..953527c8d5 100644 --- a/src/spikeinterface/core/tests/test_template_class.py +++ b/src/spikeinterface/core/tests/test_template_class.py @@ -115,7 +115,6 @@ def test_initialization_fail_with_dense_templates(): template = generate_test_template(template_type="sparse_with_dense_templates") -@pytest.mark.requires_zarr_write @pytest.mark.parametrize("is_in_uV", [True, False]) @pytest.mark.parametrize("template_type", ["dense", "sparse"]) def test_save_and_load_zarr(template_type, is_in_uV, tmp_path): diff --git a/src/spikeinterface/core/tests/test_time_handling.py b/src/spikeinterface/core/tests/test_time_handling.py index 4a08a1c7c8..2f3cc1a6bf 100644 --- a/src/spikeinterface/core/tests/test_time_handling.py +++ b/src/spikeinterface/core/tests/test_time_handling.py @@ -114,7 +114,7 @@ def test_has_time_vector(self, time_vector_recording): assert raw_recording.has_time_vector(segment_idx) is False assert times_recording.has_time_vector(segment_idx) is True - @pytest.mark.parametrize("mode", ["binary", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) + @pytest.mark.parametrize("mode", ["binary", "zarr"]) @pytest.mark.parametrize("fixture_name", ["time_vector_recording", "t_start_recording"]) def test_times_propagated_to_save_folder(self, request, fixture_name, mode, tmp_path): """ @@ -375,7 +375,7 @@ def test_save_and_load_time_shift(self, request, fixture_name, tmp_path): times_recording.get_times(segment_index=idx), loaded_recording.get_times(segment_index=idx) ) - @pytest.mark.parametrize("save_format", ["binary", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)]) + @pytest.mark.parametrize("save_format", ["binary", "zarr"]) def test_shift_times_after_load(self, request, save_format, tmp_path): """ Shift times on a recording loaded from disk as a read-only np.memmap diff --git a/src/spikeinterface/core/tests/test_unitsselectionsorting.py b/src/spikeinterface/core/tests/test_unitsselectionsorting.py index 309563df62..c240d25d88 100644 --- a/src/spikeinterface/core/tests/test_unitsselectionsorting.py +++ b/src/spikeinterface/core/tests/test_unitsselectionsorting.py @@ -248,7 +248,6 @@ def test_non_identity_selection_does_not_share(unit_ids): assert len(parent._cached_lexsorted_spike_vector) == 0 -@pytest.mark.requires_zarr_write def test_identity_selection_keeps_lazy_zarr_vector(tmp_path): """A lazy parent spike vector should stay lazy through an identity selection.""" from spikeinterface.core.zarrextractors import ZarrSortingExtractor, ZarrSpikeVector diff --git a/src/spikeinterface/core/tests/test_waveform_tools.py b/src/spikeinterface/core/tests/test_waveform_tools.py index c2750774b6..6623c92046 100644 --- a/src/spikeinterface/core/tests/test_waveform_tools.py +++ b/src/spikeinterface/core/tests/test_waveform_tools.py @@ -157,7 +157,6 @@ def test_waveform_tools(create_cache_folder): _check_all_wf_equal(list_wfs_sparse) -@pytest.mark.requires_zarr_write @pytest.mark.parametrize("sparse", [False, True]) def test_extract_waveforms_to_single_buffer_zarr(tmp_path, sparse): # the "zarr" mode writes waveforms directly to a zarr dataset. Workers return their block and @@ -211,7 +210,6 @@ def test_extract_waveforms_to_single_buffer_zarr(tmp_path, sparse): assert np.array_equal(reference, reloaded[:]) -@pytest.mark.requires_zarr_write def test_waveforms_at_segment_borders(tmp_path): # spikes near the segment borders are partially filled: samples outside of the segment are 0 from spikeinterface.core import NumpySorting diff --git a/src/spikeinterface/core/tests/test_zarrextractors.py b/src/spikeinterface/core/tests/test_zarrextractors.py index 2b388839d1..e8c76018de 100644 --- a/src/spikeinterface/core/tests/test_zarrextractors.py +++ b/src/spikeinterface/core/tests/test_zarrextractors.py @@ -9,6 +9,8 @@ generate_sorting, load, ) +from spikeinterface.core.testing import check_recordings_equal +from spikeinterface.core.zarr_tools import check_compressors_match from spikeinterface.core.zarrextractors import ( ZarrRecordingExtractor, ZarrSampleIndexSearch, @@ -18,51 +20,49 @@ ) -@pytest.mark.requires_zarr_write def test_zarr_compression_options(tmp_path): - from numcodecs import Blosc, Delta, FixedScaleOffset + from zarr.codecs import BloscCodec, BloscShuffle, Delta, FixedScaleOffset recording = generate_recording(durations=[2]) recording.set_times(recording.get_times() + 100) # store in root standard normal way # default compressor - defaut_compressor = get_default_zarr_compressor() + default_compressor = get_default_zarr_compressor() # other compressor - other_compressor1 = Blosc(cname="zlib", clevel=3, shuffle=Blosc.NOSHUFFLE) - other_compressor2 = Blosc(cname="blosclz", clevel=8, shuffle=Blosc.AUTOSHUFFLE) + other_compressor1 = BloscCodec(cname="zlib", clevel=3, shuffle=BloscShuffle.noshuffle) + other_compressor2 = BloscCodec(cname="blosclz", clevel=8, shuffle=BloscShuffle.shuffle) # timestamps compressors / filters default_filters = None - other_filters1 = [FixedScaleOffset(scale=5, offset=2, dtype=recording.get_dtype())] + other_filters1 = [FixedScaleOffset(scale=5, offset=2, dtype=recording.get_dtype().str)] other_filters2 = [Delta(dtype="float64")] # default ZarrRecordingExtractor.write_recording(recording, tmp_path / "rec_default.zarr") rec_default = ZarrRecordingExtractor(tmp_path / "rec_default.zarr") - assert rec_default._root["traces_seg0"].compressor == defaut_compressor - assert rec_default._root["traces_seg0"].filters == default_filters - assert rec_default._root["times_seg0"].compressor == defaut_compressor - assert rec_default._root["times_seg0"].filters == default_filters + check_compressors_match(rec_default._root["traces_seg0"].compressors[0], default_compressor) + check_compressors_match(rec_default._root["times_seg0"].compressors[0], default_compressor) + check_compressors_match(rec_default._root["traces_seg0"].filters, default_filters) + check_compressors_match(rec_default._root["times_seg0"].filters, default_filters) # now with other compressor ZarrRecordingExtractor.write_recording( recording, tmp_path / "rec_other.zarr", - compressor=defaut_compressor, + compressors=default_compressor, filters=default_filters, compressor_by_dataset={"traces": other_compressor1, "times": other_compressor2}, filters_by_dataset={"traces": other_filters1, "times": other_filters2}, ) rec_other = ZarrRecordingExtractor(tmp_path / "rec_other.zarr") - assert rec_other._root["traces_seg0"].compressor == other_compressor1 - assert rec_other._root["traces_seg0"].filters == other_filters1 - assert rec_other._root["times_seg0"].compressor == other_compressor2 - assert rec_other._root["times_seg0"].filters == other_filters2 + check_compressors_match(rec_other._root["traces_seg0"].compressors[0], other_compressor1) + check_compressors_match(rec_other._root["traces_seg0"].filters, other_filters1) + check_compressors_match(rec_other._root["times_seg0"].compressors[0], other_compressor2) + check_compressors_match(rec_other._root["times_seg0"].filters, other_filters2) -@pytest.mark.requires_zarr_write def test_ZarrSortingExtractor(tmp_path): np_sorting = generate_sorting() @@ -82,6 +82,49 @@ def test_ZarrSortingExtractor(tmp_path): sorting = load(sorting.to_dict()) +def test_sharding_options(tmp_path): + recording = generate_recording(durations=[10], num_channels=20) + folder = tmp_path / "zarr_sharding.zarr" + + # explicitly specify chunks and shards + ZarrRecordingExtractor.write_recording(recording, folder, chunks=(1000, 5), shards=(5000, 10), n_jobs=2) + recording_zarr = ZarrRecordingExtractor(folder) + assert recording_zarr._root["traces_seg0"].chunks == (1000, 5) + assert recording_zarr._root["traces_seg0"].shards == (5000, 10) + check_recordings_equal(recording, recording_zarr) + + # specify shard_factor and chunk_size + folder = tmp_path / "zarr_sharding_factor.zarr" + ZarrRecordingExtractor.write_recording( + recording, folder, chunk_size=1000, channel_chunk_size=2, shard_factor=(5, 2), n_jobs=2 + ) + recording_zarr = ZarrRecordingExtractor(folder) + assert recording_zarr._root["traces_seg0"].chunks == (1000, 2) + assert recording_zarr._root["traces_seg0"].shards == (5000, 4) + check_recordings_equal(recording, recording_zarr) + + # raise error if both shards and shard_factor are provided + with pytest.raises(ValueError): + folder = tmp_path / "shards_and_shard_factor.zarr" + ZarrRecordingExtractor.write_recording( + recording, folder, chunk_size=1000, channel_chunk_size=2, shard_factor=5, shards=(5000, 10), n_jobs=2 + ) + + # raise error if shards is smaller than chunks + with pytest.raises(AssertionError): + folder = tmp_path / "shards_smaller_than_chunks.zarr" + ZarrRecordingExtractor.write_recording( + recording, folder, chunk_size=1000, channel_chunk_size=2, shards=(500, 10), n_jobs=2 + ) + + # raise error if shards is not a multiple of chunks + with pytest.raises(AssertionError): + folder = tmp_path / "shards_not_multiple_of_chunks.zarr" + ZarrRecordingExtractor.write_recording( + recording, folder, chunk_size=1000, channel_chunk_size=2, shards=(5500, 10), n_jobs=2 + ) + + def test_ZarrSampleIndexSearch(tmp_path): rng = np.random.default_rng(0) # two "segments", each sorted, with long runs of equal values so that runs cross @@ -102,7 +145,6 @@ def test_ZarrSampleIndexSearch(tmp_path): np.testing.assert_array_equal(search.searchsorted([5], 10, 10), [0]) -@pytest.mark.requires_zarr_write def test_ZarrSortingExtractor_lazy_search(tmp_path): sorting = generate_sorting(num_units=10, durations=[5.0, 3.0, 4.0], firing_rates=40.0, seed=0) folder = tmp_path / "sorting.zarr" @@ -112,8 +154,8 @@ def test_ZarrSortingExtractor_lazy_search(tmp_path): spikes_group = zarr.open(folder, mode="a")["spikes"] sample_index = spikes_group["sample_index"][:] del spikes_group["sample_index"], spikes_group["sample_index_chunk_firsts"] - spikes_group.create_dataset("sample_index", data=sample_index, chunks=(97,)) - spikes_group.create_dataset("sample_index_chunk_firsts", data=sample_index[::97], compressor=None) + spikes_group.create_array("sample_index", data=sample_index, chunks=(97,)) + spikes_group.create_array("sample_index_chunk_firsts", data=sample_index[::97], compressors=None) assert spikes_group["sample_index"].nchunks > 3 in_ram = ZarrSortingExtractor(folder) diff --git a/src/spikeinterface/core/time_series_tools.py b/src/spikeinterface/core/time_series_tools.py index 758482b965..1ebf0374df 100644 --- a/src/spikeinterface/core/time_series_tools.py +++ b/src/spikeinterface/core/time_series_tools.py @@ -2,7 +2,6 @@ import warnings import numpy as np -import numpy.typing from .core_tools import add_suffix, make_shared_array from .job_tools import ( @@ -297,8 +296,9 @@ def _write_time_series_to_zarr( zarr_group, dataset_paths, dataset_timestamps_paths=None, - extra_chunks=None, dtype=None, + chunks=None, + shards=None, compressor_data=None, filters_data=None, compressor_times=None, @@ -308,6 +308,7 @@ def _write_time_series_to_zarr( ): """ Save the trace of a time_series object in several zarr format. + If shard Parameters ---------- @@ -319,10 +320,10 @@ def _write_time_series_to_zarr( List of paths to traces datasets in the zarr group dataset_timestamps_paths : list or None, default: None List of paths to timestamps datasets in the zarr group. If None, timestamps are not saved. - extra_chunks : tuple or None, default: None - Extra chunking dimensions to use for the zarr dataset. - The first dimension is always time and controlled by the job_kwargs. - This is for example useful to chunk by channel, with `extra_chunks=(channel_chunk_size,)`. + chunks : tuple or None, default: None + Chunking dimensions to use for the zarr dataset. + shards : tuple or None, default: None + Sharding configuration for the zarr dataset. dtype : dtype, default: None Type of the saved data compressor_data : zarr compressor or None, default: None @@ -338,11 +339,13 @@ def _write_time_series_to_zarr( {} """ from .job_tools import ( - ensure_chunk_size, fix_job_kwargs, TimeSeriesChunkExecutor, ) + # place an ArrayBytesCodec passed as a compressor (e.g. WavPack) in the serializer slot + from .zarr_tools import build_codec_pipeline + assert dataset_paths is not None, "Provide 'dataset_paths' to save data in zarr format" if dataset_timestamps_paths is not None: assert ( @@ -359,13 +362,9 @@ def _write_time_series_to_zarr( dtype = time_series.get_dtype() job_kwargs = fix_job_kwargs(job_kwargs) - chunk_size = ensure_chunk_size(time_series, **job_kwargs) - if extra_chunks is not None: - assert len(extra_chunks) == len(time_series.get_shape(0)[1:]), ( - "extra_chunks should have the same length as the number of dimensions " - "of the time_series minus one (time axis)" - ) + codec_kwargs_data = build_codec_pipeline(filters=filters_data, compressors=compressor_data) + codec_kwargs_times = build_codec_pipeline(filters=filters_times, compressors=compressor_times) # create zarr datasets files zarr_datasets = [] @@ -375,25 +374,27 @@ def _write_time_series_to_zarr( num_samples = time_series.get_num_samples(segment_index) dset_name = dataset_paths[segment_index] shape = time_series.get_shape(segment_index) - dset = zarr_group.create_dataset( + dset = zarr_group.create_array( name=dset_name, shape=shape, - chunks=(chunk_size,) + extra_chunks if extra_chunks is not None else (chunk_size,), + chunks=chunks, + shards=shards, dtype=dtype, - filters=filters_data, - compressor=compressor_data, + **codec_kwargs_data, ) zarr_datasets.append(dset) if dataset_timestamps_paths[segment_index] is not None: tset_name = dataset_timestamps_paths[segment_index] + chunks_times = (chunks[0],) if chunks is not None else None + shards_times = (shards[0],) if shards is not None else None zarr_timestamps_datasets.append( - zarr_group.create_dataset( + zarr_group.create_array( name=tset_name, shape=(num_samples,), - chunks=(chunk_size,), + chunks=chunks_times, + shards=shards_times, dtype="float64", - filters=filters_times, - compressor=compressor_times, + **codec_kwargs_times, ) ) else: @@ -416,7 +417,7 @@ def _write_time_series_to_zarr( t_starts[segment_index] = time_info["t_start"] if np.any(~np.isnan(t_starts)): - zarr_group.create_dataset(name="t_starts", data=t_starts, compressor=None) + zarr_group.create_array(name="t_starts", data=t_starts, compressors=None) def _init_zarr_worker(time_series, zarr_datasets, dtype, zarr_timestamps_datasets=None): diff --git a/src/spikeinterface/core/waveform_tools.py b/src/spikeinterface/core/waveform_tools.py index bcbdaf7e14..a557c52ba6 100644 --- a/src/spikeinterface/core/waveform_tools.py +++ b/src/spikeinterface/core/waveform_tools.py @@ -725,12 +725,12 @@ def _create_empty_zarr_waveforms(file_path, shape, dtype, zarr_target_chunk_byte row_nbytes = int(np.prod(shape[1:])) * dtype.itemsize chunk0 = max(1, zarr_target_chunk_bytes // max(1, row_nbytes)) zarr_root = zarr.open(str(store_path), mode="a") - return zarr_root.create_dataset( + return zarr_root.create_array( name=dataset_path, shape=shape, chunks=(chunk0,) + shape[1:], dtype=dtype, - compressor=get_default_zarr_compressor(), + compressors=get_default_zarr_compressor(), overwrite=True, ) diff --git a/src/spikeinterface/core/zarr_tools.py b/src/spikeinterface/core/zarr_tools.py index 776a33dfb0..41ff57a358 100644 --- a/src/spikeinterface/core/zarr_tools.py +++ b/src/spikeinterface/core/zarr_tools.py @@ -1,10 +1,382 @@ +import warnings import json import pickle - import numpy as np +import zarr + +from spikeinterface.core.job_tools import ensure_chunk_size + +# metadata keys that are not members of a group (zarr v2 and v3) +_ZARR_METADATA_KEYS = (".zarray", ".zattrs", ".zgroup", ".zmetadata", "zarr.json") + + +def adjust_chunks_shards_and_job_kwargs( + chunks=None, extra_chunks=None, shards=None, shard_factor=None, job_kwargs=None, time_series=None +): + """ + Adjust chunks, shards, and job_kwargs for zarr storage. + + Parameters + ---------- + chunks : tuple or None + Chunking dimensions for the zarr dataset. + extra_chunks : tuple or None + Extra chunking dimensions for the zarr dataset. + shards : tuple or None + Sharding configuration for the zarr dataset. + shard_factor : int or tuple or None + Factor to determine shard sizes based on chunks. + job_kwargs : dict or None + Job-related keyword arguments, including 'chunk_size'. + time_series : TimeSeries or None + The time series object being stored. + + Returns + ------- + chunks : tuple + Adjusted chunking dimensions. + shards : tuple or None + Adjusted sharding configuration. + job_kwargs : dict + Updated job-related keyword arguments. + """ + + # Chunking and sharding + if shards is not None and shard_factor is not None: + raise ValueError("Cannot specify both 'shards' and 'shard_factor' in zarr_kwargs") + if chunks is not None and extra_chunks is not None: + raise ValueError("Cannot specify both 'chunks' and 'extra_chunks' in zarr_kwargs") + + # If not specified by chunk, we set the chunk size in the first dimension (time) to be the chunk size that we use + # for the job executor, and the chunk size in the second dimension (channels) to be either the provided + # channel_chunk_size or the total number of channels (no chunking in channels). + if chunks is not None: + job_kwargs["chunk_size"] = chunks[0] + else: + chunk_size = ensure_chunk_size(time_series, **job_kwargs) + chunks = ( + (chunk_size,) + extra_chunks if extra_chunks is not None else (chunk_size,) + time_series.get_shape(0)[1:] + ) + + if shards is not None: + assert len(shards) == len(chunks), "Shards and chunks must have the same number of dimensions" + for dim in range(len(chunks)): + assert ( + shards[dim] >= chunks[dim] and shards[dim] % chunks[dim] == 0 + ), "Shard size must be a multiple of chunk size" + # When sharding is used, chunk_size in job_kwargs is used to determine the number of samples per chunk to + # write in each job. Each process will write all chunks in a shard. + job_kwargs["chunk_size"] = shards[0] + elif shard_factor is not None: + # If shard_factor is an integer, we only apply it to the first dimension. If it's an iterable, + # we apply it to all dimensions. + if isinstance(shard_factor, (int, np.integer)): + shards = (chunks[0] * shard_factor,) + chunks[1:] + else: + if len(shard_factor) != len(chunks): + raise ValueError("shard_factor must have the same length as chunks when it is an iterable") + shards = tuple(chunks[dim] * shard_factor[dim] for dim in range(len(chunks))) + job_kwargs["chunk_size"] = shards[0] + return chunks, shards, job_kwargs -def write_json_array(group, name: str, data, compressor=None): + +def check_compressors_match(comp1, comp2, skip_typesize=True): + """ + Check if two compressor objects match. + + Parameters + ---------- + comp1 : zarr.abc.codec.Codec | Tuple[zarr.abc.codec.Codec] + The first compressor object to compare. + comp2 : zarr.abc.codec.Codec | Tuple[zarr.abc.codec.Codec] + The second compressor object to compare. + skip_typesize : bool, optional + Whether to skip the typesize check, default: True + """ + if not isinstance(comp1, (list, tuple)): + assert not isinstance(comp2, list) + comp1 = [comp1] + comp2 = [comp2] + for i in range(len(comp1)): + comp1_dict = comp1[i].to_dict() + comp2_dict = comp2[i].to_dict() + if skip_typesize: + if "typesize" in comp1_dict["configuration"]: + comp1_dict["configuration"].pop("typesize", None) + if "typesize" in comp2_dict["configuration"]: + comp2_dict["configuration"].pop("typesize", None) + assert comp1_dict == comp2_dict, f"Compressor {i} does not match: {comp1_dict} != {comp2_dict}" + + +class LegacyZarrObjectArray: + """ + Read-only stand-in for a zarr v2 object-dtype array that zarr-python >= 3 cannot open. + + spikeinterface < 0.105 saved python objects (dicts, lists, provenance dicts, ...) as + length-1 object-dtype arrays using the `numcodecs` JSON or Pickle object codecs. + zarr-python >= 3 dropped support for these dtype/codec combinations, so such arrays + are decoded "manually" with `numcodecs` and exposed through this minimal wrapper, + which mimics the small part of the `zarr.Array` API used to read them + (`attrs`, `__getitem__`, `shape`, `dtype`, `len`). + + Parameters + ---------- + values : np.ndarray + The decoded object-dtype array. + attrs : dict + The zarr attributes of the array (content of `.zattrs`). + """ + + def __init__(self, values: np.ndarray, attrs: dict): + self._values = values + self._attrs = dict(attrs) + + @property + def attrs(self) -> dict: + return self._attrs + + @property + def shape(self): + return self._values.shape + + @property + def dtype(self): + return self._values.dtype + + def __len__(self): + return len(self._values) + + def __getitem__(self, key): + return self._values[key] + + def __array__(self, dtype=None, copy=None): + if dtype is None: + return self._values + return self._values.astype(dtype) + + def __repr__(self): + return f"LegacyZarrObjectArray(shape={self.shape}, attrs={list(self._attrs.keys())})" + + +def _sync(coroutine): + """Run a zarr async coroutine synchronously (zarr-python >= 3).""" + from zarr.core.sync import sync + + return sync(coroutine) + + +def _store_get_json(store, key: str): + """Read and json-decode a metadata key from a zarr store. Return None if missing.""" + import json + + from zarr.core.buffer import default_buffer_prototype + + buffer = _sync(store.get(key, prototype=default_buffer_prototype())) + if buffer is None: + return None + return json.loads(buffer.to_bytes().decode()) + + +def _store_get_bytes(store, key: str): + """Read raw bytes from a zarr store. Return None if the key is missing.""" + from zarr.core.buffer import default_buffer_prototype + + buffer = _sync(store.get(key, prototype=default_buffer_prototype())) + if buffer is None: + return None + return buffer.to_bytes() + + +def read_legacy_zarr_object_array(store, path: str) -> LegacyZarrObjectArray: + """ + Read a zarr v2 object-dtype array written with a `numcodecs` object codec + (JSON, Pickle, MsgPack), which zarr-python >= 3 cannot open. + + Only single-chunk arrays are supported, which is what spikeinterface < 0.105 wrote. + + Parameters + ---------- + store : zarr.abc.store.Store + The store containing the array. + path : str + The path of the array inside the store. + + Returns + ------- + legacy_array : LegacyZarrObjectArray + The decoded array. + """ + import numcodecs + + path = path.strip("/") + zarray = _store_get_json(store, f"{path}/.zarray") + if zarray is None: + raise KeyError(f"No zarr v2 array metadata found at {path}") + zattrs = _store_get_json(store, f"{path}/.zattrs") or {} + + shape = tuple(zarray["shape"]) + chunks = tuple(zarray["chunks"]) + if any(chunk_size < dim for chunk_size, dim in zip(chunks, shape)): + raise NotImplementedError( + f"Legacy zarr v2 object array at {path} has more than one chunk, which is not supported" + ) + + separator = zarray.get("dimension_separator", ".") + chunk_key = separator.join(["0"] * max(len(shape), 1)) + chunk_bytes = _store_get_bytes(store, f"{path}/{chunk_key}") + if chunk_bytes is None: + # array was never written to: return an empty object array + return LegacyZarrObjectArray(np.empty(shape, dtype=object), zattrs) + + if zarray.get("compressor", None) is not None: + chunk_bytes = numcodecs.get_codec(zarray["compressor"]).decode(chunk_bytes) + values = chunk_bytes + # filters are applied on encode, so they are decoded in reverse order. + # for object arrays the object codec is the (only) filter + for filter_config in reversed(zarray.get("filters", None) or []): + if filter_config.get("id") == "pickle": + # numcodecs>=0.16 disables Pickle.decode by default; we already trust pickles in the analyzer folder + values = pickle.loads(bytes(values)) + else: + values = numcodecs.get_codec(filter_config).decode(values) + + values = np.asarray(values, dtype=object).reshape(shape) + + return LegacyZarrObjectArray(values, zattrs) + + +def get_zarr_attr_or_legacy_object(zarr_group, name: str): + """ + Get a value saved either in the attributes of a zarr group (spikeinterface >= 0.105) + or as a legacy zarr v2 length-1 object array with the same name + (spikeinterface < 0.105, e.g. "recording" and "sorting_provenance"). + + Parameters + ---------- + zarr_group : zarr.Group + The zarr group to read from. + name : str + The name of the attribute / legacy array. + + Returns + ------- + value : Any | None + The value, or None if it is not found (or cannot be decoded). + """ + value = zarr_group.attrs.get(name, None) + if value is not None: + return value + try: + legacy_array = read_legacy_zarr_object_array(zarr_group.store, f"{zarr_group.path}/{name}") + except Exception: + return None + if len(legacy_array) == 0: + return None + return legacy_array[0] + + +def _list_member_names(zarr_group) -> list[str]: + """List the member names of a zarr group without opening them.""" + consolidated = getattr(zarr_group.metadata, "consolidated_metadata", None) + if consolidated is not None: + return list(consolidated.metadata.keys()) + + store = zarr_group.store + if not store.supports_listing: + raise ValueError( + f"The store associated to this group ({type(store).__name__}) does not support listing, " + "so its members cannot be listed without consolidated metadata." + ) + + async def _list_dir(): + return [key async for key in store.list_dir(zarr_group.path)] + + keys = _sync(_list_dir()) + # skip zarr metadata documents and hidden files (e.g. AppleDouble "._*" files) + return sorted(key for key in keys if key not in _ZARR_METADATA_KEYS and not key.startswith(".")) + + +def iterate_zarr_group(zarr_group, skip_unreadable: bool = True): + """ + Iterate over the members (arrays and sub-groups) of a zarr group, transparently + handling zarr v2 and zarr v3 groups. + + Unlike `zarr.Group.keys()` / `zarr.Group.members()`, a single member that + zarr-python >= 3 cannot open does not make the whole iteration fail. This happens for + zarr v2 data saved by spikeinterface < 0.105, where python objects were stored as + object-dtype arrays with the `numcodecs` JSON/Pickle object codecs. Such arrays are + decoded with `numcodecs` and returned as a `LegacyZarrObjectArray`. + + Parameters + ---------- + zarr_group : zarr.Group + The zarr group to iterate over. + skip_unreadable : bool, default: True + If True, members that cannot be opened nor decoded are skipped with a warning. + If False, an error is raised instead. + + Yields + ------ + name : str + The name of the member. + member : zarr.Group | zarr.Array | LegacyZarrObjectArray + The member itself. + """ + for name in _list_member_names(zarr_group): + # note: members are retrieved outside of the yield statement, so that exceptions + # raised by the consumer of this generator are not caught here + member = None + open_error = None + try: + member = zarr_group[name] + except KeyError: + # the key is an object in the store (e.g. a stray file), not a zarr node + continue + except Exception as e: + open_error = e + + if open_error is not None: + # the member exists but zarr-python cannot open it: try the legacy object array path + try: + member = read_legacy_zarr_object_array(zarr_group.store, f"{zarr_group.path}/{name}") + except Exception as legacy_error: + if not skip_unreadable: + raise ValueError( + f"Cannot read member '{name}' of zarr group '{zarr_group.path}': " + f"{open_error}\nLegacy object array fallback failed with: {legacy_error}" + ) from open_error + warnings.warn( + f"Skipping member '{name}' of zarr group '{zarr_group.path}' because it cannot be read " + f"with zarr v{zarr.__version__}: {open_error} ({legacy_error})" + ) + continue + + yield name, member + + +def get_zarr_group_keys(zarr_group) -> list[str]: + """ + Return the names of the members of a zarr group, for both zarr v2 and zarr v3 groups. + + Contrary to `zarr.Group.keys()`, the members are not opened, so this also works for + groups containing legacy zarr v2 arrays that zarr-python >= 3 cannot open + (see `iterate_zarr_group`). + + Parameters + ---------- + zarr_group : zarr.Group + The zarr group to list. + + Returns + ------- + keys : list[str] + The names of the members of the group. + """ + return _list_member_names(zarr_group) + + +def write_json_array(group, name: str, data, compressors=None, filters=None): """ Save json-serializable data as the utf-8 bytes of its json text in a uint8 array. @@ -22,8 +394,10 @@ def write_json_array(group, name: str, data, compressor=None): The name of the array. data : dict | list The json-serializable data to save. - compressor : numcodecs.Codec | None, default: None - The compressor of the array. + compressors: BytesBytesCodec | ArrayBytesCodec | list of them, or None, default: None + Compressors to use for the array. + filters: ArrayArrayCodec | list[ArrayArrayCodec] or None, default: None + Filters to use for the array. Returns ------- @@ -31,12 +405,13 @@ def write_json_array(group, name: str, data, compressor=None): The created array. """ payload = np.frombuffer(json.dumps(data).encode("utf-8"), dtype="uint8") - array = group.create_dataset(name=name, data=payload, compressor=compressor) + codec_kwargs = build_codec_pipeline(filters=filters, compressors=compressors) + array = group.create_array(name=name, data=payload, **codec_kwargs) array.attrs["encoding"] = "json" return array -def write_pickle_array(group, name: str, data, compressor=None): +def write_pickle_array(group, name: str, data, compressors=None, filters=None): """ Save a python object as its pickle bytes in a uint8 array. @@ -52,8 +427,10 @@ def write_pickle_array(group, name: str, data, compressor=None): The name of the array. data : Any The picklable object to save. - compressor : numcodecs.Codec | None, default: None - The compressor of the array. + compressors: BytesBytesCodec | ArrayBytesCodec | list of them, or None, default: None + Compressors to use for the array. + filters: ArrayArrayCodec | list[ArrayArrayCodec] or None, default: None + Filters to use for the array. Returns ------- @@ -61,7 +438,8 @@ def write_pickle_array(group, name: str, data, compressor=None): The created array. """ payload = np.frombuffer(pickle.dumps(data), dtype="uint8") - array = group.create_dataset(name=name, data=payload, compressor=compressor) + codec_kwargs = build_codec_pipeline(filters=filters, compressors=compressors) + array = group.create_array(name=name, data=payload, **codec_kwargs) array.attrs["encoding"] = "pickle" return array @@ -92,3 +470,70 @@ def read_encoded_array(array): raise ValueError( f"Unknown encoding {encoding!r} for {array.name}. It was probably saved by a newer version of spikeinterface." ) + + +def build_codec_pipeline(filters=None, compressors=None): + """ + Build zarr v3 codec kwargs from filters and compressors. + + Classifies codecs into the three slots accepted by ``zarr.Group.create_array()``: + 1. ``filters`` — ArrayArrayCodec (e.g. Delta) + 2. ``serializer`` — ArrayBytesCodec (e.g. WavPack, BytesCodec) + 3. ``compressors``— BytesBytesCodec (e.g. BloscCodec, ZstdCodec) + + This allows callers to pass an ArrayBytesCodec (e.g. WavPack) as a + compressor and have it placed in the correct serializer slot automatically. + + Parameters + ---------- + filters : ArrayArrayCodec or list of ArrayArrayCodec or None + Codec(s) applied before serialization. + compressors : codec or list of codecs or None + Can be a mix of ArrayBytesCodec (serializer) and BytesBytesCodec + (byte-level compressors). At most one ArrayBytesCodec is allowed. + + Returns + ------- + dict + Keyword arguments to unpack into ``zarr.Group.create_array()``. + Only keys with explicit values are included; omitted keys let zarr + use its defaults. + + Raises + ------ + ValueError + If filters contain non-ArrayArrayCodec instances, if more than one + ArrayBytesCodec is provided, or if an unrecognised codec type is passed. + """ + from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec, BytesBytesCodec + + if filters is None: + filters = [] + if not isinstance(filters, (list, tuple)): + filters = [filters] + + if compressors is None: + compressors = [] + if not isinstance(compressors, (list, tuple)): + compressors = [compressors] + + for f in filters: + if not isinstance(f, ArrayArrayCodec): + raise ValueError(f"All filters must be ArrayArrayCodec instances, got {type(f)}") + + serializers = [c for c in compressors if isinstance(c, ArrayBytesCodec)] + byte_compressors = [c for c in compressors if isinstance(c, BytesBytesCodec)] + invalid = [c for c in compressors if not isinstance(c, (ArrayBytesCodec, BytesBytesCodec))] + + if invalid: + raise ValueError( + f"Compressors must be ArrayBytesCodec or BytesBytesCodec instances, got {[type(c) for c in invalid]}" + ) + if len(serializers) > 1: + raise ValueError("Only one ArrayBytesCodec (serializer) is allowed in the codec pipeline.") + + codec_kwargs = {} + codec_kwargs["filters"] = filters + codec_kwargs["serializer"] = serializers[0] if len(serializers) == 1 else "auto" + codec_kwargs["compressors"] = byte_compressors + return codec_kwargs diff --git a/src/spikeinterface/core/zarrextractors.py b/src/spikeinterface/core/zarrextractors.py index aa33176cfe..bfe70691ae 100644 --- a/src/spikeinterface/core/zarrextractors.py +++ b/src/spikeinterface/core/zarrextractors.py @@ -10,16 +10,25 @@ from .base import minimum_spike_dtype, _get_class_from_string from .baserecording import BaseRecording, BaseRecordingSegment from .basesorting import BaseSorting, SpikeVectorSortingSegment -from .job_tools import split_job_kwargs from .core_tools import ( define_function_from_class, check_json, + is_path_remote, retrieve_importing_provenance, is_path_remote, - _check_zarr_write_is_supported, ) +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 .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 +# improves compression of the sorted sample_index. zarr warns every time the codec is built, +# including on every metadata read, so we silence it here. +warnings.filterwarnings( + "ignore", + message="Numcodecs codecs are not in the Zarr version 3 specification", +) + def super_zarr_open(folder_path: str | Path, mode: str = "r", storage_options: dict | None = None): """ @@ -55,11 +64,11 @@ def super_zarr_open(folder_path: str | Path, mode: str = "r", storage_options: d import zarr # if mode is append or read/write, we try to open the folder with zarr.open - # since zarr.open_consolidated does not support creating new groups/datasets + # In zarr v3, we use use_consolidated parameter instead of open_consolidated if mode in ("a", "r+"): - open_funcs = (zarr.open,) + use_consolidated_options = (False,) else: - open_funcs = (zarr.open_consolidated, zarr.open) + use_consolidated_options = (True, False) # if storage_options is None, we try to open the folder with and without anonymous access # if storage_options is not None, we try to open the folder with the given storage options @@ -70,13 +79,16 @@ def super_zarr_open(folder_path: str | Path, mode: str = "r", storage_options: d root = None exception = None - if is_path_remote(folder_path): - for open_func in open_funcs: + if is_path_remote(str(folder_path)): + from zarr.storage import FsspecStore + + for use_consolidated in use_consolidated_options: if root is not None: break for storage_options in storage_options_to_test: try: - root = open_func(str(folder_path), mode=mode, storage_options=storage_options) + store = FsspecStore.from_url(str(folder_path), storage_options=storage_options) + root = zarr.open(store, mode=mode, use_consolidated=use_consolidated) break except Exception as e: exception = e @@ -84,11 +96,9 @@ def super_zarr_open(folder_path: str | Path, mode: str = "r", storage_options: d else: if not Path(folder_path).is_dir(): raise ValueError(f"Folder {folder_path} does not exist") - # zarr>=3 refuses storage_options for a local path, even an empty dict - local_kwargs = dict(storage_options=storage_options) if storage_options else dict() - for open_func in open_funcs: + for use_consolidated in use_consolidated_options: try: - root = open_func(str(folder_path), mode=mode, **local_kwargs) + root = zarr.open(str(folder_path), mode=mode, use_consolidated=use_consolidated) break except Exception as e: exception = e @@ -139,7 +149,8 @@ def __init__( assert sampling_frequency is not None, "'sampling_frequency' attiribute not found!" assert num_segments is not None, "'num_segments' attiribute not found!" - channel_ids = np.array(channel_ids) + # zarr returns vlen-utf8 as StringDType (numpy 2.0); convert via list to classic unicode array. + channel_ids = np.array(channel_ids.tolist()) dtype = self._root["traces_seg0"].dtype @@ -177,7 +188,7 @@ def __init__( if load_compression_ratio: nbytes_segment = self._root[trace_name].nbytes - nbytes_stored_segment = self._root[trace_name].nbytes_stored + nbytes_stored_segment = self._root[trace_name].nbytes_stored() if nbytes_stored_segment > 0: cr_by_segment[segment_index] = nbytes_segment / nbytes_stored_segment else: @@ -218,11 +229,15 @@ def __init__( # load properties if "properties" in self._root: prop_group = self._root["properties"] - for key in prop_group.keys(): + for key, values in iterate_zarr_group(prop_group): # Skip contact_vector property since it is not used anymore to represent probegroup if key == "contact_vector": continue - values = self._root["properties"][key] + values = values[:] + # zarr returns vlen-utf8 as StringDType (numpy 2.0); convert via list to classic unicode array. + if hasattr(values.dtype, "na_object") or values.dtype.kind == "O": + if values.size > 0 and isinstance(values.tolist()[0], str): + values = np.array(values.tolist()) self.set_property(key, values) # load annotations @@ -573,32 +588,31 @@ def __init__( assert sampling_frequency is not None, "'sampling_frequency' attiribute not found!" assert num_segments is not None, "'num_segments' attiribute not found!" - unit_ids = np.array(unit_ids) + # zarr returns vlen-utf8 as StringDType (numpy 2.0); convert via list to classic unicode array. + unit_ids = np.array(unit_ids.tolist()) assert "spikes" in self._root.keys(), "'spikes' dataset not found!" spikes_group = self._root["spikes"] - segment_slices_list = spikes_group["segment_slices"][:] BaseSorting.__init__(self, sampling_frequency, unit_ids) + segment_slices_list = np.asarray(spikes_group["segment_slices"][:], dtype="int64") + if lazy_spike_vector: spikes = ZarrSpikeVector(spikes_group, segment_slices_list) else: - # Materialize the spike vector in memory and sort it by (segment_index, sample_index, unit_index) spikes = np.zeros(spikes_group["sample_index"].shape[0], dtype=minimum_spike_dtype) spikes["sample_index"] = spikes_group["sample_index"][:] spikes["unit_index"] = spikes_group["unit_index"][:] for i, (start, end) in enumerate(segment_slices_list): spikes["segment_index"][start:end] = i - # we do not need to lexsort at init (very high cost) because there already sorted by frame before to be saved. - # In version 0.104.X this was fully lexsorted, but we don't need it anymore because it's only important in the context of SpikeVectorBased extensions in the SortingAnalyzer, which stores its own copy of the Sorting object. This makes the extension data and the spike vector always matching their order. - # spikes = spikes[np.lexsort((spikes["unit_index"], spikes["sample_index"], spikes["segment_index"]))] self._lazy_spike_vector = lazy_spike_vector self._spikes_group = spikes_group self._sample_index_search = None + self._cached_spike_vector = spikes # pre-populate segment slices so _get_spike_vector_segment_slices() never # needs to materialise the full segment_index array - self._cached_spike_vector_segment_slices = np.asarray(segment_slices_list, dtype="int64") + self._cached_spike_vector_segment_slices = segment_slices_list for segment_index in range(num_segments): soring_segment = SpikeVectorSortingSegment(spikes, segment_index, unit_ids) @@ -607,8 +621,12 @@ def __init__( # load properties if "properties" in self._root: prop_group = self._root["properties"] - for key in prop_group.keys(): - values = self._root["properties"][key] + for key, values in iterate_zarr_group(prop_group): + values = values[:] + # zarr returns vlen-utf8 as StringDType (numpy 2.0); convert via list to classic unicode array. + if hasattr(values.dtype, "na_object") or values.dtype.kind == "O": + if values.size > 0 and isinstance(values.tolist()[0], str): + values = np.array(values.tolist()) self.set_property(key, values) # load annotations @@ -731,7 +749,6 @@ def create_zarr_path_for_write(folder_path: str | Path, overwrite: bool = False) folder_path : str or Path Path to the zarr root file """ - _check_zarr_write_is_supported() if not is_path_remote(folder_path): folder_path = Path(folder_path) folder_path = folder_path.with_suffix(".zarr") @@ -764,9 +781,16 @@ def get_default_zarr_compressor(clevel: int = 5): Blosc.compressor The compressor object that can be used with the save to zarr function """ - from numcodecs import Blosc + from zarr.codecs import BloscCodec + + return BloscCodec(cname="zstd", clevel=clevel, shuffle="bitshuffle") + - return Blosc(cname="zstd", clevel=clevel, shuffle=Blosc.BITSHUFFLE) +def _has_string_fields(dtype: np.dtype) -> bool: + """Return True if dtype is or contains fixed-length unicode (U) sub-fields.""" + if dtype.names: + return any(_has_string_fields(dtype.fields[name][0]) for name in dtype.names) + return dtype.kind == "U" def add_properties_and_annotations(zarr_group: zarr.Group, recording_or_sorting: BaseRecording | BaseSorting): @@ -777,7 +801,20 @@ def add_properties_and_annotations(zarr_group: zarr.Group, recording_or_sorting: if values.dtype.kind == "O": warnings.warn(f"Property {key} not saved because it is a python Object type") continue - prop_group.create_dataset(name=key, data=values, compressor=None) + if values.dtype.names and _has_string_fields(values.dtype): + # Structured arrays with unicode sub-fields have no stable zarr v3 spec; skip them. + # Probe geometry (contact_vector) is already persisted via zarr_group.attrs["probe"]. + warnings.warn( + f"Property '{key}' not saved because it is a structured array with unicode fields, " + "which do not have a stable zarr V3 specification." + ) + continue + # Use variable-length UTF-8 (stable zarr v3 spec) for unicode arrays. + if values.dtype.kind == "U": + arr = prop_group.create_array(name=key, shape=values.shape, dtype=str, compressors=None) + arr[:] = values + else: + prop_group.create_array(name=key, data=values, compressors=None) # save annotations zarr_group.attrs["annotations"] = check_json(recording_or_sorting._annotations) @@ -796,9 +833,21 @@ def add_sorting_to_zarr_group( zarr_group : zarr.Group The zarr group kwargs : dict - Other arguments passed to the zarr compressor + Other arguments passed to the zarr writer: + + * "target_chunk_size_bytes" : int or None, default: 10 * 1024 * 1024 (10 MB) + The target chunk size for each spike field in the zarr arrays. + If explicitly set to None, no chunking is used. + * "compressors" : zarr compressors or str or None, default: "default" + The compressor used for each spike field in the zarr arrays. + If "default", the SpikeInterface default zarr compressor is used (Blosc-zstd, level 5, bitshuffle). + * "filters" : list of zarr filters or None, default: None + The list of zarr filters to apply to each spike field in the zarr arrays. + If None, a Delta filter is applied. + * "shard_factor" : int or None, default: None + If given, a shard will be shard_factor * chunk size. """ - from numcodecs import Delta + from zarr.codecs import Delta if sorting.check_serializability("json"): zarr_group.attrs["provenance"] = check_json(sorting.to_dict(recursive=True, relative_to=relative_to)) @@ -808,35 +857,62 @@ def add_sorting_to_zarr_group( num_segments = sorting.get_num_segments() zarr_group.attrs["sampling_frequency"] = float(sorting.sampling_frequency) zarr_group.attrs["num_segments"] = int(num_segments) - zarr_group.create_dataset(name="unit_ids", data=sorting.unit_ids, compressor=None) + # Use variable-length UTF-8 (stable zarr v3 spec) instead of fixed-length unicode. + unit_ids = sorting.unit_ids + if unit_ids.dtype.kind in ("U", "S"): + unit_ids = unit_ids.astype("T") + zarr_group.create_array(name="unit_ids", data=unit_ids, compressors=None) + + compressors = kwargs.get("compressors", "default") + if isinstance(compressors, str) and compressors == "default": + compressors = get_default_zarr_compressor() + + # Save the full structured spike array. The "segment_index" field is additionally stored as + # "segment_slices" (an attribute on the spikes array) which contains the start and end indices + # of spikes for each segment, to allow efficient per-segment access without scanning the array. + spikes = sorting.to_spike_vector() - compressor = kwargs.get("compressor", get_default_zarr_compressor()) + chunks = None + shards = None + target_chunk_size_bytes = kwargs.get("target_chunk_size_bytes", 10 * 1024 * 1024) - # save sub fields + # Save sub fields: in this case chunks and shards are set per field spikes_group = zarr_group.create_group(name="spikes") spikes = sorting.to_spike_vector() for field in spikes.dtype.fields: if field != "segment_index": - spikes_group.create_dataset( - name=field, - data=spikes[field], - compressor=compressor, - filters=[Delta(dtype=spikes[field].dtype)], - ) - else: - segment_slices = [] - for segment_index in range(num_segments): - i0, i1 = np.searchsorted(spikes["segment_index"], [segment_index, segment_index + 1]) - segment_slices.append([i0, i1]) - spikes_group.create_dataset(name="segment_slices", data=segment_slices, compressor=None) + if target_chunk_size_bytes is not None: + spike_num_bytes = spikes[field].dtype.itemsize + target_chunk_size = target_chunk_size_bytes // spike_num_bytes + chunks = (target_chunk_size,) + shard_factor = kwargs.get("shard_factor") + if shard_factor is not None: + shards = (target_chunk_size * shard_factor,) + else: + chunks = (len(spikes),) + filters = kwargs.get("filters", None) + if filters is None: + filters = Delta(dtype=spikes[field].dtype.str) + codec_kwargs = build_codec_pipeline(filters=filters, compressors=compressors) + spikes_group.create_array(name=field, data=spikes[field], chunks=chunks, shards=shards, **codec_kwargs) + + segment_slices = [] + if sorting._cached_spike_vector_segment_slices is not None: + segment_slices = sorting._cached_spike_vector_segment_slices + else: + for segment_index in range(num_segments): + i0, i1 = np.searchsorted(spikes["segment_index"], [segment_index, segment_index + 1]) + segment_slices.append([i0, i1]) + segment_slices = np.array(segment_slices, dtype="int64") + spikes_group.create_array(name="segment_slices", data=segment_slices, compressors=None) # first sample_index of every zarr chunk: lets a lazy reader search sample_index # one chunk at a time (see ZarrSampleIndexSearch) instead of materialising it chunk_length = spikes_group["sample_index"].chunks[0] - spikes_group.create_dataset( + spikes_group.create_array( name="sample_index_chunk_firsts", data=np.asarray(spikes["sample_index"][::chunk_length], dtype="int64"), - compressor=None, + compressors=None, ) add_properties_and_annotations(zarr_group, sorting) @@ -851,6 +927,7 @@ def add_recording_to_zarr_group( **kwargs, ): zarr_kwargs, job_kwargs = split_job_kwargs(kwargs) + job_kwargs = fix_job_kwargs(job_kwargs) if recording.check_serializability("json"): zarr_group.attrs["provenance"] = check_json( @@ -862,7 +939,11 @@ def add_recording_to_zarr_group( # save data (done the subclass) zarr_group.attrs["sampling_frequency"] = float(recording.get_sampling_frequency()) zarr_group.attrs["num_segments"] = int(recording.get_num_segments()) - zarr_group.create_dataset(name="channel_ids", data=recording.get_channel_ids(), compressor=None) + # Use variable-length UTF-8 (stable zarr v3 spec) instead of fixed-length unicode. + channel_ids = recording.channel_ids + if channel_ids.dtype.kind in ("U", "S"): + channel_ids = channel_ids.astype("T") + arr = zarr_group.create_array(name="channel_ids", data=channel_ids, compressors=None) dataset_paths = [f"traces_seg{i}" for i in range(recording.get_num_segments())] dataset_timestamps_paths: list | None = None if any(recording.has_time_vector(i) for i in range(recording.get_num_segments())): @@ -874,16 +955,28 @@ def add_recording_to_zarr_group( dataset_timestamps_paths.append(None) dtype = recording.get_dtype() if dtype is None else dtype - channel_chunk_size = zarr_kwargs.get("channel_chunk_size", None) - global_compressor = zarr_kwargs.pop("compressor", get_default_zarr_compressor()) + + # Compressors and filters + global_compressor = kwargs.get("compressors") or kwargs.get("compressor") + if global_compressor is None: + global_compressor = get_default_zarr_compressor() compressor_by_dataset = zarr_kwargs.pop("compressor_by_dataset", {}) global_filters = zarr_kwargs.pop("filters", None) filters_by_dataset = zarr_kwargs.pop("filters_by_dataset", {}) - compressor_traces = compressor_by_dataset.get("traces", global_compressor) filters_traces = filters_by_dataset.get("traces", global_filters) compressor_times = compressor_by_dataset.get("times", global_compressor) filters_times = filters_by_dataset.get("times", global_filters) + channel_chunk_size = zarr_kwargs.get("channel_chunk_size") + + chunks, shards, job_kwargs = adjust_chunks_shards_and_job_kwargs( + chunks=zarr_kwargs.get("chunks"), + extra_chunks=(channel_chunk_size,) if channel_chunk_size is not None else None, + shards=zarr_kwargs.get("shards"), + shard_factor=zarr_kwargs.get("shard_factor"), + job_kwargs=job_kwargs, + time_series=recording, + ) _write_time_series_to_zarr( time_series=recording, @@ -893,10 +986,11 @@ def add_recording_to_zarr_group( compressor_data=compressor_traces, filters_data=filters_traces, dtype=dtype, - extra_chunks=(channel_chunk_size,), + chunks=chunks, + shards=shards, compressor_times=compressor_times, filters_times=filters_times, - verbose=False, + verbose=verbose, **job_kwargs, ) diff --git a/src/spikeinterface/extractors/nwbextractors.py b/src/spikeinterface/extractors/nwbextractors.py index 5c6f2ea6ce..e103c06392 100644 --- a/src/spikeinterface/extractors/nwbextractors.py +++ b/src/spikeinterface/extractors/nwbextractors.py @@ -137,12 +137,7 @@ def read_nwbfile( if file_path is None and file is None: raise ValueError("Provide either file_path or file") - if ( - backend == "zarr" - and file is None - and _get_zarr_format(file_path, stream_mode, storage_options) == 2 - and _has_zarr_python_3() - ): + if backend == "zarr" and file is None and _get_zarr_format(file_path, stream_mode, storage_options) == 2: from hdmf_zarr import NWBZarrV2IO # hdmf-zarr>=0.14 reads zarr v2 files with a separate read-only IO. Files written by hdmf-zarr<0.14 @@ -332,6 +327,29 @@ def _get_backend_from_local_file(file_path: str | Path) -> str: return backend +def _zarr_group_child_names(group): + """ + Return the names of the immediate children of a zarr group without parsing their metadata. + + zarr-python 3.x eagerly reads and validates every child's metadata when iterating + ``group.keys()``. Some arrays written by hdmf-zarr (e.g. variable-length string columns + with an integer ``fill_value``) cannot be parsed by zarr-python 3.x and make the whole + iteration fail. Listing the store directly avoids touching the children's metadata. + """ + if hasattr(group, "store_path"): # zarr v3 + from zarr.core.sync import sync + + async def _collect(): + return [key async for key in group.store.list_dir(group.path)] + + # Filter out this group's own metadata files (".zgroup", ".zattrs", "zarr.json", ...). + # list_dir does not guarantee an order, so sort for deterministic traversal (matches h5py). + names = [name for name in sync(_collect()) if not name.startswith(".") and name != "zarr.json"] + return sorted(names) + else: # zarr v2 + return list(group.keys()) + + def _get_zarr_format(file_path: str | Path, stream_mode=None, storage_options: dict | None = None) -> int | None: """ Return the zarr format (2 or 3) of a zarr folder, local or streamed with stream_mode="zarr", from its @@ -355,12 +373,6 @@ def _get_zarr_format(file_path: str | Path, stream_mode=None, storage_options: d return None -def _has_zarr_python_3() -> bool: - import zarr - - return int(zarr.__version__.split(".")[0]) >= 3 - - def _find_neurodata_type_from_backend(group, path="", result=None, neurodata_type="ElectricalSeries", backend="hdf5"): """ Recursively searches for groups with the specified neurodata_type hdf5 or zarr object, @@ -370,17 +382,24 @@ def _find_neurodata_type_from_backend(group, path="", result=None, neurodata_typ import h5py group_class = h5py.Group + child_names = list(group.keys()) else: import zarr group_class = zarr.Group + child_names = _zarr_group_child_names(group) if result is None: result = [] - # zarr>=3 groups have no `items()`, `keys()` works for h5py and both zarr versions - for neurodata_name in group.keys(): - value = group[neurodata_name] + for neurodata_name in child_names: + try: + value = group[neurodata_name] + except Exception: + # Skip children whose metadata cannot be parsed (e.g. hdmf-zarr arrays with a + # fill_value that zarr-python 3.x rejects). These are never groups, so skipping + # them is safe when searching for a neurodata_type. + continue # Check if it's a group and if it has the neurodata_type if isinstance(value, group_class): current_path = f"{path}/{neurodata_name}" if path else neurodata_name @@ -395,14 +414,16 @@ def _find_neurodata_type_from_backend(group, path="", result=None, neurodata_typ def _retrieve_electrodes_indices_from_electrical_series_backend(open_file, electrical_series, backend="hdf5"): """ Retrieves the indices of the electrodes from the electrical series. - For the Zarr backend, the electrodes are stored in the electrical_series.attrs["zarr_link"]. + For the Zarr backend, the electrodes are stored in the electrical_series.attrs["_LINKS"] + or legacy electrical_series.attrs["zarr_link"]. + See https://github.com/hdmf-dev/hdmf-zarr/pull/336 """ if "electrodes" not in electrical_series: if backend == "zarr": import zarr - # links must be resolved, hdmf-zarr>=0.14 stores them under "_LINKS" instead of "zarr_link" - zarr_links = electrical_series.attrs.get("zarr_link", electrical_series.attrs.get("_LINKS")) + # links must be resolved + zarr_links = electrical_series.attrs.get("_LINKS", electrical_series.attrs.get("zarr_link", [])) electrodes_path = None for zarr_link in zarr_links: if zarr_link["name"] == "electrodes": @@ -454,11 +475,7 @@ def _reading_method(use_pynwb, file_path=None, file=None, stream_mode=None, stor if use_pynwb: return "use_pynwb" backend = _NWBReader._storage_backend(file_path, file, stream_mode) - if ( - backend == "zarr" - and _has_zarr_python_3() - and _get_zarr_format(file_path, stream_mode, storage_options) == 2 - ): + if backend == "zarr" and _get_zarr_format(file_path, stream_mode, storage_options) == 2: if not HAVE_PYNWB or importlib.util.find_spec("hdmf_zarr") is None: raise ImportError( "To read zarr v2 NWB files with zarr-python 3, install pynwb and hdmf-zarr>=0.14, which requires " diff --git a/src/spikeinterface/extractors/tests/test_nwbextractors.py b/src/spikeinterface/extractors/tests/test_nwbextractors.py index 381c4f6161..b442216d86 100644 --- a/src/spikeinterface/extractors/tests/test_nwbextractors.py +++ b/src/spikeinterface/extractors/tests/test_nwbextractors.py @@ -1,3 +1,4 @@ +import importlib.util import unittest from pathlib import Path @@ -158,21 +159,26 @@ def nwbfile_with_ecephys_content(): return nwbfile +# hdmf-zarr>=0.14 (zarr v3) requires python>=3.12, so it may be missing +NWB_BACKENDS = ["hdf5"] + (["zarr"] if importlib.util.find_spec("hdmf_zarr") else []) + + def _generate_nwbfile(backend, file_path): from pynwb import NWBHDF5IO - from hdmf_zarr import NWBZarrIO nwbfile = nwbfile_with_ecephys_content() if backend == "hdf5": io_class = NWBHDF5IO elif backend == "zarr": + from hdmf_zarr import NWBZarrIO + io_class = NWBZarrIO with io_class(str(file_path), mode="w") as io: io.write(nwbfile) return file_path, nwbfile -@pytest.fixture(scope="module", params=["hdf5", "zarr"]) +@pytest.fixture(scope="module", params=NWB_BACKENDS) def generate_nwbfile(request, tmp_path_factory): nwbfile = nwbfile_with_ecephys_content() backend = request.param @@ -1158,19 +1164,20 @@ def nwbfile_with_timeseries(): def _generate_nwbfile_with_time_series(backend, file_path): from pynwb import NWBHDF5IO - from hdmf_zarr import NWBZarrIO nwbfile = nwbfile_with_timeseries() if backend == "hdf5": io_class = NWBHDF5IO elif backend == "zarr": + from hdmf_zarr import NWBZarrIO + io_class = NWBZarrIO with io_class(str(file_path), mode="w") as io: io.write(nwbfile) return file_path, nwbfile -@pytest.fixture(scope="module", params=["hdf5", "zarr"]) +@pytest.fixture(scope="module", params=NWB_BACKENDS) def generate_nwbfile_with_time_series(request, tmp_path_factory): backend = request.param nwbfile_path = tmp_path_factory.mktemp("nwb_tests_directory") / "test.nwb" diff --git a/src/spikeinterface/extractors/tests/test_nwbextractors_streaming.py b/src/spikeinterface/extractors/tests/test_nwbextractors_streaming.py index 161040cbaf..7d43253acf 100644 --- a/src/spikeinterface/extractors/tests/test_nwbextractors_streaming.py +++ b/src/spikeinterface/extractors/tests/test_nwbextractors_streaming.py @@ -1,5 +1,9 @@ +import sys from pathlib import Path import pickle +from importlib.metadata import version, PackageNotFoundError + +from packaging.version import parse import pytest import numpy as np @@ -9,6 +13,13 @@ from spikeinterface.core.testing import check_recordings_equal, check_sortings_equal from spikeinterface.extractors.extractor_classes import NwbRecordingExtractor, NwbSortingExtractor +py_version = sys.version_info + +try: + hdmf_zarr_version = parse(version("hdmf_zarr")) +except PackageNotFoundError: + hdmf_zarr_version = None + @pytest.mark.streaming_extractors @pytest.mark.parametrize("cache", [True, False]) # Test with and without cache @@ -185,6 +196,10 @@ def test_sorting_s3_nwb_remfile(tmp_path): @pytest.mark.streaming_extractors +@pytest.mark.skipif(py_version < (3, 12), reason="requires Python >= 3.12") +@pytest.mark.skipif( + hdmf_zarr_version is None or hdmf_zarr_version < parse("0.14.1"), reason="requires hdmf_zarr >= 0.14.1" +) def test_sorting_s3_nwb_zarr(tmp_path): file_path = ( "s3://aind-open-data/ecephys_625749_2022-08-03_15-15-06_nwb_2023-05-16_16-34-55/" diff --git a/src/spikeinterface/postprocessing/tests/common_extension_tests.py b/src/spikeinterface/postprocessing/tests/common_extension_tests.py index fb7770befe..da164a48b0 100644 --- a/src/spikeinterface/postprocessing/tests/common_extension_tests.py +++ b/src/spikeinterface/postprocessing/tests/common_extension_tests.py @@ -9,7 +9,6 @@ estimate_sparsity, ) from spikeinterface.core.sortinganalyzer import get_extension_class -from spikeinterface.core.core_tools import _is_zarr_write_supported extensions_which_allow_unit_ids = ["unit_locations"] extensions_with_unit_by_unit_data = ["correlograms", "template_similarity"] @@ -242,8 +241,7 @@ def run_extension_tests(self, extension_class, params): of interest with the passed parameters. Will perform tests for sparsity and format. """ - # TODO: remove once writing to zarr is supported with zarr>=3 - formats = ("memory", "binary_folder", "zarr") if _is_zarr_write_supported() else ("memory", "binary_folder") + formats = ("memory", "binary_folder", "zarr") for sparse in (True, False): for format in formats: print("sparse", sparse, format) diff --git a/src/spikeinterface/postprocessing/tests/test_multi_extensions.py b/src/spikeinterface/postprocessing/tests/test_multi_extensions.py index 6ae72e374d..17f51fd1ce 100644 --- a/src/spikeinterface/postprocessing/tests/test_multi_extensions.py +++ b/src/spikeinterface/postprocessing/tests/test_multi_extensions.py @@ -133,9 +133,7 @@ def dataset_to_split(create_cache_folder): @pytest.mark.parametrize("lazy", [False, True]) @pytest.mark.parametrize("sparse", [False, True]) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) def test_SortingAnalyzer_merge_all_extensions(dataset_to_merge, lazy, sparse, format, tmp_path): if format == "memory" and lazy: pytest.skip("lazy has no effect for format='memory' (nothing on disk to load lazily)") @@ -164,21 +162,14 @@ def test_SortingAnalyzer_merge_all_extensions(dataset_to_merge, lazy, sparse, fo sorting_analyzer.save_as(folder=tmp_path / analyzer_folder_name, format=format) sorting_analyzer = load_sorting_analyzer(tmp_path / analyzer_folder_name, format=format, lazy=lazy) - # TODO: still some UserWarnings for n_jobs, where from? - t0 = time.perf_counter() analyzer_merged_hard = sorting_analyzer.merge_units( merge_unit_groups=merges, censor_ms=2, merging_mode="hard", n_jobs=1 ) - t_hard = time.perf_counter() - t0 - t0 = time.perf_counter() analyzer_merged_soft = sorting_analyzer.merge_units( merge_unit_groups=merges, censor_ms=2, merging_mode="soft", sparsity_overlap=0.0, n_jobs=1 ) - t_soft = time.perf_counter() - t0 - # soft must faster - assert t_soft < t_hard np.testing.assert_array_equal(analyzer_merged_hard.unit_ids, analyzer_merged_soft.unit_ids) new_unit_ids = list( np.arange(max(split_unit_ids.astype(int)) + 1, max(split_unit_ids.astype(int)) + 1 + len(merges)) @@ -267,9 +258,7 @@ def test_SortingAnalyzer_merge_all_extensions(dataset_to_merge, lazy, sparse, fo @pytest.mark.parametrize("lazy", [False, True]) @pytest.mark.parametrize("sparse", [False, True]) -@pytest.mark.parametrize( - "format", ["memory", "binary_folder", pytest.param("zarr", marks=pytest.mark.requires_zarr_write)] -) +@pytest.mark.parametrize("format", ["memory", "binary_folder", "zarr"]) def test_SortingAnalyzer_split_all_extensions(dataset_to_split, lazy, sparse, format, tmp_path): if format == "memory" and lazy: pytest.skip("lazy has no effect for format='memory' (nothing on disk to load lazily)") diff --git a/src/spikeinterface/preprocessing/tests/test_pipeline.py b/src/spikeinterface/preprocessing/tests/test_pipeline.py index 7b53b0ab92..1ea89ec749 100644 --- a/src/spikeinterface/preprocessing/tests/test_pipeline.py +++ b/src/spikeinterface/preprocessing/tests/test_pipeline.py @@ -2,7 +2,6 @@ from spikeinterface.core.testing import check_recordings_equal from spikeinterface.core import create_sorting_analyzer -from spikeinterface.core.core_tools import _is_zarr_write_supported from spikeinterface.generation import generate_recording, generate_ground_truth_recording from spikeinterface.preprocessing import ( apply_preprocessing_pipeline, @@ -206,6 +205,7 @@ def test_loading_from_analyzer(create_cache_folder): cache_folder = create_cache_folder recording, sorting = generate_ground_truth_recording() + # Make it JSON-serializable by saving it to a folder and reloading it recording = recording.save(folder=cache_folder / "recording") preprocessing_dict = {"common_reference": {}, "highpass_filter": {"freq_min": 301.0}} @@ -219,13 +219,11 @@ def test_loading_from_analyzer(create_cache_folder): pp_recording_from_binary = apply_preprocessing_pipeline(recording, pp_list_from_binary) check_recordings_equal(pp_recording, pp_recording_from_binary) - # TODO: remove once writing to zarr is supported with zarr>=3 - if _is_zarr_write_supported(): - analyzer_zarr_folder = cache_folder / "zarr_format.zarr" - _ = create_sorting_analyzer(sorting=sorting, recording=pp_recording, format="zarr", folder=analyzer_zarr_folder) - pp_list_from_zarr = get_preprocessing_list_from_analyzer(analyzer_zarr_folder) - pp_recording_from_zarr = apply_preprocessing_pipeline(recording, pp_list_from_zarr) - check_recordings_equal(pp_recording, pp_recording_from_zarr) + analyzer_zarr_folder = cache_folder / "zarr_format.zarr" + _ = create_sorting_analyzer(sorting=sorting, recording=pp_recording, format="zarr", folder=analyzer_zarr_folder) + pp_list_from_zarr = get_preprocessing_list_from_analyzer(analyzer_zarr_folder) + pp_recording_from_zarr = apply_preprocessing_pipeline(recording, pp_list_from_zarr) + check_recordings_equal(pp_recording, pp_recording_from_zarr) def test_pipeline_recording_arg_substitution(create_cache_folder): diff --git a/src/spikeinterface/preprocessing/tests/test_scaling.py b/src/spikeinterface/preprocessing/tests/test_scaling.py index a19d116b16..27f1de8542 100644 --- a/src/spikeinterface/preprocessing/tests/test_scaling.py +++ b/src/spikeinterface/preprocessing/tests/test_scaling.py @@ -1,6 +1,6 @@ import pytest import numpy as np -from spikeinterface.core.testing_tools import generate_recording +from spikeinterface.core.generate import generate_recording from spikeinterface.preprocessing.preprocessing_classes import scale_to_uV, CenterRecording, scale_to_physical_units diff --git a/src/spikeinterface/sorters/internal/lupin.py b/src/spikeinterface/sorters/internal/lupin.py index 6d79bb036d..9cbadce7a5 100644 --- a/src/spikeinterface/sorters/internal/lupin.py +++ b/src/spikeinterface/sorters/internal/lupin.py @@ -11,7 +11,6 @@ ) from spikeinterface.core.job_tools import fix_job_kwargs -from spikeinterface.core.core_tools import _check_zarr_write_is_supported from spikeinterface.preprocessing import bandpass_filter, common_reference, zscore, whiten from spikeinterface.core.base import minimum_spike_dtype @@ -124,11 +123,6 @@ def get_sorter_version(cls): @classmethod def _run_from_folder(cls, sorter_output_folder, params, verbose): - - # TODO: remove once writing to zarr is supported with zarr>=3 - if params["save_array"]: - _check_zarr_write_is_supported() - from spikeinterface.sortingcomponents.tools import get_prototype_and_waveforms_from_recording from spikeinterface.sortingcomponents.matching import find_spikes_from_templates from spikeinterface.sortingcomponents.peak_detection import detect_peaks diff --git a/src/spikeinterface/sorters/internal/tests/test_lupin.py b/src/spikeinterface/sorters/internal/tests/test_lupin.py index c4eb788a6d..f838f9f4eb 100644 --- a/src/spikeinterface/sorters/internal/tests/test_lupin.py +++ b/src/spikeinterface/sorters/internal/tests/test_lupin.py @@ -13,7 +13,7 @@ class LupinSorterCommonTestSuite(SorterCommonTestSuite, unittest.TestCase): SorterClass = LupinSorter # TODO: remove once writing to zarr is supported with zarr>=3 (save_array writes the templates to zarr) - @pytest.mark.requires_zarr_write + def test_with_run(self): super().test_with_run() diff --git a/src/spikeinterface/sorters/internal/tests/test_tridesclous2.py b/src/spikeinterface/sorters/internal/tests/test_tridesclous2.py index cb6ed09ce4..6431532b87 100644 --- a/src/spikeinterface/sorters/internal/tests/test_tridesclous2.py +++ b/src/spikeinterface/sorters/internal/tests/test_tridesclous2.py @@ -13,7 +13,7 @@ class Tridesclous2SorterCommonTestSuite(SorterCommonTestSuite, unittest.TestCase SorterClass = Tridesclous2Sorter # TODO: remove once writing to zarr is supported with zarr>=3 (save_array writes the templates to zarr) - @pytest.mark.requires_zarr_write + def test_with_run(self): super().test_with_run() diff --git a/src/spikeinterface/sorters/internal/tridesclous2.py b/src/spikeinterface/sorters/internal/tridesclous2.py index 55d377d4db..56a506b293 100644 --- a/src/spikeinterface/sorters/internal/tridesclous2.py +++ b/src/spikeinterface/sorters/internal/tridesclous2.py @@ -15,7 +15,6 @@ ) from spikeinterface.core.job_tools import fix_job_kwargs -from spikeinterface.core.core_tools import _check_zarr_write_is_supported from spikeinterface.preprocessing import bandpass_filter, common_reference, zscore, whiten from spikeinterface.core.base import minimum_spike_dtype @@ -118,11 +117,6 @@ def get_sorter_version(cls): @classmethod def _run_from_folder(cls, sorter_output_folder, params, verbose): - - # TODO: remove once writing to zarr is supported with zarr>=3 - if params["save_array"]: - _check_zarr_write_is_supported() - from spikeinterface.sortingcomponents.matching import find_spikes_from_templates from spikeinterface.sortingcomponents.peak_detection import detect_peaks from spikeinterface.sortingcomponents.peak_selection import select_peaks diff --git a/src/spikeinterface/sorters/tests/test_launcher.py b/src/spikeinterface/sorters/tests/test_launcher.py index 35db70e677..6619431866 100644 --- a/src/spikeinterface/sorters/tests/test_launcher.py +++ b/src/spikeinterface/sorters/tests/test_launcher.py @@ -52,7 +52,6 @@ def job_list(create_cache_folder): return get_job_list(folder) -@pytest.mark.requires_zarr_write def test_run_sorter_jobs_loop(job_list): sortings = run_sorter_jobs(job_list, engine="loop", return_output=True) print(sortings) @@ -202,7 +201,6 @@ def test_run_sorter_jobs_slurm_kwargs(mocker, tmp_path, job_list): assert str(tmp_script_folder) in mock_subprocess_run.call_args_list[-1].args[0][5] -@pytest.mark.requires_zarr_write def test_run_sorter_by_property(create_cache_folder): cache_folder = create_cache_folder working_folder1 = cache_folder / "test_run_sorter_by_property_1" diff --git a/src/spikeinterface/sorters/tests/test_runsorter.py b/src/spikeinterface/sorters/tests/test_runsorter.py index 1316430e55..c07cd94c9b 100644 --- a/src/spikeinterface/sorters/tests/test_runsorter.py +++ b/src/spikeinterface/sorters/tests/test_runsorter.py @@ -24,7 +24,6 @@ def generate_recording(create_cache_folder): return _generate_recording(create_cache_folder) -@pytest.mark.requires_zarr_write @pytest.mark.xfail( platform.system() == "Windows" and parse(platform.python_version()) > parse("3.12"), reason="3rd parth threadpoolctl issue: OSError('GetModuleFileNameEx failed')",