{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "specclustering.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# spectral clustering \u2014 Python demo\n\nNumerical companion to the entry [spectral clustering](https://dictionaryofml.org/terms/specclustering.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 'specclustering'. The 366 days of 2024 at Krems are data points with the feature vector (morning minimum temperature, maximum daytime temperature). Each day is a node of a graph and is connected to its ten nearest days in Euclidean distance; the edges have unit weight. Spectral clustering reads the clusters off the eigenvectors of the Laplacian matrix of this graph.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/specclustering.py`](https://dictionaryofml.org/terms/specclustering.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 specclustering.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(), \"specclustering.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"Spectral clustering of the days of one year at Krems an der Donau:\nthe eigenvectors of the Laplacian matrix of their nearest-neighbor graph.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'specclustering'.  The 366\ndays of 2024 at Krems are data points with the feature vector (morning\nminimum temperature, maximum daytime temperature).  Each day is a node\nof a graph and is connected to its ten nearest days in Euclidean\ndistance; the edges have unit weight.  Spectral clustering reads the\nclusters off the eigenvectors of the Laplacian matrix of this graph.\n\nThe demo checks the entry's claims: the Laplacian matrix is symmetric\nand positive semi-definite, its quadratic form sums the squared\ndifferences over the edges, its rows sum to zero, and the multiplicity\nof its eigenvalue 0 is the number of connected components; the second\neigenvector (the Fiedler vector) of the connected graph has entries of\nboth signs, and k-means on the first k eigenvector entries delivers the\nclusters; the ratio cut of every bipartition is at least the second\neigenvalue, the value of the relaxed problem that the Fiedler vector\nsolves; and on these data, whose clusters are convex, spectral\nclustering and k-means on the raw feature vectors agree on most days.\n\nDeterministic: k-means starts from fixed data points, the eigenvectors\ncome from numpy.linalg.eigh with their signs fixed.\nSelf-contained: numpy + matplotlib only; the GeoSphere download falls\nback to the committed CSV.\n\nBlocks\n------\n[B-data]      Download the daily minimum and maximum temperature of 2024\n              at Krems (GeoSphere station 3805) or read the committed\n              copy; 366 days, no missing value.\n[B-graph]     The 10-nearest-neighbor graph of the days with unit edge\n              weights, its Laplacian matrix L = D - A: symmetric, rows\n              summing to zero, quadratic form equal to the sum of squared\n              differences over the edges, hence positive semi-definite.\n[B-spectrum]  Eigenvalues 0 = lambda_1 < lambda_2 <= ...: the graph is\n              connected, so 0 is a simple eigenvalue with the constant\n              eigenvector; the Fiedler vector has entries of both signs.\n[B-cluster]   Spectral clustering with k = 2: k-means on the rows of the\n              first two eigenvectors, which equals thresholding the\n              Fiedler vector; the clusters are the cold and the warm\n              days; agreement with k-means on the raw feature vectors.\n[B-ratiocut]  The ratio cut of the spectral bipartition, of the k-means\n              bipartition and of a random one, all at least lambda_2,\n              the value of the relaxed problem.\n[B-components] Deleting the edges between the two spectral clusters\n              leaves two connected components: eigenvalue 0 then has\n              multiplicity 2 and its eigenvectors are spanned by the two\n              indicator vectors.\n[B-three]     Spectral clustering with k = 3 from the first three\n              eigenvectors: cluster sizes and connectivity in the graph.\n[B-plot]      The graph in the feature plane, every 18th day for the\n              Fiedler-vector panel (21 of 366 days), and the preview.\n\nOutputs\n-------\nspecclustering_weather.csv : date, tmin, tmax (committed copy).\nspecclustering_nodes.csv   : every day with its features, day of the\n                             year, spectral cluster, and Fiedler entry.\nspecclustering_edges.csv   : the edges of the graph as line segments,\n                             separated by nan rows.\nspecclustering_fiedler.csv : Fiedler entry against day of the year.\nspecclustering_plot.csv    : the 21 drawn days with their cluster.\nspecclustering.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]** Download the daily minimum and maximum temperature of 2024 at Krems (GeoSphere station 3805) or read the committed copy; 366 days, no missing value."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-data] daily minimum and maximum temperature at Krems, 2024\")\nURL = (\"https://dataset.api.hub.geosphere.at/v1/station/historical/\"\n       \"klima-v2-1d?parameters=tlmin,tlmax&station_ids=3805\"\n       \"&start=2024-01-01&end=2024-12-31\")\nCSV = OUT_DIR / \"specclustering_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    records = [(stamp[:10], lo, hi)\n               for stamp, lo, hi in zip(payload[\"timestamps\"],\n                                        params[\"tlmin\"][\"data\"],\n                                        params[\"tlmax\"][\"data\"])]\n    with open(CSV, \"w\") as fh:\n        fh.write(\"date,tmin,tmax\\n\")\n        for day, lo, hi in records:\n            fh.write(f\"{day},{lo},{hi}\\n\")\n    print(f\"    downloaded {len(records)} days\")\nexcept OSError as err:\n    print(f\"    download failed ({err}); reading the committed CSV\")\n    rows = CSV.read_text().strip().splitlines()[1:]\n    records = [(d, float(a), float(b))\n               for d, a, b in (r.split(\",\") for r in rows)]\n\ncheck(\"[B-data] 366 days for 2024\", len(records) == 366)\ncheck(\"[B-data] no day is missing a temperature\",\n      all(lo is not None and hi is not None for _, lo, hi in records))\nX = np.array([[lo, hi] for _, lo, hi in records], dtype=float)\ndates = [d for d, _, _ in records]\nmonths = np.array([int(d[5:7]) for d in dates])\nn = len(X)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-graph]** The 10-nearest-neighbor graph of the days with unit edge weights, its Laplacian matrix L = D - A: symmetric, rows summing to zero, quadratic form equal to the sum of squared differences over the edges, hence positive semi-definite."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-graph] 10-nearest-neighbor graph of the days, Laplacian L = D - A\")\nNEIGHBORS = 10\ndist = np.sqrt(((X[:, None, :] - X[None, :, :]) ** 2).sum(axis=2))\nnp.fill_diagonal(dist, np.inf)\nnearest = np.argsort(dist, axis=1)[:, :NEIGHBORS]\nA = np.zeros((n, n))\nA[np.repeat(np.arange(n), NEIGHBORS), nearest.ravel()] = 1.0\nA = np.maximum(A, A.T)                       # edge if either is a neighbor\nedges = np.array([(i, j) for i in range(n) for j in range(i + 1, n)\n                  if A[i, j] > 0])\ndeg = A.sum(axis=1)\nL = np.diag(deg) - A\nn_edges = len(edges)\ncheck(f\"[B-graph] {n} nodes, {n_edges} edges of unit weight, node degrees \"\n      f\"between {int(deg.min())} and {int(deg.max())}\",\n      n_edges == int(A.sum() // 2) and deg.min() >= NEIGHBORS)\ncheck(\"[B-graph] L is symmetric\", np.array_equal(L, L.T))\ncheck(\"[B-graph] every row of L sums to zero: L 1 = 0\",\n      np.allclose(L @ np.ones(n), 0.0))\nrng = np.random.default_rng(3805)\nu = rng.standard_normal(n)\nquad = u @ L @ u\nover_edges = ((u[edges[:, 0]] - u[edges[:, 1]]) ** 2).sum()\ncheck(\"[B-graph] u^T L u equals the sum over the edges of (u_i - u_i')^2 \"\n      \"for a random u\", np.isclose(quad, over_edges))\nlam, V = np.linalg.eigh(L)\ncheck(\"[B-graph] L is positive semi-definite: all eigenvalues are \"\n      \"nonnegative\", lam.min() > -1e-9)\n\n\ndef components(adj, mask=None):\n    \"\"\"Number of connected components of the graph restricted to mask.\"\"\"\n    keep = np.ones(len(adj), bool) if mask is None else mask\n    seen = ~keep\n    count = 0\n    for start in np.flatnonzero(keep):\n        if seen[start]:\n            continue\n        count += 1\n        stack = [start]\n        seen[start] = True\n        while stack:\n            v = stack.pop()\n            for w in np.flatnonzero(adj[v] > 0):\n                if not seen[w]:\n                    seen[w] = True\n                    stack.append(w)\n    return count"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-spectrum]** Eigenvalues 0 = lambda_1 < lambda_2 <= ...: the graph is connected, so 0 is a simple eigenvalue with the constant eigenvector; the Fiedler vector has entries of both signs."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-spectrum] eigenvalues of L and the Fiedler vector\")\ncheck(f\"[B-spectrum] lambda_1 = {lam[0]:.1e} is zero and lambda_2 = \"\n      f\"{lam[1]:.3f} is positive: the graph is connected\",\n      abs(lam[0]) < 1e-9 and lam[1] > 1e-6 and components(A) == 1)\ncheck(\"[B-spectrum] the eigenvector of lambda_1 is constant\",\n      np.allclose(V[:, 0], V[0, 0]))\nfiedler = V[:, 1].copy()\ncold_ref = int(np.argmin(X[:, 1]))             # the day with the lowest tmax\nif fiedler[cold_ref] > 0:                      # the sign of an eigenvector\n    fiedler = -fiedler                         # is arbitrary; cold days < 0\nV[:, 1] = fiedler\ncheck(f\"[B-spectrum] the Fiedler vector is orthogonal to 1 and has \"\n      f\"{int((fiedler < 0).sum())} negative and {int((fiedler > 0).sum())} \"\n      f\"positive entries\",\n      abs(fiedler.sum()) < 1e-9 and (fiedler < 0).any() and (fiedler > 0).any())\ncheck(f\"[B-spectrum] lambda_2 = {lam[1]:.3f} lies well below lambda_3 = \"\n      f\"{lam[2]:.3f}\", lam[1] < 0.75 * lam[2])\n\n\ndef lloyd(Y, centroids):\n    \"\"\"Lloyd's algorithm from given centroids; returns labels, centroids,\n    and the number of iterations until the assignment stops changing.\"\"\"\n    labels = None\n    for it in range(1, 1000):\n        d2 = ((Y[:, None, :] - centroids[None, :, :]) ** 2).sum(axis=2)\n        new = np.argmin(d2, axis=1)\n        if labels is not None and np.array_equal(new, labels):\n            return labels, centroids, it\n        labels = new\n        centroids = np.vstack([Y[labels == c].mean(axis=0)\n                               for c in range(len(centroids))])\n    return labels, centroids, it\n\n\ndef farthest_first(Y, k, start):\n    \"\"\"Deterministic start: a given point, then always the farthest one.\"\"\"\n    chosen = [start]\n    for _ in range(k - 1):\n        d2 = ((Y[:, None, :] - Y[chosen]) ** 2).sum(axis=2).min(axis=1)\n        chosen.append(int(d2.argmax()))\n    return Y[chosen]\n\n\ndef ratio_cut(labels, adj):\n    \"\"\"Sum over the clusters of cut(C, V \\\\ C) / |C|.\"\"\"\n    total = 0.0\n    for c in np.unique(labels):\n        inside = labels == c\n        total += adj[inside][:, ~inside].sum() / inside.sum()\n    return total\n\n\ndef cut_size(labels, adj):\n    \"\"\"Total weight of the edges with endpoints in different clusters.\"\"\"\n    return sum(adj[labels == c][:, labels != c].sum()\n               for c in np.unique(labels)) / 2"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-cluster]** Spectral clustering with k = 2: k-means on the rows of the first two eigenvectors, which equals thresholding the Fiedler vector; the clusters are the cold and the warm days; agreement with k-means on the raw feature vectors."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-cluster] spectral clustering with k = 2\")\nK = 2\nY2 = V[:, :K]                                  # new feature vector per node\nstart = farthest_first(Y2, K, int(np.argmin(fiedler)))\nlab_sp, cent_sp, it_sp = lloyd(Y2, start)\nif lab_sp[cold_ref] != 0:\n    lab_sp = 1 - lab_sp\nsign_part = (fiedler > 0).astype(int)\ncheck(f\"[B-cluster] k-means on the rows of (v1, v2) stops after {it_sp} \"\n      f\"iterations and agrees with the sign of the Fiedler vector on \"\n      f\"{100 * np.mean(lab_sp == sign_part):.1f}% of the days\",\n      np.mean(lab_sp == sign_part) > 0.97)\nn_cold, n_warm = int((lab_sp == 0).sum()), int((lab_sp == 1).sum())\ncold_months = sorted(set(months[lab_sp == 0]))\nwarm_months = sorted(set(months[lab_sp == 1]))\nall_cold = [mth for mth in range(1, 13) if np.all(lab_sp[months == mth] == 0)]\nall_warm = [mth for mth in range(1, 13) if np.all(lab_sp[months == mth] == 1)]\nmean_cold, mean_warm = X[lab_sp == 0].mean(axis=0), X[lab_sp == 1].mean(axis=0)\ncheck(f\"[B-cluster] {n_cold} cold days with mean features \"\n      f\"({mean_cold[0]:.1f}, {mean_cold[1]:.1f}) and {n_warm} warm days with \"\n      f\"({mean_warm[0]:.1f}, {mean_warm[1]:.1f}); months entirely cold: \"\n      f\"{all_cold}, entirely warm: {all_warm}\",\n      mean_cold[1] < mean_warm[1] and 1 in all_cold and 7 in all_warm)\ncheck(\"[B-cluster] each spectral cluster is connected in the graph\",\n      components(A, lab_sp == 0) == 1 and components(A, lab_sp == 1) == 1)\n# k-means on the raw feature vectors, started from the colder and the\n# warmer half of the days as in the clusteringerror demo\norder = np.argsort(X[:, 1])\nstart_raw = np.vstack([X[order[:n // 2]].mean(axis=0),\n                       X[order[n // 2:]].mean(axis=0)])\nlab_km, cent_km, it_km = lloyd(X, start_raw)\nif cent_km[0, 1] > cent_km[1, 1]:\n    lab_km = 1 - lab_km\nagree = np.mean(lab_km == lab_sp)\ncheck(f\"[B-cluster] k-means on the raw feature vectors ({it_km} iterations) \"\n      f\"assigns {100 * agree:.1f}% of the days like spectral clustering; \"\n      f\"{int((lab_km != lab_sp).sum())} days differ\", agree > 0.9)\ndiffer = np.flatnonzero(lab_km != lab_sp)\nprint(f\"    days assigned differently: {[dates[i] for i in differ]}\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-ratiocut]** The ratio cut of the spectral bipartition, of the k-means bipartition and of a random one, all at least lambda_2, the value of the relaxed problem."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-ratiocut] ratio cut of bipartitions against lambda_2\")\nrng = np.random.default_rng(2024)\nlab_rand = rng.integers(0, 2, size=n)\ncuts = {\"spectral\": lab_sp, \"k-means on raw features\": lab_km,\n        \"random\": lab_rand}\nrc = {name: ratio_cut(lab, A) for name, lab in cuts.items()}\ncs = {name: int(cut_size(lab, A)) for name, lab in cuts.items()}\nfor name in cuts:\n    check(f\"[B-ratiocut] {name}: {cs[name]} of the {n_edges} edges are \"\n          f\"cut, ratio cut {rc[name]:.3f} >= lambda_2 = {lam[1]:.3f}\",\n          rc[name] >= lam[1] - 1e-12)\ncheck(\"[B-ratiocut] the spectral bipartition has the smallest ratio cut \"\n      \"of the three\", rc[\"spectral\"] <= min(rc.values()) + 1e-12)\n# the two-valued vector of the relaxation identity\na, b = (lab_sp == 0).sum(), (lab_sp == 1).sum()\nf = np.where(lab_sp == 0, np.sqrt(b / a), -np.sqrt(a / b))\ncheck(\"[B-ratiocut] for the two-valued vector f of the spectral partition, \"\n      \"f^T L f = n RatioCut, f is orthogonal to 1, and ||f||^2 = n\",\n      np.isclose(f @ L @ f, n * rc[\"spectral\"]) and abs(f.sum()) < 1e-9\n      and np.isclose(f @ f, n))\ncheck(f\"[B-ratiocut] the relaxed problem attains n lambda_2 = \"\n      f\"{n * lam[1]:.1f} at the Fiedler vector scaled to norm sqrt(n), \"\n      f\"below f^T L f = {f @ L @ f:.1f}\",\n      np.isclose(np.sqrt(n) * fiedler @ L @ (np.sqrt(n) * fiedler),\n                 n * lam[1]) and n * lam[1] <= f @ L @ f)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-components]** Deleting the edges between the two spectral clusters leaves two connected components: eigenvalue 0 then has multiplicity 2 and its eigenvectors are spanned by the two indicator vectors."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-components] deleting the cut edges: multiplicity of eigenvalue 0\")\nA_cut = A.copy()\nbetween = lab_sp[edges[:, 0]] != lab_sp[edges[:, 1]]\nfor i, j in edges[between]:\n    A_cut[i, j] = A_cut[j, i] = 0.0\nL_cut = np.diag(A_cut.sum(axis=1)) - A_cut\nlam_cut, V_cut = np.linalg.eigh(L_cut)\nn_zero = int((np.abs(lam_cut) < 1e-9).sum())\ncheck(f\"[B-components] {components(A_cut)} connected components, eigenvalue \"\n      f\"0 with multiplicity {n_zero}, lambda_3 = {lam_cut[2]:.3f} > 0\",\n      components(A_cut) == 2 and n_zero == 2 and lam_cut[2] > 1e-6)\nind = np.stack([(lab_sp == 0).astype(float), (lab_sp == 1).astype(float)],\n               axis=1)\ncheck(\"[B-components] the indicator vectors of the two components are \"\n      \"eigenvectors of eigenvalue 0 and span its eigenspace\",\n      np.allclose(L_cut @ ind, 0.0)\n      and np.allclose(V_cut[:, :2] @ (V_cut[:, :2].T @ ind), ind))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-three]** Spectral clustering with k = 3 from the first three eigenvectors: cluster sizes and connectivity in the graph."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-three] spectral clustering with k = 3\")\nY3 = V[:, :3]\nlab3, _, it3 = lloyd(Y3, farthest_first(Y3, 3, int(np.argmin(fiedler))))\nsizes3 = np.bincount(lab3, minlength=3)\norder3 = np.argsort([X[lab3 == c, 1].mean() for c in range(3)])\nlab3 = np.argsort(order3)[lab3]                # 0 = coldest, 2 = warmest\nsizes3 = np.bincount(lab3, minlength=3)\nmeans3 = [X[lab3 == c].mean(axis=0) for c in range(3)]\nconn3 = sum(components(A, lab3 == c) == 1 for c in range(3))\ncheck(f\"[B-three] k-means on the rows of (v1, v2, v3) after {it3} iterations: \"\n      f\"cluster sizes {sizes3.tolist()} with mean maximum temperatures \"\n      f\"{means3[0][1]:.1f}, {means3[1][1]:.1f}, {means3[2][1]:.1f}; \"\n      f\"{conn3} of 3 clusters connected in the graph\",\n      sizes3.min() > 20 and conn3 >= 2)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** The graph in the feature plane, every 18th day for the Fiedler-vector panel (21 of 366 days), and the preview."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] graph in the feature plane, Fiedler vector, preview\")\ndoy = np.arange(1, n + 1)\nwith open(OUT_DIR / \"specclustering_nodes.csv\", \"w\") as fh:\n    fh.write(\"date,doy,tmin,tmax,cluster,v2\\n\")\n    for i in range(n):\n        fh.write(f\"{dates[i]},{doy[i]},{X[i, 0]:.1f},{X[i, 1]:.1f},\"\n                 f\"{lab_sp[i]},{fiedler[i]:.5f}\\n\")\nwith open(OUT_DIR / \"specclustering_edges.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax\\n\")\n    for i, j in edges:\n        fh.write(f\"{X[i, 0]:.1f},{X[i, 1]:.1f}\\n{X[j, 0]:.1f},{X[j, 1]:.1f}\\n\"\n                 \"nan,nan\\n\")\nwith open(OUT_DIR / \"specclustering_fiedler.csv\", \"w\") as fh:\n    fh.write(\"doy,v2\\n\")\n    for i in range(n):\n        fh.write(f\"{doy[i]},{fiedler[i]:.5f}\\n\")\nidx = np.arange(0, n, 18)\nwith open(OUT_DIR / \"specclustering_plot.csv\", \"w\") as fh:\n    fh.write(\"doy,tmin,tmax,cluster,v2\\n\")\n    for i in idx:\n        fh.write(f\"{doy[i]},{X[i, 0]:.1f},{X[i, 1]:.1f},{lab_sp[i]},\"\n                 f\"{fiedler[i]:.5f}\\n\")\ncheck(f\"[B-plot] every 18th day: {len(idx)} of the {n} days are drawn in \"\n      f\"the Fiedler-vector panel\", len(idx) == 21)\ncheck(f\"[B-plot] the edge file holds {n_edges} segments\",\n      sum(1 for _ in open(OUT_DIR / \"specclustering_edges.csv\")) - 1\n      == 3 * n_edges)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9.6, 4.0))\nfor i, j in edges:\n    ax1.plot([X[i, 0], X[j, 0]], [X[i, 1], X[j, 1]], color=\"0.75\",\n             lw=0.3, zorder=1)\nfor c, mk, lab in ((0, \"o\", \"cold days\"), (1, \"s\", \"warm days\")):\n    sel = lab_sp == c\n    ax1.scatter(X[sel, 0], X[sel, 1], marker=mk, s=9, facecolors=\"black\",\n                edgecolors=\"black\", lw=0.4, zorder=2,\n                label=f\"{lab} (spectral cluster)\")\nax1.set_xlabel(\"morning minimum temperature (\u00b0C)\")\nax1.set_ylabel(\"maximum daytime temperature (\u00b0C)\")\nax1.set_title(f\"{NEIGHBORS}-nearest-neighbor graph of the {n} days\")\nax1.legend(frameon=False, fontsize=8, loc=\"upper left\")\nax2.plot(doy, fiedler, color=\"black\", lw=0.8, label=\"Fiedler vector entry\")\nax2.axhline(0.0, color=\"0.5\", lw=0.6, ls=\"--\")\nfor c, mk, lab in ((0, \"o\", \"cold days\"), (1, \"s\", \"warm days\")):\n    sel = idx[lab_sp[idx] == c]\n    ax2.scatter(doy[sel], fiedler[sel], marker=mk, facecolors=\"none\",\n                edgecolors=\"black\", s=30, zorder=3,\n                label=f\"{lab} (every 18th day)\")\nax2.set_xlabel(\"day of the year 2024\")\nax2.set_ylabel(\"entry of the Fiedler vector\")\nax2.set_title(\"the sign of the entry gives the cluster\")\nax2.legend(frameon=False, fontsize=8, loc=\"upper left\")\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"specclustering.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)"
  }
 ]
}