Data
nanodrr.data
¶
Subject
¶
Subject(
imagedata: Float[Tensor, "1 1 D H W"],
labeldata: Float[Tensor, "1 1 D H W"],
voxel_to_world: Float[Tensor, "4 4"],
world_to_voxel: Float[Tensor, "4 4"],
voxel_to_grid: Float[Tensor, "4 4"],
isocenter: Float[Tensor, 3],
max_label: int | None = None,
**mu
)
Wrapper for a CT volume and (optional) labelmap that is compatible with torch's
grid_sample.
Fuses all spatial transforms required for sampling (world → voxel → grid) so
that rendering only needs to perform a single matmul.
Updating any of convert_to_mu, mu_water, mu_bone, or hu_bone automatically
recomputes the HU to linear attenuation coefficient (LAC) conversion.
Source code in src/nanodrr/data/subject.py
image
property
¶
Volume in units of LACs (or raw values if convert_to_mu is False).
Result is cached and recomputed only when conversion params change.
set_mu
¶
set_mu(
mu_water: float | Tensor | None = None,
mu_bone: float | Tensor | None = None,
hu_bone: float | Tensor | None = None,
) -> None
Recompute the LAC cache from new conversion parameters.
Prefer this over setting mu_water, mu_bone, and hu_bone individually
when calling under torch.compile, as it performs a single tensor op and
avoids Python-side attribute mutations that would cause graph breaks.
The stored scalar params (self.mu_water etc.) are intentionally not
updated here; they remain as the baseline defaults.
Source code in src/nanodrr/data/subject.py
from_filepath
classmethod
¶
from_filepath(
imagepath: str | Path,
labelpath: str | Path | None = None,
max_label: int | None = None,
**mu
) -> Subject
Load a subject from any TorchIO-supported file path.
| PARAMETER | DESCRIPTION |
|---|---|
imagepath
|
Path to the CT volume. |
labelpath
|
Optional path to a label map. |
max_label
|
Override the maximum label index. If provided,
TYPE:
|
**mu
|
HU → μ conversion params. See
DEFAULT:
|
Source code in src/nanodrr/data/subject.py
from_images
classmethod
¶
from_images(
image: ScalarImage,
label: LabelMap | None = None,
max_label: int | None = None,
**mu
) -> Subject
Construct a subject from TorchIO image objects.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
CT volume as a
TYPE:
|
label
|
Optional segmentation as a
TYPE:
|
max_label
|
Override the maximum label index. If provided,
TYPE:
|
**mu
|
HU → μ conversion params (
DEFAULT:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If any unrecognised key is passed via |
Source code in src/nanodrr/data/subject.py
nanodrr.data.preprocess
¶
hu_to_mu
¶
hu_to_mu(
data: Float[Tensor, "1 1 D H W"],
mu_water: float | Tensor = MU_WATER,
mu_bone: float | Tensor = MU_BONE,
hu_bone: float | Tensor = HU_BONE,
) -> Float[Tensor, "1 1 D H W"]
Convert Hounsfield units to linear attenuation coefficients.
Uses bilinear scaling with air-water model for HU ≤ 0 and water-bone model for HU > 0:
| PARAMETER | DESCRIPTION |
|---|---|
data
|
CT volume in Hounsfield Units with shape (1, 1, D, H, W).
TYPE:
|
mu_water
|
Linear attenuation coefficient of water [1/mm] at target energy. Default 0.0192 corresponds to ~70 keV (typical CT effective energy).
TYPE:
|
mu_bone
|
Linear attenuation coefficient of cortical bone [1/mm] at target energy. Default 0.0573 corresponds to ~70 keV.
TYPE:
|
hu_bone
|
HU value corresponding to pure cortical bone. Default 1000. Typical range is 1000-2000 depending on bone type and scanner.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Float[Tensor, '1 1 D H W']
|
Linear attenuation coefficients [1/mm] with same shape as input. |
References
NIST XCOM database for mass attenuation coefficients. Water density: 1.0 g/cm³, cortical bone density: 1.92 g/cm³.
Source code in src/nanodrr/data/preprocess.py
nanodrr.data.demo
¶
download_deepfluoro
¶
Download a subject from the DeepFluoro dataset.