Skip to content
Draft
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
9 changes: 6 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,11 @@ src/morphobricks_preprocessing/
he_tissue_mask.py the opt-in H&E tissue mask (Spec ADR 0005 BM15; RunInputs.he_tissue_mask and
preview_he_tissue_mask through run_inputs_tissue.py's lazy mixin, the he-mask
extra): the FOV resolved from the run, every refusal before any read, BM15's
lineage line and own retry comparator; he_tissue_rule.py the fixed rule
he_tissue_v1 (provisional) as one canonical record, computed from its record
lineage line and own retry comparator; he_tissue_rule.py the fixed rules
he_tissue_v1 and he_tissue_v2 (the default: v1 on the plane rescaled to its
white, estimate_white; both provisional) as canonical records, computed from
their record; he_tissue_run_facts.py the run facts of its lineage line
(the RGB header override item, the recorded pixel size)
(SciPy clean-up, 8-connected, disks clipped by the border); code_identity.py
the verified code identity (RECORD digest, PEP 610); lineage_schema.py the
reader's dispatch by operation and the duplicate-key refusal;
Expand Down Expand Up @@ -1098,7 +1101,7 @@ Technical details go to `logging` (shown in the notebook's "Technical details" p
| Histogram range (Q34, P6) | Step 3 **Settings**: the brick's `HistogramSettings` (`minmax` default, or `robust` with adjustable quantiles); recorded in the manifest's `settings.histogram`, any change recomputes every row. Every plane binned with `histogram_with_range_check` (memory estimate with `range_check=True`, through `planes._Binning`); `narrow_histogram_range` warnings from `range_check_issues`, one per channel, with the narrow planes recorded in the manifest's `narrow_histogram_ranges` so kept rows still report them. CSV schema 4 (underflow and overflow columns, zero with `minmax`); schema 3 tables recomputed. CLI `--histogram-range`, `--robust-quantiles`. Normalization brick pinned to v0.3.1 |
| Position identity (Q20, D02) | `position_id` = image name without extension, in every CSV row and manifest file entry; FOV key = labels + `position_id`; duplicates in one leaf folder are the error `duplicate_position_id`. CSV columns and schema 2 unchanged; manifest version 1 with an optional key |
| Masks (Q32) | Optional masks folder outside the experiment, one subfolder per mask type mirroring the experiment's folders, or the experiment's folders with `<image>_<type>.tif` (both may be mixed; `ambiguous_mask` when a name could fit two images or both layouts), paired by leaf folder + `position_id`; OME-Zarr label images are masks too. Header-only errors (`masks_folder_not_found`, `masks_inside_experiment`, `mask_names_differ_in_case`, `duplicate_mask`, `ambiguous_mask`, `unreadable_mask`, `mask_not_2d`, `mask_size_mismatch`, `mask_pixel_type`) and warnings (`mask_missing`, `mask_without_image`, `mask_outside_type_folder`) in `check`/`run`; manifest key `masks` (optional, version 1). Optional mask step (`masks` command, `scan_masks`): kinds, relations with evidence, direct-parent hierarchy, `mask_relationships.json` (report version 1); default thresholds the user may change (`thresholds=` keyword, one CLI option each, checked by `mask_thresholds`, written in the report); suggestions only, the user names and confirms. SciPy core dependency. Confirmed by the owner: 2D masks at the image's YX, integer/bool pixels, masks folder outside the experiment. Next: notebook step and a file of the user's answers; tag v0.2.0 proposed after merge. Plane identity (issue #36): masks may have Z and/or T named in their own metadata (never guessed: unnamed axes are `mask_not_2d`); the masks of an image share their planes (`mask_planes_differ`) and `run` checks them against the image's confirmed roles (`mask_planes_mismatch`, `mask_plane_issues`), so a 2D mask is refused for a Z-stack or time-lapse; manifest mask entries gain `axes` and `shape` (optional keys, version 1); `read_mask(z=, t=)` reads one plane; `scan_masks` scans plane by plane |
| H&E tissue mask (I1, BM15) | Opt-in only (`he-mask` extra; scikit-image, normalization's `stain` and the helper imported only when called). Fixed rule `he_tissue_v1`, provisional until its acceptance report is accepted; no per-call thresholds; sizes in pixels; colour maths from normalization only. One `uint8` region per image through BM12's publication with BM15's own line and comparator; an unverified code identity writes a mask never reused; freezing needs a new mask ID (design `docs/design/he-tissue-mask.md`, owner decisions of 16 November 2026) |
| H&E tissue mask (I1, BM15) | Opt-in only (`he-mask` extra; scikit-image, normalization's `stain` and the helper imported only when called). Fixed rules, provisional until an acceptance report is accepted: `he_tissue_v1` (white fixed at 255) and, the default since v0.10.1, `he_tissue_v2` (v1 on the plane rescaled to its white, the median of its brightest 0.1 %; v1 kept 0 % on grey-glass scans such as Zenodo 4550300); no per-call thresholds; sizes in pixels; colour maths from normalization only. One `uint8` region per image through BM12's publication with BM15's own line and comparator; an unverified code identity writes a mask never reused; freezing needs a new mask ID (design `docs/design/he-tissue-mask.md`, owner decisions of 16 November 2026) |
| Settings (ADR 0011, Q89) | `defaults.toml` (package data) declares the run's (`common`) and Cellpose's (`cellpose`) settings with default, unit and help; `morphobricks_preprocessing.settings` loads, modifies, saves and passes them with the five shared names on the vendored `_brick_settings.py` (never edited; re-vendored from the Spec); the classes, CLI defaults and notebook widgets are copies held equal by `tests/test_settings.py`; no Cellpose CLI option |
| Notebook style | Follow the lab's NucleiSky and LabChronicle notebooks (see §7) |
| Widget widths | **99%**, never 100% |
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ The design is [`docs/design-v0.4.md`](docs/design-v0.4.md), imported unchanged f
are in [docs/plan.md](docs/plan.md) §12. (The v0.3 to v0.4 migration guide was removed in
v0.5.1: no run before v0.5.1 is read.)

## Unreleased (v0.10.1)

### Fixed

- **The H&E tissue mask kept nothing on scans whose glass is not white.** On the Zenodo 4550300
H&E TMA cores (glass near (127, 135, 145), photometric tag MINISBLACK read with
`rgb_header_override = true`), `tissue-mask` printed "kept 0.0 % (nothing kept: an empty
region)" for every core, so fiberfeatures measured `roi_area_px = 0` and NaN features. The
channel order, the override and the mask's polarity were correct: rule `he_tissue_v1` fixes the
white at 255, so against it every pixel of these scans has an optical density of at least 0.22,
the background never fires, `H >= 0.05` holds everywhere and the whole plane was "nuclei".
New rule **`he_tissue_v2`** (provisional), the default of `RunInputs.he_tissue_mask`,
`RunInputs.preview_he_tissue_mask`, `he_tissue_mask_array`, `tissue-mask --rule` and the
notebook card: v1 on the plane rescaled to its own white, estimated per channel as the median
of the brightest 0.1 % of pixels (by R + G + B) and applied with normalization's
`rescale_to_white`. v1's thresholds and sizes are unchanged; v1 itself is unchanged and still
offered (`rule="he_tissue_v1"`). Kept share on cores 1B_D9 and 2B_A6: 0.0 % -> 46.8 % and
44.4 %; on white-glass slides (CMU-1) v2 is within 0.4 points of v1, and identical when the
brightest pixels are 255; blank slides keep nothing. A mask written under v1 is not reused under
v2 (another rule record): use a new output mask ID. `TissueMaskPreview.white` gives the white
used (`None` under v1). Regression fixture: a 320 x 320 crop of core 1B_D9
(`tests/data/he_core_1B_D9_crop.npz`, CC-BY-4.0, with its generator). Design:
`docs/design/he-tissue-mask.md` ("`he_tissue_v2`").

## v0.10.0 (3 December 2026)

Minor release with a **breaking change of file format**: the histogram table is
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ and installs missing requirements into the active kernel. A standalone
download installs from GitHub if needed (internet access and Git required).

The optional `he-mask` extra installs scikit-image (through `morphobricks-normalization[histology]`)
for the opt-in H&E tissue mask (`tissue-mask`, `RunInputs.he_tissue_mask`; rule `he_tissue_v1`,
provisional); without it, nothing of that helper is imported. `he_tissue_mask_array(rgb)` computes
for the opt-in H&E tissue mask (`tissue-mask`, `RunInputs.he_tissue_mask`; rule `he_tissue_v2`
by default, provisional); without it, nothing of that helper is imported. `he_tissue_mask_array(rgb)` computes
the same mask on a `uint8` `(Y, X, 3)` array with no run and writes nothing (no lineage; for runs,
use `tissue-mask`); see the [user guide](docs/user_guide.md#an-he-tissue-mask-optional) and
[contract §15.4](docs/contract.md#154-the-he-tissue-mask-of-an-array-with-no-run-roadmap-k3).
Expand Down
31 changes: 23 additions & 8 deletions docs/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,11 @@ time.
An opt-in helper, never run by default, computes a `binary` **region** mask from one declared
RGB image with a named, versioned, fixed rule whose colour maths is normalization's
(`docs/design/he-tissue-mask.md`). `RunInputs.he_tissue_mask(fov, *, output_mask_id,
rule="he_tissue_v1")` writes it and returns `TissueMaskResult` (`image`, `mask_id`, `path`,
rule="he_tissue_v2")` writes it and returns `TissueMaskResult` (`image`, `mask_id`, `path`,
`line`, `published`, `reusable`, `fractions`); `RunInputs.preview_he_tissue_mask(fov, *,
rule="he_tissue_v1")` computes it in memory and returns `TissueMaskPreview` (`E`, `H`, `od`,
`od_floored` exactly as normalization returns them, the components, `fractions`, the rule record
and the code identities); it writes nothing. The command `tissue-mask` and the notebook card
rule="he_tissue_v2")` computes it in memory and returns `TissueMaskPreview` (`E`, `H`, `od`,
`od_floored` exactly as normalization returns them, the components, `fractions`, the rule record,
the code identities and `white`, the white the plane was rescaled to, `None` under v1); it writes nothing. The command `tissue-mask` and the notebook card
**Make an H&E tissue mask** call them. scikit-image (the `he-mask` extra:
`pip install "morphobricks-preprocessing[he-mask]"`, which installs
`morphobricks-normalization[histology]`), normalization's `stain` module and the helper's module
Expand All @@ -1245,6 +1245,20 @@ means fewer than N pixels, a disk is `x*x + y*y <= r*r`, the structuring element
the image border (never padded or wrapped; the opening's erosion counts only pixels inside the
image), and every size is in pixels: no pixel size is read or used.

**The rule `he_tissue_v2` (v0.10.1, the default; also provisional)** is `he_tissue_v1` on the plane
rescaled to its own white. v1's white is fixed at 255, so on a scan whose glass is grey (the
Zenodo 4550300 TMA cores, glass near (127, 135, 145)) every pixel reads as stained and v1 keeps
0 %. v2 estimates the white per channel as the median of the pixels whose `R + G + B` is at or
above the 99.9th percentile of `R + G + B` (NumPy, method `linear`), at least 1; rescales the plane
with normalization's `rescale_to_white` (`clip(rint(I * 255 / white), 0, 255)` as `uint8`); and
applies v1's components, thresholds and clean-up unchanged to the rescaled plane (`E`, `H` and
`od` are then the rescaled plane's). When the plane's brightest pixels are white (255) the
rescaling is the identity and v2 equals v1. The white is read from the plane, so a plane with no
glass or empty space takes its brightest tissue as white. The rule record (version 2,
`input.white` `"estimated"`, a `white` entry stating the estimate and the rescaling) is written in
the lineage like v1's; the white itself is not a lineage key (it follows from the source content
digest and the rule record). `he_tissue_v1` stays offered unchanged with `rule="he_tissue_v1"`.

**Inputs, refused before any pixel is read** (a `ValueError` naming the position): the position
is resolved by its image path from `RunInputs.fovs()`, and a caller's `image_type`, axes, counts
or dtype are ignored; an image that is not the run's (`RunInputsError`); a unit not declared
Expand Down Expand Up @@ -1295,10 +1309,10 @@ object at any depth is refused in both schemas (it was kept silently before).

### 15.4 The H&E tissue mask of an array, with no run (roadmap K3)

`he_tissue_mask_array(rgb, *, rule="he_tissue_v1") -> TissueMaskArray` (owner decision of
`he_tissue_mask_array(rgb, *, rule="he_tissue_v2") -> TissueMaskArray` (owner decision of
17 November 2026: in preprocessing) computes §15.3's rule on one image array in memory, for
users who have pixels but no MorphoBricks run (a script, another brick's folder command). **The
rule is the same provisional `he_tissue_v1`**, and the computation is the run route's own: the
rules are the same provisional `he_tissue_v2` (the default) and `he_tissue_v1`**, and the computation is the run route's own: the
array is moved to `(3, Y, X)` and given to the one function `RunInputs.he_tissue_mask` and
`preview_he_tissue_mask` call after their read (`he_tissue_mask.rule_on_plane`), never a copy,
so the mask is bitwise equal to the file the run route writes for the same pixels (tested on the
Expand All @@ -1319,11 +1333,12 @@ a pixel type other than `uint8`, or a shape other than `(Y, X, 3)` (R, G, B last
(`ImportError` naming `pip install "morphobricks-preprocessing[he-mask]"`). The input checks
import nothing of the extra; scikit-image, normalization's `stain` module and the helper's
module are imported only past them, never by `import morphobricks_preprocessing`. Like the run
route it takes no pixel size (sizes are pixels), no white (fixed at 255) and no thresholds.
route it takes no pixel size (sizes are pixels), no white (fixed at 255 under v1, estimated from
the plane under v2) and no thresholds.

**Result.** `TissueMaskArray` (frozen): `mask`, boolean `(Y, X)`, `True` kept (a region, BM3;
being boolean, an in-memory consumer refuses it as an object set, BM4); and `record`, a frozen
`TissueMaskArrayRecord`: `rule_id` (`"he_tissue_v1"`), `operation` (`"he_tissue_mask"`),
`TissueMaskArrayRecord`: `rule_id` (`"he_tissue_v2"` by default), `operation` (`"he_tissue_mask"`),
`rule_text` (the complete rule record in its canonical JSON, keys sorted and no spaces: exactly
the text `rule_sha256` is the SHA-256 of, as UTF-8; `rule` returns it as a new dict, with
`rule_version`, `status` `"provisional"` and `provisional`), `rule_sha256`, `code` (BM15's code
Expand Down
59 changes: 59 additions & 0 deletions docs/design/he-tissue-mask.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,65 @@ changes v1. The complete rule record (below) and its SHA-256 are written to the
tuned per image, and nothing is tuned silently. The preview and the "% kept" figure are how the
user judges whether the rule suits their slides.

### `he_tissue_v2`: the white estimated from the plane (v0.10.1)

**Why (3 December 2026).** On the Zenodo 4550300 H&E TMA cores (set B of criterion 3; 2048 x 2048
`uint8` RGB, photometric tag wrongly MINISBLACK, read with `rgb_header_override = true`),
`tissue-mask` kept **0.0 %** of every core, so fiberfeatures measured `roi_area_px = 0`. The
channels are R, G, B in order (the override is applied before the rule) and the mask is not
inverted: the scanner's glass is near **(127, 135, 145)**, not 255, and the brightest pixel of
1B_D9 is (138, 154, 154). Against v1's fixed white of 255 every pixel has `OD >= 0.22`, so the
background (`max(OD) < 0.08`) never fires and `H >= 0.05` holds everywhere: the whole plane was
"nuclei" (`fractions` `nuclei` 1.0).

**The rule.** `he_tissue_v2` is v1 on the plane rescaled to its own white:

1. the white, per channel: the median of the pixels whose `R + G + B` is at or above the 99.9th
percentile of `R + G + B` (NumPy `percentile`, method `linear`), raised to at least 1
(`he_tissue_rule.estimate_white`; counting only, no colour maths);
2. the plane rescaled with normalization's `rescale_to_white` (N69:
`clip(rint(I * 255 / white), 0, 255)` as `uint8`, `rint` half to even);
3. v1's components, thresholds and clean-up, unchanged, on the rescaled plane (`E` and `H` from
`he_eosin`, `OD` against 255).

No threshold or size of v1 changed (the record's `components` are v1's). The record is version 2,
`input.white` `"estimated"`, with a `white` entry stating steps 1 and 2; v1 is unchanged and
still offered (`rule="he_tissue_v1"`). v2 is the default of `RunInputs.he_tissue_mask`,
`preview_he_tissue_mask`, `he_tissue_mask_array`, `tissue-mask` and the notebook card. Both are
provisional; the acceptance criteria above apply to v2 as they did to v1. The estimated white is
in the preview (`TissueMaskPreview.white`), not in the lineage line, whose source digest and rule
record determine it (the BM15 line keys are unchanged).

**Measured (3 December 2026, scikit-image 0.26.0, normalization v0.10.0).** Kept share:

| Image | v1 kept | v2 white | v2 kept | v2 background | v2 nuclei |
| --- | --- | --- | --- | --- | --- |
| Zenodo `1B_D9_WB.tif` (SHA-256 `8c74cadd…`) | 0.00 % | (130, 138, 149) | 46.8 % | 41.9 % | 11.3 % |
| Zenodo `2B_A6_WB.tif` (SHA-256 `6e49f686…`) | 0.00 % | (127, 134, 142) | 44.4 % | 52.6 % | 2.9 % |
| Zenodo `1B_F3_WB.tif` | 0.00 % | (136, 147, 168) | 52.6 % | 28.4 % | 19.1 % |
| Zenodo `2B_G5_WB.tif` | 0.00 % | (141, 143, 153) | 40.6 % | 44.7 % | 14.8 % |
| Zenodo `2B_H2_WB.tif` | 0.00 % | (141, 142, 152) | 38.3 % | 44.7 % | 17.0 % |
| Zenodo `2B_G3_WB.tif` | 0.00 % | (141, 143, 152) | 29.8 % | 66.4 % | 3.8 % |
| Zenodo `2B_E9_WB.tif` (a thin strip of tissue) | 0.00 % | (127, 134, 143) | 7.2 % | 85.8 % | 7.0 % |

(the seven cores of the record's `validation2.zip`, SHA-256 `f7eb874b…`). On CMU-1 (set A, white
glass), 33 tiles of 1024 x 1024 at level 1 with at least 5 % tissue: the estimated white is
251 to 255 in every channel and v2's kept share is within 0.4 points of v1's on every tile; the 12
level-1 tiles of 512 x 512 with the least near-white (2 to 7 %): Dice of v1 and v2 masks 0.978 to
0.998. Blank slides (uniform 255, glass at 242 or at (127, 135, 145) with noise): 0 % kept under
both rules. The PI-1 to PI-4 images (owner's, not public) were not rerun; their v1 numbers above
stand, and v2 equals v1 on any plane whose brightest 0.1 % are white.

**Limitation.** The white is read from the plane: a plane with no glass, lumen or fat (tissue
from edge to edge) takes its brightest tissue as white, and pale tissue then moves towards
background. The preview and the printed kept share show it; `rule="he_tissue_v1"` is the choice
for such planes on a white-balanced scanner.

**Regression fixture.** `tests/data/he_core_1B_D9_crop.npz`: rows 500-819, columns 80-399 of
`1B_D9_WB.tif` (CC-BY-4.0, Eliceiri, Li, Keikhosravi 2021, doi 10.5281/zenodo.4550300), written by
`tests/data/make_he_core_crop.py` from the checked zip. v1 keeps 0 % of it, v2 35.0 %
(`tests/test_he_tissue_v2.py`, also through a run with the header override).

### The canonical rule record

The rule is defined once, as an immutable record in the helper's module, and written whole into
Expand Down
Loading
Loading