CT Geometry Calibration (Catphan Phantom)
Domain: health_medicine — Clinical Diagnostics & Imaging

Summary: Calibrate CT geometry parameters (SAD, SDD, detector offset) for a Catphan phantom scan by iteratively optimizing a LEAP-based FBP reconstruction to match a reference image.

Task prompt:
You are working on a Linux VM.

## Your Task

Calibrate the CT geometry parameters for a Catphan phantom reconstruction.

The input directory contains:
- `sinogram.npy` — measured sinogram (fan-beam CT, float32)
- `reference_image.npy` — ground-truth reconstructed image (512x512, float32)
- `reconstruct.py` — FBP reconstruction script using LEAP/leaptorch with nominal (slightly wrong) geometry parameters (SAD=800, SDD=1200, offset=0)

Your goal is to find the correct geometry parameters (SAD, SDD, detector offset,
and any other relevant geometric parameters) so that the reconstructed image
matches the reference image.

## Steps

1. Inspect `reconstruct.py` to understand the current geometry parameters and
   the LEAP-based FBP reconstruction pipeline.
2. Run the initial reconstruction with nominal parameters and compare against
   `reference_image.npy` to establish a baseline error.
3. Iteratively adjust the geometry parameters, run reconstructions, and compare
   outputs to `reference_image.npy` using SSIM and MSE metrics.
4. Continue optimization (e.g., using scipy.optimize.minimize or
   differential_evolution) until the reconstructed image achieves
   SSIM >= 0.95 and unnormalized MSE <= 4e-6 relative to `reference_image.npy`.
5. Save the final calibrated parameters to
   `instance_1/output/geometry_calibrated.json`
   and the final reconstructed image to
   `instance_1/output/reconstructed_calibrated.npy`.

## Input Files
- Located at: `instance_1/input`

## Output
- `instance_1/output/geometry_calibrated.json` — calibrated parameters
- `instance_1/output/reconstructed_calibrated.npy` — final reconstruction

## Software
- Python 3.10 + LEAP 1.26 (leaptorch), NumPy, SciPy, scikit-image, PyTorch (CPU), imageio.
- Use `software/python` (the canonical entry point for this task) to invoke
  Python; it resolves to `/usr/bin/python` on the VM.
  Example: `software/python input/reconstruct.py input/sinogram.npy`

## Important Constraints
- Do not modify files under `instance_1/input`
- SSIM is computed via `skimage.metrics.structural_similarity`
- Unnormalized MSE = mean((ref - recon)**2)


Required actions:
Inspect reconstruct.py to understand the LEAP-based FBP pipeline and nominal geometry parameters.
Run the initial reconstruction with nominal parameters and compare against reference_image.npy.
Iteratively optimize geometry parameters until SSIM >= 0.95 and unnormalized MSE <= 4e-6.
Save calibrated parameters to output/geometry_calibrated.json.
Save the final reconstructed image to output/reconstructed_calibrated.npy.

Software: Python 3.10+
LEAP 1.26 (leaptorch)
NumPy
SciPy
scikit-image
PyTorch (CPU)

Input files:
name: Sinogram
format: NumPy .npy (float32)
description: Measured sinogram of Catphan phantom (fan-beam CT).
name: Reference image
format: NumPy .npy (float32, 512x512)
description: Ground-truth reconstructed phantom image.
name: Reconstruction script
format: Python script
description: FBP reconstruction script using LEAP/leaptorch with nominal (slightly wrong) geometry parameters.