Skip to content

Lab 01 · Diagnose a notebook that will not reproduce

Lab ID Level Time Prerequisite module Verified
L01 Foundation 2 hours 02 · Python, Git, environments, 05 · Reproducible training Yes, on Windows 11 and Linux

Starter kit: examples/labs/lab-01-notebook-reproducibility/ — starter files, data generator, and acceptance checker.

Scenario

A colleague sends you an exported notebook. It reports a mean absolute error of about 4.2 for the study-score model, and they have quoted that figure in a slide deck. You run it before a review meeting and get 4.31. You run it again and get 4.19. Nothing in the code changed, and the dataset file is the same one they sent.

The analysis is not wrong. Three decisions were left to the interpreter: which rows go into which split, which rows are sampled for the manual review list, and the order in which sections are processed. The first two change the number; the third changes the report text even when the number is stable.

This is the cheapest reproducibility bug in MLOps, and the most expensive to leave in place: every later comparison, gate and dashboard inherits the noise.

What you will be able to do

  • Reproduce a difference between two runs of the same script and attribute it to a specific line rather than to "randomness".
  • Explain why PYTHONHASHSEED can change output that contains no randomness.
  • Make an analysis reproducible without freezing the whole environment.
  • Write a check that fails when a report stops being stable.

Prerequisites and setup

You need Python 3.11 or newer, and the lab requirements:

python -m pip install -r examples/requirements-labs.txt
cd examples/labs/lab-01-notebook-reproducibility
python ../_shared/make_sessions.py --out data/sessions.csv
python -m pip install -r examples/requirements-labs.txt
cd examples/labs/lab-01-notebook-reproducibility
python ../_shared/make_sessions.py --out data/sessions.csv

The generator writes 480 synthetic study sessions. No real student data is used. Expected: wrote 480 rows -> data/sessions.csv.

Your task

Make notebook_v1.py produce the same report on every run, without changing what it measures. Then prove it with check_lab01.py, which runs your script three times under different hash seeds.

Data and starter files

File Purpose
notebook_v1.py The analysis, with three sources of variation
check_lab01.py Acceptance checker: three runs, byte comparison
data/sessions.csv Generated by the shared generator

The dataset has one row per session: session_id, section, hours, prior_score, sleep_hours, score.

Steps

1. Observe the instability before touching the code

python notebook_v1.py > run-a.txt
python notebook_v1.py > run-b.txt
diff run-a.txt run-b.txt

PowerShell has no diff; use Compare-Object (Get-Content run-a.txt) (Get-Content run-b.txt).

You should see at least the rows: line, the MAE: line and the review sample differ. Confirm that the split is the cause of the metric change by running with the split seeded only.

2. Separate the three causes

Read the script and list, in your own words, which line causes:

  1. a different train/test split;
  2. a different review sample;
  3. a different order of section rows and coefficients.

Check your answer by fixing one cause at a time and re-running the comparison. A fix that removes all three at once is allowed, but you should still be able to name them.

3. Make the report stable

Seed what draws randomness, order what is iterated, and keep the number formatting fixed. Then run the checker:

python check_lab01.py notebook_v1.py

Before your fix, this reports 5/6 checks passed with 3 distinct reports. After it, 6/6 and ACCEPTED.

4. Confirm the hash seed is no longer load-bearing

The checker runs with PYTHONHASHSEED=0, 1 and 42. Run it yourself once with an explicit seed to see the mechanism:

PYTHONHASHSEED=7 python notebook_v1.py
$env:PYTHONHASHSEED="7"; python notebook_v1.py; Remove-Item Env:\PYTHONHASHSEED

5. Prove the defect existed

Keep the failing run in your evidence file:

python check_lab01.py notebook_v1.py --expect unstable

This is how you show a reviewer that the checker can fail, rather than demonstrating a green tick that might be decorative.

Expected output

Before the fix:

  [PASS] script exists -- .../notebook_v1.py
  [PASS] run with PYTHONHASHSEED=0 exits 0 -- exit 0
  [PASS] run with PYTHONHASHSEED=1 exits 0 -- exit 0
  [PASS] run with PYTHONHASHSEED=42 exits 0 -- exit 0
  [FAIL] three reports are byte-identical -- 3 distinct reports
  [PASS] report still contains a metric line -- look for 'MAE: <float>'

L01 Reproducible notebook report: 5/6 checks passed

After the fix the same command reports 1 distinct reports and ACCEPTED. Your MAE will not match the reference exactly unless you use the same seed; what must match is the stability, not the value.

Acceptance criteria

# Criterion How it is checked
1 The script still runs and prints a metric MAE: present in output
2 Three runs under different hash seeds are byte-identical Reports compared as strings
3 Every run exits 0 Exit codes captured per run
4 The defect is demonstrated, not just fixed --expect unstable run recorded
5 No data is modified git status on the data file, or a checksum before and after

Troubleshooting

Symptom Likely cause Fix
FileNotFoundError: data/sessions.csv Setup command run from the wrong directory Run it from the lab directory, or pass --out with the full path
Still 3 distinct reports after seeding A set or dict built from a set still controls order Iterate a sorted list; check every for ... in {
Report identical but MAE changes The metric line is rounded, the number is not Compare the raw value; seed the split before anything else
ModuleNotFoundError: sklearn Dependencies installed in a different interpreter python -m pip install -r examples/requirements-labs.txt and check python -c "import sys; print(sys.executable)"
Checker passes, python notebook_v1.py differs by hand You compared against a stale run-a.txt Delete the old files and re-run both commands

Reflection questions

  1. Which of the three causes would a random_state on the split have fixed, and which would it have left in place?
  2. A colleague argues that a 0.1 difference in MAE does not matter. What is the argument for fixing this anyway?
  3. Where else in a project could iteration order leak into an artefact?
  4. If you had to choose between pinning the environment and seeding the code, which buys more reproducibility per unit of effort, and why?

Optional extension

Add a --seed argument and a format_report() function that returns the report as a string, then write a test that calls it twice and asserts equality. The reference solution in examples/solutions/lab-01/ is organised this way.

Cleanup

rm -f run-a.txt run-b.txt && rm -rf data

PowerShell: Remove-Item run-a.txt, run-b.txt -ErrorAction SilentlyContinue; Remove-Item data -Recurse -Force.

Checklist

  • I reproduced the difference before changing code
  • I can name the three causes and point to their lines
  • My report is identical across three hash seeds
  • I recorded a failing run as evidence
  • I ran check_lab01.py and it reports ACCEPTED