Skip to content

Commit

Permalink
Leverage on dpctl implementation of shape.setter (#1975)
Browse files Browse the repository at this point in the history
* Leverage on dpctl shape.setter for any input

* Mute false-positive pylint issue

* Remove disable=invalid-unary-operand-type since resolved with pylint 3.2.6

* Implement `dpnp.nan_to_num()` (#1966)

* Implement dpnp.nan_to_num()

* Update cupy tests for nan_to_num()

* Add dpnp tests

* Skip test_nan_to_num_scalar_nan

* Applied review comments

* Add more tests for nan_to_num()

* Improve perfomance using out empty_like array

* Add checks for nan, posinf, neginf args

* Add type check for nan, posinf and neginf

* Update tests

* Add support boolean type

* Apply suggestions from code review

Co-authored-by: vtavana <120411540+vtavana@users.noreply.github.com>

* Corrected a link in docstring

* Updated wring indention

---------

Co-authored-by: vlad-perevezentsev <vladislav.perevezentsev@intel.com>
Co-authored-by: vtavana <120411540+vtavana@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 6ae16ce commit 4a23239
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
62 changes: 51 additions & 11 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,12 +1284,45 @@ def searchsorted(self, v, side="left", sorter=None):
@property
def shape(self):
"""
Lengths of axes. A tuple of numbers represents size of each dimension.
Tuple of array dimensions.
Setter of this property involves reshaping without copy. If the array
cannot be reshaped without copy, it raises an exception.
The shape property is usually used to get the current shape of an array,
but may also be used to reshape the array in-place by assigning a tuple
of array dimensions to it. Unlike :obj:`dpnp.reshape`, only non-negative
values are supported to be set as new shape. Reshaping an array in-place
will fail if a copy is required.
.. seealso: :attr:`numpy.ndarray.shape`
For full documentation refer to :obj:`numpy.ndarray.shape`.
Note
----
Using :obj:`dpnp.ndarray.reshape` or :obj:`dpnp.reshape` is the
preferred approach to set new shape of an array.
See Also
--------
:obj:`dpnp.shape` : Equivalent getter function.
:obj:`dpnp.reshape` : Function similar to setting `shape`.
:obj:`dpnp.ndarray.reshape` : Method similar to setting `shape`.
Examples
--------
>>> import dpnp as np
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> y.shape = (3, 6)
...
TypeError: Can not reshape array of size 24 into (3, 6)
"""

Expand All @@ -1300,16 +1333,23 @@ def shape(self, newshape):
"""
Set new lengths of axes.
A tuple of numbers represents size of each dimension.
It involves reshaping without copy. If the array cannot be reshaped without copy,
it raises an exception.
Modifies array instance in-place by changing its metadata about the
shape and the strides of the array, or raises `AttributeError`
exception if in-place change is not possible.
.. seealso: :attr:`numpy.ndarray.shape`
Whether the array can be reshape in-place depends on its strides. Use
:obj:`dpnp.reshape` function which always succeeds to reshape the array
by performing a copy if necessary.
"""
For full documentation refer to :obj:`numpy.ndarray.shape`.
if not isinstance(newshape, (list, tuple)):
newshape = (newshape,)
Parameters
----------
newshape : {tuple, int}
New shape. Only non-negative values are supported. The new shape
may not lead to the change in the number of elements in the array.
"""

self._array_obj.shape = newshape

Expand Down
2 changes: 0 additions & 2 deletions dpnp/dpnp_iface_nanfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ def nanmean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
raise TypeError("If input is inexact, then out must be inexact.")

cnt_dtype = a.real.dtype if dtype is None else dtype
# pylint: disable=invalid-unary-operand-type
cnt = dpnp.sum(
~mask, axis=axis, dtype=cnt_dtype, keepdims=keepdims, where=where
)
Expand Down Expand Up @@ -1062,7 +1061,6 @@ def nanvar(

# Compute mean
var_dtype = a.real.dtype if dtype is None else dtype
# pylint: disable=invalid-unary-operand-type
cnt = dpnp.sum(
~mask, axis=axis, dtype=var_dtype, keepdims=True, where=where
)
Expand Down

0 comments on commit 4a23239

Please sign in to comment.