gempy-project / gempy

GemPy is an open-source, Python-based 3-D structural geological modeling software, which allows the implicit (i.e. automatic) creation of complex geological models from interface and orientation data. It also offers support for stochastic modeling to address parameter and model uncertainties.
https://gempy.org
European Union Public License 1.2
986 stars 235 forks source link

Importing Gempy installed in a fresh environment gives a value error #911

Closed juanliria closed 5 months ago

juanliria commented 5 months ago

Describe the bug Installing gempy with pip in a fresh virtual environment gives the next error when importing it: Setting Backend To: AvailableBackends.numpy Traceback (most recent call last): File "c:\Users\MyEnvironment\gem_test.py", line 2, in <module> import gempy as gp File "C:\Users\MyEnvironment\.venv\Lib\site-packages\gempy\__init__.py", line 10, in <module> from .core import data File "C:\Users\MyEnvironment\.venv\Lib\site-packages\gempy\core\data\__init__.py", line 1, in <module> from .geo_model import GeoModel File "C:\Users\MyEnvironment\.venv\Lib\site-packages\gempy\core\data\geo_model.py", line 20, in <module> from .grid import Grid File "C:\Users\MyEnvironment\.venv\Lib\site-packages\gempy\core\data\grid.py", line 15, in <module> @dataclasses.dataclass ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python312\Lib\dataclasses.py", line 1268, in dataclass return wrap(cls) ^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python312\Lib\dataclasses.py", line 1258, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python312\Lib\dataclasses.py", line 994, in _process_class cls_fields.append(_get_field(cls, name, type, kw_only)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python312\Lib\dataclasses.py", line 852, in _get_field raise ValueError(f'mutable default {type(f.default)} for field ' ValueError: mutable default <class 'numpy.ndarray'> for field values is not allowed: use default_factory

My requirements.txt below: certifi==2024.6.2 charset-normalizer==3.3.2 contourpy==1.2.1 cycler==0.12.1 fonttools==4.53.0 gempy==2024.2.0 gempy_engine==2024.2.0 gempy_plugins==2024.1.0 gempy_viewer==2024.2.0 idna==3.7 imageio==2.34.1 kiwisolver==1.4.5 lazy_loader==0.4 matplotlib==3.9.0 networkx==3.3 numpy==2.0.0 packaging==24.1 pandas==2.2.2 pillow==10.3.0 platformdirs==4.2.2 pooch==1.8.2 pyparsing==3.1.2 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 pytz==2024.1 requests==2.32.3 scikit-image==0.23.2 scipy==1.13.1 six==1.16.0 tifffile==2024.5.22 tzdata==2024.1 urllib3==2.2.1

To Reproduce

  1. Create a virtual environment
  2. pip install gempy
  3. create a script that imports gempy
  4. execute script

Expected behavior import done with no error

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context not applicable

juanliria commented 5 months ago

The bug is fixed by replacing:

values: np.ndarray = dataclasses.field(default_factory=lambda: np.array([])) #np.empty((0, 3)) length: np.ndarray = dataclasses.field(default_factory=lambda: np.array([])) #np.empty(0)

in ${GEMPY_ROOT}\Lib\site-packages\gempy\core\data\grid.py

javoha commented 5 months ago

Hi, thanks for the issue (and for instantly offering a solution). I was able to reproduce the error, not sure how this was able to pass testing before it was published for the pip release. We will take a look and hopefully have it fixed soon.

Leguark commented 5 months ago

it is because Python 3.10, 3.11 and 3.12 have slightly different rules with respect mutable fields. Thanks for pointing out the exact place of issue. We will try to hotfix it this week.

In the meantime, for everybody else using Python 3.11 or 3.10 should solve the issue (or an older version of gempy)

javoha commented 5 months ago

Hi @Leguark, thanks for taking a look. Unfortunately Python 3.11 and 3.10 also throw errors on import: ValueError: mutable default <class 'numpy.ndarray'> for field values is not allowed: use default_factory

Trying around a little the only thing that worked for me was using older gempy but also older numpy version (compare #912): gempy 2024.1.4 gempy-engine 2024.1.4.2 gempy-viewer 2024.1.4 AND numpy on 1.26.4

Cheers Jan

agzimmerman commented 5 months ago

Hi @Leguark, thanks for taking a look. Unfortunately Python 3.11 and 3.10 also throw errors on import: ValueError: mutable default <class 'numpy.ndarray'> for field values is not allowed: use default_factory

Trying around a little the only thing that worked for me was using older gempy but also older numpy version (compare #912): gempy 2024.1.4 gempy-engine 2024.1.4.2 gempy-viewer 2024.1.4 AND numpy on 1.26.4

Cheers Jan

Yesterday's pip release (2024.2.0.2) should have fixed it. @javoha would you confirm?

javoha commented 5 months ago

Hi, confirmed and much appreciated. Just to make sure - we are still not using numpy 2.0.0 now, correct? Because that still throws an error.

Leguark commented 5 months ago

eh? Yesterday's patch was for numpy and scipy 2.0. That's what broke 2024.2. In a new environment you are still having problems?

javoha commented 5 months ago

Yep, sorry works with numpy 2.0.0 now in a fresh environment. I think I had some other dependency issues when just updating numpy - Perfect, thanks a lot,