diff --git a/CHANGELOG.md b/CHANGELOG.md index 9084f444..08d1b97a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ - The `uniform` toy dataset now has two coordinate systems (better test case) - Faster table conversion to the Xenium Explorer +### Fixed +- Tight patching more stable with epsilon constant + ## [1.0.2] - 2024-01-15 ### Fix diff --git a/sopa/_constants.py b/sopa/_constants.py index a4d2026c..54728eff 100644 --- a/sopa/_constants.py +++ b/sopa/_constants.py @@ -28,6 +28,7 @@ class SopaKeys: VALID_DIMENSIONS = ("c", "y", "x") LOW_AVERAGE_COUNT = 0.01 +EPS = 1e-5 class ROI: diff --git a/sopa/segmentation/patching.py b/sopa/segmentation/patching.py index 352bed03..ad627b13 100644 --- a/sopa/segmentation/patching.py +++ b/sopa/segmentation/patching.py @@ -14,7 +14,7 @@ from spatialdata.models import ShapesModel from spatialdata.transformations import get_transformation -from .._constants import ROI, SopaFiles, SopaKeys +from .._constants import EPS, ROI, SopaFiles, SopaKeys from .._sdata import get_boundaries, get_item, get_spatial_image, to_intrinsic log = logging.getLogger(__name__) @@ -32,7 +32,9 @@ def __init__(self, xmin, xmax, patch_width, patch_overlap, tight, int_coords): self._count = self.count() if tight: self.patch_width = self.tight_width() - assert self._count == self.count() + assert ( + self._count == self.count() + ), f"Invalid patching with {self.delta=}, {self.patch_width=} and {self.patch_overlap=}" def count(self): if self.patch_width >= self.delta: @@ -45,7 +47,7 @@ def update(self, patch_width): def tight_width(self): width = (self.delta + (self._count - 1) * self.patch_overlap) / self._count - return ceil(width) if self.int_coords else width + return ceil(width) if self.int_coords else width + EPS def __getitem__(self, i): start_delta = i * (self.patch_width - self.patch_overlap) diff --git a/tests/test_patches.py b/tests/test_patches.py index abb91a81..e7ceceec 100644 --- a/tests/test_patches.py +++ b/tests/test_patches.py @@ -34,10 +34,10 @@ def _patchify_transcripts(sdata: SpatialData, width: int, overlap: int) -> list[ def test_patchify_baysor(sdata: SpatialData): - valid_indices = _patchify_transcripts(sdata, 300, 100) + valid_indices = _patchify_transcripts(sdata, 30, 10) assert len(valid_indices) == 9 - valid_indices = _patchify_transcripts(sdata, 512, 0) + valid_indices = _patchify_transcripts(sdata, 52, 0) assert len(valid_indices) == 1