This commit is contained in:
Felix Delattre 2026-05-31 15:54:05 +02:00
parent e3af4bf2f4
commit bfd5d73dff
6 changed files with 760 additions and 61 deletions

View file

@ -7,7 +7,6 @@ import json
import re
from pathlib import Path
from gap_validation.calendar import DEFAULT_GAP_LENGTHS, TRANSITIONS
from gap_validation.run import run_validation
# Primary season per site (matches scripts/export_thesis_tables.py).
@ -47,26 +46,49 @@ def _parse_scenario(key: str) -> tuple[str, int | None, str]:
return strategy, sigma if sigma == 30 else (None if sigma == 20 else sigma), mode
def _best_bti_from_metrics(metrics_path: Path) -> str | None:
def _best_from_metrics(metrics_path: Path, workflow: str) -> str | None:
"""Best scenario key (max no-gap NSE_PC) for ``workflow`` (``bti`` or ``itb``)."""
if workflow not in ("bti", "itb"):
raise ValueError(f"workflow must be bti or itb, got {workflow!r}")
if not metrics_path.is_file():
return None
temporal = json.loads(metrics_path.read_text(encoding="utf-8")).get("temporal") or {}
want_itb = workflow == "itb"
best_key, best_nse = None, None
for k, v in temporal.items():
if not k.endswith("_itb") and isinstance(v, dict):
n = v.get("nse_pc")
if isinstance(n, (int, float)) and (best_nse is None or n > best_nse):
best_nse = n
best_key = k
if k.endswith("_itb") != want_itb or not isinstance(v, dict):
continue
n = v.get("nse_pc")
if isinstance(n, (int, float)) and (best_nse is None or n > best_nse):
best_nse = n
best_key = k
return best_key
def _best_bti_from_metrics(metrics_path: Path) -> str | None:
return _best_from_metrics(metrics_path, "bti")
def _best_itb_from_metrics(metrics_path: Path) -> str | None:
return _best_from_metrics(metrics_path, "itb")
def _resolve_workflows(workflow: str) -> tuple[str, ...]:
return ("bti", "itb") if workflow == "both" else (workflow,)
def main() -> None:
ap = argparse.ArgumentParser(description="Batch spatial gap validation (six sites).")
ap.add_argument("--data-dir", type=Path, default=Path("data"))
ap.add_argument("--sites-geojson", type=Path, default=Path("data/sites.geojson"))
ap.add_argument("--skip-fusion", action="store_true")
ap.add_argument("--write-manifest-only", action="store_true")
ap.add_argument(
"--workflow",
choices=["bti", "itb", "both"],
default="both",
help="Fusion workflow(s) to validate (default: both best BtI and best ItB).",
)
ap.add_argument(
"--gap-days",
type=int,
@ -76,6 +98,7 @@ def main() -> None:
args = ap.parse_args()
positions = _site_positions(args.sites_geojson)
gap_filter = args.gap_days
workflows = _resolve_workflows(args.workflow)
for site, season in sorted(PRIMARY_SEASON.items()):
pos = positions.get(site)
@ -83,28 +106,29 @@ def main() -> None:
print(f"[skip] No coordinates for {site}")
continue
metrics_path = args.data_dir / site / str(season) / "metrics.json"
scenario_key = _best_bti_from_metrics(metrics_path)
if not scenario_key:
print(f"[skip] {site} {season}: no metrics.json / BtI scenarios")
continue
strategy, sigma, mode = _parse_scenario(scenario_key)
sigma_kw = 30 if sigma == 30 else None
print(f"=== {site} {season} {scenario_key} ===")
out = run_validation(
site,
season,
pos,
strategy,
sigma_kw,
mode,
skip_manifest=False,
skip_fusion=args.skip_fusion,
write_manifest_only=args.write_manifest_only,
gap_days_filter=gap_filter,
transition_filter=None,
s2_calendar_strategy=strategy,
)
print(out)
for workflow in workflows:
scenario_key = _best_from_metrics(metrics_path, workflow)
if not scenario_key:
print(f"[skip] {site} {season}: no metrics.json / {workflow} scenarios")
continue
strategy, sigma, mode = _parse_scenario(scenario_key)
sigma_kw = 30 if sigma == 30 else None
print(f"=== {site} {season} {scenario_key} ===")
out = run_validation(
site,
season,
pos,
strategy,
sigma_kw,
mode,
skip_manifest=False,
skip_fusion=args.skip_fusion,
write_manifest_only=args.write_manifest_only,
gap_days_filter=gap_filter,
transition_filter=None,
s2_calendar_strategy=strategy,
)
print(out)
if __name__ == "__main__":