FastSurferCNN.data_loader.data_utils¶
- FastSurferCNN.data_loader.data_utils.bbox_3d(img)[source]¶
Extract the three-dimensional bounding box coordinates.
- Parameters:
- img
npt.NDArray
Mri image.
- img
- Returns:
np.ndarray
Rmin.
np.ndarray
Rmax.
np.ndarray
Cmin.
np.ndarray
Cmax.
np.ndarray
Zmin.
np.ndarray
Zmax.
- FastSurferCNN.data_loader.data_utils.clean_cortex_labels(aparc)[source]¶
Clean up aparc segmentations.
Map undetermined and optic chiasma to BKG Map Hypointensity classes to one Vessel to WM 5th Ventricle to CSF Remaining cortical labels to BKG.
- Parameters:
- aparc
npt.NDArray
Aparc segmentations.
- aparc
- Returns:
np.ndarray
Cleaned aparc.
- FastSurferCNN.data_loader.data_utils.cortex_border_mask(label, structure, ctx_thresh=33)[source]¶
Erode the cortex of a given mri image to create the inner gray matter mask (outer most cortex voxels).
- Parameters:
- label
npt.NDArray
Ground truth labels.
- structure
npt.NDArray
Structuring element to erode with.
- ctx_thresh
int
Label value of cortex (above = cortical parcels). Defaults to 33.
- label
- Returns:
np.ndarray
Inner grey matter layer.
- FastSurferCNN.data_loader.data_utils.create_weight_mask(mapped_aseg, max_weight=5, max_edge_weight=5, max_hires_weight=None, ctx_thresh=33, mean_filter=False, cortex_mask=True, gradient=True)[source]¶
Create weighted mask - with median frequency balancing and edge-weighting.
- Parameters:
- mapped_aseg
np.ndarray
Segmentation to create weight mask from.
- max_weight
int
Maximal weight on median weights (cap at this value). (Default value = 5).
- max_edge_weight
int
Maximal weight on gradient weight (cap at this value). (Default value = 5).
- max_hires_weight
int
Maximal weight on hires weight (cap at this value). (Default value = None).
- ctx_thresh
int
Label value of cortex (above = cortical parcels). (Default value = 33).
- mean_filter
bool
Flag, set to add mean_filter mask (default = False).
- cortex_mask
bool
Flag, set to create cortex weight mask (default=True).
- gradient
bool
Flag, set to create gradient mask (default = True).
- mapped_aseg
- Returns:
np.ndarray
Weights.
- FastSurferCNN.data_loader.data_utils.deep_sulci_and_wm_strand_mask(volume, structure, iteration=1, ctx_thresh=33)[source]¶
Get a binary mask of deep sulci and small white matter strands by using binary closing (erosion and dilation).
- Parameters:
- Returns:
np.ndarray
Sulcus + wm mask.
- FastSurferCNN.data_loader.data_utils.fill_unknown_labels_per_hemi(gt, unknown_label, cortex_stop)[source]¶
Replace label 1000 (lh unknown) and 2000 (rh unknown) with closest class for each voxel.
- Parameters:
- Returns:
np.ndarray
Ground truth segmentation with all classes.
- FastSurferCNN.data_loader.data_utils.filter_blank_slices_thick(img_vol, label_vol, weight_vol, threshold=50)[source]¶
Filter blank slices from the volume using the label volume.
- Parameters:
- img_vol
npt.NDArray
Orig image volume.
- label_vol
npt.NDArray
Label images (ground truth).
- weight_vol
npt.NDArray
Weight corresponding to labels.
- threshold
int
Threshold for number of pixels needed to keep slice (below = dropped). (Default value = 50).
- img_vol
- Returns:
- filtered img_vol
np.ndarray
Image volume with blank slices removed.
- label_vol
np.ndarray
Label volume with blank slices removed.
- weight_vol
np.ndarray
Weight volume with blank slices removed.
- filtered img_vol
- FastSurferCNN.data_loader.data_utils.fuse_cortex_labels(aparc)[source]¶
Fuse cortical parcels on left/right hemisphere (reduce aparc classes).
- Parameters:
- aparc
npt.NDArray
Anatomical segmentation with cortical parcels.
- aparc
- Returns:
np.ndarray
Anatomical segmentation with reduced number of cortical parcels.
- FastSurferCNN.data_loader.data_utils.get_labels_from_lut(lut, label_extract=('Left-', 'ctx-rh'))[source]¶
Extract labels from the lookup tables.
- Parameters:
- lut
Union
[str
,pd.DataFrame
] FreeSurfer like LookUp Table (either path to it or already loaded as pandas DataFrame. Example entry: ID LabelName R G B A 0 Unknown 0 0 0 0 1 Left-Cerebral-Exterior 70 130 180 0.
- label_extract
Tuple
[str
,str
] Suffix of label names to mask for sagittal labels Default: “Left-” and “ctx-rh”.
- lut
- Returns:
np.ndarray
Full label list.
np.ndarray
Sagittal label list.
- FastSurferCNN.data_loader.data_utils.get_largest_cc(segmentation)[source]¶
Find the largest connected component of segmentation.
- Parameters:
- segmentation
npt.NDArray
Segmentation.
- segmentation
- Returns:
np.ndarray
Largest connected component of segmentation (binary mask).
- FastSurferCNN.data_loader.data_utils.get_thick_slices(img_data, slice_thickness=3)[source]¶
Extract thick slices from the image.
Feed slice_thickness preceding and succeeding slices to network, label only middle one.
- Parameters:
- img_data
npt.NDArray
3D MRI image read in with nibabel.
- slice_thickness
int
Number of slices to stack on top and below slice of interest (default=3).
- img_data
- Returns:
np.ndarray
Image data with the thick slices of the n-th axis appended into the n+1-th axis.
- FastSurferCNN.data_loader.data_utils.infer_mapping_from_lut(num_classes_full, lut)[source]¶
Guess the mapping from a lookup table.
- Parameters:
- Returns:
np.ndarray
List of indexes for.
- FastSurferCNN.data_loader.data_utils.load_and_conform_image(img_filename, interpol=1, logger=<Logger FastSurferCNN.data_loader.data_utils (WARNING)>, conform_min=False)[source]¶
Load MRI image and conform it to UCHAR, RAS orientation and 1mm or minimum isotropic voxels size.
Only, if it does not already have this format.
- Parameters:
- img_filename
Path
,str
Path and name of volume to read.
- interpol
int
, default=1 Interpolation order for image conformation (0=nearest, 1=linear(default), 2=quadratic, 3=cubic).
- logger
logging.Logger
, default=<local logger> Logger to write output to (default = STDOUT).
- conform_min
bool
, default=False Conform image to minimal voxel size (for high-res).
- img_filename
- Returns:
nibabel.Header
header_info
Header information of the conformed image.
numpy.ndarray
affine_info
Affine information of the conformed image.
numpy.ndarray
orig_data
Conformed image data.
- Raises:
RuntimeError
Multiple input frames not supported.
RuntimeError
Inconsistency in nifti-header.
- FastSurferCNN.data_loader.data_utils.load_image(file, name='image', **kwargs)[source]¶
Load file ‘file’ with nibabel, including all data.
- Parameters:
- Returns:
Tuple
[nib.analyze.SpatialImage
,np.ndarray
]The nibabel image object and a numpy array of the data.
- Raises:
IOError
Failed loading the file nibabel releases the GIL, so the following is a parallel example. { >>> from concurrent.futures import ThreadPoolExecutor >>> with ThreadPoolExecutor() as pool: >>> future1 = pool.submit(load_image, filename1) >>> future2 = pool.submit(load_image, filename2) >>> image, data = future1.result() >>> image2, data2 = future2.result() }
- FastSurferCNN.data_loader.data_utils.load_maybe_conform(file, alt_file, vox_size='min')[source]¶
Load an image by file, check whether it is conformed to vox_size and conform to vox_size if it is not.
- Parameters:
- Returns:
Path
The path to the file.
nib.analyze.SpatialImage
The file container object including the corrected header.
np.ndarray
The data loaded from the file.
- FastSurferCNN.data_loader.data_utils.map_aparc_aseg2label(aseg, labels, labels_sag, sagittal_lut_dict, aseg_nocc=None, processing='aparc')[source]¶
Perform look-up table mapping of aparc.DKTatlas+aseg.mgz data to label space.
- Parameters:
- aseg
npt.NDArray
Ground truth aparc+aseg.
- labels
npt.NDArray
Labels to use (extracted from LUT with get_labels_from_lut).
- labels_sag
npt.NDArray
Sagittal labels to use (extracted from LUT with get_labels_from_lut).
- sagittal_lut_dict
Mapping
Left-right label mapping (can be extracted with unify_lateralized_labels from LUT).
- aseg_nocc
Optional
[npt.NDArray
] Ground truth aseg without corpus callosum segmentation (Default value = None).
- processing
str
Should be set to “aparc” or “aseg” for additional mappings (hard-coded) (Default value = “aparc”).
- aseg
- Returns:
np.ndarray
Mapped aseg for coronal and axial.
np.ndarray
Mapped aseg for sagittal.
- FastSurferCNN.data_loader.data_utils.map_label2aparc_aseg(mapped_aseg, labels)[source]¶
Perform look-up table mapping from sequential label space to LUT space.
- Parameters:
- mapped_aseg
torch.Tensor
Label space segmentation (aparc.DKTatlas + aseg).
- labels
Union
[torch.Tensor
,npt.NDArray
] List of labels defining LUT space.
- mapped_aseg
- Returns:
torch.Tensor
Labels in LUT space.
- FastSurferCNN.data_loader.data_utils.map_prediction_sagittal2full(prediction_sag, num_classes=51, lut=None)[source]¶
Remap the prediction on the sagittal network to full label space used by coronal and axial networks.
Create full aparc.DKTatlas+aseg.mgz.
- Parameters:
- Returns:
np.ndarray
Remapped prediction.
- FastSurferCNN.data_loader.data_utils.read_classes_from_lut(lut_file)[source]¶
Modify from datautils to allow support for FreeSurfer-distributed ColorLUTs.
Read in FreeSurfer-like LUT table.
- Parameters:
- Returns:
pandas.DataFrame
DataFrame with ids present, name of ids, color for plotting.
- FastSurferCNN.data_loader.data_utils.sagittal_coronal_remap_lookup(x)[source]¶
Convert left labels to corresponding right labels for aseg with dictionary mapping.
- Parameters:
- x
int
Label to look up.
- x
- Returns:
np.ndarray
Mapped label.
- FastSurferCNN.data_loader.data_utils.save_image(header_info, affine_info, img_array, save_as, dtype=None)[source]¶
Save an image (nibabel MGHImage), according to the desired output file format.
Supported formats are defined in supported_output_file_formats. Saves predictions to save_as.
- Parameters:
- header_info
_Header
Image header information.
- affine_info
npt.NDArray
[float
] Image affine information.
- img_array
np.ndarray
An array containing image data.
- save_as
Path
,str
Name under which to save prediction; this determines output file format.
- dtype
npt.DTypeLike
,optional
Image array type; if provided, the image object is explicitly set to match this type (Default value = None).
- header_info
- FastSurferCNN.data_loader.data_utils.split_cortex_labels(aparc)[source]¶
Splot cortex labels to completely de-lateralize structures.
- Parameters:
- aparc
npt.NDArray
Anatomical segmentation and parcellation from network.
- aparc
- Returns:
np.ndarray
Re-lateralized aparc.
- FastSurferCNN.data_loader.data_utils.transform_axial(vol, coronal2axial=True)[source]¶
Transform volume into Axial axis and back.
- Parameters:
- vol
npt.NDArray
Image volume to transform.
- coronal2axial
bool
Transform from coronal to axial = True (default).
- vol
- Returns:
np.ndarray
Transformed image.
- FastSurferCNN.data_loader.data_utils.transform_sagittal(vol, coronal2sagittal=True)[source]¶
Transform volume into Sagittal axis and back.
- Parameters:
- vol
npt.NDArray
Image volume to transform.
- coronal2sagittal
bool
Transform from coronal to sagittal = True (default).
- vol
- Returns:
- np.ndarray:
Transformed image.