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
10 changes: 8 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,14 @@ src/morphobricks_preprocessing/
channel_by_number (a channel number only when every image has the same channel
IDs at the same C positions, translated once to the ID; #440);
segmentation_cellpose_model.py choose_model (a built-in name or an existing file,
before loading), load_model (the weights file and device verified after loading, no
download without allow_download, SHA-256 recorded, DINOv3 at a pinned commit);
before loading), load_model (the weights file and device verified after loading,
SHA-256 recorded, DINOv3 installed at a pinned commit on a cpdino model's first
load: ensure_dinov3); segmentation_cellpose_weights.py fetch_weights: the chosen
built-in model's file only, from a pinned Hugging Face revision, checked against
its pinned SHA-256 (a cached copy too), a failed download a refusal (owner,
#574: choosing the model and loading it is the consent; no tick);
segmentation_cellpose_eval.py eval_arguments, inference_parameters (what
model.eval is given and what the batch records);
notebook_cellpose.py CellposeWorkflow, ChannelPicker, CellStop: the widgets behind
notebooks/MorphoBricks_Cellpose.ipynb
notebook_stop.py CellStop (one line, no traceback), defined once: cells 2-8
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ v0.5.1: no run before v0.5.1 is read.)

## Unreleased

- **Cellpose: no download ticks; the chosen model's weights download on first use, pinned and checked (v0.9.1, #574 item 3; owner decision of 26 November 2026).** Choosing the model and pressing **Preview** or **Segment** (or **Load the model** in the Cellpose notebook) is now the consent to download its weights: the "Allow the weights download from Hugging Face" and "Install DINOv3 now" ticks are removed from the model form (both notebooks share it). Cellpose no longer downloads anything itself: the new `segmentation_cellpose_weights.fetch_weights` downloads only the chosen model's file from `mouseland/cellpose-sam` at the pinned commit `WEIGHTS_REVISION` (`7c61431b5fbb`, not Cellpose's moving `main`, and with TLS verification, which Cellpose's own download turns off), through a temporary file kept only with the pinned size and SHA-256 (`PINNED_WEIGHTS`, as Hugging Face lists them at that commit), then hands Cellpose the absolute path. A copy already in Cellpose's model folder is hashed and must match the pin; one that differs is refused, not used and not replaced. The file's identity (device, inode, size, modification time) taken when it is hashed must be unchanged right before Cellpose opens it and after Cellpose loaded it (also for a model file of one's own), else the load is refused: a file gone in between would make Cellpose fetch a model from its moving branch with TLS off while the record carried the pinned hash (review of #577). The download is fsynced before the rename; a local write error (a full disk) is refused as a write error naming the folder, not as a network problem, and a failed rename names the target. A failed or blocked download, or bytes that do not match, is a refusal naming the model and the URL; nothing partial is kept and no other model is used. The notebook shows a "Downloading <model> weights…" line with the megabytes received, a bar and the elapsed time (`notebook_cellpose_download.DownloadLine`), then how long it took; Cellpose's `tqdm` bar is no longer patched (`BAR_LABEL` and `notebook_bar` are removed). DINOv3, the code of the `cpdino` networks (their weights are all in the Cellpose file), is installed at its pinned commit on a `cpdino` model's first load when no DINOv3 is installed (`ensure_dinov3`, before Cellpose is imported); a DINOv3 of another commit is refused, not replaced. The check that Cellpose imported DINOv3 now looks at `cellpose.vit` (Cellpose 4.2.1.1 defines `CPDINO` whether or not DINOv3 imports, so the former `hasattr(models, "CPDINO")` check could not fail). The record's `parameters` add `weights_repository`, `weights_revision`, `dinov3_source` and `dinov3_installed`; `weights_source` is the pinned URL. **API change:** `load_model(model, device, *, progress=None)` (no `allow_download`), `CellposeWorkflow.load` and `load_nuclei` take `(model, custom_model_file, device)`, `LoadedModel` gains `repository`, `revision` and `dinov3_installed`. `normalization`, `eval_arguments` and `inference_parameters` moved, unchanged apart from the new fields, to `segmentation_cellpose_eval` (re-exported by `segmentation_cellpose`) to keep the file under the 600-line rule. `tests/test_segmentation_cellpose_weights.py`; nothing is downloaded in the tests.
- **Cellpose: torch's "Sparse invariant checks are implicitly disabled" warning is hidden while Cellpose runs (v0.9.1, #574 item 5).** Cellpose's `dynamics.py` builds a sparse tensor and torch warns each session. `segmentation_cellpose.evaluate` now runs `model.eval` inside `segmentation_cellpose_model.sparse_warning_hidden()`, which filters only that `UserWarning`, by its message's start (`SPARSE_WARNING`), from the `torch` or `cellpose` modules, inside `warnings.catch_warnings()` (no global filter; every other warning is shown). The checks it is about only validate sparse tensors and do not change results, so nothing is recorded. `tests/test_segmentation_cellpose_weights.py`.
- **The run's masks counter ends at N of N (follow-up of #567).** The mask header check reported "done of N" before each mask and nothing after the last, so the run's wait stopped at "N-1 of N" (preflight's closing `("masks", 1, 1, "")` is a placeholder the wait ignores unless the total is 1). The check now also reports `(total, total, "")` after the last mask; what is read and recorded is unchanged. A stray docstring in `notebook_progress` (the one of `PREPARING` sat after `COUNTED_PHASES`) is back under `PREPARING`. Tests: `tests/test_run_checking_progress.py`, `tests/test_notebook_progress.py`.
- **`piece_counts` counts the pieces of masks of objects by runs (#69).** It sorted every pixel (`np.unique(..., return_inverse=True)`, most of its time) and labelled each label again in its bounding box, so labels spread over the plane read it once each. Each row is now cut into runs of one label and runs of one label in neighbouring rows that touch (8-connected, from `masks.binary.STRUCTURE`) are joined with `scipy.sparse.csgraph.connected_components` (new pure module `mask_pieces`; `mask_scan.piece_counts` is the same function, imported). On pixel-scale masks (noise, checkerboards) runs are almost pixels and that graph needs several times the memory of the sort, so above `RUNS_PER_PIXEL_LIMIT` = 0.25 runs per pixel the former route is taken, unchanged. The results are byte-identical either way (labels in the mask's pixel type, counts `int64`): a property test compares both routes, forced and as chosen, with the former function, kept in the test as the reference, on random masks of every label pixel type (touching labels, holes, single pixels, edges, corners, no background, empty) and on the benchmark masks; every digest of `tools/benchmark_kernels.py --group masks` is unchanged, including its new pixel-scale cases (`benchmark_inputs.pixel_mask`). On the benchmark machine `piece_counts` of the 4096 px cells mask went from 2.1 to 2.3 s to 0.07 to 0.10 s and one position's scan from 4.9 to 5.1 s to 0.9 to 1.0 s; the pixel-scale masks take the time and memory they took before (`docs/benchmarks.md`). Outside the former results: a mask with a side of length 0 now has no labels, where the former function failed inside SciPy; a mask that is not 2D is refused with a `ValueError` saying so (it was a SciPy `RuntimeError`, or no labels when it was all background). **The mask scan refuses a mask plane of no pixels** (`mask_without_pixels`, naming the mask): before, SciPy's failure in `piece_counts` was its only guard, and with no labels such a plane would have counted as an image and as a plane of at most one label in its mask type's kind. `pair_counts` and the repeated pair work of #69 are not changed. `tests/test_mask_pieces.py`, `tests/test_mask_scan.py`.
- **The run shows its own folder and header check, and that check is no longer quadratic (owner Colab test of v0.9.0, 25 November 2026).** On a large mixed-layout experiment, *Run* showed "Preparing: checking the folders and the image headers again · 0:00 elapsed" for 10-15 minutes before the first image: the run checks every header again (bound to its fingerprint) with no progress reported, and the elapsed time was redrawn only on a progress report. The run now reports that check through `RunMode(checking=...)` (preflight's `(phase, done, total, path)`, operational only like `workers`; `run_experiment`'s parameters are unchanged), and the notebook shows each phase and each header of how many ("Preparing: reading the image headers again · 120 of 24,000"), with the elapsed time redrawn every second by `Wait.ticking()` while the run works. While it goes, the notebook says that a cell run meanwhile waits for it, and how to interrupt it. Two steps of the check were O(n²): each image's staging key took `MetadataReport.dtype` (a pass over every header) and the mask-name screen compared each image's name with every other one (`mask_names._mask_owner` now looks up the prefixes of the name). The outputs are byte for byte the same (`plane_histograms.csv`, `experiment_design.csv`, `image_qc.csv` of a 4,800-image three- and two-channel tree); the check before the first image took 30.0 s → 10.3 s for 12,000 images and 72-75 s → 24-27 s for 24,000 on local disk (`tests/test_run_checking_progress.py`). In a `verify_content` run (the notebook's default, and any run on a fresh output folder) the run then reads every image whole for its content SHA-256 before the first pixel is read: on Google Drive that pass, not the header check, is what took minutes (owner: about 200 images, 10-15 minutes). It is now reported too, as the phase `contents` (`run.phases.CONTENTS`, "Preparing: reading each image whole for its content SHA-256 · 12 of 200"); what is read and verified is unchanged.
Expand Down
28 changes: 21 additions & 7 deletions docs/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -1399,20 +1399,34 @@ Cellpose's own `model.device` is not evidence (it only repeats the device it was
nothing beyond the network's tensors is verified. The device is the placement of the network's tensors; Cellpose's own post-processing is not
covered by it: Cellpose may move the flow-threshold quality step to the CPU when GPU memory is
short, and always computes the masks from the flows on the CPU on `mps`; that is Cellpose's
behaviour, **not refused**, and the batch records it as a note. Uncached weights are downloaded only on an explicit request; more than
behaviour, **not refused**, and the batch records it as a note. Built-in weights are never
downloaded by Cellpose: choosing a built-in model and loading it is the consent to download that
model's file (owner, #574), which `segmentation_cellpose_weights` fetches from
`mouseland/cellpose-sam` at a pinned commit (`WEIGHTS_REVISION`) and accepts only with its
pinned size and SHA-256 (`PINNED_WEIGHTS`); a copy already in Cellpose's model folder must match
too, else it is refused and left as it is; the file's identity (device, inode, size, modification
time) taken when it is hashed must be unchanged right before and after Cellpose loads it, else
the load is refused. A failed or blocked download is a refusal naming the
model and the URL (a local write error names the folder instead); nothing partial is kept and no other model is used. Cellpose is then given
the file's absolute path. While `model.eval` runs, torch's `UserWarning` starting "Sparse
invariant checks are implicitly disabled" from the `torch` or `cellpose` modules is filtered
(`warnings.catch_warnings`, never a global filter); it does not affect results. More than
three channels are refused (Cellpose reads the first three by position); a blank diameter is
the model's native scale, never an estimate and never 0, and a diameter that gives fewer than one
flow iteration (`int(200 / rescale)` is 0) is refused, never raised to 1. The batch's `model` holds the model
name, the weights file name (or the custom file's path) and its SHA-256, and its `parameters`
every effective setting (diameter in pixels or null with its meaning, thresholds, the **full**
normalisation dictionary given to Cellpose (its defaults read from its module at call time, with
the user's values applied) and the flow iterations `niter` (`int(200 / rescale)`, passed
explicitly), batch and tile size, `channel_axis` 0, device type, versions, `weights_downloaded`
and `weights_source` (the Hugging Face URL Cellpose uses; null for a model file), the device
`notes` and, for a DINOv3 model, the DINOv3 commit). The batch ID is the record's
`new_batch_id()`. The DINOv3 package is installed with `--no-deps` at a pinned commit, the head
of its default branch on 22 November 2026, which is **not verified against `cpdino`**; Cellpose
imports it only when first imported, so the runtime is restarted after installing it.
explicitly), batch and tile size, `channel_axis` 0, device type, versions, `weights_downloaded`,
`weights_source` (the pinned URL, `.../resolve/<revision>/<name>`), `weights_repository` and
`weights_revision` (all three null for a model file), the device `notes` and, for a DINOv3
model, `dinov3_commit`, `dinov3_source` and `dinov3_installed`). The batch ID is the record's
`new_batch_id()`. The DINOv3 package (the code of the `cpdino` networks) is installed on the
first load of a `cpdino` model when none is installed, with `--no-deps` at a pinned commit, the
head of its default branch on 22 November 2026, which is **not verified against `cpdino`**; a
DINOv3 of another commit is refused, not replaced. Cellpose imports it only when first imported
(`cellpose.vit`), so a DINOv3 installed after that needs a restart, and the load says so.
Two-dimensional images only. **A mask the batch publishes is never left without its record
entry**: each mask's SHA-256 and the record write (the batch so far, the same batch ID growing)
happen in `write_mask`'s `on_published`, **under the type's write lock**, before the next image
Expand Down
16 changes: 16 additions & 0 deletions docs/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1715,3 +1715,19 @@ Raised during review; to be decided before the milestones that need them:
work records; mask headers and projection markers are read as always; *Start fresh*
writes and deletes nothing; a finished run is one `RunInputs.open` opens (review of #547)
(`notebook_restore`, contract §9.2).
86. Does the Cellpose step need the user's tick to download a model's weights, and to install
DINOv3? *Decided (owner, 26 November 2026, v0.9.1, #574):* no. Choosing the model, then
pressing **Preview** or **Segment** (or **Load the model** in the Cellpose notebook), is the
consent to download that model's weights; the "Allow the weights download from Hugging
Face" and "Install DINOv3 now" ticks are removed. Only the chosen model's file is
downloaded, on first use, with a visible "Downloading <model> weights…" line; the record
names the weights exactly; another model is never used instead and a failed download is a
refusal. *Encoding choices of this brick:* the file is downloaded from
`mouseland/cellpose-sam` at a pinned commit (`WEIGHTS_REVISION`, not Cellpose's moving
`main`) and kept only with its pinned size and SHA-256 (`PINNED_WEIGHTS`, the hashes Hugging
Face lists at that commit); a copy already on disk must match too, else it is refused and
left in place; the record adds `weights_repository` and `weights_revision` beside
`weights_source`, `weights_downloaded` and the model's `sha256`. DINOv3 (the code of the
`cpdino` networks, no weights of its own) is installed at its pinned commit only when none
is installed; a DINOv3 of another commit is refused, not replaced
(`segmentation_cellpose_weights`, `segmentation_cellpose_model.ensure_dinov3`).
13 changes: 8 additions & 5 deletions docs/testing/cellpose-stepper-human-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ note anything that looks wrong next to it.
- [ ] There is no **Load the model** button. Each setting (diameter, flow threshold, cell
probability threshold, batch size) has a one-line help beside it saying which way to move
it.
- [ ] Leave the device on `cuda`. If the model's weights are not on this runtime yet, press
**Preview** without the download tick: it refuses and says to allow the download.
- [ ] Tick **Allow the weights download** and press **Preview**: the download shows as **one**
progress bar (no stream of log lines), then a green *Model ... loaded on cuda* card and the
preview figures for the image in the dropdown.
- [ ] There is no **Allow the weights download** tick and no **Install DINOv3 now** tick.
- [ ] Leave the device on `cuda`. With the model's weights not on this runtime yet, press
**Preview**: a line **Downloading cpsam_v2 weights…** shows the megabytes received of the
total (no stream of log lines), then *done in m:ss*, then a green *Model ... loaded on
cuda* card (it names the repository and revision) and the preview figures for the image
in the dropdown.
- [ ] Press **Preview** again after a runtime restart: nothing downloads; the card says the
weights on this computer match the pinned file.
- [ ] Choose another image and press **Preview**: no loading card appears this time (the model is
reused); the preview is of the new image.
- [ ] Change the diameter and press **Preview**: still no reload; the new diameter is used.
Expand Down
Loading
Loading