Skip to content

Commit

Permalink
Merge pull request #339 from hameerabbasi/clip-fix
Browse files Browse the repository at this point in the history
Fix signature for clip.
  • Loading branch information
hameerabbasi authored Apr 22, 2020
2 parents f6b3465 + 66997fb commit a9a6de2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
6 changes: 6 additions & 0 deletions docs/generated/sparse.clip.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
clip
====

.. currentmodule:: sparse

.. autofunction:: clip
2 changes: 2 additions & 0 deletions docs/generated/sparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ API

concatenate

clip

diagonal

diagonalize
Expand Down
1 change: 1 addition & 0 deletions sparse/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ._utils import check_compressed_axes
from ._coo import (
clip,
tensordot,
dot,
matmul,
Expand Down
2 changes: 2 additions & 0 deletions sparse/_coo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
dot,
matmul,
concatenate,
clip,
stack,
triu,
tril,
Expand Down Expand Up @@ -33,6 +34,7 @@
"dot",
"matmul",
"concatenate",
"clip",
"stack",
"triu",
"tril",
Expand Down
46 changes: 46 additions & 0 deletions sparse/_coo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,49 @@ def _diagonal_idx(coordlist, axis1, axis2, offset):
if coordlist[axis1][i] + offset == coordlist[axis2][i]
]
)


def clip(a, a_min=None, a_max=None, out=None):
"""
Clip (limit) the values in the array.
Return an array whose values are limited to ``[min, max]``. One of min
or max must be given.
Parameters
----------
a:
a_min : scalar or `SparseArray` or `None`
Minimum value. If `None`, clipping is not performed on lower
interval edge.
a_max : scalar or `SparseArray` or `None`
Maximum value. If `None`, clipping is not performed on upper
interval edge.
out : SparseArray, optional
If provided, the results will be placed in this array. It may be
the input array for in-place clipping. `out` must be of the right
shape to hold the output. Its type is preserved.
Returns
-------
clipped_array : SparseArray
An array with the elements of `self`, but where values < `min` are
replaced with `min`, and those > `max` with `max`.
Examples
--------
>>> import sparse
>>> x = sparse.COO.from_numpy([0, 0, 0, 1, 2, 3])
>>> sparse.clip(x, a_min=1).todense() # doctest: +NORMALIZE_WHITESPACE
array([1, 1, 1, 1, 2, 3])
>>> sparse.clip(x, a_max=1).todense() # doctest: +NORMALIZE_WHITESPACE
array([0, 0, 0, 1, 1, 1])
>>> sparse.clip(x, a_min=1, a_max=2).todense() # doctest: +NORMALIZE_WHITESPACE
array([1, 1, 1, 1, 2, 2])
See also
--------
numpy.clip : Equivalent NumPy function
"""
a = asCOO(a, name="clip")
return a.clip(a_min, a_max)
33 changes: 5 additions & 28 deletions sparse/_coo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,39 +2133,16 @@ def round(self, decimals=0, out=None):
round_ = round

def clip(self, min=None, max=None, out=None):
"""Clip (limit) the values in the array.
"""
Clip (limit) the values in the array.
Return an array whose values are limited to ``[min, max]``. One of min
or max must be given.
Parameters
----------
min : scalar or array_like or `None`
Minimum value. If `None`, clipping is not performed on lower
interval edge.
max : scalar or array_like or `None`
Maximum value. If `None`, clipping is not performed on upper
interval edge.
out : COO, optional
If provided, the results will be placed in this array. It may be
the input array for in-place clipping. `out` must be of the right
shape to hold the output. Its type is preserved.
Returns
-------
clipped_array : COO
An array with the elements of `self`, but where values < `min` are
replaced with `min`, and those > `max` with `max`.
Examples
See Also
--------
>>> x = COO.from_numpy([0, 0, 0, 1, 2, 3])
>>> x.clip(min=1).todense() # doctest: +NORMALIZE_WHITESPACE
array([1, 1, 1, 1, 2, 3])
>>> x.clip(max=1).todense() # doctest: +NORMALIZE_WHITESPACE
array([0, 0, 0, 1, 1, 1])
>>> x.clip(min=1, max=2).todense() # doctest: +NORMALIZE_WHITESPACE
array([1, 1, 1, 1, 2, 2])
sparse.clip : For full documentation and more details.
numpy.clip : Equivalent NumPy function.
"""
if min is None and max is None:
raise ValueError("One of max or min must be given.")
Expand Down

0 comments on commit a9a6de2

Please sign in to comment.