JohannesBuchner / UltraNest

Fit and compare complex models reliably and rapidly. Advanced nested sampling.
https://johannesbuchner.github.io/UltraNest/
Other
149 stars 30 forks source link

Error upon importing ultranest #29

Closed nenasedk closed 3 years ago

nenasedk commented 3 years ago

Description

Describe what you were trying to get done.

Trying to import ultranest.

Tell us what happened, what went wrong, and what you expected to happen. There is a valueError upon import:

ValueError                                Traceback (most recent call last)
<ipython-input-2-b7f2dc2ff625> in <module>
----> 1 import ultranest
      2 #import species
      3 import numpy as np
      4 from astropy.io import fits
      5 #species.SpeciesInit()

~/anaconda3/lib/python3.7/site-packages/ultranest/__init__.py in <module>
      5 """
      6 
----> 7 from .integrator import NestedSampler, ReactiveNestedSampler, read_file
      8 from .utils import vectorize
      9 

~/anaconda3/lib/python3.7/site-packages/ultranest/integrator.py in <module>
     18 
     19 from .utils import create_logger, make_run_dir, resample_equal, vol_prefactor, vectorize, listify as _listify, is_affine_transform, normalised_kendall_tau_distance
---> 20 from ultranest.mlfriends import MLFriends, AffineLayer, ScalingLayer, find_nearby, WrappingEllipsoid
     21 from .store import HDF5PointStore, TextPointStore, NullPointStore
     22 from .viz import get_default_viz_callback, nicelogger

~/anaconda3/lib/python3.7/site-packages/ultranest/mlfriends.pyx in init ultranest.mlfriends()

ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject```

### What I Did

import ultranest



This happened from a clean conda environment, using pip install ultranest to install the package.
JohannesBuchner commented 3 years ago

Install ultranest from conda.

The command is in https://johannesbuchner.github.io/UltraNest/installation.html after "If you use conda, you can install UltraNest with:"

JohannesBuchner commented 3 years ago

The cause is that the way cython code was compiled is incompatible with your architecture, so it needs to be recompiled. You can also get the code repository, and install locally. see https://johannesbuchner.github.io/UltraNest/installation.html#from-sources To compile the cython code, python3 setup.py build_ext --inplace can be useful.

MuellerSeb commented 3 years ago

This could be solved by building the cython extensions with https://github.com/scipy/oldest-supported-numpy

See: https://github.com/JohannesBuchner/UltraNest/issues/26

tomasstolker commented 3 years ago

I ran into the same issue on a Linux machine. I tried python3 setup.py build_ext --inplace but the problem remained.

JohannesBuchner commented 3 years ago

Please report what conda install ultranest followed by python3 -c 'import ultranest' produces, in a fresh conda environment.

Please report what pip3 install ultranest followed by python3 -c 'import ultranest' produces, in a fresh conda environment.

Please report what python3 setup.py install followed by python3 -c 'import ultranest' produces, in a fresh conda environment.

nenasedk commented 3 years ago

For all tests I use a new conda env, using python 3.8 on macoS 10.14.6 I have not used the 'https://github.com/scipy/oldest-supported-numpy' as recommended above.

In a clean conda env, conda install ultranest seems to install properly, there is no printed output after python3 -c 'import ultranest'

In a clean conda env pip3 install ultranest failes to install, and instead produces a loop of the following error:

  Using cached ultranest-3.2.0.tar.gz (21.4 MB)
    ERROR: Command errored out with exit status 1:
     command: /Users/nasedkin/anaconda3/envs/untest/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/zb/1kks1nb14h7_f6zq97b6sg8xxc6_b4/T/pip-install-rnrd2lm_/ultranest_3a6b8defbf954e308b5d06b1fd5417aa/setup.py'"'"'; __file__='"'"'/private/var/folders/zb/1kks1nb14h7_f6zq97b6sg8xxc6_b4/T/pip-install-rnrd2lm_/ultranest_3a6b8defbf954e308b5d06b1fd5417aa/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/zb/1kks1nb14h7_f6zq97b6sg8xxc6_b4/T/pip-pip-egg-info-721ertg1
         cwd: /private/var/folders/zb/1kks1nb14h7_f6zq97b6sg8xxc6_b4/T/pip-install-rnrd2lm_/ultranest_3a6b8defbf954e308b5d06b1fd5417aa/
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/zb/1kks1nb14h7_f6zq97b6sg8xxc6_b4/T/pip-install-rnrd2lm_/ultranest_3a6b8defbf954e308b5d06b1fd5417aa/setup.py", line 9, in <module>
        from Cython.Build import cythonize
    ModuleNotFoundError: No module named 'Cython'

The module is not imported, but no error is printed when running python3 -c 'import ultranest' If I conda install cython in the clean env before pip3 installing ultranest, the installation is sucessful, although there is an error:

  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for ultranest

If I python3 setup.py install ultranest from its directory in a clean env, on the up-to-date master branch, it throws an error, since cython is not installed. After conda installing cython, python3 -c 'import ultranest' produces an error, since the requirements still need to be installed. After pip install -r pip-requirements.txt, python3 -c 'import ultranest' still produces an error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/nasedkin/python-packages/ultranest/ultranest/__init__.py", line 7, in <module>
    from .integrator import NestedSampler, ReactiveNestedSampler, read_file
  File "/Users/nasedkin/python-packages/ultranest/ultranest/integrator.py", line 20, in <module>
    from ultranest.mlfriends import MLFriends, AffineLayer, ScalingLayer, find_nearby, WrappingEllipsoid, RobustEllipsoidRegion
ModuleNotFoundError: No module named 'ultranest.mlfriends'

Using python3 setup.py build_ext --inplace appears to have resolved this final error.

JohannesBuchner commented 3 years ago

Thanks. In general people say it is not a good idea to mix pip and conda. I am closing this in favor of discussing solutions in #26. Patches are very welcome, if you have any time.

tomasstolker commented 3 years ago

I created a new virtualenv (using Python 3.8 on a Linux machine) and emptied the pip cache. Then I installed cython with pip and afterwards UltraNest. This gave the following error:

  ultranest/mlfriends.c:612:10: fatal error: numpy/arrayobject.h: No such file or directory
    612 | #include "numpy/arrayobject.h"
        |          ^~~~~~~~~~~~~~~~~~~~~
  compilation terminated.
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for ultranest

But it finished the installation nonetheless:

Running setup.py install for ultranest ... done

Importing does not give an error anymore! Not quite sure what solved the issue but happy that it works now :).