Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing Tensorstore Dependency #41

Closed
wants to merge 12 commits into from
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- repo: https://github.com/pre-commit/mirrors-yapf
rev: '5441be1' # Use the sha / tag you want to point at
hooks:
- id: yapf
repos:
- repo: https://github.com/pre-commit/mirrors-yapf
rev: '5441be1' # Use the sha / tag you want to point at
hooks:
- id: yapf
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ install_requires =
qtpy
scipy
scikit-image
tensorstore
toolz
zarr

Expand All @@ -53,6 +52,7 @@ testing =
coverage
pytest
pytest-cov
tensorstore
pytest-qt
napari[pyqt5]

Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
# https://github.com/pypa/setuptools_scm
use_scm = {"write_to": "src/zarpaint/_version.py"}

setup(
use_scm_version=use_scm,
)
setup(use_scm_version=use_scm,)
1 change: 0 additions & 1 deletion src/zarpaint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
'set_axis_labels',
'watershed_split',
'add_points_3d_with_alt_click',
'zarr_tensorstore'
'copy_data',
]
1 change: 0 additions & 1 deletion src/zarpaint/_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import argparse
import ast
import sys

from ._zarpaint import correct_labels

parser = argparse.ArgumentParser(
Expand Down
46 changes: 24 additions & 22 deletions src/zarpaint/_normalize.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import numpy as np

_np_uints = {
8: np.uint8,
16: np.uint16,
32: np.uint32,
64: np.uint64,
}
8: np.uint8,
16: np.uint16,
32: np.uint32,
64: np.uint64,
}

_np_ints = {
8: np.int8,
16: np.int16,
32: np.int32,
64: np.int64,
}
8: np.int8,
16: np.int16,
32: np.int32,
64: np.int64,
}

_np_floats = {
16: np.float16,
32: np.float32,
64: np.float64,
}
16: np.float16,
32: np.float32,
64: np.float64,
}

_np_complex = {
64: np.complex64,
128: np.complex128,
}
64: np.complex64,
128: np.complex128,
}

_np_kinds = {
'uint': _np_uints,
'int': _np_ints,
'float': _np_floats,
'complex': _np_complex,
}
'uint': _np_uints,
'int': _np_ints,
'float': _np_floats,
'complex': _np_complex,
}


def _normalize_str_by_bit_depth(dtype_str, kind):
if not any(str.isdigit(c) for c in dtype_str): # Python 'int' or 'float'
Expand All @@ -47,6 +48,7 @@ def _normalize_str_by_bit_depth(dtype_str, kind):
if '64' in dtype_str:
return bit_dict[64]


def normalize_dtype(dtype_spec):
"""Return a proper NumPy type given ~any duck array dtype.
Parameters
Expand Down
31 changes: 21 additions & 10 deletions src/zarpaint/_tests/test_copy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from zarpaint import open_tensorstore
import zarr


def test_copy_data(make_napari_viewer):
viewer = make_napari_viewer()
labels_layer1 = viewer.add_labels(np.random.randint(0, 2**23, size=(10, 20, 30)))
labels_layer1 = viewer.add_labels(
np.random.randint(0, 2**23, size=(10, 20, 30))
)
labels_layer2 = viewer.add_labels(np.zeros((2, 10, 20, 30), dtype=int))
viewer.dims.set_point(axis=0, value=1)
widget = copy_data()
Expand All @@ -18,8 +21,14 @@ def test_copy_data(make_napari_viewer):

def test_copy_data_tensorstore(make_napari_viewer, tmp_path):
viewer = make_napari_viewer()
labels_layer1 = viewer.add_labels(np.random.randint(0, 2**23, size=(10, 20, 30)))
array2 = open_tensorstore(tmp_path/"example.zarr", shape=(2, 10, 20, 30), chunks=(1, 1, 20, 30))
labels_layer1 = viewer.add_labels(
np.random.randint(0, 2**23, size=(10, 20, 30))
)
array2 = open_tensorstore(
tmp_path / "example.zarr",
shape=(2, 10, 20, 30),
chunks=(1, 1, 20, 30)
)
labels_layer2 = viewer.add_labels(array2)
viewer.dims.set_point(axis=0, value=1)
widget = copy_data()
Expand All @@ -30,14 +39,16 @@ def test_copy_data_tensorstore(make_napari_viewer, tmp_path):

def test_copy_data_zarr(make_napari_viewer, tmp_path):
viewer = make_napari_viewer()
labels_layer1 = viewer.add_labels(np.random.randint(0, 2**23, size=(10, 20, 30)))
labels_layer1 = viewer.add_labels(
np.random.randint(0, 2**23, size=(10, 20, 30))
)
array2 = zarr.open(
str(tmp_path/"example.zarr"),
mode='w',
shape=(2, 10, 20, 30),
dtype=np.uint32,
chunks=(1, 1, 20, 30),
)
str(tmp_path / "example.zarr"),
mode='w',
shape=(2, 10, 20, 30),
dtype=np.uint32,
chunks=(1, 1, 20, 30),
)
labels_layer2 = viewer.add_labels(array2)
viewer.dims.set_point(axis=0, value=1)
widget = copy_data()
Expand Down
22 changes: 17 additions & 5 deletions src/zarpaint/_tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import zarr
import numpy as np


def test_reader_pass(tmp_path):
"""
Test that the reader is able to open a test file
Expand All @@ -11,22 +12,33 @@ def test_reader_pass(tmp_path):
test_file = zarr_tensorstore(new_path)
assert test_file is None


