From f7b98459fac8e26d352f69696a5c0754e9191a23 Mon Sep 17 00:00:00 2001 From: Karol Gotkowski Date: Wed, 10 Jul 2024 17:41:00 +0200 Subject: [PATCH] feat: Added blosc2 read & write support --- pyproject.toml | 2 +- src/napari_blosc2/_reader.py | 11 ++++------- src/napari_blosc2/_writer.py | 3 ++- src/napari_blosc2/napari.yaml | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 03f3ca6..5aa1915 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ classifiers = [ ] requires-python = ">=3.9" dependencies = [ - "numpy", + "blosc2", ] [project.optional-dependencies] diff --git a/src/napari_blosc2/_reader.py b/src/napari_blosc2/_reader.py index 8d93ff9..fdb727d 100644 --- a/src/napari_blosc2/_reader.py +++ b/src/napari_blosc2/_reader.py @@ -5,6 +5,7 @@ implement multiple readers or even other plugin contributions. see: https://napari.org/stable/plugins/guides.html?#readers """ +import blosc2 import numpy as np @@ -29,7 +30,7 @@ def napari_get_reader(path): path = path[0] # if we know we cannot read the file, we immediately return None. - if not path.endswith(".npy"): + if not path.endswith(".b2nd"): return None # otherwise we return the *function* that can read ``path``. @@ -58,12 +59,8 @@ def reader_function(path): layer. Both "meta", and "layer_type" are optional. napari will default to layer_type=="image" if not provided """ - # handle both a string and a list of strings - paths = [path] if isinstance(path, str) else path - # load all files into array - arrays = [np.load(_path) for _path in paths] - # stack arrays into single array - data = np.squeeze(np.stack(arrays)) + data = blosc2.open(urlpath=path, mode='r', mmap_mode='r') + data = data[...] # optional kwargs for the corresponding viewer.add_* method add_kwargs = {} diff --git a/src/napari_blosc2/_writer.py b/src/napari_blosc2/_writer.py index e67fcc9..94b53e3 100644 --- a/src/napari_blosc2/_writer.py +++ b/src/napari_blosc2/_writer.py @@ -9,6 +9,7 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any, List, Sequence, Tuple, Union +import blosc2 if TYPE_CHECKING: DataType = Union[Any, Sequence[Any]] @@ -33,7 +34,7 @@ def write_single_image(path: str, data: Any, meta: dict) -> List[str]: [path] : A list containing the string path to the saved file. """ - # implement your writer logic here ... + blosc2.asarray(array=data, urlpath=path, meta=meta) # return path to any file(s) that were successfully written return [path] diff --git a/src/napari_blosc2/napari.yaml b/src/napari_blosc2/napari.yaml index 45f5467..c339e55 100644 --- a/src/napari_blosc2/napari.yaml +++ b/src/napari_blosc2/napari.yaml @@ -18,11 +18,11 @@ contributions: readers: - command: napari-blosc2.get_reader accepts_directories: false - filename_patterns: ['*.npy'] + filename_patterns: ['*.b2nd'] writers: - command: napari-blosc2.write_multiple layer_types: ['image*','labels*'] filename_extensions: [] - command: napari-blosc2.write_single_image layer_types: ['image'] - filename_extensions: ['.npy'] + filename_extensions: ['.b2nd']