CellPatcher helps you turn microscopy images into reproducible patch datasets without wrestling with custom one-off scripts. It generates patch coordinates (and, if you want, extracted arrays) using segmentation-based cell patches, random patches, grid tiles, and whole-field patches. You can drive everything from interactive notebooks or the Python API.
Since v0.2.0 CellPatcher reads image and mask files only through a run of the
MorphoBricks preprocessing brick
(v0.5): describe the experiment and publish its description in the preprocessing notebook, run it
on your experiment first, then point CellPatcher to the run's output folder. A run without a
published description is refused (A_DESCRIPTION_REQUIRED). What changed, with the migration notes for the
other bricks, is in the CHANGELOG (v0.2.0rc1 to v0.6.0).
-
Normal user / manual workflow: open
notebooks/CellPatcher/CellPatcher.ipynband give it the preprocessing run's output folder. -
Choosing a patching strategy: open
notebooks/CellPatcher_Strategy_Examples/CellPatcher_Strategy_Examples.ipynb: each strategy on one image of your preprocessing run, nothing written. -
Training or feature extraction in PyTorch: open
notebooks/CellPatcher_Pytorch/CellPatcher_Pytorch.ipynb: the patches of an export as a batch of tensors. -
API copy-paste examples: read
docs/api_examples.md. -
Multidimensional C/Z/T data: read
docs/preprocessing.md. -
Normalizing intensities with the MorphoBricks preprocessing statistics: read
docs/normalization.md. -
Reads every image and mask of a folder or tree run through the preprocessing run (
RunInputs): its images, their FOV keys, the axis roles and the image/mask pairing it recorded, so every brick reads the same files the same way. Files the run did not record are refused, never left out. -
Generates patch regions (
PatchRegion) forcell,random,grid, andwhole_fovmodes. -
Optionally filters regions, extracts arrays, resizes outputs, and exports patch files.
-
Optionally clears pixels outside the selected segmented object in cell mode (
apply_mask=True). -
Writes patch metadata (CSV) alongside exported patch files when enabled. Every patch has a run-independent
patch_idbuilt from its position, object set, planes and label or rectangle, and aposition_id(see "Patch identity" indocs/configuration_reference.md). In-memory images given torun_patchingneedimage_metadata=[{"position_id": ...}](or animage_file); unnamed images are refused. -
Cell-mode masks are object sets: an
instancesmask is used as given; abinarymask (as the run's description declares it, or a boolean array in memory) is refused unless the request sets the explicitCellConfig.binary_mask_policy="connected_components"(no default), which labels it with the one shared labeller,cellpatcher.label_objects(mask, policy, connectivity). A file run asks its preprocessing run's resolver for the mask by its description ID (matching.mask_id). -
Reads TIFF and OME-Zarr images (those the preprocessing run accepted), patches time series plane by plane (Z-stacks must be projected in preprocessing first), and optionally normalizes patches with the MorphoBricks preprocessing statistics (
config.normalization, seedocs/normalization.md).
| Mode | Use it when... | Needs masks? | Typical output |
|---|---|---|---|
cell |
You have segmentation labels and want one patch per object/cell. | Yes | One patch per labeled object/cell |
random |
You want random fixed-size sampling for controls or coverage checks. | Optional | Random fixed-size patches |
grid |
You want deterministic tiling over the image. | No | Regular tiles (with optional overlap/partials) |
whole_fov |
You want one patch representing the entire field of view. | No | One patch covering the full image |
Options shared with the MorphoBricks bricks (issue #33).
random.seed_scopechooses which planes share rectangles:"plane_channel"is the default and today's behaviour: the seed includes thet,zandcthe image's metadata gives (channels split withsplit_channelsshare their region's rectangles either way);"plane": the seed leaves outc, so channels given as separate images get the same rectangles;"position": every plane of a position gets the same rectangles.
random.min_mask_fractionis the least share of a draw's pixels that must lie in the sampling mask. Left unset (the default), any overlap is accepted. An explicit0means the mask excludes nothing: the rectangles are those of a run without it (since 0.2.0, matrix finding F8).whole_fov.square_cover=Trueadds the squares covering the FOV for square-input models: modewhole_fov_square, id<parent>/sq<N>, withparent_id.
Every region also records:
requested_y0/y1/x0/x1, the rectangle before clipping to the image;patch_touches_image_border, andobject_touches_image_borderfor cell patches;- with
run_patching(..., fraction_masks=[{name: mask}, ...]), its share inside each named mask (mask_fractions, written asmask_fraction_<name>columns). Cell patches always carryown_object.
images/masks → CellPatcherConfig → run_patching → PatchRegion metadata + patch arrays and/or exported files
All four modes use the same orchestration function: run_patching. The notebooks build CellPatcherConfig for you and call the same backend API.
- Recommended:
notebooks/CellPatcher/CellPatcher.ipynb(app-style workflow and preferred manual testing path). Its four steps: the preprocessing run (and, only if the experiment was moved, where it is now), the settings, a preview of one image, and the export. It runs in JupyterLab and in Google Colab, where its first cell mounts Google Drive and installs CellPatcher's release tag. - Strategy examples:
notebooks/CellPatcher_Strategy_Examples/CellPatcher_Strategy_Examples.ipynbpreviews the cell, grid, random and whole-FOV strategies on one image of the same preprocessing run (the main notebook's preview, nothing written). - PyTorch:
notebooks/CellPatcher_Pytorch/CellPatcher_Pytorch.ipynbloads an export of the main notebook withExportedPatchTorchDatasetand shows the first batch (needs thetorchextra; Colab has PyTorch).
All three notebooks share the same first cell (LabConstrictor portability: JupyterLab and Colab, Google Drive on Colab, the install pinned to the release tag).
Open the notebook in Colab:
Common user-facing APIs
CellPatcherConfigand config dataclassesrun_patchingandrun_patching_pipelineread_arrayandvalidate_image_and_mask_foldersscan_cell_patch_sizeprepare_image_for_patchinganditer_image_planes_for_patching
Advanced helpers
- low-level strategy classes
- axis utilities
- extract/resize/export helper functions
- other lower-level utility APIs
- Start here: Quick start
- More usage patterns: API examples
- Full options and fields: Configuration reference
Local / JupyterLab:
- From the repository root, install the package:
python -m pip install -e . - Add extras as needed:
- tests:
python -m pip install -e .[test](with Ruff and the notebook-runner packages; addnotebookfor the notebook's tests) - notebooks:
python -m pip install -e .[notebook] - optional Torch adapter:
python -m pip install -e .[torch] - OME-Zarr images:
python -m pip install -e .[zarr](the MorphoBricks preprocessing and normalization bricks, used for discovery, pairing and normalization, are core dependencies)
- tests:
requirements.txtremains a pinned notebook/tooling environment snapshot.- Before a pull request:
python tools/check.py(pytest, Ruff and the MorphoBricks code rules against their shrink-only baseline,tools/code_rules_baseline.json);--rulesruns only the code rules and Ruff.
A release, anywhere (Google Colab included):
pip install "cellpatcher[notebook,zarr] @ git+https://github.com/CellMigrationLab/CellPatcher.git@v0.6.0"The notebook's first cell does this on Colab (pinned to its RELEASE_TAG), mounts Google Drive and
enables the widgets; keep the preprocessing run, the experiment and the output folder on Drive.
Patching modes are 2D Y/X; for C/Z/T data, see docs/preprocessing.md. Folder and tree runs split T with
config.common.t_policy ("all", "first", "select"). Z=1 passes; Z>1 is refused under every z_policy
(project or select first with convert --project in morphobricks-preprocessing).
Note: plane iterator metadata (the t/z indices) reaches PatchRegion rows and the export CSV when passed through as run_patching(..., image_metadata=...); the plane_id string itself is not mirrored onto PatchRegion and still needs an explicit join if you need it. See docs/preprocessing.md.
- CellPatcher does not segment cells; you must provide masks/labels for cell mode.
- CellPatcher does not run DINO/Torch inference in the core package.
- Users must provide valid images and, for cell mode, masks/labels.
-
Optional Torch dataset adapter is available via
cellpatcher.torch_dataset; Torch remains optional and DINO inference is not included in core (seedocs/api_examples.md).
LabConstrictor can prepare pull requests that keep this repository aligned with improvements in the main template, including updates to GitHub Actions workflows. Complete the one-time automatic synchronization setup to enable these updates.
