Skip to content

Commit 00188a6

Browse files
authored
Merge pull request #848 from adrtsc/main
Add upper and lower quantile rescaling to calculate_registration_image_based.py for more robust registration
2 parents 14b2b36 + 82e57c5 commit 00188a6

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
**Note**: Numbers like (\#123) point to closed Pull Requests on the fractal-tasks-core repository.
22

3+
# 1.3.2
4+
* Tasks:
5+
* Add percentile-based rescaling to calculate registration task to make it more robust (\#848)
36
* Dependencies:
47
* Relax pandas constraint to `<2`.
58
* Relax torch constraint to `<=3.0.0`.

fractal_tasks_core/__FRACTAL_MANIFEST__.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,18 @@
11231123
"title": "Method",
11241124
"description": "Method to use for image registration. The available methods are `phase_cross_correlation` (scikit-image package, works for 2D & 3D) and \"chi2_shift\" (image_registration package, only works for 2D images)."
11251125
},
1126+
"lower_rescale_quantile": {
1127+
"default": 0.0,
1128+
"title": "Lower Rescale Quantile",
1129+
"type": "number",
1130+
"description": "Lower quantile for rescaling the image intensities before applying registration. Can be helpful to deal with image artifacts. Default is 0."
1131+
},
1132+
"upper_rescale_quantile": {
1133+
"default": 0.99,
1134+
"title": "Upper Rescale Quantile",
1135+
"type": "number",
1136+
"description": "Upper quantile for rescaling the image intensities before applying registration. Can be helpful to deal with image artifacts. Default is 0.99."
1137+
},
11261138
"roi_table": {
11271139
"default": "FOV_ROI_table",
11281140
"title": "Roi Table",

fractal_tasks_core/tasks/calculate_registration_image_based.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import numpy as np
2121
import zarr
2222
from pydantic import validate_call
23+
from skimage.exposure import rescale_intensity
2324
from skimage.registration import phase_cross_correlation
2425

2526
from fractal_tasks_core.channels import get_channel_from_image_zarr
@@ -77,6 +78,8 @@ def calculate_registration_image_based(
7778
# Core parameters
7879
wavelength_id: str,
7980
method: RegistrationMethod = RegistrationMethod.PHASE_CROSS_CORRELATION,
81+
lower_rescale_quantile: float = 0.0,
82+
upper_rescale_quantile: float = 0.99,
8083
roi_table: str = "FOV_ROI_table",
8184
level: int = 2,
8285
) -> None:
@@ -102,6 +105,12 @@ def calculate_registration_image_based(
102105
are `phase_cross_correlation` (scikit-image package, works for 2D
103106
& 3D) and "chi2_shift" (image_registration package, only works for
104107
2D images).
108+
lower_rescale_quantile: Lower quantile for rescaling the image
109+
intensities before applying registration. Can be helpful
110+
to deal with image artifacts. Default is 0.
111+
upper_rescale_quantile: Upper quantile for rescaling the image
112+
intensities before applying registration. Can be helpful
113+
to deal with image artifacts. Default is 0.99.
105114
roi_table: Name of the ROI table over which the task loops to
106115
calculate the registration. Examples: `FOV_ROI_table` => loop over
107116
the field of views, `well_ROI_table` => process the whole well as
@@ -247,6 +256,22 @@ def calculate_registration_image_based(
247256
compute=compute,
248257
)
249258

259+
# Rescale the images
260+
img_ref = rescale_intensity(
261+
img_ref,
262+
in_range=(
263+
np.quantile(img_ref, lower_rescale_quantile),
264+
np.quantile(img_ref, upper_rescale_quantile),
265+
),
266+
)
267+
img_acq_x = rescale_intensity(
268+
img_acq_x,
269+
in_range=(
270+
np.quantile(img_acq_x, lower_rescale_quantile),
271+
np.quantile(img_acq_x, upper_rescale_quantile),
272+
),
273+
)
274+
250275
##############
251276
# Calculate the transformation
252277
##############

tests/tasks/test_registration.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ def test_multiplexing_registration(
237237
tmp_path,
238238
monkeypatch: MonkeyPatch,
239239
method: str,
240+
lower_rescale_quantile: float = 0.0,
241+
upper_rescale_quantile: float = 0.99,
240242
# Given the test data, only implemented per FOV
241243
roi_table="FOV_ROI_table",
242244
):
@@ -348,6 +350,8 @@ def test_multiplexing_registration(
348350
init_args=image["init_args"],
349351
wavelength_id="A01_C01",
350352
method=method,
353+
lower_rescale_quantile=lower_rescale_quantile,
354+
upper_rescale_quantile=upper_rescale_quantile,
351355
roi_table=roi_table,
352356
)
353357

@@ -438,6 +442,8 @@ def test_multiplexing_registration_3d(
438442
tmp_path,
439443
monkeypatch: MonkeyPatch,
440444
method: str,
445+
lower_rescale_quantile: float = 0.0,
446+
upper_rescale_quantile: float = 0.99,
441447
# Given the test data, only implemented per FOV
442448
roi_table="FOV_ROI_table",
443449
):
@@ -526,6 +532,8 @@ def test_multiplexing_registration_3d(
526532
init_args=image["init_args"],
527533
wavelength_id="A01_C01",
528534
method=method,
535+
lower_rescale_quantile=lower_rescale_quantile,
536+
upper_rescale_quantile=upper_rescale_quantile,
529537
roi_table=roi_table,
530538
)
531539
pytest.skip(

0 commit comments

Comments
 (0)