voxelmorph.py.utils
General-purpuse python utilities for VoxelMorph.
affine_shift_to_matrix
Converts an affine shift to a matrix (over the identity). To convert back from center-shifted transform, provide image shape to unshift_shape.
TODO: make ND compatible - currently just 3D
Source code in voxelmorph/py/utils.py
clean_seg
Cleans a segmentation image.
Source code in voxelmorph/py/utils.py
clean_seg_batch
Cleans batches of segmentation images.
Source code in voxelmorph/py/utils.py
dice
Computes the dice overlap between two arrays for a given set of integer labels.
Parameters: array1: Input array 1. array2: Input array 2. labels: List of labels to compute dice on. If None, all labels will be used. include_zero: Include label 0 in label list. Default is False.
Source code in voxelmorph/py/utils.py
dist_trf
Computes positive distance transform from positive entries in a logical image.
edge_to_surface_pts
Converts edges to surface points.
Source code in voxelmorph/py/utils.py
extract_largest_vol
Extracts the binary (boolean) image with just the largest component. TODO: This might be less than efficiently implemented.
Source code in voxelmorph/py/utils.py
filter_labels
Filters given volumes to only include given labels, all other voxels are set to 0.
Source code in voxelmorph/py/utils.py
get_surface_pts_per_label
Gets the number of surface points per label, given the total number of surface points.
Source code in voxelmorph/py/utils.py
jacobian_determinant
jacobian determinant of a displacement field. NB: to compute the spatial gradients, we use np.gradient.
Parameters: disp: 2D or 3D displacement field of size [*vol_shape, nb_dims], where vol_shape is of len nb_dims
Returns: jacobian determinant (scalar)
Source code in voxelmorph/py/utils.py
load_labels
Load label maps, return a list of unique labels and the label maps. The label maps have to be of an integer type and identical shape.
Parameters: arg: Path to folder containing label maps, string for globbing, or a list of these. ext: List or tuple of file extensions.
Returns: np.array: List of unique labels. list: List of label maps, each as a NumPy array.
Source code in voxelmorph/py/utils.py
load_pheno_csv
Loads an attribute csv file into a dictionary. Each line in the csv should represent attributes for a single training file and should be formatted as:
filename,attr1,attr2,attr2...
Where filename is the file basename and each attr is a floating point number. If a list of training_files is specified, the dictionary file keys will be updated to match the paths specified in the list. Any training files not found in the loaded dictionary are pruned.
Source code in voxelmorph/py/utils.py
load_volfile
load_volfile(filename, np_var='vol', add_batch_axis=False, add_feat_axis=False, pad_shape=None, resize_factor=1, ret_affine=False)
Loads a file in nii, nii.gz, mgz, npz, or npy format. If input file is not a string, returns it directly (allows files preloaded in memory to be passed to a generator)
Parameters: filename: Filename to load, or preloaded volume to be returned. np_var: If the file is a npz (compressed numpy) with multiple variables, the desired variable can be specified with np_var. Default is 'vol'. add_batch_axis: Adds an axis to the beginning of the array. Default is False. add_feat_axis: Adds an axis to the end of the array. Default is False. pad_shape: Zero-pad the array to a target shape. Default is None. resize: Volume resize factor. Default is 1 ret_affine: Additionally returns the affine transform (or None if it doesn't exist).
Source code in voxelmorph/py/utils.py
pad
Zero-pads an array to a given shape. Returns the padded array and crop slices.
Source code in voxelmorph/py/utils.py
read_file_list
Reads a list of files from a line-seperated text file.
Parameters: filename: Filename to load. prefix: File prefix. Default is None. suffix: File suffix. Default is None.
Source code in voxelmorph/py/utils.py
read_pair_list
Reads a list of registration file pairs from a line-seperated text file.
Parameters: filename: Filename to load. delim: File pair delimiter. Default is a whitespace seperator (None). prefix: File prefix. Default is None. suffix: File suffix. Default is None.
Source code in voxelmorph/py/utils.py
resize
Resizes an array by a given factor. This expects the input array to include a feature dimension. Use batch_axis=True to avoid resizing the first (batch) dimension.
Source code in voxelmorph/py/utils.py
save_volfile
Saves an array to nii, nii.gz, or npz format.
Parameters: array: The array to save. filename: Filename to save to. affine: Affine vox-to-ras matrix. Saves LIA matrix if None (default).
Source code in voxelmorph/py/utils.py
sdt_to_surface_pts
sdt_to_surface_pts(X_sdt, nb_surface_pts, surface_pts_upsample_factor=2, thr=0.50001, resize_fn=None)
Converts a signed distance transform to surface points.
Source code in voxelmorph/py/utils.py
signed_dist_trf
Computes the signed distance transform from the surface between the binary elements of an image NOTE: The distance transform on either side of the surface will be +/- 1, so there are no voxels for which the distance should be 0. NOTE: Currently the function uses bwdist twice. If there is a quick way to compute the surface, bwdist could be used only once.
Source code in voxelmorph/py/utils.py
vol_to_sdt
Computes the signed distance transform from a volume.
Source code in voxelmorph/py/utils.py
vol_to_sdt_batch
Computes the signed distance transforms from volume batches.