Skip to content

H&E tissue mask: rule he_tissue_v2 estimates the white (0 % kept on grey-glass cores) - #622

Draft
guijacquemet wants to merge 2 commits into
mainfrom
claude/friendly-meitner-w5xoc5
Draft

guijacquemet wants to merge 2 commits into
mainfrom
claude/friendly-meitner-w5xoc5

Conversation

@guijacquemet

Copy link
Copy Markdown
Contributor

Summary

morphobricks-preprocessing tissue-mask (rule he_tissue_v1) kept 0.0 % of every H&E core of Zenodo 4550300 ("nothing kept: an empty region"). fiberfeatures then measured roi_area_px = 0 and NaN features. This PR adds rule he_tissue_v2 and makes it the default.

Diagnosis (reproduced on the real data)

Data: the record's validation2.zip (SHA-256 f7eb874b…), cores 1B_D9_WB.tif (8c74cadd…) and 2B_A6_WB.tif (6e49f686…): 2048 × 2048 uint8, YXS, photometric MINISBLACK, so the run uses rgb_header_override = true.

  • Not the channel order or the override: the planes are R, G, B in order, and a contrast-stretched picture is ordinary H&E.
  • Not an inverted mask, and not a scale or units issue.
  • The cause is the white. v1 fixes the white at 255. These scans' glass is near (127, 135, 145), and the brightest pixel of 1B_D9 is (138, 154, 154). Against white 255, every pixel has OD >= 0.22, so the background test (max(OD) < 0.08) never fires. H >= 0.05 also holds everywhere. The fractions were nuclei 1.0, kept 0.0: the whole plane was read as nuclei.

The fix: he_tissue_v2 (provisional)

v2 is v1 run on the plane after rescaling it to its own white:

  1. Estimate 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, method linear), raised to at least 1 (he_tissue_rule.estimate_white).
  2. Rescale with normalization's own rescale_to_white (N69: clip(rint(I*255/white), 0, 255) as uint8). No colour maths is added here.
  3. Apply v1's components to the rescaled plane. Thresholds, sizes and clean-up are all unchanged: the record's components are identical to v1's.

Version semantics follow the design ("changing any number gives he_tissue_v2 and never changes v1"):

  • The v2 record is version 2, with input.white set to "estimated" and a white entry stating steps 1 and 2.
  • v1 is unchanged and still offered (rule="he_tissue_v1").
  • Both rules are provisional.
  • A mask written under v1 is not reused under v2 (its rule record differs), so re-running into the same output mask ID is refused. Use a new ID.

By module:

  • he_tissue_rule.py: the v2 record, HE_TISSUE_V2, DEFAULT_RULE, estimate_white, estimates_white.
  • he_tissue_mask.py: rule_on_plane estimates the white and rescales before E, H and OD. The new field TissueMaskPreview.white holds the white used (None under v1). The lineage line's keys are unchanged; the white follows from the source content digest and the rule record.
  • he_tissue_run_facts.py (new, orchestration layer): the lineage's run facts (override_record, pixel_size_record), moved out of he_tissue_mask.py unchanged so that file stays under the 600-line code rule.
  • run_inputs_tissue.py, he_tissue_array.py, cli_tissue_mask.py, notebook_tissue_mask.py: default rule is v2, and the texts are updated.
  • Docs: design he-tissue-mask.md (a new "he_tissue_v2" section with the diagnosis, the numbers and the limitation), contract §15.3/§15.4, user guide, README, AGENTS.md, and CHANGELOG under Unreleased (v0.10.1).

Before / after: kept share

Image v1 kept v2 estimated white v2 kept
1B_D9_WB.tif 0.00 % (130, 138, 149) 46.8 %
2B_A6_WB.tif 0.00 % (127, 134, 142) 44.4 %
1B_F3_WB.tif 0.00 % (136, 147, 168) 52.6 %
2B_G5_WB.tif 0.00 % (141, 143, 153) 40.6 %
2B_H2_WB.tif 0.00 % (141, 142, 152) 38.3 %
2B_G3_WB.tif 0.00 % (141, 143, 152) 29.8 %
2B_E9_WB.tif (a thin strip of tissue on glass) 0.00 % (127, 134, 143) 7.2 %

I also inspected the v2 masks as overlays: glass, fat holes and lumens are excluded, nuclei are excluded and dilated, and stroma is kept.

White-glass slides (CMU-1.svs, Aperio, CC0, SHA-256 checked against the design):

  • On 33 level-1 tiles of 1024 px 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.
  • On the 12 densest 512 px tiles (2–7 % near-white), Dice between the v1 and v2 masks is 0.978–0.998.
  • Blank slides (pure 255; glass at 242; grey glass at (127, 135, 145), with and without noise) keep 0 % under v2.

