#!/usr/bin/env python
"""Compare V8 and V11 lateral parameter convergence"""
import optuna
import statistics

# Load V8 study
v8_study = optuna.load_study(
    study_name='m1_mirror_cost_reduction_V8',
    storage='sqlite:///studies/M1_Mirror/m1_mirror_cost_reduction_V8/3_results/study.db'
)

# Load V11 study
v11_study = optuna.load_study(
    study_name='m1_mirror_cost_reduction_V11',
    storage='sqlite:///studies/M1_Mirror/m1_mirror_cost_reduction_V11/3_results/study.db'
)

print("="*70)
print("V8 BEST TRIAL (Z-only Zernike)")
print("="*70)
v8_best = v8_study.best_trial
print(f"Trial: {v8_best.number}")
print(f"WS: {v8_best.value:.2f}")
print("\nLateral Parameters:")
for k, v in sorted(v8_best.params.items()):
    print(f"  {k}: {v:.4f}")
print("\nObjectives:")
for k, v in v8_best.user_attrs.items():
    if isinstance(v, (int, float)):
        print(f"  {k}: {v:.4f}")

print("\n" + "="*70)
print("V11 BEST TRIAL (ZernikeOPD + extract_relative)")
print("="*70)
v11_best = v11_study.best_trial
print(f"Trial: {v11_best.number}")
print(f"WS: {v11_best.value:.2f}")
print("\nLateral Parameters:")
for k, v in sorted(v11_best.params.items()):
    print(f"  {k}: {v:.4f}")
print("\nObjectives:")
for k, v in v11_best.user_attrs.items():
    if isinstance(v, (int, float)):
        print(f"  {k}: {v:.4f}")

# Compare parameter ranges explored
print("\n" + "="*70)
print("PARAMETER EXPLORATION COMPARISON")
print("="*70)

params = ['lateral_inner_angle', 'lateral_outer_angle', 'lateral_outer_pivot',
          'lateral_inner_pivot', 'lateral_middle_pivot', 'lateral_closeness']

for p in params:
    v8_vals = [t.params.get(p) for t in v8_study.trials if t.state.name == 'COMPLETE' and p in t.params]
    v11_vals = [t.params.get(p) for t in v11_study.trials if t.state.name == 'COMPLETE' and p in t.params]

    if v8_vals and v11_vals:
        print(f"\n{p}:")
        print(f"  V8:  mean={statistics.mean(v8_vals):.2f}, std={statistics.stdev(v8_vals) if len(v8_vals) > 1 else 0:.2f}, range=[{min(v8_vals):.2f}, {max(v8_vals):.2f}]")
        print(f"  V11: mean={statistics.mean(v11_vals):.2f}, std={statistics.stdev(v11_vals) if len(v11_vals) > 1 else 0:.2f}, range=[{min(v11_vals):.2f}, {max(v11_vals):.2f}]")
        print(f"  Best V8:  {v8_best.params.get(p, 'N/A'):.2f}")
        print(f"  Best V11: {v11_best.params.get(p, 'N/A'):.2f}")

# Lateral displacement comparison (V11 has this data)
print("\n" + "="*70)
print("V11 LATERAL DISPLACEMENT DATA (not available in V8)")
print("="*70)
for t in v11_study.trials:
    if t.state.name == 'COMPLETE':
        lat_rms = t.user_attrs.get('lateral_rms_um', None)
        lat_max = t.user_attrs.get('lateral_max_um', None)
        if lat_rms is not None:
            print(f"Trial {t.number}: RMS={lat_rms:.2f} um, Max={lat_max:.2f} um, WS={t.value:.2f}")
