Skip to content

Commit 21ce80d

Browse files
committed
Prototype of ROI-based illumination_correction task (ref #114)
1 parent d068c4a commit 21ce80d

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

fractal/tasks/illumination_correction.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
import json
1515
import warnings
1616

17+
import anndata as ad
18+
import dask
1719
import dask.array as da
1820
import numpy as np
1921
from skimage.io import imread
2022

2123
from fractal.tasks.lib_pyramid_creation import write_pyramid
24+
from fractal.tasks.lib_regions_of_interest import convert_ROI_table_to_indices
25+
from fractal.tasks.lib_regions_of_interest import (
26+
split_3D_indices_into_z_layers,
27+
)
2228

2329

2430
def correct(
2531
img,
2632
illum_img=None,
2733
background=110,
28-
img_size_y=2160,
29-
img_size_x=2560,
30-
block_info=None,
3134
):
3235
"""
3336
Corrects single Z level input image using an illumination profile
@@ -42,25 +45,21 @@ def correct(
4245
:type illum_img: np.array
4346
:param background: value for background subtraction (optional, default 110)
4447
:type background: int
45-
:param img_size_y: image size along Y (optional, default 2160)
46-
:type img_size_y: int
47-
:param img_size_x: image size along X (optional, default 2560)
48-
:type img_size_x: int
4948
5049
"""
5150

5251
# Check shapes
53-
if img.shape != (1, img_size_y, img_size_x):
52+
if illum_img.shape != img.shape[1:]:
5453
raise Exception(
55-
f"Error in illumination_correction, img.shape: {img.shape}"
56-
)
57-
if illum_img.shape != (img_size_y, img_size_x):
58-
raise Exception(
59-
"Error in illumination_correction, "
54+
"Error in illumination_correction\n"
55+
f"img.shape: {img.shape}\n"
6056
f"illum_img.shape: {illum_img.shape}"
6157
)
6258

6359
# Background subtraction
60+
# FIXME: is there a problem with these changes?
61+
# devdoc.net/python/dask-2.23.0-doc/delayed-best-practices.html
62+
# ?highlight=delayed#don-t-mutate-inputs
6463
img[img <= background] = 0
6564
img[img > background] = img[img > background] - background
6665

@@ -190,24 +189,45 @@ def illumination_correction(
190189
f"Error in illumination_correction, chunks_x: {chunks_x}"
191190
)
192191

192+
# Read FOV ROIs
193+
FOV_ROI_table = ad.read_zarr(f"{zarrurl}tables/FOV_ROI_table")
194+
195+
# Create list of indices for 3D FOVs spanning the entire Z direction
196+
list_indices = convert_ROI_table_to_indices(
197+
FOV_ROI_table, level=0, coarsening_xy=coarsening_xy
198+
)
199+
200+
# Create the final list of single-Z-layer FOVs
201+
list_indices = split_3D_indices_into_z_layers(list_indices)
202+
203+
# Prepare delayed function
204+
delayed_correct = dask.delayed(correct)
205+
206+
# FIXME The dask array will consist of a single chunk.
207+
# (docs.dask.org/en/stable/_modules/dask/array/core.html#from_delayed)
208+
193209
# Loop over channels
194-
# FIXME: map_blocks could take care of this
195210
data_czyx_new = []
196211
for ind_ch, ch in enumerate(chl_list):
197212

198213
data_zyx = data_czyx[ind_ch]
199214
illum_img = corrections[ch]
200215

201-
# Map correct(..) function onto each block
202-
data_zyx_new = data_zyx.map_blocks(
203-
correct,
204-
chunks=(1, img_size_y, img_size_x),
205-
meta=np.array((), dtype=dtype),
206-
illum_img=illum_img,
207-
background=background,
208-
img_size_y=img_size_y,
209-
img_size_x=img_size_x,
210-
)
216+
data_zyx_new = da.empty_like(data_zyx)
217+
218+
for indices in list_indices:
219+
s_z, e_z, s_y, e_y, s_x, e_x = indices[:]
220+
shape = [e_z - s_z, e_y - s_y, e_x - s_x]
221+
new_img = delayed_correct(
222+
data_zyx[s_z:e_z, s_y:e_y, s_x:e_x],
223+
illum_img,
224+
background=background,
225+
)
226+
# FIXME what about meta and name kwargs?
227+
data_zyx_new[s_z:e_z, s_y:e_y, s_x:e_x] = da.from_delayed(
228+
new_img, shape, dtype
229+
)
230+
211231
data_czyx_new.append(data_zyx_new)
212232
accumulated_data = da.stack(data_czyx_new, axis=0)
213233

fractal/tasks/lib_regions_of_interest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def convert_ROI_table_to_indices(
129129
return list_indices
130130

131131

132-
def split_3D_ROI_indices_into_z_layers(
132+
def split_3D_indices_into_z_layers(
133133
list_indices: List[List[int]],
134134
) -> List[List[int]]:
135135

@@ -164,7 +164,7 @@ def _inspect_ROI_table(
164164
adata, level=level, coarsening_xy=coarsening_xy
165165
)
166166

167-
list_indices = split_3D_ROI_indices_into_z_layers(list_indices)
167+
list_indices = split_3D_indices_into_z_layers(list_indices)
168168

169169
print(f"level: {level}")
170170
print(f"coarsening_xy: {coarsening_xy}")

0 commit comments

Comments
 (0)