14
14
import json
15
15
import warnings
16
16
17
+ import anndata as ad
18
+ import dask
17
19
import dask .array as da
18
20
import numpy as np
19
21
from skimage .io import imread
20
22
21
23
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
+ )
22
28
23
29
24
30
def correct (
25
31
img ,
26
32
illum_img = None ,
27
33
background = 110 ,
28
- img_size_y = 2160 ,
29
- img_size_x = 2560 ,
30
- block_info = None ,
31
34
):
32
35
"""
33
36
Corrects single Z level input image using an illumination profile
@@ -42,25 +45,21 @@ def correct(
42
45
:type illum_img: np.array
43
46
:param background: value for background subtraction (optional, default 110)
44
47
: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
49
48
50
49
"""
51
50
52
51
# Check shapes
53
- if img .shape != ( 1 , img_size_y , img_size_x ) :
52
+ if illum_img .shape != img . shape [ 1 :] :
54
53
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 "
60
56
f"illum_img.shape: { illum_img .shape } "
61
57
)
62
58
63
59
# 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
64
63
img [img <= background ] = 0
65
64
img [img > background ] = img [img > background ] - background
66
65
@@ -190,24 +189,45 @@ def illumination_correction(
190
189
f"Error in illumination_correction, chunks_x: { chunks_x } "
191
190
)
192
191
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
+
193
209
# Loop over channels
194
- # FIXME: map_blocks could take care of this
195
210
data_czyx_new = []
196
211
for ind_ch , ch in enumerate (chl_list ):
197
212
198
213
data_zyx = data_czyx [ind_ch ]
199
214
illum_img = corrections [ch ]
200
215
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
+
211
231
data_czyx_new .append (data_zyx_new )
212
232
accumulated_data = da .stack (data_czyx_new , axis = 0 )
213
233
0 commit comments