Skip to content

Commit

Permalink
[Python] Fix Numpy 2.0 compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jun 11, 2024
1 parent 026c5c7 commit 0f2ef59
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 14 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,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
Expand Down
8 changes: 4 additions & 4 deletions samples/python/onedim/flame_speed_convergence_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions site_scons/buildutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]),
Expand Down
4 changes: 2 additions & 2 deletions test/python/test_onedim.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,11 @@ def test_unity_lewis(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion test/python/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ def test_interface_adjacent(self):
class InterfaceReactionTests(ReactionTests):
# test suite for surface reaction expressions

_value = np.NAN # reference value
_value = np.nan # reference value
_coverage_deps = None

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions test/python/test_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,9 @@ def calc_dtdh(self, species):
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)
Expand Down
2 changes: 2 additions & 0 deletions test/python/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,8 @@ def test_isotropic_velocity_electron_energy_distribution(self):
(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])
Expand Down
6 changes: 3 additions & 3 deletions test/python/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0f2ef59

Please sign in to comment.