From 2810b5971c39ed85b97f11d17f477d444a73e37e Mon Sep 17 00:00:00 2001 From: Mogtaba-Alim Date: Wed, 21 Feb 2024 14:27:32 -0500 Subject: [PATCH] Added function to manually check for roi name if it is missing when calling negative control. Fixed output file checking --- src/readii/negative_controls.py | 37 ++++++++++++++++++++++++++------- src/readii/pipeline.py | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/readii/negative_controls.py b/src/readii/negative_controls.py index 7847c28..079c00f 100644 --- a/src/readii/negative_controls.py +++ b/src/readii/negative_controls.py @@ -2,7 +2,7 @@ import numpy as np import random -from readii.image_processing import alignImages +from readii.image_processing import alignImages, getROIVoxelLabel def shuffleImage(imageToShuffle: sitk.Image): @@ -80,7 +80,7 @@ def makeRandomImage(baseImage: sitk.Image): return alignedRandomImage -def makeRandomRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def makeRandomRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to generate random pixel values within the Region of Interest based on the range of values in a sitk Image Parameters @@ -97,6 +97,10 @@ def makeRandomRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1) sitk.Image Image with all pixel values within the Region of Interest randomly generated with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) + # Initialize variables to track the highest and lowest pixel values in the ROI maxVoxelVal = float('-inf') minVoxelVal = float('inf') @@ -133,7 +137,7 @@ def makeRandomRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1) return new_base -def shuffleROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def shuffleROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to shuffle all pixel values within the Region of Interest in a sitk Image Parameters @@ -150,6 +154,9 @@ def shuffleROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): sitk.Image Image with all pixel values in the Region of Interest randomly shuffled with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) # A collection of corresponding value in the BaseImage of all the pixels in the ROI count = [] @@ -180,7 +187,7 @@ def shuffleROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): return new_base -def makeRandomNonRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def makeRandomNonRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to generate random pixel values outside the Region of Interest based on the range of values in a sitk Image Parameters @@ -197,6 +204,10 @@ def makeRandomNonRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = sitk.Image Image with all pixel values outside the Region of Interest randomly generated with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) + # Initialize variables to track the highest and lowest pixel values not in the ROI maxVoxelVal = float('-inf') minVoxelVal = float('inf') @@ -236,7 +247,7 @@ def makeRandomNonRoi(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = return new_base -def shuffleNonROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def shuffleNonROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to shuffle all pixel values that are not within the Region of Interest in a sitk Image Parameters @@ -253,6 +264,9 @@ def shuffleNonROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1) sitk.Image Image with all pixel values outside the Region of Interest randomly shuffled with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) # A collection of corresponding value in the BaseImage of all the pixels not in the ROI count = [] @@ -321,7 +335,7 @@ def randomizeImageFromDistribtutionSampling(imageToRandomize: sitk.Image): return alignedRandomlySampledImage -def makeRandomFromRoiDistribution(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def makeRandomFromRoiDistribution(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to randomly sample pixel values within the Region of Interest uniformly from the distribution of pixel values in the ROI region sitk Image Parameters @@ -338,6 +352,10 @@ def makeRandomFromRoiDistribution(baseImage: sitk.Image, baseROI: sitk.Image, ro sitk.Image Image with all pixel values within the Region of Interest randomly sampled with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) + # Initialize array of ROI pixel distribution distributionROI = [] @@ -369,7 +387,7 @@ def makeRandomFromRoiDistribution(baseImage: sitk.Image, baseROI: sitk.Image, ro return new_base -def makeRandomNonRoiFromDistribution(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1): +def makeRandomNonRoiFromDistribution(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: str = None): """Function to random sample pixel values outside the Region of Interest uniformly from the distribution of pixel values outside the ROI in a sitk Image Parameters @@ -386,6 +404,9 @@ def makeRandomNonRoiFromDistribution(baseImage: sitk.Image, baseROI: sitk.Image, sitk.Image Image with all pixel values outside the Region of Interest randomly sample form outside the ROI with same dimensions as input image """ + # Check the ROI Label exists, if not extract it manually + if not roiLabel: + roiLabel = getROIVoxelLabel(baseROI) # Initialize array of non-ROI pixel distribution distributionROI = [] @@ -422,7 +443,7 @@ def makeRandomNonRoiFromDistribution(baseImage: sitk.Image, baseROI: sitk.Image, return new_base -def applyNegativeControl(nc_type: str, baseImage: sitk.Image, baseROI: sitk.Image = None, roiLabel: int = 1): +def applyNegativeControl(nc_type: str, baseImage: sitk.Image, baseROI: sitk.Image = None, roiLabel: str = None): """Function to generate random pixel values within the Region of Interest based on the range of values in a sitk Image Parameters diff --git a/src/readii/pipeline.py b/src/readii/pipeline.py index 50cd49d..4288032 100644 --- a/src/readii/pipeline.py +++ b/src/readii/pipeline.py @@ -73,7 +73,7 @@ def main(): print("Image metadata file has already been created.") # Check if radiomic feature file already exists - radFeatOutPath = os.path.join(outputDir, "features/", "radfeatures_" + datasetName + ".csv") + radFeatOutPath = os.path.join(outputDir, "features/", "radiomicfeatures_" + datasetName + ".csv") if not os.path.exists(radFeatOutPath) or args.update: print("Starting radiomic feature extraction...") radiomicFeatures = radiomicFeatureExtraction(imageMetadataPath = imageMetadataPath,