skrt.simulation module

Classes for creating synthetic images with simple geometries.

class skrt.simulation.Cuboid(side_length, centre, intensity, name=None)

Bases: object

Class representing a cuboid.

__init__(side_length, centre, intensity, name=None)
get_data(coords)
class skrt.simulation.Cylinder(radius, length, axis, centre, intensity, name=None)

Bases: object

Class representing a cylinder.

__init__(radius, length, axis, centre, intensity, name=None)
get_data(coords)
class skrt.simulation.Grid(shape, spacing, thickness, intensity, axis=None, name=None)

Bases: object

Class representing a grid.

__init__(shape, spacing, thickness, intensity, axis=None, name=None)
get_data(_)
class skrt.simulation.ShapeGroup(shapes, name)

Bases: object

Class for grouping multiple shapes, to be represented by a single ROI.

__init__(shapes, name)
add_shape(shape)
get_data(coords)
class skrt.simulation.Sphere(radius, centre, intensity, name=None)

Bases: object

Class representing a sphere.

__init__(radius, centre, intensity, name=None)
get_data(coords)
class skrt.simulation.SyntheticImage(shape, filename=None, origin=(0, 0, 0), voxel_size=(1, 1, 1), intensity=-1000, noise_std=None, auto_timestamp=False)

Bases: Image

Class for creating synthetic image data with simple geometric shapes.

__init__(shape, filename=None, origin=(0, 0, 0), voxel_size=(1, 1, 1), intensity=-1000, noise_std=None, auto_timestamp=False)

Create an initially blank synthetic image to which geometric ROIs can be added.

Parameters:

shapeint/tuple

Dimensions of the image array to create in order (x, y, z). If an int is given, the image will be created with dimensions (shape, shape, shape).

filenamestr, default=None

Name of output file or directory. If given, the file will automatically be written; otherwise, no file will be written until the ‘write’ method is called.

origintuple, default=(0, 0, 0)

Origin in mm for the image in order (x, y, z).

voxel_sizetuple, default=(1, 1, 1)

Voxel sizes in mm for the image in order (x, y, z).

intensityfloat, default=-1000

Intensity in HU for the background of the image.

noise_stdfloat, default=None

Standard deviation of Gaussian noise to apply to the image. If None, no noise will be applied.

auto_timestampbool default=False

If true and no valid timestamp is found within the path string, timestamp generated from current date and time.

add_cube(side_length, centre=None, intensity=0, is_roi=None, name=None, above=True, group=None)
add_cuboid(side_length, centre=None, intensity=0, is_roi=None, name=None, above=True, group=None)
add_cylinder(radius, length, axis='z', centre=None, intensity=0, is_roi=None, name=None, above=True, group=None)
add_grid(spacing, thickness=1, intensity=0, axis=None, name=None, above=True)
add_shape(shape, shape_type, is_roi, above, group)
add_sphere(radius, centre=None, intensity=0, is_roi=None, name=None, above=True, group=None)
apply_noise(array)

Apply background noise to an array.

get_background(with_noise=True)

Make blank image array or noisy array.

get_data(*args, **kwargs)

Get Image data.

get_image()

Get self as an Image object.

get_roi(name)

Get a named ROI as an ROI object.

get_roi_data()

Get dict of ROIs and names with any transformations applied.

get_rois()

Get list of all owned ROI objects.

get_structure_set()

Return StructureSet containing own structures.

hu_to_mu(mu_water=None)

Assume that image greyscale values are Hounsfield units, and convert to linear attenuation coefficients.

Parameter:

mu_waterfloat, default=None

Value to be used for the linear attenuation coefficient of water. If None, set to the value of skrt.core.Defaults().mu_water.

Note: Image linear attenuation coefficients will be returned in the same units as <mu_water>. For consistency with the units used for image voxel dimensions, it’s recommended that <mu_water> be specified in units of mm^-1.

mu_to_hu(mu_water=None)

Assume that image greyscale values are linear attenuation coefficients, and convert to Hounsfield units.

Parameter:

mu_waterfloat, default=None

Value to be used for the linear attenuation coefficient of water. If None, set to the value of skrt.core.Defaults().mu_water.

Note: Image linear attenuation coefficients and <mu_water> should have the same units. For consistency with the units used for image voxel dimensions, it’s recommended that linear attenuation coefficients be specified in units of mm^-1.

