Nv Generate Mr
NVIDIAOfficialCreated by NVIDIA MedTech Team
- Skill
- Tool integration
- Data and AI
- Apache-2.0
- Medium risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
NV-Generate-MR provides a standardized interface for the NVIDIA-Medtech/NV-Generate-CTMR rflow-mr model, enabling the generation of synthetic body MRI volumes. The skill acts as a thin wrapper that manages configuration staging, output path handling, and validation of generated NIfTI volumes. It is designed for research and experimentation, specifically excluding clinical deployment, autonomous diagnosis, or the creation of production training data. Users must provide a model configuration file to define generation parameters, and the tool ensures that outputs meet specific geometric and intensity constraints.
What it does
- Generates synthetic body MRI volumes using the rflow-mr model.
- Manages configuration overrides for model inference.
- Validates output geometry, spacing, and intensity ranges.
- Records metadata including staged configs and model inventory.
- Supports multiple MRI modalities including T1, T2, and FLAIR.
When to use it
- When you need to generate synthetic body MRI images for research purposes.
- When you require a standardized way to invoke the rflow-mr model with specific configuration overrides.
- When you need to validate the output of an MRI generation run against predefined engineering invariants.
Advantages
- Provides a consistent, documented interface for the upstream rflow-mr model.
- Automates the staging of configurations and output validation.
- Ensures reproducibility by recording run metadata and model inventory.
- Integrates validation gates to catch common upstream failures or invalid outputs.
Usage examples
- “Generate a synthetic T1-weighted body MRI volume using the configuration file at configs/my_mri_config.json.”
- The agent will execute the wrapper script with the provided config, outputting the synthetic MRI volume to the specified directory after validating the results.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/nvidia/nv-generate-mr/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip nv-generate-mr.zip -d .agents/skills
Documentation by NVIDIA MedTech Team
NV-Generate-MR
Purpose
Used for generating synthetic body MRI volumes with NV-Generate-CTMR rflow-mr. Not for paired masks or production training data.
Use the wrapper exactly as documented; do not replace the upstream entrypoint with a handwritten implementation.
Do not write custom inference code for normal runs. The wrapper owns config staging, output paths, and validation.
Manifest I/O: inputs are
model_config_override; outputs aresynthetic_mr_volumesandresult_json.
Instructions
Read
skill_manifest.yamlbefore changing arguments, side effects, or validation gates.Run
scripts/run_mr.pythrough the documented command below; keep outputs under a caller-provided run directory.If a host agent exposes
run_script, userun_script("scripts/run_mr.py", args=[...]); otherwise run the Bash/Python command shown below.Emit a single bash code block, and keep the
python -m pip install -r "$NV_GENERATE_ROOT/requirements.txt"step in that same command — the runtime may be a fresh environment withoutnibabel/MONAI, so dropping the install fails withModuleNotFoundError.Do not add
rm,mkdir, or any cleanup of--output-dir; the wrapper creates it. Use a fresh--output-dirinstead of deleting one.Check the emitted JSON and paired verifier guidance before treating the run as evidence.
Available Scripts
| Script | Purpose | Arguments |
|---|---|---|
scripts/run_mr.py | Primary entrypoint declared by skill_manifest.yaml. | MODEL_CONFIG.json --output-dir OUT_DIR --modality mri_t1 [--random-seed N] [--yes] |
Prerequisites
Runtime requirements: GPU/CUDA when declared by the manifest; Python packages listed in
runtime.side_effects.pip_packages.Side effects: writes generated outputs under the caller's
--output-dir, may cache model assets under~/.cache/huggingface/, and may contacthttps://huggingface.coorhttps://github.comduring setup.Run commands from the repository root unless an existing section below says otherwise.
Limitations
This is a thin wrapper. Inference, sampling, and decoding are delegated entirely to NVIDIA-Medtech/NV-Generate-CTMR's
scripts.diff_model_infer. Do not modify code under $NV_GENERATE_ROOT or the repo-local fallback at .workbench_data/upstreams/NV-Generate-CTMR.rflow-mr generates image-only synthetic MRI volumes. It does not emit paired segmentation masks.
The upstream README recommends
rflow-mr-braininstead for brain MRI synthesis; useskills/nv-generate-mr-brainfor that path.NV-Generate-MR weights are listed by upstream as NVIDIA Non-Commercial. Do not use outputs as production training data without legal and quality review.
Not for clinical deployment, clinical interpretation, autonomous diagnosis, regulatory submission.
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
| Missing dependency or import error | Runtime package drift from skill_manifest.yaml. | Install the packages declared in the manifest or use the documented setup command. |
| Empty or schema-invalid output | Wrong input path, unsupported modality, or upstream failure. | Re-run with a known fixture and inspect the wrapper JSON plus stderr. |
| Validation gate failure | Output violated a declared engineering invariant. | Keep the failed evidence pack and use the gate message to repair inputs or wrapper code. |
Wraps the upstream
NVIDIA-Medtech/NV-Generate-CTMR
MR image-only generation workflow. The wrapper does not reimplement diffusion
sampling or autoencoder decoding. It stages config overrides, runs the
documented python -m scripts.diff_model_infer command for rflow-mr, then
summarizes the generated NIfTI volume.
Exact Runnable Surface
For user run commands in a fresh benchmark environment, use this setup plus repo-root wrapper command exactly:
export NV_GENERATE_ROOT="${NV_GENERATE_ROOT:-$HOME/.cache/nvidia-skills/upstreams/NV-Generate-CTMR-61c4ec7}" && \
python -m pip install -r "$NV_GENERATE_ROOT/requirements.txt" && \
python skills/nv-generate-mr/scripts/run_mr.py PATH_TO_MR_CONFIG.json --output-dir OUT_DIR --modality mri_t1 --random-seed 0Do not invent generate.sh, infer.py, Medical AI Skills run, or python -m nv_generate_mr commands. PATH_TO_MR_CONFIG.json must be the user's supplied request path.
Preconditions
If NV_GENERATE_ROOT already names a local checkout, the wrapper uses it and
records its current commit in the result. Otherwise, create the recommended
pinned default checkout once:
if [ -z "${NV_GENERATE_ROOT:-}" ]; then
export NV_GENERATE_COMMIT=61c4ec709b84cad468852243c48e250bec732074
export NV_GENERATE_ROOT="$HOME/.cache/nvidia-skills/upstreams/NV-Generate-CTMR-61c4ec7"
if [ ! -d "$NV_GENERATE_ROOT/.git" ]; then
git clone https://github.com/NVIDIA-Medtech/NV-Generate-CTMR.git "$NV_GENERATE_ROOT"
git -C "$NV_GENERATE_ROOT" checkout --detach "$NV_GENERATE_COMMIT"
fi
fi
pip install -r "$NV_GENERATE_ROOT/requirements.txt"Download the MR weights:
cd "$NV_GENERATE_ROOT"
python -m scripts.download_model_data --version rflow-mr --root_dir ./ --model_onlyRuntime needs an NVIDIA GPU with at least 16 GB VRAM. There is no CPU fallback in the upstream path.
The wrapper also searches .workbench_data/upstreams/NV-Generate-CTMR if
NV_GENERATE_ROOT is unset or does not have the required upstream layout.
For agent-generated user run commands, use the command in Usage. Do not prepend
clone or model-download setup steps when the repo-local upstream cache already
exists. In a fresh Python environment, still include
pip install -r "$NV_GENERATE_ROOT/requirements.txt" before the wrapper unless
the active environment has already proven those imports are available; cached
weights do not imply cached Python packages. If setup requires cd "$NV_GENERATE_ROOT", return to the Medical AI Skills repo before invoking
skills/nv-generate-mr/scripts/run_mr.py.
Usage
export NV_GENERATE_ROOT="${NV_GENERATE_ROOT:-$HOME/.cache/nvidia-skills/upstreams/NV-Generate-CTMR-61c4ec7}" && \
python -m pip install -r "$NV_GENERATE_ROOT/requirements.txt" && \
python skills/nv-generate-mr/scripts/run_mr.py \
PATH_TO_MR_CONFIG.json \
--output-dir runs/nv_generate_mr_demo \
--modality mri_t1 \
--random-seed 0Replace PATH_TO_MR_CONFIG.json with the user's actual request/config path.
Do not copy the fixture path from this document unless the user explicitly
asked to run that fixture. If the user says "the request is at
runs/.../default_mri_t1.json", that exact path is the first positional
argument to scripts/run_mr.py.
Supported rflow-mr modality names are mri, mri_t1, mri_t2, and
mri_flair, matching the upstream MR image-generation guide. The upstream
README recommends rflow-mr-brain instead when synthesizing brain images;
use skills/nv-generate-mr-brain for that path.
For FOV and setup details, see references/fov-and-downloads.md.
The fixture argument is a small JSON override for
configs/config_maisi_diff_model_rflow-mr.json. Pass default to use the
upstream defaults plus the CLI modality and random seed. Common override keys
are dim, spacing, num_inference_steps, cfg_guidance_scale, and
modality.
Each run records the staged config, model inventory, upstream command, output geometry, spacing, affine, intensity range, and non-constant / finite-data checks. Output volumes are synthetic and are not safe as production training data without independent review.
Not for clinical interpretation, production deployment, autonomous diagnosis, or regulatory submission.
Files
- ATTRIBUTION.mdAdded by AutoGineer913 B
- BENCHMARK.md3.9 KiB
- LICENSELicence file added by AutoGineer11.2 KiB
- SKILL.md7.8 KiB
- evals/evals.json1.1 KiB
- fixtures/README.md925 B
- fixtures/default_mri_t1.json313 B
- fixtures/mri_flair.json233 B
- fixtures/mri_t2.json227 B
- references/fov-and-downloads.md1011 B
- scripts/run_mr.pyScript25.4 KiB
- skill-card.md4.1 KiB
- skill.oms.sig6.5 KiB
- skill_manifest.yaml7.3 KiB
- tests/test_run_mr.pyScript6.3 KiB
- validators/output_schema.json5.5 KiB
scripts/run_mr.py
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""NVIDIA-Medtech NV-Generate-CTMR rflow-mr skill.
Thin wrapper around the upstream `scripts.diff_model_infer` entry point from
https://github.com/NVIDIA-Medtech/NV-Generate-CTMR. The wrapper does NOT
implement diffusion sampling or autoencoder decoding. It stages config
overrides, shells out to the upstream command, and summarizes generated MR
NIfTI outputs.
Engineering verification only. Output is NOT clinically meaningful.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
import time
from pathlib import Path
from typing import Any
import nibabel as nib
import numpy as np
import typer
SKILL_NAME = "nv_generate_mr"
MODEL_REPO = "https://github.com/NVIDIA-Medtech/NV-Generate-CTMR"
MODEL_WEIGHTS_REPO = "https://huggingface.co/nvidia/NV-Generate-MR"
VERSION = "rflow-mr"
REPO_ROOT = Path(__file__).resolve().parents[int("3")]
UPSTREAM_NETWORK_CONFIG = "configs/config_network_rflow.json"
UPSTREAM_MODEL_CONFIG = "configs/config_maisi_diff_model_rflow-mr.json"
UPSTREAM_ENV_CONFIG = "configs/environment_maisi_diff_model_rflow-mr.json"
UPSTREAM_MODALITY_MAPPING = "configs/modality_mapping.json"
UPSTREAM_MODEL_FILES = (
"models/autoencoder_v2.pt",
"models/diff_unet_3d_rflow-mr.pt",
)
SUPPORTED_MODALITIES = ("mri", "mri_t1", "mri_t2", "mri_flair")
OVERRIDE_KEYS = (
"dim",
"spacing",
"top_region_index",
"bottom_region_index",
"random_seed",
"num_inference_steps",
"modality",
"cfg_guidance_scale",
"output_prefix",
)
MAX_VOXELS = int("512") * int("512") * int("128")
app = typer.Typer(add_completion=False)
def emit(payload: dict[str, Any]) -> None:
sys.stdout.write(json.dumps(payload, indent=2))
sys.stdout.flush()
def tail(s: str, n_chars: int = int("4000")) -> str:
if len(s) <= n_chars:
return s
return "..." + s[-n_chars:]
def sha256_file(path: Path, chunk: int = 1 << int("20")) -> str:
import hashlib
h = hashlib.sha256()
with path.open("rb") as f:
while True:
buf = f.read(chunk)
if not buf:
break
h.update(buf)
return h.hexdigest()
def file_sha256_safe(path: Path) -> str:
if not path.is_file():
return ""
try:
return sha256_file(path)
except Exception:
return ""
def git_commit(root: Path) -> str:
try:
proc = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=str(root),
check=False,
capture_output=True,
text=True,
timeout=int("10"),
)
except Exception:
return ""
if proc.returncode == 0:
return proc.stdout.strip()
return ""
def _round(values: Any, ndigits: int = int("6")) -> Any:
if isinstance(values, (list, tuple, np.ndarray)):
return [round(float(v), ndigits) for v in values]
return round(float(values), ndigits)
def _load_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text())
def _valid_upstream_root(path: Path) -> bool:
return (path / UPSTREAM_NETWORK_CONFIG).is_file()
def _candidate_upstream_roots(env_value: str) -> list[Path]:
candidates: list[Path] = []
if env_value:
candidates.append(Path(env_value).expanduser())
candidates.extend(
[
REPO_ROOT / ".workbench_data/upstreams/NV-Generate-CTMR",
Path.home() / "NV-Generate-CTMR",
Path.home() / "nv-generate-ctmr",
]
)
deduped: list[Path] = []
seen: set[str] = set()
for candidate in candidates:
key = str(candidate)
if key not in seen:
seen.add(key)
deduped.append(candidate)
return deduped
def _resolve_upstream_root(env_value: str) -> tuple[Path | None, list[str]]:
checked: list[str] = []
for candidate in _candidate_upstream_roots(env_value):
resolved = candidate.resolve()
checked.append(str(resolved))
if _valid_upstream_root(resolved):
return resolved, checked
return None, checked
def _load_modality_mapping(upstream_root: Path) -> dict[str, int]:
mapping = _load_json(upstream_root / UPSTREAM_MODALITY_MAPPING)
return {str(k): int(v) for k, v in mapping.items()}
def _modality_to_code(modality: str, mapping: dict[str, int]) -> int:
if modality not in SUPPORTED_MODALITIES:
raise typer.BadParameter(
f"--modality must be one of {list(SUPPORTED_MODALITIES)}, got {modality!r}"
)
if modality not in mapping:
raise typer.BadParameter(f"modality {modality!r} not found in upstream modality mapping")
return int(mapping[modality])
def _load_config_override(fixture_arg: str) -> tuple[dict[str, Any], str | None]:
if fixture_arg == "default":
return {}, None
fixture_path = Path(fixture_arg).expanduser().resolve()
if not fixture_path.is_file():
raise typer.BadParameter(f"model config override not found: {fixture_arg}")
raw = json.loads(fixture_path.read_text())
cleaned = {k: v for k, v in raw.items() if not k.startswith("_")}
if "diffusion_unet_inference" in cleaned:
nested = cleaned.pop("diffusion_unet_inference")
if not isinstance(nested, dict):
raise typer.BadParameter("diffusion_unet_inference must be a JSON object")
cleaned.update(nested)
unknown = sorted(k for k in cleaned if k not in OVERRIDE_KEYS)
if unknown:
raise typer.BadParameter(
f"MR override contains unknown key(s): {unknown}. Allowed: {sorted(OVERRIDE_KEYS)}"
)
return cleaned, str(fixture_path)
def _validate_inference_config(rendered_inference: dict[str, Any]) -> list[str]:
errors: list[str] = []
dim = rendered_inference.get("dim")
if not (isinstance(dim, (list, tuple)) and len(dim) == int("3")):
errors.append(f"dim must be a 3-tuple, got {dim!r}")
else:
voxels = 1
for i, v in enumerate(dim):
if not isinstance(v, int):
errors.append(f"dim[{i}] must be int, got {v!r}")
elif v < int("64") or v > int("512"):
errors.append(f"dim[{i}]={v} outside rflow-mr range [64, 512]")
elif v % int("32") != 0:
errors.append(f"dim[{i}]={v} must be a multiple of 32")
if isinstance(v, int):
voxels *= int(v)
if voxels > MAX_VOXELS:
errors.append(
f"dim product {voxels} exceeds rflow-mr max volume {MAX_VOXELS} "
"(512x512x128 per upstream README)"
)
spacing = rendered_inference.get("spacing")
if not (isinstance(spacing, (list, tuple)) and len(spacing) == int("3")):
errors.append(f"spacing must be a 3-tuple, got {spacing!r}")
else:
for i, v in enumerate(spacing):
if not isinstance(v, (int, float)) or v <= 0:
errors.append(f"spacing[{i}] must be a positive float, got {v!r}")
for key in ("top_region_index", "bottom_region_index"):
value = rendered_inference.get(key)
if not (isinstance(value, (list, tuple)) and len(value) == int("4")):
errors.append(f"{key} must be a 4-tuple, got {value!r}")
elif not all(isinstance(v, (int, float)) for v in value):
errors.append(f"{key} values must be numeric, got {value!r}")
n_steps = rendered_inference.get("num_inference_steps")
if not isinstance(n_steps, int) or n_steps < 1 or n_steps > int("2000"):
errors.append(f"num_inference_steps must be int in [1, 2000], got {n_steps!r}")
seed = rendered_inference.get("random_seed")
if not isinstance(seed, int):
errors.append(f"random_seed must be int, got {seed!r}")
cfg = rendered_inference.get("cfg_guidance_scale")
if not isinstance(cfg, (int, float)):
errors.append(f"cfg_guidance_scale must be numeric, got {cfg!r}")
modality = rendered_inference.get("modality")
if not isinstance(modality, int) or modality < 0:
errors.append(f"modality must be a non-negative int code, got {modality!r}")
return errors
def _stage_config(
upstream_root: Path,
stage_dir: Path,
override: dict[str, Any],
output_dir: Path,
modality_code: int,
modality_name: str,
seed: int,
) -> tuple[dict[str, Any], dict[str, Any], Path, Path]:
stage_dir.mkdir(parents=True, exist_ok=True)
base_model = _load_json(upstream_root / UPSTREAM_MODEL_CONFIG)
rendered_model = dict(base_model)
inference = dict(rendered_model.get("diffusion_unet_inference") or {})
inference.update(override)
inference["modality"] = modality_code
inference["random_seed"] = seed
rendered_model["diffusion_unet_inference"] = inference
staged_model_path = stage_dir / "config_maisi_diff_model_rflow-mr.json"
staged_model_path.write_text(json.dumps(rendered_model, indent=2))
base_env = _load_json(upstream_root / UPSTREAM_ENV_CONFIG)
rendered_env = dict(base_env)
rendered_env["output_dir"] = str(output_dir)
if "output_prefix" in override:
rendered_env["output_prefix"] = str(override["output_prefix"])
else:
rendered_env["output_prefix"] = f"mr_{modality_name}"
staged_env_path = stage_dir / "environment_maisi_diff_model_rflow-mr.json"
staged_env_path.write_text(json.dumps(rendered_env, indent=2))
return rendered_model, rendered_env, staged_model_path, staged_env_path
def _estimate_cost(rendered_inference: dict[str, Any]) -> dict[str, Any]:
dim = rendered_inference.get("dim") or [int("128"), int("256"), int("256")]
n_steps = int(rendered_inference.get("num_inference_steps") or int("30"))
voxels = int(dim[0]) * int(dim[1]) * int(dim[2])
ref_voxels = int("128") * int("256") * int("256")
ref_steps = int("30")
ref_seconds = float("60.0")
seconds = ref_seconds * (voxels / ref_voxels) * (n_steps / ref_steps)
vram = float("16.0") if voxels <= ref_voxels else float("32.0")
disk_mb = (voxels * 2.0) / (int("1024") * int("1024"))
return {
"version": VERSION,
"voxels_per_sample": voxels,
"num_inference_steps": n_steps,
"estimated_wall_seconds": round(seconds, 1),
"estimated_peak_vram_gb": round(vram, 1),
"estimated_disk_mb": round(disk_mb, 1),
}
def _detect_cuda() -> dict[str, Any]:
info: dict[str, Any] = {"available": False, "device_name": None, "total_memory_gb": None}
try:
import torch # noqa: PLC0415
info["torch_version"] = torch.__version__
info["available"] = bool(torch.cuda.is_available())
if info["available"]:
props = torch.cuda.get_device_properties(0)
info["device_name"] = props.name
info["total_memory_gb"] = round(props.total_memory / (int("1024") ** int("3")), 1)
info["cuda_version"] = torch.version.cuda
except Exception as e:
info["import_error"] = repr(e)
return info
def _preflight(rendered_inference: dict[str, Any]) -> tuple[list[str], list[str], dict[str, Any]]:
errors = _validate_inference_config(rendered_inference)
warnings: list[str] = []
cuda = _detect_cuda()
cost = _estimate_cost(rendered_inference)
if not cuda["available"]:
errors.append(
"CUDA not available. rflow-mr synthesis needs an NVIDIA GPU; "
"there is no CPU fallback in the upstream code path."
)
elif cuda["total_memory_gb"] is not None:
usable = cuda["total_memory_gb"] * float("0.85")
if cost["estimated_peak_vram_gb"] > usable:
warnings.append(
f"estimated peak VRAM {cost['estimated_peak_vram_gb']} GB exceeds "
f"85% of detected GPU memory ({cuda['total_memory_gb']} GB on "
f"{cuda['device_name']}). Risk of OOM; reduce dim or use a larger GPU."
)
return errors, warnings, {"cuda": cuda, "estimated_cost": cost}
def _model_inventory(upstream_root: Path) -> dict[str, Any]:
files: list[dict[str, Any]] = []
all_present = True
for rel in UPSTREAM_MODEL_FILES:
path = upstream_root / rel
present = path.is_file()
files.append(
{
"path": rel,
"present": present,
"bytes": path.stat().st_size if present else None,
"sha256": file_sha256_safe(path) if present else "",
}
)
all_present = all_present and present
return {"all_present": all_present, "files": files}
def _build_command(staged_model_path: Path, staged_env_path: Path, num_gpus: int) -> list[str]:
cmd = [
sys.executable,
"-m",
"scripts.diff_model_infer",
"-t",
f"./{UPSTREAM_NETWORK_CONFIG}",
"-e",
str(staged_env_path),
"-c",
str(staged_model_path),
]
if num_gpus != 1:
cmd.extend(["-g", str(num_gpus)])
return cmd
def _scan_outputs(output_dir: Path, run_started: float) -> list[Path]:
if not output_dir.is_dir():
return []
paths: list[Path] = []
for path in output_dir.rglob("*.nii*"):
if not path.is_file():
continue
try:
if path.stat().st_size > 0 and path.stat().st_mtime >= run_started - 1:
paths.append(path)
except OSError:
continue
return sorted(paths)
def _summarize_image(
image_path: Path,
requested_dim: list[int],
requested_spacing: list[float],
) -> dict[str, Any]:
record: dict[str, Any] = {
"image_path": str(image_path),
"image_bytes": image_path.stat().st_size if image_path.exists() else None,
"image_sha256": file_sha256_safe(image_path) if image_path.exists() else "",
"image_readable": False,
}
try:
img = nib.load(str(image_path))
arr = np.asarray(img.get_fdata(), dtype=np.float32)
finite = arr[np.isfinite(arr)]
record["image_readable"] = True
record["image_shape"] = [int(v) for v in arr.shape]
record["requested_shape"] = [int(v) for v in requested_dim]
record["shape_match_requested"] = record["image_shape"] == record["requested_shape"]
record["image_spacing"] = _round(img.header.get_zooms()[: int("3")])
record["requested_spacing"] = _round(requested_spacing)
record["spacing_match_requested"] = record["image_spacing"] == record["requested_spacing"]
record["image_affine"] = [list(map(float, row)) for row in img.affine.tolist()]
record["finite_fraction"] = (
round(float(finite.size) / float(arr.size), int("6")) if arr.size else 0.0
)
record["all_finite"] = bool(finite.size == arr.size)
if finite.size:
record["intensity_min"] = _round(float(finite.min()), int("3"))
record["intensity_max"] = _round(float(finite.max()), int("3"))
record["intensity_mean"] = _round(float(finite.mean()), int("3"))
record["intensity_std"] = _round(float(finite.std()), int("3"))
record["image_nonconstant"] = bool(finite.max() - finite.min() > 1.0)
record["image_nonnegative"] = bool(finite.min() >= 0)
else:
record["image_nonconstant"] = False
record["image_nonnegative"] = False
except Exception as e:
record["image_error"] = repr(e)
return record
def _aggregate(samples: list[dict[str, Any]]) -> dict[str, Any]:
n = len(samples)
return {
"num_samples": n,
"all_images_readable": bool(n) and all(s.get("image_readable") for s in samples),
"all_shapes_match_requested": bool(n)
and all(s.get("shape_match_requested") for s in samples),
"all_spacing_match_requested": bool(n)
and all(s.get("spacing_match_requested") for s in samples),
"all_images_finite": bool(n) and all(s.get("all_finite") for s in samples),
"all_images_nonconstant": bool(n) and all(s.get("image_nonconstant") for s in samples),
"all_images_nonnegative": bool(n) and all(s.get("image_nonnegative") for s in samples),
}
@app.command()
def main(
model_config: str = typer.Argument(
...,
help='Path to a model-config override JSON, or "default" for upstream defaults.',
),
output_dir: Path | None = typer.Option(
None, "--output-dir", "-o", help="Absolute directory for generated NIfTI volumes."
),
modality: str | None = typer.Option(None, "--modality", help="MR modality name."),
seed: int = typer.Option(0, "--random-seed", "-s"),
num_gpus: int = typer.Option(1, "--num-gpus", min=1),
timeout_seconds: float = typer.Option(float("3600.0"), "--timeout-seconds"),
preflight_only: bool = typer.Option(
False,
"--preflight-only",
help="Validate config, CUDA, cost estimate, and model inventory without inference.",
),
yes: bool = typer.Option(
False,
"--yes",
"-y",
help="Skip the cost-preview confirmation gate for large runs.",
),
) -> None:
"""Generate synthetic 3D MRI volumes via NV-Generate-CTMR rflow-mr."""
upstream_root_env = os.environ.get("NV_GENERATE_ROOT", "").strip()
upstream_root, checked_roots = _resolve_upstream_root(upstream_root_env)
if upstream_root is None and not upstream_root_env:
emit(
{
"skill": SKILL_NAME,
"error": "NV_GENERATE_ROOT is unset",
"detail": "Clone https://github.com/NVIDIA-Medtech/NV-Generate-CTMR and export "
"NV_GENERATE_ROOT to its path, or place the clone at "
".workbench_data/upstreams/NV-Generate-CTMR.",
"checked_roots": checked_roots,
}
)
raise typer.Exit(2)
if upstream_root is None:
emit(
{
"skill": SKILL_NAME,
"error": "NV_GENERATE_ROOT layout invalid",
"detail": f"{UPSTREAM_NETWORK_CONFIG} not found in any checked root",
"checked_roots": checked_roots,
}
)
raise typer.Exit(2)
if output_dir is None:
output_dir = upstream_root / "output"
output_dir = output_dir.expanduser().resolve()
output_dir.mkdir(parents=True, exist_ok=True)
mapping = _load_modality_mapping(upstream_root)
override, override_source = _load_config_override(model_config)
override_modality = str(override.pop("modality")) if "modality" in override else None
modality_name = modality or override_modality or "mri_t1"
modality_code = _modality_to_code(modality_name, mapping)
stage_dir = output_dir / "_staged_configs"
rendered_model, rendered_env, staged_model_path, staged_env_path = _stage_config(
upstream_root,
stage_dir,
override,
output_dir,
modality_code,
modality_name,
seed,
)
rendered_inference = rendered_model["diffusion_unet_inference"]
errors, warnings, context = _preflight(rendered_inference)
inventory = _model_inventory(upstream_root)
if not inventory["all_present"]:
errors.append(
"missing rflow-mr model weights. Run `python -m scripts.download_model_data "
"--version rflow-mr --root_dir ./ --model_only` from $NV_GENERATE_ROOT."
)
cost = context["estimated_cost"]
cuda = context["cuda"]
print(
f"[nv_generate_mr] preflight: dim={rendered_inference.get('dim')} "
f"spacing={rendered_inference.get('spacing')} modality={modality_name}({modality_code}) "
f"steps={rendered_inference.get('num_inference_steps')}",
file=sys.stderr,
)
print(
f"[nv_generate_mr] cost estimate: ~{cost['estimated_wall_seconds']}s wall, "
f"~{cost['estimated_peak_vram_gb']} GB VRAM peak, ~{cost['estimated_disk_mb']} MB disk. "
f"GPU: {cuda.get('device_name','?')} ({cuda.get('total_memory_gb','?')} GB)",
file=sys.stderr,
)
for warning in warnings:
print(f"[nv_generate_mr] warning: {warning}", file=sys.stderr)
if errors:
for error in errors:
print(f"[nv_generate_mr] error: {error}", file=sys.stderr)
emit(
{
"skill": SKILL_NAME,
"error": "preflight validation failed",
"preflight_errors": errors,
"preflight_warnings": warnings,
"estimated_cost": cost,
"cuda": cuda,
"invocation": {"model_inventory": inventory},
}
)
raise typer.Exit(2)
if preflight_only:
emit(
{
"skill": SKILL_NAME,
"preflight": "ok",
"preflight_warnings": warnings,
"estimated_cost": cost,
"cuda": cuda,
"model_inventory": inventory,
"rendered_model_config": rendered_model,
"rendered_env_config": rendered_env,
}
)
raise typer.Exit(0)
if not yes and (
cost["estimated_wall_seconds"] > float("300.0")
or cost["estimated_peak_vram_gb"] > float("30.0")
):
emit(
{
"skill": SKILL_NAME,
"error": "cost gate: run would be expensive; re-run with --yes to proceed",
"estimated_cost": cost,
"cuda": cuda,
}
)
raise typer.Exit(2)
cmd = _build_command(staged_model_path, staged_env_path, num_gpus)
run_env = os.environ.copy()
run_env.setdefault("MONAI_DATA_DIRECTORY", str(upstream_root / "temp_work_dir"))
run_env.setdefault("PYTORCH_CUDA_ALLOC_CONF", "max_split_size_mb:128,expandable_segments:True")
run_started = time.time()
t0 = time.monotonic()
try:
proc = subprocess.run(
cmd,
cwd=str(upstream_root),
env=run_env,
capture_output=True,
text=True,
timeout=timeout_seconds,
check=False,
)
rc = proc.returncode
stdout = proc.stdout
stderr = proc.stderr
except subprocess.TimeoutExpired as e:
rc = int("124")
stdout = e.stdout.decode() if isinstance(e.stdout, bytes) else (e.stdout or "")
stderr_raw = e.stderr.decode() if isinstance(e.stderr, bytes) else (e.stderr or "")
stderr = stderr_raw + f"\n[TIMEOUT after {timeout_seconds}s]"
elapsed = time.monotonic() - t0
requested_dim = [int(v) for v in rendered_inference["dim"]]
requested_spacing = [float(v) for v in rendered_inference["spacing"]]
output_paths = _scan_outputs(output_dir, run_started)
samples = [_summarize_image(p, requested_dim, requested_spacing) for p in output_paths]
aggregate = _aggregate(samples)
payload: dict[str, Any] = {
"skill": SKILL_NAME,
"model": "NVIDIA-Medtech/NV-Generate-CTMR (rflow-mr)",
"model_repo": MODEL_REPO,
"model_weights_repo": MODEL_WEIGHTS_REPO,
"license": "Wrapper Apache-2.0; NV-Generate-MR weights use NVIDIA Non-Commercial License.",
"input": {
"model_config_override_path": override_source,
"model_config_override": override,
"modality_name": modality_name,
"modality_code": modality_code,
"dim_requested": requested_dim,
"spacing_requested": requested_spacing,
"num_inference_steps_requested": rendered_inference.get("num_inference_steps"),
"cfg_guidance_scale_requested": rendered_inference.get("cfg_guidance_scale"),
"random_seed": seed,
"version": VERSION,
},
"output": {
"directory": str(output_dir),
"samples": samples,
**aggregate,
},
"invocation": {
"official_entrypoint": "python -m scripts.diff_model_infer",
"upstream_root": str(upstream_root),
"upstream_commit": git_commit(upstream_root),
"command": cmd,
"exit_code": rc,
"subprocess_seconds": round(elapsed, int("3")),
"model_inventory": inventory,
"rendered_model_config": rendered_model,
"rendered_env_output_dir": rendered_env.get("output_dir"),
"rendered_env_output_prefix": rendered_env.get("output_prefix"),
},
"runtime": {
"subprocess_seconds": round(elapsed, int("3")),
"device": "cuda",
},
"logs": {
"stdout_tail": tail(stdout),
"stderr_tail": tail(stderr),
},
"preflight": {
"warnings": warnings,
"estimated_cost": cost,
"cuda": cuda,
},
"intended_use_disclaimer": (
"Engineering verification only. Output is synthetic and NOT clinically meaningful. "
"This wrapper invokes the upstream scripts.diff_model_infer entry point from the "
"NV-Generate-CTMR README; it does not modify diffusion sampling or autoencoder decoding."
),
}
emit(payload)
raise typer.Exit(0)
if __name__ == "__main__":
app()
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Declares allowed tools, Needs a command-line tool, Needs Python
Security
What our scanner noticed· Medium risk
- Large base64-encoded blobs
- Instructions that use the network
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- NVIDIA MedTech Team
- Publisher
- NVIDIA (Official)
- Source
- NVIDIA/skills · skills/nv-generate-mr · commit fce3cca
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE-APACHE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by NVIDIA MedTech Team or NVIDIA.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download nv-generate-mr.zip