Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
imagejan committed Mar 7, 2024
1 parent b714043 commit cf67ba2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ classifiers = [
dependencies = [
"cellpose",
"confuse",
"pyimagej",
"rich",
"scikit-image",
"scyjava",
"tqdm",
"typer"
]
Expand Down
30 changes: 30 additions & 0 deletions src/faim_wako_searchfirst/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,33 @@ def cellpose(
**kwargs,
)
return mask


def weka_classifier(
img,
classifier_path: Path,
logger=logging,
):
"""Apply a classifier.model file.
Use Trainable Weka Segmentation (via pyimagej)
to apply a trained classifier to the input image.
"""
import imagej
from scyjava import jimport

logger.info("Initializing ImageJ...")
ij = imagej.init("sc.fiji:fiji:2.15.0")

WekaSegmentation = jimport("trainableSegmentation.WekaSegmentation")

logger.info("Starting Trainable Segmentation plugin...")
segmenter = WekaSegmentation(ij.py.to_imageplus(img))
logger.info(f"Loading classifier from {classifier_path}.")
segmenter.loadClassifier(str(classifier_path))
segmenter.applyClassifier(False)
# get result imp
# convert imp from_java
result = ij.py.from_java(segmenter.getClassifiedImage())
ij.dispose()
return label(result, background=1).astype(np.uint16)
Binary file added tests/resources/classifier.model
Binary file not shown.
31 changes: 31 additions & 0 deletions tests/test_weka_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: 2024 Friedrich Miescher Institute for Biomedical Research (FMI), Basel (Switzerland)
#
# SPDX-License-Identifier: MIT
"""Test segment.weka_classifier functionality."""
from pathlib import Path

import numpy as np
import pytest
from faim_wako_searchfirst.segment import weka_classifier
from skimage.io import imread

SAMPLE_IMAGE_NAME = "worms_C01.tif"


@pytest.fixture
def _classifier_path():
return Path("tests/resources/classifier.model")


@pytest.fixture
def _sample_image():
return imread(Path("tests/resources/worms") / SAMPLE_IMAGE_NAME)


def test_weka_classifier(_sample_image, _classifier_path):
"""Directly call weka_classifier."""
result = weka_classifier(_sample_image, _classifier_path)
assert result.shape == (442, 442)
assert np.count_nonzero(result == 1) == 3
assert np.count_nonzero(result == 2) == 8
assert np.count_nonzero(result == 3) == 831

0 comments on commit cf67ba2

Please sign in to comment.