From dcd3e463a23716679d1f88e34cd7d814f6c93510 Mon Sep 17 00:00:00 2001 From: Piotr Dziekan Date: Mon, 24 Aug 2026 16:08:05 +0200 Subject: [PATCH 1/2] np.assert_allclose instead of isclose.all --- long_test/test_timestep.py | 2 +- unit_test/test_pressure.py | 2 +- unit_test_debug/test_spectrum.py | 13 +++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/long_test/test_timestep.py b/long_test/test_timestep.py index 7ad6d90..34c1a93 100644 --- a/long_test/test_timestep.py +++ b/long_test/test_timestep.py @@ -95,7 +95,7 @@ def test_timestep_diff(data, dt, eps=2.e-4): f_test = netcdf.netcdf_file(filename, "r") f_ref = netcdf.netcdf_file(os.path.join("long_test/refdata", filename), "r") for var in ["t", "z", "th_d", "T", "p", "r_v", "rhod"]: - assert np.isclose(f_test.variables[var][:], f_ref.variables[var][:], atol=0, rtol=eps).all(), "differs e.g. " + str(var) + "; max(ref diff) = " + str(np.where(f_ref.variables[var][:] != 0., abs((f_test.variables[var][:]-f_ref.variables[var][:])/f_ref.variables[var][:]), 0.).max()) + np.testing.assert_allclose(f_test.variables[var][:], f_ref.variables[var][:], atol=0, rtol=eps, err_msg="differs e.g. " + str(var) + "; max(ref diff) = " + str(np.where(f_ref.variables[var][:] != 0., abs((f_test.variables[var][:]-f_ref.variables[var][:])/f_ref.variables[var][:]), 0.).max())) def test_timestep_plot(data): diff --git a/unit_test/test_pressure.py b/unit_test/test_pressure.py index d5cbab9..9be62a8 100644 --- a/unit_test/test_pressure.py +++ b/unit_test/test_pressure.py @@ -64,7 +64,7 @@ def test_pressure_diff(data, pprof, eps=3.e-4): f_ref = netcdf.netcdf_file(os.path.join("unit_test/refdata", "profopttest_" + pprof + str(data["dt"]) + ".nc"), "r") for var in ["t", "z", "th_d", "T", "p", "r_v", "rhod"]: - assert np.isclose(f_ref.variables[var][:], data[pprof].variables[var][:], atol=0, rtol=eps).all(), "differs e.g. " + str(var) + "; max(ref diff) = " + str(np.where(f_ref.variables[var][:] != 0., abs((data[pprof].variables[var][:]-f_ref.variables[var][:])/f_ref.variables[var][:]), 0.).max()) + np.testing.assert_allclose(f_ref.variables[var][:], data[pprof].variables[var][:], atol=0, rtol=eps, err_msg="differs e.g. " + str(var) + "; max(ref diff) = " + str(np.where(f_ref.variables[var][:] != 0., abs((data[pprof].variables[var][:]-f_ref.variables[var][:])/f_ref.variables[var][:]), 0.).max())) assert np.isclose(f_ref.RH_max, data[pprof].RH_max, atol=0, rtol=eps) diff --git a/unit_test_debug/test_spectrum.py b/unit_test_debug/test_spectrum.py index aed8aa6..ccce349 100644 --- a/unit_test_debug/test_spectrum.py +++ b/unit_test_debug/test_spectrum.py @@ -47,7 +47,7 @@ def test_bin_checker(data, name_spect, eps_d=1.e-14): dr = np.empty(r_nc.shape[0] - 1) dr[:] = (r_nc[1:] - r_nc[0:-1]) - assert np.isclose(dr, dr_nc[:-1], atol=0, rtol=eps_d).all() + np.testing.assert_allclose(dr, dr_nc[:-1], atol=0, rtol=eps_d) @pytest.mark.parametrize("var", ["wradii_r_wet", "wradii_dr_wet", "linwradii_r_wet", "linwradii_dr_wet", @@ -61,11 +61,11 @@ def test_spectrum_diff(data, var, eps_d = 1e-15): f_ref = netcdf.netcdf_file("unit_test/refdata/test_spectrum.nc", "r") # ... the bin edges and bin sizes ... - assert np.isclose(f_ref.variables[var][:], data.variables[var][:],atol=0, rtol=eps_d).all() + np.testing.assert_allclose(f_ref.variables[var][:], data.variables[var][:], atol=0, rtol=eps_d) # ... and 0th, 1st, 3rd moment of wet and dry radius size distribution -@pytest.mark.parametrize("mom, eps", [("wradii_m0", 1e-15), ("dradii_m0", 1e-15), - ("lindradii_m0", 1e-15), ("linwradii_m0", 1e-15), +@pytest.mark.parametrize("mom, eps", [("wradii_m0", 1e-15), ("dradii_m0", 1e-14), + ("lindradii_m0", 1e-14), ("linwradii_m0", 1e-15), ("wradii_m1", 7e-4), ("dradii_m1", 5e-15), ("lindradii_m1", 4e-15), ("linwradii_m1", 5e-6), ("wradii_m3", 1.6e-3),("dradii_m3", 2e-14), @@ -78,9 +78,10 @@ def test_mom_checker(data, mom, eps): refdata = np.reshape(refdata, np.product(refdata.shape)) cmpdata = np.reshape(cmpdata, np.product(cmpdata.shape)) - assert np.isclose(cmpdata, refdata, atol=0, rtol=eps).all(),\ - "differs e.g. " + str(mom) + "; max(ref diff) = " +\ + np.testing.assert_allclose(cmpdata, refdata, atol=0, rtol=eps, err_msg=( + "differs e.g. " + str(mom) + "; max(ref diff) = " + str(np.where(refdata != 0.,abs((cmpdata - refdata) / refdata), abs(cmpdata - refdata)).max()) + )) def test_spectrum_plot(data): From b2727a962aafc70c15ef9bd46c9d5f36931aee01 Mon Sep 17 00:00:00 2001 From: Piotr Dziekan Date: Tue, 25 Aug 2026 12:06:22 +0200 Subject: [PATCH 2/2] relax test_spectrum_diff - it was below machine precision? --- unit_test_debug/test_spectrum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit_test_debug/test_spectrum.py b/unit_test_debug/test_spectrum.py index ccce349..e190972 100644 --- a/unit_test_debug/test_spectrum.py +++ b/unit_test_debug/test_spectrum.py @@ -52,7 +52,7 @@ def test_bin_checker(data, name_spect, eps_d=1.e-14): @pytest.mark.parametrize("var", ["wradii_r_wet", "wradii_dr_wet", "linwradii_r_wet", "linwradii_dr_wet", "dradii_r_dry", "dradii_dr_dry", "lindradii_r_dry", "lindradii_dr_dry"]) -def test_spectrum_diff(data, var, eps_d = 1e-15): +def test_spectrum_diff(data, var, eps_d = 1e-14): """ Compare the results with the referential simulation (stored in refdata folder)