ngio.common API documentation¶
ngio.common
¶
Common classes and functions that are used across the package.
Dimensions
¶
Dimensions(
shape: tuple[int, ...],
chunks: tuple[int, ...],
dataset: Dataset,
)
Dimension metadata Handling Class.
This class is used to handle and manipulate dimension metadata. It provides methods to access and validate dimension information, such as shape, axes, and properties like is_2d, is_3d, is_time_series, etc.
Create a Dimension object from a Zarr array.
Parameters:
-
shape(tuple[int, ...]) –The shape of the Zarr array.
-
chunks(tuple[int, ...]) –The chunks of the Zarr array.
-
dataset(Dataset) –The dataset object.
Source code in src/ngio/common/_dimensions.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
require_dimensions_match
class-attribute
instance-attribute
¶
require_dimensions_match = require_dimensions_match
check_if_dimensions_match
class-attribute
instance-attribute
¶
check_if_dimensions_match = check_if_dimensions_match
is_multi_channels
property
¶
is_multi_channels: bool
Return whether the image has multiple channels.
get
¶
get(axis_name: str, default: None = None) -> int | None
get(axis_name: str, default: int) -> int
get(
axis_name: str, default: int | None = None
) -> int | None
Return the dimension/shape of the given axis name.
Parameters:
-
axis_name(str) –The name of the axis (either canonical or non-canonical).
-
default(int | None, default:None) –The default value to return if the axis does not exist.
Source code in src/ngio/common/_dimensions.py
325 326 327 328 329 330 331 332 333 334 335 | |
ImagePyramidBuilder
¶
Bases: BaseModel
dimension_separator
class-attribute
instance-attribute
¶
dimension_separator: Literal['.', '/'] = '/'
model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(arbitrary_types_allowed=True)
from_scaling_factors
classmethod
¶
from_scaling_factors(
levels_paths: tuple[str, ...],
scaling_factors: tuple[float, ...],
base_shape: tuple[int, ...],
base_scale: tuple[float, ...],
axes: tuple[str, ...],
base_translation: Sequence[float] | None = None,
chunks: ChunksLike = "auto",
shards: ShardsLike | None = None,
data_type: str = "uint16",
dimension_separator: Literal[".", "/"] = "/",
compressors: Any = "auto",
zarr_format: Literal[2, 3] = 2,
other_array_kwargs: Mapping[str, Any] | None = None,
precision_scale: bool = True,
) -> ImagePyramidBuilder
Source code in src/ngio/common/_pyramid.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
from_shapes
classmethod
¶
from_shapes(
shapes: Sequence[tuple[int, ...]],
base_scale: tuple[float, ...] | list[tuple[float, ...]],
axes: tuple[str, ...],
base_translation: Sequence[float] | None = None,
levels_paths: Sequence[str] | None = None,
chunks: ChunksLike = "auto",
shards: ShardsLike | None = None,
data_type: str = "uint16",
dimension_separator: Literal[".", "/"] = "/",
compressors: Any = "auto",
zarr_format: Literal[2, 3] = 2,
other_array_kwargs: Mapping[str, Any] | None = None,
) -> ImagePyramidBuilder
Source code in src/ngio/common/_pyramid.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
to_zarr
¶
to_zarr(group: Group) -> None
Save the pyramid specification to a Zarr group.
Parameters:
-
group(Group) –The Zarr group to save the pyramid specification to.
Source code in src/ngio/common/_pyramid.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
Roi
¶
Bases: BaseModel
A multi-dimensional Region of Interest (ROI).
An Roi groups one RoiSlice per axis into a named, labelled region that can live in either world (physical) or pixel coordinate space.
Attributes:
-
name(str | None) –Human-readable name for this ROI, or None.
-
slices(list[RoiSlice]) –List of per-axis slices. Must contain at least two entries and each axis name must be unique.
-
label(int | None) –Integer label identifying the ROI (e.g. from a label image). Must be non-negative, or None if unlabelled.
-
space(Literal['world', 'pixel']) –Coordinate space of the slice values - either "world" for physical units or "pixel" for pixel indices.
validate_no_duplicate_axes
classmethod
¶
Source code in src/ngio/common/_roi.py
279 280 281 282 283 284 285 | |
from_values
classmethod
¶
from_values(
slices: Mapping[str, SliceValueType | RoiSlice],
name: str | None,
label: int | None = None,
space: Literal["world", "pixel"] = "world",
**kwargs,
) -> Self
Create an Roi from a mapping of axis names to slice values.
Parameters:
-
slices(Mapping[str, SliceValueType | RoiSlice]) –Mapping from axis name to a slice value accepted by RoiSlice.from_value (slice, tuple, float, or RoiSlice).
-
name(str | None) –Human-readable name for the ROI.
-
label(int | None, default:None) –Integer label, or None if unlabelled.
-
space(Literal['world', 'pixel'], default:'world') –Coordinate space of the provided values ("world" or "pixel").
-
**kwargs–Additional fields stored on the model.
Returns:
-
Self–A new Roi instance.
Source code in src/ngio/common/_roi.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
get
¶
Return the RoiSlice for a given axis, or None if not present.
Parameters:
-
axis_name(str) –Name of the axis to look up.
-
default(RoiSlice | None, default:None) –Value to return if the axis is not found.
Returns:
-
RoiSlice | None–The matching RoiSlice, or the default value if no slice with
-
RoiSlice | None–the given axis name exists.
Source code in src/ngio/common/_roi.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
get_name
¶
get_name() -> str
Return a display name for this ROI.
Falls back to the string label, then a full repr, when name is None.
Returns:
-
str–The ROI name, label as string, or a repr string.
Source code in src/ngio/common/_roi.py
353 354 355 356 357 358 359 360 361 362 363 364 365 | |
intersection
¶
intersection(other: Self) -> Self | None
Return the per-axis intersection of this ROI and other, or None.
Axes present in both ROIs are intersected; axes present in only one are kept as-is. Returns None if any shared axis has no overlap.
Parameters:
-
other(Self) –Another Roi in the same coordinate space.
Returns:
-
Self | None–A new Roi representing the intersection, or None if the ROIs
-
Self | None–do not overlap.
Raises:
-
NgioValueError–If the two ROIs are in different coordinate spaces, or if the labels conflict.
Source code in src/ngio/common/_roi.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
union
¶
union(other: Self) -> Self
Return the per-axis union (bounding box) of this ROI and other.
Axes present in both ROIs are unioned; axes present in only one are kept as-is.
Parameters:
-
other(Self) –Another Roi in the same coordinate space.
Returns:
-
Self–A new Roi whose slices span the combined extent of both inputs.
Raises:
-
NgioValueError–If the two ROIs are in different coordinate spaces, or if the labels conflict.
Source code in src/ngio/common/_roi.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
zoom
¶
zoom(
zoom_factor: float = 1.0,
axes: tuple[str, ...] = ("x", "y"),
) -> Self
Expand or shrink the ROI symmetrically along the specified axes.
Parameters:
-
zoom_factor(float, default:1.0) –Multiplicative scale applied to the length of each selected slice. Must be strictly positive.
-
axes(tuple[str, ...], default:('x', 'y')) –Names of the axes to zoom. Defaults to ("x", "y").
Returns:
-
Self–A new Roi with the zoom applied to the selected axes.
Source code in src/ngio/common/_roi.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
to_world
¶
to_world(pixel_size: PixelSize | None = None) -> Self
Convert the ROI to world (physical) coordinate space.
If the ROI is already in world space, a copy is returned unchanged.
Parameters:
-
pixel_size(PixelSize | None, default:None) –Per-axis pixel sizes used for the conversion. Required when the ROI is currently in pixel space.
Returns:
-
Self–A new Roi with all slices expressed in world units.
Raises:
-
NgioValueError–If the ROI is in pixel space and pixel_size is not provided.
Source code in src/ngio/common/_roi.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
to_pixel
¶
to_pixel(pixel_size: PixelSize | None = None) -> Self
Convert the ROI to pixel coordinate space.
If the ROI is already in pixel space, a copy is returned unchanged.
Parameters:
-
pixel_size(PixelSize | None, default:None) –Per-axis pixel sizes used for the conversion. Required when the ROI is currently in world space.
Returns:
-
Self–A new Roi with all slices expressed in pixel units.
Raises:
-
NgioValueError–If the ROI is in world space and pixel_size is not provided.
Source code in src/ngio/common/_roi.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
to_slicing_dict
¶
to_slicing_dict(
pixel_size: PixelSize | None = None,
) -> dict[str, slice]
Convert the ROI to a dict of axis-name -> slice in pixel space.
Converts to pixel coordinates first if necessary.
Parameters:
-
pixel_size(PixelSize | None, default:None) –Per-axis pixel sizes, required when the ROI is in world space.
Returns:
-
dict[str, slice]–A dict mapping each axis name to a Python slice object.
Source code in src/ngio/common/_roi.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
update_slice
¶
update_slice(
name: str, new_slice: SliceValueType | RoiSlice
) -> Self
Replace or add the slice for a given axis.
If an axis with the given name already exists it is replaced; otherwise the new slice is appended.
Parameters:
-
name(str) –Axis name of the slice to update or add.
-
new_slice(SliceValueType | RoiSlice) –New slice value, accepted by RoiSlice.from_value.
Returns:
-
Self–A new Roi with the updated slice list.
Source code in src/ngio/common/_roi.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
remove_slice
¶
remove_slice(name: str) -> Self
Remove the slice for a given axis.
Parameters:
-
name(str) –Axis name of the slice to remove.
Returns:
-
Self–A new Roi with the named slice removed.
Raises:
-
NgioValueError–If no slice with the given axis name exists.
Source code in src/ngio/common/_roi.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
RoiSlice
¶
Bases: BaseModel
A 1-D slice along a named axis for use within a Region of Interest.
Represents a contiguous interval [start, start + length) along a single named axis. Either bound may be None to indicate an open (unbounded) interval.
Attributes:
-
axis_name(str) –Name of the axis this slice applies to (e.g. "x", "y", "z").
-
start(float | None) –Start coordinate of the interval (inclusive). None means "from the beginning".
-
length(float | None) –Length of the interval. Must be non-negative. None means "to the end".
end
property
¶
end: float | None
Exclusive end coordinate of the interval.
Returns:
-
float | None–start + length, or None if either bound is unset.
from_value
classmethod
¶
Create a RoiSlice from a variety of input types.
Parameters:
-
axis_name(str) –Name of the axis this slice applies to.
-
value(SliceValueType | RoiSlice) –The interval to represent. Accepted types: - slice: converted directly via start / stop. - tuple[float | None, float | None]: interpreted as (start, length). - int | float: a single-unit interval starting at value (length=1). - RoiSlice: returned as-is.
Returns:
-
RoiSlice–A new RoiSlice instance.
Raises:
-
TypeError–If value is not one of the supported types.
Source code in src/ngio/common/_roi.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
to_slice
¶
to_slice() -> slice
Convert to a standard Python slice object.
Returns:
-
slice–A slice(start, end) representing this interval.
Source code in src/ngio/common/_roi.py
134 135 136 137 138 139 140 | |
union
¶
Return the smallest interval that contains both slices.
Parameters:
-
other(RoiSlice) –Another RoiSlice on the same axis.
Returns:
-
RoiSlice–A new RoiSlice spanning from the minimum start to the maximum
-
RoiSlice–end of the two inputs.
Raises:
-
NgioValueError–If the two slices are on different axes.
Source code in src/ngio/common/_roi.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
intersection
¶
Return the overlap between this slice and other, or None.
Parameters:
-
other(RoiSlice) –Another RoiSlice on the same axis.
Returns:
-
RoiSlice | None–A new RoiSlice representing the overlapping interval, or None
-
RoiSlice | None–if the two slices do not overlap.
Raises:
-
NgioValueError–If the two slices are on different axes.
Source code in src/ngio/common/_roi.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
to_world
¶
to_world(pixel_size: float) -> RoiSlice
Convert from pixel coordinates to world (physical) coordinates.
Parameters:
-
pixel_size(float) –Physical size of one pixel along this axis.
Returns:
-
RoiSlice–A new RoiSlice with start and length expressed in world units.
Source code in src/ngio/common/_roi.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
to_pixel
¶
to_pixel(pixel_size: float) -> RoiSlice
Convert from world (physical) coordinates to pixel coordinates.
Parameters:
-
pixel_size(float) –Physical size of one pixel along this axis.
Returns:
-
RoiSlice–A new RoiSlice with start and length expressed in pixel units.
Source code in src/ngio/common/_roi.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
zoom
¶
zoom(zoom_factor: float = 1.0) -> RoiSlice
Expand or shrink the slice symmetrically around its centre.
A zoom_factor greater than 1 enlarges the interval; less than 1 shrinks it. The centre of the interval is preserved, and the start is clamped to 0.
Parameters:
-
zoom_factor(float, default:1.0) –Multiplicative scale applied to the length. Must be strictly positive.
Returns:
-
RoiSlice–A new RoiSlice with the adjusted interval.
Raises:
-
NgioValueError–If zoom_factor is not greater than 0.
Source code in src/ngio/common/_roi.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
compute_masking_roi
¶
compute_masking_roi(
segmentation: ndarray | Array,
pixel_size: PixelSize,
axes_order: Sequence[str],
) -> list[Roi]
Compute ROIs for each label in a segmentation.
This function expects a 2D, 3D, or 4D segmentation array. The axes order should match the segmentation dimensions.
Parameters:
-
segmentation(ndarray | Array) –The segmentation array (2D, 3D, or 4D).
-
pixel_size(PixelSize) –The pixel size metadata for coordinate conversion.
-
axes_order(Sequence[str]) –The order of axes in the segmentation (e.g., 'zyx' or 'yx').
Returns:
-
list[Roi]–A list of Roi objects, one for each unique label in the segmentation.
Source code in src/ngio/common/_masking_roi.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
consolidate_pyramid
¶
consolidate_pyramid(
source: Array,
targets: list[Array],
order: InterpolationOrder = "linear",
mode: Literal["dask", "numpy", "coarsen"] = "dask",
) -> None
Consolidate the Zarr array.
Source code in src/ngio/common/_pyramid.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
on_disk_zoom
¶
on_disk_zoom(
source: Array,
target: Array,
order: InterpolationOrder = "linear",
mode: Literal["dask", "numpy", "coarsen"] = "dask",
) -> None
Apply a zoom operation from a source zarr array to a target zarr array.
Parameters:
-
source(Array) –The source array to zoom.
-
target(Array) –The target array to save the zoomed result to.
-
order(InterpolationOrder, default:'linear') –The order of interpolation. Defaults to "linear".
-
mode(Literal['dask', 'numpy', 'coarsen'], default:'dask') –The mode to use. Defaults to "dask".
Source code in src/ngio/common/_pyramid.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
dask_zoom
¶
dask_zoom(
source_array: Array,
scale: tuple[float | int, ...] | None = None,
target_shape: tuple[int, ...] | None = None,
order: InterpolationOrder = "linear",
) -> Array
Dask implementation of zooming an array.
Only one of scale or target_shape must be provided.
Parameters:
-
source_array(Array) –The source array to zoom.
-
scale(tuple[int, ...] | None, default:None) –The scale factor to zoom by.
-
target_shape((tuple[int, ...], None), default:None) –The target shape to zoom to.
-
order(Literal['nearest', 'linear', 'cubic'], default:'linear') –The order of interpolation. Defaults to "linear".
Returns:
-
Array–da.Array: The zoomed array.
Source code in src/ngio/common/_zoom.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
numpy_zoom
¶
numpy_zoom(
source_array: ndarray,
scale: tuple[int | float, ...] | None = None,
target_shape: tuple[int, ...] | None = None,
order: InterpolationOrder = "linear",
) -> ndarray
Numpy implementation of zooming an array.
Only one of scale or target_shape must be provided.
Parameters:
-
source_array(ndarray) –The source array to zoom.
-
scale(tuple[int, ...] | None, default:None) –The scale factor to zoom by.
-
target_shape((tuple[int, ...], None), default:None) –The target shape to zoom to.
-
order(Literal[0, 1, 2], default:'linear') –The order of interpolation. Defaults to 1.
Returns:
-
ndarray–np.ndarray: The zoomed array
Source code in src/ngio/common/_zoom.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |