Skip to content

BUG: fix count_nonzero #267

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

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion array_api_compat/cupy/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ def astype(
return out.copy() if copy and out is x else out


# cupy.count_nonzero does not have keepdims
def count_nonzero(
x: ndarray,
axis=None,
keepdims=False
) -> ndarray:
result = cp.count_nonzero(x, axis)
if keepdims:
if axis is None:
return cp.reshape(result, [1]*x.ndim)
return cp.expand_dims(result, axis)
return result


# These functions are completely new here. If the library already has them
# (i.e., numpy 2.0), use the library version instead of our wrapper.
if hasattr(cp, 'vecdot'):
Expand All @@ -146,6 +160,6 @@ def astype(
'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'bitwise_left_shift',
'bitwise_invert', 'bitwise_right_shift',
'bool', 'concat', 'pow', 'sign']
'bool', 'concat', 'count_nonzero', 'pow', 'sign']

_all_ignore = ['cp', 'get_xp']
17 changes: 16 additions & 1 deletion array_api_compat/dask/array/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ def argsort(
return restore(x)


# dask.array.count_nonzero does not have keepdims
def count_nonzero(
x: Array,
axis=None,
keepdims=False
) -> Array:
result = da.count_nonzero(x, axis)
if keepdims:
if axis is None:
return da.reshape(result, [1]*x.ndim)
return da.expand_dims(result, axis)
return result



__all__ = _aliases.__all__ + [
'__array_namespace_info__', 'asarray', 'astype', 'acos',
'acosh', 'asin', 'asinh', 'atan', 'atan2',
Expand All @@ -343,6 +358,6 @@ def argsort(
'result_type', 'bool', 'float32', 'float64', 'int8', 'int16', 'int32', 'int64',
'uint8', 'uint16', 'uint32', 'uint64',
'complex64', 'complex128', 'iinfo', 'finfo',
'can_cast', 'result_type']
'can_cast', 'count_nonzero', 'result_type']

_all_ignore = ["Callable", "array_namespace", "get_xp", "da", "np"]
15 changes: 14 additions & 1 deletion array_api_compat/numpy/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def astype(
return x.astype(dtype=dtype, copy=copy)


# count_nonzero returns a python int for axis=None and keepdims=False
# https://github.com/numpy/numpy/issues/17562
def count_nonzero(
x : ndarray,
axis=None,
keepdims=False
) -> ndarray:
result = np.count_nonzero(x, axis=axis, keepdims=keepdims)
if axis is None and not keepdims:
return np.asarray(result)
return result


# These functions are completely new here. If the library already has them
# (i.e., numpy 2.0), use the library version instead of our wrapper.
if hasattr(np, 'vecdot'):
Expand All @@ -148,6 +161,6 @@ def astype(
'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'bitwise_left_shift',
'bitwise_invert', 'bitwise_right_shift',
'bool', 'concat', 'pow']
'bool', 'concat', 'count_nonzero', 'pow']

_all_ignore = ['np', 'get_xp']
11 changes: 9 additions & 2 deletions array_api_compat/torch/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,22 @@ def diff(
return torch.diff(x, dim=axis, n=n, prepend=prepend, append=append)


# torch uses `dim` instead of `axis`
# torch uses `dim` instead of `axis`, does not have keepdims
def count_nonzero(
x: array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> array:
return torch.count_nonzero(x, dim=axis, keepdims=keepdims)
result = torch.count_nonzero(x, dim=axis)
if keepdims:
if axis is not None:
return result.unsqueeze(axis)
return _axis_none_keepdims(result, x.ndim, keepdims)
else:
return result



def where(condition: array, x1: array, x2: array, /) -> array:
Expand Down
Loading