openmm / pdbfixer

PDBFixer fixes problems in PDB files
Other
443 stars 112 forks source link

[bug] Ensure numpy < 2 version #292

Closed awlange closed 1 month ago

awlange commented 1 month ago

TL;DR Small bug fix for numpy < 2 version cap.

I encountered an error when attempting to perform a local installation of pdbfixer on macOS Sonoma 14.1, Apple M3 Max with python 3.12.3.

After creating a clean virtual environment and pip install setuptools, I ran python setup.py install. The installation appears to successfully complete, however when calling the CLI executable pdbfixer, the following (abbreviated) error message is displayed:

...
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0rc2 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.
...

This error is preventing successful execution of pdbfixer.

Looking at setup.py, I see that the version for numpy is not specified, in which case I believe the installation will attempt to use the latest available version. According to the release history of numpy, the latest version is now numpy 2.0.0rc2. I believe this was the root cause of the error.

To help, I simply added a version cap in setup.py for numpy < 2. Then, re-installing succeeds, and the CLI pdbfixer command works as expected.

peastman commented 1 month ago

PDBFixer is pure Python. It isn't compiled at all, and shouldn't care which version of numpy you have.

The real issue comes from OpenMM. It has a compiled Python module, which limits binary compatibility. If you install it with conda, it will guarantee that compatible versions of packages are installed. We're currently working on support for installation with pip, but we're not quite there yet.

awlange commented 1 month ago

Righto, can use the conda install in a separate environment. I was initially attempting to avoid using conda to install in order to avoid mixing pip and conda in the environment I was using. So, that's why I went the route of trying to install. Hope to see that pip/pypi package soon!

Agreed that it looks like OpenMM being the ultimate culprit. Here is my full stack trace (at bottom), where I can see the call in OpenMM as the issue. It seems the upstream dependency on OpenMM numpy version is implicit for PDBFixer.

It's a-okay by me that this PR is not merged. I just figured out how to make this work for my case, and I thought it might be a simple quick fix that is useful for others.

/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/__init__.py:16: SyntaxWarning: invalid escape sequence '\p'
  os.environ['PATH'] = '%(lib)s;%(lib)s\plugins;%(path)s' % {
/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/openmm.py:10518: SyntaxWarning: invalid escape sequence '\S'
  match = re.search("<([^?]\S*)", inputString)
/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/internal/pdbstructure.py:1071: SyntaxWarning: invalid escape sequence '\.'
  if not re.match("pdb.%2s.\.ent\.gz" % subdir , pdb_file):
/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/internal/pdbstructure.py:1082: SyntaxWarning: invalid escape sequence '\.'
  if not re.match("pdb.%2s.\.ent\.gz" % subdir , pdb_file):

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0rc2 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/bin/pdbfixer", line 33, in <module>
    sys.exit(load_entry_point('pdbfixer==1.9.0', 'console_scripts', 'pdbfixer')())
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/bin/pdbfixer", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/metadata/__init__.py", line 205, in load
    module = import_module(match.group('module'))
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/pdbfixer-1.9.0-py3.12.egg/pdbfixer/__init__.py", line 2, in <module>
    from .pdbfixer import PDBFixer
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/pdbfixer-1.9.0-py3.12.egg/pdbfixer/pdbfixer.py", line 36, in <module>
    import openmm.app as app
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/__init__.py", line 17, in <module>
    from .xtcfile import XTCFile
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/xtcfile.py", line 6, in <module>
    from openmm.app.internal.xtc_utils import (
Traceback (most recent call last):
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/bin/pdbfixer", line 33, in <module>
    sys.exit(load_entry_point('pdbfixer==1.9.0', 'console_scripts', 'pdbfixer')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/bin/pdbfixer", line 25, in importlib_load_entry_point
    return next(matches).load()
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/metadata/__init__.py", line 205, in load
    module = import_module(match.group('module'))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/pdbfixer-1.9.0-py3.12.egg/pdbfixer/__init__.py", line 2, in <module>
    from .pdbfixer import PDBFixer
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/pdbfixer-1.9.0-py3.12.egg/pdbfixer/pdbfixer.py", line 36, in <module>
    import openmm.app as app
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/__init__.py", line 17, in <module>
    from .xtcfile import XTCFile
  File "/Users/adrianlange/programming/copy2/pdbfixer/venv/lib/python3.12/site-packages/OpenMM-8.1.1-py3.12-macosx-14.0-arm64.egg/openmm/app/xtcfile.py", line 6, in <module>
    from openmm.app.internal.xtc_utils import (
  File "openmm/app/internal/xtc_utils/xtc.pyx", line 1, in init openmm.app.internal.xtc_utils
ImportError: numpy.core.multiarray failed to import (auto-generated because you didn't call 'numpy.import_array()' after cimporting numpy; use '<void>numpy._import_array' to disable if you are certain you don't need it).
peastman commented 1 month ago

Yeah, mixing pip and conda can be awkward. We'll hopefully have a better solution for it soon.