8000 addressed all the reviews · numpy/numpy@9260d40 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9260d40

Browse files
committed
addressed all the reviews
1 parent 568251e commit 9260d40

File tree

9 files changed

+122
-148
lines changed

9 files changed

+122
-148
lines changed
Lines changed: 3 additions & 3 deletions

numpy/__init__.pyi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
`keepdims` optional argument added to `numpy.argmin`, `numpy.argmax`
1+
``keepdims`` optional argument added to `numpy.argmin`, `numpy.argmax`
22
--------------------------------------------------------------------
33

4-
`keepdims` argument has been added to `numpy.argmin`, `numpy.argmax`.
4+
``keepdims`` argument has been added to `numpy.argmin`, `numpy.argmax`.
55
By default, it is `False` and hence no behaviour change should be expected
6-
in existing codes using `numpy.argmin`, `numpy.argmax`. If set to `True`,
6+
in existing codes using `numpy.argmin`, `numpy.argmax`. If set to ``True``,
77
the axes which are reduced are left in the result as dimensions with size one.
88
In simple words, the resulting array will be having same dimensions
99
as the input array.
Original file line numberDiff line numberDiff line change
@@ -1299,43 +1299,37 @@ class _ArrayOrScalarCommon:
12991299
self,
13001300
axis: None = ...,
13011301
out: None = ...,
1302-
keepdims: L[False] = ...,
13031302
) -> intp: ...
13041303
@overload
13051304
def argmax(
13061305
self,
13071306
axis: _ShapeLike = ...,
13081307
out: None = ...,
1309-
keepdims: bool = ...,
13101308
) -> Any: ...
13111309
@overload
13121310
def argmax(
13131311
self,
13141312
axis: Optional[_ShapeLike] = ...,
13151313
out: _NdArraySubClass = ...,
1316-
keepdims: bool = ...,
13171314
) -> _NdArraySubClass: ...
13181315

13191316
@overload
13201317
def argmin(
13211318
self,
13221319
axis: None = ...,
13231320
out: None = ...,
1324-
keepdims: L[False] = ...,
13251321
) -> intp: ...
13261322
@overload
13271323
def argmin(
13281324
self,
13291325
axis: _ShapeLike = ...,
13301326
out: None = ...,
1331-
keepdims: bool = ...,
13321327
) -> Any: ...
13331328
@overload
13341329
def argmin(
13351330
self,
13361331
axis: Optional[_ShapeLike] = ...,
13371332
out: _NdArraySubClass = ...,
1338-
keepdims: bool = ...,
13391333
) -> _NdArraySubClass: ...
13401334

13411335
def argsort(

numpy/core/fromnumeric.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ def _wrapit(obj, method, *args, **kwds):
4949

5050

5151
def _wrapfunc(obj, method, *args, **kwds):
52+
passkwargs = {k: v for k, v in kwds.items()
53+
if v is not np._NoValue}
5254
bound = getattr(obj, method, None)
5355
if bound is None:
54-
return _wrapit(obj, method, *args, **kwds)
56+
return _wrapit(obj, method, *args, **passkwargs)
5557

5658
try:
57-
return bound(*args, **kwds)
59+
return bound(*args, **passkwargs)
5860
except TypeError:
5961
# A TypeError occurs if the object does have such a method in its
6062
# class, but its signature is not identical to that of NumPy's. This
@@ -63,7 +65,7 @@ def _wrapfunc(obj, method, *args, **kwds):
6365
#
6466
# Call _wrapit from within the except clause to ensure a potential
6567
# exception has a traceback chain.
66-
return _wrapit(obj, method, *args, **kwds)
68+
return _wrapit(obj, method, *args, **passkwargs)
6769

6870

6971
def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):
@@ -1114,12 +1116,12 @@ def argsort(a, axis=-1, kind=None, order=None):
11141116
return _wrapfunc(a, 'argsort', axis=axis, kind=kind, order=order)
11151117

11161118

1117-
def _argmax_dispatcher(a, axis=None, out=None, keepdims=None):
1118-
return (a, out, keepdims)
1119+
def _argmax_dispatcher(a, axis=None, out=None, *, keepdims=np._NoValue):
1120+
return (a, out)
11191121

11201122

