Skip to content
SciStack

Reproducible scientific code

Environments, testing numerical code, seeds, and versioning so that results can be rerun.

Estimate π from 10,000 random points in the unit square, and NumPy's generator gives 3.1484 with seed 0 and 3.1564 with seed 1. Without a seed every run gives another number, and a referee who reruns your script never sees yours. Even with a seed, adding the same million random numbers in a for loop and with np.sum gives 998.570649438616 and 998.5706494386213, because np.sum adds them in a different order. The molecular dynamics run a collaborator cannot repeat and the seismic inversion that shifts with the solver version are one problem: a result that depends on more than the code you wrote down.

In Python, a virtual environment from venv or uv with every version pinned in a lock file fixes the libraries; conda does the same when a dependency is not written in Python. np.random.default_rng(seed) creates a generator that you pass to every function that draws numbers. pytest runs the tests, and np.testing.assert_allclose or pytest.approx compares within a tolerance, the only comparison numerical code survives. In Julia, a Pkg environment lists the packages in Project.toml and pins their versions in Manifest.toml, and Pkg.instantiate() rebuilds it on another machine. Random.seed! fixes the random numbers, and the Test standard library checks results with @test and ≈.

Start with an environment file and a seeded generator, two changes that take minutes. Then write tests that compare with a known answer, such as an analytic solution, within a stated tolerance. Version control of code and data comes next, then recording which commit produced each figure. A figure in a paper should come out of one command on a clean checkout. If it depends on the order in which notebook cells were run, nobody can rerun it, including you in a year.

What belongs here

Making a computational result rerunnable by someone else, or by yourself in a year: pinned environments, seeding random numbers, testing numerical code with tolerances, version control of code and data, notebooks versus scripts, and recording the provenance of a figure. Making code faster belongs to performance.

0 tutorials by type and language

PythonJulia
Concept – –
Tool – –
Recipe – –
Visualization – –
Project – –