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
973 stars 232 forks source link

t21_gempy.ipynb TypeError unhashable type: 'numpy.ndarray' #673

Closed gui-doh closed 2 years ago

gui-doh commented 2 years ago

Hello! I was following the t21_gempy through the notebook and i got stuck

To reproduce the bug run " notebooks/gempy_intro/workshops/Transform2021/t21_gempy.ipynb "

Instead of going on it stops and report this error

When I try to run this

# Add points
geo_model.add_surface_points(X=458, Y=0.01, Z=-107, 
                                 surface='surface1', idx=1)
geo_model.add_surface_points(X=612, Y=0.01, Z=-14, 
                                 surface='surface1', idx=2)

# Plotting
p2d.plot_data(ax, cell_number=11)
p3d.plot_surface_points()

I get this

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\arrays\categorical.py:438, in Categorical.__init__(self, values, categories, ordered, dtype, fastpath, copy)
    437 try:
--> 438     codes, categories = factorize(values, sort=True)
    439 except TypeError as err:

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\algorithms.py:763, in factorize(values, sort, na_sentinel, size_hint)
    761         na_value = None
--> 763     codes, uniques = factorize_array(
    764         values, na_sentinel=na_sentinel, size_hint=size_hint, na_value=na_value
    765     )
    767 if sort and len(uniques) > 0:

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\algorithms.py:560, in factorize_array(values, na_sentinel, size_hint, na_value, mask)
    559 table = hash_klass(size_hint or len(values))
--> 560 uniques, codes = table.factorize(
    561     values, na_sentinel=na_sentinel, na_value=na_value, mask=mask
    562 )
    564 codes = ensure_platform_int(codes)

File pandas\_libs\hashtable_class_helper.pxi:5394, in pandas._libs.hashtable.PyObjectHashTable.factorize()

File pandas\_libs\hashtable_class_helper.pxi:5310, in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Input In [17], in <module>
      1 # Add points
----> 2 geo_model.add_surface_points(X=458, Y=0.01, Z=-107, 
      3                                  surface='surface1', idx=1)
      4 geo_model.add_surface_points(X=612, Y=0.01, Z=-14, 
      5                                  surface='surface1', idx=2)
      7 # Plotting

File ~\anaconda3\envs\t20-gempy\lib\site-packages\gempy\plot\decorators.py:10, in plot_add_surface_points.<locals>.pasp(*args, **kwargs)
      7 @wraps(func)
      8 def pasp(*args, **kwargs):
      9     plot_object = kwargs.pop('plot_object') if 'plot_object' in kwargs else None
---> 10     surface_points, idx = func(*args, **kwargs)
     11     if plot_object is not None:
     12         if isinstance(plot_object, GemPyToVista):

File ~\anaconda3\envs\t20-gempy\lib\site-packages\gempy\core\model.py:1114, in ImplicitCoKriging.add_surface_points(self, X, Y, Z, surface, idx, recompute_rescale_factor)
   1112 surface = np.atleast_1d(surface)
   1113 idx = self._add_valid_idx_s(idx)
-> 1114 self._surface_points.add_surface_points(X, Y, Z, surface, idx)
   1116 if recompute_rescale_factor is True or idx < 20:
   1117     # This will rescale all data again
   1118     self._rescaling.rescale_data()

File ~\anaconda3\envs\t20-gempy\lib\site-packages\gempy\core\data_modules\geometric_data.py:303, in SurfacePoints.add_surface_points(self, x, y, z, surface, idx)
    299     raise ValueError(error)
    301 self.df.loc[idx, ['smooth']] = 1e-6
--> 303 self.df['surface'] = self.df['surface'].astype('category', copy=True)
    304 self.df['surface'].cat.set_categories(self.surfaces.df['surface'].values, inplace=True)
    306 self.df['series'] = self.df['series'].astype('category', copy=True)

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\generic.py:5920, in NDFrame.astype(self, dtype, copy, errors)
   5913     results = [
   5914         self.iloc[:, i].astype(dtype, copy=copy)
   5915         for i in range(len(self.columns))
   5916     ]
   5918 else:
   5919     # else, only a single dtype is given
-> 5920     new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
   5921     return self._constructor(new_data).__finalize__(self, method="astype")
   5923 # GH 33113: handle empty frame or series

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\internals\managers.py:419, in BaseBlockManager.astype(self, dtype, copy, errors)
    418 def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
--> 419     return self.apply("astype", dtype=dtype, copy=copy, errors=errors)

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\internals\managers.py:304, in BaseBlockManager.apply(self, f, align_keys, ignore_failures, **kwargs)
    302         applied = b.apply(f, **kwargs)
    303     else:
--> 304         applied = getattr(b, f)(**kwargs)
    305 except (TypeError, NotImplementedError):
    306     if not ignore_failures:

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\internals\blocks.py:580, in Block.astype(self, dtype, copy, errors)
    562 """
    563 Coerce to the new dtype.
    564 
   (...)
    576 Block
    577 """
    578 values = self.values
