StrokeSegLab
Loading...
Searching...
No Matches
StrokeSegLab.preprocessing.preprocessor.Preprocessor Class Reference

Public Member Functions

None __init__ (self, gui=None)
 
tuple[np.ndarray, np.ndarray, list[tuple[int, int]], tuple[int, int, int], str, tuple[float, float, float], list[tuple[int, int]], str, str] run (self, str t1, str flair, str temp_dir, bool bet_only=False)
 
 clean (self, temp_dir)
 
tuple[dict[str, str|None], int, int, list[str]] find_nii_files (self)
 

Public Attributes

 logger = logging.getLogger()
 
 option = Option()
 
 config = Config()
 
 resampler = Resampler()
 
 wrapper = AnimaWrapper()
 
 atlasImage = os.path.join(ATLAS_DIR,"Reference_T1.nrrd")
 
 gui = gui
 
 brain_extracter = BrainExtracter(self.wrapper,self.atlasImage,gui)
 
list preprocessing_steps = []
 

Protected Member Functions

tuple[np.ndarray, tuple[float, float, float], np.ndarray, tuple[int, int, int]] _load_img (self, str img_path)
 
np.ndarray _z_score_norm (self, np.ndarray data, np.ndarray seg=None)
 
str _bias_correct (self, str img_path, str prefix)
 
tuple[str, str] _register_to_reference (self, str img_path, str ref, str suffix, str prefix)
 
None _print_action (self, str action_name)
 
str _reorient_RAS (self, str img_path, str prefix)
 
np.ndarray _create_nonzero_mask (self, np.ndarray data)
 
tuple[np.ndarray, np.ndarray, tuple[slice, slice, slice]] _crop_to_nonzero (self, np.ndarray data, np.ndarray seg=None, int nonzero_label=-1, tuple[slice, slice, slice] bbox=None)
 
tuple[np.ndarray, list[tuple[int, int]]] _padding (self, np.ndarray data, int min_size=128)
 
tuple[np.ndarray, np.ndarray, list[tuple[int, int]], tuple[int, int, int], str, tuple[float, float, float], list[tuple[int, int]], str] _preprocess_modality (self, str modality, bool is_MNI, tuple[slice, slice, slice] bbox=None)
 

Detailed Description

This class performs preprocessing on 3D images

Constructor & Destructor Documentation

◆ __init__()

None StrokeSegLab.preprocessing.preprocessor.Preprocessor.__init__ ( self,
gui = None )
Initialize the main preprocessing class with optional GUI integration.

Args:
    gui (GUIMain, optional): Instance of the gui class to enable the display of messages and status updates. Defaults to None.

Member Function Documentation

◆ _bias_correct()

str StrokeSegLab.preprocessing.preprocessor.Preprocessor._bias_correct ( self,
str img_path,
str prefix )
protected
@public
Use an Anima executable through a wrapper to perform bias correction on the image

Args:
    img_path (str): Path of the image
    prefix (str): Prefix for the output file name (the method will append '_N4.nii.gz' to this prefix).

Returns:
    str: Output path

◆ _create_nonzero_mask()

np.ndarray StrokeSegLab.preprocessing.preprocessor.Preprocessor._create_nonzero_mask ( self,
np.ndarray data )
protected
@public
Create a binary mask indicating where the input data is non-zero

Args:
    data (np.ndarray): Input array of shape (c, x, y, z)

Returns:
    np.ndarray: Binary mask (x, y, z) with True where any channel is non-zero.

◆ _crop_to_nonzero()

tuple[np.ndarray, np.ndarray, tuple[slice, slice, slice]] StrokeSegLab.preprocessing.preprocessor.Preprocessor._crop_to_nonzero ( self,
np.ndarray data,
np.ndarray seg = None,
int nonzero_label = -1,
tuple[slice, slice, slice] bbox = None )
protected
@public
Crop the input data and segmentation to the non-zero region. Optionally reuses a provided bounding box (Used to crop the FLAIR image using the same bounding box as the T1 image)

Args:
    data (np.ndarray): Input array of shape (c, x, y, z)
    seg (np.ndarray, optional): Corresponding segmentation array. Defaults to None.
    nonzero_label (int, optional): Label used to mark regions outside the non-zero crop. Defaults to -1.
    bbox (tuple[slice, slice, slice], optional): _description_. Defaults to None.

Returns:
    tuple[np.ndarray, np.ndarray, tuple[slice, slice, slice]]: Cropped image data, Cropped segmentation mask, Bounding box used for cropping

◆ _load_img()

tuple[np.ndarray, tuple[float,float,float], np.ndarray, tuple[int,int,int]] StrokeSegLab.preprocessing.preprocessor.Preprocessor._load_img ( self,
str img_path )
protected
@public
Load a NIFTI image from the given file path.
- Load using nibabel
- extract affine matrix and voxel spacing
- extract, transpose and add a channel to data if necessary : (z, y, x) -> (c, x, y, z)
- Store original shape (used later in postprocessing)

Args:
    img_path (str): The image path

Returns:
    tuple[np.ndarray, tuple[float,float,float], np.ndarray, tuple[int,int,int]]: Data loaded, spacing, affine matrix and the original shape

◆ _padding()

