diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/include/numpy/ndarraytypes.h | 6 | ||||
-rw-r--r-- | numpy/core/numeric.py | 13 | ||||
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 71 | ||||
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.h | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 4 |
5 files changed, 82 insertions, 15 deletions
diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 63e8bf974..f616268f1 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -236,6 +236,12 @@ typedef enum { NPY_RAISE=2 } NPY_CLIPMODE; +typedef enum { + NPY_VALID=0, + NPY_SAME=1, + NPY_FULL=2 +} NPY_CORRELATEMODE; + /* The special not-a-time (NaT) value */ #define NPY_DATETIME_NAT NPY_MIN_INT64 diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 7675386e7..a6ee9eba9 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -662,17 +662,6 @@ def flatnonzero(a): return np.nonzero(np.ravel(a))[0] -_mode_from_name_dict = {'v': 0, - 's': 1, - 'f': 2} - - -def _mode_from_name(mode): - if isinstance(mode, str): - return _mode_from_name_dict[mode.lower()[0]] - return mode - - def _correlate_dispatcher(a, v, mode=None): return (a, v) @@ -748,7 +737,6 @@ def correlate(a, v, mode='valid'): array([ 0.0+0.j , 3.0+1.j , 1.5+1.5j, 1.0+0.j , 0.5+0.5j]) """ - mode = _mode_from_name(mode) return multiarray.correlate2(a, v, mode) @@ -852,7 +840,6 @@ def convolve(a, v, mode='full'): raise ValueError('a cannot be empty') if len(v) == 0: raise ValueError('v cannot be empty') - mode = _mode_from_name(mode) return multiarray.correlate(a, v[::-1], mode) diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index dd18f71fd..bcb916791 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -715,6 +715,77 @@ PyArray_ConvertClipmodeSequence(PyObject *object, NPY_CLIPMODE *modes, int n) return NPY_SUCCEED; } +static int correlatemode_parser(char const *str, Py_ssize_t length, void *data) +{ + NPY_CORRELATEMODE *val = (NPY_CORRELATEMODE *)data; + int is_exact = 0; + + if (length < 1) { + return -1; + } + if (str[0] == 'V' || str[0] == 'v') { + *val = NPY_VALID; + is_exact = (length == 5 && strcmp(str, "valid") == 0); + } + else if (str[0] == 'S' || str[0] == 's') { + *val = NPY_SAME; + is_exact = (length == 4 && strcmp(str, "same") == 0); + } + else if (str[0] == 'F' || str[0] == 'f') { + *val = NPY_FULL; + is_exact = (length == 4 && strcmp(str, "full") == 0); + } + else { + return -1; + } + + /* Filters out the case sensitive/non-exact + * match inputs and other inputs and outputs DeprecationWarning + */ + if (!is_exact) { + if (DEPRECATE("inexact matches and case insensitive matches for " + "convolve/correlate mode are deprecated, please " + "use one of 'valid', 'same', or 'full' instead.") < 0) { + return -1; + } + } + + return 0; +} + +/* + * Convert an object to NPY_VALID / NPY_SAME / NPY_FULL + */ +NPY_NO_EXPORT int +PyArray_CorrelatemodeConverter(PyObject *object, NPY_CORRELATEMODE *val) +{ + if (PyUnicode_Check(object)) { + return string_converter_helper( + object, (void *)val, correlatemode_parser, "mode", + "must be one of 'valid', 'same', or 'full'"); + } + + else { + /* For users passing integers */ + int number = PyArray_PyIntAsInt(object); + if (error_converting(number)) { + PyErr_SetString(PyExc_TypeError, + "convolve/correlate mode not understood"); + return NPY_FAIL; + } + if (number <= (int) NPY_FULL + && number >= (int) NPY_VALID) { + *val = (NPY_CORRELATEMODE) number; + return NPY_SUCCEED; + } + else { + PyErr_Format(PyExc_ValueError, + "integer convolve/correlate mode must be 0, 1, or 2"); + return NPY_FAIL; + } + } +} + static int casting_parser(char const *str, Py_ssize_t length, void *data) { NPY_CASTING *casting = (NPY_CASTING *)data; diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index bee0c6064..7d1871c43 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -43,6 +43,9 @@ NPY_NO_EXPORT PyObject * PyArray_IntTupleFromIntp(int len, npy_intp const *vals); NPY_NO_EXPORT int +PyArray_CorrelatemodeConverter(PyObject *object, NPY_CORRELATEMODE *val); + +NPY_NO_EXPORT int PyArray_SelectkindConverter(PyObject *obj, NPY_SELECTKIND *selectkind); /* diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index a0f7afeb5..12705dc19 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -2839,7 +2839,7 @@ array_correlate(PyObject *NPY_UNUSED(dummy), if (npy_parse_arguments("correlate", args, len_args, kwnames, "a", NULL, &a0, "v", NULL, &shape, - "|mode", &PyArray_PythonPyIntFromInt, &mode, + "|mode", &PyArray_CorrelatemodeConverter, &mode, NULL, NULL, NULL) < 0) { return NULL; } @@ -2857,7 +2857,7 @@ array_correlate2(PyObject *NPY_UNUSED(dummy), if (npy_parse_arguments("correlate2", args, len_args, kwnames, "a", NULL, &a0, "v", NULL, &shape, - "|mode", &PyArray_PythonPyIntFromInt, &mode, + "|mode", &PyArray_CorrelatemodeConverter, &mode, NULL, NULL, NULL) < 0) { return NULL; } |