- Python 3.11 or newer.
- Core dependencies:
numpy,tifffileandmorphobricks-normalization(installed from its GitHub release tag; it holds the histogram, CSV, pooling and normalization code). Notebook dependencies (ipywidgets,matplotlib) are in thenotebookextra,zarr(2.18 or 3.x, for OME-Zarr copies) in thezarrextra, and development tools in thedevextra. - Set up a clone with
python -m venv .venvandpip install -e ".[dev]".
src/morphobricks_preprocessing/ importable package, one module per job
tests/ pytest suite with simulated data
docs/ plan, contract, these conventions
experiments/ studies behind design decisions (not part of the package)
notebooks/ biologist-facing notebooks, calling the API only
The notebook and the CLI contain no processing logic; they call the same public
functions the tests use.
The notebook's Step 0 contains a standalone bootstrap using only the standard library,
so it can locate a checkout or install missing requirements before importing the package.
Its environment alternatives are tested without installing packages during the test suite.
Colab-only setup sits inside if "google.colab" in sys.modules: and the GitHub installation
is pinned to the release in RELEASE_TAG (plan §12 question 58);
tests/test_notebook_file.py checks these rules and runs the notebook in a real kernel.
- PEP 8 naming; lines up to 100 characters.
ruff formatformats the code andruff checklints it; both run in CI.- Type hints on all functions (see the code standards below).
- NumPy-style docstrings on public modules, classes and functions.
- Comments explain why, not what.
python tools/check.py runs everything a pull request must pass: ruff check,
ruff format --check, pytest and the code rules. CI runs the code rules with
python tools/check.py --rules.
- Size: files at most 600 lines; functions at most 60 executable lines, complexity 15
(
C901), 8 parameters (PLR0913) and 50 statements (PLR0915). - Annotations on every function in
srcandtools, private and nested ones included (ANN), withfrom __future__ import annotationsin every module (I002). Tests are exempt, as they are from the docstring rules. - Docstrings: NumPy style for public names (
D); at least an imperative line for private functions over 10 lines. - Layers: every module of the package is declared
pure,io,uiororchestrationinpyproject.toml([tool.morphobricks.code-rules.layers]). Pure modules do no I/O and no UI; UI modules do not compute. - Ownership: shared logic lives in its owner repository;
RESERVED_NAMESintools/code_rules.pyfails a copy anywhere else. Write files with_io.write_text_atomicand word messages withvalidation.pluralandvalidation.agree, never a local copy. - Enforcement:
tools/code_rules.pyis the shared file, identical in every MorphoBricks repository (tests/test_code_rules.pychecks its SHA-256). The violations that existed when the rules came in are listed intools/code_rules_baseline.json, with the number of# noqacomments of the rules above. The baseline may only shrink: a new or grown violation fails, and so does an entry the code no longer needs, untilpython tools/check.py --update-baselinelowers it (it never adds an entry). A# noqamust name its rules (PGH004), and line and file-level ones are counted, tests included. - Embedding API:
notebook_ui.PreprocessingNotebookandnotebook_styleare a declared, tested embedding API (MorphoRelate builds its notebook from them).tests/test_embedding_api.pypins their public names and parameters; changing them needs a coordinated change in the bricks that embed them.
- Small dataclasses for settings and results; no global state.
- Functions take explicit inputs and return results.
- Expected problems become validation issues with a severity, a plain-language message, the affected paths and a suggested action; they are never raw exceptions shown to users. Custom exception classes are for genuine failures.
- Technical details go through the
loggingmodule to the diagnostic log. - Source images are never modified. Nothing is written inside the experiment folder.
- Folders and files are processed in sorted order; the same inputs always produce the same output.
- Paths written to outputs are relative and use
/. - Floats are written at full precision (
repr); files are UTF-8. - Outputs are written to a temporary file and replace the previous version only after a complete successful write.
- Experiment runs publish their outputs as one generation (contract §9.4, PP-12): the
whole generation is written into
.publish, then the manifest'sos.replacecommits it, then the other files are moved in. Whatever takes the output folder's lock first finishes or discards an interrupted publication (run.generation.finish_publication); readers go through the manifest (published_path,read_stable). Each image's rows are staged in.stagingas soon as it is read, so an interrupted run resumes. Run only one writer per output folder (the lock).
pytest; simulated images are generated inside the tests (seetests/simulation.py).- Every acceptance check in the plan has a test.
- A bug fix comes with a test that fails without it.
tools/benchmark_run_workers.py times whole runs with 1, 2, 4 and auto workers; its
docstring is the protocol. A performance claim needs an acceptance run (at least 10 rounds; the
default is the smallest multiple of the configurations from 10, 12 for 1 2 4 auto, so every
counterbalancing block is complete; --quick is a smoke test, never a result), the counterbalanced seeded order (the default,
recorded with its seed), the speed-up paired by round with its spread, the peak RSS
distribution, CPUs pinned with --cpus (taskset) where available, the load average
recorded in the report quoted with the numbers, and identical output digests in every run
(the tool exits 1 otherwise). --phases times the calling thread's steps of a run.
tools/benchmark_image_qc.py times the histogram step with and without the image QC metrics
(contract §19) against a checkout without them (--baseline-src), whole and in blocks, and
exits 1 unless every run writes the same plane_histograms.csv.
The experiment-description reader is tested against the fixtures of
MorphoBricks-Spec
(conformance plan, sections 1 and 2). They are vendored in tests/spec_fixtures/ at the full
commit SHA in its PIN.toml, never a branch.
- Pin integrity:
tests/test_spec_pin.pyfails when one byte of the vendoredfixtures/differs, or a file is added or missing (git tree hash against the pin), so a local edit of a fixture cannot make a reader pass. Never edit the vendored files; updating the pin is a reviewed change (tests/spec_fixtures/README.md). - Skip is failure: every test module named
tests/test_spec_*.pyis held bytests/spec_gate.py: a skipped or xfailed test there fails the run, and so does a module skipped while it is collected (module-levelskiporimportorskip). A run over the whole tests folder also fails when no test was collected from a module ofEXPECTED_MODULESin that file (a deleted or emptied Spec module); a new Spec module is added to that list. - No second copy of the rules:
tests/test_spec_no_fallback.pyscans the reader's modules (description*.py) for a second table of codes or names and for lenient, older or default-version paths.
The result of the last recorded whole-suite run, with its skips, is in conformance.md.
- Semantic versioning for the package.
schema_versionin the CSV changes whenever the contract indocs/contract.mdchanges in a way readers must know about.