Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Numpy 2.0 compatibility issues #1706

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
- name: Install Apt dependencies
run: |
sudo apt update
sudo apt install libboost-dev gfortran libopenmpi-dev libpython3-dev \
libblas-dev liblapack-dev libhdf5-dev
sudo apt install libboost-dev gfortran libopenmpi-dev libblas-dev \
liblapack-dev libhdf5-dev
gcc --version
- name: Upgrade pip
run: python3 -m pip install -U pip setuptools wheel
Expand All @@ -59,13 +59,20 @@ jobs:
select-by-folder: /home/runner/work/cantera/cantera/test/matlab_experimental

ubuntu-multiple-pythons:
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}, Numpy ${{ matrix.numpy || 'latest' }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
matrix:
python-version: ['3.8', '3.10', '3.11', '3.12']
os: ['ubuntu-20.04', 'ubuntu-22.04']
numpy: ['']
include:
# Keep some test cases with NumPy 1.x until we drop support
- python-version: '3.12'
os: 'ubuntu-24.04'
numpy: "'<2.0'"

fail-fast: false
env:
HDF5_LIBDIR: /usr/lib/x86_64-linux-gnu/hdf5/serial
Expand All @@ -83,15 +90,16 @@ jobs:
- name: Install Apt dependencies
run: |
sudo apt update
sudo apt install libboost-dev gfortran libopenmpi-dev libpython3-dev \
libblas-dev liblapack-dev libhdf5-dev libfmt-dev
sudo apt install libboost-dev gfortran libopenmpi-dev libblas-dev \
liblapack-dev libhdf5-dev libfmt-dev
gcc --version
- name: Upgrade pip
run: python3 -m pip install -U pip setuptools wheel
- name: Install Python dependencies
run: |
python3 -m pip install ruamel.yaml scons==3.1.2 numpy cython pandas pytest \
pytest-github-actions-annotate-failures pytest-xdist pint graphviz
python3 -m pip install ruamel.yaml scons==3.1.2 numpy${{ matrix.numpy }} \
cython pandas pytest pytest-github-actions-annotate-failures pytest-xdist \
pint graphviz
- name: Build Cantera
run: |
python3 `which scons` build env_vars=all -j4 debug=n --debug=time \
Expand Down Expand Up @@ -393,14 +401,38 @@ jobs:
"${DOCS_OUTPUT_DIR}" ${RSYNC_USER}@${RSYNC_SERVER}:${RSYNC_DEST}

run-examples:
name: Run Python ${{ matrix.python-version }} examples on ${{ matrix.os }}
name: Run Python ${{ matrix.python-version }} examples on ${{ matrix.os }}, NumPy ${{ matrix.numpy || 'latest' }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
needs: ["ubuntu-multiple-pythons"]
strategy:
matrix:
os: ["ubuntu-20.04", "ubuntu-22.04"]
python-version: ['3.8', '3.10', '3.11', '3.12']
# Keep some test cases with NumPy 1.x until we explicitly drop support
include:
- os: "ubuntu-20.04"
python-version: "3.8"
numpy: ""
- os: "ubuntu-22.04"
python-version: "3.8"
numpy: ""
- os: "ubuntu-20.04"
python-version: "3.10"
numpy: "==1.21.6"
- os: "ubuntu-22.04"
python-version: "3.10"
numpy: ""
- os: "ubuntu-22.04"
python-version: "3.11"
numpy: "==1.23.5"
- os: "ubuntu-20.04"
python-version: "3.11"
numpy: ""
- os: "ubuntu-22.04"
python-version: "3.12"
numpy: "==1.26.4"
- os: "ubuntu-20.04"
python-version: "3.12"
numpy: ""
fail-fast: false
env:
HDF5_LIBDIR: /usr/lib/x86_64-linux-gnu/hdf5/serial
Expand Down Expand Up @@ -433,8 +465,8 @@ jobs:
run: python3 -m pip install -U pip setuptools wheel
- name: Install Python dependencies
run: |
python3 -m pip install numpy ruamel.yaml pandas pyarrow matplotlib scipy \
pint graphviz CoolProp
python3 -m pip install numpy${{ matrix.numpy }} ruamel.yaml pandas pyarrow \
matplotlib scipy pint graphviz CoolProp
python3 -m pip install --pre --no-index --find-links dist cantera
- name: Run the examples
# See https://unix.stackexchange.com/a/392973 for an explanation of the -exec part.
Expand All @@ -450,15 +482,17 @@ 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
with:
path: results.txt
retention-days: 2
name: example-results-${{ matrix.python-version }}-${{ matrix.os }}
name: example-results-${{ matrix.python-version }}-${{ matrix.os }}-np${{ matrix.numpy || 'latest' }}
# Run this step if the job was successful or failed, but not if it was cancelled
# Using always() would run this step if the job was cancelled as well.
if: failure() || success()
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 @@ -487,11 +487,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 @@ -1851,7 +1851,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
Loading