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.ndarrayRmin.
np.ndarrayRmax.
np.ndarrayCmin.
np.ndarrayCmax.
np.ndarrayZmin.
np.ndarrayZmax.
- 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.ndarrayCleaned 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.ndarrayInner 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.ndarrayWeights.
- 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.ndarraySulcus + 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.ndarrayGround 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.ndarrayAnatomical 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.ndarrayFull label list.
np.ndarraySagittal 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.ndarrayLargest 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.ndarrayImage 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.ndarrayList of indexes for.
- FastSurferCNN.data_loader.data_utils.load_and_conform_image(img_filename, order=1, logger=<Logger FastSurferCNN.data_loader.data_utils (INFO)>, **conform_kwargs)[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.
- order
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_kwargs
Additional parameters to conform and is_conform.
- img_filename
- Returns:
nibabel.Headerheader_infoHeader information of the conformed image.
numpy.ndarrayaffine_infoAffine information of the conformed image.
numpy.ndarrayorig_dataConformed image data.
- Raises:
RuntimeErrorIf input has multiple input frames or inconsistent nifti headers.
- 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:
IOErrorFailed 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, **conform_kwargs)[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:
PathThe path to the file.
nib.analyze.SpatialImageThe file container object including the corrected header.
np.ndarrayThe data loaded from the file.
See also
FastSurferCNN.data_loader.conform.conformFor additional parameters supported via
conform_kwargs.
- 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.ndarrayMapped aseg for coronal and axial.
np.ndarrayMapped 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.TensorLabels 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.ndarrayRemapped 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.DataFrameDataFrame 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.ndarrayMapped 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.
- 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.ndarrayRe-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.ndarrayTransformed 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.