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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# Embedding Benchmark Analysis\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"Comparative evaluation of time series embedding methods for piezometric groundwater stations. \n",
|
| 10 |
+
"**Dataset**: ~2000 French groundwater stations, daily frequency, 2010--2024. \n",
|
| 11 |
+
"**Encoders**: MiniRocket, TS2Vec, SoftCLT, Catch22, PCA brut, Random baseline. \n",
|
| 12 |
+
"**Input spaces**: univariate (groundwater level only) and multivariate (+ temperature, precipitation, evapotranspiration). \n",
|
| 13 |
+
"\n",
|
| 14 |
+
"This notebook reproduces and extends the analysis from `reports/benchmark_20260318/comparison_report.md`."
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"import json\n",
|
| 24 |
+
"import pathlib\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"import numpy as np\n",
|
| 27 |
+
"import pandas as pd\n",
|
| 28 |
+
"import plotly.express as px\n",
|
| 29 |
+
"import plotly.graph_objects as go\n",
|
| 30 |
+
"from plotly.subplots import make_subplots\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"pd.set_option(\"display.max_columns\", 30)\n",
|
| 33 |
+
"pd.set_option(\"display.precision\", 4)"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "markdown",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"source": [
|
| 40 |
+
"---\n",
|
| 41 |
+
"## 1. Load Results\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"Parse `metrics.json` and build a flat DataFrame with one row per method, containing all metrics."
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"cell_type": "code",
|
| 48 |
+
"execution_count": null,
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"METRICS_PATH = pathlib.Path(\"../reports/benchmark_20260318/data/metrics.json\")\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"with open(METRICS_PATH) as f:\n",
|
| 55 |
+
" raw = json.load(f)\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"print(f\"Loaded {len(raw)} method entries.\")"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": null,
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"outputs": [],
|
| 65 |
+
"source": [
|
| 66 |
+
"rows = []\n",
|
| 67 |
+
"for entry in raw:\n",
|
| 68 |
+
" row = {\n",
|
| 69 |
+
" \"method\": entry[\"space\"],\n",
|
| 70 |
+
" \"input_space\": entry.get(\"input_space\", \"unknown\"),\n",
|
| 71 |
+
" \"dim\": entry[\"embedding_dim\"],\n",
|
| 72 |
+
" \"n_stations\": entry[\"n_stations\"],\n",
|
| 73 |
+
" # Classification — milieu_eh\n",
|
| 74 |
+
" \"lp_balacc_milieu\": entry[\"linear_milieu\"][\"balanced_accuracy\"],\n",
|
| 75 |
+
" \"lp_f1_milieu\": entry[\"linear_milieu\"][\"macro_f1\"],\n",
|
| 76 |
+
" \"fisher_milieu\": entry[\"fisher_milieu\"][\"ratio\"],\n",
|
| 77 |
+
" # Retrieval — milieu_eh\n",
|
| 78 |
+
" \"p1_milieu\": entry[\"knn_retrieval\"][\"precision@1\"],\n",
|
| 79 |
+
" \"p5_milieu\": entry[\"knn_retrieval\"][\"precision@5\"],\n",
|
| 80 |
+
" \"p10_milieu\": entry[\"knn_retrieval\"][\"precision@10\"],\n",
|
| 81 |
+
" # Clustering\n",
|
| 82 |
+
" \"ami_milieu\": entry[\"clustering_milieu\"].get(\"ami\"),\n",
|
| 83 |
+
" # Dynamic typology\n",
|
| 84 |
+
" \"lp_balacc_typo\": entry[\"linear_typology\"][\"balanced_accuracy\"],\n",
|
| 85 |
+
" \"lp_f1_typo\": entry[\"linear_typology\"][\"macro_f1\"],\n",
|
| 86 |
+
" \"fisher_typo\": entry[\"fisher_typology\"][\"ratio\"],\n",
|
| 87 |
+
" \"p5_typo\": entry[\"knn_typology\"][\"precision@5\"],\n",
|
| 88 |
+
" # Spatial\n",
|
| 89 |
+
" \"mantel_r\": entry[\"mantel_geo\"][\"r\"],\n",
|
| 90 |
+
" \"mantel_p\": entry[\"mantel_geo\"][\"p\"],\n",
|
| 91 |
+
" # Regression\n",
|
| 92 |
+
" \"altitude_r2\": entry[\"regression\"][\"altitude\"][\"r2\"],\n",
|
| 93 |
+
" \"altitude_rho\": entry[\"regression\"][\"altitude\"][\"spearman\"],\n",
|
| 94 |
+
" # Intrinsic quality\n",
|
| 95 |
+
" \"participation_ratio\": entry[\"participation_ratio\"],\n",
|
| 96 |
+
" \"uniformity\": entry[\"uniformity\"],\n",
|
| 97 |
+
" }\n",
|
| 98 |
+
" rows.append(row)\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"df = pd.DataFrame(rows)\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"# Derive helper columns\n",
|
| 103 |
+
"df[\"encoder\"] = df[\"method\"].str.replace(r\" \\((uni|multi)\\)\", \"\", regex=True).str.replace(\" +W\", \"\", regex=False)\n",
|
| 104 |
+
"df[\"whitened\"] = df[\"method\"].str.contains(\"+W\", regex=False)\n",
|
| 105 |
+
"df[\"is_baseline\"] = df[\"encoder\"].isin([\"Random\", \"PCA brut\"])\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"# Remove duplicate Random entries (one per space)\n",
|
| 108 |
+
"df = df.drop_duplicates(subset=[\"method\", \"input_space\"])\n",
|
| 109 |
+
"\n",
|
| 110 |
+
"print(f\"DataFrame shape: {df.shape}\")\n",
|
| 111 |
+
"df[[\"method\", \"input_space\", \"dim\", \"whitened\", \"lp_balacc_milieu\", \"p5_milieu\", \"mantel_r\", \"participation_ratio\"]].sort_values(\"lp_balacc_milieu\", ascending=False)"
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "markdown",
|
| 116 |
+
"metadata": {},
|
| 117 |
+
"source": [
|
| 118 |
+
"---\n",
|
| 119 |
+
"## Color Scheme\n",
|
| 120 |
+
"\n",
|
| 121 |
+
"Consistent color mapping: one hue per encoder, lighter shade for whitened (+W) variants."
|
| 122 |
+
]
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"cell_type": "code",
|
| 126 |
+
"execution_count": null,
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [],
|
| 129 |
+
"source": [
|
| 130 |
+
"# Base colors per encoder\n",
|
| 131 |
+
"ENCODER_COLORS = {\n",
|
| 132 |
+
" \"MiniRocket\": \"#1f77b4\", # blue\n",
|
| 133 |
+
" \"TS2Vec\": \"#ff7f0e\", # orange\n",
|
| 134 |
+
" \"SoftCLT\": \"#2ca02c\", # green\n",
|
| 135 |
+
" \"Catch22\": \"#9467bd\", # purple\n",
|
| 136 |
+
" \"PCA brut\": \"#8c564b\", # brown\n",
|
| 137 |
+
" \"Random\": \"#7f7f7f\", # grey\n",
|
| 138 |
+
"}\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"# Lighter shades for +W variants\n",
|
| 141 |
+
"ENCODER_COLORS_W = {\n",
|
| 142 |
+
" \"MiniRocket\": \"#aec7e8\",\n",
|
| 143 |
+
" \"TS2Vec\": \"#ffbb78\",\n",
|
| 144 |
+
" \"SoftCLT\": \"#98df8a\",\n",
|
| 145 |
+
" \"Catch22\": \"#c5b0d5\",\n",
|
| 146 |
+
" \"PCA brut\": \"#c49c94\",\n",
|
| 147 |
+
" \"Random\": \"#c7c7c7\",\n",
|
| 148 |
+
"}\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"\n",
|
| 151 |
+
"def method_color(method_name: str) -> str:\n",
|
| 152 |
+
" \"\"\"Return color for a method name.\"\"\"\n",
|
| 153 |
+
" encoder = method_name.replace(\" (uni)\", \"\").replace(\" (multi)\", \"\").replace(\" +W\", \"\")\n",
|
| 154 |
+
" if \"+W\" in method_name:\n",
|
| 155 |
+
" return ENCODER_COLORS_W.get(encoder, \"#c7c7c7\")\n",
|
| 156 |
+
" return ENCODER_COLORS.get(encoder, \"#7f7f7f\")\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"\n",
|
| 159 |
+
"def method_colors(methods: list[str]) -> list[str]:\n",
|
| 160 |
+
" \"\"\"Return list of colors matching a list of method names.\"\"\"\n",
|
| 161 |
+
" return [method_color(m) for m in methods]\n",
|
| 162 |
+
"\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"# Common layout for publication-quality figures\n",
|
| 165 |
+
"LAYOUT_DEFAULTS = dict(\n",
|
| 166 |
+
" template=\"plotly_white\",\n",
|
| 167 |
+
" font=dict(family=\"Serif\", size=13),\n",
|
| 168 |
+
" width=900,\n",
|
| 169 |
+
" height=500,\n",
|
| 170 |
+
" margin=dict(l=60, r=30, t=50, b=80),\n",
|
| 171 |
+
")"
|
| 172 |
+
]
|
| 173 |
+
},
|
| 174 |
+
{
|
| 175 |
+
"cell_type": "markdown",
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"source": [
|
| 178 |
+
"---\n",
|
| 179 |
+
"## 2. Classification Performance (milieu_eh)\n",
|
| 180 |
+
"\n",
|
| 181 |
+
"Linear probe balanced accuracy and macro F1 for hydrogeological environment classification. \n",
|
| 182 |
+
"Grouped by input space (univariate vs. multivariate). The random baseline (BalAcc = 0.1147) is shown as a horizontal line."
|
| 183 |
+
]
|
| 184 |
+
},
|
| 185 |
+
{
|
| 186 |
+
"cell_type": "code",
|
| 187 |
+
"execution_count": null,
|
| 188 |
+
"metadata": {},
|
| 189 |
+
"outputs": [],
|
| 190 |
+
"source": [
|
| 191 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 192 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"lp_balacc_milieu\", ascending=False).copy()\n",
|
| 193 |
+
" if sub.empty:\n",
|
| 194 |
+
" continue\n",
|
| 195 |
+
"\n",
|
| 196 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 197 |
+
" colors = method_colors(methods)\n",
|
| 198 |
+
"\n",
|
| 199 |
+
" fig = go.Figure()\n",
|
| 200 |
+
"\n",
|
| 201 |
+
" fig.add_trace(go.Bar(\n",
|
| 202 |
+
" x=methods, y=sub[\"lp_balacc_milieu\"],\n",
|
| 203 |
+
" name=\"LP Balanced Accuracy\",\n",
|
| 204 |
+
" marker_color=colors,\n",
|
| 205 |
+
" text=sub[\"lp_balacc_milieu\"].round(3).astype(str),\n",
|
| 206 |
+
" textposition=\"outside\",\n",
|
| 207 |
+
" ))\n",
|
| 208 |
+
"\n",
|
| 209 |
+
" fig.add_trace(go.Bar(\n",
|
| 210 |
+
" x=methods, y=sub[\"lp_f1_milieu\"],\n",
|
| 211 |
+
" name=\"LP Macro F1\",\n",
|
| 212 |
+
" marker_color=[c + \"99\" for c in colors], # semi-transparent\n",
|
| 213 |
+
" text=sub[\"lp_f1_milieu\"].round(3).astype(str),\n",
|
| 214 |
+
" textposition=\"outside\",\n",
|
| 215 |
+
" ))\n",
|
| 216 |
+
"\n",
|
| 217 |
+
" # Random baseline\n",
|
| 218 |
+
" random_balacc = df[df[\"method\"] == \"Random\"][\"lp_balacc_milieu\"].iloc[0]\n",
|
| 219 |
+
" fig.add_hline(y=random_balacc, line_dash=\"dash\", line_color=\"#7f7f7f\",\n",
|
| 220 |
+
" annotation_text=f\"Random baseline ({random_balacc:.3f})\",\n",
|
| 221 |
+
" annotation_position=\"top left\")\n",
|
| 222 |
+
"\n",
|
| 223 |
+
" # Highlight best method\n",
|
| 224 |
+
" best_idx = sub[\"lp_balacc_milieu\"].idxmax()\n",
|
| 225 |
+
" best_method = sub.loc[best_idx, \"method\"]\n",
|
| 226 |
+
" best_val = sub.loc[best_idx, \"lp_balacc_milieu\"]\n",
|
| 227 |
+
"\n",
|
| 228 |
+
" fig.update_layout(\n",
|
| 229 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 230 |
+
" title=f\"Classification Performance (milieu_eh) -- {space_label} Input\",\n",
|
| 231 |
+
" yaxis_title=\"Score\",\n",
|
| 232 |
+
" barmode=\"group\",\n",
|
| 233 |
+
" yaxis_range=[0, max(sub[\"lp_balacc_milieu\"].max(), sub[\"lp_f1_milieu\"].max()) * 1.2],\n",
|
| 234 |
+
" legend=dict(x=0.75, y=0.95),\n",
|
| 235 |
+
" annotations=[\n",
|
| 236 |
+
" dict(\n",
|
| 237 |
+
" x=best_method, y=best_val + 0.02,\n",
|
| 238 |
+
" text=f\"Best: {best_method}\", showarrow=False,\n",
|
| 239 |
+
" font=dict(size=11, color=\"#d62728\"),\n",
|
| 240 |
+
" )\n",
|
| 241 |
+
" ] + (fig.layout.annotations or []),\n",
|
| 242 |
+
" )\n",
|
| 243 |
+
"\n",
|
| 244 |
+
" fig.show()"
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"cell_type": "markdown",
|
| 249 |
+
"metadata": {},
|
| 250 |
+
"source": [
|
| 251 |
+
"**Observations**:\n",
|
| 252 |
+
"- In multivariate mode, TS2Vec achieves the highest balanced accuracy (0.489), followed by SoftCLT (0.473). Both contrastive methods benefit from the additional climate covariates.\n",
|
| 253 |
+
"- In univariate mode, all methods cluster between 0.26--0.30, with MiniRocket +W slightly ahead (0.302).\n",
|
| 254 |
+
"- F1 tracks BalAcc closely. The large gap between BalAcc and F1 reflects the class imbalance (8 classes, Porous = 48%).\n",
|
| 255 |
+
"- All learned methods substantially outperform the random baseline."
|
| 256 |
+
]
|
| 257 |
+
},
|
| 258 |
+
{
|
| 259 |
+
"cell_type": "markdown",
|
| 260 |
+
"metadata": {},
|
| 261 |
+
"source": [
|
| 262 |
+
"---\n",
|
| 263 |
+
"## 3. Retrieval Quality (k-NN Precision@5)\n",
|
| 264 |
+
"\n",
|
| 265 |
+
"Precision@5 measures whether the 5 nearest neighbors in embedding space share the same hydrogeological label. \n",
|
| 266 |
+
"The random baseline is the expected precision under class frequency distribution: $\\sum p_i^2 = 0.2944$."
|
| 267 |
+
]
|
| 268 |
+
},
|
| 269 |
+
{
|
| 270 |
+
"cell_type": "code",
|
| 271 |
+
"execution_count": null,
|
| 272 |
+
"metadata": {},
|
| 273 |
+
"outputs": [],
|
| 274 |
+
"source": [
|
| 275 |
+
"RANDOM_BASELINE_P5 = 0.2944\n",
|
| 276 |
+
"\n",
|
| 277 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 278 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"p5_milieu\", ascending=False).copy()\n",
|
| 279 |
+
" if sub.empty:\n",
|
| 280 |
+
" continue\n",
|
| 281 |
+
"\n",
|
| 282 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 283 |
+
" colors = method_colors(methods)\n",
|
| 284 |
+
"\n",
|
| 285 |
+
" fig = go.Figure()\n",
|
| 286 |
+
" fig.add_trace(go.Bar(\n",
|
| 287 |
+
" x=methods, y=sub[\"p5_milieu\"],\n",
|
| 288 |
+
" marker_color=colors,\n",
|
| 289 |
+
" text=sub[\"p5_milieu\"].round(3).astype(str),\n",
|
| 290 |
+
" textposition=\"outside\",\n",
|
| 291 |
+
" ))\n",
|
| 292 |
+
"\n",
|
| 293 |
+
" fig.add_hline(y=RANDOM_BASELINE_P5, line_dash=\"dash\", line_color=\"#7f7f7f\",\n",
|
| 294 |
+
" annotation_text=f\"Random baseline ({RANDOM_BASELINE_P5})\",\n",
|
| 295 |
+
" annotation_position=\"top left\")\n",
|
| 296 |
+
"\n",
|
| 297 |
+
" fig.update_layout(\n",
|
| 298 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 299 |
+
" title=f\"Retrieval Quality: Precision@5 (milieu_eh) -- {space_label}\",\n",
|
| 300 |
+
" yaxis_title=\"Precision@5\",\n",
|
| 301 |
+
" yaxis_range=[0, sub[\"p5_milieu\"].max() * 1.2],\n",
|
| 302 |
+
" showlegend=False,\n",
|
| 303 |
+
" )\n",
|
| 304 |
+
" fig.show()"
|
| 305 |
+
]
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"cell_type": "markdown",
|
| 309 |
+
"metadata": {},
|
| 310 |
+
"source": [
|
| 311 |
+
"**Observations**:\n",
|
| 312 |
+
"- Whitened variants (+W) consistently improve retrieval. In multivariate space, SoftCLT +W and TS2Vec +W reach P@5 > 0.52, a +14% relative gain over their raw counterparts.\n",
|
| 313 |
+
"- Whitening isotropizes the embedding, reducing hubness and making k-NN distances more meaningful.\n",
|
| 314 |
+
"- Even the raw MiniRocket (multi) embeddings already reach P@5 = 0.44, well above the random baseline of 0.29."
|
| 315 |
+
]
|
| 316 |
+
},
|
| 317 |
+
{
|
| 318 |
+
"cell_type": "markdown",
|
| 319 |
+
"metadata": {},
|
| 320 |
+
"source": [
|
| 321 |
+
"---\n",
|
| 322 |
+
"## 4. Dynamic Typology (inertial / annual / reactive)\n",
|
| 323 |
+
"\n",
|
| 324 |
+
"Data-driven labels from lag-365 autocorrelation. Tests whether embeddings capture temporal dynamics (not geological structure). \n",
|
| 325 |
+
"Note: labels are derived from the same time series, so this is a **consistency check**, not an independent evaluation."
|
| 326 |
+
]
|
| 327 |
+
},
|
| 328 |
+
{
|
| 329 |
+
"cell_type": "code",
|
| 330 |
+
"execution_count": null,
|
| 331 |
+
"metadata": {},
|
| 332 |
+
"outputs": [],
|
| 333 |
+
"source": [
|
| 334 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 335 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"lp_balacc_typo\", ascending=False).copy()\n",
|
| 336 |
+
" if sub.empty:\n",
|
| 337 |
+
" continue\n",
|
| 338 |
+
"\n",
|
| 339 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 340 |
+
" colors = method_colors(methods)\n",
|
| 341 |
+
"\n",
|
| 342 |
+
" fig = go.Figure()\n",
|
| 343 |
+
"\n",
|
| 344 |
+
" fig.add_trace(go.Bar(\n",
|
| 345 |
+
" x=methods, y=sub[\"lp_balacc_typo\"],\n",
|
| 346 |
+
" name=\"LP Balanced Accuracy\",\n",
|
| 347 |
+
" marker_color=colors,\n",
|
| 348 |
+
" text=sub[\"lp_balacc_typo\"].round(3).astype(str),\n",
|
| 349 |
+
" textposition=\"outside\",\n",
|
| 350 |
+
" ))\n",
|
| 351 |
+
"\n",
|
| 352 |
+
" fig.add_trace(go.Bar(\n",
|
| 353 |
+
" x=methods, y=sub[\"lp_f1_typo\"],\n",
|
| 354 |
+
" name=\"LP Macro F1\",\n",
|
| 355 |
+
" marker_color=[c + \"99\" for c in colors],\n",
|
| 356 |
+
" text=sub[\"lp_f1_typo\"].round(3).astype(str),\n",
|
| 357 |
+
" textposition=\"outside\",\n",
|
| 358 |
+
" ))\n",
|
| 359 |
+
"\n",
|
| 360 |
+
" # Random baseline for typology\n",
|
| 361 |
+
" random_typo = df[df[\"method\"] == \"Random\"][\"lp_balacc_typo\"].iloc[0]\n",
|
| 362 |
+
" fig.add_hline(y=random_typo, line_dash=\"dash\", line_color=\"#7f7f7f\",\n",
|
| 363 |
+
" annotation_text=f\"Random baseline ({random_typo:.3f})\",\n",
|
| 364 |
+
" annotation_position=\"top left\")\n",
|
| 365 |
+
"\n",
|
| 366 |
+
" fig.update_layout(\n",
|
| 367 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 368 |
+
" title=f\"Dynamic Typology Performance -- {space_label} Input\",\n",
|
| 369 |
+
" yaxis_title=\"Score\",\n",
|
| 370 |
+
" barmode=\"group\",\n",
|
| 371 |
+
" yaxis_range=[0, sub[\"lp_balacc_typo\"].max() * 1.15],\n",
|
| 372 |
+
" legend=dict(x=0.75, y=0.95),\n",
|
| 373 |
+
" )\n",
|
| 374 |
+
" fig.show()"
|
| 375 |
+
]
|
| 376 |
+
},
|
| 377 |
+
{
|
| 378 |
+
"cell_type": "markdown",
|
| 379 |
+
"metadata": {},
|
| 380 |
+
"source": [
|
| 381 |
+
"**Observations**:\n",
|
| 382 |
+
"- MiniRocket dominates the dynamic typology task in both spaces. MiniRocket +W (uni) reaches BalAcc = 0.762, the highest score in the entire benchmark.\n",
|
| 383 |
+
"- This makes sense: MiniRocket extracts random convolutional features that directly capture temporal patterns (frequency content, trend, seasonality).\n",
|
| 384 |
+
"- Contrastive methods (TS2Vec, SoftCLT) are optimized for instance discrimination, not temporal pattern classification, which explains their lower performance here.\n",
|
| 385 |
+
"- The gap between MiniRocket and contrastive methods is larger in univariate mode, where covariates cannot compensate."
|
| 386 |
+
]
|
| 387 |
+
},
|
| 388 |
+
{
|
| 389 |
+
"cell_type": "markdown",
|
| 390 |
+
"metadata": {},
|
| 391 |
+
"source": [
|
| 392 |
+
"---\n",
|
| 393 |
+
"## 5. Spatial Coherence (Mantel Test)\n",
|
| 394 |
+
"\n",
|
| 395 |
+
"The Mantel statistic *r* measures the correlation between pairwise embedding distances and pairwise geographic distances. \n",
|
| 396 |
+
"A positive *r* means that stations close in embedding space tend to be geographically close."
|
| 397 |
+
]
|
| 398 |
+
},
|
| 399 |
+
{
|
| 400 |
+
"cell_type": "code",
|
| 401 |
+
"execution_count": null,
|
| 402 |
+
"metadata": {},
|
| 403 |
+
"outputs": [],
|
| 404 |
+
"source": [
|
| 405 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 406 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"mantel_r\", ascending=False).copy()\n",
|
| 407 |
+
" if sub.empty:\n",
|
| 408 |
+
" continue\n",
|
| 409 |
+
"\n",
|
| 410 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 411 |
+
" colors = method_colors(methods)\n",
|
| 412 |
+
"\n",
|
| 413 |
+
" fig = go.Figure()\n",
|
| 414 |
+
" fig.add_trace(go.Bar(\n",
|
| 415 |
+
" x=methods, y=sub[\"mantel_r\"],\n",
|
| 416 |
+
" marker_color=colors,\n",
|
| 417 |
+
" text=sub[\"mantel_r\"].round(4).astype(str),\n",
|
| 418 |
+
" textposition=\"outside\",\n",
|
| 419 |
+
" ))\n",
|
| 420 |
+
"\n",
|
| 421 |
+
" fig.add_hline(y=0, line_dash=\"dot\", line_color=\"#7f7f7f\")\n",
|
| 422 |
+
"\n",
|
| 423 |
+
" fig.update_layout(\n",
|
| 424 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 425 |
+
" title=f\"Spatial Coherence: Mantel r -- {space_label}\",\n",
|
| 426 |
+
" yaxis_title=\"Mantel r (embedding vs. geographic distance)\",\n",
|
| 427 |
+
" showlegend=False,\n",
|
| 428 |
+
" )\n",
|
| 429 |
+
" fig.show()"
|
| 430 |
+
]
|
| 431 |
+
},
|
| 432 |
+
{
|
| 433 |
+
"cell_type": "markdown",
|
| 434 |
+
"metadata": {},
|
| 435 |
+
"source": [
|
| 436 |
+
"**Observations**:\n",
|
| 437 |
+
"- Whitening dramatically improves spatial coherence. SoftCLT +W (multi) reaches r = 0.214, the highest overall.\n",
|
| 438 |
+
"- In multivariate mode, raw embeddings already show positive Mantel r (0.09--0.12), indicating that climate covariates encode spatial proximity.\n",
|
| 439 |
+
"- In univariate mode, some raw methods have near-zero or negative Mantel r, which means their embedding geometry has no geographic structure.\n",
|
| 440 |
+
"- The whitened multivariate embeddings (r = 0.17--0.21) confirm that post-hoc isotropization reveals latent spatial structure that was masked by dimension collapse."
|
| 441 |
+
]
|
| 442 |
+
},
|
| 443 |
+
{
|
| 444 |
+
"cell_type": "markdown",
|
| 445 |
+
"metadata": {},
|
| 446 |
+
"source": [
|
| 447 |
+
"---\n",
|
| 448 |
+
"## 6. Intrinsic Quality\n",
|
| 449 |
+
"\n",
|
| 450 |
+
"### 6a. Participation Ratio\n",
|
| 451 |
+
"\n",
|
| 452 |
+
"The Participation Ratio (PR) measures the effective dimensionality of the embedding. A PR of *d* means the embedding variance is spread across *d* effective dimensions. \n",
|
| 453 |
+
"Higher PR = more dimensions are used (less information waste)."
|
| 454 |
+
]
|
| 455 |
+
},
|
| 456 |
+
{
|
| 457 |
+
"cell_type": "code",
|
| 458 |
+
"execution_count": null,
|
| 459 |
+
"metadata": {},
|
| 460 |
+
"outputs": [],
|
| 461 |
+
"source": [
|
| 462 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 463 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"participation_ratio\", ascending=False).copy()\n",
|
| 464 |
+
" if sub.empty:\n",
|
| 465 |
+
" continue\n",
|
| 466 |
+
"\n",
|
| 467 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 468 |
+
" colors = method_colors(methods)\n",
|
| 469 |
+
"\n",
|
| 470 |
+
" fig = go.Figure()\n",
|
| 471 |
+
" fig.add_trace(go.Bar(\n",
|
| 472 |
+
" x=methods, y=sub[\"participation_ratio\"],\n",
|
| 473 |
+
" marker_color=colors,\n",
|
| 474 |
+
" text=sub[\"participation_ratio\"].round(1).astype(str),\n",
|
| 475 |
+
" textposition=\"outside\",\n",
|
| 476 |
+
" ))\n",
|
| 477 |
+
"\n",
|
| 478 |
+
" fig.update_layout(\n",
|
| 479 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 480 |
+
" title=f\"Participation Ratio (effective dimensionality) -- {space_label}\",\n",
|
| 481 |
+
" yaxis_title=\"Participation Ratio\",\n",
|
| 482 |
+
" showlegend=False,\n",
|
| 483 |
+
" )\n",
|
| 484 |
+
" fig.show()"
|
| 485 |
+
]
|
| 486 |
+
},
|
| 487 |
+
{
|
| 488 |
+
"cell_type": "markdown",
|
| 489 |
+
"metadata": {},
|
| 490 |
+
"source": [
|
| 491 |
+
"### 6b. Uniformity\n",
|
| 492 |
+
"\n",
|
| 493 |
+
"Uniformity measures how well embeddings are spread on the unit hypersphere. \n",
|
| 494 |
+
"More negative = better spread (ideal uniform distribution on the sphere). \n",
|
| 495 |
+
"Values near 0 indicate **dimensional collapse**: all embeddings cluster in a low-dimensional subspace."
|
| 496 |
+
]
|
| 497 |
+
},
|
| 498 |
+
{
|
| 499 |
+
"cell_type": "code",
|
| 500 |
+
"execution_count": null,
|
| 501 |
+
"metadata": {},
|
| 502 |
+
"outputs": [],
|
| 503 |
+
"source": [
|
| 504 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 505 |
+
" sub = df[df[\"input_space\"] == space_val].sort_values(\"uniformity\").copy() # more negative = better\n",
|
| 506 |
+
" if sub.empty:\n",
|
| 507 |
+
" continue\n",
|
| 508 |
+
"\n",
|
| 509 |
+
" methods = sub[\"method\"].tolist()\n",
|
| 510 |
+
" colors = method_colors(methods)\n",
|
| 511 |
+
"\n",
|
| 512 |
+
" fig = go.Figure()\n",
|
| 513 |
+
" fig.add_trace(go.Bar(\n",
|
| 514 |
+
" x=methods, y=sub[\"uniformity\"],\n",
|
| 515 |
+
" marker_color=colors,\n",
|
| 516 |
+
" text=sub[\"uniformity\"].round(2).astype(str),\n",
|
| 517 |
+
" textposition=\"outside\",\n",
|
| 518 |
+
" ))\n",
|
| 519 |
+
"\n",
|
| 520 |
+
" # Highlight collapse zone\n",
|
| 521 |
+
" fig.add_hrect(y0=-0.5, y1=0, fillcolor=\"#ff000015\", line_width=0,\n",
|
| 522 |
+
" annotation_text=\"Collapse zone\", annotation_position=\"top right\")\n",
|
| 523 |
+
"\n",
|
| 524 |
+
" fig.update_layout(\n",
|
| 525 |
+
" **LAYOUT_DEFAULTS,\n",
|
| 526 |
+
" title=f\"Uniformity (embedding spread) -- {space_label}\",\n",
|
| 527 |
+
" yaxis_title=\"Uniformity (more negative = better)\",\n",
|
| 528 |
+
" showlegend=False,\n",
|
| 529 |
+
" )\n",
|
| 530 |
+
" fig.show()"
|
| 531 |
+
]
|
| 532 |
+
},
|
| 533 |
+
{
|
| 534 |
+
"cell_type": "markdown",
|
| 535 |
+
"metadata": {},
|
| 536 |
+
"source": [
|
| 537 |
+
"**Observations**:\n",
|
| 538 |
+
"- **Dimensional collapse is severe in contrastive methods.** TS2Vec and SoftCLT (raw) have uniformity near 0 (-0.12 multi, -0.34/-0.50 uni) and PR of only 1.7--3.0. This means they collapse onto 2--3 effective dimensions out of 320.\n",
|
| 539 |
+
"- MiniRocket (raw) fares better (uniformity -2.15 to -2.39, PR ~2.3--3.0) but still underuses its 320 dimensions.\n",
|
| 540 |
+
"- **Whitening fully resolves the collapse.** All +W variants achieve PR = 64 (the target PCA dimension) and uniformity near -3.8, matching the ideal Random baseline.\n",
|
| 541 |
+
"- This explains why whitened methods improve so much on retrieval (P@5) and Mantel r: isotropic embeddings make distance-based metrics meaningful."
|
| 542 |
+
]
|
| 543 |
+
},
|
| 544 |
+
{
|
| 545 |
+
"cell_type": "markdown",
|
| 546 |
+
"metadata": {},
|
| 547 |
+
"source": [
|
| 548 |
+
"---\n",
|
| 549 |
+
"## 7. Whitening Effect\n",
|
| 550 |
+
"\n",
|
| 551 |
+
"For each encoder, show the delta (whitened - raw) on key metrics. \n",
|
| 552 |
+
"Positive delta means whitening helped; negative means it hurt."
|
| 553 |
+
]
|
| 554 |
+
},
|
| 555 |
+
{
|
| 556 |
+
"cell_type": "code",
|
| 557 |
+
"execution_count": null,
|
| 558 |
+
"metadata": {},
|
| 559 |
+
"outputs": [],
|
| 560 |
+
"source": [
|
| 561 |
+
"# Build pairs: raw vs whitened\n",
|
| 562 |
+
"deltas = []\n",
|
| 563 |
+
"for space_val in [\"uni\", \"multi\"]:\n",
|
| 564 |
+
" sub = df[df[\"input_space\"] == space_val]\n",
|
| 565 |
+
" raw_methods = sub[~sub[\"whitened\"] & ~sub[\"is_baseline\"]]\n",
|
| 566 |
+
" for _, raw_row in raw_methods.iterrows():\n",
|
| 567 |
+
" w_name = raw_row[\"method\"] + \" +W\"\n",
|
| 568 |
+
" w_row = sub[sub[\"method\"] == w_name]\n",
|
| 569 |
+
" if w_row.empty:\n",
|
| 570 |
+
" continue\n",
|
| 571 |
+
" w_row = w_row.iloc[0]\n",
|
| 572 |
+
" deltas.append({\n",
|
| 573 |
+
" \"encoder\": raw_row[\"method\"],\n",
|
| 574 |
+
" \"space\": space_val,\n",
|
| 575 |
+
" \"delta_balacc_milieu\": w_row[\"lp_balacc_milieu\"] - raw_row[\"lp_balacc_milieu\"],\n",
|
| 576 |
+
" \"delta_f1_milieu\": w_row[\"lp_f1_milieu\"] - raw_row[\"lp_f1_milieu\"],\n",
|
| 577 |
+
" \"delta_p5_milieu\": w_row[\"p5_milieu\"] - raw_row[\"p5_milieu\"],\n",
|
| 578 |
+
" \"delta_mantel\": w_row[\"mantel_r\"] - raw_row[\"mantel_r\"],\n",
|
| 579 |
+
" \"delta_balacc_typo\": w_row[\"lp_balacc_typo\"] - raw_row[\"lp_balacc_typo\"],\n",
|
| 580 |
+
" \"delta_pr\": w_row[\"participation_ratio\"] - raw_row[\"participation_ratio\"],\n",
|
| 581 |
+
" \"delta_uniformity\": w_row[\"uniformity\"] - raw_row[\"uniformity\"],\n",
|
| 582 |
+
" })\n",
|
| 583 |
+
"\n",
|
| 584 |
+
"df_delta = pd.DataFrame(deltas)\n",
|
| 585 |
+
"df_delta"
|
| 586 |
+
]
|
| 587 |
+
},
|
| 588 |
+
{
|
| 589 |
+
"cell_type": "code",
|
| 590 |
+
"execution_count": null,
|
| 591 |
+
"metadata": {},
|
| 592 |
+
"outputs": [],
|
| 593 |
+
"source": [
|
| 594 |
+
"# Scatter: method vs delta on key metrics\n",
|
| 595 |
+
"metrics_to_plot = [\n",
|
| 596 |
+
" (\"delta_balacc_milieu\", \"Delta LP BalAcc (milieu_eh)\"),\n",
|
| 597 |
+
" (\"delta_p5_milieu\", \"Delta Precision@5 (milieu_eh)\"),\n",
|
| 598 |
+
" (\"delta_mantel\", \"Delta Mantel r\"),\n",
|
| 599 |
+
" (\"delta_balacc_typo\", \"Delta LP BalAcc (dynamic typology)\"),\n",
|
| 600 |
+
"]\n",
|
| 601 |
+
"\n",
|
| 602 |
+
"fig = make_subplots(\n",
|
| 603 |
+
" rows=2, cols=2,\n",
|
| 604 |
+
" subplot_titles=[t for _, t in metrics_to_plot],\n",
|
| 605 |
+
" vertical_spacing=0.15,\n",
|
| 606 |
+
" horizontal_spacing=0.12,\n",
|
| 607 |
+
")\n",
|
| 608 |
+
"\n",
|
| 609 |
+
"for idx, (col, title) in enumerate(metrics_to_plot):\n",
|
| 610 |
+
" row, c = divmod(idx, 2)\n",
|
| 611 |
+
" row += 1\n",
|
| 612 |
+
" c += 1\n",
|
| 613 |
+
"\n",
|
| 614 |
+
" labels = df_delta[\"encoder\"] + \" (\" + df_delta[\"space\"] + \")\"\n",
|
| 615 |
+
" colors_list = [method_color(m) for m in df_delta[\"encoder\"]]\n",
|
| 616 |
+
"\n",
|
| 617 |
+
" fig.add_trace(\n",
|
| 618 |
+
" go.Bar(\n",
|
| 619 |
+
" x=labels, y=df_delta[col],\n",
|
| 620 |
+
" marker_color=colors_list,\n",
|
| 621 |
+
" text=df_delta[col].round(3).astype(str),\n",
|
| 622 |
+
" textposition=\"outside\",\n",
|
| 623 |
+
" showlegend=False,\n",
|
| 624 |
+
" ),\n",
|
| 625 |
+
" row=row, col=c,\n",
|
| 626 |
+
" )\n",
|
| 627 |
+
" fig.add_hline(y=0, line_dash=\"dot\", line_color=\"#7f7f7f\", row=row, col=c)\n",
|
| 628 |
+
"\n",
|
| 629 |
+
"fig.update_layout(\n",
|
| 630 |
+
" template=\"plotly_white\",\n",
|
| 631 |
+
" font=dict(family=\"Serif\", size=11),\n",
|
| 632 |
+
" width=1000,\n",
|
| 633 |
+
" height=700,\n",
|
| 634 |
+
" title=\"Effect of Whitening (+W) on Key Metrics\",\n",
|
| 635 |
+
" margin=dict(l=50, r=30, t=80, b=80),\n",
|
| 636 |
+
")\n",
|
| 637 |
+
"fig.show()"
|
| 638 |
+
]
|
| 639 |
+
},
|
| 640 |
+
{
|
| 641 |
+
"cell_type": "markdown",
|
| 642 |
+
"metadata": {},
|
| 643 |
+
"source": [
|
| 644 |
+
"**Observations**:\n",
|
| 645 |
+
"- Whitening **always improves** Precision@5 and Mantel r. The effect is strongest for contrastive methods that suffer from dimensional collapse.\n",
|
| 646 |
+
"- On classification (LP BalAcc), whitening has mixed effects: it slightly helps MiniRocket but can slightly hurt TS2Vec and SoftCLT. The linear probe can compensate for anisotropy, so whitening is less critical here.\n",
|
| 647 |
+
"- On dynamic typology, whitening has a small positive effect for MiniRocket but a small negative effect for contrastive methods.\n",
|
| 648 |
+
"- The main benefit of whitening is for **distance-based evaluation** (k-NN, Mantel), not for **linear evaluation** (probes)."
|
| 649 |
+
]
|
| 650 |
+
},
|
| 651 |
+
{
|
| 652 |
+
"cell_type": "markdown",
|
| 653 |
+
"metadata": {},
|
| 654 |
+
"source": [
|
| 655 |
+
"---\n",
|
| 656 |
+
"## 8. Uni vs Multi Comparison\n",
|
| 657 |
+
"\n",
|
| 658 |
+
"For each encoder (MiniRocket, TS2Vec, SoftCLT), compare univariate vs. multivariate performance side by side."
|
| 659 |
+
]
|
| 660 |
+
},
|
| 661 |
+
{
|
| 662 |
+
"cell_type": "code",
|
| 663 |
+
"execution_count": null,
|
| 664 |
+
"metadata": {},
|
| 665 |
+
"outputs": [],
|
| 666 |
+
"source": [
|
| 667 |
+
"# Filter to encoders available in both spaces (exclude Catch22, PCA brut, Random)\n",
|
| 668 |
+
"encoders_both = [\"MiniRocket\", \"TS2Vec\", \"SoftCLT\"]\n",
|
| 669 |
+
"metrics_compare = [\n",
|
| 670 |
+
" (\"lp_balacc_milieu\", \"LP BalAcc (milieu_eh)\"),\n",
|
| 671 |
+
" (\"p5_milieu\", \"P@5 (milieu_eh)\"),\n",
|
| 672 |
+
" (\"lp_balacc_typo\", \"LP BalAcc (dynamic typology)\"),\n",
|
| 673 |
+
" (\"mantel_r\", \"Mantel r\"),\n",
|
| 674 |
+
"]\n",
|
| 675 |
+
"\n",
|
| 676 |
+
"fig = make_subplots(\n",
|
| 677 |
+
" rows=2, cols=2,\n",
|
| 678 |
+
" subplot_titles=[t for _, t in metrics_compare],\n",
|
| 679 |
+
" vertical_spacing=0.18,\n",
|
| 680 |
+
" horizontal_spacing=0.12,\n",
|
| 681 |
+
")\n",
|
| 682 |
+
"\n",
|
| 683 |
+
"for idx, (metric_col, metric_label) in enumerate(metrics_compare):\n",
|
| 684 |
+
" row, c = divmod(idx, 2)\n",
|
| 685 |
+
" row += 1\n",
|
| 686 |
+
" c += 1\n",
|
| 687 |
+
"\n",
|
| 688 |
+
" for space_val, pattern_suffix in [(\"uni\", \"(uni)\"), (\"multi\", \"(multi)\")]:\n",
|
| 689 |
+
" vals = []\n",
|
| 690 |
+
" for enc in encoders_both:\n",
|
| 691 |
+
" method_name = f\"{enc} {pattern_suffix}\"\n",
|
| 692 |
+
" match = df[(df[\"method\"] == method_name) & (df[\"input_space\"] == space_val)]\n",
|
| 693 |
+
" if not match.empty:\n",
|
| 694 |
+
" vals.append(match.iloc[0][metric_col])\n",
|
| 695 |
+
" else:\n",
|
| 696 |
+
" vals.append(None)\n",
|
| 697 |
+
"\n",
|
| 698 |
+
" fig.add_trace(\n",
|
| 699 |
+
" go.Bar(\n",
|
| 700 |
+
" x=encoders_both, y=vals,\n",
|
| 701 |
+
" name=space_val.capitalize(),\n",
|
| 702 |
+
" marker_color=\"#1f77b4\" if space_val == \"uni\" else \"#ff7f0e\",\n",
|
| 703 |
+
" marker_opacity=0.8,\n",
|
| 704 |
+
" text=[f\"{v:.3f}\" if v is not None else \"\" for v in vals],\n",
|
| 705 |
+
" textposition=\"outside\",\n",
|
| 706 |
+
" showlegend=(idx == 0), # only show legend once\n",
|
| 707 |
+
" ),\n",
|
| 708 |
+
" row=row, col=c,\n",
|
| 709 |
+
" )\n",
|
| 710 |
+
"\n",
|
| 711 |
+
"fig.update_layout(\n",
|
| 712 |
+
" template=\"plotly_white\",\n",
|
| 713 |
+
" font=dict(family=\"Serif\", size=11),\n",
|
| 714 |
+
" width=1000,\n",
|
| 715 |
+
" height=700,\n",
|
| 716 |
+
" title=\"Univariate vs. Multivariate: Side-by-Side Comparison\",\n",
|
| 717 |
+
" barmode=\"group\",\n",
|
| 718 |
+
" margin=dict(l=50, r=30, t=80, b=60),\n",
|
| 719 |
+
" legend=dict(x=0.85, y=1.0),\n",
|
| 720 |
+
")\n",
|
| 721 |
+
"fig.show()"
|
| 722 |
+
]
|
| 723 |
+
},
|
| 724 |
+
{
|
| 725 |
+
"cell_type": "code",
|
| 726 |
+
"execution_count": null,
|
| 727 |
+
"metadata": {},
|
| 728 |
+
"outputs": [],
|
| 729 |
+
"source": [
|
| 730 |
+
"# Quantify the multivariate gain\n",
|
| 731 |
+
"print(\"Multivariate gain (multi - uni) for raw encoders:\")\n",
|
| 732 |
+
"print(\"=\" * 65)\n",
|
| 733 |
+
"for enc in encoders_both:\n",
|
| 734 |
+
" uni = df[(df[\"method\"] == f\"{enc} (uni)\") & (df[\"input_space\"] == \"uni\")]\n",
|
| 735 |
+
" multi = df[(df[\"method\"] == f\"{enc} (multi)\") & (df[\"input_space\"] == \"multi\")]\n",
|
| 736 |
+
" if uni.empty or multi.empty:\n",
|
| 737 |
+
" continue\n",
|
| 738 |
+
" uni, multi = uni.iloc[0], multi.iloc[0]\n",
|
| 739 |
+
" print(f\"\\n{enc}:\")\n",
|
| 740 |
+
" for col, label in [(\"lp_balacc_milieu\", \"LP BalAcc (milieu)\"), (\"p5_milieu\", \"P@5\"),\n",
|
| 741 |
+
" (\"lp_balacc_typo\", \"LP BalAcc (typo)\"), (\"mantel_r\", \"Mantel r\")]:\n",
|
| 742 |
+
" delta = multi[col] - uni[col]\n",
|
| 743 |
+
" pct = delta / max(abs(uni[col]), 1e-6) * 100\n",
|
| 744 |
+
" print(f\" {label:30s}: {delta:+.4f} ({pct:+.1f}%)\")"
|
| 745 |
+
]
|
| 746 |
+
},
|
| 747 |
+
{
|
| 748 |
+
"cell_type": "markdown",
|
| 749 |
+
"metadata": {},
|
| 750 |
+
"source": [
|
| 751 |
+
"**Observations**:\n",
|
| 752 |
+
"- Multivariate input provides a **massive boost for geological classification** (milieu_eh): TS2Vec goes from 0.267 (uni) to 0.489 (multi), an 83% relative improvement.\n",
|
| 753 |
+
"- For dynamic typology, multivariate input is **neutral or slightly negative**: temporal dynamics are primarily captured by the groundwater level itself, not by climate covariates.\n",
|
| 754 |
+
"- Spatial coherence (Mantel r) improves with multivariate input because ERA5 covariates (temperature, precipitation) carry geographic information.\n",
|
| 755 |
+
"- The climate covariates provide complementary geological and spatial signal that the univariate series alone cannot capture."
|
| 756 |
+
]
|
| 757 |
+
},
|
| 758 |
+
{
|
| 759 |
+
"cell_type": "markdown",
|
| 760 |
+
"metadata": {},
|
| 761 |
+
"source": [
|
| 762 |
+
"---\n",
|
| 763 |
+
"## 9. Summary Ranking Table\n",
|
| 764 |
+
"\n",
|
| 765 |
+
"Rank all methods across key metrics. Lower mean rank = better overall performance."
|
| 766 |
+
]
|
| 767 |
+
},
|
| 768 |
+
{
|
| 769 |
+
"cell_type": "code",
|
| 770 |
+
"execution_count": null,
|
| 771 |
+
"metadata": {},
|
| 772 |
+
"outputs": [],
|
| 773 |
+
"source": [
|
| 774 |
+
"rank_metrics = {\n",
|
| 775 |
+
" \"LP BalAcc (milieu)\": (\"lp_balacc_milieu\", True),\n",
|
| 776 |
+
" \"LP F1 (milieu)\": (\"lp_f1_milieu\", True),\n",
|
| 777 |
+
" \"P@5 (milieu)\": (\"p5_milieu\", True),\n",
|
| 778 |
+
" \"LP BalAcc (typo)\": (\"lp_balacc_typo\", True),\n",
|
| 779 |
+
" \"Mantel r\": (\"mantel_r\", True),\n",
|
| 780 |
+
" \"Participation Ratio\": (\"participation_ratio\", True),\n",
|
| 781 |
+
"}\n",
|
| 782 |
+
"\n",
|
| 783 |
+
"ranking_tables = {}\n",
|
| 784 |
+
"for space_label, space_val in [(\"Univariate\", \"uni\"), (\"Multivariate\", \"multi\")]:\n",
|
| 785 |
+
" sub = df[df[\"input_space\"] == space_val].copy()\n",
|
| 786 |
+
" if sub.empty:\n",
|
| 787 |
+
" continue\n",
|
| 788 |
+
"\n",
|
| 789 |
+
" rank_df = pd.DataFrame(index=sub[\"method\"])\n",
|
| 790 |
+
" for metric_label, (col, ascending) in rank_metrics.items():\n",
|
| 791 |
+
" rank_df[metric_label] = sub[col].rank(ascending=not ascending, method=\"min\").values\n",
|
| 792 |
+
"\n",
|
| 793 |
+
" rank_df[\"Mean Rank\"] = rank_df.mean(axis=1)\n",
|
| 794 |
+
" rank_df = rank_df.sort_values(\"Mean Rank\")\n",
|
| 795 |
+
" ranking_tables[space_label] = rank_df\n",
|
| 796 |
+
"\n",
|
| 797 |
+
" print(f\"\\n{'=' * 80}\")\n",
|
| 798 |
+
" print(f\"Overall Ranking -- {space_label} Input Space\")\n",
|
| 799 |
+
" print(f\"{'=' * 80}\")\n",
|
| 800 |
+
" print(rank_df.round(2).to_string())\n",
|
| 801 |
+
" print()"
|
| 802 |
+
]
|
| 803 |
+
},
|
| 804 |
+
{
|
| 805 |
+
"cell_type": "code",
|
| 806 |
+
"execution_count": null,
|
| 807 |
+
"metadata": {},
|
| 808 |
+
"outputs": [],
|
| 809 |
+
"source": [
|
| 810 |
+
"# Color-coded heatmap of ranks\n",
|
| 811 |
+
"for space_label, rank_df in ranking_tables.items():\n",
|
| 812 |
+
" # Exclude \"Mean Rank\" column for the heatmap body\n",
|
| 813 |
+
" display_df = rank_df.drop(columns=[\"Mean Rank\"])\n",
|
| 814 |
+
"\n",
|
| 815 |
+
" fig = go.Figure(data=go.Heatmap(\n",
|
| 816 |
+
" z=display_df.values,\n",
|
| 817 |
+
" x=display_df.columns.tolist(),\n",
|
| 818 |
+
" y=display_df.index.tolist(),\n",
|
| 819 |
+
" colorscale=[\n",
|
| 820 |
+
" [0.0, \"#2ca02c\"], # rank 1 = green\n",
|
| 821 |
+
" [0.5, \"#ffffbf\"], # middle = yellow\n",
|
| 822 |
+
" [1.0, \"#d62728\"], # worst = red\n",
|
| 823 |
+
" ],\n",
|
| 824 |
+
" text=display_df.values.round(0).astype(int).astype(str),\n",
|
| 825 |
+
" texttemplate=\"%{text}\",\n",
|
| 826 |
+
" textfont=dict(size=12),\n",
|
| 827 |
+
" hovertemplate=\"Method: %{y}<br>Metric: %{x}<br>Rank: %{z}<extra></extra>\",\n",
|
| 828 |
+
" colorbar=dict(title=\"Rank\"),\n",
|
| 829 |
+
" ))\n",
|
| 830 |
+
"\n",
|
| 831 |
+
" # Add Mean Rank as annotation on the right\n",
|
| 832 |
+
" for i, (method, mean_rank) in enumerate(rank_df[\"Mean Rank\"].items()):\n",
|
| 833 |
+
" fig.add_annotation(\n",
|
| 834 |
+
" x=len(display_df.columns) - 0.3,\n",
|
| 835 |
+
" y=method,\n",
|
| 836 |
+
" text=f\" Avg: {mean_rank:.2f}\",\n",
|
| 837 |
+
" showarrow=False,\n",
|
| 838 |
+
" xanchor=\"left\",\n",
|
| 839 |
+
" font=dict(size=11, color=\"black\"),\n",
|
| 840 |
+
" )\n",
|
| 841 |
+
"\n",
|
| 842 |
+
" fig.update_layout(\n",
|
| 843 |
+
" template=\"plotly_white\",\n",
|
| 844 |
+
" font=dict(family=\"Serif\", size=12),\n",
|
| 845 |
+
" width=900,\n",
|
| 846 |
+
" height=50 + 40 * len(display_df),\n",
|
| 847 |
+
" title=f\"Method Ranking Heatmap -- {space_label} Input\",\n",
|
| 848 |
+
" margin=dict(l=200, r=120, t=50, b=60),\n",
|
| 849 |
+
" xaxis=dict(side=\"top\"),\n",
|
| 850 |
+
" yaxis=dict(autorange=\"reversed\"),\n",
|
| 851 |
+
" )\n",
|
| 852 |
+
" fig.show()"
|
| 853 |
+
]
|
| 854 |
+
},
|
| 855 |
+
{
|
| 856 |
+
"cell_type": "markdown",
|
| 857 |
+
"metadata": {},
|
| 858 |
+
"source": [
|
| 859 |
+
"---\n",
|
| 860 |
+
"## 10. Key Takeaways\n",
|
| 861 |
+
"\n",
|
| 862 |
+
"1. **Contrastive methods (TS2Vec, SoftCLT) excel at geological classification in multivariate mode.** TS2Vec multi achieves the highest LP BalAcc (0.489) for milieu_eh, a 70% relative improvement over the univariate case. The climate covariates provide geological signal that the groundwater level alone cannot capture.\n",
|
| 863 |
+
"\n",
|
| 864 |
+
"2. **MiniRocket dominates temporal dynamics.** For the data-driven dynamic typology (inertial/annual/reactive), MiniRocket +W (uni) reaches BalAcc = 0.762, outperforming all contrastive methods by 15+ points. Random convolutional features are well suited to capturing frequency content and autocorrelation structure.\n",
|
| 865 |
+
"\n",
|
| 866 |
+
"3. **Dimensional collapse is the main failure mode of contrastive learning.** Raw TS2Vec and SoftCLT embeddings collapse onto 2--3 effective dimensions (out of 320), with uniformity near 0. This makes k-NN retrieval and distance-based metrics unreliable.\n",
|
| 867 |
+
"\n",
|
| 868 |
+
"4. **Post-hoc whitening (ZCA + PCA to 64d) consistently improves retrieval and spatial metrics.** P@5 gains of +6 to +8 points; Mantel r doubles or triples. Whitening resolves the dimensional collapse without requiring retraining.\n",
|
| 869 |
+
"\n",
|
| 870 |
+
"5. **No single encoder wins everywhere.** The best choice depends on the downstream task: TS2Vec (multi) for geology-aware embeddings, MiniRocket (uni) +W for temporal pattern classification, SoftCLT (multi) +W for the best overall retrieval and spatial coherence.\n",
|
| 871 |
+
"\n",
|
| 872 |
+
"6. **All learned methods substantially outperform the random baseline** (BalAcc 0.115), confirming that the embeddings capture meaningful structure. Even the simplest approach (PCA brut) outperforms random on most metrics."
|
| 873 |
+
]
|
| 874 |
+
}
|
| 875 |
+
],
|
| 876 |
+
"metadata": {
|
| 877 |
+
"kernelspec": {
|
| 878 |
+
"display_name": "Python 3",
|
| 879 |
+
"language": "python",
|
| 880 |
+
"name": "python3"
|
| 881 |
+
},
|
| 882 |
+
"language_info": {
|
| 883 |
+
"name": "python",
|
| 884 |
+
"version": "3.12.0"
|
| 885 |
+
}
|
| 886 |
+
},
|
| 887 |
+
"nbformat": 4,
|
| 888 |
+
"nbformat_minor": 4
|
| 889 |
+
}
|