FastSurferCNN.data_loader.data_utils¶
- FastSurferCNN.data_loader.data_utils.as_mgh_image(data, affine, header, dtype=None)[source]¶
Build an MGHImage from data, affine and header, and set the two fields the conversion drops.
MGHHeader.from_headercarries neither the field of view nor the data type over from a non-MGH header, so both are set here and the file does not depend on the container its header came from. The fov is the largest of the three extents, as FreeSurfer keeps it, taken fromdataso that it is right even where the header is inherited from a volume of a different shape.- Parameters:
- data
np.ndarray An array containing image data.
- affine
AffineMatrix4x4 Image affine information.
- header
_Header Image header information; a non-MGH header is converted. Required, because every image we write is derived from one we read, and the header is the only carrier of the acquisition parameters and of the type to store.
- dtype
npt.DTypeLike,optional The type to store, overriding the one the header carries. Neither may lose data nor be one MGH cannot store, see
choose_dtype.
- data
- Returns:
nib.MGHImageThe image, with
fovand the data type set.
- 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.choose_dtype(array, header, dtype=None)[source]¶
The type to store
arrayas:dtypeif the caller gave one, else the header’s.That type is used or nothing is. It may not lose data: narrowing that would round or clip is the caller’s to do, deliberately and outside, because only the caller knows whether to cast, to rescale or to refuse.
Whether the type can be stored at all is the output format’s answer, not this function’s, so it is asked when the type is applied. The byte order is dropped here for the same reason: an MGH file is always big-endian, whatever type it stores.
- Parameters:
- array
np.ndarray The data to store.
- header
_Header The header, whose type is used when
dtypeis None.- dtype
npt.DTypeLike,optional The type to store, overriding the header’s.
- array
- Returns:
np.dtypeThe type to write, in native byte order.
- Raises:
ValueErrorIf storing
arrayas that type would round or clip a value.
- 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.fits_dtype(array, dtype)[source]¶
Whether every value of
arraysurvives being stored asdtype.An integer loses its identity as surely by being rounded into a float as by being clipped, so a float target holds integers only as far as it counts exactly: 2**24 for float32, 2**53 for float64. Narrowing a value that was already floating-point counts as fitting, since a measurement losing digits is not a label becoming a different label.
- Parameters:
- array
np.ndarray The data to store.
- dtype
npt.DTypeLike The type to store it as.
- array
- Returns:
- 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:
- the_image
nibabelImage The SpatialImage object from nibabel of the conformed image (including updated affine).
- the_data
np.ndarray The data of the conformed image.
- the_image
- 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.
nibabelImageThe 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
AffineMatrix4x4 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 The type to store, overriding the one the header carries. Neither may lose data nor be one the format cannot store, see
choose_dtype.
- header_info
- Returns:
nibabelImageThe image as it was written, so a caller that also needs it in memory does not have to build it a second time, and gets the container the file actually uses.
Notes
The type is decided the same way for every format. The file name only decides which types are available at all: MGH has no float64 and no int64, NIfTI has both.
- 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.storable_dtype(array, candidates=(<class 'numpy.uint8'>, <class 'numpy.uint16'>, <class 'numpy.int16'>, <class 'numpy.int32'>, <class 'numpy.float32'>))[source]¶
The array’s own type if the output format can store it, else the closest one that holds it.
For an output whose type is not ours to choose:
mri/orig/001.mgz, the archival copy of whatever the scanner or the conversion tool produced. Every other output has a type that belongs to the output rather than to the input, so its caller names it andchoose_dtyperefuses anything that does not fit.Closest means the same kind first, so a float is never answered with an integer and signed data stays signed, and within the kind the narrowest that holds every value. An MGH file has no float64, so a float64 input is archived as float32.
- Parameters:
- array
np.ndarray The data to store.
- candidates
tupleofnpt.DTypeLike, default=MGH_DTYPES Every type the output format can store.
- array
- Returns:
np.dtypeThe type to write, in native byte order.
- Raises:
ValueErrorIf nothing the format can store holds
array.
- 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.