From d63bb2edee1d54a606dc85e6d3f5ec56be3dfc83 Mon Sep 17 00:00:00 2001 From: Philipp Sommer <philipp.sommer@hereon.de> Date: Fri, 12 Jul 2024 09:09:18 +0200 Subject: [PATCH] consequently use assertAlmostArrayEqual for array comparison --- tests/test_combined.py | 30 ++++++++++++++++-------------- tests/test_combined_icon.py | 30 ++++++++++++++++-------------- tests/test_plot2d.py | 22 ++++++++++++---------- tests/test_plot2d_icon.py | 17 +++++++++-------- tests/test_plot2d_icon_edge.py | 19 ++++++++++--------- tests/test_vector.py | 19 ++++++++++--------- tests/test_vector_icon.py | 19 ++++++++++--------- 7 files changed, 83 insertions(+), 73 deletions(-) diff --git a/tests/test_combined.py b/tests/test_combined.py index 330645b..27c5c22 100644 --- a/tests/test_combined.py +++ b/tests/test_combined.py @@ -1,6 +1,5 @@ """Test module for the CombinedSimplePlotter.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -318,20 +317,22 @@ class CombinedSimplePlotterTest(tv.SimpleVectorPlotterTest): 286.03, 289.84, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(250, 290, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(250, 290, 5, endpoint=True), + atol=1e-2, ) # test vector bounds self.update(color="absolute") - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 2).tolist(), - np.linspace(0, 15, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, + np.linspace(0, 15, 11, endpoint=True), + atol=1e-2, ) self.update(vbounds="minmax") bounds = [ @@ -347,13 +348,14 @@ class CombinedSimplePlotterTest(tv.SimpleVectorPlotterTest): 9.77, 10.81, ] - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, bounds, atol=1e-2 ) self.update(vbounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 3).tolist(), - np.linspace(1.0, 8.5, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, + np.linspace(1.0, 8.5, 5, endpoint=True), + atol=1e-3, ) def test_clabel(self): diff --git a/tests/test_combined_icon.py b/tests/test_combined_icon.py index 76a7ceb..eb4f3e5 100644 --- a/tests/test_combined_icon.py +++ b/tests/test_combined_icon.py @@ -1,6 +1,5 @@ """Test module for the combined simple plotter for the icon grid.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -107,20 +106,22 @@ class IconCombinedSimplePlotterTest( 300.21, 306.48, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(255, 305, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(255, 305, 5, endpoint=True), + atol=1e-2, ) # test vector bounds self.update(color="absolute") - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 2).tolist(), - np.linspace(0, 15, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, + np.linspace(0, 15, 11, endpoint=True), + atol=1e-2, ) self.update(vbounds="minmax") bounds = [ @@ -136,13 +137,14 @@ class IconCombinedSimplePlotterTest( 9.99, 11.09, ] - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, bounds, atol=1e-2 ) self.update(vbounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.vbounds.norm.boundaries, 2).tolist(), - np.round(np.linspace(0.5, 9.0, 5, endpoint=True), 2).tolist(), + self.assertAlmostArrayEqual( + self.plotter.vbounds.norm.boundaries, + np.linspace(0.5, 9.0, 5, endpoint=True), + atol=1e-2, ) @property diff --git a/tests/test_plot2d.py b/tests/test_plot2d.py index 6c881d6..3cf7ac0 100644 --- a/tests/test_plot2d.py +++ b/tests/test_plot2d.py @@ -1,6 +1,5 @@ """Test module for 2D plots.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -217,11 +216,12 @@ class Simple2DPlotterTest(tl.LinePlotterTest, References2D): cticks = self._minmax_cticks self.update(cticks="minmax") cbar = self.plotter.cbar.cbars["b"] - self.assertEqual( + self.assertAlmostArrayEqual( list( map(lambda t: float(t.get_text()), cbar.ax.get_xticklabels()) ), cticks, + atol=1e-2, ) self.update(cticklabels="%3.1f") cticks = np.round(cticks, decimals=1).tolist() @@ -288,9 +288,10 @@ class Simple2DPlotterTest(tl.LinePlotterTest, References2D): def test_bounds(self): """Test bounds formatoption""" - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(235, 310, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(235, 310, 11, endpoint=True), + atol=1e-2, ) self.update(bounds="minmax") bounds = [ @@ -306,13 +307,14 @@ class Simple2DPlotterTest(tl.LinePlotterTest, References2D): 302.79, 309.78, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(245, 300, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(245, 300, 5, endpoint=True), + atol=1e-2, ) def test_miss_color(self, *args): diff --git a/tests/test_plot2d_icon.py b/tests/test_plot2d_icon.py index a29f3e9..17500d3 100644 --- a/tests/test_plot2d_icon.py +++ b/tests/test_plot2d_icon.py @@ -1,6 +1,5 @@ """Test module for the 2D plot of icon files.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -112,9 +111,10 @@ class IconSimplePlotterTest(IconTestMixin, t2d.Simple2DPlotterTest): def test_bounds(self): """Test bounds formatoption""" - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(240, 310, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(240, 310, 11, endpoint=True), + atol=1e-2, ) self.update(bounds="minmax") bounds = [ @@ -130,11 +130,12 @@ class IconSimplePlotterTest(IconTestMixin, t2d.Simple2DPlotterTest): 300.21, 306.48, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, np.linspace(255, 305, 5, endpoint=True).tolist(), + atol=1e-2, ) diff --git a/tests/test_plot2d_icon_edge.py b/tests/test_plot2d_icon_edge.py index 59e5af1..a2d565a 100644 --- a/tests/test_plot2d_icon_edge.py +++ b/tests/test_plot2d_icon_edge.py @@ -1,6 +1,5 @@ """Test module for the contour plot for the icon edge grid.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -27,9 +26,10 @@ class IconEdgeSimplePlotterTest(IconTestMixin, t2d.Simple2DPlotterTest): def test_bounds(self): """Test bounds formatoption""" - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(240, 310, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(240, 310, 11, endpoint=True), + atol=1e-2, ) self.update(bounds="minmax") bounds = [ @@ -45,11 +45,12 @@ class IconEdgeSimplePlotterTest(IconTestMixin, t2d.Simple2DPlotterTest): 301.67, 308.25, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(255, 305, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(255, 305, 5, endpoint=True), + atol=1e-2, ) diff --git a/tests/test_vector.py b/tests/test_vector.py index bc54690..88a06ef 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -1,6 +1,5 @@ """Test module for vector (quiver) plots.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -149,9 +148,10 @@ class SimpleVectorPlotterTest(t2d.Simple2DPlotterTest): def test_bounds(self): """Test bounds formatoption""" self.update(color="absolute") - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(0, 15, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(0, 15, 11, endpoint=True), + atol=1e-2, ) self.update(bounds="minmax") bounds = [ @@ -168,11 +168,12 @@ class SimpleVectorPlotterTest(t2d.Simple2DPlotterTest): 10.81, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 3).tolist(), - np.linspace(1.0, 8.5, 5, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(1.0, 8.5, 5, endpoint=True), + atol=1e-3, ) diff --git a/tests/test_vector_icon.py b/tests/test_vector_icon.py index fe7d4cd..45bae2a 100644 --- a/tests/test_vector_icon.py +++ b/tests/test_vector_icon.py @@ -1,6 +1,5 @@ """Test module for vector (quiver) plots with icon data.""" - # SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum Hereon # SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht # SPDX-FileCopyrightText: 2016-2024 University of Lausanne @@ -52,9 +51,10 @@ class IconSimpleVectorPlotterTest(IconTestMixin, tv.SimpleVectorPlotterTest): def test_bounds(self): """Test bounds formatoption""" self.update(color="absolute") - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.linspace(0, 15, 11, endpoint=True).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(0, 15, 11, endpoint=True), + atol=1e-2, ) self.update(bounds="minmax") bounds = [ @@ -70,11 +70,12 @@ class IconSimpleVectorPlotterTest(IconTestMixin, tv.SimpleVectorPlotterTest): 9.99, 11.09, ] - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), bounds + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, bounds, atol=1e-2 ) self.update(bounds=["rounded", 5, 5, 95]) - self.assertEqual( - np.round(self.plotter.bounds.norm.boundaries, 2).tolist(), - np.round(np.linspace(0.5, 9.0, 5, endpoint=True), 2).tolist(), + self.assertAlmostArrayEqual( + self.plotter.bounds.norm.boundaries, + np.linspace(0.5, 9.0, 5, endpoint=True), + atol=1e-2, ) -- GitLab