flowchart TD
subgraph S1["Draft & Debug"]
direction LR
A["1. Draft .ipynb<br/>Exploratory Edits"] --> B["2. Live Kernel RAM<br/>Out-of-Order Risk<br/>counts: 5, 2, 3"]
B -->|"Hidden State Trap"| B
end
subgraph S2["Verify & Publish"]
direction LR
C["3. Restart & Run All<br/>Fresh Kernel RAM<br/>counts: 1, 2, 3"] --> D["4. Preflight Export<br/>Strip Outputs +<br/>Atomic .tmp Ckpt"]
D --> E["5. Reproducible Repo<br/>Clean Git Diff +<br/>Colab Badge URL"]
end
S1 --> S2
13 Mastering Jupyter Notebooks & Google Colab Engineering
Chapter Mission: Transform interactive notebooks from fragile scratchpads into publication-grade engineering instruments. Master the
.ipynbJSON schema, eliminate hidden-state bugs via strictRestart & Run Alldiscipline, manage Google Colab CPU/GPU runtimes,drive.mount, and IPython magics (%timeit,%%writefile) cleanly, build portable Drive/local path resolvers, protect long runs with atomic JSON checkpoints, and isolate randomness withrandom.Random(seed).
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch13 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch13 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch13 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Inspect and manipulate Jupyter
.ipynbfiles directly as structured JSON documents (cells,cell_type,execution_count,source,outputs). - Detect hidden-state execution traps by auditing
execution_countmonotonicity and enforcingRestart & Run Allbefore sharing or publishing. - Strip bulky code-cell outputs on a deep copy for clean Git diffs and construct canonical Google Colab GitHub launch URLs.
- Engineer portable Google Colab workflows—choosing CPU vs. GPU runtimes wisely, bridging
drive.mountand local folders with a path resolver, and separating IPython magics (%timeit,%%writefile,!pip) from pure.pymodules. - Guard long Colab experiments against runtime disconnects using crash-proof atomic JSON checkpoints (
.tmp\(\rightarrow\).replace()) andresume_or_initrecovery. - Guarantee deterministic train/test splits across cell re-runs using isolated
random.Random(seed)generators instead of mutating globalrandomstate.
13.1 Stage 1 — First-Principles Intuition: The Notebook File vs. the Live Kernel
Almost every beginner assumes a Jupyter notebook executes top-to-bottom like a standard Python script. In reality, a notebook consists of two completely separate entities:
- The
.ipynbFile on Disk (JSON Document): A plain UTF-8 JSON file containing a list of cell dictionaries ("cells"), each holding its"cell_type"("markdown"or"code"),"source"lines, saved"outputs", and the integer"execution_count"printed inside[3]:. - The Live Python Kernel in RAM (Stateful Process): A long-running Python interpreter that remembers every variable, import, and mutated list from whichever cell you ran last—even if you later edited or deleted the cell that created that variable!
Think of your .ipynb file as your written lab protocol and the Jupyter kernel as your physical lab bench:
- Hidden State: If you mix reagent
x = 10in Cell 1, run Cell 2, then delete Cell 1 and run Cell 2 again, Cell 2 still succeeds becausexis sitting on the bench (kernel RAM)—even though the protocol (.ipynb) no longer definesx. The moment a peer reviewer clicks Restart & Run All, your paper crashes withNameError. - Ephemeral Colab VMs: Google Colab hands you a temporary Linux virtual machine (CPU for data cleaning and Pandas; T4/L4/A100 GPU for deep learning). After an idle timeout or 12-hour cap, Google recycles the VM and wipes
/content/. Only files saved to mounted Google Drive or downloaded to Git survive. - Magics Are Bench Tools, Not Pure Python: Line magics (
%timeit), cell magics (%%writefile), and shell escapes (!pip install) are conveniences parsed by IPython—not valid Python syntax. Before shipping reusable code into a.pymodule, every!and%line must be stripped.
13.1.1 Choosing Colab Runtimes & Mounting Drive Wisely
In Google Colab (Runtime \(\rightarrow\) Change runtime type), match hardware to workload:
- CPU Runtime (Default): Use for CSV/JSON wrangling, exploratory plots, statistical tests, and chapters in Parts 1–5. CPU sessions start quickly and preserve your limited free GPU quota.
- GPU Runtime (T4 / L4 / A100): Enable only when training neural networks or running matrix-heavy tensor workloads (Part 6 onward), and switch back to CPU once training finishes.
- Google Drive Persistence: In an interactive Colab cell, researchers mount Drive once via
from google.colab import drive; drive.mount("/content/drive"), exposing persistent folders under/content/drive/MyDrive/.... Never hardcode that raw path across 15 cells—wrap dataset lookup in a portable resolver that checks Drive first and falls back to a local directory on your laptop or CI runner.
13.2 Stage 2 — Visual Mental Model: Kernel-State & Restart-Run-All Lifecycle
Trace how a raw exploratory notebook matures into a crash-safe, Git-friendly, peer-reviewable research artifact:
Notice the two engineering gates that separate fragile notebooks from reproducible research pipelines:
- Verification Gate (
Restart & Run All): Wiping kernel RAM and executing top-to-bottom proves every cell depends only on code written above it, stamping strictly increasingexecution_countvalues (\(1 < 2 < 3 < \dots\)). - Persistence & Git Gate (Strip Outputs + Atomic Checkpoints): Stripping megabytes of base64 PNG plots before
git commitkeeps diffs clean, while writing experiment state tockpt.tmpand atomically renaming via.replace(ckpt)guarantees a Colab disconnect never leaves a half-written JSON file behind.
.py Modules)
| Magic / Escape | Scope | Typical Research Use | Valid in .py? |
|---|---|---|---|
%timeit expr |
Single line | Micro-benchmark a clean Python expression | No |
%%writefile f.py |
Entire cell | Export a clean cell directly to a .py file |
No |
%matplotlib inline |
Notebook session | Render figures directly inside cell outputs | No |
!pip install -q pkg |
Shell command | Install a missing package inside Colab VM | No |
13.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_03_engineering_workflow/ch13_jupyter_colab/01_walkthrough.ipynb in Colab and run every cell top-to-bottom alongside the dissections below. Every file demo executes inside tempfile.TemporaryDirectory() so zero junk files appear in your workspace.
13.3.1 .ipynb JSON Anatomy & Hidden-State Out-of-Order Audit
Because every .ipynb file is standard UTF-8 JSON, Python can audit any notebook programmatically for hidden-state risk before publication:
import json
from pathlib import Path
import tempfile
sample_nb = {
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {},
"cells": [
{"cell_type": "markdown", "source": ["# Dengue Audit\n"]},
{
"cell_type": "code",
"execution_count": 5,
"source": ["threshold = 38.0\n"],
"outputs": [],
},
{
"cell_type": "code",
"execution_count": 2,
"source": ["print(threshold)\n"],
"outputs": [{"output_type": "stream", "text": ["38.0\n"]}],
},
],
}
def is_out_of_order(nb_dict):
prev = 0
for cell in nb_dict["cells"]:
if cell["cell_type"] != "code":
continue
ec = cell["execution_count"]
if ec is None or ec <= prev:
return True
prev = ec
return False
with tempfile.TemporaryDirectory() as tmp:
nb_file = Path(tmp) / "analysis.ipynb"
nb_file.write_text(json.dumps(sample_nb, indent=2), encoding="utf-8")
loaded = json.loads(nb_file.read_text(encoding="utf-8"))
counts = [
c["execution_count"] for c in loaded["cells"]
if c["cell_type"] == "code"
]
print("Code cell counts:", counts)
print("Hidden-state risk (out of order):", is_out_of_order(loaded))Expected Output:
Key mechanics to inspect closely:
- Skip non-code cells: Markdown cells carry
"source"text but have no"execution_count"or"outputs". Always guard withif cell["cell_type"] != "code": continue. - Strict monotonicity (
ec <= prev): Cell 1 ran at count5after Cell 2 ran at count2, meaning Cell 2’s saved output was produced by an older value ofthreshold! Bothec is None(unexecuted) andec <= prev(out-of-order or duplicate) flag hidden-state risk.
13.3.2 Portable Colab/Local Path Resolver & Magic Awareness
Never scatter raw /content/drive/MyDrive/... strings across your analysis cells. Instead, resolve data files through one portable helper that prioritizes Google Drive when mounted and falls back to your local repository folder otherwise:
from pathlib import Path
import tempfile
# Colab-only reference (never invoke live inside portable modules):
# from google.colab import drive; drive.mount('/content/drive')
# Interactive magics like '%timeit' or '%%writefile' are IPython-only.
def find_dataset(filename, drive_dir=None, local_dir=None):
for folder in (drive_dir, local_dir):
if folder is not None:
candidate = Path(folder) / filename
if candidate.exists():
return candidate
raise FileNotFoundError(f"Missing dataset: {filename}")
with tempfile.TemporaryDirectory() as tmp:
sim_drive = Path(tmp) / "colab_drive"
sim_local = Path(tmp) / "local_data"
sim_drive.mkdir()
sim_local.mkdir()
(sim_local / "mch_cohort.csv").write_text("id,hb\n1,11.2\n")
resolved = find_dataset("mch_cohort.csv", sim_drive, sim_local)
print("Resolved source folder:", resolved.parent.name)
print("First line:", resolved.read_text().splitlines()[0])Expected Output:
Notice how cleanly this pattern decouples storage location from analysis code:
- Fail loudly with
FileNotFoundError: If neither Drive nor local storage contains the dataset, raisingFileNotFoundErrorimmediately stops execution with a clear diagnostic message instead of returningNoneand crashing ten cells later. - No live Colab dependencies: Because
drive.mountstays isolated in a single Colab setup cell (or optional guard), the rest of your pipeline runs identically in Colab, local VS Code, and automated CI tests.
13.3.3 Crash-Proof Atomic JSON Checkpointing
If a Colab session disconnects halfway through json.dump(state, open(ckpt, "w")), the target file is already truncated to zero bytes and left half-written—destroying hours of cross-validation or neural-network progress. Atomic checkpointing eliminates that window completely by writing to a sibling .tmp file first and swapping it into place with POSIX-atomic Path.replace():
import json
from pathlib import Path
import tempfile
def write_checkpoint(ckpt_path, state_dict):
target = Path(ckpt_path)
tmp_path = target.with_suffix(".tmp")
with open(tmp_path, "w", encoding="utf-8") as out_f:
json.dump(state_dict, out_f, indent=2)
tmp_path.replace(target)
with tempfile.TemporaryDirectory() as tmp:
ckpt = Path(tmp) / "train_state.json"
write_checkpoint(ckpt, {"epoch": 1, "val_auc": 0.81})
write_checkpoint(ckpt, {"epoch": 2, "val_auc": 0.86})
restored = json.loads(ckpt.read_text(encoding="utf-8"))
leftover_tmp = ckpt.with_suffix(".tmp").exists()
print("Restored checkpoint:", restored)
print("Leftover .tmp exists:", leftover_tmp)Expected Output:
.replace() Beats .rename() Across Operating Systems
On Linux and macOS, Path.rename() overwrites an existing target file, but on Windows Path.rename() raises FileExistsError when ckpt_path already exists! Always use tmp_path.replace(target)—it atomically overwrites the destination cleanly on Linux, macOS, Windows, and Google Colab alike.
13.3.4 Isolated RNG Reproducibility (random.Random(seed))
Calling global random.seed(42) at the top of a notebook is fragile: if a collaborator runs a plotting or bootstrap cell twice before reaching your train/test split, Python’s global RNG advances and silently changes which patients land in your test fold! Instantiate a local generator rng = random.Random(seed) so your split is 100% immune to activity in other cells:
import random
def split_indices(n_samples, test_ratio=0.2, seed=42):
rng = random.Random(seed)
indices = list(range(n_samples))
rng.shuffle(indices)
n_test = int(n_samples * test_ratio)
return indices[n_test:], indices[:n_test]
random.seed(999)
before_global = random.getstate()
train_a, test_a = split_indices(10, test_ratio=0.2, seed=42)
after_global = random.getstate()
random.seed(12345)
train_b, test_b = split_indices(10, test_ratio=0.2, seed=42)
print("Test fold indices:", test_a)
print("Identical across global reseeds:", test_a == test_b)
print("Global RNG untouched:", before_global == after_global)Expected Output:
13.3.5 Break It On Purpose! (Notebook & Colab Error Anatomy)
Python’s Traceback:
Diagnosis: After a Colab runtime reset, /content/drive/MyDrive is no longer mounted, so any hardcoded Drive path immediately fails with FileNotFoundError.
Fix: Route all dataset lookups through resolve_data_path(filename, drive_dir, local_dir) and verify candidate.exists() cleanly.
Python’s Traceback:
Diagnosis: Writing directly to a checkpoint file leaves truncated JSON if Colab disconnects mid-write, crashing every subsequent resume attempt with json.JSONDecodeError.
Fix: Write to ckpt.with_suffix(".tmp") followed by tmp_path.replace(ckpt), and guard checkpoint loads with try ... except json.JSONDecodeError:.
import json
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
bad_nb_path = Path(tmp) / "malformed.ipynb"
bad_nb_path.write_text('{"nbformat": 4}', encoding="utf-8")
with open(bad_nb_path, "r", encoding="utf-8") as f:
nb_data = json.load(f)
cell_count = len(nb_data["cells"])Python’s Traceback:
Diagnosis: Indexing nb_data["cells"] directly crashes with KeyError on truncated or non-standard JSON documents (such as a failed Git merge conflict inside a .ipynb file).
Fix: Use defensive access (nb_data.get("cells", [])) or validate required top-level schema keys before auditing notebook contents.
13.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: Before pushing an exploratory Colab notebook to your paper’s GitHub repository, you need an automated preflight tool preflight_and_clean_notebook(in_nb, out_nb) that inspects the raw .ipynb file, records whether any code cell ran out of order and whether saved outputs were present, strips all code-cell outputs and execution counts on a deep copy, atomically writes the Git-ready .ipynb to out_nb, and returns the audit report.
13.4.1 Blueprint (Completed on Paper Before Coding)
Input & Output Contract:
- Input:
in_nb(source.ipynbpath) andout_nb(destination clean.ipynbpath). - Output: Clean UTF-8
.ipynbwritten atomically toout_nb+ returned dict{"out_of_order": bool, "had_outputs": bool}.
- Input:
Edge Cases & Traps:
- Never mutate the parsed
rawdictionary in place; work oncopy.deepcopy(raw)so upstream inspection remains pure. - Ignore markdown cells when checking
execution_countand clearingoutputs. - Latch
out_of_order = Truepermanently once any code cell hasec is Noneorec <= prev. - Write to
out_nb.with_suffix(".tmp")and swap via.replace(target)so partial writes never corruptout_nb.
- Never mutate the parsed
Step-by-Step Algorithm:
- Load
rawfromin_nbusingjson.load. - Deep-copy
rawintocleaned; initializeprev = 0,out_of_order = False,had_outputs = False. - Loop over
cleaned.get("cells", []); for each"code"cell, auditexecution_countandoutputs, then resetoutputs = []andexecution_count = None. - Write
cleanedtotarget.with_suffix(".tmp")and atomically calltmp_path.replace(target). - Return
{"out_of_order": out_of_order, "had_outputs": had_outputs}.
- Load
Verification Plan (Hand Check):
- Draft notebook with counts
[4, 2]and one stream output \(\rightarrow\)out_of_order = True,had_outputs = True, and savedclean.ipynbcell 0 hasoutputs == [].
- Draft notebook with counts
13.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
import copy
import json
from pathlib import Path
import tempfile
def preflight_and_clean_notebook(in_nb, out_nb):
"""Audit execution order, strip outputs, and save atomically."""
with open(in_nb, "r", encoding="utf-8") as in_f:
raw = json.load(in_f)
prev = 0
out_of_order = False
had_outputs = False
cleaned = copy.deepcopy(raw)
for cell in cleaned.get("cells", []):
if cell.get("cell_type") != "code":
continue
ec = cell.get("execution_count")
if ec is None or ec <= prev:
out_of_order = True
else:
prev = ec
if cell.get("outputs"):
had_outputs = True
cell["outputs"] = []
cell["execution_count"] = None
target = Path(out_nb)
tmp_path = target.with_suffix(".tmp")
with open(tmp_path, "w", encoding="utf-8") as out_f:
json.dump(cleaned, out_f, indent=2)
tmp_path.replace(target)
return {"out_of_order": out_of_order, "had_outputs": had_outputs}
with tempfile.TemporaryDirectory() as tmp:
raw_nb = Path(tmp) / "draft.ipynb"
git_nb = Path(tmp) / "clean.ipynb"
draft = {
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {},
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"source": ["x = 7\n"],
"outputs": [{"output_type": "stream", "text": ["7\n"]}],
},
{
"cell_type": "code",
"execution_count": 2,
"source": ["y = x + 1\n"],
"outputs": [],
},
],
}
raw_nb.write_text(json.dumps(draft, indent=2), encoding="utf-8")
report = preflight_and_clean_notebook(raw_nb, git_nb)
saved = json.loads(git_nb.read_text(encoding="utf-8"))
print("Preflight report:", report)
print("Clean cell 0 outputs:", saved["cells"][0]["outputs"])Output:
13.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_03_engineering_workflow/ch13_jupyter_colab/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and build all six notebook-engineering utilities below. Every file-based check runs inside tempfile.TemporaryDirectory():
- To-Do 13.1 (Hidden-State Audit —
detect_out_of_order_cells(nb_dict)): Inspect code cells’execution_count(skipping markdown cells) and returnTrueif any code cell hasexecution_count is Noneor<=the previous executed count, elseFalse. - To-Do 13.2 (Git Hygiene —
strip_notebook_outputs(nb_dict)): Return a deep-copied clean notebook dict withoutputs = []andexecution_count = Noneon every code cell without mutating the caller’s input dictionary. - To-Do 13.3 (Colab Launcher —
build_colab_url(owner, repo, branch, rel_path)): Return the exact URLhttps://colab.research.google.com/github/{owner}/{repo}/blob/{branch}/{rel_path}after stripping any leading/fromrel_path. - To-Do 13.4 (Portable Storage —
resolve_data_path(filename, drive_dir=None, local_dir=None)): CheckPath(drive_dir) / filenamefirst, thenPath(local_dir) / filename; return the first existingPathor raiseFileNotFoundError. - To-Do 13.5 (Atomic Checkpointing —
save_checkpoint_atomic(ckpt_path, state_dict)): Serializestate_dictas UTF-8 JSON toPath(ckpt_path).with_suffix(".tmp"), then atomically rename it ontockpt_pathvia.replace(). - To-Do 13.6 (Isolated Reproducibility —
seeded_train_test_indices(n_samples, test_ratio=0.2, seed=42)): Use an isolatedrandom.Random(seed)(never globalrandom.seed()) to shufflelist(range(n_samples))and return(train_idx, test_idx)withlen(test_idx) == int(n_samples * test_ratio).
13.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Test your understanding on paper before checking Stage 8:
- Hidden-State Execution Counts: A notebook has three code cells with
execution_countvalues[1, 4, 2]and one markdown cell between them. Why is this notebook unsafe to submit as reproducible supplementary material? - Unexecuted Cell Risk: A notebook has code cells with
execution_countvalues[1, None, 2]. What doesdetect_out_of_order_cells(nb_dict)return, and why? - Shallow vs. Deep Copy Bug: Why does
cleaned = nb_dict.copy()fail to protectnb_dictwhenstrip_notebook_outputssetscell["outputs"] = []? - Colab Ephemeral Disk: You save
cleaned_cohort.csvdirectly to/content/cleaned_cohort.csvin Google Colab, close your laptop for 3 hours, and reconnect to a new runtime. What happened tocleaned_cohort.csv? - CPU vs. GPU Discipline: Why is it poor practice to leave a Colab T4/A100 GPU runtime enabled while writing Pandas data-cleaning code and reading documentation?
- Magic Syntax Error: You copy a Colab cell containing
!pip install -q pandasand%timeit sum(range(1000))straight intoutils.pyand runimport utils. What Python exception occurs? .replace()vs. Direct Write: Supposetrain_state.jsoncurrently holds a valid epoch-10 checkpoint, and your code writes epoch 11 usingsave_checkpoint_atomic. Ifjson.dump()crashes halfway through with aTypeError, what is insidetrain_state.jsonafterward?- Global vs. Isolated RNG: Why is
rng = random.Random(42); rng.shuffle(idx)strictly safer inside a notebook thanrandom.seed(42); random.shuffle(idx)? - Zero Test-Ratio Slice Trap: Suppose
indiceshas length 10 andn_test = int(10 * 0.0) == 0. What doesindices[-n_test:]evaluate to in Python, and why doesindices[:n_test]avoid this bug? - Git Diff Bloat: Why do research teams strip cell
"outputs"before committing.ipynbfiles to Git?
13.7 Stage 7 — Chapter 13 Hands-On Coding Exam (100 Points)
Open part_03_engineering_workflow/ch13_jupyter_colab/03_coding_exam.ipynb, implement all three notebook-engineering functions, and run tutor.check_exam("ch13", exam_submissions) to earn your Chapter 13 score out of 100 points.
13.7.1 Exam Task 13.1 (30 Points): Audit Notebook Reproducibility
Write audit_notebook_reproducibility(nb_path) to inspect any .ipynb file on disk:
- Load
nb_pathas UTF-8 JSON and count"code"cells ("code_cells") and"markdown"cells ("markdown_cells"). - Compute
"out_of_order"(Trueif any code cell hasexecution_count is Noneor<=previous executed count, elseFalse). - Compute
"has_outputs"(Trueif any code cell has non-empty"outputs", elseFalse) and"clean_for_git"(not has_outputs). - Return the 5-key dictionary
{"code_cells": int, "markdown_cells": int, "out_of_order": bool, "has_outputs": bool, "clean_for_git": bool}.
13.7.2 Exam Task 13.2 (35 Points): Extract Code Cells to Pure Python Module
Write extract_code_to_module(nb_path, py_out_path) to graduate a prototype notebook into a clean importable .py module:
- Load
nb_pathas UTF-8 JSON and extract only"code"cells (ignoring markdown cells). - Skip every line whose stripped text starts with
!or%(shell escapes and line/cell magics). - Join non-empty code cell blocks with blank lines (
"\n\n"), write the UTF-8.pyfile topy_out_path, and return the integer count of non-empty code lines written.
13.7.3 Exam Task 13.3 (35 Points): Resume or Atomically Initialize Experiment
Write resume_or_init_experiment(ckpt_path, default_state) for disconnect-proof Colab experiments:
- If
ckpt_pathexists and parses cleanly as a JSONdict, load and return(loaded_state, True). - Otherwise (missing file, corrupted/truncated JSON raising
json.JSONDecodeError, or valid non-dict JSON such as a list), deep-copydefault_state, atomically write it tockpt_pathvia.tmp+.replace(), and return(copy_of_default, False).
13.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
13.8.1 A. Concept & Execution Trace Test Key
- Because cell 2 ran at count
4after cell 3 ran at count2, cell 3’s output was computed from stale kernel state—running Restart & Run All may produce completely different results or crash. - It returns
True, because an unexecuted code cell (execution_count is None) has never been verified during a clean top-to-bottom run. .copy()creates a shallow copy whose"cells"list still points to the exact same inner cell dictionaries in memory; mutatingcell["outputs"] = []modifies the caller’s originalnb_dict.copy.deepcopy(nb_dict)copies all nested lists and dicts safely.- It was permanently deleted when Google Colab recycled the ephemeral VM. Persistent outputs must be written to mounted Google Drive or exported to Git/cloud storage.
- Colab enforces rolling GPU usage quotas; burning GPU hours on CPU-bound data cleaning can lock you out of GPU access when you actually need to train a deep-learning model later.
SyntaxError, because!shell escapes and%magics are IPython preprocessor directives, not valid Python grammar.train_state.jsonstill contains the intact epoch-10 checkpoint, because the crash occurred while writing totrain_state.tmpbeforetmp_path.replace(target)ever ran.random.Random(seed)isolates its internal PRNG state inside a local object so unrelated random calls in other notebook cells can never perturb your train/test split.- In Python,
-0 == 0, soindices[-0:]is identical toindices[0:]and wrongly returns all 10 elements! Usingindices[:n_test]returns[]cleanly whenn_test == 0. - Embedded outputs (especially base64-encoded matplotlib PNGs and noisy execution counts) bloat repository size and generate unreadable multi-thousand-line merge conflicts on every edit.
13.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import copy
import json
from pathlib import Path
import random
# To-Do 13.1
def detect_out_of_order_cells(nb_dict):
"""Return True if any code cell is unexecuted or out of order."""
prev = 0
for cell in nb_dict.get("cells", []):
if cell.get("cell_type") != "code":
continue
ec = cell.get("execution_count")
if ec is None or ec <= prev:
return True
prev = ec
return False
# To-Do 13.2
def strip_notebook_outputs(nb_dict):
"""Return a deep copy of nb_dict with cleared code cell outputs."""
cleaned = copy.deepcopy(nb_dict)
for cell in cleaned.get("cells", []):
if cell.get("cell_type") == "code":
cell["outputs"] = []
cell["execution_count"] = None
return cleaned
# To-Do 13.3
def build_colab_url(owner, repo, branch, rel_path):
"""Return the Google Colab GitHub launch URL for rel_path."""
clean_path = str(rel_path).lstrip("/")
return (
"https://colab.research.google.com/github/"
f"{owner}/{repo}/blob/{branch}/{clean_path}"
)
# To-Do 13.4
def resolve_data_path(filename, drive_dir=None, local_dir=None):
"""Resolve filename from drive_dir first, then local_dir."""
for directory in (drive_dir, local_dir):
if directory is not None:
candidate = Path(directory) / filename
if candidate.exists():
return candidate
raise FileNotFoundError(f"Dataset not found: {filename}")
# To-Do 13.5
def save_checkpoint_atomic(ckpt_path, state_dict):
"""Atomically write state_dict as UTF-8 JSON via a .tmp file."""
target = Path(ckpt_path)
tmp_path = target.with_suffix(".tmp")
with open(tmp_path, "w", encoding="utf-8") as f:
json.dump(state_dict, f, indent=2)
tmp_path.replace(target)
# To-Do 13.6
def seeded_train_test_indices(n_samples, test_ratio=0.2, seed=42):
"""Return deterministic (train_idx, test_idx) via local RNG."""
rng = random.Random(seed)
indices = list(range(n_samples))
rng.shuffle(indices)
n_test = int(n_samples * test_ratio)
return indices[n_test:], indices[:n_test]13.8.3 C. Annotated Solutions — Chapter 13 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
import copy
import json
from pathlib import Path
# Exam Task 13.1 (30 pts)
def audit_notebook_reproducibility(nb_path):
"""Audit .ipynb cell counts, execution order, and Git cleanliness."""
with open(nb_path, "r", encoding="utf-8") as f:
nb = json.load(f)
code_cells = 0
markdown_cells = 0
out_of_order = False
has_outputs = False
prev = 0
for cell in nb.get("cells", []):
ctype = cell.get("cell_type")
if ctype == "markdown":
markdown_cells += 1
elif ctype == "code":
code_cells += 1
ec = cell.get("execution_count")
if ec is None or ec <= prev:
out_of_order = True
else:
prev = ec
if cell.get("outputs"):
has_outputs = True
return {
"code_cells": code_cells,
"markdown_cells": markdown_cells,
"out_of_order": out_of_order,
"has_outputs": has_outputs,
"clean_for_git": not has_outputs,
}
# Exam Task 13.2 (35 pts)
def extract_code_to_module(nb_path, py_out_path):
"""Export code cells without !/% magics to .py and return line count."""
with open(nb_path, "r", encoding="utf-8") as f:
nb = json.load(f)
blocks = []
for cell in nb.get("cells", []):
if cell.get("cell_type") != "code":
continue
src = cell.get("source", [])
text = "".join(src) if isinstance(src, list) else src
kept = [
line for line in text.splitlines()
if not line.lstrip().startswith(("!", "%"))
]
block = "\n".join(kept).strip("\n")
if block:
blocks.append(block)
content = ("\n\n".join(blocks) + "\n") if blocks else ""
with open(py_out_path, "w", encoding="utf-8") as out_f:
out_f.write(content)
return sum(1 for line in content.splitlines() if line.strip())
# Exam Task 13.3 (35 pts)
def resume_or_init_experiment(ckpt_path, default_state):
"""Resume valid JSON dict checkpoint or atomically initialize it."""
target = Path(ckpt_path)
if target.exists():
try:
with open(target, "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, dict):
return loaded, True
except (json.JSONDecodeError, OSError):
pass
fresh = copy.deepcopy(default_state)
tmp_path = target.with_suffix(".tmp")
with open(tmp_path, "w", encoding="utf-8") as out_f:
json.dump(fresh, out_f, indent=2)
tmp_path.replace(target)
return fresh, False13.8.4 D. Debugging Post-Mortem: Top 3 Chapter 13 Failure Modes
- Mutating Input Notebooks via Shallow Copy or Touching Markdown Cells: Calling
nb_dict.copy()leaves the nested"cells"list shared with the caller, silently wiping original outputs in place; similarly, accessingcell["execution_count"]without checkingcell.get("cell_type") == "code"crashes on the very first markdown heading. - Corrupting Checkpoints on Mid-Write Disconnects: Writing JSON straight to
ckpt_pathtruncates the previous good state the instantopen(..., "w")executes. Always stage writes intarget.with_suffix(".tmp"), swap withtmp_path.replace(target), and catchjson.JSONDecodeErrorduring resume. - Leaving Magics in Exported
.pyFiles or Sharing Global RNG State: Failing to filter!and%lines produces broken.pymodules that raiseSyntaxErroron import, while relying on globalrandom.seed()lets an interactive exploration cell silently alter your held-out test indices.
Your notebooks are now auditable, Git-clean, crash-resilient, and deterministically reproducible across both Google Colab and local machines. In the next chapter, we scale these engineering habits to full scientific repositories with Git version control, virtual environments, and automated testing.


