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: Combining SciPy 1.14.1 and statsmodels 0.14.2 on OSX causes "symbol not found in flat namespace '_npy_cabs'" #9333

Closed
nickodell opened this issue Aug 22, 2024 · 17 comments

Comments

@nickodell
Copy link

nickodell commented Aug 22, 2024

Describe the bug

When importing statsmodels.api, the following error appears:

Traceback (most recent call last):
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/test.py", line 1, in <module>
    import statsmodels.api as sm
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/api.py", line 136, in <module>
    from .regression.recursive_ls import RecursiveLS
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/regression/recursive_ls.py", line 14, in <module>
    from statsmodels.tsa.statespace.mlemodel import (
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/mlemodel.py", line 33, in <module>
    from .simulation_smoother import SimulationSmoother
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/simulation_smoother.py", line 11, in <module>
    from .kalman_smoother import KalmanSmoother
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/kalman_smoother.py", line 11, in <module>
    from statsmodels.tsa.statespace.representation import OptionWrapper
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/representation.py", line 10, in <module>
    from .tools import (
  File "/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/tools.py", line 14, in <module>
    from . import (_initialization, _representation, _kalman_filter,
  File "statsmodels/tsa/statespace/_initialization.pyx", line 1, in init statsmodels.tsa.statespace._initialization
ImportError: dlopen(/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels/tsa/statespace/_representation.cpython-312-darwin.so, 0x0002): symbol not found in flat namespace '_npy_cabs'

This issue was originally reported on StackOverflow. I have independently reproduced the failure on OSX, but failed to reproduce it on Linux.

Curiously, this issue only shows up when SciPy 1.14.1 is installed. If SciPy 1.14.0 is installed, this error does not show up. Similarly, if statsmodels 0.14.1 is installed, the issue also goes away. Strangely, downgrading from statsmodels 0.14.2 to 0.14.1 and upgrading back to 0.14.2 also fixes the issue.

I'm unsure if this is a bug in statsmodels or SciPy - it seems very strange that a change in another package could cause this kind of error.

Code Sample, a copy-pastable example if possible

import statsmodels.api as sm

If you are using a released version, have you verified that the bug exists in the main branch of this repository? It helps the limited resources if we know problems exist in the current main branch so that they do not need to check whether the code sample produces a bug in the next release.

I was unable to verify whether this issue has been fixed in main, because building the package from source also makes the issue go away.

For example, if I install scipy and statsmodels like this:

pip install scipy==1.14.1 git+https://github.com/statsmodels/statsmodels@v0.14.2

the error goes away, despite being the same version that breaks when I use pre-built binaries.

Expected Output

I expected that the latest version of SciPy and statsmodels could be used together.

Output of import statsmodels.api as sm; sm.show_versions()

This is the output of import statsmodels.api as sm; sm.show_versions() for the working version of SciPy. After installing SciPy 1.14.1, import statsmodels.api as sm no longer works.

import statsmodels.api as sm; sm.show_versions()

INSTALLED VERSIONS

Python: 3.12.5.final.0
OS: Darwin 23.4.0 Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:54 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6030 arm64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

statsmodels

Installed: 0.14.2 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/statsmodels)

Required Dependencies

cython: Not installed
numpy: 2.1.0 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/numpy)
scipy: 1.14.0 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/scipy)
pandas: 2.2.2 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/pandas)
dateutil: 2.9.0.post0 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/dateutil)
patsy: 0.5.6 (/private/var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh/venv/lib/python3.12/site-packages/patsy)

Optional Dependencies

matplotlib: Not installed
cvxopt: Not installed
joblib: Not installed

Developer Tools

IPython: Not installed
jinja2: Not installed
sphinx: Not installed
pygments: Not installed
pytest: Not installed
virtualenv: Not installed

Additional details:

x-ref scipy/scipy#21434

Original report from StackOverflow

@bashtage
Copy link
Member

bashtage commented Aug 22, 2024 via email

@bashtage
Copy link
Member

bashtage commented Aug 22, 2024

Should have nothing to do with SciPy, but perhaps SciPy 1.14 is bumping NumPy to 2.1.0, while SciPy 1.13 is happy with NumPy 2.0.x.

@nickodell
Copy link
Author

What happens if you downgrade to NumPy 2.0.x?

Curiously, the configuration pip install scipy==1.14.1 statsmodels==0.14.2 numpy==2.0.1 works.

Should have nothing to do with SciPy, but perhaps SciPy 1.14 is bumping NumPy to 2.1.0

According to pip freeze, both working and non-working configurations have the same NumPy version. The only package that differs is SciPy.

Working:

numpy==2.1.0
packaging==24.1
pandas==2.2.2
patsy==0.5.6
python-dateutil==2.9.0.post0
pytz==2024.1
scipy==1.14.0
six==1.16.0
statsmodels==0.14.2
tzdata==2024.1

Failing:

numpy==2.1.0
packaging==24.1
pandas==2.2.2
patsy==0.5.6
python-dateutil==2.9.0.post0
pytz==2024.1
scipy==1.14.1
six==1.16.0
statsmodels==0.14.2
tzdata==2024.1

@nickodell
Copy link
Author

I've done a test of all combinations of SciPy 1.14.0/1.14.1, NumPy 2.0.1/2.1.0, and statsmodels 0.14.1/0.14.2.

The result is that the only failing configuration is latest SciPy plus latest NumPy plus latest statsmodels.

scipy Version statsmodels Version numpy Version Compatibility
1.14.0 0.14.1 2.0.1 incompatible (statsmodels requires numpy < 2)
1.14.0 0.14.1 2.1.0 incompatible (statsmodels requires numpy < 2)
1.14.0 0.14.1 1.26.4 works
1.14.0 0.14.2 2.0.1 works
1.14.0 0.14.2 2.1.0 works
1.14.1 0.14.1 2.0.1 incompatible (statsmodels requires numpy < 2)
1.14.1 0.14.1 2.1.0 incompatible (statsmodels requires numpy < 2)
1.14.1 0.14.1 1.26.4 works
1.14.1 0.14.2 2.0.1 works
1.14.1 0.14.2 2.1.0 fails

Everything else either works, or pip refuses to install that combination of packages.

@bashtage
Copy link
Member

bashtage commented Aug 22, 2024 via email

@nickodell
Copy link
Author

nickodell commented Aug 22, 2024

You're right. It took me a bit of googling to work out how to view the symbol tables, but NumPy 2.1.0 no longer exports this symbol, and SciPy 1.14.1 no longer exports it either.

$ pip install numpy==2.1.0                                                                                          1 ↵
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ pip install scipy==1.14.0                                                                                       130 ↵
Collecting scipy==1.14.0
  Using cached scipy-1.14.0-cp312-cp312-macosx_14_0_arm64.whl.metadata (60 kB)
Requirement already satisfied: numpy<2.3,>=1.23.5 in ./venv/lib/python3.12/site-packages (from scipy==1.14.0) (2.1.0)
Using cached scipy-1.14.0-cp312-cp312-macosx_14_0_arm64.whl (23.1 MB)
Installing collected packages: scipy
  Attempting uninstall: scipy
    Found existing installation: scipy 1.14.1
    Uninstalling scipy-1.14.1:
      Successfully uninstalled scipy-1.14.1
Successfully installed scipy-1.14.0
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ nm -gU venv/lib/python3.12/site-packages/scipy/special/_ufuncs.cpython-312-darwin.so | grep cabs     
000000000007c248 T _npy_cabs
000000000007c204 T _npy_cabsf
000000000007c28c T _npy_cabsl
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ pip install scipy==1.14.1                                                                       
Collecting scipy==1.14.1
  Using cached scipy-1.14.1-cp312-cp312-macosx_14_0_arm64.whl.metadata (60 kB)
Requirement already satisfied: numpy<2.3,>=1.23.5 in ./venv/lib/python3.12/site-packages (from scipy==1.14.1) (2.1.0)
Using cached scipy-1.14.1-cp312-cp312-macosx_14_0_arm64.whl (23.1 MB)
Installing collected packages: scipy
  Attempting uninstall: scipy
    Found existing installation: scipy 1.14.0
    Uninstalling scipy-1.14.0:
      Successfully uninstalled scipy-1.14.0
Successfully installed scipy-1.14.1
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ nm -gU venv/lib/python3.12/site-packages/scipy/special/_ufuncs.cpython-312-darwin.so | grep cabs
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ pip install numpy==2.0.1                                                                                          1 ↵
Collecting numpy==2.0.1
  Using cached numpy-2.0.1-cp312-cp312-macosx_14_0_arm64.whl.metadata (60 kB)
Using cached numpy-2.0.1-cp312-cp312-macosx_14_0_arm64.whl (5.0 MB)
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 2.1.0
    Uninstalling numpy-2.1.0:
      Successfully uninstalled numpy-2.1.0
Successfully installed numpy-2.0.1
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ nm -gU venv/lib/python3.12/site-packages/scipy/special/_ufuncs.cpython-312-darwin.so | grep cabs
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ nm -gU venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-darwin.so | grep cabs             1 ↵
00000000000148c0 T _npy_cabs
000000000001487c T _npy_cabsf
0000000000014904 T _npy_cabsl
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ pip install numpy==2.1.0                                                                             
Collecting numpy==2.1.0
  Using cached numpy-2.1.0-cp312-cp312-macosx_14_0_arm64.whl.metadata (60 kB)
Using cached numpy-2.1.0-cp312-cp312-macosx_14_0_arm64.whl (5.1 MB)
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 2.0.1
    Uninstalling numpy-2.0.1:
      Successfully uninstalled numpy-2.0.1
Successfully installed numpy-2.1.0
(venv) nick@Nicks-MacBook-Pro /var/folders/7v/pg90myg55bs218ff8dpkrfrc0000gn/T/tmp.8CjfzJEYnh 
$ nm -gU venv/lib/python3.12/site-packages/numpy/linalg/_umath_linalg.cpython-312-darwin.so | grep cabs

