cBrainMRIPrePro package
Subpackages
Submodules
cBrainMRIPrePro.pipe module
- class cBrainMRIPrePro.pipe.DataPreprocessing(dict_image, reference, output_folder, reorient_image=None, resample_spacing=None, inter_type_resample=4, n4_correction=None, do_coregistration=True, type_of_transform='Affine', template=True, inter_type_apply_transform_registration=4, do_ss=True, normalize_z_score=None, scaling_factor_z_score=1, device='0', overwrite=False, save_step=('reorient', 'resample', 'n4_correction', 'coregistration', 'affine_transform', 'skullstripping', 'normalize'))[source]
Bases:
ABCThe provided pipeline offer the possibility to resample image spacing, bias field correct, co-register (in SRI24 template or in a reference modality), skull-stripped, and z-score normalize.
Class for data cBrainMRIPrePro input modalities as a dict:
Bias field correction of modalities (optional).
resampling (optional).
Co-registration: If template is set to True. Register to reference to template and then register other modalities to reference. If template is set to False. Register to reference.
Skull-stripped the reference modalities and apply the mask on others modalities.
Normalization Z-Score (optional).
Note
Template is the SRI24 LPS atlas (as used in BraTS challenge). It will add the suffix of the keys to data of each step. so if your keys is “T2” and values “/path/data_t1.nii.gz” it will split the values and look at the last suffix “t1”, so will be data_step_t1_T2.nii.gz it will ignore case sensitivity, so if keys is “T1” and values “/path/data_t1.nii.gz”, the step will be data_step_t1.nii.gz
- Parameters
dict_image (
Dict[str,str]) – keys is the corresponding modality and value is path, ie {“T1”: “t1_path”, “T2”: “t2_path”} ..reference (
Dict[str,str]) – reference modality (used for co-registration and ss). if reference is not in dict_image, no cBrainMRIPrePro step is applied, except for coregistration.output_folder (
str) – output directory were to save cBrainMRIPrePro datareorient_image (
Optional[str]) – str for Reorient an image. For possible reorientation see https://antspy.readthedocs.io/en/latest/_modules/ants/registration/reorient_image.html default is None.resample_spacing (
Optional[Tuple[int,int,int]]) – resolution for resampling (ie (1, 1, 1) in mminter_type_resample (
int) – mode for resample 0 (Linear), 1 (NearestNeighbor), 2 (Gaussian), 3 (WindowedSinc), 4 (BSpline), default: 4n4_correction (
Optional[List]) – bias field correction. Specified as list corresponding to keys of dict_imagedo_coregistration (
bool) – set to True to coregister the data.type_of_transform (
str) –type of transform for registration. (default: “Affine”). Can be of type:
”Translation”: Translation transformation.
”Rigid”: Rigid transformation: Only rotation and translation.
”Similarity”: Similarity transformation: scaling, rotation and translation.
”QuickRigid”: Rigid transformation: Only rotation and translation. May be useful for quick visualization fixes.
”DenseRigid”: Rigid transformation: Only rotation and translation. Employs dense sampling during metric estimation.
”BOLDRigid”: Rigid transformation: Parameters typical for BOLD to BOLD intrasubject registration.
”Affine”: Affine transformation: Rigid + scaling.
”AffineFast”: Fast version of Affine.
”BOLDAffine”: Affine transformation: Parameters typical for BOLD to BOLD intrasubject registration.
”TRSAA”: translation, rigid, similarity, affine (twice).
template (
bool) – set to true to register the data in template spaceinter_type_apply_transform_registration (
int) – choice of interpolator for apply affine transform of registration 0 (Linear), 1 (NearestNeighbor), 2 (MultiLabel), 3 (Gaussian), 4 (BSpline), 4 (CosineWindowedSinc), 6 (WelchWindowedSinc), 7 (HammingWindowedSinc), 8 (LanczosWindowedSinc), 9 (GenericLabel) default: 4.do_ss (
bool) – use HD-BET to skull-strip the reference and apply mask on other modalities. do_coregistration need to be set to True to have a referencenormalize_z_score (
Optional[List]) – normalize skull-stripped image by substraction the mean of the whole brain (considers non zero) and dividing by the standard deviationscaling_factor_z_score (
int) – scaling factor to apply to normalization (default: 1)device (
str) – device to run HD-BT (default GPU: “0”), you can use “cpu”.overwrite (
bool) – if a step file already exist. will overwrite it. (default False)save_step (
Sequence[str]) –specify the step that you want to save : (“resample”, “n4_correction”, “coregistration”, “affine_transform”, “skullstripping”, “normalize”)
resample: save the resampling image’
n4_correction: save bias field correction with already applied previous step
coregistration: save coregistered image with already applied previous step
affine_transform: save affine transform of registration
mask: save brain mask resulting of ss of reference
ss: save all skull-stripped image
- _run_normalize_z_score(img_dict)[source]
Run z-score normalization in brain.
\[I_{z-score}(\mathbf x) = \dfrac{I(\mathbf x) - \mu}{\sigma}.\]- Parameters
img_dict (
Dict[str,str]) – image dict with key is an identifier and value the corresponding image path- Return type
None
- _run_resample_image(img_dict)[source]
Run image resampling using
ants.resample_image()- Parameters
img_dict (
Dict[str,str]) – image dict with key is an identifier and value the corresponding image path- Return type
None
- _run_bias_field_correction(img_dict)[source]
Run n4 bias field correction using
ants.n4_bias_field_correction()Warning
N4 includes a log transform, so we are be aware of negative values. The function will check range of image intensities and rescale to positive if negative values.
get range of input image = [a,b]
rescale input image to be in a positive, e.g., [10 , 1000]
perform N4 on 1)
rescale image from 2) to be in [a,b]
See Also: https://github.com/ANTsX/ANTs/issues/822
Note
To reproduce behavior as in ANTs a mask equally weighting the entire image is supplied. In ANTsPy when no mask is supplied, a mask is computed with the get_mask function. Function is based on a mean threshold. Resulting head mask is very often filled with holes. Here we compute a real head mask with no holes with the
utils.image_processing.get_mask()- Parameters
img_dict (
Dict[str,str]) – image dict with key is an identifier and value the corresponding image path- Return type
None
- _run_coregistration(img_dict, reference)[source]
Run coregistration using
ants.registration().- Parameters
img_dict (
Dict[str,str]) – image dict with key is an identifier and value the corresponding image pathreference (
Dict[str,str]) – reference dict with key is an identifier and value the corresponding image path. Need to be a dict of length 1.
- Return type
None
- _run_skull_stripping(img_dict, reference)[source]
Run skull stripping using
run_hd_bet()fromHD_BET.- Parameters
img_dict (
Dict[str,str]) – image dict with key is an identifier and value the corresponding image pathreference (
Dict[str,str]) – reference dict with key is an identifier and value the corresponding image path. Need to be a dict of length 1.
- Return type
None
- static apply_brain_mask(input_file_path, mask)[source]
Apply brain mask on head image
- Parameters
input_file_path (
str) – input file pathmask (
Union[str,ndarray]) – input brain mask file path or array-like
- Return type
Tuple[ndarray,Tuple[Tuple,Tuple,Tuple]]- Returns
array, header
- static check_output_filename(filename, modality, step)[source]
check output filename. if mod is already in filename will not add it
- Parameters
filename (
str) – filenamemodality (
str) – modalitystep (
str) – cBrainMRIPrePro step
- Return type
str- Returns
filename
- run_pipeline()[source]
Main function to run
DataPreprocessing