Sub topic 1
What Reproducible Means and Why Notebooks Drift
Prerequisites
This chapter assumes you can do the following:
- Write basic Python and run cells in a Jupyter notebook.
- Use the command line.
- Use Git for simple commits.
- Install packages with pip or conda.
What reproducible means
An analysis is reproducible when another person, or you in six months, can take the same inputs and the same code and get the same results. Three related terms are often mixed up:
| Term | Same code | Same data | What it asks |
|---|---|---|---|
| Repeatable | Yes | Yes | Can you get the same result again, on your own machine? |
| Reproducible | Yes | Yes | Can someone else get the same result on another machine? |
| Replicable | No, new code | No, new data | Does the finding hold up in a fresh study? |
This tutorial is about the first two. Replication is a scientific question, not a tooling one.
"Same result" needs a definition too. For text and tables, it usually means identical values. For floating-point numbers and plots, it can mean values equal within a small tolerance. Decide which one you need before you start checking.
Why notebooks drift
A notebook is an interactive session that you saved to a file. The file keeps your code and outputs, but it does not keep everything the session depended on. Results drift when any of these inputs change without you noticing:
- Kernel state. Variables, imports, and functions live in memory. A cell can use a value that no cell in the saved file creates.
- Execution order. Cells can run in any order. The numbers on the left margin show the order, but a reader may not check them.
- Software versions. A new pandas release can change a default or a result.
- Randomness. Sampling, shuffling, and model training give different values on each run unless you control the generator.
- Input data. A file can be edited, replaced, or re-downloaded with different contents.
- Machine and paths. Code that reads
C:\Users\you\data.csvworks only for you.
A small example of drift
The following notebook has two cells. Suppose you first ran the cell that defines threshold as 10, then edited that cell to 5 and never re-ran it.
# Cell 1
threshold = 5
# Cell 2
values = [3, 7, 12, 4, 9]
kept = [v for v in values if v > threshold]
print(kept)If cell 2 runs after cell 1 in a fresh kernel, it prints [7, 12, 9]. In the old session, threshold is still 10, so the output shown in the saved file is [12]. The file's code and its output disagree, and nothing warns you.
The next chapters fix this by restarting the kernel and running all cells in order. That check is the first and cheapest tool you will use.
The plan for this tutorial
Each cause of drift has a matching practice:
| Cause of drift | Practice |
|---|---|
| Kernel state and order | Restart and run all |
| Software versions | Virtual environments and pinned dependencies |
| Randomness | Explicit seeds |
| Input data | Checksums and recorded sources |
| Manual, interactive runs | Command-line execution and automated checks |
Exercise
A colleague sends you a notebook and its saved outputs. When you open it and run every cell in a fresh kernel, one number differs from the saved output. Name three possible causes from this chapter.
Show answer
Any three of these: a cell was run out of order or edited without being re-run (hidden kernel state), a package version differs, a random process had no fixed seed, or the input data file has changed.