#!/usr/bin/env python3 """ Backfill mass_kg in results.json for all DOE iterations. Reads _temp_mass.txt (key=value format) and patches results.json. """ import json from pathlib import Path ITER_DIR = Path("/home/papa/atomizer/projects/hydrotech-beam/studies/01_doe_landscape/iterations") fixed = 0 skipped = 0 errors = [] for iter_path in sorted(ITER_DIR.iterdir()): if not iter_path.is_dir() or not iter_path.name.startswith("iter"): continue mass_file = iter_path / "_temp_mass.txt" results_file = iter_path / "results.json" if not mass_file.exists(): errors.append(f"{iter_path.name}: no _temp_mass.txt") continue if not results_file.exists(): errors.append(f"{iter_path.name}: no results.json") continue # Parse mass content = mass_file.read_text().strip() if '=' in content: content = content.split('=', 1)[1] try: mass_kg = float(content) except ValueError: errors.append(f"{iter_path.name}: unparseable mass: {content!r}") continue # Patch results.json with open(results_file) as f: results = json.load(f) old_mass = results.get("mass_kg") results["mass_kg"] = mass_kg with open(results_file, 'w') as f: json.dump(results, f, indent=2) status = "NaN→fixed" if (old_mass is None or old_mass != old_mass) else f"{old_mass}→{mass_kg}" print(f" {iter_path.name}: mass_kg = {mass_kg:.4f} kg ({status})") fixed += 1 print(f"\n✅ Backfilled {fixed} iterations") if skipped: print(f"⏭️ Skipped {skipped}") if errors: print(f"❌ Errors: {len(errors)}") for e in errors: print(f" {e}")