Python API

Segmentation API

Spinal Cord Segmentation API

deepseg_sc API

Model.

Created: 2017-12 Last changes: 2018-01-24 Contributors: charley

GM/WM Segmentation API

deepseg_gm API

class spinalcordtoolbox.deepseg_gm.deepseg_gm.CroppedRegion(original_shape, starts, crops)[source]

This class holds cropping information about the volume center crop.

pad(image)[source]

This method will pad an image using the saved cropped region.

Parameters:image – the image to pad.
Returns:padded image.
class spinalcordtoolbox.deepseg_gm.deepseg_gm.DataResource(dirname)[source]

This class is responsible for resource file management (such as loding models).

get_file_path(filename)[source]

Get the absolute file path based on the data root directory.

Parameters:filename – the filename.
spinalcordtoolbox.deepseg_gm.deepseg_gm.check_backend()[source]

This function will check for the current backend and then it will warn the user if the backend is theano.

spinalcordtoolbox.deepseg_gm.deepseg_gm.crop_center(img, cropx, cropy)[source]

This function will crop the center of the volume image.

Parameters:
  • img – image to be cropped.
  • cropx – x-coord of the crop.
  • cropy – y-coord of the crop.
Returns:

(cropped image, cropped region)

spinalcordtoolbox.deepseg_gm.deepseg_gm.segment_file(input_filename, output_filename, model_name, verbosity)[source]

Segment a volume file.

Parameters:
  • input_filename – the input filename.
  • output_filename – the output filename.
  • model_name – the name of model to use.
  • verbosity – the verbosity level.
Returns:

the output filename.

spinalcordtoolbox.deepseg_gm.deepseg_gm.segment_volume(ninput_volume, model_name)[source]

Segment a nifti volume.

Parameters:
  • ninput_volume – the input volume.
  • model_name – the name of the model to use.
Returns:

segmented slices.

spinalcordtoolbox.deepseg_gm.deepseg_gm.threshold_predictions(predictions, thr=0.999)[source]

This method will threshold predictions.

Parameters:thr – the threshold.
Returns:thresholded predictions.
spinalcordtoolbox.deepseg_gm.model.dice_coef(y_true, y_pred)[source]

Dice coefficient specification

Parameters:
  • y_true – ground truth.
  • y_pred – predictions.
spinalcordtoolbox.deepseg_gm.model.dice_coef_loss(y_true, y_pred)[source]

Dice loss specification.

Parameters:
  • y_true – ground truth.
  • y_pred – predictions.

Centerline API

spinalcordtoolbox.centerline

spinalcordtoolbox.centerline.optic.centerline2roi(fname_image, folder_output='./', verbose=0)[source]

Tis method converts a binary centerline image to a .roi centerline file

Parameters:
  • fname_image – filename of the binary centerline image, in RPI orientation
  • folder_output – path to output folder where to copy .roi centerline
  • verbose – adjusts the verbosity of the logging.
Returns:

filename of the .roi centerline that has been created

spinalcordtoolbox.centerline.optic.detect_centerline(image_fname, contrast_type, optic_models_path, folder_output, remove_temp_files=False, init_option=None, output_roi=False, verbose=0)[source]

This method will use the OptiC to detect the centerline.

Parameters:
  • image_fname – The input image filename.
  • init_option – Axial slice where the propagation starts.
  • contrast_type – The contrast type.
  • optic_models_path – The path with the Optic model files.
  • folder_output – The OptiC output folder.
  • remove_temp_files – Remove the temporary created files.
  • verbose – Adjusts the verbosity of the logging.
Returns:

The OptiC output filename.

Image Processing API

spinalcordtoolbox.resample

spinalcordtoolbox.resample.nipy_resample.resample_file(fname_data, fname_out, new_size, new_size_type, interpolation, verbose)[source]

This function will resample the specified input image file to the target size.

Parameters:
  • fname_data – The input image filename.
  • fname_out – The output image filename.
  • new_size – The target size, i.e. 0.25x0.25
  • new_size_type – Unit of resample (mm, vox, factor)
  • interpolation – The interpolation type
  • verbose – verbosity level
spinalcordtoolbox.resample.nipy_resample.resample_image(input_image, new_size, new_size_type, interpolation='linear', verbose=1)[source]

This function will resample the specified input image to the target size.