plot(**kwargs)

Plot the current image with ROIs overlaid.

rescale(v_min=0.0, v_max=1.0, constant=0.5)

Linearly rescale image greyscale values, so that they span a specified range.

Rescaling is applied also to greyscale values for shapes.

Parameters:

v_min: float, default=0.0

Minimum greyscale value after rescaling.

v_max: float, default=1.0

Maximum greyscale value after rescaling.

constant: float, default=0.5

Greyscale value to assign after rescaling if all values in the original image are the same. If None, original value is kept.

reset()

Remove all shapes.

reset_transforms()

Remove any rotations or translations.

rotate(yaw=0, pitch=0, roll=0)

Set a rotation to apply to the final image.

set_noise_std(std)

Set noise standard deviation.

translate(dx=0, dy=0, dz=0)

Set a translation to apply to the final image.

update()

Update self.data so that it contains all current shapes.

update_roi(name)

Update an ROI to ensure it has the correct data.

update_rois()

Update all ROIs to have the correct data.

view(images=None, **kwargs)

View self with BetterViewer along with any additional images in <images>. Any **kwargs will be passed to BetterViewer initialisation.

write(outname=None, overwrite_roi_dir=False)

Write image data to an output file.

write_dicom_dataset(patient_id='001', outdir='synthetic_dicom', modality='CT', series_number=1, structure_set_label='Geometric structures', root_uid=None, verbose=False)

Write image and associated structure set as a patient DICOM dataset.

Parameters:

patient_id: str, default=”001”

Patient identifier to be assigned to the dataset.

outdir: str/pathlib.Pat, default=”synthetic_dicom”

Path to directory where dataset is to be written.

modality: str, default=”CT”

Modality to be assigned to synthetic image.

series_number: int, default=1

Series number to be assigned to synthetic image.

structure_set_label: str, default=”Geometric structures”

Label to be assigned to structure set, if present.

root_uidstr, default=None

Root to be used in Globally Unique Identifiers (GUIDs). This should uniquely identify the institution or group generating the GUID. If None, the value of pydicom.uid.PYDICOM_ROOT_UID is used.

verbosebool, default=False

If True, print information about progress of dataset writing.

skrt.simulation.get_value(val)
skrt.simulation.get_values(vals)
skrt.simulation.make_grid(image, spacing=(30, 30, 30), thickness=(2, 2, 2), background=-1000, foreground=1000, voxel_units=False)

Create a synthetic image of a grid pattern for a specified image.

Parameters:

imageskrt.image.Image

Reference image, for which grid pattern is to be created.

spacingtuple, default=(30, 30, 30)

Spacing along (x, y, z) directions of grid lines. If voxel_units is True, values are taken to be in numbers of voxels. Otherwise, values are taken to be in the same units as the voxel dimensions of the reference image.

thickness: tuple, default=(2, 2, 2)

Thickness along (x, y, z) directions of grid lines. If voxel_units is True, values are taken to be in numbers of voxels. Otherwise, values are taken to be in the same units as the voxel dimensions of the reference image.

background: int/float, default=-1000

Intensity value to be assigned to voxels not on grid lines.

foreground: int/float, default=1000

Intensity value to be assigned to voxels on grid lines.

voxel_units: bool, default=False

If True, values for spacing and thickness are taken to be in numbers of voxels. If False, values for spacing and thickness are taken to be in the same units as the voxel dimensions of the reference image.

skrt.simulation.make_head(shape=(256, 256, (50, 80)), origin=None, voxel_size=(1, 1, 3), noise_std=10, intensity=-1000)

Create an image featuring a loose approximation of a head.

In the parameters described below, each value may be a single number or a two-element tuple. In the latter case, the number used will be drawn at random from a uniform distribution over the range from the first element to the second.

Parameters: shape : int/tuple

Dimensions of the image array to create in order (x, y, z). If an int is given, the image will be created with dimensions (shape, shape, shape).

origintuple, default=None

Origin in mm for the image in order (x, y, z). If None, set the origin so that that the image centre is at (0, 0, 0).

voxel_sizetuple, default=(1, 1, 3)

Voxel sizes in mm for the image in order (x, y, z).

noise_stdfloat, default=None

Standard deviation of Gaussian noise to apply to the image. If None, no noise will be applied.

intensityfloat, default=-1000

Intensity in HU for the background of the image.