Skip to content

Commit c1dbc24

Browse files
committed
Copy scale information in labeling tasks (ref #112)
1 parent 5c9a964 commit c1dbc24

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

fractal/tasks/image_labeling.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,23 @@ def image_labeling(
133133
if model_type not in ["nuclei", "cyto2", "cyto"]:
134134
raise Exception(f"ERROR model_type={model_type} is not allowed.")
135135

136-
# Load .zattrs file
136+
# Load zattrs file
137137
zattrs_file = f"{zarrurl}.zattrs"
138138
with open(zattrs_file, "r") as jsonfile:
139139
zattrs = json.load(jsonfile)
140140

141+
# Preliminary checks on multiscales
142+
multiscales = zattrs["multiscales"]
143+
if len(multiscales) > 1:
144+
raise Exception(f"ERROR: There are {len(multiscales)} multiscales")
145+
if "coordinateTransformations" in multiscales[0].keys():
146+
raise Exception(
147+
"ERROR: coordinateTransformations at the multiscales "
148+
"level are not currently supported"
149+
)
150+
141151
# Extract num_levels
142-
num_levels = len(zattrs["multiscales"][0]["datasets"])
152+
num_levels = len(multiscales[0]["datasets"])
143153
print("num_levels", num_levels)
144154
print()
145155

@@ -213,13 +223,8 @@ def image_labeling(
213223
{
214224
"name": label_name,
215225
"version": "0.4",
216-
"axes": [
217-
{"name": axis_name, "type": "space"}
218-
for axis_name in ["z", "y", "x"]
219-
],
220-
"datasets": [
221-
{"path": f"{ind_level}"} for ind_level in range(num_levels)
222-
],
226+
"axes": multiscales[0]["axes"],
227+
"datasets": multiscales[0]["datasets"],
223228
}
224229
]
225230

fractal/tasks/image_labeling_whole_well.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ def image_labeling_whole_well(
7676
" but there can be only one Z plane."
7777
)
7878

79-
# Load .zattrs file
79+
# Load zattrs file
8080
zattrs_file = f"{zarrurl}.zattrs"
8181
with open(zattrs_file, "r") as jsonfile:
8282
zattrs = json.load(jsonfile)
8383

84+
# Preliminary checks on multiscales
85+
multiscales = zattrs["multiscales"]
86+
if len(multiscales) > 1:
87+
raise Exception(f"ERROR: There are {len(multiscales)} multiscales")
88+
if "coordinateTransformations" in multiscales[0].keys():
89+
raise Exception(
90+
"ERROR: coordinateTransformations at the multiscales "
91+
"level are not currently supported"
92+
)
93+
8494
# Extract num_levels
85-
num_levels = len(zattrs["multiscales"][0]["datasets"])
95+
num_levels = len(multiscales[0]["datasets"])
8696
print("num_levels", num_levels)
8797
print()
8898

@@ -142,6 +152,26 @@ def image_labeling_whole_well(
142152
f"da.from_array(upscaled_mask) [with rechunking]: {mask_da}\n\n"
143153
)
144154

155+
# Construct rescaled datasets
156+
datasets = multiscales[0]["datasets"]
157+
new_datasets = []
158+
for ds in datasets:
159+
new_ds = {}
160+
for key in ds.keys():
161+
if key != "coordinateTransformations":
162+
new_ds[key] = ds[key]
163+
old_transformations = ds["coordinateTransformations"]
164+
new_transformations = []
165+
for t in old_transformations:
166+
if t["type"] == "scale":
167+
new_t = t
168+
new_t["scale"][1] *= coarsening_xy**labeling_level
169+
new_t["scale"][2] *= coarsening_xy**labeling_level
170+
new_transformations.append(new_t)
171+
else:
172+
new_transformations.append(t)
173+
new_datasets.append(new_ds)
174+
145175
# Write zattrs for labels and for specific label
146176
# FIXME deal with: (1) many channels, (2) overwriting
147177
labels_group = zarr.group(f"{zarrurl}labels")
@@ -152,30 +182,8 @@ def image_labeling_whole_well(
152182
{
153183
"name": label_name,
154184
"version": "0.4",
155-
"axes": [
156-
{"name": axis_name, "type": "space"}
157-
for axis_name in ["z", "y", "x"]
158-
],
159-
"datasets": [
160-
{
161-
"path": f"{ind_level}",
162-
"coordinateTransformations": [
163-
{
164-
"type": "scale",
165-
"scale": [
166-
1.0,
167-
1.0
168-
* coarsening_xy
169-
** (labeling_level + ind_level),
170-
1.0
171-
* coarsening_xy
172-
** (labeling_level + ind_level),
173-
],
174-
}
175-
],
176-
}
177-
for ind_level in range(num_levels)
178-
],
185+
"axes": multiscales[0]["axes"],
186+
"datasets": new_datasets,
179187
}
180188
]
181189

0 commit comments

Comments
 (0)