diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dacce07..72ec2956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Features + +- Expose `ngio.common.pixel_to_world` and `ngio.common.world_to_pixel` for scalar coordinate conversions with the same near-grid snapping as ROI conversion ([#238](https://github.com/BioVisionCenter/ngio/issues/238)). + ## [v1.1.5] ### Fixes diff --git a/src/ngio/common/__init__.py b/src/ngio/common/__init__.py index 532f23bb..de36ce15 100644 --- a/src/ngio/common/__init__.py +++ b/src/ngio/common/__init__.py @@ -11,7 +11,7 @@ consolidate_pyramid, on_disk_zoom, ) -from ngio.common._roi import Roi, RoiSlice +from ngio.common._roi import Roi, RoiSlice, pixel_to_world, world_to_pixel from ngio.common._zoom import InterpolationOrder, dask_zoom, numpy_zoom __all__ = [ @@ -29,4 +29,6 @@ "dask_zoom", "numpy_zoom", "on_disk_zoom", + "pixel_to_world", + "world_to_pixel", ] diff --git a/src/ngio/common/_roi.py b/src/ngio/common/_roi.py index 8895d44e..df79a2ef 100644 --- a/src/ngio/common/_roi.py +++ b/src/ngio/common/_roi.py @@ -17,20 +17,44 @@ def world_to_pixel(value: float, pixel_size: float, eps: float = 1e-2) -> float: - raster_value = value / pixel_size + """Convert a position or length from physical units to pixel units. + + Divide by the pixel size, then snap to the nearest integer when the + distance is strictly less than `eps` pixels. The nearest integer uses + Python's `round`, which rounds ties to even. Values at or beyond the + tolerance retain their fractional part. Positions are relative to the + axis origin. + + Snapping applies to each input independently. Image slicing applies its + floor for the start and its ceiling for the exclusive stop separately. - # Round to nearest integer if within eps, this allows to keep pixel-aligned ROIs - # when machine precision issues would otherwise make them slightly off. - # Since errors in precision are both on the value and the pixel size, - # the tolerance must be large enough to accommodate compounded errors, - # we default to 1e-2 which allows for ~1% error. - _rounded = round(raster_value) - if abs(_rounded - raster_value) < eps: - return _rounded + Args: + value: Position or length in the same physical units as `pixel_size`. + pixel_size: Physical size of one pixel along the axis. + eps: Absolute snapping tolerance in pixel units. Zero disables snapping. + + Returns: + The converted value in pixel units. + """ + raster_value = value / pixel_size + nearest = round(raster_value) + if abs(nearest - raster_value) < eps: + return nearest return raster_value def pixel_to_world(value: float, pixel_size: float) -> float: + """Convert a position or length from pixel units to physical units. + + Multiply by the pixel size. Positions are relative to the axis origin. + + Args: + value: Position or length in pixel units, including fractional pixels. + pixel_size: Physical size of one pixel along the axis. + + Returns: + The converted value in the units of `pixel_size`. + """ return value * pixel_size diff --git a/tests/unit/common/test_roi.py b/tests/unit/common/test_roi.py index 39c69b71..148dd34c 100644 --- a/tests/unit/common/test_roi.py +++ b/tests/unit/common/test_roi.py @@ -2,12 +2,51 @@ from pydantic import ValidationError from ngio import PixelSize -from ngio.common import Roi, RoiSlice +from ngio.common import Roi, RoiSlice, pixel_to_world, world_to_pixel from ngio.tables import RoiTable from ngio.tables._tables_container import open_table, write_table from ngio.utils import NgioValueError +@pytest.mark.parametrize( + ("world", "pixel_size", "eps", "expected"), + [ + (2.0, 0.5, 1e-2, 4), + (2.003, 0.5, 1e-2, 4), + (-2.003, 0.5, 1e-2, -4), + (2.01, 0.5, 1e-2, 4.02), + (2.125, 0.5, 0.25, 4.25), + (2.0625, 0.5, 0.25, 4), + (2.003, 0.5, 0.0, 4.006), + (2.25, 0.5, 0.6, 4), + (-2.75, 0.5, 0.6, -6), + ], +) +def test_world_to_pixel_snapping(world, pixel_size, eps, expected): + assert world_to_pixel(world, pixel_size, eps=eps) == pytest.approx(expected) + + +@pytest.mark.parametrize("pixel", [0, 4, -4, 1.25, -1.25]) +def test_pixel_world_round_trip(pixel): + pixel_size = 0.3 + world = pixel_to_world(pixel, pixel_size) + assert world == pytest.approx(pixel * pixel_size) + assert world_to_pixel(world, pixel_size) == pytest.approx(pixel) + + +def test_scalar_conversions_match_roi_slice(): + roi_slice = RoiSlice(axis_name="x", start=-2.003, length=1.125) + pixel_size = 0.5 + + pixel_slice = roi_slice.to_pixel(pixel_size) + assert pixel_slice.start == world_to_pixel(-2.003, pixel_size) == -4 + assert pixel_slice.length == world_to_pixel(1.125, pixel_size) == 2.25 + + world_slice = pixel_slice.to_world(pixel_size) + assert world_slice.start == pixel_to_world(-4, pixel_size) == -2.0 + assert world_slice.length == pixel_to_world(2.25, pixel_size) == 1.125 + + def test_basic_rois_ops(): roi = Roi.from_values( name="test",