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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ are unchanged; one optional output-folder file (`ignored_masks.toml`) and one op
key (`ignored_masks`) are new. A v0.10.0 reader of a run that uses them ignores the key and so
refuses the skipped label images as unrecorded: refused, never silently wrong.

### Changed

- **Command-line wording (independent review of v0.10.1, owner answers of 4 December 2026).**
Every subcommand has a description and examples in `--help`; option help is rewritten in plain
language. Success lines say what was saved and where, then print the next commands with the
user's own options, shell-quoted (`describe --publish`, `check`, `run`, `convert`,
`calibrate`, `declare-masks`, `tissue-mask`, `masks`). Refusals say what was refused, why and
what to do, with commands on their own lines (`A_DESCRIPTION_REQUIRED`, ID reasons in
`describe --id`, undeclared mask types with each representation's meaning, an unreadable
`ignored_masks.toml`, projection, conversion and tissue-mask refusals); the `zarr` install line
names the installed release. Issue codes, severities and paths are unchanged. README and user
guide follow (complete workflow, both masks-folder layouts, `plane_histograms.parquet`).
`cli_run_options` and `cli_describe_options` hold option definitions moved out of `cli_run`
and `cli_describe` (line limit); `parse_size` and `add_describe_parser` are re-exported.

### Fixed

- **OME-Zarr copies are written in spatial chunks, so a later brick can read them in windows.**
Expand Down
128 changes: 83 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

