Skip to content

Commit 219491c

Browse files
Merge pull request #15 from rhornb/dev
Fix 2D table error
2 parents b9c3443 + 43a5861 commit 219491c

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ dev = [
6565
# https://docs.astral.sh/ruff
6666
[tool.ruff]
6767
line-length = 88
68-
target-version = "py311"
68+
target-version = "py39"
6969
src = ["src"]
7070

7171
# https://docs.astral.sh/ruff/rules
@@ -89,6 +89,7 @@ select = [
8989
ignore = [
9090
"D401", # First line should be in imperative mood (remove to opt in)
9191
"D415", # First line should end with a period (remove to opt in)
92+
"B008",
9293
]
9394

9495
[tool.ruff.lint.per-file-ignores]

src/ilastik_tasks/ilastik_pixel_classification_segmentation.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import json
2424
import logging
2525
import os
26-
from typing import Any, Union
26+
from typing import Any, Optional
2727

2828
import anndata as ad
2929
import dask.array as da
@@ -83,7 +83,7 @@ def segment_ROI(
8383
foreground_class: int = 0,
8484
threshold: float = 0.5,
8585
min_size: int = 15,
86-
label_dtype: Union[np.dtype, None] = None,
86+
label_dtype: Optional[np.dtype] = None,
8787
relabeling: bool = True,
8888
) -> np.ndarray:
8989
"""Run the Ilastik model on a single ROI.
@@ -189,8 +189,8 @@ def ilastik_pixel_classification_segmentation(
189189
default_factory=IlastikChannel2InputModel
190190
),
191191
input_ROI_table: str = "FOV_ROI_table",
192-
output_ROI_table: Union[str, None] = None,
193-
output_label_name: Union[str, None] = None,
192+
output_ROI_table: Optional[str] = None,
193+
output_label_name: Optional[str] = None,
194194
use_masks: bool = True,
195195
# Ilastik-related arguments
196196
ilastik_model: str,
@@ -503,6 +503,24 @@ def ilastik_pixel_classification_segmentation(
503503
preprocessing_kwargs=preprocessing_kwargs,
504504
)
505505

506+
# Make 3D in case of 2D data
507+
# Check that the shape of the new label image matches the expected shape
508+
expected_shape = (
509+
e_z - s_z,
510+
e_y - s_y,
511+
e_x - s_x,
512+
)
513+
logger.info(f"Expected shape: {expected_shape}")
514+
if new_label_img.shape != expected_shape:
515+
try:
516+
new_label_img = np.broadcast_to(new_label_img, expected_shape)
517+
except Exception as err:
518+
raise ValueError(
519+
f"Shape mismatch: {new_label_img.shape} != {expected_shape} "
520+
"Between the segmented label image and expected shape in "
521+
"the zarr array."
522+
) from err
523+
506524
if output_ROI_table:
507525
bbox_df = array_to_bounding_box_table(
508526
new_label_img,
@@ -519,21 +537,6 @@ def ilastik_pixel_classification_segmentation(
519537
f"{len(overlap_list)} bounding-box pairs overlap"
520538
)
521539

522-
# Check that the shape of the new label image matches the expected shape
523-
expected_shape = (
524-
e_z - s_z,
525-
e_y - s_y,
526-
e_x - s_x,
527-
)
528-
if new_label_img.shape != expected_shape:
529-
try:
530-
new_label_img = da.broadcast_to(new_label_img, expected_shape)
531-
except:
532-
raise ValueError(
533-
f"Shape mismatch: {new_label_img.shape} != {expected_shape} "
534-
"Between the segmented label image and expected shape in the zarr array."
535-
)
536-
537540
# Compute and store 0-th level to disk
538541
da.array(new_label_img).to_zarr(
539542
url=mask_zarr,
-57.8 KB
Binary file not shown.

tests/test_ilastik_pixel_classification_segmentation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
IlastikChannel2InputModel,
1313
)
1414

15-
# TODO: add 2D testdata
16-
1715

1816
@pytest.fixture(scope="function")
1917
def ome_zarr_3d_url(tmp_path: Path, testdata_path: Path) -> str:
@@ -80,7 +78,9 @@ def test_ilastik_pixel_classification_segmentation_task_3D_single_channel(
8078
channel=IlastikChannel1InputModel(label="DAPI_2"),
8179
channel2=IlastikChannel2InputModel(label=None),
8280
ilastik_model=str(ilastik_model),
81+
input_ROI_table="well_ROI_table",
8382
output_label_name="test_label",
83+
output_ROI_table="test_ROI_table",
8484
relabeling=True,
8585
)
8686

@@ -93,6 +93,7 @@ def test_ilastik_pixel_classification_segmentation_task_3D_single_channel(
9393
channel=IlastikChannel1InputModel(label="DAPI_2"),
9494
channel2=IlastikChannel2InputModel(label="ECadherin_2"),
9595
ilastik_model=str(ilastik_model),
96+
input_ROI_table="well_ROI_table",
9697
output_label_name="test_label",
9798
relabeling=True,
9899
)
@@ -130,6 +131,7 @@ def test_ilastik_pixel_classification_segmentation_task_2D_single_channel(
130131
channel2=IlastikChannel2InputModel(label=None),
131132
ilastik_model=str(ilastik_model),
132133
output_label_name="test_label",
134+
output_ROI_table="test_ROI_table",
133135
relabeling=True,
134136
)
135137

0 commit comments

Comments
 (0)