bjodah / chempy

⚗ A package useful for chemistry written in Python
BSD 2-Clause "Simplified" License
536 stars 79 forks source link

New numpy error on import #234

Open hepcat72 opened 1 month ago

hepcat72 commented 1 month ago

I just ran into a chempy-related import error when I attempted to add a django migration. The model being migrated has a chempy Substance import and the migrations check on github raises the following exception:

  File "/home/runner/work/tracebase/tracebase/DataRepo/models/utilities.py", line 5, in <module>
    from chempy import Substance
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/chempy/__init__.py", line 9, in <module>
    from .chemistry import (
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/chempy/chemistry.py", line 22, in <module>
    from .units import default_units, is_quantity, unit_of, to_unitless
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/chempy/units.py", line 42, in <module>
    _patch_quantities(pq)
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/chempy/util/_quantities.py", line 65, in _patch_quantities
    if (-a).uncertainty[0] != a.uncertainty[0]:
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/quantities/uncertainquantity.py", line 132, in __neg__
    return self*-1
  File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/site-packages/quantities/uncertainquantity.py", line 121, in __mul__
    other = np.array(other, copy=False, subok=True)
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

I tried updating to chempy 0.9.0 (from 0.8.3), but it still produces the same exception.

bjodah commented 1 month ago

Thank you for reporting this, I can reproduce the error locally. It looks like the problem might be with https://github.com/python-quantities/python-quantities. I'll see if I can create a reproducer and report upstream.

hepcat72 commented 1 month ago

I just downgraded numpy to 1.26.4 (last stable version before 2.x), which fixes our current issue. There are migration notes for numpy 2.x.

hepcat72 commented 1 month ago

Looks like the numpy 2.0.0 release was yesterday.

bjodah commented 1 month ago

Yes, I guess that the quantities package isn't as popular as it once was and is now lagging behind. Hopefully the fix will be small.

bjodah commented 1 month ago

I opened a PR over at https://github.com/python-quantities/python-quantities/pull/232

slayoo commented 1 month ago

@hepcat72 @bjodah, thanks! (our CI in a project depending on chempy also stopped working yesterday)