Skip to content

Dose contours

plotting

Dose contours: RADDOSE-3D's DoseState.R, drawn with matplotlib.

RADDOSE-3D writes an R script that draws the final dose as nested surfaces -- everything above 0.1 MGy, above 20 and above 30 -- in a box the size of the crystal, and saves five views of them; it needs R's rgl and misc3d. This draws the same surfaces, in the same colours, with matplotlib::

pip install "pyRADDOSE-3D[plot]"

from a :class:~pyraddose_3d.solve.Simulation, or from a DoseState.csv written earlier, so a finished run can be drawn again without running it.

Matplotlib is imported only by the functions that draw, so the rest of the package never needs it.

The surfaces

:func:isosurface finds them by marching tetrahedra: each cube of eight neighbouring voxel centres is cut into six tetrahedra, and the dose is interpolated linearly along each edge that crosses the level. Neighbouring cubes cut their shared face along the same diagonal, so the surface has no cracks. The voxels outside the crystal are dose zero, and so is a layer added round the grid, so every surface closes, as the R script's do.

DoseField dataclass

The final dose on the voxel grid.

Attributes:

  • dose (ndarray) –

    (nx, ny, nz), in MGy; zero outside the crystal.

  • x, y, z (ndarray) –

    The voxel centres along each axis, in micrometres: z along the beam, y the rotation axis.

from_simulation classmethod

from_simulation(simulation: Simulation) -> DoseField

The dose at the end of a run, on the run's whole grid.

Parameters:

  • simulation (Simulation) –

    From :func:pyraddose_3d.solve.simulate.

Returns:

  • DoseField –

    The dose DoseState.csv lists, and zero outside the crystal.

read_dose_state classmethod

read_dose_state(path: str | Path) -> DoseField

Read a DoseState.csv: x,y,z,dose,fluence,elastic per voxel.

Parameters:

  • path (str | Path) –

    A DoseState.csv, from :func:pyraddose_3d.outputs.write_outputs or programs/dose/run_dose.py.

Returns:

  • DoseField –

    The voxels it lists, on their grid.

from_voxels classmethod

from_voxels(
    coordinates: ndarray, dose: ndarray
) -> DoseField

Put voxels listed by position, as DoseState.csv lists them, on a grid.

Parameters:

  • coordinates (ndarray) –

    (N, 3) voxel centres in micrometres, on a regular grid.

  • dose (ndarray) –

    (N,), in MGy.

Returns:

  • DoseField –

    The smallest grid holding them; a voxel not listed is zero.

isosurface

isosurface(field: DoseField, level: float) -> np.ndarray

The surface where the dose crosses level.

Parameters:

  • field (DoseField) –

    The dose.

  • level (float) –

    In MGy, above zero: everything outside the crystal is zero.

Returns:

  • ndarray –

    (T, 3, 3) triangles: corner, then x, y, z in micrometres. Each is wound so that its normal points from the higher dose to the lower. Empty if no voxel reaches the level.

plot_dose_contours

plot_dose_contours(
    source: Simulation | DoseField | str | Path,
    levels: Sequence[float] = CONTOUR_LEVELS,
    colours: Sequence[Any] = CONTOUR_COLOURS,
    opacities: Sequence[float] = CONTOUR_OPACITIES,
    view: tuple[float, float] = VIEWS[2],
    ax: Any = None,
) -> Axes

Draw the dose as DoseState.R does: a surface at each level.

Parameters:

  • source (Simulation | DoseField | str | Path) –

    A run, its dose, or the path of a DoseState.csv.

  • levels (Sequence[float], default: CONTOUR_LEVELS ) –

    Doses in MGy, each above zero. DoseState.R's by default.

  • colours (Sequence, default: CONTOUR_COLOURS ) –

    A matplotlib colour for each level.

  • opacities (Sequence[float], default: CONTOUR_OPACITIES ) –

    Each level's opacity, from 0 to 1.

  • view (tuple[float, float], default: VIEWS[2] ) –

    (theta, phi) in degrees, as in :data:VIEWS.

  • ax (Axes3D, default: None ) –

    A 3D axes to draw in. A new figure's, by default.

Returns:

  • Axes –

    The axes drawn in. The rotation axis, y, points up, and the beam runs along z. A box marks the crystal's extent, as the R script's does.

save_dose_contours

save_dose_contours(
    source: Simulation | DoseField | str | Path,
    prefix: str = "output-",
    views: Sequence[tuple[float, float]] = VIEWS,
    **options: Any,
) -> list[Path]

Save a picture of the contours from each view, as DoseState.R does.

Parameters:

  • source (Simulation | DoseField | str | Path) –

    A run, its dose, or the path of a DoseState.csv.

  • prefix (str, default: 'output-' ) –

    Prepended to each file name, as write_outputs's is.

  • views (Sequence[tuple[float, float]], default: VIEWS ) –

    (theta, phi) for each picture; DoseState.R's five by default.

  • **options (Any, default: {} ) –

    levels, colours and opacities, as for :func:plot_dose_contours.

Returns:

  • list[Path] –

    DoseContours_<theta>_<phi>.png for each view -- the R script's plot_<theta>_<phi>.png -- after the prefix.