[![CI](https://github.com/CellMigrationLab/morphobricks-preprocessing/actions/workflows/ci.yml/badge.svg)](https://github.com/CellMigrationLab/morphobricks-preprocessing/actions/workflows/ci.yml)

The preprocessing brick of MorphoBricks. It reads an experiment folder of
microscopy TIFFs, computes one intensity histogram per channel of every plane, and writes
them, with each plane's experimental labels, to `plane_histograms.csv`: one row per plane
(an image at one time point), each channel in its own columns. MorphoBricks v1 analyses 2-D
planes: a Z-stack (more than one depth slice) is refused (`z_stack_not_supported`); make
recorded 2-D projected copies first with `convert --project max|mean|sum`, and use the copies. Later bricks
(for example patch extraction) use that table to calculate normalization
parameters for any group of images without rereading the pixels.
MorphoBricks preprocessing is the first step before analysing a microscopy experiment. It
records experimental conditions, repeats, channels, pixel sizes and masks, checks the images,
and prepares the files used by downstream MorphoBricks analysis tools.

Its main output is `plane_histograms.parquet`: the intensity histograms of every image plane,
with its experimental labels and channel IDs. The table contains one row per plane and stores
each channel's histogram as lists of bin indices and counts. Later MorphoBricks tools read this
file to calculate normalisation parameters without rereading the image pixels. From v0.10.0,
this file replaces `plane_histograms.csv`. To update an older preprocessing run, run
preprocessing again.

MorphoBricks v1 analyses 2-D planes: a Z-stack (more than one depth slice) is refused
(`z_stack_not_supported`); make recorded 2-D projected copies first with
`convert --project max|mean|sum`, and use the copies.

The brick never modifies source images and never applies a normalization itself.
The histograms, the table format, pooling and the normalization methods come from the
Expand Down Expand Up @@ -50,16 +56,16 @@ Pre-alpha; the current release is v0.10.0 (what is planned next is in
- A step-by-step notebook ([notebooks/MorphoBricks_Preprocessing.ipynb](notebooks/MorphoBricks_Preprocessing.ipynb))
and a command line (`morphobricks-preprocessing check` / `run` / `masks` / `calibrate` /
`describe` / `convert`)
- **Every run is described** (v0.5.1): the output folder must hold a published experiment
description, `morphobricks.toml`, written by the notebook's describe steps (*Check and
save*). A run without one is refused with `A_DESCRIPTION_REQUIRED` on every route.
- **Every run is described:** publish the experiment description using `describe --publish`
or the notebook. Every preprocessing run requires a published description
(`morphobricks.toml`); a run without one is refused with `A_DESCRIPTION_REQUIRED`.

Every feature, message code and output file is described in the [user guide](docs/user_guide.md).

From the normalization brick (`morphobricks_normalization`, a required dependency, pinned
to its release v0.8.0): per-plane histograms with the robust range and the range check
(`histogram`, `ranges`), the histogram table (`csv_io`, schema version 5: channels by canonical
ID, so acquisitions may have different channels), pooled group statistics
to its release v0.10.0): per-plane histograms with the robust range and the range check
(`histogram`, `ranges`), the histogram table (`parquet_io`, `plane_histograms.parquet` schema
version 6: channels by canonical ID, so acquisitions may have different channels), pooled group statistics
(`pooling`) and normalization parameters (`normalization`); import them from
`morphobricks_normalization` (the deprecated paths through `morphobricks_preprocessing` were
removed in v0.4.0).
Expand All @@ -81,8 +87,17 @@ contract version 3 and an objects use with `RunInputs.read_objects`. See the

## Installation

You need Python 3.11 or newer, Git and an internet connection. We recommend installing into a
new Python virtual environment. JupyterLab is not required for command-line use.

```bash
pip install "git+https://github.com/CellMigrationLab/morphobricks-preprocessing.git@v0.10.0"
python -m pip install "morphobricks-preprocessing @ git+https://github.com/CellMigrationLab/morphobricks-preprocessing.git@v0.10.1"
```

For OME-Zarr images or the H&E tissue mask, install the extras too:

```bash
python -m pip install "morphobricks-preprocessing[zarr,he-mask] @ git+https://github.com/CellMigrationLab/morphobricks-preprocessing.git@v0.10.1"
```

For development, from a clone:
Expand All @@ -92,7 +107,7 @@ pip install -e ".[dev]"
```

The optional `notebook` extra installs the widget dependencies for the notebook
(`pip install "morphobricks-preprocessing[notebook] @ git+https://github.com/CellMigrationLab/morphobricks-preprocessing.git@v0.10.0"`).
(`python -m pip install "morphobricks-preprocessing[notebook] @ git+https://github.com/CellMigrationLab/morphobricks-preprocessing.git@v0.10.1"`).
The notebook's first code cell (**1. Install**) can set this up automatically: it uses a nearby
checkout or an installed package only when it is the pinned `RELEASE_REF` commit (never by version alone),
and installs missing requirements into the active kernel. A standalone
Expand Down Expand Up @@ -143,12 +158,7 @@ LabConstrictor's
JupyterLab/LabConstrictor before their release candidate is tagged; see
[docs/colab-release-check.md](docs/colab-release-check.md).

From a terminal:

```bash
morphobricks-preprocessing check /data/exp1
morphobricks-preprocessing run /data/exp1 --axes ZCYX --image-type fluorescence
```
From a terminal, see [Command line](#command-line) below.

## Running an experiment

Expand Down Expand Up @@ -185,30 +195,57 @@ the [user guide](docs/user_guide.md) §9, "Settings".

## Command line

A complete workflow. This example assumes that every image contains collagen and fibronectin
in that order, that the image axes are named correctly, and that your biological repeats were
prepared and acquired comparably. Replace the channel names and date with your own. Review the
proposed IDs before accepting them. If these assumptions do not fit your experiment, use the
relevant instructions in the [user guide](docs/user_guide.md) §5.

```bash
morphobricks-preprocessing describe my_experiment # preview: proposed IDs, open questions
morphobricks-preprocessing describe my_experiment --channels COL,FN --image-type channels \
--accept-ids --comparable --confirmed-on 2026-12-04 --publish
morphobricks-preprocessing check my_experiment
morphobricks-preprocessing run my_experiment
```

Your experiment is now described, checked and processed. The default output folder is
`my_experiment_MorphoBricks_Preprocessing`. Give this folder to the MorphoBricks tool that will
analyse your images.

Fast publication from the command line creates a new description for an experiment with a
shared channel layout. Use the notebook to edit an existing description, change repeat pairing
or describe acquisitions with different channel lists or orders. Give `--comparable` only if
the samples of each biological repeat were prepared and acquired comparably; never give it
just to get past the question.

Other commands:

```bash
morphobricks-preprocessing describe /data/exp1 # the draft description, what is open
morphobricks-preprocessing check /data/exp1 # report, suggested --axes
morphobricks-preprocessing run /data/exp1 --axes ZCYX
morphobricks-preprocessing run --output /data/exp1_MorphoBricks_Preprocessing # later runs
morphobricks-preprocessing masks --output /data/exp1_MorphoBricks_Preprocessing # mask step
morphobricks-preprocessing calibrate --output /data/exp1_MorphoBricks_Preprocessing
morphobricks-preprocessing convert /data/exp1 --to /data/exp1_named_axes --axes ZXY # axis errors
morphobricks-preprocessing convert-mask --output /data/exp1_MorphoBricks_Preprocessing --mask TumorRegion --to-instances TumorObjects # new mask files
morphobricks-preprocessing tissue-mask --output /data/exp1_MorphoBricks_Preprocessing --to HETissue # H&E tissue region (he-mask extra; provisional rule)
morphobricks-preprocessing run --output my_experiment_MorphoBricks_Preprocessing # later runs
morphobricks-preprocessing declare-masks --output my_experiment_MorphoBricks_Preprocessing \
--masks my_masks --type Cells=instances # declare masks
morphobricks-preprocessing masks --output my_experiment_MorphoBricks_Preprocessing # check masks
morphobricks-preprocessing calibrate my_experiment --size . XY 0.65 um # pixel sizes
morphobricks-preprocessing convert my_experiment --to my_experiment_max --axes ZCYX --project max # 2-D projections
morphobricks-preprocessing convert-mask --output my_experiment_MorphoBricks_Preprocessing --mask TumorRegion --to-instances TumorObjects # new mask files
morphobricks-preprocessing tissue-mask --output my_experiment_MorphoBricks_Preprocessing --to HETissue # H&E tissue region (he-mask extra; provisional rule)
```

`check`, `run` and `masks` need the experiment description published in the output folder
(the notebook's describe steps); without it they stop with `A_DESCRIPTION_REQUIRED` and write
nothing. `describe` shows the draft and what is still to be answered. `check` writes the
report. `run` and `masks` reuse every input the run in the
output folder recorded. A run that stops writes its report and says so. Every output file
gets the permissions a file you save yourself would (your umask). `convert` saves the
images whose metadata name their axes wrongly or not at all as named-axis OME-TIFF copies
with your roles in a new folder (each plane bit for bit the plane read with those roles;
every other image copied as it is; a conversion record written last), then you run
preprocessing on that folder with the same output folder. The notebook offers the same
conversion in step 2, under the axis check, when it finds such images. Details in the
[user guide](docs/user_guide.md) §5.
(`describe --publish` or the notebook's describe steps); without it they stop with
`A_DESCRIPTION_REQUIRED` and write nothing. `check` inspects the folders, image headers and
supplied masks, and suggests axis roles; it does not measure image pixels or perform every
validation of the published description. `run` validates the description and confirmed axis
roles before processing. `run` and `masks` reuse every input the run in
the output folder recorded. External TIFF masks are stored in a separate masks folder, given
with `--masks`; OME-Zarr label images are discovered automatically. If your analysis uses
masks, run `masks` after preprocessing. After `calibrate`, run preprocessing again. Every
output file gets the permissions a file you save yourself would (your umask). `convert` writes
copies of the images in a new folder (named-axis OME-TIFF, OME-Zarr with `--store ome-zarr`,
or 2-D projections with `--project`); the copies are a new experiment folder, described and
published before `check` and `run`. Use the actual stored axis order of your images: RGB TIFFs store their colour samples
last (`YXC`). Details in the [user guide](docs/user_guide.md) §5.

## Example

Expand All @@ -232,13 +269,14 @@ record = PlaneRecord(
technical_repeat="tech_rep1",
histograms=[compute_plane_histogram(plane)], # one per channel
position_id="img_01", # required: the XY position within its technical repeat
channels=("DNA",), # the channel ID of each histogram, in order
)
write_plane_histograms([record], "plane_histograms.csv")
write_plane_histograms([record], "plane_histograms.parquet", channel_ids=["DNA"])

# In a later brick: pool one channel of any group and derive normalization parameters
records = read_plane_histograms("plane_histograms.csv")
records = read_plane_histograms("plane_histograms.parquet")
for key, members in group_records(records, "condition").items():
pooled = PooledHistogram(members, channel=0)
pooled = PooledHistogram(members, channel="DNA")
params = percentile_range(pooled, low=1, high=99.8)
normalized = params.apply(plane, clip=(0, 1))
```
Expand Down
Loading
Loading