def test_reader_return_callable(tmp_path):
"""
Test the the reader returns a valid funciton when opening a file
"""
"""
example_zarr_folder = tmp_path / 'example.zarr'
z1 = zarr.open_array(example_zarr_folder, mode='w', shape=(10000, 10000), chunks=(1000, 1000), dtype='i4', fill_value=0)
z1 = zarr.open_array(
example_zarr_folder,
mode='w',
shape=(10000, 10000),
chunks=(1000, 1000),
dtype='i4',
fill_value=0
)
res = zarr_tensorstore(example_zarr_folder)
assert callable(res)



def test_reader_can_read_and_write_to_file(tmp_path):
"""
Creates a zarr file, writes random data to it, then saves the file. Once saved, the file is then
reopened and the data is compared.
"""
example_zarr_folder = tmp_path / 'example.zarr'
z1 = zarr.open_array(example_zarr_folder, mode='w', shape=(100, 100), chunks=(100, 100))
z1 = zarr.open_array(
example_zarr_folder, mode='w', shape=(100, 100), chunks=(100, 100)
)
z1[:] = np.random.rand(100, 100)

reader_func = zarr_tensorstore(example_zarr_folder)
Expand All @@ -36,5 +48,5 @@ def test_reader_can_read_and_write_to_file(tmp_path):
assert len(layers) == 1

layer_info = layers[0]
assert isinstance(layer_info, Tuple)
assert isinstance(layer_info, Tuple)
np.testing.assert_allclose(np.asarray(layer_info[0]), z1)
9 changes: 3 additions & 6 deletions src/zarpaint/_zarpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ast
import os
import yaml

from magicgui import magic_factory
import dask.array as da
import napari
Expand Down Expand Up @@ -63,7 +62,7 @@ def open_tensorstore(labels_file: pathlib.Path, *, shape=None, chunks=None):
dtype=np.uint32,
chunks=chunks,
)
# read some of the metadata for tensorstore driver from file
# read some metadata for tensorstore driver from file
labels_temp = zarr.open(str(labels_file), mode='r')
metadata = {
'dtype': labels_temp.dtype.str,
Expand Down Expand Up @@ -120,10 +119,8 @@ def create_labels(
else: # use default
chunks = (1,) * (source_image.ndim - 2) + (128, 128)

layer_data = open_tensorstore(
labels_file,
shape=source_image.data.shape,
chunks=chunks,
layer_data = zarr.open(
labels_file, shape=source_image.data.shape, chunks=chunks
)
layer_type = 'labels'
layer_metadata = {
Expand Down
3 changes: 0 additions & 3 deletions src/zarpaint/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ contributions:
- id: zarpaint.watershed_split
title: Split With Watershed…
python_name: zarpaint:watershed_split
- id: zarpaint.get_reader
title: Get Reader
python_name: zarpaint:zarr_tensorstore
- id: zarpaint.copy_data
title: Copy Data…
python_name: zarpaint:copy_data
Expand Down
8 changes: 3 additions & 5 deletions src/zarpaint/reader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

import zarr
import os
import pathlib
from ._zarpaint import open_tensorstore, open_ts_meta
from ._zarpaint import open_ts_meta


def zarr_tensorstore(path: str | pathlib.Path):
if (str(path).endswith('.zarr') and os.path.isdir(path)
and '.zarray' in os.listdir(path)):
return lambda path: [
(open_tensorstore(path), open_ts_meta(path), 'labels')
]
return lambda path: [(zarr.open(path), open_ts_meta(path), 'labels')]