Decisions (for the owner)

  • v2 is the default everywhere a rule is defaulted, because the owner's priority is H&E working end to end. This changes the pinned default in RunInputs.he_tissue_mask, preview_he_tissue_mask and he_tissue_mask_array (tests/test_brick_api_surface.py is updated). If you prefer to keep v1 as the default and make v2 opt-in, that is a one-line change of DEFAULT_RULE plus the three re-exported constants.
  • The estimator is the median of the brightest 0.1 % by R+G+B. It is chosen because it saturates at 255 on normal slides (so v2 equals v1 there) and finds the glass on these scans. I fixed the 99.9 percentile after trying 99 % and 99.9 %. 99 % moved CMU-1 dense tiles by up to 4 points; 99.9 % moved them by at most 0.4.

Notes for review

  • 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, which pushes pale tissue towards "background". The design says so; rule="he_tissue_v1" is the choice for such planes on a white-balanced scanner.
  • Not rerun: PI-1 to PI-4. They are the owner's images and not public. v2 equals v1 on any plane whose brightest 0.1 % are pure white, but whether that holds for those four WebP files was not checked.
  • Not verified: the fiberfeatures consumer gate (test_he_tissue_consumer_gate.py skips without fiberfeatures v0.7.0), and the notebook card in a real JupyterLab or Colab.
  • Follow-up, outside this repo: fiberfeatures' own he_eosin maps on these scans are still computed against white 255. The region mask is now right, but the eosin values measured inside it sit on a shifted baseline. That is a fiberfeatures or normalization question (normalization's he_eosin preset takes no white).
  • Regression fixture: tests/data/he_core_1B_D9_crop.npz, a 320 × 320 crop of 1B_D9 at full resolution (160 KiB). The record's licence is CC-BY-4.0, and the attribution and the checked digests are in the generator, tests/data/make_he_core_crop.py.

What the owner's local gate should check

On the same clean install, with v0.10.1 or this branch:

morphobricks-preprocessing tissue-mask --output <run folder> --to HETissueV2
  • Every core prints kept well above 0 %, about 47 % for 1B_D9_WB.tif and 44 % for 2B_A6_WB.tif, and never "nothing kept: an empty region" for a core with tissue.
  • The lineage line's rule.version is 2.
  • Use a new output mask ID: the earlier v1 masks are not reused.
  • After declaring the mask binary and running preprocessing and the masks step again, fiberfeatures with MaskRequest("HETissueV2", "region") should report roi_area_px > 0 and finite features.

Tests

  • New: tests/test_he_tissue_v2.py, 18 tests:
    • the record;
    • the estimate against a written-out definition;
    • v2 == v1 on the rescaled plane (5 images);
    • v2 == v1 when the brightest pixels are 255;
    • the preview's white;
    • regression on the real crop: v1 keeps 0 %, v2 keeps 25–60 % (35.0 %);
    • blank slides: less than 1 % kept;
    • the run route with a MINISBLACK-tagged TIFF and rgb_header_override = true.
  • Updated: v1-threshold tests now name rule="he_tissue_v1", and the pinned defaults and texts say v2.
  • python tools/check.py (ruff check, ruff format --check, pytest, code rules) at 1345159: 9288 passed, 24 skipped; code rules pass. Run locally: GitHub Actions is blocked.

Generated by Claude Code

…he plane

he_tissue_v1 fixes the white at 255. On the Zenodo 4550300 TMA cores the
glass is near (127, 135, 145), so every pixel read as stained (H >= 0.05
everywhere, no background) and tissue-mask kept 0.0 % of every core.

he_tissue_v2 estimates each channel's white as the median of the
brightest 0.1 % of pixels (by R+G+B), rescales with normalization's
rescale_to_white and applies v1's components unchanged. It is the new
default of RunInputs.he_tissue_mask, preview_he_tissue_mask,
he_tissue_mask_array, tissue-mask and the notebook card; v1 is unchanged
and still offered. Cores 1B_D9 and 2B_A6: 0.0 % -> 46.8 % and 44.4 % kept;
white-glass slides are unchanged within 0.4 points; blank slides keep
nothing.

Regression fixture: a 320 x 320 crop of core 1B_D9 (CC-BY-4.0) with its
generator script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AEzaRH4WH1TyJRA61
v2's rescaling moves to he_tissue_rule.white_rescaled, and the lineage's
run facts (the RGB header override item, the recorded pixel size) move
to the new module he_tissue_run_facts (orchestration layer). No
behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AEzaRH4WH1TyJRA61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants