Skip to content

steinbock.segmentation

cellprofiler special

deepcell

deepcell_available

Application (Enum)

MESMER

try_segment_objects(img_files, application, model=None, channelwise_minmax=False, channelwise_zscore=False, channel_groups=None, aggr_func=<function mean at 0x7f08ca385af0>, **predict_kwargs)

Source code in steinbock/segmentation/deepcell.py
def try_segment_objects(
    img_files: Sequence[Union[str, PathLike]],
    application: Application,
    model: Optional["Model"] = None,
    channelwise_minmax: bool = False,
    channelwise_zscore: bool = False,
    channel_groups: Optional[np.ndarray] = None,
    aggr_func: Callable[[np.ndarray], np.ndarray] = np.mean,
    **predict_kwargs,
) -> Generator[Tuple[Path, np.ndarray], None, None]:
    app, predict = application.value(model=model)
    for img_file in img_files:
        try:
            img = io.read_image(img_file)
            if channelwise_minmax:
                channel_mins = np.nanmin(img, axis=(1, 2), keepdims=True)
                channel_maxs = np.nanmax(img, axis=(1, 2), keepdims=True)
                img = (img - channel_mins) / (channel_maxs - channel_mins)
            if channelwise_zscore:
                channel_means = np.nanmean(img, axis=(1, 2), keepdims=True)
                channel_stds = np.nanstd(img, axis=(1, 2), keepdims=True)
                img = (img - channel_means) / channel_stds
            if channel_groups is not None:
                img = np.stack(
                    [
                        aggr_func(img[channel_groups == channel_group], axis=0)
                        for channel_group in np.unique(channel_groups)
                        if not np.isnan(channel_group)
                    ]
                )
            mask = predict(img, **predict_kwargs)
            yield Path(img_file), mask
            del img, mask
        except:
            _logger.exception(f"Error segmenting objects in {img_file}")
Back to top