From 41aba5449b62cbc8ab8e2162ca52df757057112f Mon Sep 17 00:00:00 2001 From: saranti Date: Fri, 10 Mar 2023 22:37:43 +1100 Subject: [PATCH 1/6] Added get_shapes method as alias to get_size --- lib/matplotlib/image.py | 13 +++++++++++-- lib/matplotlib/tests/test_image.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 495f131a1b24..264af2b9be69 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -275,8 +275,8 @@ def __init__(self, ax, def __str__(self): try: - size = self.get_size() - return f"{type(self).__name__}(size={size!r})" + shape = self.get_shape() + return f"{type(self).__name__}(shape={shape!r})" except RuntimeError: return type(self).__name__ @@ -291,6 +291,15 @@ def get_size(self): return self._A.shape[:2] + def get_shape(self): + """ + Return the shape of the image as tuple (numrows, numcols, channels). + """ + if self._A is None: + raise RuntimeError('You must first set the image array') + + return self._A.shape + def set_alpha(self, alpha): """ Set the alpha value used for blending - not supported on all backends. diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 3ab99104c7ee..555e3ac95e5c 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1468,3 +1468,21 @@ def test__resample_valid_output(): resample(np.zeros((9, 9), np.uint8), np.zeros((9, 9))) with pytest.raises(ValueError, match="must be C-contiguous"): resample(np.zeros((9, 9)), np.zeros((9, 9)).T) + + +def test_axesimage_get_size(): + # generate dummy image to test get_size method + ax = plt.gca() + im = AxesImage(ax) + z = np.arange(12, dtype=float).reshape((4, 3)) + im.set_data(z) + assert im.get_size() == (4, 3) + + +def test_axesimage_get_shape(): + # generate dummy image to test get_shape method + ax = plt.gca() + im = AxesImage(ax) + z = np.arange(12, dtype=float).reshape((4, 3)) + im.set_data(z) + assert im.get_shape() == (4, 3) From bfd3ccf4481ef38b1ab7eb368d76f9dda1c44461 Mon Sep 17 00:00:00 2001 From: saranti Date: Sat, 11 Mar 2023 16:08:49 +1100 Subject: [PATCH 2/6] merge tests and add exception test --- lib/matplotlib/tests/test_image.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 555e3ac95e5c..bb8ffd7f2eac 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1470,19 +1470,13 @@ def test__resample_valid_output(): resample(np.zeros((9, 9)), np.zeros((9, 9)).T) -def test_axesimage_get_size(): - # generate dummy image to test get_size method - ax = plt.gca() - im = AxesImage(ax) - z = np.arange(12, dtype=float).reshape((4, 3)) - im.set_data(z) - assert im.get_size() == (4, 3) - - def test_axesimage_get_shape(): # generate dummy image to test get_shape method ax = plt.gca() im = AxesImage(ax) + with pytest.raises(RuntimeError): + im.get_shape() z = np.arange(12, dtype=float).reshape((4, 3)) im.set_data(z) assert im.get_shape() == (4, 3) + assert im.get_size() == im.get_shape() From 86c50637e098606eae748678327cd207b553311e Mon Sep 17 00:00:00 2001 From: saranti Date: Tue, 14 Mar 2023 22:04:16 +1100 Subject: [PATCH 3/6] match the correct runtime error --- lib/matplotlib/tests/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index bb8ffd7f2eac..5d44dc0694ec 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1474,7 +1474,7 @@ def test_axesimage_get_shape(): # generate dummy image to test get_shape method ax = plt.gca() im = AxesImage(ax) - with pytest.raises(RuntimeError): + with pytest.raises(RuntimeError, match="You must first set the image array"): im.get_shape() z = np.arange(12, dtype=float).reshape((4, 3)) im.set_data(z) From ca54ced3ca625f394eb69b93b763b34a113c6638 Mon Sep 17 00:00:00 2001 From: saranti Date: Thu, 16 Mar 2023 22:05:43 +1100 Subject: [PATCH 4/6] remove reduntant lines by making get_size call get_shape --- lib/matplotlib/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 264af2b9be69..9506408de7f9 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -287,7 +287,7 @@ def __getstate__(self): def get_size(self): """Return the size of the image as tuple (numrows, numcols).""" if self._A is None: - raise RuntimeError('You must first set the image array') + self.get_shape() return self._A.shape[:2] From 076e3c17e83fbc16731e9ffc3f1fd7ece5c1b8dd Mon Sep 17 00:00:00 2001 From: saranti Date: Thu, 16 Mar 2023 23:01:36 +1100 Subject: [PATCH 5/6] simplify further --- lib/matplotlib/image.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 9506408de7f9..9cea4e5a3e22 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -286,9 +286,6 @@ def __getstate__(self): def get_size(self): """Return the size of the image as tuple (numrows, numcols).""" - if self._A is None: - self.get_shape() - return self._A.shape[:2] def get_shape(self): From 34dc0dabbd6904270c9f97e0be8d94a020047e8c Mon Sep 17 00:00:00 2001 From: saranti Date: Thu, 16 Mar 2023 23:07:43 +1100 Subject: [PATCH 6/6] fix previous commit error --- lib/matplotlib/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 9cea4e5a3e22..eb1dedae34e1 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -286,7 +286,7 @@ def __getstate__(self): def get_size(self): """Return the size of the image as tuple (numrows, numcols).""" - return self._A.shape[:2] + return self.get_shape()[:2] def get_shape(self): """