Astra-toolbox Module

Tomography utility functions using the ASTRA toolbox

This module provides a collection of functions for generating sinograms, reconstructing 2D and 3D tomographic datasets, forward projections, and computing system matrices using the ASTRA toolbox. It supports both parallel-beam and cone-beam geometries, GPU acceleration, and multiple reconstruction algorithms including FBP, SIRT, and FDK.

Author: Antony Vamvakeros

nDTomo.tomo.astra_tomo.ConeBeamCTGeometry(downSizeFactor=4, distance_source_detector=926.79, distance_source_origin=349.565, detector_pixel_size=0.1, mag_factor=2.65, horizontalOffset=0, verticalOffset=0)[source]

Define cone-beam CT geometry.

Parameters:
  • downSizeFactor (int, optional) – Downsampling factor for resolution.

  • distance_source_detector (float) – Distance from source to detector [mm].

  • distance_source_origin (float) – Distance from source to rotation center [mm].

  • detector_pixel_size (float) – Pixel size of detector [mm].

  • mag_factor (float) – Magnification factor.

  • horizontalOffset (float) – Horizontal offset in pixels.

  • verticalOffset (float) – Vertical offset in pixels.

Returns:

geo – Geometry dictionary.

Return type:

dict

nDTomo.tomo.astra_tomo.ConeBeamCTbeam(sf=15, whiteCounts=4500, countsToCut=1)[source]

Define beam parameters for cone-beam CT simulation.

Parameters:
  • sf (float) – Scaling factor for attenuation.

  • whiteCounts (float) – Maximum detector counts.

  • countsToCut (float) – Threshold for zeroing low-intensity counts.

Returns:

beam – Beam parameter dictionary.

Return type:

dict

nDTomo.tomo.astra_tomo.astra_Amatrix(ntr, ang)[source]

Generate a sparse system matrix A for 2D parallel-beam geometry using ASTRA.

Parameters:
  • ntr (int) – Number of translation steps (image size).

  • ang (array_like) – Array of projection angles in radians.

Returns:

A – Sparse system matrix (size: [n_projections * detector_pixels, ntr^2]).

Return type:

scipy.sparse.csr_matrix

nDTomo.tomo.astra_tomo.astra_create_geo(sino, theta=None)[source]

Set up volume and projection geometry and create a reconstruction volume ID.

Parameters:
  • sino (ndarray) – 2D sinogram array (shape: [translations, angles]).

  • theta (ndarray, optional) – Array of angles in radians.

Returns:

  • proj_geom (dict) – ASTRA projection geometry.

  • rec_id (int) – ASTRA volume ID.

  • proj_id (int) – ASTRA projector ID.

nDTomo.tomo.astra_tomo.astra_create_sino(im, npr=None, scanrange='180', theta=None, proj_id=None)[source]

Generate a 2D sinogram from an image using ASTRA Toolbox.

Parameters:
  • im (ndarray) – 2D input image.

  • npr (int, optional) – Number of projection angles. Defaults to image width.

  • scanrange (str, optional) – Range of scan angles (‘180’ or ‘360’) if theta is not provided.

  • theta (ndarray, optional) – Projection angles in radians. Overrides scanrange if provided.

  • proj_id (int, optional) – Pre-computed projector ID. If None, will be created.

Returns:

sinogram – 2D sinogram with shape (n_angles, n_pixels).

Return type:

ndarray

nDTomo.tomo.astra_tomo.astra_create_sino_geo(im, theta=None)[source]

Create an ASTRA projector ID for 2D parallel beam geometry.

Parameters:
  • im (ndarray) – 2D image array.

  • theta (ndarray, optional) – Array of projection angles in radians. If None, defaults to linspace(0, pi, im.shape[0]).

Returns:

proj_id – ASTRA projector ID for the given geometry.

Return type:

int

nDTomo.tomo.astra_tomo.astra_create_sinostack(vol, npr=None, scanrange='180', theta=None, proj_id=None, dtype='float32')[source]

Create a sinogram stack from a 3D volume using ASTRA.

Parameters:
  • vol (ndarray) – 3D volume (shape: [N, N, num_slices]).

  • npr (int, optional) – Number of projection angles. Defaults to volume width.

  • scanrange (str, optional) – Scan angle range (‘180’ or ‘360’).

  • theta (ndarray, optional) – Projection angles in radians.

  • proj_id (int, optional) – Reuse existing ASTRA projector ID.

  • dtype (str, optional) – Data type for output (default: ‘float32’).

