Skip to content

Commit

Permalink
Fixed issue with shuffleROI and shuffleNonROI, where some pixels with…
Browse files Browse the repository at this point in the history
… duplicate values were ignored
  • Loading branch information
Mogtaba-Alim committed Dec 21, 2023
1 parent 302a284 commit e812bcc
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/yarea/negative_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,35 +151,31 @@ def shuffleROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1):
Image with all pixel values in the Region of Interest randomly shuffled with same dimensions as input image
"""

# A collection of all the pixels in the ROI and their corresponding value in the BaseImage
count = {}
# A collection of corresponding value in the BaseImage of all the pixels in the ROI
count = []

Check warning on line 155 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L155

Added line #L155 was not covered by tests
# Iterate through baseROI to store the corresponding value in the baseImage of all the pixels in the ROI
baseROISize = baseROI.GetSize()
for x in range(baseROISize[0]):
for y in range(baseROISize[1]):
for z in range(baseROISize[2]):
if baseROI.GetPixel(x, y, z) == roiLabel:
# Here tthe key is the pixel coordinate and the value is the value of the pixel in the base image
count[str(x) + str(y) + str(z)] = baseImage.GetPixel(x, y, z)
# append the ROI corresponding pixel values to the list
count.append(baseImage.GetPixel(x, y, z))

Check warning on line 163 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L163

Added line #L163 was not covered by tests

# Create a new base image so we are not directly editing the input image
new_base = baseImage.__copy__()
# Delete the input image to save space
del (baseImage)

# A list of all the values in the base image of the pixels in the ROI
voxelVals = list(count.values())
# Randomly shuffling the pixel values
random.shuffle(voxelVals)

# Combine the pixels with randomly shuffled values, so each pixel has a new randomly shuffled value
new_count = zip(count.keys(), voxelVals)
# Deleting the cound map to save on memory
del (count)
# # Randomly shuffling the pixel values
random.shuffle(count)

Check warning on line 171 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L171

Added line #L171 was not covered by tests

# Iterating over the pixels and their new shuffled value and assigning it in the image
for coord, shuffled_value in new_count:
new_base.SetPixel(int(coord[0]), int(coord[1]), int(coord[2]), shuffled_value)
for x in range(baseROISize[0]):
for y in range(baseROISize[1]):
for z in range(baseROISize[2]):
if baseROI.GetPixel(x, y, z) == roiLabel:

Check warning on line 176 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L173-L176

Added lines #L173 - L176 were not covered by tests
# Set the value of a pixel in the ROI to be a shuffled value
new_base.SetPixel(x, y, z, count.pop())

Check warning on line 178 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L178

Added line #L178 was not covered by tests

return new_base

Expand Down Expand Up @@ -258,8 +254,8 @@ def shuffleNonROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1)
Image with all pixel values outside the Region of Interest randomly shuffled with same dimensions as input image
"""

# A collection of all the pixels outside the ROI and their corresponding value in the BaseImage
count = {}
# A collection of corresponding value in the BaseImage of all the pixels not in the ROI
count = []

Check warning on line 258 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L258

Added line #L258 was not covered by tests
# Iterate through baseImage to store the corresponding value in the baseImage of all the pixels outside the ROI
baseImageSize = baseImage.GetSize()
baseROISize = baseROI.GetSize()
Expand All @@ -269,26 +265,23 @@ def shuffleNonROI(baseImage: sitk.Image, baseROI: sitk.Image, roiLabel: int = 1)
if x > baseROISize[0] or y > baseROISize[1] or z > baseROISize[2] or baseROI.GetPixel(x, y,
z) != roiLabel:
# Here the key is the pixel coordinate and the value is the value of the pixel in the base image
count[str(x) + str(y) + str(z)] = baseImage.GetPixel(x, y, z)
count.append(baseImage.GetPixel(x, y, z))

Check warning on line 268 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L268

Added line #L268 was not covered by tests

# Create a new base image so we are not directly editing the input image
new_base = baseImage.__copy__()
# Delete the input image to save space
del (baseImage)

# A list of all the values in the base image of the pixels outside the ROI
voxelVals = list(count.values())
# Randomly shuffling the pixel values
random.shuffle(voxelVals)
random.shuffle(count)

Check warning on line 276 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L276

Added line #L276 was not covered by tests

# Combine the pixels with randomly shuffled values, so each pixel has a new randomly shuffled value
new_count = zip(count.keys(), voxelVals)
# Deleting the count map to save on memory
del (count)

# Iterating over the pixels and their new shuffled value and assigning it in the image
for coord, shuffled_value in new_count:
new_base.SetPixel(int(coord[0]), int(coord[1]), int(coord[2]), shuffled_value)
for x in range(baseImageSize[0]):
for y in range(baseImageSize[1]):
for z in range(baseImageSize[2]):
if x > baseROISize[0] or y > baseROISize[1] or z > baseROISize[2] or baseROI.GetPixel(x, y,

Check warning on line 281 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L278-L281

Added lines #L278 - L281 were not covered by tests
z) != roiLabel:
# Set the value of a pixel outside the ROI to be a shuffled value
new_base.SetPixel(x, y, z, count.pop())

Check warning on line 284 in src/yarea/negative_controls.py

View check run for this annotation

Codecov / codecov/patch

src/yarea/negative_controls.py#L284

Added line #L284 was not covered by tests

return new_base

Expand Down

0 comments on commit e812bcc

Please sign in to comment.