Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating cellpose args documentation and fixing cellpose import #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ultrack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Cellpose and ultrack had conflicts due to torch/cuda leading to Segmentation Fault
# importing Cellpose first avoids the issue, https://github.com/royerlab/ultrack/issues/108
try:
from cellpose.models import Cellpose # noqa: F401
except (ImportError, ModuleNotFoundError):
pass

# ignoring small float32/64 zero flush warning
warnings.filterwarnings("ignore", message="The value of the smallest subnormal for")

Expand Down
28 changes: 21 additions & 7 deletions ultrack/imgproc/segmentation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import logging
from typing import Optional
from typing import Callable, Optional

import edt
import numpy as np
Expand Down Expand Up @@ -211,20 +212,33 @@ def inverted_edt(
return dist


def _maybe_wrap(wrapper_name: str) -> Callable:
"""Wraps function with cellpose model method if cellpose is available."""
try:
from cellpose.models import CellposeModel as _Cellpose
except ImportError:
return lambda x: x

return functools.wraps(getattr(_Cellpose, wrapper_name))


class Cellpose:
@_maybe_wrap("__init__")
def __init__(self, **kwargs) -> None:
"""See cellpose.models.Cellpose documentation for details."""
from cellpose.models import CellposeModel as _Cellpose
try:
from cellpose.models import CellposeModel as _Cellpose
except ImportError as e:
raise ImportError(
"Cellpose not found, please install it."
"See for instructions https://github.com/MouseLand/cellpose"
) from e

if "pretrained_model" not in kwargs and "model_type" not in kwargs:
kwargs["model_type"] = "cyto"

self.model = _Cellpose(**kwargs)

@_maybe_wrap("eval")
def __call__(self, image: ArrayLike, **kwargs) -> np.ndarray:
"""
Predicts image labels.
See cellpose.models.Cellpose.eval documentation for details.
"""
labels, _, _ = self.model.eval(image, **kwargs)
return labels
Loading