Returns:

sinograms – Sinogram stack with shape (n_angles, N, num_slices).

Return type:

ndarray

nDTomo.tomo.astra_tomo.astra_rec_2vols(sinos, method='FBP_CUDA', filt='Ram-Lak')[source]

Reconstruct two interleaved volumes from alternating angle subsets.

Parameters:
  • sinos (ndarray) – 3D array with shape (z, projections, x).

  • method (str, optional) – Reconstruction method (default: ‘FBP_CUDA’).

  • filt (str, optional) – Filter type (default: ‘Ram-Lak’).

Returns:

  • vol1 (ndarray) – Volume reconstructed from even-indexed angles.

  • vol2 (ndarray) – Volume reconstructed from odd-indexed angles.

nDTomo.tomo.astra_tomo.astra_rec_alg(sino, proj_geom, rec_id, proj_id, method='FBP', filt='Ram-Lak')[source]

Reconstruct a 2D image from a sinogram using the specified ASTRA algorithm.

Parameters:
  • sino (ndarray) – 2D sinogram.

  • proj_geom (dict) – ASTRA projection geometry.

  • rec_id (int) – ASTRA volume ID for output.

  • proj_id (int) – ASTRA projector ID.

  • method (str, optional) – Reconstruction algorithm (‘FBP’, ‘SIRT’, etc.).

  • filt (str, optional) – Filter type (only for FBP).

Returns:

rec – 2D reconstructed image.

Return type:

ndarray

nDTomo.tomo.astra_tomo.astra_rec_single(sino, theta=None, scanrange='180', method='FBP_CUDA', filt='Ram-Lak', nits=None, timing=False)[source]

Reconstruct a single 2D slice from a sinogram using ASTRA Toolbox.

Parameters:
  • sino (ndarray) – 2D sinogram (shape: [translation_steps, projections]).

  • theta (ndarray, optional) – Projection angles in radians. If None, generated from scanrange.

  • scanrange (str, optional) – Angle range (‘180’ or ‘360’) to auto-generate theta if not provided.

  • method (str, optional) – ASTRA reconstruction method, e.g., ‘FBP_CUDA’, ‘SIRT’, ‘CGLS’.

  • filt (str, optional) – Filter type for FBP-based methods (e.g., ‘Ram-Lak’, ‘Hann’).

  • nits (int, optional) – Number of iterations (for iterative methods).

  • timing (bool, optional) – Print reconstruction time if True.

Returns:

rec – Reconstructed 2D image.

Return type:

ndarray

nDTomo.tomo.astra_tomo.astra_rec_vol(sinos, scanrange='180', theta=None, proj_geom=None, proj_id=None, rec_id=None, method='FBP_CUDA', filt='Ram-Lak', pbar=True)[source]

Reconstruct a 3D volume from a stack of sinograms using ASTRA.

Parameters:
  • sinos (ndarray) – 3D stack of sinograms (shape: [N, angles, slices]).

  • scanrange (str, optional) – Scan angle range (‘180’ or ‘360’).

  • theta (ndarray, optional) – Array of angles in radians.

  • proj_geom (dict, optional) – Predefined projection geometry.

  • proj_id (int, optional) – Predefined projector ID.

  • rec_id (int, optional) – Predefined reconstruction volume ID.

  • method (str, optional) – Reconstruction algorithm (default: ‘FBP_CUDA’).

  • filt (str, optional) – Filter type.

  • pbar (bool, optional) – Show progress bar.

Returns:

rec – 3D reconstructed volume.

Return type:

ndarray

nDTomo.tomo.astra_tomo.astra_rec_vol_singlesino(sino, ims=100, ofs=None, scanrange='180', proj_geom=None, proj_id=None, rec_id=None, method='FBP_CUDA', filt='Ram-Lak')[source]

Reconstruct multiple volumes from a single sinogram using random angular offsets and interleaved angle subsets.

This function generates two 3D volumes by randomly rotating the full-angle set (via ofs) and reconstructing from interleaved projections (even vs odd indices) at each iteration.

