Skip to content

Commit

Permalink
NumPy compatibility fixes (deprecated API elements) + Python 3.12 com…
Browse files Browse the repository at this point in the history
…patibility fixes (setuptools, etc) + upper Python version bump for CI to 3.12 (#1402)
  • Loading branch information
slayoo authored Nov 11, 2024
1 parent d00d17c commit f192545
Show file tree
Hide file tree
Showing 26 changed files with 70 additions and 58 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/tests+artifacts+pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
persist-credentials: false
- uses: actions/[email protected]
with:
python-version: 3.9
python-version: "3.12"
- run: |
pip install pdoc nbformat
pip install -e .
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.11"]
python-version: ["3.9", "3.12"]
test-suite: ["unit_tests/!(dynamics)", "unit_tests/dynamics/!(condensation)", "unit_tests/dynamics/condensation", "smoke_tests/no_env", "smoke_tests/box", "smoke_tests/parcel_a", "smoke_tests/parcel_b", "smoke_tests/parcel_c", "smoke_tests/kinematic_1d", "smoke_tests/kinematic_2d", "tutorials_tests"]
exclude:
- platform: "macos-14"
Expand Down Expand Up @@ -197,7 +197,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-24.04, macos-13, windows-latest]
python-version: ["3.9", "3.11"]
python-version: ["3.9", "3.12"]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/[email protected]
Expand Down Expand Up @@ -233,7 +233,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-24.04, macos-13, windows-latest]
python-version: ["3.9", "3.11"]
python-version: ["3.9", "3.12"]
test-suite: [ "chemistry_freezing_isotopes", "condensation_a", "condensation_b", "coagulation", "breakup", "multi-process_a", "multi-process_b"]
fail-fast: false
runs-on: ${{ matrix.platform }}
Expand Down Expand Up @@ -318,15 +318,19 @@ jobs:
fetch-depth: 0 # https://github.com/pypa/setuptools_scm/issues/480
- uses: actions/[email protected]
with:
python-version: "3.11"
python-version: "3.12"

- run: pip install twine build

