Skip to content
Merged
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
| Tests | `python tools/check.py` (pytest, Ruff and the MorphoBricks code rules against `tools/code_rules_baseline.json`, design v0.4 §9); `PYTHONPATH=src python -m pytest -q` for the tests alone |
| Environment | `python -m pip install -e .` (add extras as needed: `.[test]`, `.[notebook]`, `.[torch]`) or `conda env create -f environment.yaml` |

Key dependencies: `numpy`, `scipy`, `tifffile`, `imageio`, and the MorphoBricks bricks `morphobricks-preprocessing` and `morphobricks-normalization` from GitHub at pinned tags (normalization `v0.7.0`; preprocessing `v0.6.1` — Spec ADR 0005 steps 2 and 2b: resolver contract v3, `read_objects`, the conversion helpers; v0.6.1 records the verified mask fingerprint) (core, required; normalization is declared at the tag the preprocessing tag pins, owner decision D-h, checked by `tests/test_dependency_pins.py`; histograms, the plane table, plane IDs, pooling and normalization parameters are imported from `morphobricks_normalization`, not from their deprecated preprocessing paths). Optional extras: `ipywidgets`, `matplotlib`, `pillow`, `tqdm`, `joblib`, `jl-hidecode` (notebook extra); `torch` (optional `cellpatcher.torch_dataset` adapter only — never a core dependency); `morphobricks-preprocessing[zarr]` at the same tag (`zarr` extra, formerly `preprocessing`: OME-Zarr images and OME-NGFF label masks).
Key dependencies: `numpy`, `scipy`, `tifffile`, `imageio`, and the MorphoBricks bricks `morphobricks-preprocessing` and `morphobricks-normalization` from GitHub at pinned tags (normalization `v0.7.1`; preprocessing `v0.7.0` — Spec ADR 0005 steps 2 and 2b: resolver contract v3, `read_objects`, the conversion helpers; v0.6.1 records the verified mask fingerprint; v0.7.0 raises `RunStoppedError` for a stopped run) (core, required; normalization is declared at the tag the preprocessing tag pins, owner decision D-h, checked by `tests/test_dependency_pins.py`; histograms, the plane table, plane IDs, pooling and normalization parameters are imported from `morphobricks_normalization`, not from their deprecated preprocessing paths). Optional extras: `ipywidgets`, `matplotlib`, `pillow`, `tqdm`, `joblib`, `jl-hidecode` (notebook extra); `torch` (optional `cellpatcher.torch_dataset` adapter only — never a core dependency); `morphobricks-preprocessing[zarr]` at the same tag (`zarr` extra, formerly `preprocessing`: OME-Zarr images and OME-NGFF label masks).

Current health snapshot (2026-11-16, v0.7.1 release prep: preprocessing `v0.6.0` (then `v0.6.1`, see the CHANGELOG) and normalization `v0.7.0` installed from their tags in a fresh venv, Linux): `.[test,zarr,notebook]` with CPU `torch` 2.15.0: 3000 passed, 0 skipped on Python 3.11 before #123, and 3004 passed, 0 skipped on Python 3.13 after it (`python tools/check.py`, code rules and Ruff clean; the smaller extras were not run locally). CI also runs `.[test]` on `windows-latest` (`pytest-windows`): 2899 passed, 42 skipped after #123, every skip an extra that job does not install (zarr, notebook, torch). Preprocessing refuses Z>1 inputs (2-D planes only), so the tests that built Z-stacks use T.

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Controls how patch arrays are extracted from images and optionally resized.
| `resize_order` | `int` | `1` | Spline interpolation order for resizing (0 = nearest, 1 = bilinear, 3 = bicubic). Must be `>= 0`. |
| `apply_mask` | `bool` | `False` | **Cell mode only.** When `True`, pixels outside the selected cell label are set to `mask_fill_value` before resize/export. |
| `mask_fill_value` | `float` | `0.0` | Fill value for pixels outside the selected label when `apply_mask=True`. |
| `split_channels` | `bool` or `list[str]` | `False` | When `True`, every region becomes one single-channel patch per channel of its image instead of one multichannel patch (requires a `C` axis in `common.image_axes`). A list of **canonical channel IDs** (for example `["DNA"]` or `["DNA", "ACTIN"]`) gives one patch per named channel only, in each image's C order. Channels are chosen by ID only, never by position (owner decision of 13 November 2026): a list of indices such as `[1]` is not a valid value. Each ID is the C index it has **in each image**, by the image's own channel IDs: in a folder or tree run those of the run's description (CP-3), where an ID some image lacks, or no image has, is refused by the run's resolver before anything is read (`A_SCOPE_INCOMPLETE`, "present in k of n acquisitions"; no subset is taken); for in-memory images `image_metadata[i]["channel_ids"]`, one ID per C index (an image without them, or without a named ID, is refused before any patch is made). Every split patch's `channel_id` column names its channel. Channel patches keep the region's coordinates and `position_id`, and get `c` = channel index (a `c<index>` level of `patch_id`; see "Patch identity" below). Filters are evaluated on the whole multichannel region before splitting; masking and resizing are applied before splitting too. |
| `split_channels` | `bool` or `list[str]` | `False` | When `True`, every region becomes one single-channel patch per channel of its image instead of one multichannel patch (requires a `C` axis in `common.image_axes`). A list of **canonical channel IDs** (for example `["DNA"]` or `["DNA", "ACTIN"]`) gives one patch per named channel only, in each image's C order. Channels are chosen by ID only, never by position (owner decision of 13 November 2026): a list of indices such as `[1]` is not a valid value. Each ID is the C index it has **in each image**, by the image's own channel IDs: in a folder or tree run those of the run's description (CP-3), where an ID some image lacks, or no image has, is refused by the run's resolver before anything is read (`A_SCOPE_INCOMPLETE`, "present in k of n images" since preprocessing v0.7.0; no subset is taken); for in-memory images `image_metadata[i]["channel_ids"]`, one ID per C index (an image without them, or without a named ID, is refused before any patch is made). Every split patch's `channel_id` column names its channel. Channel patches keep the region's coordinates and `position_id`, and get `c` = channel index (a `c<index>` level of `patch_id`; see "Patch identity" below). Filters are evaluated on the whole multichannel region before splitting; masking and resizing are applied before splitting too. |

> `apply_mask=True` is only supported in `mode="cell"` and requires masks to be provided.

Expand Down
3 changes: 2 additions & 1 deletion docs/normalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before.

## The workflow

1. **Install** CellPatcher: the preprocessing (v0.6.1) and normalization (v0.7.0) bricks are core
1. **Install** CellPatcher: the preprocessing (v0.7.0) and normalization (v0.7.1) bricks are core
dependencies. OME-Zarr images need the optional extra `pip install "cellpatcher[zarr]"` (the
notebook's first cell installs it on Google Colab).
2. **Describe the experiment and run the preprocessing notebook** on your experiment folder: publish
Expand Down Expand Up @@ -194,6 +194,7 @@ planes and what to do:
| *The preprocessing run in ... cannot be used: The experiment holds ... that the preprocessing run did not record* (`unrecorded_files`) | Since preprocessing v0.4.0rc2: images or masks were added to the experiment (or masks folder) after the run. None is left out: run preprocessing again on the experiment as it is, or exclude the images there. |
| *... the zarr package is not installed* (`zarr_extra_missing`) | The run holds OME-Zarr images: `pip install "cellpatcher[zarr]"`. |
| *The preprocessing run in ... cannot be used* (`preprocessing_run_refused`) | The preprocessing brick's `RunInputs` refuses the run (since v0.2.0): no `run_manifest.json` next to the table, a run written before preprocessing v0.4.0, a table or description changed since the run, or an image or mask changed or missing since the run where CellPatcher reads it. Run preprocessing v0.5 on the experiment as it is now; an experiment only moved or copied (times kept) since its run is read where it is now when CellPatcher is given that folder and its masks folder (since CP-2a). |
| *RUN_FOLDER_STOPPED: the preprocessing run in ... stopped with ...* (`RUN_FOLDER_STOPPED`), then preprocessing's own errors | Since preprocessing v0.7.0 (issue #126): preprocessing stopped in that folder without writing a run, for example Spec `v29` (RGB units beside fluorescence units, `R_HETEROGENEOUS_LAYOUT_NOT_ENABLED`). CellPatcher passes preprocessing's `RunStoppedError` on unchanged (its code and message, then the stopped run's errors with their codes, messages, paths and suggested actions, in order; Spec ADR 0005) and adds no advice of its own: do what those errors say. A report preprocessing cannot read there is `RUN_FOLDER_REPORT_UNREADABLE`, naming the report, also unchanged. CellPatcher never reads `discovery_report.json`. A folder with neither a run nor a stopped run's report is `preprocessing_run_refused` (give a written run, or run preprocessing first). |
| *The preprocessing run in ... has no published experiment description* (`A_DESCRIPTION_REQUIRED`) | Every run needs one (Spec ADR 0002 AR10): describe the experiment and publish its description in the preprocessing notebook, run preprocessing again and give CellPatcher that run. |
| *normalization.plane_histograms/manifest names ...* (`not_the_run_output`) | Only the run's own files, unrenamed in its output folder, are read. Give that folder's `plane_histograms.csv` (and leave `manifest` empty). |
| *... these images of the run have no repeat group* (`missing_repeat_group`) / *... several repeat groups to the images of one condition and biological-repeat folder* (`repeat_group_conflict`) | The `biological_repeat` scope pools the repeat groups of the run's description: run preprocessing again with its published description. `normalization.experiment_design` was removed (the derived `experiment_design.csv` is never read). |
Expand Down
2 changes: 1 addition & 1 deletion docs/normalization_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ brick's

| Rule | Detail |
|---|---|
| Dependencies | `morphobricks-preprocessing` (`v0.6.1` since CellPatcher v0.7.1; `v0.6.0` in v0.7.0, Spec ADR 0005 steps 2 and 2b; `v0.5.6` in v0.6.2; `v0.5.5` in v0.6.1; `v0.5.3` in v0.6.0; `v0.5.2` in v0.5.0; `v0.5.1` in v0.4.0; `v0.5.0` in v0.3.0; `v0.4.0` in v0.2.0, `v0.4.0rc5` in v0.2.0rc3, `v0.4.0rc4` in v0.2.0rc2, `v0.4.0rc2` in v0.2.0rc1) and `morphobricks-normalization` (`v0.7.0` since CellPatcher v0.7.0, the tag preprocessing v0.6.0 and v0.6.1 pin; `v0.6.1` in CellPatcher v0.6.1 and v0.6.2, the tag preprocessing v0.5.5 and v0.5.6 pin; `v0.6.0` in CellPatcher v0.6.0, the tag preprocessing v0.5.3 pins; `v0.5.0` in CellPatcher v0.5.0, the tag preprocessing v0.5.2 pins; `v0.4.0` in CellPatcher v0.4.0, the tag preprocessing v0.5.1 pins; `v0.3.4` in v0.2.0 and v0.3.0: the tag preprocessing `v0.5.0` pins; `v0.3.3` in v0.2.0rc3, `v0.3.2` before) are core dependencies, pinned to tags in `setup.py`; normalization at the tag the preprocessing tag pins (owner decision D-h, checked by `tests/test_dependency_pins.py`). The `zarr` extra (`pip install "cellpatcher[zarr]"`, formerly `preprocessing`) installs `morphobricks-preprocessing[zarr]` at the same tag, for OME-Zarr images. Histograms, the plane table, plane IDs, pooling and normalization parameters, which moved to the normalization brick, are imported from `morphobricks_normalization`. |
| Dependencies | `morphobricks-preprocessing` (unreleased: `v0.7.0`, `RunStoppedError`; `v0.6.1` in CellPatcher v0.7.1; `v0.6.0` in v0.7.0, Spec ADR 0005 steps 2 and 2b; `v0.5.6` in v0.6.2; `v0.5.5` in v0.6.1; `v0.5.3` in v0.6.0; `v0.5.2` in v0.5.0; `v0.5.1` in v0.4.0; `v0.5.0` in v0.3.0; `v0.4.0` in v0.2.0, `v0.4.0rc5` in v0.2.0rc3, `v0.4.0rc4` in v0.2.0rc2, `v0.4.0rc2` in v0.2.0rc1) and `morphobricks-normalization` (unreleased: `v0.7.1`, the tag preprocessing v0.7.0 pins; `v0.7.0` in CellPatcher v0.7.0 and v0.7.1, the tag preprocessing v0.6.0 and v0.6.1 pin; `v0.6.1` in CellPatcher v0.6.1 and v0.6.2, the tag preprocessing v0.5.5 and v0.5.6 pin; `v0.6.0` in CellPatcher v0.6.0, the tag preprocessing v0.5.3 pins; `v0.5.0` in CellPatcher v0.5.0, the tag preprocessing v0.5.2 pins; `v0.4.0` in CellPatcher v0.4.0, the tag preprocessing v0.5.1 pins; `v0.3.4` in v0.2.0 and v0.3.0: the tag preprocessing `v0.5.0` pins; `v0.3.3` in v0.2.0rc3, `v0.3.2` before) are core dependencies, pinned to tags in `setup.py`; normalization at the tag the preprocessing tag pins (owner decision D-h, checked by `tests/test_dependency_pins.py`). The `zarr` extra (`pip install "cellpatcher[zarr]"`, formerly `preprocessing`) installs `morphobricks-preprocessing[zarr]` at the same tag, for OME-Zarr images. Histograms, the plane table, plane IDs, pooling and normalization parameters, which moved to the normalization brick, are imported from `morphobricks_normalization`. |
| Version | Installed from GitHub at a pinned commit or tag, recorded in `setup.py` and raised deliberately. |
| Formats | Since v0.2.0 (CP-1) the preprocessing run is read through the brick's `RunInputs` (its design v0.4 §6), which needs `run_manifest.json` manifest version `2` (preprocessing v0.4), or `3` for a run with the brick's `--verify-content` (preprocessing v0.4.0rc5, CellPatcher v0.2.0rc3), or `4` for a described run (preprocessing v0.5.0, CellPatcher v0.3.0, which refuses a run without a published description: `A_DESCRIPTION_REQUIRED`); since preprocessing v0.5.1 only `4` is read, and since preprocessing v0.5.2 (the W3 pins) only `5`, whose file entries record their `channel_ids` (an older manifest is refused, `preprocessing_run_refused`, never converted) and `RunInputs.open` itself refuses a run without a description, and the `plane_histograms.csv` version that brick reads; CellPatcher pins no version of its own. Any run `RunInputs` refuses is an error. |
| Needed for | Normalization (every scope), OME-Zarr images and labels, and plane-by-plane image reading. |
Expand Down
10 changes: 9 additions & 1 deletion docs/patching_modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ In memory (`run_patching`, the object scan and `CellSegmentationStrategy` called
integer NumPy array, used as stored: a boolean, float or other array, a list or None is `A_MASK_NOT_INSTANCES`, and
`binary_mask_policy` is refused. Labels above the int32 maximum are refused.

> **An in-memory 0/1 mask is one object (decision D47, kept on 17 November 2026, issue #124).** An array in
> memory carries no representation, and its values cannot tell a region from instance labels, so CellPatcher
> takes every integer array as instance labels and never labels it: a 0/1 or 0/255 integer mask is **one**
> object, however many separate regions it has. A region such as a tissue mask passed to cell mode therefore
> gives one large patch, without a refusal. Pass instance labels (preprocessing's `label_binary` in memory, or its recorded
> `binary_to_instances` conversion), or use a described file run, where a mask declared `binary` is refused as
> objects unless `binary_mask_policy` is set. See `docs/plan.md` §13, D47.

Key parameters to know:

- `patch_size`
Expand All @@ -64,7 +72,7 @@ Notes:
Common pitfalls:

- Wrong `image_axes` / `mask_axes`.
- Passing a binary mask: give instance labels in memory; in a file run declare it `binary` and set `binary_mask_policy="connected_components"` (whose computed labels are not the mask's values).
- Passing a binary mask: give instance labels in memory (a 0/1 integer array is one object, D47); in a file run declare it `binary` and set `binary_mask_policy="connected_components"` (whose computed labels are not the mask's values).
- Picking a global patch size without scanning first.
- Expecting border-touching objects when `exclude_border=True` (default): it skips objects whose *bounding boxes* touch the image border, not patches.

Expand Down
19 changes: 19 additions & 0 deletions docs/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,22 @@ Reviewed in October 2026.
`Normalization_Scale_C<i>` pair per channel; earlier tables are still read on load (N47).
15. **[D15] Filters.** *Decided:* keep filters on raw values.
16. **[D16] Changed images.** *Decided:* block the run.

Decision recorded later, kept here with its reason:

- **[D47] An in-memory integer mask is instance labels, used as stored** (owner decision of
2026-10-26; *kept* by the owner on 17 November 2026, issue #124). In memory (`run_patching`,
the object scan, `CellSegmentationStrategy`) a cell-mode mask is a plain integer NumPy array
whose every non-zero value is one object, so a 0/1 or 0/255 integer mask is **one** object
(label 1 or 255), however many separate regions it has. *Why it is kept:* an in-memory array
carries no representation, and its pixels cannot prove instance semantics (Spec ADR 0005, P05:
a 0/1 `instances` mask is valid), so CellPatcher has nothing to tell a region from a one-object
label image by; guessing from the values would label some masks silently, which ADR 0005 BM2/BM4
forbid (CellPatcher labels no mask). The representation is declared where it exists: in a
described file run, a mask declared `binary` is refused as objects (`A_MASK_NOT_INSTANCES`)
unless the request sets `binary_mask_policy` explicitly. A boolean or float array is still
refused in memory (`A_MASK_NOT_INSTANCES`). *Consequence, documented for users:* a region (for
example a tissue mask) passed in memory to cell mode becomes one large "cell"; pass instance
labels instead (preprocessing's `label_binary` in memory, or its recorded
`binary_to_instances` conversion). The rejected alternative was a `representation=` argument on every in-memory call (a breaking API
change). `tests/test_cell_strategy.py` pins the behaviour through `run_patching`.
Loading
Loading