25
25
from anndata import read_zarr
26
26
from dask .array .image import imread
27
27
28
- from fractal_tasks_core .lib_regions_of_interest import (
29
- convert_ROI_table_to_indices ,
30
- )
31
- from fractal_tasks_core .lib_zattrs_utils import extract_zyx_pixel_sizes
32
-
33
- # import numpy as np
34
-
35
- # from fractal_tasks_core.lib_pyramid_creation import write_pyramid
36
-
37
- # from skimage.io import imread
38
-
39
-
40
- def get_ROIs_bounding_box (adata , pxl_size ):
41
- # x_min_micrometer = min(adata[:, "x_micrometer"].X)
42
- x_max_micrometer = max (
43
- adata [:, "x_micrometer" ].X + adata [:, "len_x_micrometer" ].X
44
- )
45
- # ind_x_min = x_min_micrometer / pxl_size[2]
46
- ind_x_max = x_max_micrometer / pxl_size [2 ]
47
- # y_min_micrometer = min(adata[:, "y_micrometer"].X)
48
- y_max_micrometer = max (
49
- adata [:, "y_micrometer" ].X + adata [:, "len_y_micrometer" ].X
50
- )
51
- # ind_y_min = y_min_micrometer / pxl_size[1]
52
- ind_y_max = y_max_micrometer / pxl_size [1 ]
53
- # z_min_micrometer = min(adata[:, "z_micrometer"].X)
54
- z_max_micrometer = max (
55
- adata [:, "z_micrometer" ].X + adata [:, "len_z_micrometer" ].X
56
- )
57
- # ind_z_min = z_min_micrometer / pxl_size[0]
58
- ind_z_max = z_max_micrometer / pxl_size [0 ]
59
-
60
- # assert ind_x_min == 0
61
- # assert ind_y_min == 0
62
- # assert ind_z_min == 0
63
-
64
- return ind_x_max , ind_y_max , ind_z_max
28
+ from .lib_pyramid_creation import build_pyramid
29
+ from .lib_regions_of_interest import convert_ROI_table_to_indices
30
+ from .lib_regions_of_interest import get_ROIs_bounding_box
31
+ from .lib_zattrs_utils import extract_zyx_pixel_sizes
65
32
66
33
67
34
def sort_fun (s ):
@@ -104,62 +71,39 @@ def yokogawa_to_zarr(
104
71
original_path_list = metadata ["original_paths" ]
105
72
in_path = Path (original_path_list [0 ]).parent
106
73
ext = Path (original_path_list [0 ]).name
107
- # num_levels = metadata["num_levels"]
108
- # coarsening_xy = metadata["coarsening_xy"]
109
-
110
- # Hard-coded values (by now) of chunk sizes to be passed to rechunk,
111
- # both at level 0 (before coarsening) and at levels 1,2,.. (after
112
- # repeated coarsening).
113
- # Note that balance=True may override these values.
114
- # chunk_size_x = 2560
115
- # chunk_size_y = 2160
74
+ num_levels = metadata ["num_levels" ]
75
+ coarsening_xy = metadata ["coarsening_xy" ]
116
76
117
77
# Define well
118
78
component_split = component .split ("/" )
119
79
well_row = component_split [1 ]
120
80
well_column = component_split [2 ]
121
-
122
81
well_ID = well_row + well_column
123
82
124
- # delayed_imread = delayed(imread)
125
- # from devtools import debug
126
- # debug(f"Channels: {chl_list}")
127
-
83
+ # Read useful information from ROI table and .zattrs
128
84
zarrurl = input_paths [0 ].parent .as_posix () + f"/{ component } "
129
85
adata = read_zarr (f"{ zarrurl } /tables/FOV_ROI_table" )
130
86
pxl_size = extract_zyx_pixel_sizes (f"{ zarrurl } /.zattrs" )
131
87
fov_indices = convert_ROI_table_to_indices (
132
88
adata , full_res_pxl_sizes_zyx = pxl_size
133
89
)
134
-
135
90
max_x , max_y , max_z = get_ROIs_bounding_box (adata , pxl_size )
136
91
137
- # ref_img_size = None
138
- # for indices in fov_indices:
139
- # img_size = (indices[3] - indices[2], indices[5] - indices[4])
140
- # if ref_img_size is None:
141
- # ref_img_size = img_size
142
- # else:
143
- # if img_size != ref_img_size:
144
- # raise Exception(
145
- # "ERROR: inconsistent image sizes in list_indices"
146
- # )
147
-
148
- # img_size_y, img_size_x = img_size[:]
149
-
92
+ # Load a single image, to retrieve useful information
150
93
sample = imread (glob (f"{ in_path } /*_{ well_ID } _*{ ext } " )[0 ])
151
- from devtools import debug
152
94
153
- debug (f"{ sample .shape = } , { sample .shape [1 ]= } " )
95
+ # Initialize zarr
96
+ chunksize = (1 , 1 , sample .shape [1 ], sample .shape [2 ])
154
97
canvas_zarr = zarr .create (
155
98
shape = (len (chl_list ), max_z , max_y , max_x ),
156
- chunks = ( 1 , 1 , sample . shape [ 1 ], sample . shape [ 2 ]) ,
99
+ chunks = chunksize ,
157
100
dtype = sample .dtype ,
158
101
store = da .core .get_mapper (zarrurl + "/0" ),
159
102
overwrite = False ,
160
103
dimension_separator = "/" ,
161
104
)
162
- # list_channels = []
105
+
106
+ # Loop over channels
163
107
for i_c , chl in enumerate (chl_list ):
164
108
A , C = chl .split ("_" )
165
109
@@ -175,78 +119,44 @@ def yokogawa_to_zarr(
175
119
f" channel: { chl } ,\n "
176
120
f" glob_path: { glob_path } "
177
121
)
178
-
179
- # max_x = max(roi[5] for roi in fov_position)
180
- # max_y = max(roi[3] for roi in fov_position)
181
- # max_z = max(roi[1] for roi in fov_position)
182
-
183
- # img_position = []
184
- # for fov in fov_position:
185
- # for z in range(fov[1]):
186
- # img = [z, z + 1, fov[2], fov[3], fov[4], fov[5]]
187
- # img_position.append(img)
188
-
189
- # regions = []
190
- # for i_c, channel in enumerate(chl_list):
122
+ # Loop over 3D FOV ROIs
191
123
for indices in fov_indices :
192
124
s_z , e_z , s_y , e_y , s_x , e_x = indices [:]
193
- # for i_z in range(s_z, e_z):
194
125
region = (
195
126
slice (i_c , i_c + 1 ),
196
127
slice (s_z , e_z ),
197
128
slice (s_y , e_y ),
198
129
slice (s_x , e_x ),
199
130
)
200
-
201
- # assert s_z == 0
202
-
203
- FOV = da .concatenate (
131
+ FOV_3D = da .concatenate (
204
132
[imread (img ) for img in filenames [:e_z ]],
205
133
)
206
- FOV_4D = da .expand_dims (FOV , axis = 0 )
207
- debug (FOV_4D )
134
+ FOV_4D = da .expand_dims (FOV_3D , axis = 0 )
208
135
filenames = filenames [e_z :]
209
-
210
136
da .array (FOV_4D ).to_zarr (
211
137
url = canvas_zarr ,
212
138
region = region ,
213
139
compute = True ,
214
140
)
215
141
216
- # canvas = da.zeros(
217
- # (max_z, max_y, max_x),
218
- # dtype=sample.dtype,
219
- # chunks=(1, chunk_size_y, chunk_size_x),
220
- # )
221
-
222
- # for indexes, image_file in zip(*(img_position, filenames)):
223
- # canvas[
224
- # indexes[0] : indexes[1], # noqa: 203
225
- # indexes[2] : indexes[3], # noqa: 203
226
- # indexes[4] : indexes[5], # noqa: 203
227
- # ] = imread(image_file)
228
-
229
- # list_channels.append(canvas)
230
- # data_czyx = da.stack(list_channels, axis=0)
142
+ # Starting from on-disk highest-resolution data, build and write to disk a
143
+ # pyramid of coarser levels
144
+ build_pyramid (
145
+ zarrurl = zarrurl ,
146
+ overwrite = False ,
147
+ num_levels = num_levels ,
148
+ coarsening_xy = coarsening_xy ,
149
+ chunksize = chunksize ,
150
+ )
231
151
152
+ # Delete images (optional)
232
153
if delete_input :
233
154
for f in filenames :
234
155
try :
235
156
os .remove (f )
236
157
except OSError as e :
237
158
print ("Error: %s : %s" % (f , e .strerror ))
238
159
239
- # Construct resolution pyramid
240
- # write_pyramid(
241
- # data_czyx,
242
- # newzarrurl=output_path.parent.as_posix() + f"/{component}",
243
- # overwrite=False,
244
- # coarsening_xy=coarsening_xy,
245
- # num_levels=num_levels,
246
- # chunk_size_x=chunk_size_x,
247
- # chunk_size_y=chunk_size_y,
248
- # )
249
-
250
160
251
161
if __name__ == "__main__" :
252
162
from argparse import ArgumentParser
0 commit comments