Skip to content

Commit

Permalink
Merged in bugfix/RAM-3906_0_crop (pull request jrkerns#438)
Browse files Browse the repository at this point in the history
RAM-3906 Allow cropping of 0 pixels.

Approved-by: Randy Taylor
Approved-by: Hasan Ammar
  • Loading branch information
jrkerns committed Sep 5, 2024
2 parents 37319ba + 3ab04a6 commit 3f82d62
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ ACR MRI
the full circle of the phantom. The algorithm no longer relies on this assumption and
is robust to these air gaps for the geometric distortion analysis.

Core
^^^^

* Performing :meth:`~pylinac.core.image.BaseImage.crop` on an image now allows for a ``pixels`` input of 0. This allows for a no-op crop.

v 3.26.0
--------

Expand Down
6 changes: 5 additions & 1 deletion pylinac/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,12 @@ def crop(
edges : tuple
Which edges to remove from. Can be any combination of the four edges.
"""
if pixels <= 0:
if pixels < 0:
raise ValueError("Pixels to remove must be a positive number")
if pixels == 0:
# using 0 will cause an error in numpy slicing
# we also want to be able to handle a no-op
return
if "top" in edges:
self.array = self.array[pixels:, :]
if "bottom" in edges:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dev = [
"sphinx-copybutton",
"pre-commit",
"hatch",
"parameterized",
]
docs = [
"sphinx",
Expand Down
7 changes: 4 additions & 3 deletions tests_basic/core/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import PIL.Image
import pydicom
from numpy.testing import assert_array_almost_equal
from parameterized import parameterized
from pydicom.uid import UID

from pylinac import settings
Expand Down Expand Up @@ -342,9 +343,9 @@ def setUp(self):
array = np.arange(42).reshape(6, 7)
self.arr = image.load(array)

def test_crop(self):
@parameterized.expand([15, 0])
def test_crop(self, crop):
"""Remove the edges from a pixel array."""
crop = 15
orig_shape = self.img.shape
orig_dpi = self.img.dpi
self.img.crop(crop)
Expand All @@ -356,7 +357,7 @@ def test_crop(self):

def test_crop_must_be_positive(self):
"""Crop must be manifestly positive"""
crop = 0
crop = -1
with self.assertRaises(ValueError):
self.img.crop(crop)

Expand Down
6 changes: 5 additions & 1 deletion tests_basic/test_picketfence.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ class PFTestMixin(CloudFileMixin):
abs_median_error = 0
sag_adjustment = 0
invert = False
crop_mm: int = 3
passes = True
log = None
mean_picket_spacing = 15
Expand All @@ -496,7 +497,9 @@ def get_logfile(cls):

@classmethod
def setUpClass(cls):
cls.pf = PicketFence(cls.get_filename(), log=cls.get_logfile())
cls.pf = PicketFence(
cls.get_filename(), log=cls.get_logfile(), crop_mm=cls.crop_mm
)
if cls.pass_num_pickets:
cls.pf.analyze(
sag_adjustment=cls.sag_adjustment,
Expand Down Expand Up @@ -581,6 +584,7 @@ class PerfectSimulation(PFTestMixin, TestCase):
abs_median_error = 0
num_pickets = 5
mean_picket_spacing = 20
crop_mm = 0


class Rotated2Simulation(PFTestMixin, TestCase):
Expand Down

0 comments on commit 3f82d62

Please sign in to comment.