{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "workflow",
      "metadata": {},
      "source": "# Review, pause and resume an energy prediction\n\nUse Aner `0.1.2-dev.2` on a local macOS or Linux filesystem. Set the kernel working directory to the project containing `examples/fixtures/contextual/energy.csv`. A local Ollama service with `qwen2.5:14b-instruct` is needed for the proposal cell. Run the cells individually.\n"
    },
    {
      "cell_type": "markdown",
      "id": "fit-help",
      "metadata": {},
      "source": "## Prepare the fitted inputs\n\nRun this cell now and again after restarting the kernel. It fits the training buildings and reconstructs the validation view.\n"
    },
    {
      "cell_type": "code",
      "id": "fit",
      "metadata": {},
      "source": [
        "import aner.agent\n",
        "import aner.llm\n",
        "import aner.data\n",
        "import aner.ml\n",
        "import aner.tensor\n",
        "\n",
        "let schema = data.schema()\n",
        "    .column(\"row_id\", \"Int64\", false)\n",
        "    .column(\"building_id\", \"String\", false)\n",
        "    .column(\"temperature\", \"Float64\", false)\n",
        "    .column(\"occupancy\", \"Float64\", false)\n",
        "    .column(\"energy\", \"Float64\", false)\n",
        "let table = data.scan_csv(\"examples/fixtures/contextual/energy.csv\", schema, \"NA\").collect(16, 1048576, 4)\n",
        "let frame = data.frame(table, \"row_id\", \"building_id\", \"energy\", List(\"\", \"\", \"degC\", \"1\", \"kWh\"))\n",
        "let split = frame.partition_groups(List(\"B0\", \"B1\", \"B2\", \"B3\"), List(\"B4\", \"B5\"), List(\"B6\", \"B7\"))\n",
        "let scaler = ml.fit_standardizer(split.train())\n",
        "let fit = ml.fit_linear(scaler.transform(split.train()).batch(1048576))\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "start-help",
      "metadata": {},
      "source": "## Create the proposal once\n\nChoose a fresh run directory. To continue an existing run, skip this cell and open its journal below.\n"
    },
    {
      "cell_type": "code",
      "id": "propose",
      "metadata": {},
      "source": [
        "let local = llm.client(provider: \"ollama\", model: \"qwen2.5:14b-instruct\", endpoint: \"http://127.0.0.1:11434\")\n",
        "let proposal = llm.prediction_turn(local, \"Predict energy for validation observation 401.\")\n",
        "let run = agent.start_run(\"energy-401.run\", proposal, 401, fit, scaler, split.validation())\n",
        "print(proposal.model())\n",
        "print(proposal.text())\n",
        "print(run.row_id())\n",
        "print(run.state())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "decision-help",
      "metadata": {},
      "source": "## Review and choose one decision\n\nRead the bound row and journal. Uncomment one decision in the next cell. Approval permits a later prediction step. Denial and cancellation before dispatch finish the run with zero predictions.\n"
    },
    {
      "cell_type": "code",
      "id": "decide",
      "metadata": {},
      "source": [
        "print(run.replay())\n",
        "// print(run.approve())\n",
        "// print(run.deny())\n",
        "// print(run.cancel())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "pause-help",
      "metadata": {},
      "source": "With the run approved, execute the pause cell, then restart the notebook kernel.\n"
    },
    {
      "cell_type": "code",
      "id": "pause",
      "metadata": {},
      "source": [
        "print(run.pause())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "open-help",
      "metadata": {},
      "source": "## Return after a restart\n\nUse the same working directory and run path. You can inspect the journal even with Ollama stopped. Re run the fitted inputs cell before the scientific step.\n"
    },
    {
      "cell_type": "code",
      "id": "reopen",
      "metadata": {},
      "source": [
        "import aner.agent\n",
        "let saved = agent.open_run(\"energy-401.run\")\n",
        "print(saved.row_id())\n",
        "print(saved.state())\n",
        "print(saved.replay())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "resume-help",
      "metadata": {},
      "source": "Resume reopens the paused decision. To cancel here, substitute `saved.cancel()` for `saved.resume()`.\n"
    },
    {
      "cell_type": "code",
      "id": "resume",
      "metadata": {},
      "source": [
        "print(saved.resume())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "step-help",
      "metadata": {},
      "source": "## Run the authorized predictor\n\nUse the same fitted inputs and validation grant. A completed run can be reopened later to read its value, unit and history.\n"
    },
    {
      "cell_type": "code",
      "id": "dispatch",
      "metadata": {},
      "source": [
        "print(saved.step(fit, scaler, split.validation()))\n",
        "if saved.state() == \"completed\" {\n",
        "    print(saved.value())\n",
        "    print(saved.unit())\n",
        "}\n",
        "print(saved.replay())\n"
      ],
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "aner": {
      "execution_mode": "persistent-session",
      "runtime_mode": "production"
    },
    "kernelspec": {
      "display_name": "Aner",
      "language": "aner",
      "name": "aner"
    },
    "language_info": {
      "name": "aner",
      "file_extension": ".aner"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