--> 580 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
    582 new_values = maybe_coerce_values(new_values)
    583 newb = self.make_block(new_values)

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\dtypes\cast.py:1292, in astype_array_safe(values, dtype, copy, errors)
   1289     dtype = dtype.numpy_dtype
   1291 try:
-> 1292     new_values = astype_array(values, dtype, copy=copy)
   1293 except (ValueError, TypeError):
   1294     # e.g. astype_nansafe can fail on object-dtype of strings
   1295     #  trying to convert to float
   1296     if errors == "ignore":

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\dtypes\cast.py:1237, in astype_array(values, dtype, copy)
   1234     values = values.astype(dtype, copy=copy)
   1236 else:
-> 1237     values = astype_nansafe(values, dtype, copy=copy)
   1239 # in pandas we don't store numpy str dtypes, so convert to object
   1240 if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\dtypes\cast.py:1108, in astype_nansafe(arr, dtype, copy, skipna)
   1106 # dispatch on extension dtype if needed
   1107 if isinstance(dtype, ExtensionDtype):
-> 1108     return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
   1110 elif not isinstance(dtype, np.dtype):  # pragma: no cover
   1111     raise ValueError("dtype must be np.dtype or ExtensionDtype")

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\arrays\categorical.py:489, in Categorical._from_sequence(cls, scalars, dtype, copy)
    487 @classmethod
    488 def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
--> 489     return Categorical(scalars, dtype=dtype, copy=copy)

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\arrays\categorical.py:440, in Categorical.__init__(self, values, categories, ordered, dtype, fastpath, copy)
    438     codes, categories = factorize(values, sort=True)
    439 except TypeError as err:
--> 440     codes, categories = factorize(values, sort=False)
    441     if dtype.ordered:
    442         # raise, as we don't have a sortable data structure and so
    443         # the user should give us one by specifying categories
    444         raise TypeError(
    445             "'values' is not ordered, please "
    446             "explicitly specify the categories order "
    447             "by passing in a categories argument."
    448         ) from err

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\algorithms.py:763, in factorize(values, sort, na_sentinel, size_hint)
    760     else:
    761         na_value = None
--> 763     codes, uniques = factorize_array(
    764         values, na_sentinel=na_sentinel, size_hint=size_hint, na_value=na_value
    765     )
    767 if sort and len(uniques) > 0:
    768     uniques, codes = safe_sort(
    769         uniques, codes, na_sentinel=na_sentinel, assume_unique=True, verify=False
    770     )

File ~\anaconda3\envs\t20-gempy\lib\site-packages\pandas\core\algorithms.py:560, in factorize_array(values, na_sentinel, size_hint, na_value, mask)
    557 hash_klass, values = _get_data_algo(values)
    559 table = hash_klass(size_hint or len(values))
--> 560 uniques, codes = table.factorize(
    561     values, na_sentinel=na_sentinel, na_value=na_value, mask=mask
    562 )
    564 codes = ensure_platform_int(codes)
    565 return codes, uniques

File pandas\_libs\hashtable_class_helper.pxi:5394, in pandas._libs.hashtable.PyObjectHashTable.factorize()

File pandas\_libs\hashtable_class_helper.pxi:5310, in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'numpy.ndarray'

Im on Windows 10, I dont know Gempy version, I just installed it following this guide through Conda ( https://docs.gempy.org/installation.html#windows-installation-guide-march-2020 ) and then cloned this git https://github.com/cgre-aachen/gempy_intro.git

Thank you!

Japhiolite commented 2 years ago

Hi @gui-doh Can you provide your pandas version? There are problems with the most recent versions of pandas, and we advise to use pandas version 1.3.4 for the time being.

Best, Jan

Japhiolite commented 2 years ago

@gui-doh did downgrading Pandas fix your error?

pvabreu7 commented 2 years ago

Hi @gui-doh Can you provide your pandas version? There are problems with the most recent versions of pandas, and we advise to use pandas version 1.3.4 for the time being.

Best, Jan

Hello Japhiolite, I'm facing the same issue. My pandas version is 1.4.1, and gempy is 2.2.10.

Japhiolite commented 2 years ago

Hi @pvabreu7 there's a problem with pandas Version > 1.4 and how it parses data. For the time being, please downgrade pandas to a 1.3.X Version.

Japhiolite commented 2 years ago

see #660 for this

gui-doh commented 2 years ago

@gui-doh did downgrading Pandas fix your error?

Hello!

Sorry for the delay but I still had no chance to test a downgraded version of Pandas

I did a fresh installation on a different setup (different computer) yesterday. The Pandas version on the new setup is 1.3.x and no problems anymore

As soon as I downgrade on the first setup I will let you know the result

Japhiolite commented 2 years ago

For the time being, we decided to fix the pandas dependency at version < 1.4 for gempy, as there were more and more problems popping up. Future refactoring of gempy will most likely resolve these issues. See PR #499 for more information.