Scripts

SilentInfarctionSegmentationFLAIR.flair_t1_sum

SilentInfarctionSegmentationFLAIR.flair_t1_sum.main(flair, t1, alpha, beta, gm_mask, wm_mask, gt=None, show=False, save_dir=None, verbose=True)[source]

Combine FLAIR and T1 images using a weighted sum of FLAIR and gaussian-transformed T1. Optionally, compute and plot tissue histograms.

  • Apply gaussian transform to T1 and combine with FLAIR using weights alpha and beta.

  • Normalize resulting image to 8-bit.

  • Optionally save image and plot histograms for WM, GM, and lesions.

  • Optionally display images and histograms interactively.

Parameters:
  • flair (SimpleITK.Image) – Preloaded FLAIR image.

  • t1 (SimpleITK.Image) – Preloaded T1 image.

  • alpha (float) – Scaling factor for the standard deviation of the gaussian transform applied to T1.

  • beta (float) – Weight applied to the gaussian-transformed T1 image.

  • gm_mask (SimpleITK.Image) – Mask for gray matter (GM).

  • wm_mask (SimpleITK.Image) – Mask for white matter (WM).

  • gt (SimpleITK.Image, optional) – Ground-truth lesion mask, if available (default: None).

  • show (bool, optional) – Whether to show histograms interactively (default: False).

  • save_dir (str, optional) – Directory to save output images and histograms (default: None).

  • verbose (bool, optional) – Print progress messages (default: True).

Returns:

Weighted sum of FLAIR and gaussian-transformed T1, normalized to 8-bit.

Return type:

SimpleITK.Image

SilentInfarctionSegmentationFLAIR.threshold

SilentInfarctionSegmentationFLAIR.threshold.main(image, gm_mask, gamma, show=False, verbose=True, save_dir=None)[source]

Compute a gray-matter-based threshold using histogram mode and RHWHM, apply it to the input image, and optionally plot/save the results.

Parameters:
  • image (SimpleITK.Image) – Input image to be thresholded.

  • gm_mask (SimpleITK.Image) – Gray matter mask used to compute the histogram.

  • gamma (float) – Multiplier for the right-side half-width at half-maximum (RHWHM) to set the threshold.

  • show (bool, optional) – Whether to display plots interactively (default: False).

  • verbose (bool, optional) – Whether to print progress messages (default: True).

  • save_dir (str, optional) – Directory to save histogram figure and thresholded mask (default: None).

Returns:

thr_mask – Thresholded binary mask of the input image.

Return type:

SimpleITK.Image

SilentInfarctionSegmentationFLAIR.refinement_step

SilentInfarctionSegmentationFLAIR.refinement_step.main(thr_mask, image=None, pves=[], segm=None, min_diameter=None, surround_dilation_radius=None, n_std=None, extend_dilation_radius=1, min_points=3, labels_to_remove=[], keywords_to_remove=[], label_name_file=None, verbose=True, save_dir=None, show=False)[source]

Processes a set of patients to evaluate the performance of FLAIR silent lesion segmentation.

For each patient in the data folder:

both after thresholding and after the refinement step.

  • Compute the number of lesions and total lesion volume.

  • Compute volume similarity between ground truth and segmentation.

  • Generate summary boxplots for metrics saving them to the results folder.

