steinbock.segmentation
cellprofiler
special
cellprofiler
create_and_save_segmentation_pipeline(segmentation_pipeline_file)
Source code in steinbock/segmentation/cellprofiler/cellprofiler.py
def create_and_save_segmentation_pipeline(
segmentation_pipeline_file: Union[str, PathLike]
):
shutil.copyfile(
_segmentation_pipeline_file_template, segmentation_pipeline_file
)
run_object_segmentation(cellprofiler_binary, segmentation_pipeline_file, probabilities_dir, mask_dir, cellprofiler_plugin_dir=None)
Source code in steinbock/segmentation/cellprofiler/cellprofiler.py
def run_object_segmentation(
cellprofiler_binary: Union[str, PathLike],
segmentation_pipeline_file: Union[str, PathLike],
probabilities_dir: Union[str, PathLike],
mask_dir: Union[str, PathLike],
cellprofiler_plugin_dir: Union[str, PathLike, None] = None,
):
args = [
str(cellprofiler_binary),
"-c",
"-r",
"-p",
str(segmentation_pipeline_file),
"-i",
str(probabilities_dir),
"-o",
str(mask_dir),
]
if cellprofiler_plugin_dir is not None:
args.append("--plugins-directory")
args.append(str(cellprofiler_plugin_dir))
return run_captured(args)
deepcell
deepcell_available
Application
MESMER
run_object_segmentation(img_files, application, model=None, channelwise_minmax=False, channelwise_zscore=False, channel_groups=None, aggr_func=<function mean at 0x7faf1b5c6160>, **predict_kwargs)
Source code in steinbock/segmentation/deepcell.py
def run_object_segmentation(
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:
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