+
Skip to content
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
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载