|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import jax.numpy as jnp |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from rubix.core.data import Galaxy, GasData, RubixData, StarsData |
| 11 | + |
| 12 | +from .optimize import optimize_ifu_cube |
| 13 | +from .posterior_predictive import ( |
| 14 | + compute_residual_products, |
| 15 | + sample_posterior_predictive_cubes, |
| 16 | + summarize_masked_metrics, |
| 17 | + summarize_predictive_cube_samples, |
| 18 | +) |
| 19 | +from .variational import optimize_variational_ifu_cube |
| 20 | + |
| 21 | + |
| 22 | +class SyntheticScalePipeline: |
| 23 | + """Simple synthetic IFU pipeline used by the science recipe workflow.""" |
| 24 | + |
| 25 | + def __init__(self, template: jnp.ndarray): |
| 26 | + self.template = template |
| 27 | + |
| 28 | + def run_sharded(self, rubixdata: RubixData) -> jnp.ndarray: |
| 29 | + scale = rubixdata.stars.age[0] |
| 30 | + return scale * self.template |
| 31 | + |
| 32 | + |
| 33 | +def _make_static_data() -> RubixData: |
| 34 | + return RubixData( |
| 35 | + galaxy=Galaxy(), |
| 36 | + stars=StarsData( |
| 37 | + coords=jnp.zeros((1, 3)), |
| 38 | + velocity=jnp.zeros((1, 3)), |
| 39 | + mass=jnp.ones(1), |
| 40 | + age=jnp.array([0.0]), |
| 41 | + metallicity=jnp.array([0.01]), |
| 42 | + ), |
| 43 | + gas=GasData( |
| 44 | + coords=jnp.zeros((1, 3)), |
| 45 | + velocity=jnp.zeros((1, 3)), |
| 46 | + mass=jnp.ones(1), |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _to_jsonable(value: Any) -> Any: |
| 52 | + """Convert nested JAX/NumPy containers into JSON-serializable values.""" |
| 53 | + if isinstance(value, dict): |
| 54 | + return {k: _to_jsonable(v) for k, v in value.items()} |
| 55 | + if isinstance(value, (list, tuple)): |
| 56 | + return [_to_jsonable(v) for v in value] |
| 57 | + if isinstance(value, np.ndarray): |
| 58 | + if value.ndim == 0: |
| 59 | + return value.item() |
| 60 | + return value.tolist() |
| 61 | + if isinstance(value, jnp.ndarray): |
| 62 | + arr = np.asarray(value) |
| 63 | + if arr.ndim == 0: |
| 64 | + return arr.item() |
| 65 | + return arr.tolist() |
| 66 | + if isinstance(value, np.generic): |
| 67 | + return value.item() |
| 68 | + return value |
| 69 | + |
| 70 | + |
| 71 | +def run_synthetic_science_recipe( |
| 72 | + cube_shape: tuple[int, int, int] = (4, 4, 16), |
| 73 | + target_scale: float = 1.7, |
| 74 | + optimize_steps: int = 120, |
| 75 | + vi_steps: int = 120, |
| 76 | + num_vi_samples: int = 4, |
| 77 | + num_posterior_draws: int = 8, |
| 78 | + seed: int = 0, |
| 79 | +) -> dict[str, Any]: |
| 80 | + """Run a compact end-to-end synthetic science workflow. |
| 81 | +
|
| 82 | + This workflow performs deterministic optimization, variational inference, |
| 83 | + posterior predictive sampling, and residual/metric summarization. |
| 84 | +
|
| 85 | + Args: |
| 86 | + cube_shape (tuple[int, int, int], optional): Synthetic IFU cube shape |
| 87 | + ``(nx, ny, nw)``. Defaults to ``(4, 4, 16)``. |
| 88 | + target_scale (float, optional): Multiplicative scale for the synthetic |
| 89 | + target cube. Defaults to 1.7. |
| 90 | + optimize_steps (int, optional): Maximum deterministic optimization |
| 91 | + steps. Defaults to 120. |
| 92 | + vi_steps (int, optional): Maximum variational optimization steps. |
| 93 | + Defaults to 120. |
| 94 | + num_vi_samples (int, optional): Monte Carlo samples per VI step. |
| 95 | + Defaults to 4. |
| 96 | + num_posterior_draws (int, optional): Number of posterior predictive |
| 97 | + cube draws. Defaults to 8. |
| 98 | + seed (int, optional): Random seed for VI and predictive sampling. |
| 99 | + Defaults to 0. |
| 100 | +
|
| 101 | + Returns: |
| 102 | + dict[str, Any]: Workflow outputs and diagnostic summaries. |
| 103 | + """ |
| 104 | + template = jnp.ones(cube_shape, dtype=jnp.float32) |
| 105 | + target = target_scale * template |
| 106 | + |
| 107 | + pipeline = SyntheticScalePipeline(template) |
| 108 | + static_data = _make_static_data() |
| 109 | + params_init = {"stars": {"age": jnp.array([0.2])}} |
| 110 | + |
| 111 | + opt_result = optimize_ifu_cube( |
| 112 | + pipeline=pipeline, |
| 113 | + params_init=params_init, |
| 114 | + static_data=static_data, |
| 115 | + target=target, |
| 116 | + learning_rate=0.1, |
| 117 | + max_steps=optimize_steps, |
| 118 | + tol=1e-8, |
| 119 | + ) |
| 120 | + |
| 121 | + vi_result = optimize_variational_ifu_cube( |
| 122 | + pipeline=pipeline, |
| 123 | + params_init=params_init, |
| 124 | + static_data=static_data, |
| 125 | + target=target, |
| 126 | + sigma=jnp.ones_like(target), |
| 127 | + learning_rate=5e-2, |
| 128 | + max_steps=vi_steps, |
| 129 | + tol=1e-8, |
| 130 | + num_samples=num_vi_samples, |
| 131 | + beta_kl=1e-4, |
| 132 | + seed=seed, |
| 133 | + ) |
| 134 | + |
| 135 | + predictive_samples = sample_posterior_predictive_cubes( |
| 136 | + pipeline=pipeline, |
| 137 | + posterior_mean_params=vi_result.posterior_mean_params, |
| 138 | + posterior_log_std_params=vi_result.posterior_log_std_params, |
| 139 | + static_data=static_data, |
| 140 | + num_samples=num_posterior_draws, |
| 141 | + seed=seed + 1, |
| 142 | + ) |
| 143 | + predictive_summary = summarize_predictive_cube_samples(predictive_samples) |
| 144 | + residual_products = compute_residual_products( |
| 145 | + prediction=predictive_summary["mean"], |
| 146 | + target=target, |
| 147 | + ) |
| 148 | + metrics = summarize_masked_metrics( |
| 149 | + prediction=predictive_summary["mean"], |
| 150 | + target=target, |
| 151 | + ) |
| 152 | + |
| 153 | + return { |
| 154 | + "config": { |
| 155 | + "cube_shape": cube_shape, |
| 156 | + "target_scale": target_scale, |
| 157 | + "optimize_steps": optimize_steps, |
| 158 | + "vi_steps": vi_steps, |
| 159 | + "num_vi_samples": num_vi_samples, |
| 160 | + "num_posterior_draws": num_posterior_draws, |
| 161 | + "seed": seed, |
| 162 | + }, |
| 163 | + "optimization": { |
| 164 | + "final_loss": opt_result.final_loss, |
| 165 | + "best_loss": opt_result.best_loss, |
| 166 | + "steps_run": opt_result.steps_run, |
| 167 | + "converged": opt_result.converged, |
| 168 | + }, |
| 169 | + "variational": { |
| 170 | + "final_objective": vi_result.final_objective, |
| 171 | + "best_objective": vi_result.best_objective, |
| 172 | + "steps_run": vi_result.steps_run, |
| 173 | + "converged": vi_result.converged, |
| 174 | + }, |
| 175 | + "predictive_summary": predictive_summary, |
| 176 | + "residual_products": residual_products, |
| 177 | + "metrics": metrics, |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | +def save_science_recipe_outputs( |
| 182 | + outputs: dict[str, Any], |
| 183 | + output_dir: str, |
| 184 | +) -> None: |
| 185 | + """Persist workflow outputs to JSON and NPZ files. |
| 186 | +
|
| 187 | + Args: |
| 188 | + outputs (dict[str, Any]): Outputs from |
| 189 | + :func:`run_synthetic_science_recipe`. |
| 190 | + output_dir (str): Destination directory. |
| 191 | + """ |
| 192 | + out_dir = Path(output_dir) |
| 193 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 194 | + |
| 195 | + summary = { |
| 196 | + "config": outputs["config"], |
| 197 | + "optimization": outputs["optimization"], |
| 198 | + "variational": outputs["variational"], |
| 199 | + "metrics": outputs["metrics"], |
| 200 | + } |
| 201 | + json_summary = _to_jsonable(summary) |
| 202 | + (out_dir / "summary.json").write_text( |
| 203 | + json.dumps(json_summary, indent=2), encoding="utf-8" |
| 204 | + ) |
| 205 | + |
| 206 | + predictive_np = {k: np.asarray(v) for k, v in outputs["predictive_summary"].items()} |
| 207 | + residual_np = {k: np.asarray(v) for k, v in outputs["residual_products"].items()} |
| 208 | + |
| 209 | + np.savez(out_dir / "predictive_summary.npz", **predictive_np) |
| 210 | + np.savez(out_dir / "residual_products.npz", **residual_np) |
0 commit comments