11211123
@array_function_dispatch(_argmax_dispatcher)
1122-
def argmax(a, axis=None, out=None, keepdims=False):
1124+
def argmax(a, axis=None, out=None, *, keepdims=np._NoValue):
11231125
"""
11241126
Returns the indices of the maximum values along an axis.
11251127
@@ -1134,9 +1136,9 @@ def argmax(a, axis=None, out=None, keepdims=False):
11341136
If provided, the result will be inserted into this array. It should
11351137
be of the appropriate shape and dtype.
11361138
keepdims : bool, optional
1137-
If this is set to True, the axes which are reduced are left
1138-
in the result as dimensions with size one. With this option,
1139-
the result will broadcast correctly against the array.
1139+
If this is set to True, the axes which are reduced are left
1140+
in the result as dimensions with size one. With this option,
1141+
the result will broadcast correctly against the array.
11401142
11411143
Returns
11421144
-------
@@ -1204,18 +1206,16 @@ def argmax(a, axis=None, out=None, keepdims=False):
12041206
>>> res.shape
12051207
(2, 1, 4)
12061208
"""
1207-
if isinstance(a, np.matrix):
1208-
return _wrapfunc(a, 'argmax', axis=axis, out=out)
12091209
return _wrapfunc(a, 'argmax', axis=axis, out=out,
12101210
keepdims=keepdims)
12111211

12121212

1213-
def _argmin_dispatcher(a, axis=None, out=None, keepdims=None):
1214-
return (a, out, keepdims)
1213+
def _argmin_dispatcher(a, axis=None, out=None, *, keepdims=np._NoValue):
1214+
return (a, out)
12151215

12161216

12171217
@array_function_dispatch(_argmin_dispatcher)
1218-
def argmin(a, axis=None, out=None, keepdims=False):
1218+
def argmin(a, axis=None, out=None, *, keepdims=np._NoValue):
12191219
"""
12201220
Returns the indices of the minimum values along an axis.
12211221
@@ -1300,9 +1300,7 @@ def argmin(a, axis=None, out=None, keepdims=False):
13001300
>>> res.shape
13011301
(2, 1, 4)
13021302
"""
1303-
if isinstance(a, np.matrix):
1304-
return _wrapfunc(a, 'argmin', axis=axis, out=out)
1305-
return _wrapfunc(a, 'argmin', axis=axis, out=out, keepdims=keepdims)
1303+
return _wrapfunc(a, 'argmin', axis, out=out, keepdims=keepdims)
13061304

13071305

13081306
def _searchsorted_dispatcher(a, v, side=None, sorter=None):

numpy/core/fromnumeric.pyi

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,25 @@ def argmax(
130130
a: ArrayLike,
131131
axis: None = ...,
132132
out: Optional[ndarray] = ...,
133-
keepdims: Literal[False] = ...,
134133
) -> intp: ...
135134
@overload
136135
def argmax(
137136
a: ArrayLike,
138137
axis: Optional[int] = ...,
139138
out: Optional[ndarray] = ...,
140-
keepdims: bool = ...,
141139
) -> Any: ...
142140

143141
@overload
144142
def argmin(
145143
a: ArrayLike,
146144
axis: None = ...,
147145
out: Optional[ndarray] = ...,
148-
keepdims: Literal[False] = ...,
149146
) -> intp: ...
150147
@overload
151148
def argmin(
152149
a: ArrayLike,
153150
axis: Optional[int] = ...,
154151
out: Optional[ndarray] = ...,
155-
keepdims: bool = ...,
156152
) -> Any: ...
157153

158154
@overload

numpy/core/src/multiarray/calculation.c

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ power_of_ten(int n)
3838
* ArgMax
3939
*/
4040
NPY_NO_EXPORT PyObject *
41-
PyArray_ArgMaxWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int keepdims)
41+
PyArray_ArgMaxWithKeepdims(PyArrayObject *op,
42+
int axis, PyArrayObject *out, int keepdims)
4243
{
4344
PyArrayObject *ap = NULL, *rp = NULL;
4445
PyArray_ArgFunc* arg_func;
@@ -59,23 +60,22 @@ PyArray_ArgMaxWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int
5960
if ((ap = (PyArrayObject *)PyArray_CheckAxis(op, &axis, 0)) == NULL) {
6061
return NULL;
6162
}
62-
6363
/*
6464
* We need to permute the array so that axis is placed at the end.
6565
* And all other dimensions are shifted left.
6666
*/
6767
if (axis != PyArray_NDIM(ap)-1) {
6868
PyArray_Dims newaxes;
6969
npy_intp dims[NPY_MAXDIMS];
70-
int i;
70+
int j;
7171

7272
newaxes.ptr = dims;
7373
newaxes.len F438 = PyArray_NDIM(ap);
74-
for (i = 0; i < axis; i++) {
75-
dims[i] = i;
74+
for (j = 0; j < axis; j++) {
75+
dims[j] = j;
7676
}
77-
for (i = axis; i < PyArray_NDIM(ap) - 1; i++) {
78-
dims[i] = i + 1;
77+
for (j = axis; j < PyArray_NDIM(ap) - 1; j++) {
78+
dims[j] = j + 1;
7979
}
8080
dims[PyArray_NDIM(ap) - 1] = axis;
8181
op = (PyArrayObject *)PyArray_Transpose(ap, &newaxes);
@@ -103,10 +103,11 @@ PyArray_ArgMaxWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int
103103
} else {
104104
out_shape = _shape_buf;
105105
if (axis_copy == NPY_MAXDIMS) {
106-
for( int i = 0; i < out_ndim; i++ ) {
106+
for (int i = 0; i < out_ndim; i++) {
107107
out_shape[i] = 1;
108108
}
109-
} else {
109+
}
110+
else {
110111
/*
111112
* While `ap` may be transposed, we can ignore this for `out` because the
112113
* transpose only reorders the size 1 `axis` (not changing memory layout).
@@ -140,21 +141,9 @@ PyArray_ArgMaxWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int
140141
}
141142
}
142143
else {
143-
int is_out_shape_good = PyArray_NDIM(out) == out_ndim;
144-
for( int i = 0, j = 0; is_out_shape_good && i < out_ndim; i++ ) {
145-
if( keepdims ) {
146-
if( i == axis || axis_copy == NPY_MAXDIMS ) {
147-
is_out_shape_good = PyArray_DIMS(out)[i] == 1;
148-
} else {
149-
is_out_shape_good = PyArray_DIMS(out)[i] == PyArray_DIMS(ap)[j];
150-
j++;
151-
}
152-
} else {
153-
is_out_shape_good = PyArray_DIMS(out)[i] == PyArray_DIMS(ap)[j];
154-
j++;
155-
}
156-
}
157-
if ( !is_out_shape_good ) {
144+
if ((PyArray_NDIM(out) != out_ndim) ||
145+
!PyArray_CompareLists(PyArray_DIMS(out), out_shape,
146+
out_ndim)) {
158147
PyErr_SetString(PyExc_ValueError,
159148
"output array does not match result of np.argmax.");
160149
goto fail;
@@ -205,7 +194,8 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out)
205194
* ArgMin
206195
*/
207196
NPY_NO_EXPORT PyObject *
208-
PyArray_ArgMinWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int keepdims)
197+
PyArray_ArgMinWithKeepdims(PyArrayObject *op,
198+
int axis, PyArrayObject *out, int keepdims)
209199
{
210200
PyArrayObject *ap = NULL, *rp = NULL;
211201
PyArray_ArgFunc* arg_func;
@@ -307,21 +297,9 @@ PyArray_ArgMinWithKeepdims(PyArrayObject *op, int axis, PyArrayObject *out, int
307297
}
308298
}
309299
else {
310-
int is_out_shape_good = PyArray_NDIM(out) == out_ndim;
311-
for( int i = 0, j = 0; is_out_shape_good && i < out_ndim; i++ ) {
312-
if( keepdims ) {
313-
if( i == axis || axis_copy == NPY_MAXDIMS ) {
314-
is_out_shape_good = PyArray_DIMS(out)[i] == 1;
315-
} else {
316-
is_out_shape_good = PyArray_DIMS(out)[i] == PyArray_DIMS(ap)[j];
317-
j++;
318-
}
319-
} else {
320-
is_out_shape_good = PyArray_DIMS(out)[i] == PyArray_DIMS(ap)[j];
321-
j++;
322-
}
323-
}
324-
if ( !is_out_shape_good ) {
300+
if ((PyArray_NDIM(out) != out_ndim) ||
301+
!PyArray_CompareLists(PyArray_DIMS(out), out_shape,
302+
out_ndim)) {
325303
PyErr_SetString(PyExc_ValueError,
326304
"output array does not match result of np.argmin.");
327305
goto fail;

numpy/core/src/multiarray/methods.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ array_argmax(PyArrayObject *self,
290290
if (npy_parse_arguments("argmax", args, len_args, kwnames,
291291
"|axis", &PyArray_AxisConverter, &axis,
292292
"|out", &PyArray_OutputConverter, &out,
293-
"|keepdims", &PyArray_BoolConverter, &keepdims,
293+
"$keepdims", &PyArray_BoolConverter, &keepdims,
294294
NULL, NULL, NULL) < 0) {
295295
return NULL;
296296
}
@@ -314,15 +314,14 @@ array_argmin(PyArrayObject *self,
314314
PyArrayObject *out = NULL;
315315
npy_bool keepdims = NPY_FALSE;
316316
NPY_PREPARE_ARGPARSER;
317-
318317
if (npy_parse_arguments("argmin", args, len_args, kwnames,
319318
"|axis", &PyArray_AxisConverter, &axis,
320319
"|out", &PyArray_OutputConverter, &out,
321-
"|keepdims", &PyArray_BoolConverter, &keepdims,
320+
"$keepdims", &PyArray_BoolConverter, &keepdims,
322321
NULL, NULL, NULL) < 0) {
323322
return NULL;
324323
}
325-
324+
326325
PyObject *ret = PyArray_ArgMinWithKeepdims(self, axis, out, keepdims);
327326

328327
/* this matches the unpacking behavior of ufuncs */

0 commit comments

Comments
 (0)
0