{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "purpose",
   "metadata": {},
   "source": [
    "# Keep the notebook, inputs and execution state separate\n",
    "\n",
    "This original AI Essay practice uses the **same six fictional rows** as the [blank-versus-zero exercise](https://ai-essay.com/tools/julius-ai#missing-values-check). Its new task is to reconstruct an execution from saved cells and an explicitly supplied input. There are no real participants or measured learning results.\n",
    "\n",
    "The code was checked locally in Python. It has **not** been executed in Colab, and no Gemini, GPU, paid plan, Drive mount, network request or package installation is required by this notebook's code. Availability of your chosen notebook service remains separate.\n",
    "\n",
    "## Prepare the input\n",
    "\n",
    "Download the [unchanged practice CSV](https://ai-essay.com/assets/practice/julius-six-row-missing-values-ed21e7f851cd.csv). Put it in the Python kernel's current working directory using the file facilities of your environment. In Colab, opening this `.ipynb` does not itself upload that separate CSV. If your file is elsewhere, deliberately set `DATA_PATH` in the first code cell to its actual location. The input checksum will reject a changed or re-encoded file.\n",
    "\n",
    "## Try first\n",
    "\n",
    "1. Read all four code cells before executing them. Identify the cell that defines each name used later.\n",
    "2. Run the cells from top to bottom. Record the input checksum, Python version, missing-value policy and group results from the final cell.\n",
    "3. Start a fresh kernel/runtime and try the **last code cell alone**. Predict why names from the earlier session are unavailable. Then supply the input again if needed and run all cells from the top.\n",
    "4. Compare the new record with the first. A missing-file error asks you to restore the input; a checksum mismatch asks you to inspect which file you supplied. Do not repair either by pasting expected answer numbers.\n",
    "\n",
    "The distributed notebook contains no saved execution output. Keep only outputs you intend to share, and inspect your own downloaded file before sharing it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "setup",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from io import StringIO\n",
    "import csv\n",
    "import hashlib\n",
    "import json\n",
    "import platform\n",
    "\n",
    "DATA_PATH = Path('julius-six-row-missing-values-ed21e7f851cd.csv')\n",
    "EXPECTED_SHA256 = 'ed21e7f851cd080864d3ccefe759ae9adc2af595324fbe5ae29765dfe9e14f3c'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "input-note",
   "metadata": {},
   "source": [
    "## Read the actual input\n",
    "\n",
    "This cell depends on the setup cell and the separate CSV file. The file stays separate from the `.ipynb`. A temporary runtime's files are not an automatic part of a shared notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "load-input",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_input = DATA_PATH.read_bytes()\n",
    "input_sha256 = hashlib.sha256(raw_input).hexdigest()\n",
    "assert input_sha256 == EXPECTED_SHA256, \"The input differs from the frozen practice CSV.\"\n",
    "rows = list(csv.DictReader(StringIO(raw_input.decode(\"utf-8\"))))\n",
    "assert len(rows) == 6\n",
    "assert set(rows[0]) == {\"practice_record\", \"group\", \"minutes\"}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "method-note",
   "metadata": {},
   "source": [
    "## State the method explicitly\n",
    "\n",
    "The grouping below excludes a blank from recorded-minute counts and means, but includes a recorded zero. This is the rule for this invented file, not a recommendation to use that rule for every dataset. Reproducing code does not establish that a research method is suitable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "summarize",
   "metadata": {},
   "outputs": [],
   "source": [
    "def summarize_records(records):\n",
    "    grouped = {}\n",
    "    for row in records:\n",
    "        group = grouped.setdefault(row[\"group\"], {\"rows\": 0, \"values\": []})\n",
    "        group[\"rows\"] += 1\n",
    "        value = row[\"minutes\"].strip()\n",
    "        if value != \"\":\n",
    "            group[\"values\"].append(float(value))\n",
    "    return {\n",
    "        name: {\n",
    "            \"rows\": group[\"rows\"],\n",
    "            \"recorded\": len(group[\"values\"]),\n",
    "            \"mean_minutes\": sum(group[\"values\"]) / len(group[\"values\"])\n",
    "                            if group[\"values\"] else None,\n",
    "        }\n",
    "        for name, group in sorted(grouped.items())\n",
    "    }\n",
    "\n",
    "results = summarize_records(rows)\n",
    "assert results[\"A\"] == {\"rows\": 3, \"recorded\": 2, \"mean_minutes\": 15.0}\n",
    "assert results[\"B\"] == {\"rows\": 3, \"recorded\": 3, \"mean_minutes\": 20.0}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "record-note",
   "metadata": {},
   "source": [
    "## Retain a small rerun record\n",
    "\n",
    "Running this final cell by itself in a fresh kernel is intentionally insufficient: it relies on names defined earlier. The printed record describes the kernel that actually runs it; it is not a prefilled Colab result. A matching checksum and expected result check this particular execution, not universal accuracy or learning gains.\n",
    "\n",
    "After a complete fresh run, keep the `.ipynb`, unchanged input and the record you actually obtained. If you later add third-party packages, also record and reconstruct their required versions. A historical runtime option is not an indefinite environment archive."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "record-rerun",
   "metadata": {},
   "outputs": [],
   "source": [
    "rerun_record = {\n",
    "    \"python_version\": platform.python_version(),\n",
    "    \"input_file\": DATA_PATH.name,\n",
    "    \"input_sha256\": input_sha256,\n",
    "    \"dependencies\": \"Python standard library only\",\n",
    "    \"missing_value_rule\": \"Blank excluded from recorded minutes; zero retained.\",\n",
    "    \"group_results\": results,\n",
    "}\n",
    "print(json.dumps(rerun_record, indent=2, sort_keys=True))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reflection",
   "metadata": {},
   "source": [
    "## Explain what was retained\n",
    "\n",
    "- Which names had to be recreated after a kernel restart?\n",
    "- Which input was not contained in the notebook file?\n",
    "- Could someone reproduce your result from a screenshot of output alone?\n",
    "- What did the assertions verify, and what do they leave unproven?\n",
    "- Before sharing, do the saved text, code, outputs or comments contain anything you did not intend to disclose?\n",
    "\n",
    "Official references: [Colab FAQ](https://research.google.com/colaboratory/faq.html), [past runtime versions](https://research.google.com/colaboratory/runtime-version-faq.html), [Jupyter notebook format](https://nbformat.readthedocs.io/en/latest/format_description.html). This is a proposed learning activity, not a classroom or product trial."
   ]
  }
 ],
 "metadata": {
  "ai_essay": {
   "distributed_outputs": "Empty; not a saved product execution",
   "input_sha256": "ed21e7f851cd080864d3ccefe759ae9adc2af595324fbe5ae29765dfe9e14f3c",
   "input_url": "https://ai-essay.com/assets/practice/julius-six-row-missing-values-ed21e7f851cd.csv",
   "purpose": "Original clean-rerun practice using six fictional records",
   "source_page": "https://ai-essay.com/tools/google-colab#clean-rerun-practice",
   "verification_scope": "Code checked locally in Python; no Colab session or AI feature test"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}