ai_ct_scans.data_loading module

class ai_ct_scans.data_loading.BodyPartLoader(root_dir, body_part, rescale_on_load=True)

Bases: object

A class providing intuitive loading of DICOM data, scan-order-wise for a particular body part

scan_1

A ScanLoader for the former scan of a particular body part

Type

ScanLoader

scan_2

A ScanLoader for the latter scan of a particular body part

Type

ScanLoader

class ai_ct_scans.data_loading.MultiPatientAxialStreamer

Bases: object

Class for streaming either random or sequential axial images from the CT dataset

reset_indices()

Set all the indices back to zero - useful for switching from random to sequential streaming

stream_next(threshold=None, random=False)

Get the next image from the streamer

Parameters
  • threshold (int or None) – Minimum value within a streamed image to accept the streamed image. If this

  • met (threshold isn't) –

  • above (move on to the next image in the stream until one is found that has pixels) –

  • threshold=500 (this thres. Useful for ignoring all-air images in the dataset with) –

  • random (bool) – Whether to randomly select images from the dataset (True) or step through sequentially

  • (False)

Returns

2D image

Return type

(np.ndarray)

class ai_ct_scans.data_loading.MultiPatientLoader(data_directory=None, rescale_on_load=True)

Bases: object

A class providing intuitive loading of DICOM data patient-wise, scan-order-wise and body-region-wise

root_dir

Path to the top level directory that stores multiple patient scans

Type

pathlib Path

patient_paths

A natsorted list of paths to directories of patients, where each patient is expected to be stored in a purely numeric directory name

Type

list of pathlib Paths

patients

A list of PatientLoaders, one per patient in patient_paths, with the same ordering

Type

list of PatientLoaders

class ai_ct_scans.data_loading.PatientLoader(root_dir, rescale_on_load=True)

Bases: object

A class providing intuitive loading of DICOM data patient-wise, scan-order-wise and body-region-wise

abdo

A BodyPartLoader for the abdomen of a patient

Type

BodyPartLoader

class ai_ct_scans.data_loading.ScanLoader(root_dir, rescale_on_load=True)

Bases: object

A class providing intuitive loading of DICOM data, for a single scan for a single body part

dicom_paths

All the paths to dicom files within a given scan directory

Type

list of pathlib Paths

full_scan

The entire scan loaded as a 3D ndarray

Type

np.ndarray or None

clear_scan()

Sets the full_scan to none

delete_memmap()
full_scan_to_memmap(directory=None, normalise=True)

Send the 3D data to two numpy memmaps, with filename-embedded shapes that allows for reloading. Default to the same directory as the DICOM data. Second memmap is first and second axes (0-indexed) swapped, to enable faster slicing during DL training

Parameters
  • normalise (bool) – whether to normalise the data upon saving to memmap

  • directory (pathlib Path) – directory in which to save the memmap

Returns:

load_2d_array(index, ignore_rescale=False, ignore_z_rescale=False)

Loads the pixel array from the dicom at position index in self.dicom_paths

Parameters
  • ignore_z_rescale (bool) – If True, do not store the z location of the slice for later axial rescaling in the

  • view. (full 3D) –

  • ignore_rescale (bool) – If True, load raw data without rescaling, otherwise rescale in accordance with state

  • self.rescale_on_load (of) –

  • index (int) – Scans are ordered I0 … IN in the original directory. This arg sets the N to load

Returns

(np.ndarray) A 2D slice of the scan

load_full_memmap(normalise=True)

Loads the numpy memmapped version of the full scan, or creates it if it doesn’t yet exist

Returns (None):

load_memmap_and_clear_scan(normalise=True)

Loads the memmap of the scan and clears the in-memory scan

Returns (None):

load_patient_metadata()

Load relevant patient metadata.

load_scan()

Loads the entire 3D scan into memory, optionally rescaling transverse plane views to build the stack according to whether self.rescale_on_load is True or False

Returns

The full 3D data of the scan, excluding metadata

Return type

(np.ndarray)

mean_axial_thickness()

Get the average axial layer thickness of the scan

Returns

The average thickness of axial layers in the scan

Return type

(float)

raw_transverse_pixel_spacing_and_shape()

Get the original pixel array shape for the transverse plane of the scan, its pixel spacing

Returns (tuple of tuple of ints and ndarray):

The original pixel array shape for the transverse plane of the scan and its pixel spacing

rescale_depth()

Rescale the full 3D scan along depth based on the ContentTime difference between last and first scans and total slice number

ai_ct_scans.data_loading.data_root_directory()

Get a path to the top level data directory. Checks whether you have created the local store of data at the expected location and returns that as default if found, otherwise returns the location on the shared folder

Returns

A path

Return type

(pathlib Path)

ai_ct_scans.data_loading.dir_dicom_paths(directory)

Return list of paths to files within a directory at any level below, beginning with ‘I’. This is the naming convention expected for the project.

Parameters

directory (pathlib Path) – A path

Returns

(list of pathlib Paths) All paths to files beginning with ‘I’ within the given directory

ai_ct_scans.data_loading.load_memmap(path, dtype='float64')

Load a memmap array with an expected naming convention of the form [prefix]__[shape element 1]_[shape_element 2]_…_[shape element n].npy

Parameters
  • path (pathlib Path) – path to the memmap

  • dtype (str or valid numpy dtype) – the data type the memmap was saved as

Returns

A memmap ndarray

Return type

(np.memmap)

ai_ct_scans.data_loading.load_validation_set()