Parameters:
  • input_image – The input image.
  • new_size – The target size, i.e. ‘0.25x0.25’
  • new_size_type – Unit of resample (mm, vox, factor)
  • interpolation – The interpolation type
  • verbose – Verbosity level
Returns:

The resampled image.

Quality Control API

The modules spinalcordtoolbox.reports.qc and spinalcordtoolbox.reports.slice are used to generate Quality Control reports.

spinalcordtoolbox.reports.qc

class spinalcordtoolbox.reports.qc.Params(input_file, command, args, orientation, dest_folder)[source]

Parses and stores the variables that will included into the QC details

We derive the value of the contrast and subject name from the input_file path, by splitting it into [subject]/[contrast]/input_file

class spinalcordtoolbox.reports.qc.QcImage(qc_report, interpolation, action_list, stretch_contrast=True)[source]

Class used to create a .png file from a 2d image produced by the class “Slice”

template(mask)[source]

Show template statistical atlas

class spinalcordtoolbox.reports.qc.QcReport(qc_params, usage)[source]

This class generates the quality control report.

It will also setup the folder structure so the report generator only needs to fetch the appropriate files.

make_content_path()[source]

Creates the whole directory to contain the QC report

Returns:return “root folder of the report” and the “furthest folder path” containing the images
update_description_file(dimension)[source]

Create the description file with a JSON structure

Param:dimension 2-tuple, the dimension of the image frame (w, h)
spinalcordtoolbox.reports.qc.add_entry(src, process, args, path_qc, plane, background=None, foreground=None, qcslice=None, qcslice_operations=[], qcslice_layout=None)[source]

spinalcordtoolbox.reports.slice

class spinalcordtoolbox.reports.slice.Axial(images)[source]

The axial representation of a slice

class spinalcordtoolbox.reports.slice.Coronal(images)[source]

The coronal representation of a slice

class spinalcordtoolbox.reports.slice.Sagittal(images)[source]

The sagittal representation of a slice

class spinalcordtoolbox.reports.slice.Slice(images)[source]

Abstract class representing slicing applied to >=1 volumes for the purpose of generating ROI slices.

The many volumes that are worked on are usually an original MRI volume, then other ones which can be processed or segmentations of the first volumes; the ROIs are computed on the last volume by default.

For convenience, the volumes are all brought in the SAL reference frame.

Functions with the suffix _slice gets a slice cut in the desired axis at the “i” position of the data of the 3D image. While the functions with the suffix _dim gets the size of the desired dimension of the 3D image.

static add_slice(matrix, i, column, size, patch)[source]

Adds a slice to the canvas containing all the slices

TODO : Move this to the Axial class

Parameters:
  • matrix – input/output “big canvas”
  • i – slice position
  • column – number of columns in mosaic
  • size
  • patch – patch to insert
Returns:

matrix

static crop(matrix, x, y, width, height)[source]

Crops the matrix to width and heigth from the center

Select the size of the matrix if the calulated crop width or height are larger then the size of the matrix.

TODO : Move this into the Axial class

Parameters:
  • matrix – Array representation of the image
  • x – The center of the crop area in the x axis
  • y – The center of the crop area in the y axis
  • width – The width from the center
  • height – The height from the center
Returns:

cropped matrix

get_dim(image)[source]

Abstract method to obtain the depth of the 3d matrix.

Parameters:image – input msct_image.Image
Returns:numpy.ndarray
get_name()[source]

Get the class name

get_slice(data, i)[source]

Abstract method to obtain a slice of a 3d matrix

Parameters:
  • data – volume
  • i – position to slice
Returns:

2D slice

mosaic(nb_column=0, size=15)[source]

Obtain matrices of the mosaics

Calculates how many squares will fit in a row based on the column and the size Multiply by 2 because the sides are of size*2. Central point is size +/-.

Parameters:
  • nb_column – number of mosaic columns
  • size – each column size
Returns:

tuple of numpy.ndarray containing the mosaics of each slice pixels

static nan_fill(A)[source]

Interpolate NaN values with neighboring values in array (in-place) If only NaNs, return an array of zeros.

single()[source]

Obtain the matrices of the single slices

Returns:tuple of numpy.ndarray, matrix of the input 3D MRI containing the slices and matrix of the transformed 3D RMI to output containing the slices

…. autofunction:: spinalcordtoolbox.metadata