Title: Prostate IMRT matRad Reproduction
Domain: health_medicine — Therapeutic & Oncology Services

Summary: Act as a senior medical physicist: reproduce a fixed-field prostate IMRT reference plan with matRad + GNU Octave on Linux, repair three planted RTSTRUCT defects, optimize fluence + Engel leaf sequencing, export a consistent DICOM-RT bundle, and produce independent Python QA artifacts.

Task prompt:
You are a senior medical physicist performing an independent research replanning benchmark on a fixed-field prostate IMRT reference plan using matRad (open-source treatment planning) and GNU Octave on Linux.

Task directory:
- `base`

Visible inputs (do not modify):
- CT DICOM series: `base/input/dicom/CT`
- Corrupted RTSTRUCT (contains 3 planted defects): `base/input/dicom/RTSTRUCT.dcm`
- Prescription: `base/input/prescription.json`
- Institutional DVH constraints and margin recipe: `base/input/institutional_constraints.md`
- Beam geometry (7 gantry angles): `base/input/beam_geometry.json`
- matRad beam model: `base/input/machine/benchmark_6MV.mat`
- matRad plan template: `base/input/pln_template.mat`
- Agent-facing README: `base/input/README_AGENT.md`
- Repair recipe notes: `base/input/repair_recipes.md`
- Submission output template: `base/input/submission_template`
- File hashes: `base/input/manifest.json`
- matRad/Octave launcher (preinstalled): `base/software/run_matrad.sh`

Your task (six phases):
1. Structure QA: identify and fix three defects planted in the supplied RTSTRUCT (clinical-rule corrections described in `base/input/repair_recipes.md`).
2. Import into matRad using the shipped `base/input/pln_template.mat` and `base/input/machine/benchmark_6MV.mat` (7 coplanar 6 MV beams, Engel sequencing 7 levels, RNG seed 42).
3. Fluence optimization with Engel leaf sequencing. Naive weights violate the rectum V70Gy constraint; expect 3-6 weight-tuning iterations.
4. Deterministic pencil-beam (SVDPB) dose calculation at 3 mm grid.
5. DICOM-RT export of RTPLAN.dcm, RTDOSE.dcm, and corrected RTSTRUCT, all sharing a consistent FrameOfReferenceUID.
6. Independent QA in Python: recompute DVH from the submitted RTDOSE + RTSTRUCT, render axial/sagittal/coronal isodose PNGs, and produce `report.md`, `plan_metrics.json`, `beam_summary.csv`, `decisions.md`.

Required outputs, written only under `base/output`:
- `RTPLAN.dcm`, `RTDOSE.dcm`, `RTSTRUCT_corrected.dcm`
- `replay_state.mat` (numeric-only matRad state for independent replay)
- `dvh_metrics.csv`, `plan_metrics.json`, `beam_summary.csv`
- `figures/axial.png`, `figures/sagittal.png`, `figures/coronal.png` (each >= 800x800)
- `report.md`, `decisions.md`

Runtime guidance:
- Invoke matRad / Octave via `base/software/run_matrad.sh`; the wrapper activates the pinned `rtplan-matrad` environment (GNU Octave 6.4.0, matRad commit `c014dc82`, Python 3.10 with pydicom / pymedphys / numba / numpy / scipy / matplotlib / scikit-image).
- No network access is required after the environment is installed.
- Do not modify files under `input/`. Write the final bundle only under `base/output`.


The agent must:
Repair the three planted RTSTRUCT defects (Rectum `RTROIInterpretedType = ORGAN`; derive `PTV_7800` by expanding the `PTV_placeholder` CTV with the institutional margin recipe; re-derive the clipped External contour from CT).
Import the clinical geometry into matRad using the shipped `pln_template.mat` + `benchmark_6MV.mat` with the 7 coplanar 6 MV beams, RNG seed 42, and Engel sequencing (7 levels).
Tune fluence weights (3–6 iterations) so rectum V70Gy ≤ 20% and the other institutional DVH constraints are met.
Run deterministic pencil-beam (SVDPB) dose calculation at the 3 mm grid.
Export `RTPLAN.dcm`, `RTDOSE.dcm`, and the corrected RTSTRUCT with consistent `FrameOfReferenceUID`, CT-aligned origin/spacing, and contour z-planes on CT slices.
Produce the independent Python QA bundle (`dvh_metrics.csv`, `plan_metrics.json`, `beam_summary.csv`, three ≥800×800 isodose PNGs, `report.md`, `decisions.md`) plus the numeric-only `replay_state.mat` in `output/`.

Software: matRad
GNU Octave
Python
pydicom
pymedphys
numpy
scipy
matplotlib
scikit-image

Input files:
name: dicom/CT
format: DICOM image series
path: input/dicom/CT
description: Planning CT — 90 axial slices, 3 mm spacing, 183×183 matrix.
name: dicom/RTSTRUCT.dcm
format: DICOM-RT structure set
path: input/dicom/RTSTRUCT.dcm
description: Corrupted RTSTRUCT with three planted defects the agent must diagnose and repair.
name: prescription.json
format: JSON
path: input/prescription.json
description: Prescription (78 Gy / 39 fractions / 6 MV photons).
name: institutional_constraints.md
format: Markdown
path: input/institutional_constraints.md
description: PTV coverage, OAR DVH limits, margin recipe, and ring handling rules.
name: beam_geometry.json
format: JSON
path: input/beam_geometry.json
description: Seven fixed coplanar 6 MV gantry angles.
name: machine/benchmark_6MV.mat
format: MAT-file
path: input/machine/benchmark_6MV.mat
description: Frozen matRad photon beam model (SHA256 pinned).
name: pln_template.mat
format: MAT-file
path: input/pln_template.mat
description: matRad plan template with non-objective knobs pinned.
name: repair_recipes.md
format: Markdown
path: input/repair_recipes.md
description: External re-derivation and PTV rasterization rules.
name: submission_template/
format: Directory
path: input/submission_template
description: Blank templates for the required output artifacts.
name: manifest.json
format: JSON
path: input/manifest.json
description: SHA256 hashes for every shipped input file.
name: README_AGENT.md
format: Markdown
path: input/README_AGENT.md
description: Agent-facing briefing and required-output contract.
name: run_matrad.sh
format: Shell wrapper
path: software/run_matrad.sh
description: Launcher that activates the pinned `rtplan-matrad` micromamba env (matRad + Octave 6.4.0 + Python QA stack).