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

[Bug]: Conversion to scalars for arrays with ndim>0: NumPy 1.25 deprecation (Python 3.9–3.11) #3052

Closed
agriyakhetarpal opened this issue Jun 20, 2023 · 1 comment · Fixed by #3055
Labels
bug Something isn't working

Comments

@agriyakhetarpal
Copy link
Member

agriyakhetarpal commented Jun 20, 2023

PyBaMM Version

23.5

Python Version

3.11

Describe the bug

From NumPy 1.25 Release Notes – Deprecations

Only ndim-0 arrays are treated as scalars. NumPy used to treat all arrays of size 1 (e.g., np.array([3.14])) as scalars. In the future, this will be limited to arrays of ndim 0 (e.g., np.array(3.14)). The following expressions will report a deprecation warning:

a = np.array([3.14])
float(a)  # better: a[0] to get the numpy.float or a.item()

b = np.array([[3.14]])
c = numpy.random.rand(10)
c[0] = b  # better: c[0] = b[0, 0]

in numpy/numpy#10404 and numpy/numpy#10615

We convert arrays to scalars with float() in six locations, which bring up warnings as shown below:

Steps to Reproduce

Run the test suite

tox -e unit

Relevant log output

/Users/agriyakhetarpal/Desktop/PyBaMM/pybamm/models/full_battery_models/lithium_ion/electrode_soh.py:428: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

PyBaMM/pybamm/models/full_battery_models/lithium_ion/electrode_soh.py:625: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

test_error (test_models.test_full_battery_models.test_lithium_ion.test_electrode_soh.TestElectrodeSOH.test_error) ... /Users/agriyakhetarpal/Desktop/PyBaMM/pybamm/models/full_battery_models/lithium_ion/electrode_soh.py:425: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
  V_lower_bound = float(

test_parameter_values_with_model (test_parameters.test_parameter_sets.test_parameters_with_default_models.TestParameterValuesWithModel.test_parameter_values_with_model) ... /Users/agriyakhetarpal/Desktop/PyBaMM/pybamm/parameters/parameter_values.py:650: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

test_convergence_without_bcs (test_spatial_methods.test_finite_volume.test_extrapolation.TestExtrapolation.test_convergence_without_bcs) ... /Users/agriyakhetarpal/Desktop/PyBaMM/tests/unit/test_spatial_methods/test_finite_volume/test_extrapolation.py:54: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

test_model_solver_events (test_solvers.test_casadi_solver.TestCasadiSolver.test_model_solver_events) ... /Users/agriyakhetarpal/Desktop/PyBaMM/pybamm/solvers/base_solver.py:505: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
@agriyakhetarpal agriyakhetarpal added the bug Something isn't working label Jun 20, 2023
@agriyakhetarpal
Copy link
Member Author

agriyakhetarpal commented Jun 20, 2023

I do think I am filing this issue a bit early since we still support Python 3.8 (equivalent to NumPy 1.24.X), while these warnings only occur on Python 3.9–3.11 (NumPy 1.25)

Nonetheless, I managed to fix this (partially), I shall open a PR soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant