Skip to content

Commit

Permalink
Add apoc pixel classifier segmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
imagejan committed Mar 4, 2024
1 parent 0e3c635 commit 08d5c37
Show file tree
Hide file tree
Showing 8 changed files with 2,404 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

# # these libraries, along with pytest-xvfb (added in the `deps` in tox.ini),
# # enable testing on Qt on linux
# - name: Install Linux libraries
# if: runner.os == 'Linux'
# run: |
# sudo apt-get install -y libdbus-1-3 libxkbcommon-x11-0 libxcb-icccm4 \
# libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 \
# libxcb-xinerama0 libxcb-xinput0 libxcb-xfixes0

# # strategy borrowed from vispy for installing opengl libs on windows
# - name: Install Windows OpenGL
# if: runner.os == 'Windows'
# run: |
# git clone --depth 1 https://github.com/pyvista/gl-ci-helpers.git
# powershell gl-ci-helpers/appveyor/install_opengl.ps1

- name: Install Hatch
run: pip install --upgrade hatch

Expand Down
19 changes: 19 additions & 0 deletions config_apoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2024 Friedrich Miescher Institute for Biomedical Research (FMI), Basel (Switzerland)
#
# SPDX-License-Identifier: MIT

# Required
file_selection: # criteria for file selection in case of multiple channels/slices per position
channel: C01
process: # choose method how to segment, filter, and sample the objects
segment: apoc_classifier
filter: [area]
sample: centers

# Each subsequent section provides arguments to one of the methods defined in 'process'
area:
min_area: 100
max_area: 1000000
apoc_classifier:
classifier_path: "tests/resources/PixelClassifier.cl"
background_label: 2
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"apoc",
"cellpose",
"confuse",
"pyopencl",
"rich",
"scikit-image",
"tqdm",
Expand All @@ -45,11 +47,22 @@ Source = "https://github.com/fmi-faim/faim-wako-searchfirst"
source = "vcs"

[tool.hatch.envs.default]
platforms = ["windows", "macos"]
dependencies = [
"pandas",
"pytest",
"pytest-cov",
]

[tool.hatch.envs.linux]
platforms = ["linux"]
dependencies = [
"pandas",
"pocl-binary-distribution",
"pytest",
"pytest-cov",
]

[tool.hatch.envs.default.scripts]
cov = "pytest --cov-report=term-missing --cov-config=pyproject.toml --cov=faim_wako_searchfirst --cov=tests {args}"
no-cov = "cov --no-cov {args}"
Expand Down
23 changes: 22 additions & 1 deletion src/faim_wako_searchfirst/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Union

import numpy as np
from cellpose import models
from scipy.ndimage import binary_fill_holes
from skimage.filters import gaussian
from skimage.measure import label
Expand Down Expand Up @@ -61,6 +60,8 @@ def cellpose(
:return: a label image representing the detected objects
"""
from cellpose import models

logger.info(f"Load cellpose model: {pretrained_model}")
model: models.CellposeModel = models.CellposeModel(
pretrained_model=pretrained_model,
Expand All @@ -72,3 +73,23 @@ def cellpose(
**kwargs,
)
return mask


def apoc_classifier(
img,
classifier_path: Path,
background_label: int = None,
logger=logging,
):
"""Apply a PixelClassifier.cl file.
Use apoc (https://github.com/haesleinhuepf/apoc)
to apply a trained classifier to the input image.
"""
import apoc

clf = apoc.PixelClassifier(opencl_filename=classifier_path)
mask = clf.predict(image=img)
labeled_image, num_objects = label(mask, return_num=True, background=background_label)
logger.info(f"Found {num_objects} connected components.")
return labeled_image.astype(np.uint16)
Loading

0 comments on commit 08d5c37

Please sign in to comment.