Parameters:
  • thr_mask (SimpleITK.Image) – Thresholded lesion mask.

  • image (SimpleITK.Image, optional) – Reference image used for lesion extension (default: None).

  • pves (list of SimpleITK.Image, optional) – List of partial volume effect maps [WM, GM, CSF] (default: []).

  • segm (SimpleITK.Image, optional) – Anatomical segmentation used to remove impossible voxels (default: None).

  • min_diameter (float, optional) – Minimum lesion diameter in mm (default: None).

  • surround_dilation_radius (int, optional) – Dilation radius for surrounding PVE filter (default: None).

  • n_std (float, optional) – Number of standard deviations for lesion extension (default: None).

  • extend_dilation_radius (int, optional) – Dilation radius for lesion extension (default: 1).

  • min_points (int, optional) – Minimum score a lesion must satisfy (default: 3).

  • labels_to_remove (list of int, optional) – List of segmentation labels to remove (default: []).

  • keywords_to_remove (list of str, optional) – Keywords of labels to remove (default: []).

  • label_name_file (str, optional) – Label-name LUT file (default: None).

  • verbose (bool, optional) – Print progress messages (default: True).

  • save_dir (str, optional) – Directory to save output segmentation and plots (default: None).

  • show (bool, optional) – Whether to display plots interactively (default: False).

Returns:

ref_mask – Refined lesion mask after applying all filters.

Return type:

SimpleITK.Image

SilentInfarctionSegmentationFLAIR.evaluate_test_set

SilentInfarctionSegmentationFLAIR.evaluate_test_set.main(data_folder, params_path, results_folder, verbose, show)[source]

Processes a set of patients to evaluate the performance of FLAIR silent lesion segmentation.

For each patient in the data folder:

  • Computes the number of lesions and total lesion volume.

  • Computes volume similarity between ground truth and segmentation.

  • Generates summary boxplots for metrics, lesion count, total volume, and volume similarity, saving them to the results folder.

Parameters:
  • data_folder (str) – Path to the folder containing input patient images (.nii).

  • params_path (str) – Path to the .yaml file containing segmentation parameters.

  • results_folder (str) – Path to the folder where results and boxplots will be saved.

  • verbose (bool) – If True, prints detailed log messages during processing.

  • show (bool) – If True, displays the generated boxplots.

Returns:

All results are saved to files; nothing is returned.

Return type:

None

Notes

  • If present, a ‘test_patients.pkl’ file is used to process only the test set patients. Otherwise, all patients in the folder are processed.

  • Uses SimpleITK for volume calculations and Seaborn/Matplotlib for visualization.

  • Requires the functions process_patient, evaluate_voxel_wise, get_paths_df, and connected_components to be importable from the SilentInfarctionSegmentationFLAIR package.

SilentInfarctionSegmentationFLAIR.tuning_alpha_beta

SilentInfarctionSegmentationFLAIR.tuning_alpha_beta.main(data_folder, results_folder, init_points, n_iter, n_cores)[source]

Main pipeline for Bayesian optimization of segmentation parameters.

Steps: - Load patient data - Split into training and validation sets - Optimize alpha and beta using Bayesian Optimization - Save best parameters and separation metrics - Validate the best parameters on the validation set

Parameters:
  • data_folder (str) – Path to the dataset directory.

  • results_folder (str) – Path to save outputs, logs, and metrics.

  • init_points (int) – Number of initial random exploration steps for Bayesian Optimization.

  • n_iter (int) – Number of BO iterations.

  • n_cores (int) – Number of CPU cores to use for multiprocessing.

Return type:

None

SilentInfarctionSegmentationFLAIR.tuning_gamma_rs

SilentInfarctionSegmentationFLAIR.tuning_gamma_rs.main(data_folder, alpha, beta, gammas, results_folder, init_points, n_iter, n_cores)[source]

Main pipeline for Bayesian optimization of segmentation settings.

Performs:

  • Threshold mask computation

  • Refinement step optimization with Bayesian Optimization

  • Validation of best parameters

Parameters:
  • data_folder (str) – Path to the dataset directory.

  • alpha (float) – Segmentation parameter alpha.

  • beta (float) – Segmentation parameter beta.

  • gammas (sequence of float) – Gamma values to evaluate.

  • results_folder (str) – Directory to save outputs, logs, and metrics.

  • init_points (int) – Number of initial random exploration steps for BO.

  • n_iter (int) – Number of BO iterations.

  • n_cores (int) – Number of CPU cores to use for multiprocessing.

Return type:

None