Parameters:
  • sino (ndarray) – 2D sinogram (shape: [translation_steps, projections]).

  • ims (int, optional) – Number of volumes to reconstruct (default: 100).

  • ofs (ndarray, optional) – Random angular offsets (length: ims). If None, generated uniformly in [0,1).

  • scanrange (str, optional) – ‘180’ or ‘360’ scan in degrees (default: ‘180’).

  • proj_geom (dict, optional) – ASTRA projection geometry, will be overwritten inside loop.

  • proj_id (int, optional) – ASTRA projector ID (ignored, recomputed each iteration).

  • rec_id (int, optional) – ASTRA reconstruction volume ID.

  • method (str, optional) – ASTRA reconstruction algorithm (default: ‘FBP_CUDA’).

  • filt (str, optional) – Filter type for FBP methods (default: ‘Ram-Lak’).

Returns:

  • rec (ndarray) – Reconstructed volumes from even-indexed angles (shape: [H, W, ims]).

  • rec2 (ndarray) – Reconstructed volumes from odd-indexed angles (shape: [H, W, ims]).

nDTomo.tomo.astra_tomo.coneBeamFDK(projections, geo, v_cut=0)[source]

Reconstruct a 3D volume from cone-beam projections using FDK algorithm.

Parameters:
  • projections (ndarray) – 3D cone-beam sinograms (shape: [detector_rows, angles, detector_cols]).

  • geo (dict) – Geometry dictionary returned by ConeBeamCTGeometry().

  • v_cut (float, optional) – Vertical cropping factor (fraction from top, default: 0).

Returns:

reconstruction – 3D reconstructed volume (ASTRA volume geometry).

Return type:

ndarray

nDTomo.tomo.astra_tomo.coneBeamFP(vol, nproj, geo, v_cut=0, detector_cols=None, detector_rows=None)[source]

Perform forward projection of a 3D volume using cone-beam geometry (ASTRA GPU-based).

Projects the input volume to generate synthetic cone-beam radiographs over a full 360° rotation.

Parameters:
  • vol (ndarray) – 3D volume to project (shape: [detector_rows, _, detector_cols]).

  • nproj (int) – Number of projection angles (evenly spaced over 0–2π).

  • geo (dict) – Geometry dictionary (as returned by ConeBeamCTGeometry()).

  • v_cut (float, optional) – Fraction of the volume to crop from the top (default: 0).

  • detector_cols (int, optional) – Number of horizontal detector pixels. If None, inferred from vol.

  • detector_rows (int, optional) – Number of vertical detector pixels. If None, inferred from vol.

Returns:

proj_data – 3D array of cone-beam projections (shape: [detector_rows, nproj, detector_cols]).

Return type:

ndarray

nDTomo.tomo.astra_tomo.coneBeamFP_FDK(vol, nproj, geo, beam=None, v_cut=0)[source]

Perform forward projection of a 3D volume and reconstruct it using FDK.

Optionally applies exponential attenuation and Poisson noise before reconstruction.

Parameters:
  • vol (ndarray) – 3D volume to be projected (shape: [H, W, D]).

  • nproj (int) – Number of projection angles.

  • geo (dict) – Cone-beam CT geometry.

  • beam (dict, optional) – Beam model for forward projection (see ConeBeamCTbeam()).

  • v_cut (float, optional) – Fraction of top pixels to crop from vertical axis.

Returns:

reconstruction – 3D volume reconstructed from forward projections using FDK.

Return type:

ndarray

nDTomo.tomo.astra_tomo.create_Amat(npix, ang)[source]

Construct a system matrix A using ASTRA’s sparse matrix projector.

Parameters:
  • npix (int) – Number of pixels in one dimension (volume assumed square).

  • ang (ndarray) – Projection angles in radians.

Returns:

  • Acoo (scipy.sparse.coo_matrix) – Sparse matrix in coordinate format.

  • values (ndarray) – Non-zero values of the sparse matrix.

  • indices (ndarray) – Row and column indices of non-zero elements (shape: [2, nnz]).

  • shape (tuple) – Shape of the full matrix.

nDTomo.tomo.astra_tomo.dummy(*args, **kwargs)[source]
nDTomo.tomo.astra_tomo.radiographs_mu(projections, geo, beam)[source]

Convert line integrals to intensity using beam attenuation, then re-log.

Parameters:
  • projections (ndarray) – Projection data (line integrals).

  • geo (dict) – CT geometry.

  • beam (dict) – Beam parameters.

Returns:

P – Log-normalized projection data.

Return type:

ndarray