From 4d509b5423b5e84208b4f6498607c053fe856714 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 8 Jun 2024 18:38:38 -0400 Subject: [PATCH] [Python] Fix Numpy 2.0 compatibility issues --- .github/workflows/main.yml | 6 ++++-- samples/python/onedim/flame_speed_convergence_analysis.py | 8 ++++---- site_scons/buildutils.py | 4 ++-- test/python/test_onedim.py | 4 ++-- test/python/test_reaction.py | 2 +- test/python/test_reactor.py | 2 ++ test/python/test_thermo.py | 2 ++ test/python/utilities.py | 6 +++--- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 92d4b1e47..bbc0fac9e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -450,8 +450,10 @@ jobs: find samples/python -type f -iname "*.py" \ -exec sh -c 'for n; do echo "$n" | tee -a results.txt && python3 "$n" >> results.txt || exit 1; done' sh {} + env: - # The ignore setting here is due to a new warning introduced in Matplotlib==3.6.0 - PYTHONWARNINGS: "error,ignore:warn_name_set_on_empty_Forward::pyparsing,ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:" + # The pyparsing ignore setting is due to a new warning introduced in Matplotlib==3.6.0 + # @todo: Remove the trapz-related ignore when dropping support for NumPy 1.x + # and replacing np.trapz with np.trapezoid + PYTHONWARNINGS: "error,ignore:warn_name_set_on_empty_Forward::pyparsing,ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:,ignore:`trapz`:DeprecationWarning" MPLBACKEND: Agg - name: Save the results file for inspection uses: actions/upload-artifact@v4 diff --git a/samples/python/onedim/flame_speed_convergence_analysis.py b/samples/python/onedim/flame_speed_convergence_analysis.py index 5b1f41b7a..545123f49 100644 --- a/samples/python/onedim/flame_speed_convergence_analysis.py +++ b/samples/python/onedim/flame_speed_convergence_analysis.py @@ -305,10 +305,10 @@ def analyze_errors(grids, speeds, true_speed): and compare these with our estimated errors. This will show if our estimates are reasonable, or conservative, or too optimistic. """ - true_speed_estimates = np.full_like(speeds, np.NaN) - total_percent_error_estimates = np.full_like(speeds, np.NaN) - actual_extrapolated_percent_errors = np.full_like(speeds, np.NaN) - actual_raw_percent_errors = np.full_like(speeds, np.NaN) + true_speed_estimates = np.full_like(speeds, np.nan) + total_percent_error_estimates = np.full_like(speeds, np.nan) + actual_extrapolated_percent_errors = np.full_like(speeds, np.nan) + actual_raw_percent_errors = np.full_like(speeds, np.nan) for i in range(3, len(grids)): print(grids[: i + 1]) true_speed_estimate, total_percent_error_estimate = extrapolate_uncertainty( diff --git a/site_scons/buildutils.py b/site_scons/buildutils.py index 4fdf6f660..c138115f9 100644 --- a/site_scons/buildutils.py +++ b/site_scons/buildutils.py @@ -972,9 +972,9 @@ def compare_profiles( "pos. err" )) header.append(f"{10*'-'} ----- {14*'-'} {14*'-'} {9*'-'} {9*'-'} {9*'-'}") - ref_ptp = reference.ptp(axis=1) + ref_ptp = np.ptp(reference, axis=1) ref_max = np.abs(reference).max(axis=1) - sample_ptp = sample.ptp(axis=1) + sample_ptp = np.ptp(sample, axis=1) sample_max = np.abs(sample).max(axis=1) scale = np.maximum( np.maximum(ref_ptp[1:], ref_max[1:]), diff --git a/test/python/test_onedim.py b/test/python/test_onedim.py index ebbd5e893..152c22124 100644 --- a/test/python/test_onedim.py +++ b/test/python/test_onedim.py @@ -487,11 +487,11 @@ class TestFreeFlame(utilities.CanteraTest): self.sim.transport_model = 'unity-Lewis-number' self.sim.set_refine_criteria(ratio=3.0, slope=0.08, curve=0.12) self.sim.solve(loglevel=0, auto=True) - dh_unity_lewis = self.sim.enthalpy_mass.ptp() + dh_unity_lewis = np.ptp(self.sim.enthalpy_mass) self.sim.transport_model = 'mixture-averaged' self.sim.solve(loglevel=0) - dh_mix = self.sim.enthalpy_mass.ptp() + dh_mix = np.ptp(self.sim.enthalpy_mass) # deviation of enthalpy should be much lower for unity Le model (tends # towards zero as grid is refined) diff --git a/test/python/test_reaction.py b/test/python/test_reaction.py index 10fcc3d73..54000a35c 100644 --- a/test/python/test_reaction.py +++ b/test/python/test_reaction.py @@ -1851,7 +1851,7 @@ class TestExtensible3(utilities.CanteraTest): class InterfaceReactionTests(ReactionTests): # test suite for surface reaction expressions - _value = np.NAN # reference value + _value = np.nan # reference value _coverage_deps = None @classmethod diff --git a/test/python/test_reactor.py b/test/python/test_reactor.py index 818233b71..25806ea40 100644 --- a/test/python/test_reactor.py +++ b/test/python/test_reactor.py @@ -2383,7 +2383,9 @@ class TestReactorSensitivities(utilities.CanteraTest): return dtdp # See https://github.com/Cantera/enhancements/issues/55 + # @todo: replace np.trapz with np.trapezoid when dropping support for NumPy 1.x @unittest.skip("Integration of sensitivity ODEs is unreliable") + @pytest.mark.filterwarnings("ignore:`trapz` is deprecated") def test_ignition_delay_sensitivity(self): species = ('H2', 'H', 'O2', 'H2O2', 'H2O', 'OH', 'HO2') dtigdh_cvodes = self.calc_dtdh(species) diff --git a/test/python/test_thermo.py b/test/python/test_thermo.py index 8f96eef53..1c2ab424d 100644 --- a/test/python/test_thermo.py +++ b/test/python/test_thermo.py @@ -1208,6 +1208,8 @@ class TestPlasmaPhase(utilities.CanteraTest): (ct.avogadro * ct.electron_charge)) self.assertNear(mean_electron_energy, self.phase.mean_electron_energy) + # @todo: replace np.trapz with np.trapezoid when dropping support for NumPy 1.x + @pytest.mark.filterwarnings("ignore:`trapz` is deprecated") def test_discretized_electron_energy_distribution(self): levels = np.array([0.0, 1.0, 10.0]) dist = np.array([0.0, 0.9, 0.01]) diff --git a/test/python/utilities.py b/test/python/utilities.py index dd25084f1..90732f36d 100644 --- a/test/python/utilities.py +++ b/test/python/utilities.py @@ -181,10 +181,10 @@ def compareProfiles(reference, sample, rtol=1e-5, atol=1e-12, xtol=1e-5): bad = [] template = '{0:9.4e} {1: 3d} {2:14.7e} {3:14.7e} {4:9.3e} {5:9.3e} {6:9.3e}' for i in range(1, nVars): - scale = max(max(abs(reference[i])), reference[i].ptp(), - max(abs(sample[i])), sample[i].ptp()) + scale = max(max(abs(reference[i])), np.ptp(reference[i]), + max(abs(sample[i])), np.ptp(sample[i])) slope = np.zeros(nTimes) - slope[1:] = np.diff(reference[i]) / np.diff(reference[0]) * reference[0].ptp() + slope[1:] = np.diff(reference[i]) / np.diff(reference[0]) * np.ptp(reference[0]) comp = np.interp(reference[0], sample[0], sample[i]) for j in range(nTimes):