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

Ome ngff #16

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion funlib/persistence/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .arrays import Array, open_ds, prepare_ds # noqa
from .arrays import Array, open_ds, prepare_ds, open_ome_ds, prepare_ome_ds # noqa

__version__ = "0.5.3"
__version_info__ = tuple(int(i) for i in __version__.split("."))
1 change: 1 addition & 0 deletions funlib/persistence/arrays/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .array import Array # noqa
from .datasets import prepare_ds, open_ds # noqa
from .ome_datasets import prepare_ome_ds, open_ome_ds # noqa
28 changes: 20 additions & 8 deletions funlib/persistence/arrays/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ def __init__(
voxel_size: Optional[Sequence[int]] = None,
axis_names: Optional[Sequence[str]] = None,
units: Optional[Sequence[str]] = None,
types: Optional[Sequence[str]] = None,
chunks: Optional[Union[int, Sequence[int], str]] = "auto",
lazy_op: Optional[LazyOp] = None,
strict_metadata: bool = True,
):
if not isinstance(data, da.Array):
self.data = da.from_array(data, chunks=chunks)
Expand All @@ -80,7 +82,9 @@ def __init__(
voxel_size=Coordinate(voxel_size) if voxel_size is not None else None,
axis_names=list(axis_names) if axis_names is not None else None,
units=list(units) if units is not None else None,
types=list(types) if types is not None else None,
shape=self._source_data.shape,
strict=strict_metadata,
)

if lazy_op is not None:
Expand All @@ -91,7 +95,7 @@ def __init__(

self.freeze()

self.validate()
self.validate(strict_metadata)

@property
def chunk_shape(self) -> Coordinate:
Expand All @@ -101,8 +105,8 @@ def uncollapsed_dims(self, physical: bool = False) -> list[bool]:
if physical:
return [
x
for x, c in zip(self._uncollapsed_dims, self._metadata.axis_names)
if not c.endswith("^")
for x, t in zip(self._uncollapsed_dims, self._metadata.types)
if t == "space"
]
else:
return self._uncollapsed_dims
Expand Down Expand Up @@ -148,14 +152,22 @@ def axis_names(self) -> list[str]:
if uncollapsed
]

@property
def types(self) -> list[str]:
return [
self._metadata.types[ii]
for ii, uncollapsed in enumerate(self.uncollapsed_dims(physical=False))
if uncollapsed
]

@property
def physical_shape(self):
return tuple(
self._source_data.shape[ii]
for ii, (uncollapsed, name) in enumerate(
zip(self.uncollapsed_dims(physical=False), self._metadata.axis_names)
for ii, (uncollapsed, type) in enumerate(
zip(self.uncollapsed_dims(physical=False), self._metadata.types)
)
if uncollapsed and not name.endswith("^")
if uncollapsed and type in ["space", "time"]
)

@property
Expand Down Expand Up @@ -414,8 +426,8 @@ def __index(self, coordinate):
index = (Ellipsis,) + index
return index

def validate(self):
self._metadata.validate()
def validate(self, strict: bool = False):
self._metadata.validate(strict)
assert len(self.axis_names) == len(self._source_data.shape), (
f"Axis names must be provided for every dimension. Got ({self.axis_names}) "
f"but expected {len(self.shape)} to match the data shape: {self.shape}"
Expand Down
32 changes: 29 additions & 3 deletions funlib/persistence/arrays/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def open_ds(
voxel_size: Optional[Sequence[int]] = None,
axis_names: Optional[Sequence[str]] = None,
units: Optional[Sequence[str]] = None,
types: Optional[Sequence[str]] = None,
chunks: Optional[Union[int, Sequence[int], str]] = "strict",
strict_metadata: bool = False,
**kwargs,
) -> Array:
"""
Expand Down Expand Up @@ -68,6 +70,11 @@ def open_ds(

An override for the units of your dataset.

types (`str`, (optional)):

An override for the types of your axes. For more details see:
https://ngff.openmicroscopy.org/latest/#axes-md

chunks (`Coordinate`, (optional)):

An override for the size of the chunks in the dataset.
Expand Down Expand Up @@ -106,6 +113,7 @@ def open_ds(
voxel_size=voxel_size,
axis_names=axis_names,
units=units,
types=types,
)

return Array(
Expand All @@ -114,6 +122,7 @@ def open_ds(
metadata.voxel_size,
metadata.axis_names,
metadata.units,
metadata.types,
data.chunks if chunks == "strict" else chunks,
)

Expand All @@ -125,6 +134,7 @@ def prepare_ds(
voxel_size: Optional[Coordinate] = None,
axis_names: Optional[Sequence[str]] = None,
units: Optional[Sequence[str]] = None,
types: Optional[Sequence[str]] = None,
chunk_shape: Optional[Sequence[int]] = None,
dtype: DTypeLike = np.float32,
mode: str = "a",
Expand Down Expand Up @@ -155,8 +165,7 @@ def prepare_ds(

axis_names:

The axis names of the dataset to create. The names of non-physical
dimensions should end with "^". e.g. ["samples^", "channels^", "z", "y", "x"]
The axis names of the dataset to create.
Set to ["c{i}^", "d{j}"] by default. Where i, j are the index of the non-physical
and physical dimensions respectively.

Expand All @@ -165,6 +174,11 @@ def prepare_ds(
The units of the dataset to create. Only provide for physical dimensions.
Set to all "" by default.

types:

The types of the axes of the dataset to create. For more details see:
https://ngff.openmicroscopy.org/latest/#axes-md

chunk_shape:

The shape of the chunks to use in the dataset. For all dimensions,
Expand Down Expand Up @@ -201,6 +215,7 @@ def prepare_ds(
voxel_size=voxel_size,
axis_names=axis_names,
units=units,
types=types,
)

try:
Expand Down Expand Up @@ -250,6 +265,14 @@ def prepare_ds(
)
metadata_compatible = False

if given_metadata.types != existing_metadata.types:
logger.info(
"Types differ: given (%s) vs parsed (%s)",
given_metadata.types,
existing_metadata.types,
)
metadata_compatible = False

if given_metadata.axis_names != existing_metadata.axis_names:
logger.info(
"Axis names differ: given (%s) vs parsed (%s)",
Expand Down Expand Up @@ -292,6 +315,7 @@ def prepare_ds(
existing_metadata.voxel_size,
existing_metadata.axis_names,
existing_metadata.units,
existing_metadata.types,
ds.chunks,
)

Expand All @@ -302,6 +326,7 @@ def prepare_ds(
voxel_size=voxel_size,
axis_names=axis_names,
units=units,
types=types,
)

# create the dataset
Expand All @@ -325,10 +350,11 @@ def prepare_ds(
default_metadata_format.units_attr: combined_metadata.units,
default_metadata_format.voxel_size_attr: combined_metadata.voxel_size,
default_metadata_format.offset_attr: combined_metadata.offset,
default_metadata_format.types_attr: combined_metadata.types,
}
)

# open array
array = Array(ds, offset, voxel_size, axis_names, units)
array = Array(ds, offset, voxel_size, axis_names, units, types)

return array
Loading
Loading