Lab 24 — Reproducible Workflows¶
Session: Week 12 · Session 24 · 90 min CLO: CLO-3 Difficulty: Intermediate
Learning objectives¶
By the end of this lab you can:
- Pin dependencies with a
requirements.txtand reproduce an environment. - Control randomness with seeds everywhere it matters.
- Structure a project folder so someone else can run it.
- Audit a notebook for reproducibility holes.
Problem statement¶
"Works on my machine" is the enemy. You will take the EDA/model notebook from your Lab 23 repo and make it runnable by a stranger: pinned requirements, seeded randomness, a documented folder layout, and a written reproducibility checklist. Then you will prove it by running your own notebook from a fresh kernel (Restart & Run All) after "losing" the venv.
Dataset requirements¶
Your own project dataset from Lab 23 (or penguins as fallback).
Step-by-step tasks¶
- Freeze requirements: in your project folder run
pip freeze > requirements.txt. Inspect it: it will contain transitive deps — that is fine, but add a comment header naming the Python version you used (edit the file to add# Python 3.14etc. as the first line). - Seed audit: open your notebook. Find every place randomness appears:
train_test_split,KMeans,DecisionTreeClassifier,np.random.*. Addrandom_state=42(or aSEED = 42constant defined once at the top) to each. If you usenp.random, callnp.random.seed(SEED)at the top of the notebook. - Two-run test: run the notebook twice (Restart & Run All). All printed numbers that should be deterministic must be identical. Record any that aren't, and fix them.
- Fresh environment test: create a second venv
(
.venv-repro), install-r requirements.txt, and run the notebook with that kernel. It must work (allow 5–10 minutes for installs). - Folder layout: confirm your project folder has at least:
Add a
projects/final/ README.md # proposal + how to run requirements.txt data/ # or data_loader.py eda.ipynb models.ipynbHOW_TO_RUN.mdwith exactly three commands. - Checklist (markdown): write a 6-item reproducibility checklist (seed, requirements, data loader, order-of-cells, versioned outputs, README) and self-grade each item ✓/✗.
Starter code¶
# HOW_TO_RUN.md
1. python -m venv .venv
2. .venv/bin/pip install -r requirements.txt (or .venv\Scripts\pip on Windows)
3. .venv/bin/jupyter lab eda.ipynb (then: Kernel -> Restart & Run All)
# put this at the top of your notebook (cell 1)
SEED = 42
import numpy as np
np.random.seed(SEED)
Expected output¶
requirements.txtwith a# Pythonversion header, committed.- Notebook with
SEEDused in every randomized call — verified by search (random_stateappears ≥ 3 times orSEEDis threaded through). - Two consecutive Restart & Run All runs produce identical key numbers.
.venv-reproinstall + run succeeds (document the run).HOW_TO_RUN.mdcommitted; folder layout matches the tree.- Checklist with all 6 items self-graded.
Questions¶
- Why pin exact versions (
numpy==2.0.1) instead ofnumpy>=2.0? - Where else can nondeterminism hide besides random seeds? (Think: model training order, dict iteration, time-based features.)
- Why does the seed belong at the top of the notebook?
- What is the difference between reproducibility and correctness? Can a run be reproducible and wrong?
- Why keep data loading in a function/file instead of pasting the URL in a notebook cell?
Challenge task¶
Write a tiny data_loader.py for your project dataset: a function
load_data() that returns the DataFrame, with a docstring stating source,
license, and cache location (see Lab 11's caching pattern). Import and call
it from your notebook (replace the inline load). Commit and push everything
from this lab.
Marking rubric¶
| Criteria | Max | Notes |
|---|---|---|
| requirements.txt + version header | 3 | committed |
| Seed audit complete | 5 | every random call seeded |
| Two-run determinism test | 4 | identical key numbers |
| Fresh venv run succeeds | 4 | documented |
| Folder layout + HOW_TO_RUN | 3 | matches tree |
| Checklist self-graded | 3 | 6 items |
| Answers to questions | 3 | Q1, Q2, Q4 correct |
| Challenge: data_loader.py | 5 | function + docstring + used |
| Total | 30 |