- run: |
unset CI
cd ${{ matrix.packages-dir }}
python -m build 2>&1 | tee build.log
exit `fgrep -i warning build.log | grep -v impl_numba/warnings.py | grep -v "no previously-included files matching" | wc -l`
exit `fgrep -i warning build.log | grep -v impl_numba/warnings.py \
| grep -v "no previously-included files matching" \
| grep -v "version of {dist_name} already set" \
| grep -v -E "UserWarning: version of PySDM(-examples)? already set" \
| wc -l`
twine check --strict dist/*
- uses: actions/upload-artifact@v4
Expand All @@ -338,7 +342,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.11"]
python-version: ["3.9", "3.12"]
exclude:
- platform: macos-14
python-version: "3.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(self, settings, products=None):
key="S_VI",
dry_radius_bins_edges=settings.dry_radius_bins_edges,
name="dm_S_VI/dlog_10(dry diameter)",
unit='ug / m^3"',
unit="ug / m^3",
),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/PySDM_examples/Srivastava_1982/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def run_convergence_analysis(self, x, seeds):
if step != 0:
particulator.run(steps=1)
for prod in self.settings.prods:
self.simulation_res[n_sd][prod][seed][step] = (
(self.simulation_res[n_sd][prod][seed][step],) = (
particulator.products[prod].get()
)

Expand Down
6 changes: 3 additions & 3 deletions examples/PySDM_examples/deJong_Azimi/simulation_0D.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def run_box(settings, backend_class=CPU):
for i, step in enumerate(settings.steps):
particulator.run(step - particulator.n_steps)
y[i] = particulator.products["dv/dlnr"].get()[0]
mom[i, 0] = particulator.products["M0"].get()
mom[i, 1] = particulator.products["M1"].get()
mom[i, 2] = particulator.products["M2"].get()
(mom[i, 0],) = particulator.products["M0"].get()
(mom[i, 1],) = particulator.products["M1"].get()
(mom[i, 2],) = particulator.products["M2"].get()

return namedtuple("_", ("radius_bins_left_edges", "dv_dlnr", "moments"))(
radius_bins_left_edges=settings.radius_bins_edges[:-1], dv_dlnr=y, moments=mom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def run_box_breakup(
core.run(step - core.n_steps)
y[i] = core.products["dv/dlnr"].get()[0]
if return_nv:
y2[i] = core.products["N(v)"].get()[0]
rates[i, 0] = core.products["cr"].get()
rates[i, 1] = core.products["crd"].get()
rates[i, 2] = core.products["cor"].get()
rates[i, 3] = core.products["br"].get()
(y2[i],) = core.products["N(v)"].get()
(rates[i, 0],) = core.products["cr"].get()
(rates[i, 1],) = core.products["crd"].get()
(rates[i, 2],) = core.products["cor"].get()
(rates[i, 3],) = core.products["br"].get()

x = (settings.radius_bins_edges[:-1] / si.micrometres,)[0]

Expand Down Expand Up @@ -124,10 +124,10 @@ def run_box_NObreakup(settings, steps=None, backend_class=CPU):
# run
for i, step in enumerate(steps):
core.run(step - core.n_steps)
y[i] = core.products["dv/dlnr"].get()[0]
rates[i, 0] = core.products["cr"].get()
rates[i, 1] = core.products["crd"].get()
rates[i, 2] = core.products["cor"].get()
(y[i],) = core.products["dv/dlnr"].get()
(rates[i, 0],) = core.products["cr"].get()
(rates[i, 1],) = core.products["crd"].get()
(rates[i, 2],) = core.products["cor"].get()

x = (settings.radius_bins_edges[:-1] / si.micrometres,)[0]

Expand Down
5 changes: 4 additions & 1 deletion examples/PySDM_examples/seeding/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def run(self):
data[data == 0] = np.nan
attr.append(data)
for key, prod in output["products"].items():
prod.append(float(self.particulator.products[key].get()))
value = self.particulator.products[key].get()
if not isinstance(value, float):
(value,) = value
prod.append(float(value))
for out in ("attributes", "products"):
for key, val in output[out].items():
output[out][key] = np.array(val)
Expand Down
4 changes: 2 additions & 2 deletions examples/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.setuptools_scm]
root = ".."

# TODO #1200
[build-system]
requires = ['setuptools==56.0.0', 'setuptools-scm==6.0.1']
requires = ['setuptools==75.3.0', 'setuptools-scm==8.1.0']
1 change: 0 additions & 1 deletion examples/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def get_long_description():
use_scm_version={
"local_scheme": lambda _: "",
"version_scheme": "post-release",
"root": "..",
},
install_requires=[
"PySDM",
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ profile = "black"

[tool.setuptools_scm]

# TODO #1200
[build-system]
requires = ['setuptools==56.0.0', 'setuptools-scm==6.0.1']
requires = ['setuptools==75.3.0', 'setuptools-scm==8.1.0']
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def get_long_description():
"matplotlib",
"pytest",
"pytest-timeout",
"PyPartMC==1.3.6",
]
+ (["PyPartMC==1.3.6"] if sys.version_info < (3, 12) else []) # TODO #1410
+ (
[
"pywinpty" + ("==0.5.7" if CI else ""),
Expand Down
6 changes: 4 additions & 2 deletions tests/smoke_tests/box/partmc/test_dry_wet_equilibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# pylint: disable=missing-function-docstring,no-member
import platform
import sys
from collections import namedtuple

import numpy as np
Expand All @@ -16,7 +17,7 @@
from PySDM.initialisation.spectra import Lognormal
from PySDM.physics import si

if platform.architecture()[0] == "64bit":
if platform.architecture()[0] == "64bit" and sys.version_info < (3, 12): # TODO #1410
import PyPartMC

linestyles = {"PyPartMC": "dashed", "PySDM": "dotted"}
Expand Down Expand Up @@ -74,7 +75,8 @@ def pypartmc(dry_diam, temp, rel_humid, kpa):


@pytest.mark.skipif(
platform.architecture()[0] != "64bit", reason="binary package availability"
platform.architecture()[0] != "64bit" or sys.version_info >= (3, 12),
reason="binary package availability", # TODO #1410
)
@pytest.mark.parametrize("kappa", (0.1, 1))
@pytest.mark.parametrize("temperature", (300 * si.K,))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ def test_adaptive_displacement(rtol, plot=False):
# Assert
if rtol is not None:
assert 1 < simulation.particulator.dynamics["Displacement"]._n_substeps < 50
assert np.count_nonzero(sd_count) == np.product(settings.grid)
assert np.count_nonzero(sd_count) == np.prod(settings.grid)
assert np.std(sd_count) < settings.n_sd_per_gridbox / 2.5
assert np.max(sd_count) < 2.5 * settings.n_sd_per_gridbox
2 changes: 1 addition & 1 deletion tests/smoke_tests/no_env/lamb_et_al_2017/test_fig_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_values_match(notebook_local_variables, T, alpha_i_2H, paper):
plot_x = notebook_local_variables["T"]
plot_y = notebook_local_variables["alphas"][paper]
eps = 5e-1
index = np.where(abs(plot_x - T) < eps)
((index,),) = np.where(abs(plot_x - T) < eps)
np.testing.assert_approx_equal(
actual=plot_y[index], desired=alpha_i_2H, significant=3
)
Expand Down
10 changes: 5 additions & 5 deletions tests/smoke_tests/no_env/pierchala_et_al_2022/test_fig_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestFig3:
),
)
def test_top_left_panel(notebook_local_variables, isotope, F, enrichment):
index = np.where(notebook_local_variables["F"] == F)
((index,),) = np.where(notebook_local_variables["F"] == F)
np.testing.assert_approx_equal(
actual=notebook_local_variables["enrichments"][isotope][index],
desired=enrichment,
Expand All @@ -54,7 +54,7 @@ def test_top_left_panel(notebook_local_variables, isotope, F, enrichment):
),
)
def test_bottom_left_panel(notebook_local_variables, label, F, excess):
index = np.where(notebook_local_variables["F"] == F)
((index,),) = np.where(notebook_local_variables["F"] == F)
excesses = notebook_local_variables["excess"]
deltas = notebook_local_variables["deltas"]
np.testing.assert_approx_equal(
Expand All @@ -73,7 +73,7 @@ def test_bottom_left_panel(notebook_local_variables, label, F, excess):
)
def test_top_right_panel(notebook_local_variables, delta_18O, delta_2H):
eps = 0.01 * PER_MILLE
index = np.where(
((index,),) = np.where(
abs(notebook_local_variables["deltas"]["18O"] - delta_18O) < eps
)
np.testing.assert_approx_equal(
Expand All @@ -92,7 +92,7 @@ def test_gmvl(notebook_local_variables, delta_18O, delta_2H):
eps = 0.1 * PER_MILLE
x = np.linspace(-10 * PER_MILLE, 10 * PER_MILLE, 100)
y = x * cd.CRAIG_1961_SLOPE_COEFF + cd.CRAIG_1961_INTERCEPT_COEFF
index = np.where(abs(x - delta_18O) < eps)
((index,),) = np.where(abs(x - delta_18O) < eps)
np.testing.assert_approx_equal(actual=y[index], desired=delta_2H, significant=3)

@staticmethod
Expand All @@ -108,7 +108,7 @@ def test_gmvl(notebook_local_variables, delta_18O, delta_2H):
def test_bottom_right_panel(notebook_local_variables, label, delta_18O, excess):
eps = 0.1 * PER_MILLE
deltas = notebook_local_variables["deltas"]
index = np.where(abs(deltas["18O"] - delta_18O) < eps)
((index,),) = np.where(abs(deltas["18O"] - delta_18O) < eps)
excesses = notebook_local_variables["excess"]
np.testing.assert_approx_equal(
actual={
Expand Down
4 changes: 2 additions & 2 deletions tests/smoke_tests/no_env/pierchala_et_al_2022/test_fig_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TestFig4:
def test_top_panel(notebook_local_variables, RH, delta_18O, delta_2H):
deltas_per_rh = notebook_local_variables["deltas_per_rh"]
eps = 0.5 * PER_MILLE
index = np.where(abs(deltas_per_rh[RH]["18O"] - delta_18O) < eps)
((index,),) = np.where(abs(deltas_per_rh[RH]["18O"] - delta_18O) < eps)
np.testing.assert_approx_equal(
actual=delta_2H, desired=deltas_per_rh[RH]["2H"][index], significant=3
)
Expand All @@ -58,7 +58,7 @@ def test_top_panel(notebook_local_variables, RH, delta_18O, delta_2H):
def test_bottom_panel(notebook_local_variables, RH, delta_18O, excess_17O):
deltas_per_rh = notebook_local_variables["deltas_per_rh"]
eps = 0.5 * PER_MILLE
index = np.where(abs(deltas_per_rh[RH]["18O"] - delta_18O) < eps)
((index,),) = np.where(abs(deltas_per_rh[RH]["18O"] - delta_18O) < eps)
np.testing.assert_approx_equal(
actual=notebook_local_variables[
"formulae"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_water_mass_conservation(settings_idx, mass_of_dry_air, scheme, coord):
output = simulation.run()

# Assert
total_water_mixing_ratio = simulation.particulator.environment[
(total_water_mixing_ratio,) = simulation.particulator.environment[
"water_vapour_mixing_ratio"
].to_ndarray() + liquid_water_mixing_ratio(simulation)
np.testing.assert_approx_equal(
Expand Down Expand Up @@ -77,4 +77,4 @@ def test_energy_conservation(settings_idx, mass_of_dry_air, coord):
simulation.run()

# Assert
np.testing.assert_approx_equal(thd0.to_ndarray(), env["thd"].to_ndarray())
np.testing.assert_array_almost_equal(thd0.to_ndarray(), env["thd"].to_ndarray())
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class TestInitialisation:
@staticmethod
def simulation_test(var, expected, setup):
simulation = Simulation(setup)
np.testing.assert_approx_equal(
simulation.particulator.environment[var].to_ndarray(), expected
)
(actual,) = simulation.particulator.environment[var].to_ndarray()
np.testing.assert_approx_equal(actual=actual, desired=expected)

@staticmethod
@pytest.mark.parametrize("settings_idx", range(len(setups)))
Expand Down
7 changes: 4 additions & 3 deletions tests/unit_tests/attributes/test_isotopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ def test_delta_attribute(backend_class, isotope, heavier_water_specific_content)
particulator = builder.build(attributes=attributes)

# act
delta = particulator.attributes[f"delta_{isotope}"].to_ndarray()
(delta,) = particulator.attributes[f"delta_{isotope}"].to_ndarray()

# assert
n_heavy_isotope = attributes[f"moles_{isotope}"][0]
n_light_water = (
((n_heavy_isotope,),) = attributes[f"moles_{isotope}"]
(n_light_water,) = (
(1 - heavier_water_specific_content)
* attributes["water mass"]
/ (constants_defaults.M_1H * 2 + constants_defaults.M_16O)
)
print(delta, n_heavy_isotope, n_light_water)
if isotope[-1] == "O":
n_light_isotope = n_light_water
elif isotope[-1] == "H":
Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/backends/test_freezing_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ def test_freeze_singular(backend_class):
particulator.run(steps=steps)

# assert
(qi,) = particulator.products["qi"].get()
np.testing.assert_approx_equal(
actual=np.asarray(particulator.products["qi"].get()),
actual=qi,
desired=n_sd * multiplicity * water_mass / dv,
significant=7,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/dynamics/collisions/test_efficiencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_efficiency_dist(sut, backend_class=CPU, plot=False):
)
particulator.attributes.mark_updated("water mass")
sut(eff, is_first_in_pair)
res[i, j] = eff.data
(res[i, j],) = eff.data

# plot
pyplot.colorbar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def test_fragmentation_nf_and_frag_mass_equals( # TODO #987
sut(nf, frag_mass, u01, is_first_in_pair)

# Assert
np.testing.assert_approx_equal(nf.to_ndarray(), expected_nf)
np.testing.assert_array_almost_equal(nf.to_ndarray(), expected_nf)
np.testing.assert_array_almost_equal(
[expected_frag_mass], frag_mass.to_ndarray()
)
5 changes: 4 additions & 1 deletion tests/unit_tests/dynamics/test_seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def simulation(*, dynamics):
if step != 0:
particulator.run(steps=1)
for key, val in products.items():
val.append(float(particulator.products[key].get()))
value = particulator.products[key].get()
if not isinstance(value, float):
(value,) = value
val.append(float(value))
for key in products:
products[key] = np.array(products[key])
return products
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/dynamics/test_terminal_velocity.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_terminal_velocity_boundary_values(

# act
with context:
v_term = particulator.attributes["terminal velocity"].to_ndarray()
(v_term,) = particulator.attributes["terminal velocity"].to_ndarray()

# assert
np.testing.assert_approx_equal(v_term, expected_v_term)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/impl/test_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_moment_0d(backend_class):
particulator.moments(
moment_0=moment_0, moments=moments, specs={"temperature": (2,)}
)
discr_mean_T_squared = moments[0, slice(0, 1)].to_ndarray()
(discr_mean_T_squared,) = moments[0, slice(0, 1)].to_ndarray()

# Assert
assert abs(discr_zero - 1) / 1 < 1e-3
Expand Down
Loading

0 comments on commit f192545

Please sign in to comment.