-
Notifications
You must be signed in to change notification settings - Fork 23
implement dpnp.piecewise
#2550
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
Open
vtavana
wants to merge
7
commits into
master
Choose a base branch
from
impl-piecewise
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
implement dpnp.piecewise
#2550
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a4163e
implement dpnp.piecewise
vtavana f4dd36f
fix pre-commit and a test
vtavana 807e731
improve coverage
vtavana be68602
fix dtype for unsigned integer test
vtavana 3e5c2b9
fix formatting
vtavana fd58e42
raise error when string is passed for function
vtavana c9b00d9
implement dpnp.piecewise using dpnp.where
vtavana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
|
||
""" | ||
|
||
# pylint: disable=protected-access | ||
|
||
from dpctl.tensor._numpy_helper import ( | ||
normalize_axis_index, | ||
|
@@ -44,7 +45,10 @@ | |
|
||
import dpnp | ||
|
||
__all__ = ["apply_along_axis", "apply_over_axes"] | ||
# pylint: disable=no-name-in-module | ||
from dpnp.dpnp_utils import get_usm_allocations | ||
|
||
__all__ = ["apply_along_axis", "apply_over_axes", "piecewise"] | ||
|
||
|
||
def apply_along_axis(func1d, axis, arr, *args, **kwargs): | ||
|
@@ -266,3 +270,141 @@ def apply_over_axes(func, a, axes): | |
) | ||
a = res | ||
return res | ||
|
||
|
||
def piecewise(x, condlist, funclist): | ||
""" | ||
Evaluate a piecewise-defined function. | ||
|
||
Given a set of conditions and corresponding functions, evaluate each | ||
function on the input data wherever its condition is true. | ||
|
||
For full documentation refer to :obj:`numpy.piecewise`. | ||
|
||
Parameters | ||
---------- | ||
x : {dpnp.ndarray, usm_ndarray} | ||
The input domain. | ||
condlist : {list of array-like boolean, bool scalars} | ||
Each boolean array/scalar corresponds to a function in `funclist`. | ||
Wherever `condlist[i]` is ``True``, `funclist[i](x)` is used as the | ||
output value. | ||
|
||
Each boolean array in `condlist` selects a piece of `x`, and should | ||
therefore be of the same shape as `x`. | ||
|
||
The length of `condlist` must correspond to that of `funclist`. | ||
If one extra function is given, i.e. if | ||
``len(funclist) == len(condlist) + 1``, then that extra function | ||
is the default value, used wherever all conditions are ``False``. | ||
funclist : {array-like of scalars} | ||
A constant value is returned wherever corresponding condition of `x` | ||
is ``True``. | ||
|
||
Returns | ||
------- | ||
out : dpnp.ndarray | ||
The output is the same shape and type as `x` and is found by | ||
calling the functions in `funclist` on the appropriate portions of `x`, | ||
as defined by the boolean arrays in `condlist`. Portions not covered | ||
by any condition have a default value of ``0``. | ||
|
||
Limitations | ||
----------- | ||
Parameters `args` and `kw` are not supported and `funclist` cannot include a | ||
callable functions. | ||
|
||
See Also | ||
-------- | ||
:obj:`dpnp.choose` : Construct an array from an index array and a set of | ||
arrays to choose from. | ||
:obj:`dpnp.select` : Return an array drawn from elements in `choicelist`, | ||
depending on conditions. | ||
:obj:`dpnp.where` : Return elements from one of two arrays depending | ||
on condition. | ||
|
||
Examples | ||
-------- | ||
>>> import dpnp as np | ||
|
||
Define the signum function, which is -1 for ``x < 0`` and +1 for ``x >= 0``. | ||
|
||
>>> x = np.linspace(-2.5, 2.5, 6) | ||
>>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) | ||
array([-1., -1., -1., 1., 1., 1.]) | ||
|
||
""" | ||
dpnp.check_supported_arrays_type(x) | ||
x_dtype = x.dtype | ||
if isinstance(condlist, dpnp.ndarray) and condlist.ndim in [0, 1]: | ||
condlist = [condlist] | ||
elif dpnp.isscalar(condlist) or ( | ||
dpnp.isscalar(condlist[0]) and x.ndim != 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to raise an error like
|
||
): | ||
# convert scalar to a list of one array | ||
# convert list of scalars to a list of one array | ||
condlist = [ | ||
dpnp.full( | ||
x.shape, condlist, usm_type=x.usm_type, sycl_queue=x.sycl_queue | ||
) | ||
] | ||
elif not isinstance(condlist[0], (dpnp.ndarray)): | ||
# convert list of lists to list of arrays | ||
# convert list of scalars to a list of 0d arrays (for 0d input) | ||
tmp = [] | ||
for _, cond in enumerate(condlist): | ||
tmp.append( | ||
dpnp.array(cond, usm_type=x.usm_type, sycl_queue=x.sycl_queue) | ||
) | ||
condlist = tmp | ||
|
||
dpnp.check_supported_arrays_type(*condlist) | ||
if dpnp.is_supported_array_type(funclist): | ||
usm_type, exec_q = get_usm_allocations([x, *condlist, funclist]) | ||
else: | ||
usm_type, exec_q = get_usm_allocations([x, *condlist]) | ||
|
||
result = dpnp.empty_like(x, usm_type=usm_type, sycl_queue=exec_q) | ||
|
||
condlen = len(condlist) | ||
try: | ||
if isinstance(funclist, str): | ||
raise TypeError | ||
funclen = len(funclist) | ||
vtavana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except TypeError as e: | ||
raise TypeError("funclist must be a sequence of scalars") from e | ||
if condlen == funclen: | ||
# default value is zero | ||
default_value = x_dtype.type(0) | ||
elif condlen + 1 == funclen: | ||
# default value is the last element of funclist | ||
default_value = funclist[-1] | ||
if callable(default_value): | ||
raise NotImplementedError( | ||
"Callable functions are not supported currently" | ||
) | ||
if isinstance(default_value, dpnp.ndarray): | ||
default_value = default_value.astype(x_dtype) | ||
else: | ||
default_value = x_dtype.type(default_value) | ||
funclist = funclist[:-1] | ||
|
||
else: | ||
raise ValueError( | ||
f"with {condlen} condition(s), either {condlen} or {condlen + 1} " | ||
"functions are expected" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please handle this case as well
|
||
for condition, func in zip(condlist, funclist): | ||
if callable(func): | ||
raise NotImplementedError( | ||
"Callable functions are not supported currently" | ||
) | ||
if isinstance(func, dpnp.ndarray): | ||
func = func.astype(x_dtype) | ||
else: | ||
func = x_dtype.type(func) | ||
dpnp.where(condition, func, default_value, out=result) | ||
default_value = result | ||
|
||
return result |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no check that
condlist
is exactly a list. Do we need that or it is assuming to accept any sequence?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice,
condlist
can be a list, tuple or ndarray in both NumPy and CuPy. Anddpnp
follows the same practice.