Skip to content

Commit

Permalink
fix edge case in pyramid build
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Sep 27, 2024
1 parent 5a652c2 commit 3d89667
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/ngio/core/image_like_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ def consolidate(self, order: int = 1) -> None:
)
]
)

partial_zoom = partial(zoom, zoom=zoom_factors, order=order)
out_image = da.map_blocks(
partial_zoom,
Expand All @@ -432,7 +431,7 @@ def consolidate(self, order: int = 1) -> None:
chunks=target_image.array.chunks,
)
out_image = out_image.astype(target_image.array.dtype)
# da.to_zarr(out_image, target_image.array, overwrite=True)
target_image.array[...] = out_image.compute()
# compute the transformation

processed_paths.append(target_image)
108 changes: 108 additions & 0 deletions src/ngio/core/label_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""A module to handle OME-NGFF images stored in Zarr format."""

from typing import Literal

import zarr

from ngio._common_types import ArrayLike
from ngio.core.image_handler import Image
from ngio.core.image_like_handler import ImageLike
from ngio.core.roi import WorldCooROI
from ngio.core.utils import create_empty_ome_zarr_label
from ngio.io import StoreLike, StoreOrGroup
from ngio.ngff_meta.fractal_image_meta import LabelMeta, PixelSize
Expand Down Expand Up @@ -57,6 +61,110 @@ def metadata(self) -> LabelMeta:
"""Return the metadata of the image."""
return super().metadata

def get_data_from_roi(
self,
roi: WorldCooROI,
t: int | slice | None = None,
mode: Literal["numpy"] | Literal["dask"] = "numpy",
preserve_dimensions: bool = False,
) -> ArrayLike:
"""Return the label data from a region of interest (ROI).
Args:
roi (WorldCooROI): The region of interest.
t (int | slice | None): The time index or slice.
mode (str): The mode to return the data.
preserve_dimensions (bool): Whether to preserve the dimensions of the data.
"""
return super().get_data_from_roi(
roi=roi, t=t, c=None, mode=mode, preserve_dimensions=preserve_dimensions
)

def set_data_from_roi(
self,
patch: ArrayLike,
roi: WorldCooROI,
t: int | slice | None = None,
preserve_dimensions: bool = False,
) -> None:
"""Set the label data from a region of interest (ROI).
Args:
roi (WorldCooROI): The region of interest.
patch (ArrayLike): The patch to set.
t (int | slice | None): The time index or slice.
preserve_dimensions (bool): Whether to preserve the dimensions of the data.
"""
return super().set_data_from_roi(
patch=patch, roi=roi, t=t, c=None, preserve_dimensions=preserve_dimensions
)

def get_data(
self,
x: int | slice | None = None,
y: int | slice | None = None,
z: int | slice | None = None,
t: int | slice | None = None,
mode: Literal["numpy"] | Literal["dask"] = "numpy",
preserve_dimensions: bool = False,
) -> ArrayLike:
"""Return the label data.
Args:
x (int | slice | None): The x index or slice.
y (int | slice | None): The y index or slice.
z (int | slice | None): The z index or slice.
t (int | slice | None): The time index or slice.
mode (str): The mode to return the data.
preserve_dimensions (bool): Whether to preserve the dimensions of the data.
"""
return super().get_data(
x=x,
y=y,
z=z,
t=t,
c=None,
mode=mode,
preserve_dimensions=preserve_dimensions,
)

def set_data(
self,
patch: ArrayLike,
x: int | slice | None = None,
y: int | slice | None = None,
z: int | slice | None = None,
t: int | slice | None = None,
preserve_dimensions: bool = False,
) -> None:
"""Set the label data in the zarr array.
Args:
patch (ArrayLike): The patch to set.
x (int | slice | None): The x index or slice.
y (int | slice | None): The y index or slice.
z (int | slice | None): The z index or slice.
t (int | slice | None): The time index or slice.
preserve_dimensions (bool): Whether to preserve the dimensions of the data.
"""
return super().set_data(
patch=patch,
x=x,
y=y,
z=z,
t=t,
c=None,
preserve_dimensions=preserve_dimensions,
)

def consolidate(self) -> None:
"""Consolidate the label group.
This method consolidates the label group by
filling all other pyramid levels with the data
"""
return super().consolidate(order=0)


class LabelGroup:
"""A class to handle the /labels group in an OME-NGFF file."""
Expand Down
19 changes: 16 additions & 3 deletions src/ngio/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Utility functions for creating and manipulating images."""

from typing import Any
import math

from ngio.core.image_handler import Image
from ngio.io import StoreLike
from ngio.ngff_meta import (
create_image_metadata,
get_ngff_image_meta_handler,
create_label_metadata,
get_ngff_image_meta_handler,
)
from ngio.ngff_meta.fractal_image_meta import (
PixelSize,
Expand Down Expand Up @@ -184,7 +185,19 @@ def create_empty_ome_zarr_label(
)

# Todo redo this with when a proper build of pyramid id implemente
shape = [int(s / sc) for s, sc in zip(shape, scaling_factor, strict=True)]
_shape = []
for s, sc in zip(shape, scaling_factor, strict=True):
if math.floor(s / sc) % 2 == 0:
_shape.append(math.floor(s / sc))
else:
_shape.append(math.ceil(s / sc))
shape = list(_shape)

if chunks is not None:
chunks = [int(c / sc) for c, sc in zip(chunks, scaling_factor, strict=True)]
_chunks = []
for c, sc in zip(chunks, scaling_factor, strict=True):
if math.floor(c / sc) % 2 == 0:
_chunks.append(math.floor(c / sc))
else:
_chunks.append(math.ceil(c / sc))
chunks = list(_chunks)
4 changes: 2 additions & 2 deletions src/ngio/tables/tables_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ def new(
**type_specific_kwargs,
)

if name not in list_of_tables:
self._group.attrs["tables"] = [*list_of_tables, name]
self._group.attrs["tables"] = [*list_of_tables, name]

return new_table

0 comments on commit 3d89667

Please sign in to comment.