Skip to content

Latest commit

 

History

History
145 lines (121 loc) · 8.06 KB

development.md

File metadata and controls

145 lines (121 loc) · 8.06 KB

Development Conventions

Environment

  • Python 3.11 or newer.
  • Core dependencies: numpy, tifffile and morphobricks-normalization (installed from its GitHub release tag; it holds the histogram, CSV, pooling and normalization code). Notebook dependencies (ipywidgets, matplotlib) are in the notebook extra, zarr (2.18 or 3.x, for OME-Zarr copies) in the zarr extra, and development tools in the dev extra.
  • Set up a clone with python -m venv .venv and pip install -e ".[dev]".

Layout

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.

Style

  • PEP 8 naming; lines up to 100 characters.
  • ruff format formats the code and ruff check lints 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.

Code standards (MorphoBricks spec §9)

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 src and tools, private and nested ones included (ANN), with from __future__ import annotations in 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, ui or orchestration in pyproject.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_NAMES in tools/code_rules.py fails a copy anywhere else. Write files with _io.write_text_atomic and word messages with validation.plural and validation.agree, never a local copy.
  • Enforcement: tools/code_rules.py is the shared file, identical in every MorphoBricks repository (tests/test_code_rules.py checks its SHA-256). The violations that existed when the rules came in are listed in tools/code_rules_baseline.json, with the number of # noqa comments 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, until python tools/check.py --update-baseline lowers it (it never adds an entry). A # noqa must name its rules (PGH004), and line and file-level ones are counted, tests included.
  • Embedding API: notebook_ui.PreprocessingNotebook and notebook_style are a declared, tested embedding API (MorphoRelate builds its notebook from them). tests/test_embedding_api.py pins their public names and parameters; changing them needs a coordinated change in the bricks that embed them.

Design rules

  • 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 logging module to the diagnostic log.
  • Source images are never modified. Nothing is written inside the experiment folder.

Determinism and files

  • 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's os.replace commits 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 .staging as soon as it is read, so an interrupted run resumes. Run only one writer per output folder (the lock).

Tests

  • pytest; simulated images are generated inside the tests (see tests/simulation.py).
  • Every acceptance check in the plan has a test.
  • A bug fix comes with a test that fails without it.

Benchmarks

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.

Spec conformance tests

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.py fails when one byte of the vendored fixtures/ 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_*.py is held by tests/spec_gate.py: a skipped or xfailed test there fails the run, and so does a module skipped while it is collected (module-level skip or importorskip). A run over the whole tests folder also fails when no test was collected from a module of EXPECTED_MODULES in 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.py scans 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.

Versioning

  • Semantic versioning for the package.
  • schema_version in the CSV changes whenever the contract in docs/contract.md changes in a way readers must know about.