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

Some functions seem missing from the top level import #622

Closed
aulemahal opened this issue Jan 9, 2024 · 2 comments · Fixed by #623
Closed

Some functions seem missing from the top level import #622

aulemahal opened this issue Jan 9, 2024 · 2 comments · Fixed by #623
Labels

Comments

@aulemahal
Copy link

Describe the bug
It seems a few functions needed for the Array API are missing from the top level.

To Reproduce
Using xarray:

import numpy as np
import xarray as xr
from sparse import GCXS

x = np.random.negative_binomial(1, 0.5, size=(100, 100))
array = xr.DataArray(GCXS.from_numpy(x))
array.mean()

Fails with AttributeError: module 'sparse' has no attribute 'mean' on my machine.

I also had the problem with isnan on a float array.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 7
      5 x = np.random.negative_binomial(1, 0.5, size=(100, 100))
      6 array = xr.DataArray(GCXS.from_numpy(x))
----> 7 array.mean()

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/core/_aggregations.py:1663, in DataArrayAggregations.mean(self, dim, skipna, keep_attrs, **kwargs)
   1588 def mean(
   1589     self,
   1590     dim: Dims = None,
   (...)
   1594     **kwargs: Any,
   1595 ) -> Self:
   1596     """
   1597     Reduce this DataArray's data by applying ``mean`` along some dimension(s).
   1598 
   (...)
   1661     array(nan)
   1662     """
-> 1663     return self.reduce(
   1664         duck_array_ops.mean,
   1665         dim=dim,
   1666         skipna=skipna,
   1667         keep_attrs=keep_attrs,
   1668         **kwargs,
   1669     )

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/core/dataarray.py:3776, in DataArray.reduce(self, func, dim, axis, keep_attrs, keepdims, **kwargs)
   3732 def reduce(
   3733     self,
   3734     func: Callable[..., Any],
   (...)
   3740     **kwargs: Any,
   3741 ) -> Self:
   3742     """Reduce this array by applying `func` along some dimension(s).
   3743 
   3744     Parameters
   (...)
   3773         summarized data and the indicated dimension(s) removed.
   3774     """
-> 3776     var = self.variable.reduce(func, dim, axis, keep_attrs, keepdims, **kwargs)
   3777     return self._replace_maybe_drop_dims(var)

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/core/variable.py:1756, in Variable.reduce(self, func, dim, axis, keep_attrs, keepdims, **kwargs)
   1749 keep_attrs_ = (
   1750     _get_keep_attrs(default=False) if keep_attrs is None else keep_attrs
   1751 )
   1753 # Noe that the call order for Variable.mean is
   1754 #    Variable.mean -> NamedArray.mean -> Variable.reduce
   1755 #    -> NamedArray.reduce
-> 1756 result = super().reduce(
   1757     func=func, dim=dim, axis=axis, keepdims=keepdims, **kwargs
   1758 )
   1760 # return Variable always to support IndexVariable
   1761 return Variable(
   1762     result.dims, result._data, attrs=result._attrs if keep_attrs_ else None
   1763 )

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/namedarray/core.py:772, in NamedArray.reduce(self, func, dim, axis, keepdims, **kwargs)
    770         data = func(self.data, axis=axis, **kwargs)
    771     else:
--> 772         data = func(self.data, **kwargs)
    774 if getattr(data, "shape", ()) == self.shape:
    775     dims = self.dims

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/core/duck_array_ops.py:637, in mean(array, axis, skipna, **kwargs)
    635     return _to_pytimedelta(mean_timedeltas, unit="us") + offset
    636 else:
--> 637     return _mean(array, axis=axis, skipna=skipna, **kwargs)

File ~/mambaforge/envs/xesmf/lib/python3.11/site-packages/xarray/core/duck_array_ops.py:399, in _create_nan_agg_method.<locals>.f(values, axis, skipna, **kwargs)
    396         kwargs.pop("min_count", None)
    398     xp = get_array_namespace(values)
--> 399     func = getattr(xp, name)
    401 try:
    402     with warnings.catch_warnings():

AttributeError: module 'sparse' has no attribute 'mean'

Expected behavior
A mean to be computed.

System

  • OS and version: Linux, Fedora 39
  • sparse version : 0.15
  • NumPy version : 1.26.3
  • Numba version : 0.58.1

Additional context
This is an expanded copy of pydata/xarray#8602. I don't know much about the Array API, I am basing this issue on a comment on that other thread.

I only tested a few cases and had those problems:

  • integer array and "mean"
  • float array and "isnan"

integer arr

@hameerabbasi
Copy link
Collaborator

Thanks for the bug report, @aulemahal! This is indeed due to missing top level functions. I'll try to come up with a more complete list and make a PR.

@hameerabbasi
Copy link
Collaborator

PR is up at #623 if you'd like to test with your use-case.

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

Successfully merging a pull request may close this issue.

2 participants