Skip to content

Commit 219936a

Browse files
committed
bug fixes; code cleanup
1 parent c69ef4a commit 219936a

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/ilastik_tasks/ilastik_pixel_classification_segmentation.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def segment_ROI(
9797
# Shape from (czyx) to (tzyxc)
9898
input_data = np.moveaxis(input_data, 0, -1)
9999
input_data = np.expand_dims(input_data, axis=0)
100-
print(f"{input_data.shape=}")
100+
logger.info(f"{input_data.shape=}")
101101
data = [
102102
{
103103
"Raw Data": PreloadedArrayDatasetInfo(
@@ -109,26 +109,22 @@ def segment_ROI(
109109
ilastik_output = shell.workflow.batchProcessingApplet.run_export(
110110
data, export_to_array=True
111111
)[0]
112-
logger.info(f"{ilastik_output.shape=}")
112+
logger.info(f"{ilastik_output.shape=} after ilastik prediction")
113113

114114
# Get foreground class and reshape to 3D
115-
ilastik_output = ilastik_output[..., foreground_class]
116-
ilastik_output = np.reshape(
117-
ilastik_output, (input_data.shape[1], input_data.shape[2], input_data.shape[3])
118-
)
119-
logger.info(f"{ilastik_output.shape=}")
115+
ilastik_output = np.squeeze(ilastik_output[..., foreground_class])
116+
print(f"{ilastik_output.shape=} after foreground class selection")
120117

121118
# take mask of regions above threshold
122-
ilastik_output[ilastik_output < threshold] = 0
123-
ilastik_output[ilastik_output >= threshold] = 1
119+
ilastik_labels = ilastik_output > threshold
124120

125121
# remove small holes
126-
ilastik_output = remove_small_holes(
127-
ilastik_output.astype(bool), area_threshold=min_size
122+
ilastik_labels = remove_small_holes(
123+
ilastik_labels, area_threshold=min_size
128124
)
129125

130126
# label image
131-
ilastik_labels = label(ilastik_output)
127+
ilastik_labels = label(ilastik_labels)
132128

133129
# remove objects below min_size - also removes anything with major or minor axis
134130
# length of 0 for compatibility with current measurements task (01.24)
@@ -141,10 +137,10 @@ def segment_ROI(
141137
or (label_props[i].axis_major_length < 1)
142138
or (label_props[i].major_axis_length < 1)
143139
]
144-
print(f"number of labels before filtering for size = {ilastik_labels.max()}")
140+
logger.info(f"number of labels before filtering for size = {ilastik_labels.max()}")
145141
ilastik_labels[np.isin(ilastik_labels, labels2remove)] = 0
146142
ilastik_labels = label(ilastik_labels)
147-
print(f"number of labels after filtering for size = {ilastik_labels.max()}")
143+
logger.info(f"number of labels after filtering for size = {ilastik_labels.max()}")
148144
label_props = regionprops(ilastik_labels)
149145

150146
return ilastik_labels.astype(label_dtype)
@@ -255,7 +251,7 @@ def ilastik_pixel_classification_segmentation(
255251
label=channel2.label,
256252
)
257253
if tmp_channel_2:
258-
ind_channel_c2 = tmp_channel.index
254+
ind_channel_c2 = tmp_channel_2.index
259255
else:
260256
return ValueError(f"Channel {channel2} could not be loaded.")
261257

@@ -382,7 +378,7 @@ def ilastik_pixel_classification_segmentation(
382378
chunks=chunks,
383379
dtype=label_dtype,
384380
store=store,
385-
overwrite=False,
381+
overwrite=overwrite,
386382
dimension_separator="/",
387383
)
388384

tests/test_ilastik_pixel_classification_segmentation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
from devtools import debug
66
from fractal_tasks_core.channels import ChannelInputModel
77

8-
from ilastik_tasks.ilastik_pixel_classification_segmentation import (
8+
from src.ilastik_tasks.ilastik_pixel_classification_segmentation import (
99
ilastik_pixel_classification_segmentation,
1010
)
1111

1212
# TODO: add 2D testdata
1313

14-
1514
@pytest.fixture(scope="function")
1615
def test_data_dir_3d(tmp_path: Path, zenodo_zarr_3d: list) -> str:
1716
"""
1817
Copy a test-data folder into a temporary folder.
1918
"""
19+
tmp_path = Path("/data/active/rhornb/fractal/pytest/")
2020
dest_dir = (tmp_path / "ilastik_data_3d").as_posix()
21+
if Path(dest_dir).exists():
22+
shutil.rmtree(dest_dir)
2123
debug(zenodo_zarr_3d, dest_dir)
2224
shutil.copytree(zenodo_zarr_3d, dest_dir)
2325
return dest_dir

0 commit comments

Comments
 (0)