tuple[np.ndarray, list[tuple[int, int]]] StrokeSegLab.preprocessing.preprocessor.Preprocessor._padding ( self,
np.ndarray data,
int min_size = 128 )
protected
@public
Pad the data with zeros so that each dimension is at least min_size
If the input data has 4 dimensions and the first dimension (channels) is 1, this dimension is squeezed out before padding and then restored afterwards
The padding is applied symmetrically (half before, half after) on each dimension.

Args:
    data (np.ndarray): Input array of shape (c, x, y, z)
    min_size (int, optional): Minimum size. Defaults to 128.

Returns:
    tuple[np.ndarray, list[tuple[int, int]]]: Padded data, List of tuples indicating how much padding was added before and after each dimension

◆ _preprocess_modality()

tuple[np.ndarray,np.ndarray,list[tuple[int, int]], tuple[int,int,int], str, tuple[float,float,float], list[tuple[int, int]], str] StrokeSegLab.preprocessing.preprocessor.Preprocessor._preprocess_modality ( self,
str modality,
bool is_MNI,
tuple[slice, slice, slice] bbox = None )
protected
@public
Run the full preprocessing pipeline except brain extraction : 
- If the image is not already register to MNI : bias correction, reorient to RAS, register to MNI
- Resampling, Z score, padding

Args:
    modality (str): Image path
    is_MNI (bool): True if the image is already register to MNI
    bbox (tuple[slice, slice, slice], optional): Bounding box coordinates used for cropping. Defaults to None.

Returns:
    tuple[np.ndarray,np.ndarray,list[tuple[int, int]], tuple[int,int,int], str, tuple[float,float,float], list[tuple[int, int]], str]:
    - data preprocessed 
    - affine matrix 
    - Bounding box coordinates used for cropping
    - Original shape of the image before preprocessing
    - Path to the transformation text file
    - Voxel spacing of the image
    - List of padding tuples applied to each dimension
    - Reference MNI image array or None    

◆ _print_action()

None StrokeSegLab.preprocessing.preprocessor.Preprocessor._print_action ( self,
str action_name )
protected
@public
Log the current action
Args:
    action_name (str): Name of the action

◆ _register_to_reference()

tuple[str,str] StrokeSegLab.preprocessing.preprocessor.Preprocessor._register_to_reference ( self,
str img_path,
str ref,
str suffix,
str prefix )
protected
@public
Register an image to a reference using Anima's pyramidal block matching registration.

Args:
    img_path (str): Image path
    ref (str): Reference image path
    suffix (str): Suffix to add to the output registered image filename
    prefix (str): Prefix for the output image filename

Returns:
    tuple[str,str]: Output path of the registered image, Output path of the transformation file (.txt)

◆ _reorient_RAS()

str StrokeSegLab.preprocessing.preprocessor.Preprocessor._reorient_RAS ( self,
str img_path,
str prefix )
protected
@public
Reorient to RAS an image

Args:
    img_path (str): Image path
    prefix (str): Prefix for the output image filename

Returns:
    str: Output path

◆ _z_score_norm()

np.ndarray StrokeSegLab.preprocessing.preprocessor.Preprocessor._z_score_norm ( self,
np.ndarray data,
np.ndarray seg = None )
protected
@public
Apply z-score normalization to the input data. If a segmentation mask is provided, the normalization is applied only on the region where the mask is non-negative. Otherwise, the entire volume is normalized.

Args:
    data (np.ndarray): Input data (c, x, y, z)
    seg (np.ndarray, optional): _description_. Defaults to None.

Returns:
    np.ndarray: _description_

◆ find_nii_files()

tuple[dict[str, str | None], int, int, list[str]] StrokeSegLab.preprocessing.preprocessor.Preprocessor.find_nii_files ( self)
Search for NIfTI (.nii or .nii.gz) files in the input directory or single input file.

Returns:
    tuple[dict[str, str | None], int, int, list[str]]: 
    - Dictionary : T1 path : FLAIR path or None
    - The number of T1 subjects (FLAIR too if using a 1 channel model)
    - The number of FLAIR images (if using a 2 channels model)
    - List of subject IDs for which T1 or FLAIR was missing

◆ run()

tuple[np.ndarray,np.ndarray,list[tuple[int, int]], tuple[int,int,int], str, tuple[float,float,float], list[tuple[int, int]], str, str] StrokeSegLab.preprocessing.preprocessor.Preprocessor.run ( self,
str t1,
str flair,
str temp_dir,
bool bet_only = False )
Run the brain extraction and handle different case : 1 or 2 channels, BET only or not, MNI input or not, save MNI or not

Args:
    t1 (str): Path to the T1 image
    flair (str): Path to the FLAIR image
    temp_dir (str): Path to the temporary directory
    bet_only (bool, optional): If True doing brain extraction only. Defaults to False.

Returns:
    tuple[np.ndarray,np.ndarray,list[tuple[int, int]], tuple[int,int,int], str, tuple[float,float,float], list[tuple[int, int]], str, str]: 
    - data preprocessed 
    - affine matrix 
    - Bounding box coordinates used for cropping
    - Original shape of the image before preprocessing
    - Path to the transformation text file
    - Voxel spacing of the image
    - List of padding tuples applied to each dimension
    - Path to the brain-extracted T1 image
    - Reference MNI image array or None

The documentation for this class was generated from the following file: