{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "decoder.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# decoder \u2014 Python demo\n\nNumerical companion to the entry [decoder](https://dictionaryofml.org/terms/decoder.html) of the [Dictionary of Applied Machine Learning](https://dictionaryofml.org/): it recomputes what the entry states and prints one line per check.\n\nNumerical companion to the glossary entry 'decoder'. The GeoSphere Austria weather station Krems an der Donau (station id 3805) records eight measurements per day: lowest, highest and mean air temperature, precipitation, sunshine duration, relative humidity, air pressure and wind speed. The 366 days of 2024 are downloaded from the data hub (dataset klima-v2-1d) and each measurement is scaled to zero sample mean and unit sample variance, because temperatures, precipitation and pressure carry different units.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/decoder.py`](https://dictionaryofml.org/terms/decoder.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 decoder.py` (B102,B307,B602,B605 are the exec, eval and shell checks of [bandit](https://bandit.readthedocs.io/)) reports nothing: the script runs no shell command and evaluates no generated code."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "# Notebook shim: the script resolves output paths relative to __file__,\n# which a notebook kernel does not define; everything lands in the\n# working directory instead.\nimport os\n__file__ = os.path.join(os.getcwd(), \"decoder.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"The decoder of a code: for the linear decoder of principal component\nanalysis the reconstruction is an orthogonal projection and the average\nsquared reconstruction error is the sum of the discarded eigenvalues.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'decoder'.  The GeoSphere\nAustria weather station Krems an der Donau (station id 3805) records\neight measurements per day: lowest, highest and mean air temperature,\nprecipitation, sunshine duration, relative humidity, air pressure and\nwind speed.  The 366 days of 2024 are downloaded from the data hub\n(dataset klima-v2-1d) and each measurement is scaled to zero sample mean\nand unit sample variance, because temperatures, precipitation and\npressure carry different units.\n\nThe script recomputes what the entry claims about a decoder.  (1) For a\nlinear encoder with orthonormal rows, the decoder that leaves the\nsmallest average squared reconstruction error is the transpose of the\nencoder.  (2) The reconstruction map is then an orthogonal projection:\nidempotent, symmetric, the reconstruction lies in the span of the\nretained eigenvectors and the error it leaves is orthogonal to that\nspan.  (3) The average squared reconstruction error equals the sum of\nthe discarded eigenvalues, for every code length.  (4) A probabilistic\ndecoder that returns a Gaussian conditional distribution with a fixed\nspread has the same best linear map, because its log-likelihood is a\ndecreasing affine function of the same reconstruction error.  (5) A\ndecoder onto a distribution over a vocabulary: a linear map followed by\nthe softmax function sends a context vector to a point of the\nprobability simplex, the parameters that maximize the log-likelihood\nreproduce the observed next-token frequencies, and a sequence is\ngenerated one token at a time.  (6) What the flat reconstruction set of a\nlinear decoder costs: read back in the recorded units it holds values no\nweather record takes, a decoder free to curve does not beat the line on\nthis record, and the eight-token vocabulary resolves the maximum daytime\ntemperature of a day only to its bin.\n\nDeterministic: a fixed archive year, an eigenvalue decomposition, and\nfixed seeds for the random comparison maps and the generated sequence.\nSelf-contained: numpy and matplotlib only (stdlib urllib for the\ndownload, which falls back to the committed CSV).\n\nBlocks\n------\n[B-data]        The 366 days with eight measurements each; the two\n                temperatures centered for the picture, all eight scaled\n                for the identities.\n[B-transpose]   For a decoder with orthonormal columns the best encoder\n                is its transpose, and for the PCA encoder the best\n                decoder is its transpose; a generic encoder does not\n                have this property.\n[B-projection]  The reconstruction map of the PCA pair is an orthogonal\n                projection onto the span of the retained eigenvectors.\n[B-eigenvalues] The average squared reconstruction error equals the sum\n                of the discarded eigenvalues, for every code length.\n[B-gauss]       A Gaussian probabilistic decoder: its log-likelihood is\n                a decreasing affine function of the reconstruction\n                error, so both are minimized by the same decoder.\n[B-vocabulary]  A decoder onto a distribution over eight temperature\n                tokens: a linear map and the softmax function land in\n                the probability simplex, the maximum-likelihood\n                parameters reproduce the observed next-token\n                frequencies, and a sequence is generated one token at a\n                time.\n[B-limits]      What the flat reconstruction set costs: in the recorded\n                units it holds values no day of the record takes, a\n                decoder free to curve does not beat the line on the days\n                it was not learned from, and the eight tokens resolve a\n                day only to its bin.\n[B-plot]        The CSVs the figure reads and the matplotlib preview: the\n                days with their reconstructions from a code of one\n                number, and the error against the code length.\n\nOutputs\n-------\ndecoder_weather.csv      : date and the eight measurements, 366 days\ndecoder_points_plot.csv  : the 20 days (every 19th) the figure draws,\n                           with the reconstruction of each from a code\n                           of one number\ndecoder_direction.csv    : the retained direction as a segment\ndecoder_errord.csv       : code length, average squared reconstruction\n                           error, sum of the discarded eigenvalues\ndecoder.png              : matplotlib preview (checking only)\n\"\"\"\n\n# /// script\n# requires-python = \">=3.10\"\n# dependencies = [\"matplotlib==3.10.7\", \"numpy==2.3.4\"]\n# ///\n\nimport json\nimport urllib.request\nfrom pathlib import Path\n\nimport numpy as np\nimport matplotlib\n\nmatplotlib.use(\"Agg\")\nimport matplotlib.pyplot as plt\n\nOUT_DIR = Path(__file__).parent\n\nreport = []\n\n\ndef check(name, ok):\n    report.append((name, bool(ok)))\n    print(f\"  [{'ok' if ok else 'FAIL'}] {name}\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-data]** The 366 days with eight measurements each; the two temperatures centered for the picture, all eight scaled for the identities."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-data] eight measurements for each of the 366 days of 2024\")\nPARAMS = [\"tlmin\", \"tlmax\", \"tl_mittel\", \"rr\", \"so_h\", \"rf_mittel\",\n          \"p_mittel\", \"vv_mittel\"]\nURL = (\"https://dataset.api.hub.geosphere.at/v1/station/historical/\"\n       f\"klima-v2-1d?parameters={','.join(PARAMS)}&station_ids=3805\"\n       \"&start=2024-01-01&end=2024-12-31\")\nCSV = OUT_DIR / \"decoder_weather.csv\"\ntry:\n    with urllib.request.urlopen(URL, timeout=120) as resp:\n        payload = json.load(resp)\n    params = payload[\"features\"][0][\"properties\"][\"parameters\"]\n    stamps = [t[:10] for t in payload[\"timestamps\"]]\n    M = np.stack([np.array(params[p][\"data\"], dtype=float) for p in PARAMS], 1)\n    M[:, 3] = np.maximum(M[:, 3], 0.0)          # -1 marks a trace of rain\n    with open(CSV, \"w\") as fh:\n        fh.write(\"date,\" + \",\".join(PARAMS) + \"\\n\")\n        for day, row in zip(stamps, M):\n            fh.write(day + \",\" + \",\".join(f\"{v:g}\" for v in row) + \"\\n\")\n    print(f\"    downloaded {len(stamps)} days\")\nexcept OSError as err:\n    print(f\"    download failed ({err}); reading the committed CSV\")\n    rows = CSV.read_text().strip().splitlines()[1:]\n    stamps = [r.split(\",\")[0] for r in rows]\n    M = np.array([[float(v) for v in r.split(\",\")[1:]] for r in rows])\n\nT = M[:, :2] - M[:, :2].mean(axis=0)            # centered, in degrees\nX = (M - M.mean(axis=0)) / M.std(axis=0)        # scaled, all eight\nm, d = X.shape\ncheck(\"[B-data] 366 days with eight measurements each\", (m, d) == (366, 8))\ncheck(\"[B-data] the record matches the archive (Feb 1: -3.8 to 10.4 deg)\",\n      stamps[31] == \"2024-02-01\" and np.allclose(M[31, :2], [-3.8, 10.4]))\ncheck(\"[B-data] every measurement has zero sample mean and unit sample \"\n      \"variance after scaling\",\n      np.allclose(X.mean(axis=0), 0) and np.allclose(X.std(axis=0), 1))\n\n\ndef principal(A):\n    \"\"\"Eigenvalues and eigenvectors of the sample covariance matrix of the\n    centered rows of A, in decreasing order of eigenvalue.\"\"\"\n    Q = A.T @ A / len(A)\n    vals, vecs = np.linalg.eigh(Q)\n    order = np.argsort(vals)[::-1]\n    return vals[order], vecs[:, order]\n\n\ndef avg_sq_error(R, W, A):\n    \"\"\"Average over the rows a of A of || a - R W a ||^2.\"\"\"\n    return float((((A - A @ W.T @ R.T) ** 2).sum(axis=1)).mean())\n\n\ndef best_decoder(W, A):\n    \"\"\"The decoder R that minimizes avg_sq_error(R, W, A), by least squares.\"\"\"\n    Z = A @ W.T\n    return np.linalg.lstsq(Z, A, rcond=None)[0].T\n\n\nlam, U = principal(X)\nDPRIME = 2\nW = U[:, :DPRIME].T                             # PCA encoder, DPRIME x d"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-transpose]** For a decoder with orthonormal columns the best encoder is its transpose, and for the PCA encoder the best decoder is its transpose; a generic encoder does not have this property."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-transpose] the best decoder for the PCA encoder is its transpose\")\nR_hat = best_decoder(W, X)\ncheck(f\"[B-transpose] the rows of the PCA encoder are orthonormal \"\n      f\"({DPRIME} retained eigenvectors)\", np.allclose(W @ W.T, np.eye(DPRIME)))\ncheck(\"[B-transpose] the least-squares decoder for that encoder is its \"\n      \"transpose\", np.allclose(R_hat, W.T, atol=1e-10))\nrng = np.random.default_rng(0)\ngeneric = rng.standard_normal((DPRIME, d))\ngap = np.abs(best_decoder(generic, X) - generic.T).max()\ncheck(f\"[B-transpose] a generic encoder has no such property: its \"\n      f\"least-squares decoder differs from its transpose by up to \"\n      f\"{gap:.2f} entrywise\", gap > 0.1)\nerr_tp = avg_sq_error(W.T, W, X)\nworse = [avg_sq_error(W.T + 0.05 * rng.standard_normal((d, DPRIME)), W, X)\n         for _ in range(200)]\ncheck(f\"[B-transpose] the transpose leaves {err_tp:.3f}; no perturbed \"\n      f\"decoder does better (best of 200: {min(worse):.3f})\",\n      err_tp <= min(worse))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-projection]** The reconstruction map of the PCA pair is an orthogonal projection onto the span of the retained eigenvectors."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-projection] the reconstruction is an orthogonal projection\")\nP = W.T @ W\ncheck(\"[B-projection] the reconstruction map is idempotent and symmetric\",\n      np.allclose(P @ P, P) and np.allclose(P, P.T))\nrecon = X @ P.T\nresid = X - recon\ncheck(\"[B-projection] the reconstruction of every day lies in the span of \"\n      \"the retained eigenvectors\",\n      np.allclose(recon @ U[:, DPRIME:], 0, atol=1e-10))\ncheck(\"[B-projection] the error the decoder leaves is orthogonal to that \"\n      \"span\", np.allclose(resid @ U[:, :DPRIME], 0, atol=1e-10))\npyth = np.allclose((X ** 2).sum(axis=1),\n                   (recon ** 2).sum(axis=1) + (resid ** 2).sum(axis=1))\ncheck(\"[B-projection] squared lengths add: reconstruction plus error\",\n      pyth)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-eigenvalues]** The average squared reconstruction error equals the sum of the discarded eigenvalues, for every code length."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-eigenvalues] the error is the sum of the discarded eigenvalues\")\nerrs, drops = [], []\nfor dp in range(1, d + 1):\n    Wd = U[:, :dp].T\n    errs.append(avg_sq_error(Wd.T, Wd, X))\n    drops.append(float(lam[dp:].sum()))\nerrs, drops = np.array(errs), np.array(drops)\ncheck(\"[B-eigenvalues] for every code length the average squared \"\n      \"reconstruction error equals the sum of the discarded eigenvalues\",\n      np.allclose(errs, drops))\nprint(\"    code length : error : sum of discarded eigenvalues\")\nfor dp in range(1, d + 1):\n    print(f\"    {dp:11d} : {errs[dp - 1]:.4f} : {drops[dp - 1]:.4f}\")\ncheck(f\"[B-eigenvalues] a code of two numbers leaves {errs[1]:.2f} of the \"\n      f\"total {d:d}, so the two retained eigenvalues carry \"\n      f\"{100 * (1 - errs[1] / d):.0f} percent of the total sample variance\",\n      abs(errs[1] - drops[1]) < 1e-9)\ncheck(f\"[B-eigenvalues] the error falls with the code length and vanishes \"\n      f\"at {d:d}, where nothing is compressed\",\n      np.all(np.diff(errs) < 0) and abs(errs[-1]) < 1e-9)\nlam2, U2 = principal(T)\nerr1_T = avg_sq_error(U2[:, :1], U2[:, :1].T, T)\ncheck(f\"[B-eigenvalues] on the two temperatures in degrees, a code of one \"\n      f\"number leaves {err1_T:.2f} squared degrees, the discarded \"\n      f\"eigenvalue {lam2[1]:.2f}\", np.isclose(err1_T, lam2[1]))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-gauss]** A Gaussian probabilistic decoder: its log-likelihood is a decreasing affine function of the reconstruction error, so both are minimized by the same decoder."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-gauss] a Gaussian probabilistic decoder has the same best map\")\nSIGMA = 0.5\nZ = X @ W.T\n\n\ndef loglik(R):\n    \"\"\"Average log-density of the days under the Gaussian decoder\n    p(x | z) with mean R z and covariance SIGMA^2 times the identity.\"\"\"\n    sq = (((X - Z @ R.T) ** 2).sum(axis=1)).mean()\n    return -0.5 * d * np.log(2 * np.pi * SIGMA ** 2) - sq / (2 * SIGMA ** 2)\n\n\ncands = [W.T] + [W.T + 0.1 * rng.standard_normal((d, DPRIME))\n                 for _ in range(300)]\nlls = np.array([loglik(R) for R in cands])\nsqs = np.array([avg_sq_error(R, W, X) for R in cands])\ncheck(\"[B-gauss] the log-likelihood is a decreasing affine function of the \"\n      \"reconstruction error\",\n      np.allclose(lls, lls[0] + (sqs[0] - sqs) / (2 * SIGMA ** 2)))\ncheck(f\"[B-gauss] the transpose maximizes the log-likelihood \"\n      f\"({lls[0]:.3f} against a best of {lls[1:].max():.3f} among 300 \"\n      f\"perturbations)\", lls[0] >= lls.max() - 1e-12)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-vocabulary]** A decoder onto a distribution over eight temperature tokens: a linear map and the softmax function land in the probability simplex, the maximum-likelihood parameters reproduce the observed next-token frequencies, and a sequence is generated one token at a time."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-vocabulary] a decoder onto a distribution over eight tokens\")\nVSIZE = 8\nedges = np.quantile(M[:, 1], np.arange(1, VSIZE) / VSIZE)\ntokens = np.searchsorted(edges, M[:, 1])        # one token per day\ncheck(f\"[B-vocabulary] the maximum daytime temperature of each day is cut \"\n      f\"into {VSIZE} tokens of about {m // VSIZE} days each\",\n      tokens.min() == 0 and tokens.max() == VSIZE - 1)\ncounts = np.zeros((VSIZE, VSIZE))\nfor a, b in zip(tokens[:-1], tokens[1:]):\n    counts[a, b] += 1\ncontext = np.eye(VSIZE)                         # context vector of a token\n\n\ndef softmax(S):\n    E = np.exp(S - S.max(axis=1, keepdims=True))\n    return E / E.sum(axis=1, keepdims=True)\n\n\n# the decoder: context vector -> linear map -> softmax -> distribution\nTheta = np.zeros((VSIZE, VSIZE))\nlrate, steps = 1.0, 4000\nfor _ in range(steps):\n    Pr = softmax(context @ Theta.T)\n    Theta += lrate * (counts - counts.sum(axis=1, keepdims=True) * Pr).T / m\nPr = softmax(context @ Theta.T)\ncheck(\"[B-vocabulary] every output of the decoder is a point of the \"\n      \"probability simplex: nonnegative entries summing to one\",\n      np.all(Pr >= 0) and np.allclose(Pr.sum(axis=1), 1))\nfreq = counts / counts.sum(axis=1, keepdims=True)\ncheck(f\"[B-vocabulary] the maximum-likelihood parameters reproduce the \"\n      f\"observed next-token frequencies (largest deviation \"\n      f\"{np.abs(Pr - freq).max():.4f})\", np.abs(Pr - freq).max() < 0.01)\ngen_rng = np.random.default_rng(2024)\nseq = [int(tokens[0])]\nfor _ in range(20000):                          # one token at a time\n    p = Pr[seq[-1]]\n    seq.append(int(gen_rng.choice(VSIZE, p=p)))\nseq = np.array(seq)\nshare_gen = np.array([(seq == v).mean() for v in range(VSIZE)])\nshare_obs = np.array([(tokens == v).mean() for v in range(VSIZE)])\nprint(\"    generated share : observed share, per token\")\nfor v in range(VSIZE):\n    print(f\"    {share_gen[v]:.3f} : {share_obs[v]:.3f}\")\ncheck(f\"[B-vocabulary] a sequence of {len(seq)} tokens generated one token \"\n      f\"at a time matches the observed token shares to within \"\n      f\"{np.abs(share_gen - share_obs).max():.3f}\",\n      np.abs(share_gen - share_obs).max() < 0.03)\ngreedy = int(np.argmax(Pr[0]))\ncheck(f\"[B-vocabulary] after the coldest token the decoder puts the most \"\n      f\"probability, {Pr[0, greedy]:.2f}, on token {greedy}\", greedy <= 1)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-limits]** What the flat reconstruction set costs: in the recorded units it holds values no day of the record takes, a decoder free to curve does not beat the line on the days it was not learned from, and the eight tokens resolve a day only to its bin."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-limits] the reconstructions of a linear decoder lie in a subspace\")\nunits = X @ P.T * M.std(axis=0) + M.mean(axis=0)   # back in the recorded units\nrain, sun, hum = units[:, 3], units[:, 4], units[:, 5]\ncheck(\"[B-limits] no day of the record has a negative precipitation, a \"\n      \"negative sunshine duration or a humidity above 100 percent\",\n      M[:, 3].min() >= 0 and M[:, 4].min() >= 0 and M[:, 5].max() <= 100)\ncheck(f\"[B-limits] a code of {DPRIME} numbers gives {(rain < 0).sum():d} of \"\n      f\"the {m:d} days a negative precipitation, down to {rain.min():.1f} \"\n      f\"millimeters\", (rain < 0).sum() > 0)\ncheck(f\"[B-limits] it gives {(sun < 0).sum():d} days a negative sunshine \"\n      f\"duration, down to {sun.min():.1f} hours, and {(hum > 100).sum():d} \"\n      f\"days a humidity above 100 percent, up to {hum.max():.0f}\",\n      (sun < 0).sum() > 0 and (hum > 100).sum() > 0)\neven = np.arange(m) % 2 == 0\nz1 = X @ U[:, 0]\nNBINS = 8\ncuts = np.quantile(z1[even], np.arange(1, NBINS) / NBINS)\nb_in, b_out = np.searchsorted(cuts, z1[even]), np.searchsorted(cuts, z1[~even])\ncurve = np.array([X[even][b_in == b].mean(axis=0) for b in range(NBINS)])\nerr_curve = float((((X[~even] - curve[b_out]) ** 2).sum(axis=1)).mean())\nerr_line = float((((X[~even] - np.outer(z1[~even], U[:, 0])) ** 2).sum(axis=1))\n                 .mean())\ncheck(f\"[B-limits] with the same code of one number, the average of the days \"\n      f\"in each of {NBINS} bins of the code leaves {err_curve:.2f} on the days \"\n      f\"it was not learned from, against the linear decoder's {err_line:.2f}\",\n      err_curve > err_line)\nmid = np.array([M[tokens == v, 1].mean() for v in range(VSIZE)])\nrmse_tok = float(np.sqrt(((M[:, 1] - mid[tokens]) ** 2).mean()))\nwidths = [float(np.ptp(M[tokens == v, 1])) for v in range(VSIZE)]\ncheck(f\"[B-limits] the {VSIZE} tokens resolve the maximum daytime temperature \"\n      f\"of a day only to its bin: {rmse_tok:.1f} degrees in root mean square, \"\n      f\"and {max(widths):.1f} degrees for the coldest token, which holds every \"\n      f\"day at or below {edges[0]:.1f} degrees\",\n      max(widths) == widths[0] and rmse_tok > 1)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** The CSVs the figure reads and the matplotlib preview: the days with their reconstructions from a code of one number, and the error against the code length."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] CSVs for the figure and the matplotlib preview\")\nSTEP = 19\nsel = np.arange(0, m, STEP)[:20]\nu1 = U2[:, 0] * np.sign(U2[0, 0])\nrec_T = np.outer(T @ u1, u1)\nwith open(OUT_DIR / \"decoder_points_plot.csv\", \"w\") as fh:\n    fh.write(\"x1,x2,r1,r2\\n\")\n    for i in sel:\n        fh.write(f\"{T[i, 0]:.4f},{T[i, 1]:.4f},\"\n                 f\"{rec_T[i, 0]:.4f},{rec_T[i, 1]:.4f}\\n\")\nspan = 1.15 * np.abs(T @ u1).max()\nwith open(OUT_DIR / \"decoder_direction.csv\", \"w\") as fh:\n    fh.write(\"x1,x2\\n\")\n    for s in (-span, span):\n        fh.write(f\"{s * u1[0]:.4f},{s * u1[1]:.4f}\\n\")\nwith open(OUT_DIR / \"decoder_errord.csv\", \"w\") as fh:\n    fh.write(\"dprime,err,dropped\\n\")\n    for dp in range(1, d + 1):\n        fh.write(f\"{dp},{errs[dp - 1]:.4f},{drops[dp - 1]:.4f}\\n\")\ncheck(f\"[B-plot] the figure draws {len(sel)} of the {m} days, every \"\n      f\"{STEP}th\", len(sel) == 20)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9.6, 4.0))\nax1.plot([-span * u1[0], span * u1[0]], [-span * u1[1], span * u1[1]],\n         color=\"black\", ls=\"-\", lw=1.0, label=\"retained direction (solid line)\")\nfor i in sel:\n    ax1.plot([T[i, 0], rec_T[i, 0]], [T[i, 1], rec_T[i, 1]],\n             color=\"gray\", ls=\":\", lw=0.9)\nax1.plot(T[sel, 0], T[sel, 1], ls=\"none\", marker=\"o\", mfc=\"none\",\n         color=\"black\", ms=5, label=\"day (open circles)\")\nax1.plot(rec_T[sel, 0], rec_T[sel, 1], ls=\"none\", marker=\"s\", color=\"black\",\n         ms=4, label=\"reconstruction (filled squares)\")\nax1.set_xlabel(\"lowest temperature of the day in degrees, centered\")\nax1.set_ylabel(\"highest temperature in degrees, centered\")\nax1.set_title(\"a code of one number sent back to the plane\", fontsize=10)\nax1.legend(frameon=False, fontsize=8)\nax2.plot(np.arange(1, d + 1), errs, color=\"black\", ls=\"-\", marker=\"o\", ms=4,\n         label=\"average squared reconstruction error (solid curve)\")\nax2.plot(np.arange(1, d + 1), drops, color=\"black\", ls=\"none\", marker=\"x\",\n         ms=9, label=\"sum of the discarded eigenvalues (crosses)\")\nax2.set_xlabel(\"code length\")\nax2.set_ylabel(\"error on the eight scaled measurements\")\nax2.set_title(\"the error left is the discarded eigenvalues\", fontsize=10)\nax2.legend(frameon=False, fontsize=8)\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"decoder.png\", dpi=110)\n\nn_ok = sum(ok for _, ok in report)\nprint(f\"\\n{n_ok}/{len(report)} checks pass\")\nif n_ok != len(report):\n    raise SystemExit(1)"
  }
 ]
}