File size: 16,061 Bytes
87904b0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Correlations Example\n",
"\n",
"Clean, reproducible correlation analysis for Hyperview, DAT, Intuition-1, and EnMAP submissions.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How To Use\n",
"\n",
"1. Update paths in the configuration cells below.\n",
"2. Run cells from top to bottom.\n",
"3. The notebook will generate:\n",
" - `metrics.xlsx` (aggregated `all_metrics.json` from run folders),\n",
" - `correlation_results.xlsx` (cross-split PLCC/SRCC/RMSE + custom score).\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import json\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"from openpyxl import Workbook\n",
"from scipy.stats import pearsonr, spearmanr\n",
"from sklearn.metrics import mean_squared_error\n",
"\n",
"\n",
"# Shared utility functions used by all sections.\n",
"def compute_plcc_srcc_rmse(a: np.ndarray, b: np.ndarray) -> tuple[float, float, float]:\n",
" \"\"\"Return PLCC, SRCC, and RMSE for two equally sized arrays.\"\"\"\n",
" plcc = pearsonr(a, b)[0]\n",
" srcc = spearmanr(a, b)[0]\n",
" rmse = float(np.sqrt(mean_squared_error(a, b)))\n",
" return plcc, srcc, rmse\n",
"\n",
"\n",
"def load_submission_flat(csv_path: Path) -> np.ndarray | None:\n",
" \"\"\"Load submission CSV, drop `sample_index` if present, and flatten to 1D.\"\"\"\n",
" if not csv_path.exists():\n",
" return None\n",
" return pd.read_csv(csv_path).drop(columns=['sample_index'], errors='ignore').values.flatten()\n",
"\n",
"\n",
"def load_custom_score(json_path: Path) -> float | str:\n",
" \"\"\"Load `custom` metric from JSON file; return 'N/A' if missing.\"\"\"\n",
" if not json_path.exists():\n",
" return 'N/A'\n",
" with json_path.open('r', encoding='utf-8') as f:\n",
" return json.load(f).get('custom', 'N/A')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1) Export Metrics Workbook\n",
"\n",
"Collect `all_metrics.json` from each run directory and export grouped sheets (`P`, `K`, `Mg`, `pH`, `stats`).\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved metrics workbook to: /mnt/d/new_runs_decoder_v2/metrics.xlsx\n",
"Number of runs exported: 43\n"
]
}
],
"source": [
"# Configure source directory with run folders and output Excel path.\n",
"RUNS_DIR = Path('/mnt/d/new_runs_decoder_v2')\n",
"OUTPUT_METRICS_XLSX = RUNS_DIR / 'metrics.xlsx'\n",
"\n",
"# Columns to export per worksheet.\n",
"SHEETS_DEFINITIONS = {\n",
" 'P': [\n",
" 'run_name',\n",
" 'P_avg_acc', 'P_acc', 'P_f1', 'P_mcc', 'P_kappa',\n",
" 'P_r2', 'P_mse', 'P_mae',\n",
" ],\n",
" 'K': [\n",
" 'run_name',\n",
" 'K_avg_acc', 'K_acc', 'K_f1', 'K_mcc', 'K_kappa',\n",
" 'K_r2', 'K_mse', 'K_mae',\n",
" ],\n",
" 'Mg': [\n",
" 'run_name',\n",
" 'Mg_avg_acc', 'Mg_acc', 'Mg_f1', 'Mg_mcc', 'Mg_kappa',\n",
" 'Mg_r2', 'Mg_mse', 'Mg_mae',\n",
" ],\n",
" 'pH': [\n",
" 'run_name',\n",
" 'pH_avg_acc', 'pH_acc', 'pH_f1', 'pH_mcc', 'pH_kappa',\n",
" 'pH_r2', 'pH_mse', 'pH_mae',\n",
" ],\n",
" 'stats': [\n",
" 'run_name',\n",
" 'mean_avg_acc', 'std_avg_acc',\n",
" 'mean_acc', 'std_acc',\n",
" 'mean_mcc', 'std_mcc',\n",
" 'mean_f1', 'std_f1',\n",
" 'P_score', 'K_score', 'Mg_score', 'pH_score',\n",
" 'custom',\n",
" ],\n",
"}\n",
"\n",
"\n",
"def export_metrics_workbook(runs_dir: Path, output_xlsx: Path) -> int:\n",
" \"\"\"Scan run folders and export JSON metrics to a multi-sheet Excel workbook.\"\"\"\n",
" rows: list[dict] = []\n",
"\n",
" for run_path in sorted(runs_dir.iterdir()):\n",
" if not run_path.is_dir():\n",
" continue\n",
" metrics_path = run_path / 'all_metrics.json'\n",
" if not metrics_path.exists():\n",
" continue\n",
"\n",
" with metrics_path.open('r', encoding='utf-8') as f:\n",
" metrics = json.load(f)\n",
"\n",
" row = {'run_name': run_path.name}\n",
" row.update(metrics)\n",
" rows.append(row)\n",
"\n",
" wb = Workbook()\n",
" wb.remove(wb.active)\n",
"\n",
" for sheet_name, columns in SHEETS_DEFINITIONS.items():\n",
" ws = wb.create_sheet(title=sheet_name)\n",
" ws.append(columns)\n",
" for row in rows:\n",
" ws.append([row.get(col, '') for col in columns])\n",
"\n",
" wb.save(output_xlsx)\n",
" return len(rows)\n",
"\n",
"\n",
"n_runs = export_metrics_workbook(RUNS_DIR, OUTPUT_METRICS_XLSX)\n",
"print(f'Saved metrics workbook to: {OUTPUT_METRICS_XLSX}')\n",
"print(f'Number of runs exported: {n_runs}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2) Correlation Study Across Splits\n",
"\n",
"Compute PLCC/SRCC/RMSE and attach `custom` score for selected model directories.\n",
"\n",
"Comparisons included:\n",
"- Hyperview submission vs Hyperview GT,\n",
"- Intuition submission vs Hyperview GT,\n",
"- DAT submission vs EnMAP GT,\n",
"- Intuition vs Hyperview submission,\n",
"- EnMAP (per AOI) vs EnMAP GT,\n",
"- EnMAP (per AOI) vs DAT submission.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processing: /mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_base_decoder_name_UperNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_64_epochs_100_lr_5e-05\n",
"Processing: /mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_base_decoder_name_UNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_64_epochs_100_lr_5e-05\n",
"Processing: /mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_large_decoder_name_UperNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_32_epochs_100_lr_5e-05\n",
"Processing: /mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_large_decoder_name_UNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_32_epochs_100_lr_5e-05\n",
"Saved correlation workbook to: /home/jsadel/fastEO_uc4/correlation_results.xlsx\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>model</th>\n",
" <th>hyperview_plcc</th>\n",
" <th>hyperview_srcc</th>\n",
" <th>dat_plcc</th>\n",
" <th>dat_srcc</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>backbone_name_terramind_v1_base_decoder_name_U...</td>\n",
" <td>0.922376</td>\n",
" <td>0.934178</td>\n",
" <td>0.946735</td>\n",
" <td>0.958205</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>backbone_name_terramind_v1_base_decoder_name_U...</td>\n",
" <td>0.923012</td>\n",
" <td>0.932405</td>\n",
" <td>0.946247</td>\n",
" <td>0.955108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>backbone_name_terramind_v1_large_decoder_name_...</td>\n",
" <td>0.915983</td>\n",
" <td>0.926709</td>\n",
" <td>0.936536</td>\n",
" <td>0.945435</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>backbone_name_terramind_v1_large_decoder_name_...</td>\n",
" <td>0.918901</td>\n",
" <td>0.930218</td>\n",
" <td>0.940773</td>\n",
" <td>0.952539</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" model hyperview_plcc \\\n",
"0 backbone_name_terramind_v1_base_decoder_name_U... 0.922376 \n",
"1 backbone_name_terramind_v1_base_decoder_name_U... 0.923012 \n",
"2 backbone_name_terramind_v1_large_decoder_name_... 0.915983 \n",
"3 backbone_name_terramind_v1_large_decoder_name_... 0.918901 \n",
"\n",
" hyperview_srcc dat_plcc dat_srcc \n",
"0 0.934178 0.946735 0.958205 \n",
"1 0.932405 0.946247 0.955108 \n",
"2 0.926709 0.936536 0.945435 \n",
"3 0.930218 0.940773 0.952539 "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Input GT paths.\n",
"HYPERVIEW_GT_PATH = Path('/home/jsadel/fast_eo/hyperview_data/test_gt.csv')\n",
"ENMAP_GT_PATH = Path('/home/jsadel/fast_eo/hyperview_data/enmap_test_gt.csv')\n",
"\n",
"# Select model directories to compare.\n",
"MODEL_DIRS = [\n",
" Path('/mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_base_decoder_name_UperNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_64_epochs_100_lr_5e-05'),\n",
" Path('/mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_base_decoder_name_UNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_64_epochs_100_lr_5e-05'),\n",
" Path('/mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_large_decoder_name_UperNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_32_epochs_100_lr_5e-05'),\n",
" Path('/mnt/d/new_runs_decoder_v2/backbone_name_terramind_v1_large_decoder_name_UNetDecoder_output_size_4_mask_none_fill_last_with_150_True_target_scaling_std_batch_size_32_epochs_100_lr_5e-05'),\n",
"]\n",
"\n",
"# EnMAP AOIs (one submission file per AOI).\n",
"AOIS = [\n",
" '20231106T103216Z',\n",
" '20231109T101043Z',\n",
" '20231117T101736Z',\n",
" '20240324T103513Z',\n",
" '20240331T101712Z',\n",
" '20240427T101706Z',\n",
" '20240501T102037Z',\n",
" '20240505T102406Z',\n",
" '20240702T102823Z',\n",
"]\n",
"\n",
"CORRELATION_OUTPUT_XLSX = Path('correlation_results.xlsx')\n",
"\n",
"# Load GT arrays once and flatten values.\n",
"hyperview_gt = pd.read_csv(HYPERVIEW_GT_PATH).iloc[:, 1:].values.flatten()\n",
"enmap_gt = pd.read_csv(ENMAP_GT_PATH).iloc[:, 1:].values.flatten()\n",
"\n",
"summary_rows: list[dict] = []\n",
"\n",
"with pd.ExcelWriter(CORRELATION_OUTPUT_XLSX) as writer:\n",
" for idx, model_dir in enumerate(MODEL_DIRS, 1):\n",
" print(f'Processing: {model_dir}')\n",
"\n",
" dat_pred = load_submission_flat(model_dir / 'test_dat_submission.csv')\n",
" hv_pred = load_submission_flat(model_dir / 'submission.csv')\n",
" i1_pred = load_submission_flat(model_dir / 'test_intuition_submission.csv')\n",
"\n",
" rows: list[list] = []\n",
"\n",
" def maybe_metric(metric_name: str, ref: np.ndarray | None, pred: np.ndarray | None, score: float | str = 'N/A') -> None:\n",
" if ref is None or pred is None:\n",
" rows.append([metric_name, 'N/A', 'N/A', 'N/A', score])\n",
" return\n",
" plcc, srcc, rmse = compute_plcc_srcc_rmse(pred, ref)\n",
" rows.append([metric_name, plcc, srcc, rmse, score])\n",
"\n",
" maybe_metric(\n",
" 'Hyperview vs Hyperview GT',\n",
" hyperview_gt,\n",
" hv_pred,\n",
" load_custom_score(model_dir / 'all_metrics.json'),\n",
" )\n",
" maybe_metric(\n",
" 'Intuition vs Hyperview GT',\n",
" hyperview_gt,\n",
" i1_pred,\n",
" load_custom_score(model_dir / 'test_intuition_all_metrics.json'),\n",
" )\n",
" maybe_metric(\n",
" 'DAT vs EnMAP GT',\n",
" enmap_gt,\n",
" dat_pred,\n",
" load_custom_score(model_dir / 'test_dat_all_metrics.json'),\n",
" )\n",
" maybe_metric('Intuition vs Hyperview Submission', hv_pred, i1_pred)\n",
"\n",
" for aoi in AOIS:\n",
" enmap_pred = load_submission_flat(model_dir / f'test_enmap_{aoi}_submission.csv')\n",
" enmap_score = load_custom_score(model_dir / f'test_enmap_{aoi}_all_metrics.json')\n",
"\n",
" maybe_metric(f'EnMAP {aoi} vs EnMAP GT', enmap_gt, enmap_pred, enmap_score)\n",
" maybe_metric(f'EnMAP {aoi} vs DAT', dat_pred, enmap_pred)\n",
"\n",
" df = pd.DataFrame(rows, columns=['Metric', 'PLCC', 'SRCC', 'RMSE', 'Score'])\n",
" df.to_excel(writer, sheet_name=f'Model_{idx:02d}', index=False)\n",
"\n",
" # Compact summary row for quick comparison across models.\n",
" hv_row = df[df['Metric'] == 'Hyperview vs Hyperview GT'].head(1)\n",
" dat_row = df[df['Metric'] == 'DAT vs EnMAP GT'].head(1)\n",
" summary_rows.append(\n",
" {\n",
" 'model': model_dir.name,\n",
" 'hyperview_plcc': hv_row['PLCC'].iloc[0] if not hv_row.empty else 'N/A',\n",
" 'hyperview_srcc': hv_row['SRCC'].iloc[0] if not hv_row.empty else 'N/A',\n",
" 'dat_plcc': dat_row['PLCC'].iloc[0] if not dat_row.empty else 'N/A',\n",
" 'dat_srcc': dat_row['SRCC'].iloc[0] if not dat_row.empty else 'N/A',\n",
" }\n",
" )\n",
"\n",
" pd.DataFrame(summary_rows).to_excel(writer, sheet_name='Summary', index=False)\n",
"\n",
"print(f'Saved correlation workbook to: {CORRELATION_OUTPUT_XLSX.resolve()}')\n",
"pd.DataFrame(summary_rows)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Notes\n",
"\n",
"- This notebook keeps the original workflow but removes duplicated cells.\n",
"- To test another experiment group, only edit `MODEL_DIRS` and rerun the last section.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "fastEO",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|