File size: 15,912 Bytes
7da2ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run object-based validation for several model thresholds."""

from __future__ import annotations

import argparse
import csv
import json
import os
from copy import deepcopy
from pathlib import Path
from typing import Any

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt

from .clusterers import create_clusterer
from .loaders import CloudLabelLoader, ValidationJsonLoader, create_prediction_provider
from .metrics import compute_scores
from .threshold_sweep import ThresholdSweepValidator
from .utils import expand_modes
from .config import load_config as load_release_config


DEFAULT_THRESHOLDS = [round(0.1 * idx, 1) for idx in range(1, 10)]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Threshold-sweep object-based validation")
    parser.add_argument("--config", default="config_threshold_sweep.yaml", help="Path to YAML config")
    parser.add_argument("--data-source", choices=["Model"], default=None)
    parser.add_argument("--device", default=None, help="Accepted for a common CLI; validation runs on CPU")
    parser.add_argument("--dates", nargs="*", default=None, help="Override dates, e.g. 2024 2025")
    parser.add_argument("--target-filters", nargs="*", choices=["true_only", "all", "both"], default=None)
    parser.add_argument("--leadtime-modes", nargs="*", choices=["exact", "accumulate", "both"], default=None)
    parser.add_argument("--thresholds", nargs="*", type=float, default=None, help="Override thresholds, e.g. 0.1 0.3 0.5")
    parser.add_argument("--max-cases", type=int, default=None, help="Override validation.max_cases")
    parser.add_argument("--output-dir", default=None, help="Override paths.output_dir")
    return parser.parse_args()


def apply_overrides(config: dict[str, Any], args: argparse.Namespace) -> dict[str, Any]:
    config = deepcopy(config)
    config.setdefault("validation", {})
    config.setdefault("paths", {})
    config.setdefault("threshold_sweep", {})

    if args.data_source is not None:
        config["data_source"] = args.data_source
    if args.dates is not None and len(args.dates) > 0:
        config["dates"] = args.dates
    if args.target_filters is not None and len(args.target_filters) > 0:
        config["validation"]["target_filters"] = args.target_filters
    if args.leadtime_modes is not None and len(args.leadtime_modes) > 0:
        config["validation"]["leadtime_modes"] = args.leadtime_modes
    if args.thresholds is not None and len(args.thresholds) > 0:
        config["threshold_sweep"]["thresholds"] = args.thresholds
    if args.max_cases is not None:
        config["validation"]["max_cases"] = args.max_cases
    if args.output_dir is not None:
        config["paths"]["output_dir"] = args.output_dir
    return config


def json_default(obj: Any) -> Any:
    if hasattr(obj, "item"):
        return obj.item()
    return str(obj)


def write_json(data: Any, path: Path) -> None:
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2, ensure_ascii=False, default=json_default)


def csv_value(value: Any) -> Any:
    if isinstance(value, list):
        return ";".join(str(item) for item in value)
    if isinstance(value, dict):
        return json.dumps(value, ensure_ascii=False)
    return value


def write_csv(records: list[dict[str, Any]], path: Path) -> None:
    if not records:
        path.write_text("", encoding="utf-8")
        return

    fieldnames: list[str] = []
    for record in records:
        for key in record:
            if key not in fieldnames:
                fieldnames.append(key)

    with open(path, "w", encoding="utf-8", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        for record in records:
            writer.writerow({key: csv_value(record.get(key)) for key in fieldnames})


def threshold_values(config: dict[str, Any]) -> list[float]:
    values = (config.get("threshold_sweep") or {}).get("thresholds", DEFAULT_THRESHOLDS)
    thresholds = sorted({round(float(value), 10) for value in values})
    if not thresholds:
        raise ValueError("No thresholds configured")
    return thresholds


def threshold_dir_name(threshold: float) -> str:
    return f"threshold_{threshold:g}".replace(".", "p")


def release_relative_path(value: str | Path, config: dict[str, Any]) -> str:
    path = Path(value)
    config_path = Path(config["_config_path"])
    repository_root = config_path.parents[3]
    try:
        return path.resolve().relative_to(repository_root).as_posix()
    except ValueError:
        return path.name


def create_threshold_clusterers(config: dict[str, Any], thresholds: list[float]):
    clusterers = {}
    for threshold in thresholds:
        threshold_config = deepcopy(config)
        threshold_config.setdefault("clusterer", {})
        threshold_config["clusterer"]["threshold"] = float(threshold)
        clusterers[float(threshold)] = create_clusterer(threshold_config)
    return clusterers


def combo_output_dir(config: dict[str, Any], date_str: str, target_filter: str, leadtime_mode: str) -> Path:
    del target_filter, leadtime_mode
    return Path(config["paths"]["output_dir"]) / str(config["data_source"]) / str(date_str)


def summary_csv_row(threshold: float, summary: dict[str, Any]) -> dict[str, Any]:
    total = summary["total"]
    return {
        "threshold": float(threshold),
        "hits": total["hits"],
        "misses": total["misses"],
        "falses": total["falses"],
        "POD": total["POD"],
        "FAR": total["FAR"],
        "F1": total["F1"],
        "CSI": total["CSI"],
        "valid_labels": total["valid_labels"],
        "impossible": total["impossible"],
        "total_input_labels": total["total_input_labels"],
        "model_hit_clusters": total["model_hit_clusters"],
        "false_model_clusters": total["false_model_clusters"],
        "total_model_clusters": total["total_model_clusters"],
    }


def apply_report_leadtime_range(
    summary: dict[str, Any],
    label_records: list[dict[str, Any]],
    model_cluster_records: list[dict[str, Any]],
    report_min: int,
    report_max: int,
) -> dict[str, Any]:
    """Report a lead-time subset after accumulation over the full evaluation range."""
    selected_leadtimes = {
        int(leadtime): values
        for leadtime, values in summary["leadtime"].items()
        if report_min <= int(leadtime) <= report_max
    }
    if not selected_leadtimes:
        raise ValueError(f"No lead-time results in reporting range {report_min}..{report_max}")

    hits = sum(int(values["hits"]) for values in selected_leadtimes.values())
    misses = sum(int(values["misses"]) for values in selected_leadtimes.values())
    falses = sum(int(values["falses"]) for values in selected_leadtimes.values())
    impossible = sum(int(values["impossible"]) for values in selected_leadtimes.values())
    eligible_cloud_ids = {
        str(record["cloud_id"])
        for record in label_records
        if report_min <= int(record["leadtime"]) <= report_max
    }
    model_hits = sum(
        1
        for record in model_cluster_records
        if not record["is_false"]
        and any(str(cloud_id) in eligible_cloud_ids for cloud_id in record.get("matched_cloud_ids", []))
    )

    report_total = compute_scores(hits, misses, falses)
    report_total.update(
        {
            "valid_labels": int(hits + misses),
            "impossible": int(impossible),
            "total_input_labels": int(hits + misses + impossible),
            "model_hit_clusters": int(model_hits),
            "false_model_clusters": int(falses),
            "total_model_clusters": int(model_hits + falses),
        }
    )
    summary["evaluation_total"] = summary["total"]
    summary["total"] = report_total
    summary["leadtime"] = selected_leadtimes
    summary["report_leadtime_range"] = {"min": int(report_min), "max": int(report_max)}
    return summary


def plot_pod_far(rows: list[dict[str, Any]], output_path: Path) -> None:
    fig, ax = plt.subplots(figsize=(6.5, 5.5), constrained_layout=True)
    valid_rows = [row for row in rows if row.get("POD") is not None and row.get("FAR") is not None]
    if valid_rows:
        far = [float(row["FAR"]) for row in valid_rows]
        pod = [float(row["POD"]) for row in valid_rows]
        thresholds = [float(row["threshold"]) for row in valid_rows]
        ax.plot(far, pod, marker="o", linewidth=1.8)
        for x, y, threshold in zip(far, pod, thresholds):
            ax.annotate(f"{threshold:g}", (x, y), textcoords="offset points", xytext=(5, 5), fontsize=8)
    else:
        ax.text(0.5, 0.5, "No valid POD/FAR points", ha="center", va="center", transform=ax.transAxes)

    ax.set_xlabel("FAR")
    ax.set_ylabel("POD")
    ax.set_title("POD-FAR Threshold Curve")
    ax.set_xlim(-0.02, 1.02)
    ax.set_ylim(-0.02, 1.02)
    ax.grid(True, linestyle=":", linewidth=0.6, alpha=0.7)
    fig.savefig(output_path, dpi=150)
    plt.close(fig)


def write_details(
    out_dir: Path,
    threshold: float,
    label_records: list[dict[str, Any]],
    model_cluster_records: list[dict[str, Any]],
    missing_predictions: list[dict[str, Any]],
) -> None:
    detail_dir = out_dir / "details" / threshold_dir_name(threshold)
    detail_dir.mkdir(parents=True, exist_ok=True)
    write_json(label_records, detail_dir / "label_results.json")
    write_json(model_cluster_records, detail_dir / "model_cluster_results.json")
    write_json(missing_predictions, detail_dir / "missing_predictions.json")
    write_csv(label_records, detail_dir / "label_results.csv")
    write_csv(model_cluster_records, detail_dir / "model_cluster_results.csv")


def run_for_date_and_filter(config: dict[str, Any], date_str: str, target_filter: str) -> dict[str, Any]:
    validation_config = config["validation"]
    sweep_config = config.get("threshold_sweep") or {}
    json_path = config["paths"]["validation_json_template"].format(date=date_str)
    thresholds = threshold_values(config)

    targets = ValidationJsonLoader(json_path).load_targets(
        target_filter=target_filter,
        leadtime_min=int(validation_config.get("leadtime_min", 10)),
        leadtime_max=int(validation_config.get("leadtime_max", 120)),
        max_cases=validation_config.get("max_cases"),
    )

    label_loader = CloudLabelLoader(config["paths"]["temporal_overlapping_dir"])
    provider = create_prediction_provider(config)
    clusterers = create_threshold_clusterers(config, thresholds)
    matching_config = config.get("matching") or {}
    performance_config = config.get("performance") or {}

    validator = ThresholdSweepValidator(
        targets=targets,
        label_loader=label_loader,
        prediction_provider=provider,
        clusterers=clusterers,
        pixel_size_km=float(matching_config.get("pixel_size_km", 2.0)),
        label_buffer_km=float(matching_config.get("label_buffer_km", 0.0)),
        model_false_buffer_km=float(matching_config.get("model_false_buffer_km", 0.0)),
        leadtime_min=int(validation_config.get("leadtime_min", 10)),
        leadtime_max=int(validation_config.get("leadtime_max", 120)),
        time_step=int(validation_config.get("time_step", 10)),
        buffer_backend=str(performance_config.get("buffer_backend", "auto")),
    )

    sweep_result = validator.evaluate_raw()
    leadtime_modes = expand_modes(validation_config.get("leadtime_modes", ["exact"]), ("exact", "accumulate"))
    if len(leadtime_modes) != 1:
        raise ValueError("The public output layout requires exactly one lead-time mode")
    save_details = bool(sweep_config.get("save_details", False))
    mode_summaries: dict[str, Any] = {}

    for leadtime_mode in leadtime_modes:
        out_dir = combo_output_dir(config, date_str, target_filter, leadtime_mode)
        out_dir.mkdir(parents=True, exist_ok=True)

        summary_items: list[dict[str, Any]] = []
        csv_rows: list[dict[str, Any]] = []
        for threshold in thresholds:
            raw_result = sweep_result.raw_results[threshold]
            label_records = validator.apply_leadtime_mode(raw_result.label_records, leadtime_mode)
            summary = validator.summarize(label_records, raw_result.model_cluster_records)
            report_min = int(
                validation_config.get("report_leadtime_min", validation_config.get("leadtime_min", 10))
            )
            report_max = int(
                validation_config.get("report_leadtime_max", validation_config.get("leadtime_max", 120))
            )
            summary = apply_report_leadtime_range(
                summary,
                label_records,
                raw_result.model_cluster_records,
                report_min,
                report_max,
            )
            summary.update(
                {
                    "date": date_str,
                    "data_source": config["data_source"],
                    "target_filter": target_filter,
                    "leadtime_mode": leadtime_mode,
                    "threshold": float(threshold),
                    "validation_json_path": release_relative_path(json_path, config),
                    "num_loaded_targets": len(targets),
                    "num_report_targets": summary["total"]["total_input_labels"],
                    "clusterer": dict(config.get("clusterer", {}), threshold=float(threshold)),
                    "matching": config.get("matching", {}),
                }
            )
            summary_items.append(summary)
            csv_rows.append(summary_csv_row(threshold, summary))

            if save_details:
                write_details(
                    out_dir=out_dir,
                    threshold=threshold,
                    label_records=label_records,
                    model_cluster_records=raw_result.model_cluster_records,
                    missing_predictions=raw_result.missing_predictions,
                )

            total = summary["total"]
            print(
                f"[{date_str}][{config['data_source']}][{target_filter}][{leadtime_mode}] "
                f"threshold={threshold:g} POD={total['POD']} FAR={total['FAR']} "
                f"F1={total['F1']} CSI={total['CSI']} H={total['hits']} "
                f"M={total['misses']} F={total['falses']} impossible={total['impossible']}"
            )

        payload = {
            "date": date_str,
            "data_source": config["data_source"],
            "target_filter": target_filter,
            "leadtime_mode": leadtime_mode,
            "thresholds": thresholds,
            "summaries": summary_items,
        }
        write_json(payload, out_dir / "threshold_summary.json")
        write_csv(csv_rows, out_dir / "threshold_summary.csv")
        plot_pod_far(csv_rows, out_dir / "pod_far_curve.png")
        mode_summaries[leadtime_mode] = payload

    return mode_summaries


def main() -> None:
    args = parse_args()
    config_path = Path(args.config)
    if not config_path.is_absolute():
        config_path = Path(os.getcwd()) / config_path
    config = apply_overrides(load_release_config(config_path), args)

    target_filters = expand_modes(
        config["validation"].get("target_filters", ["true_only"]),
        ("true_only", "all"),
    )
    if len(target_filters) != 1:
        raise ValueError("The public output layout requires exactly one target filter")
    dates = [str(date) for date in config.get("dates", [])]
    if not dates:
        raise ValueError("No dates configured")

    for date_str in dates:
        for target_filter in target_filters:
            run_for_date_and_filter(config, date_str, target_filter)

    output_root = Path(config["paths"]["output_dir"]) / str(config["data_source"])
    print(f"Validation summaries saved under {output_root}")


if __name__ == "__main__":
    main()