SciPy used to export the symbol via scipy/special/_ufuncs.cpython-312-darwin.so, and NumPy used to export it via numpy/linalg/_umath_linalg.cpython-312-darwin.so.

@bashtage
Copy link
Member

We can't reproduce on CI:

https://dev.azure.com/statsmodels/statsmodels-testing/_build/results?buildId=5869&view=logs&j=ff02c0bf-a6da-574b-cd48-e6a8a9d1f05d
Configuration:

build           1.2.1
certifi         2024.7.4
colorama        0.4.6
contourpy       1.2.1
coverage        7.6.1
cycler          0.12.1
Cython          3.0.11
execnet         2.1.1
flake8          7.1.1
fonttools       4.53.1
iniconfig       2.0.0
isort           5.13.2
joblib          1.4.2
kiwisolver      1.4.5
matplotlib      3.9.2
mccabe          0.7.0
numpy           2.1.0
packaging       24.1
pandas          2.2.2
patsy           0.5.6
pillow          10.4.0
pip             24.2
pluggy          1.5.0
pycodestyle     2.12.1
pyflakes        3.2.0
pyparsing       3.1.2
pyproject_hooks 1.1.0
pytest          7.4.4
pytest-cov      5.0.0
pytest-randomly 3.15.0
pytest-xdist    3.6.1
python-dateutil 2.9.0.post0
pytz            2024.1
scipy           1.14.1
setuptools      73.0.1
setuptools-scm  8.1.0
six             1.16.0
statsmodels     0.15.0.dev334+gde18b6fbb /Users/runner/work/1/s
tzdata          2024.1
wheel           0.44.0

I wonder if it matters that the CI one is built against scipy 1.14.1 - not 100% but could imagine that since SciPy doesn't have the symbol anymore, it is finding it from NumPy and so is happy. The release 0.14.2 was built against SciPy 1.13 and probably expects to find the symbol there. At least this is my working hypothesis.

@joegasewicz
Copy link

joegasewicz commented Aug 28, 2024

0.15.0.dev334+gde18b6fbb

I just tried running with a Pipfile -

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
jupyter = "*"
pandas = "2.2.2"
matplotlib = "*"
scipy = "1.14.1"
numpy = "2.1.0"
statsmodels = "0.14.2"

[dev-packages]

[requires]
python_version = "3.12"

Output:

ImportError: dlopen(/Users/$USER/.local/share/virtualenvs/machine_learning_course-oXhJl1U5/lib/python3.12/site-packages/statsmodels/tsa/statespace/_representation.cpython-312-darwin.so, 0x0002): symbol not found in flat namespace '_npy_cabs'

MAC:

Chip Apple M1 Pro
Memory 32G
MacOS 14.5

@EwoutH
Copy link
Contributor

EwoutH commented Sep 9, 2024

We have a similar issue in our CI, see this run for example. Numpy 2.1.1, SciPy 1.14.1 and statsmodels 0.14.2. Also happening only on macOS.

@karrmagadgeteer2
Copy link

Also on macOS. this run. Numpy 2.1.0, SciPy 1.14.1 and statsmodels 0.14.2.

@dsdanielko
Copy link

I am having the same problem while using any combinations of python==3.10.15, scipy==1.14.0/1.14.1/1.14.2, numpy==2.0.1/2.1.1 and statsmodels==0.14.2

@bashtage
Copy link
Member

There seems to be a decent bug here that isn't easy to fix. It appears in the 0.14.2 code even if built against SciPy 1.14.1.

bashtage added a commit to bashtage/statsmodels that referenced this issue Sep 12, 2024
Simplify setup since NumPy wikk always be installed when using pip
install.
Standardize library usage

xref statsmodels#9333
@bashtage
Copy link
Member

The source of the issue has been found. It is due to some changes in the arch used by the GitHub MaxOS runners, which were previously x86_64 and have moved to arm64. This means that the x86_64 wheels were not correctly linked. The fix is in place, although there are a number of bugs due to changes in other libraries (probably pandas) that need to be addressed before we can release 0.14.3.

https://github.com/MacPython/statsmodels-wheels/actions/runs/10844514094

@bashtage
Copy link
Member

Appears to be fixed now. Will need a release note and then likely to release early next week.

https://github.com/MacPython/statsmodels-wheels/actions/runs/10848683287

@bashtage
Copy link
Member

Wheels are on PyPi (excluding a couple that are slow building). Closing. Please let us know if there continues to be a problem.

@EwoutH
Copy link
Contributor

EwoutH commented Sep 16, 2024

I can confirm statsmodels 0.14.3 resolves this bug for us, and our CI now passes again!

Thanks a lot for the time and effort to crack this complex bug!

@nickodell
Copy link
Author

Hi, original reporter here, I can confirm the issue is fixed on my computer with statmodels 0.14.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants