Skip to content

Commit 4a23239

Browse files
antonwolfyvlad-perevezentsevvtavana
authored
Leverage on dpctl implementation of shape.setter (#1975)
* 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 <[email protected]> * Corrected a link in docstring * Updated wring indention --------- Co-authored-by: vlad-perevezentsev <[email protected]> Co-authored-by: vtavana <[email protected]>
1 parent 6ae16ce commit 4a23239

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

dpnp/dpnp_array.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,12 +1284,45 @@ def searchsorted(self, v, side="left", sorter=None):
12841284
@property
12851285
def shape(self):
12861286
"""
1287-
Lengths of axes. A tuple of numbers represents size of each dimension.
1287+
Tuple of array dimensions.
12881288
1289-
Setter of this property involves reshaping without copy. If the array
1290-
cannot be reshaped without copy, it raises an exception.
1289+
The shape property is usually used to get the current shape of an array,
1290+
but may also be used to reshape the array in-place by assigning a tuple
1291+
of array dimensions to it. Unlike :obj:`dpnp.reshape`, only non-negative
1292+
values are supported to be set as new shape. Reshaping an array in-place
1293+
will fail if a copy is required.
12911294
1292-
.. seealso: :attr:`numpy.ndarray.shape`
1295+
For full documentation refer to :obj:`numpy.ndarray.shape`.
1296+
1297+
Note
1298+
----
1299+
Using :obj:`dpnp.ndarray.reshape` or :obj:`dpnp.reshape` is the
1300+
preferred approach to set new shape of an array.
1301+
1302+
See Also
1303+
--------
1304+
:obj:`dpnp.shape` : Equivalent getter function.
1305+
:obj:`dpnp.reshape` : Function similar to setting `shape`.
1306+
:obj:`dpnp.ndarray.reshape` : Method similar to setting `shape`.
1307+
1308+
Examples
1309+
--------
1310+
>>> import dpnp as np
1311+
>>> x = np.array([1, 2, 3, 4])
1312+
>>> x.shape
1313+
(4,)
1314+
>>> y = np.zeros((2, 3, 4))
1315+
>>> y.shape
1316+
(2, 3, 4)
1317+
1318+
>>> y.shape = (3, 8)
1319+
>>> y
1320+
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
1321+
[ 0., 0., 0., 0., 0., 0., 0., 0.],
1322+
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
1323+
>>> y.shape = (3, 6)
1324+
...
1325+
TypeError: Can not reshape array of size 24 into (3, 6)
12931326
12941327
"""
12951328

@@ -1300,16 +1333,23 @@ def shape(self, newshape):
13001333
"""
13011334
Set new lengths of axes.
13021335
1303-
A tuple of numbers represents size of each dimension.
1304-
It involves reshaping without copy. If the array cannot be reshaped without copy,
1305-
it raises an exception.
1336+
Modifies array instance in-place by changing its metadata about the
1337+
shape and the strides of the array, or raises `AttributeError`
1338+
exception if in-place change is not possible.
13061339
1307-
.. seealso: :attr:`numpy.ndarray.shape`
1340+
Whether the array can be reshape in-place depends on its strides. Use
1341+
:obj:`dpnp.reshape` function which always succeeds to reshape the array
1342+
by performing a copy if necessary.
13081343
1309-
"""
1344+
For full documentation refer to :obj:`numpy.ndarray.shape`.
13101345
1311-
if not isinstance(newshape, (list, tuple)):
1312-
newshape = (newshape,)
1346+
Parameters
1347+
----------
1348+
newshape : {tuple, int}
1349+
New shape. Only non-negative values are supported. The new shape
1350+
may not lead to the change in the number of elements in the array.
1351+
1352+
"""
13131353

13141354
self._array_obj.shape = newshape
13151355

dpnp/dpnp_iface_nanfunctions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ def nanmean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
551551
raise TypeError("If input is inexact, then out must be inexact.")
552552

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

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

0 commit comments

Comments
 (0)