Skip to content

Commit

Permalink
feat: Added blosc2 read & write support
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol-G committed Jul 10, 2024
1 parent d8d818e commit f7b9845
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
"numpy",
"blosc2",
]

[project.optional-dependencies]
Expand Down
11 changes: 4 additions & 7 deletions src/napari_blosc2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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``.
Expand Down Expand Up @@ -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 = {}
Expand Down
3 changes: 2 additions & 1 deletion src/napari_blosc2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/napari_blosc2/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']

0 comments on commit f7b9845

Please sign in to comment.