10  Pyradiomics - Extract to DF

Code
from pyhere import here
import pandas as pd
Code
from radiomics import featureextractor

# Initialize the RadiomicsFeatureExtractor
extractor = featureextractor.RadiomicsFeatureExtractor()

10.1 Extract One File

Code
import pandas as pd
import os

# Define a function to apply to each row
def extract_radiomics_features(imageFilepath, maskFilepath, label):
    # Check if image, mask file, and label exist
    if not os.path.exists(imageFilepath) or not os.path.exists(maskFilepath) or pd.isna(label):
        # If any of these are missing, return NA values
        return pd.Series([pd.NA] * len(extractor.enabledFeatures.keys()))
    
    # Try to extract features, return NA if an error occurs
    result = extractor.execute(imageFilepath=imageFilepath,
                               maskFilepath=maskFilepath,
                               label=int(label))
        
    return pd.Series(result)
Code
extract_radiomics_features("../../data/MRI-Brain-Eye/Case1/3 t1 axial Processed_CaPTk.nrrd",
                           "../../data/MRI-Brain-Eye/Case1/Segmentation.seg.nrrd",
                           1)
GLCM is symmetrical, therefore Sum Average = 2 * Joint Average, only 1 needs to be calculated
diagnostics_Versions_PyRadiomics                   v3.0.1
diagnostics_Versions_Numpy                         1.26.4
diagnostics_Versions_SimpleITK               2.3.1-g42ce2
diagnostics_Versions_PyWavelet                      1.5.0
diagnostics_Versions_Python                       3.10.10
                                            ...          
original_ngtdm_Busyness                3.5402573095824215
original_ngtdm_Coarseness           0.0009334119757378282
original_ngtdm_Complexity              1483.2263634997792
original_ngtdm_Contrast               0.10701153309807594
original_ngtdm_Strength                1.0848440176409921
Length: 129, dtype: object

10.2 Extract Features to DF

Code
import pandas as pd
import os
from radiomics import featureextractor


# Initialize the RadiomicsFeatureExtractor
extractor = featureextractor.RadiomicsFeatureExtractor()

def radiomics_extract_to_df(imageFilepaths, maskFilepaths, labels):
    """
    Extracts radiomics features for a list of image and mask file paths with specified labels.
    
    Parameters:
    - imageFilepaths: list or pandas series of paths to image files
    - maskFilepaths: list or pandas series of paths to mask files
    - labels: list or pandas series of labels to be used for feature extraction
    
    Returns:
    - DataFrame with radiomics features, one row per image, mask, and label combination.
    """
    # Create a DataFrame to store the file paths and labels
    df = pd.DataFrame({
        "imageFilepath": imageFilepaths,
        "maskFilepath": maskFilepaths,
        "label": labels
    })

    # Apply the feature extraction function to each row
    features_df = df.apply(lambda row: extract_radiomics_features(row["imageFilepath"], row["maskFilepath"], row["label"]), axis=1)

    # Combine original file information with extracted features
    result_df = pd.concat([df, features_df], axis=1)

    return result_df
Code
# Example lists of file paths and labels
imageFilepaths = ["../../data/MRI-Brain-Eye/Case1/3 t1 axial Processed_CaPTk.nrrd", "../../data/MRI-Brain-Eye/Case2/7 t2_Flair_axial Processed_CaPTk_2.nrrd"]
maskFilepaths = ["../../data/MRI-Brain-Eye/Case1/Segmentation.seg.nrrd", "../../data/MRI-Brain-Eye/Case2/Segmentation_Eye.seg.nrrd"]
labels = [1, 2]  # Example labels for each segment

# Call function
radiomics_df = radiomics_extract_to_df(imageFilepaths, maskFilepaths, labels)
radiomics_df
GLCM is symmetrical, therefore Sum Average = 2 * Joint Average, only 1 needs to be calculated
GLCM is symmetrical, therefore Sum Average = 2 * Joint Average, only 1 needs to be calculated
imageFilepath maskFilepath label diagnostics_Versions_PyRadiomics diagnostics_Versions_Numpy diagnostics_Versions_SimpleITK diagnostics_Versions_PyWavelet diagnostics_Versions_Python diagnostics_Configuration_Settings diagnostics_Configuration_EnabledImageTypes ... original_glszm_SmallAreaHighGrayLevelEmphasis original_glszm_SmallAreaLowGrayLevelEmphasis original_glszm_ZoneEntropy original_glszm_ZonePercentage original_glszm_ZoneVariance original_ngtdm_Busyness original_ngtdm_Coarseness original_ngtdm_Complexity original_ngtdm_Contrast original_ngtdm_Strength
0 ../../data/MRI-Brain-Eye/Case1/3 t1 axial Proc... ../../data/MRI-Brain-Eye/Case1/Segmentation.se... 1 v3.0.1 1.26.4 2.3.1-g42ce2 1.5.0 3.10.10 {'minimumROIDimensions': 2, 'minimumROISize': ... {'Original': {}} ... 143.74413264102546 0.0165588948802172 6.12011149362317 0.1779624893435635 1972.0849022912257 3.5402573095824215 0.0009334119757378282 1483.2263634997792 0.10701153309807594 1.0848440176409921
1 ../../data/MRI-Brain-Eye/Case2/7 t2_Flair_axia... ../../data/MRI-Brain-Eye/Case2/Segmentation_Ey... 2 v3.0.1 1.26.4 2.3.1-g42ce2 1.5.0 3.10.10 {'minimumROIDimensions': 2, 'minimumROISize': ... {'Original': {}} ... 89.63527189334039 0.011517574782977553 6.031264647874513 0.24972647702407003 171.04926886676446 1.4260178519990565 0.002340576074194839 289.673667008558 0.08092096635016202 0.3530084647821763

2 rows × 132 columns