Skip to content

Cellpose plugin

blockbuster.plugins.cellpose.cellpose_fn(model: str = 'cyto3', *, gpu: bool = False, diameter: float | None = None, do_3D: bool = False, channels: list[int] | None = None, channel_axis: int | None = None, **cellpose_kwargs: Any) -> Callable[[np.ndarray], np.ndarray]

Return a ready-to-use Cellpose function for tile_process.

One-liner convenience wrapper: combines model configuration and function creation into a single call.

Parameters:

Name Type Description Default
model str

Cellpose model type: "cyto3", "cyto2", "nuclei", etc.

'cyto3'
gpu bool

Use GPU for inference.

False
diameter float | None

Expected cell diameter in pixels. None → Cellpose auto-estimates.

None
do_3D bool

Run in 3-D mode. Each tile must contain the full z-stack — use auto_tile_shape_cellpose(do_3D=True) for appropriate tile shapes.

False
channels list[int] | None

Cellpose 3 only. [cytoplasm_channel, nucleus_channel] (1-based, 0 = greyscale). [0, 0] → greyscale. [1, 2] → cyto=ch1, nuc=ch2.

None
channel_axis int | None

Cellpose 4 only. Index of the channel axis in the input array. None → greyscale input.

None
**cellpose_kwargs Any

Extra kwargs forwarded to model.eval() (e.g. flow_threshold, cellprob_threshold, anisotropy).

{}

Returns:

Type Description
Callable[[ndarray], ndarray]

Picklable function ready for tile_process.

Examples:

Greyscale 2-D:

>>> fn = cellpose_fn("cyto3", gpu=True, diameter=30)
>>> result = tile_process("image.zarr", fn, tile_shape=(1, 2048, 2048), overlap=20)

Nuclear segmentation:

>>> fn = cellpose_fn("nuclei", diameter=15)
>>> result = tile_process("image.zarr", fn, channel=1)

3-D with anisotropy:

>>> fn = cellpose_fn("cyto3", gpu=True, do_3D=True, anisotropy=3.0, diameter=20)
>>> from functools import partial
>>> from blockbuster import auto_tile_shape_cellpose, tile_process
>>> tile_fn = partial(auto_tile_shape_cellpose, do_3D=True, use_gpu=True, diameter=20)
>>> result = tile_process("image.zarr", fn, tile_shape=tile_fn, overlap=10)
Source code in src/blockbuster/plugins/cellpose.py
def cellpose_fn(
    model: str = "cyto3",
    *,
    gpu: bool = False,
    diameter: float | None = None,
    do_3D: bool = False,
    channels: list[int] | None = None,
    channel_axis: int | None = None,
    **cellpose_kwargs: Any,
) -> Callable[[np.ndarray], np.ndarray]:
    """Return a ready-to-use Cellpose function for ``tile_process``.

    One-liner convenience wrapper: combines model configuration and function
    creation into a single call.

    Parameters
    ----------
    model:
        Cellpose model type: ``"cyto3"``, ``"cyto2"``, ``"nuclei"``, etc.
    gpu:
        Use GPU for inference.
    diameter:
        Expected cell diameter in pixels. ``None`` → Cellpose auto-estimates.
    do_3D:
        Run in 3-D mode. Each tile must contain the full z-stack — use
        ``auto_tile_shape_cellpose(do_3D=True)`` for appropriate tile shapes.
    channels:
        *Cellpose 3 only.* ``[cytoplasm_channel, nucleus_channel]`` (1-based,
        0 = greyscale). ``[0, 0]`` → greyscale. ``[1, 2]`` → cyto=ch1, nuc=ch2.
    channel_axis:
        *Cellpose 4 only.* Index of the channel axis in the input array.
        ``None`` → greyscale input.
    **cellpose_kwargs:
        Extra kwargs forwarded to ``model.eval()``
        (e.g. ``flow_threshold``, ``cellprob_threshold``, ``anisotropy``).

    Returns
    -------
    Callable[[ndarray], ndarray]
        Picklable function ready for ``tile_process``.

    Examples
    --------
    Greyscale 2-D:

    >>> fn = cellpose_fn("cyto3", gpu=True, diameter=30)
    >>> result = tile_process("image.zarr", fn, tile_shape=(1, 2048, 2048), overlap=20)

    Nuclear segmentation:

    >>> fn = cellpose_fn("nuclei", diameter=15)
    >>> result = tile_process("image.zarr", fn, channel=1)

    3-D with anisotropy:

    >>> fn = cellpose_fn("cyto3", gpu=True, do_3D=True, anisotropy=3.0, diameter=20)
    >>> from functools import partial
    >>> from blockbuster import auto_tile_shape_cellpose, tile_process
    >>> tile_fn = partial(auto_tile_shape_cellpose, do_3D=True, use_gpu=True, diameter=20)
    >>> result = tile_process("image.zarr", fn, tile_shape=tile_fn, overlap=10)
    """
    _require_cellpose()
    cfg = _make_config(model, gpu, channels, channel_axis, diameter, do_3D, **cellpose_kwargs)
    return partial(_run, cellpose_dict=cfg)