Reproducible Scientific Python

Scientific code often begins as an exploratory notebook or a short script. That is appropriate while the question is still changing. Problems arise when the same prototype becomes the undocumented production path for a published figure or engineering decision.

A minimal project structure

A durable project should separate:

  • immutable or externally sourced input data;
  • importable analysis code;
  • tests and reference cases;
  • configuration and parameters;
  • generated figures, tables, and reports;
  • documentation explaining how results are reproduced.

The exact directory names matter less than making dependencies and data flow explicit.

Environment and dependencies

A pyproject.toml file provides a standard place for package metadata, Python requirements, and tool configuration. Dependencies should be constrained tightly enough to recreate a working environment without pretending that an environment lock guarantees scientific validity.

The Python version, important native libraries, operating-system assumptions, and external commands also need documentation when they affect results.

Quality checks

Formatting and linting improve consistency, while static typing can expose incorrect assumptions about data structures and interfaces. Tests should focus on domain properties rather than only implementation details:

  • conservation laws and dimensional consistency;
  • behaviour at boundaries and invalid inputs;
  • agreement with analytical or independently calculated cases;
  • invariance under transformations that should not change the result;
  • regression tests for previously observed failures.

Data provenance

Raw data should remain unchanged. Cleaning and transformation steps should be implemented as code, with rejected or corrected records reported explicitly. Each generated result should be traceable to input data, parameters, and a software revision.

Determinism and uncertainty

Random seeds are necessary for repeatability but insufficient for robust validation. Numerical libraries, parallel execution, and hardware may still affect results. More importantly, repeating the same computation exactly does not test whether the method or assumptions are sound.

Reproducibility reconstructs the result. Scientific credibility additionally requires sensitivity analysis, uncertainty assessment, and independent validation.

Recommended command surface

A small command-line interface or Make target can define operations such as check, test, analyse, and report. The objective is that another person can rebuild the published artefacts without reproducing the author's sequence of interactive clicks.