static PyObject *Py_DistanceTransformBruteForce(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *features = NULL; PyArrayObject *sampling = NULL; int metric; if (!PyArg_ParseTuple(args, "O&iO&O&O&", NI_ObjectToInputArray, &input, &metric, NI_ObjectToOptionalInputArray, &sampling, NI_ObjectToOptionalOutputArray, &output, NI_ObjectToOptionalOutputArray, &features)) goto exit; NI_DistanceTransformBruteForce(input, metric, sampling, output, features); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(sampling); Py_XDECREF(output); Py_XDECREF(features); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_ZoomShift(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *shift = NULL; PyArrayObject *zoom = NULL; int mode, order; double cval; if (!PyArg_ParseTuple(args, "O&O&O&O&iid", NI_ObjectToInputArray, &input, NI_ObjectToOptionalInputArray, &zoom, NI_ObjectToOptionalInputArray, &shift, NI_ObjectToOutputArray, &output, &order, &mode, &cval)) goto exit; NI_ZoomShift(input, zoom, shift, output, order, (NI_ExtendMode)mode, cval); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(shift); Py_XDECREF(zoom); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_MinOrMaxFilter1D(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL; int axis, mode, minimum; npy_intp filter_size, origin; double cval; if (!PyArg_ParseTuple(args, "O&niO&idni", NI_ObjectToInputArray, &input, &filter_size, &axis, NI_ObjectToOutputArray, &output, &mode, &cval, &origin, &minimum)) goto exit; NI_MinOrMaxFilter1D(input, filter_size, axis, output, (NI_ExtendMode)mode, cval, origin, minimum); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_FourierFilter(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *parameters = NULL; int axis, filter_type; npy_intp n; if (!PyArg_ParseTuple(args, "O&O&niO&i", NI_ObjectToInputArray, &input, NI_ObjectToInputArray, ¶meters, &n, &axis, NI_ObjectToOutputArray, &output, &filter_type)) goto exit; NI_FourierFilter(input, parameters, n, axis, output, filter_type); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(parameters); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_RankFilter(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *footprint = NULL; PyArray_Dims origin; int mode, rank; double cval; if (!PyArg_ParseTuple(args, "O&iO&O&idO&", NI_ObjectToInputArray, &input, &rank, NI_ObjectToInputArray, &footprint, NI_ObjectToOutputArray, &output, &mode, &cval, PyArray_IntpConverter, &origin)) { goto exit; } if (!_validate_origin(input, origin)) { goto exit; } NI_RankFilter(input, rank, footprint, output, (NI_ExtendMode)mode, cval, origin.ptr); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(footprint); Py_XDECREF(output); PyDimMem_FREE(origin.ptr); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_Correlate1D(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *weights = NULL; int axis, mode; double cval; npy_intp origin; if (!PyArg_ParseTuple(args, "O&O&iO&idn" , NI_ObjectToInputArray, &input, NI_ObjectToInputArray, &weights, &axis, NI_ObjectToOutputArray, &output, &mode, &cval, &origin)) goto exit; NI_Correlate1D(input, weights, axis, output, (NI_ExtendMode)mode, cval, origin); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(weights); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_BinaryErosion(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *strct = NULL; PyArrayObject *mask = NULL; PyObject *cobj = NULL; int border_value, invert, center_is_true; int changed = 0, return_coordinates; NI_CoordinateList *coordinate_list = NULL; PyArray_Dims origin; if (!PyArg_ParseTuple(args, "O&O&O&O&iO&iii", NI_ObjectToInputArray, &input, NI_ObjectToInputArray, &strct, NI_ObjectToOptionalInputArray, &mask, NI_ObjectToOutputArray, &output, &border_value, PyArray_IntpConverter, &origin, &invert, ¢er_is_true, &return_coordinates)) { goto exit; } if (!_validate_origin(input, origin)) { goto exit; } if (!NI_BinaryErosion(input, strct, mask, output, border_value, origin.ptr, invert, center_is_true, &changed, return_coordinates ? &coordinate_list : NULL)) { goto exit; } if (return_coordinates) { cobj = NpyCapsule_FromVoidPtr(coordinate_list, _FreeCoordinateList); } #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(strct); Py_XDECREF(mask); Py_XDECREF(output); PyDimMem_FREE(origin.ptr); if (PyErr_Occurred()) { Py_XDECREF(cobj); return NULL; } else { if (return_coordinates) { return Py_BuildValue("iN", changed, cobj); } else { return Py_BuildValue("i", changed); } } }
static PyObject *Py_SplineFilter1D(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL; int axis, order, mode; if (!PyArg_ParseTuple(args, "O&iiO&i", NI_ObjectToInputArray, &input, &order, &axis, NI_ObjectToOutputArray, &output, &mode)) goto exit; NI_SplineFilter1D(input, order, axis, mode, output); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_WatershedIFT(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *markers = NULL; PyArrayObject *strct = NULL; if (!PyArg_ParseTuple(args, "O&O&O&O&", NI_ObjectToInputArray, &input, NI_ObjectToInputArray, &markers, NI_ObjectToInputArray, &strct, NI_ObjectToOutputArray, &output)) goto exit; NI_WatershedIFT(input, markers, strct, output); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(markers); Py_XDECREF(strct); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_FourierShift(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *shifts = NULL; int axis; npy_intp n; if (!PyArg_ParseTuple(args, "O&O&niO&", NI_ObjectToInputArray, &input, NI_ObjectToInputArray, &shifts, &n, &axis, NI_ObjectToOutputArray, &output)) goto exit; NI_FourierShift(input, shifts, n, axis, output); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: Py_XDECREF(input); Py_XDECREF(shifts); Py_XDECREF(output); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
/* * This function executes all the standard NumPy reduction function * boilerplate code, just calling assign_identity and the appropriate * inner loop function where necessary. * * operand : The array to be reduced. * out : NULL, or the array into which to place the result. * wheremask : NOT YET SUPPORTED, but this parameter is placed here * so that support can be added in the future without breaking * API compatibility. Pass in NULL. * operand_dtype : The dtype the inner loop expects for the operand. * result_dtype : The dtype the inner loop expects for the result. * casting : The casting rule to apply to the operands. * axis_flags : Flags indicating the reduction axes of 'operand'. * reorderable : If True, the reduction being done is reorderable, which * means specifying multiple axes of reduction at once is ok, * and the reduction code may calculate the reduction in an * arbitrary order. The calculation may be reordered because * of cache behavior or multithreading requirements. * keepdims : If true, leaves the reduction dimensions in the result * with size one. * subok : If true, the result uses the subclass of operand, otherwise * it is always a base class ndarray. * assign_identity : If NULL, PyArray_InitializeReduceResult is used, otherwise * this function is called to initialize the result to * the reduction's unit. * loop : The loop which does the reduction. * data : Data which is passed to assign_identity and the inner loop. * buffersize : Buffer size for the iterator. For the default, pass in 0. * funcname : The name of the reduction function, for error messages. * errormask : forwarded from _get_bufsize_errmask * * TODO FIXME: if you squint, this is essentially an second independent * implementation of generalized ufuncs with signature (i)->(), plus a few * extra bells and whistles. (Indeed, as far as I can tell, it was originally * split out to support a fancy version of count_nonzero... which is not * actually a reduction function at all, it's just a (i)->() function!) So * probably these two implementation should be merged into one. (In fact it * would be quite nice to support axis= and keepdims etc. for arbitrary * generalized ufuncs!) */ NPY_NO_EXPORT PyArrayObject * PyUFunc_ReduceWrapper(PyArrayObject *operand, PyArrayObject *out, PyArrayObject *wheremask, PyArray_Descr *operand_dtype, PyArray_Descr *result_dtype, NPY_CASTING casting, npy_bool *axis_flags, int reorderable, int keepdims, int subok, PyArray_AssignReduceIdentityFunc *assign_identity, PyArray_ReduceLoopFunc *loop, void *data, npy_intp buffersize, const char *funcname, int errormask) { PyArrayObject *result = NULL, *op_view = NULL; npy_intp skip_first_count = 0; /* Iterator parameters */ NpyIter *iter = NULL; PyArrayObject *op[2]; PyArray_Descr *op_dtypes[2]; npy_uint32 flags, op_flags[2]; /* Validate that the parameters for future expansion are NULL */ if (wheremask != NULL) { PyErr_SetString(PyExc_RuntimeError, "Reduce operations in NumPy do not yet support " "a where mask"); return NULL; } /* * This either conforms 'out' to the ndim of 'operand', or allocates * a new array appropriate for this reduction. * * A new array with WRITEBACKIFCOPY is allocated if operand and out have memory * overlap. */ Py_INCREF(result_dtype); result = PyArray_CreateReduceResult(operand, out, result_dtype, axis_flags, keepdims, subok, funcname); if (result == NULL) { goto fail; } /* * Initialize the result to the reduction unit if possible, * otherwise copy the initial values and get a view to the rest. */ if (assign_identity != NULL) { /* * If this reduction is non-reorderable, make sure there are * only 0 or 1 axes in axis_flags. */ if (!reorderable && check_nonreorderable_axes(PyArray_NDIM(operand), axis_flags, funcname) < 0) { goto fail; } if (assign_identity(result, data) < 0) { goto fail; } op_view = operand; Py_INCREF(op_view); } else { op_view = PyArray_InitializeReduceResult(result, operand, axis_flags, reorderable, &skip_first_count, funcname); if (op_view == NULL) { goto fail; } /* empty op_view signals no reduction; but 0-d arrays cannot be empty */ if ((PyArray_SIZE(op_view) == 0) || (PyArray_NDIM(operand) == 0)) { Py_DECREF(op_view); op_view = NULL; goto finish; } } /* Set up the iterator */ op[0] = result; op[1] = op_view; op_dtypes[0] = result_dtype; op_dtypes[1] = operand_dtype; flags = NPY_ITER_BUFFERED | NPY_ITER_EXTERNAL_LOOP | NPY_ITER_GROWINNER | NPY_ITER_DONT_NEGATE_STRIDES | NPY_ITER_ZEROSIZE_OK | NPY_ITER_REDUCE_OK | NPY_ITER_REFS_OK; op_flags[0] = NPY_ITER_READWRITE | NPY_ITER_ALIGNED | NPY_ITER_NO_SUBTYPE; op_flags[1] = NPY_ITER_READONLY | NPY_ITER_ALIGNED; iter = NpyIter_AdvancedNew(2, op, flags, NPY_KEEPORDER, casting, op_flags, op_dtypes, -1, NULL, NULL, buffersize); if (iter == NULL) { goto fail; } /* Start with the floating-point exception flags cleared */ PyUFunc_clearfperr(); if (NpyIter_GetIterSize(iter) != 0) { NpyIter_IterNextFunc *iternext; char **dataptr; npy_intp *strideptr; npy_intp *countptr; int needs_api; iternext = NpyIter_GetIterNext(iter, NULL); if (iternext == NULL) { goto fail; } dataptr = NpyIter_GetDataPtrArray(iter); strideptr = NpyIter_GetInnerStrideArray(iter); countptr = NpyIter_GetInnerLoopSizePtr(iter); needs_api = NpyIter_IterationNeedsAPI(iter); /* Straightforward reduction */ if (loop == NULL) { PyErr_Format(PyExc_RuntimeError, "reduction operation %s did not supply an " "inner loop function", funcname); goto fail; } if (loop(iter, dataptr, strideptr, countptr, iternext, needs_api, skip_first_count, data) < 0) { goto fail; } } /* Check whether any errors occurred during the loop */ if (PyErr_Occurred() || _check_ufunc_fperr(errormask, NULL, "reduce") < 0) { goto fail; } NpyIter_Deallocate(iter); Py_DECREF(op_view); finish: /* Strip out the extra 'one' dimensions in the result */ if (out == NULL) { if (!keepdims) { PyArray_RemoveAxesInPlace(result, axis_flags); } } else { PyArray_ResolveWritebackIfCopy(result); /* prevent spurious warnings */ Py_DECREF(result); result = out; Py_INCREF(result); } return result; fail: PyArray_ResolveWritebackIfCopy(result); /* prevent spurious warnings */ Py_XDECREF(result); Py_XDECREF(op_view); if (iter != NULL) { NpyIter_Deallocate(iter); } return NULL; }
static PyObject *Py_GeometricTransform(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL; PyArrayObject *coordinates = NULL, *matrix = NULL, *shift = NULL; PyObject *fnc = NULL, *extra_arguments = NULL, *extra_keywords = NULL; int mode, order; double cval; void *func = NULL, *data = NULL; NI_PythonCallbackData cbdata; ccallback_t callback; static ccallback_signature_t callback_signatures[] = { {"int (intptr_t *, double *, int, int, void *)"}, {"int (npy_intp *, double *, int, int, void *)"}, #if NPY_SIZEOF_INTP == NPY_SIZEOF_SHORT {"int (short *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_INT {"int (int *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONG {"int (long *, double *, int, int, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONGLONG {"int (long long *, double *, int, int, void *)"}, #endif {NULL} }; callback.py_function = NULL; callback.c_function = NULL; if (!PyArg_ParseTuple(args, "O&OO&O&O&O&iidOO", NI_ObjectToInputArray, &input, &fnc, NI_ObjectToOptionalInputArray, &coordinates, NI_ObjectToOptionalInputArray, &matrix, NI_ObjectToOptionalInputArray, &shift, NI_ObjectToOutputArray, &output, &order, &mode, &cval, &extra_arguments, &extra_keywords)) goto exit; if (fnc != Py_None) { if (!PyTuple_Check(extra_arguments)) { PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); goto exit; } if (!PyDict_Check(extra_keywords)) { PyErr_SetString(PyExc_RuntimeError, "extra_keywords must be a dictionary"); goto exit; } if (PyCapsule_CheckExact(fnc) && PyCapsule_GetName(fnc) == NULL) { func = PyCapsule_GetPointer(fnc, NULL); data = PyCapsule_GetContext(fnc); #if PY_VERSION_HEX < 0x03000000 } else if (PyCObject_Check(fnc)) { /* 'Legacy' low-level callable on Py2 */ func = PyCObject_AsVoidPtr(fnc); data = PyCObject_GetDesc(fnc); #endif } else { int ret; ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS); if (ret == -1) { goto exit; } if (callback.py_function != NULL) { cbdata.extra_arguments = extra_arguments; cbdata.extra_keywords = extra_keywords; callback.info_p = (void*)&cbdata; func = Py_Map; data = (void*)&callback; } else { func = callback.c_function; data = callback.user_data; } } } NI_GeometricTransform(input, func, data, matrix, shift, coordinates, output, order, (NI_ExtendMode)mode, cval); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: if (callback.py_function != NULL || callback.c_function != NULL) { ccallback_release(&callback); } Py_XDECREF(input); Py_XDECREF(output); Py_XDECREF(coordinates); Py_XDECREF(matrix); Py_XDECREF(shift); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
static PyObject *Py_GenericFilter(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL, *output = NULL, *footprint = NULL; PyObject *fnc = NULL, *extra_arguments = NULL, *extra_keywords = NULL; void *func = NULL, *data = NULL; NI_PythonCallbackData cbdata; int mode; PyArray_Dims origin; double cval; ccallback_t callback; static ccallback_signature_t callback_signatures[] = { {"int (double *, intptr_t, double *, void *)"}, {"int (double *, npy_intp, double *, void *)"}, #if NPY_SIZEOF_INTP == NPY_SIZEOF_SHORT {"int (double *, short, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_INT {"int (double *, int, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONG {"int (double *, long, double *, void *)"}, #endif #if NPY_SIZEOF_INTP == NPY_SIZEOF_LONGLONG {"int (double *, long long, double *, void *)"}, #endif {NULL} }; callback.py_function = NULL; callback.c_function = NULL; if (!PyArg_ParseTuple(args, "O&OO&O&idO&OO", NI_ObjectToInputArray, &input, &fnc, NI_ObjectToInputArray, &footprint, NI_ObjectToOutputArray, &output, &mode, &cval, PyArray_IntpConverter, &origin, &extra_arguments, &extra_keywords)) { goto exit; } if (!_validate_origin(input, origin)) { goto exit; } if (!PyTuple_Check(extra_arguments)) { PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); goto exit; } if (!PyDict_Check(extra_keywords)) { PyErr_SetString(PyExc_RuntimeError, "extra_keywords must be a dictionary"); goto exit; } if (PyCapsule_CheckExact(fnc) && PyCapsule_GetName(fnc) == NULL) { func = PyCapsule_GetPointer(fnc, NULL); data = PyCapsule_GetContext(fnc); #if PY_VERSION_HEX < 0x03000000 } else if (PyCObject_Check(fnc)) { /* 'Legacy' low-level callable on Py2 */ func = PyCObject_AsVoidPtr(fnc); data = PyCObject_GetDesc(fnc); #endif } else { int ret; ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS); if (ret == -1) { goto exit; } if (callback.py_function != NULL) { cbdata.extra_arguments = extra_arguments; cbdata.extra_keywords = extra_keywords; callback.info_p = (void*)&cbdata; func = Py_FilterFunc; data = (void*)&callback; } else { func = callback.c_function; data = callback.user_data; } } NI_GenericFilter(input, func, data, footprint, output, (NI_ExtendMode)mode, cval, origin.ptr); #ifdef HAVE_WRITEBACKIFCOPY PyArray_ResolveWritebackIfCopy(output); #endif exit: if (callback.py_function != NULL || callback.c_function != NULL) { ccallback_release(&callback); } Py_XDECREF(input); Py_XDECREF(output); Py_XDECREF(footprint); PyDimMem_FREE(origin.ptr); return PyErr_Occurred() ? NULL : Py_BuildValue(""); }
/*NUMPY_API * Clip */ NPY_NO_EXPORT PyObject * PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *out) { PyArray_FastClipFunc *func; int outgood = 0, ingood = 0; PyArrayObject *maxa = NULL; PyArrayObject *mina = NULL; PyArrayObject *newout = NULL, *newin = NULL; PyArray_Descr *indescr = NULL, *newdescr = NULL; char *max_data, *min_data; PyObject *zero; /* Treat None the same as NULL */ if (min == Py_None) { min = NULL; } if (max == Py_None) { max = NULL; } if ((max == NULL) && (min == NULL)) { PyErr_SetString(PyExc_ValueError, "array_clip: must set either max or min"); return NULL; } func = PyArray_DESCR(self)->f->fastclip; if (func == NULL || (min != NULL && !PyArray_CheckAnyScalar(min)) || (max != NULL && !PyArray_CheckAnyScalar(max)) || PyArray_ISBYTESWAPPED(self) || (out && PyArray_ISBYTESWAPPED(out))) { return _slow_array_clip(self, min, max, out); } /* Use the fast scalar clip function */ /* First we need to figure out the correct type */ if (min != NULL) { indescr = PyArray_DescrFromObject(min, NULL); if (indescr == NULL) { goto fail; } } if (max != NULL) { newdescr = PyArray_DescrFromObject(max, indescr); Py_XDECREF(indescr); indescr = NULL; if (newdescr == NULL) { goto fail; } } else { /* Steal the reference */ newdescr = indescr; indescr = NULL; } /* * Use the scalar descriptor only if it is of a bigger * KIND than the input array (and then find the * type that matches both). */ if (PyArray_ScalarKind(newdescr->type_num, NULL) > PyArray_ScalarKind(PyArray_DESCR(self)->type_num, NULL)) { indescr = PyArray_PromoteTypes(newdescr, PyArray_DESCR(self)); if (indescr == NULL) { goto fail; } func = indescr->f->fastclip; if (func == NULL) { Py_DECREF(indescr); return _slow_array_clip(self, min, max, out); } } else { indescr = PyArray_DESCR(self); Py_INCREF(indescr); } Py_DECREF(newdescr); newdescr = NULL; if (!PyDataType_ISNOTSWAPPED(indescr)) { PyArray_Descr *descr2; descr2 = PyArray_DescrNewByteorder(indescr, '='); Py_DECREF(indescr); indescr = NULL; if (descr2 == NULL) { goto fail; } indescr = descr2; } /* Convert max to an array */ if (max != NULL) { Py_INCREF(indescr); maxa = (PyArrayObject *)PyArray_FromAny(max, indescr, 0, 0, NPY_ARRAY_DEFAULT, NULL); if (maxa == NULL) { goto fail; } } /* * If we are unsigned, then make sure min is not < 0 * This is to match the behavior of _slow_array_clip * * We allow min and max to go beyond the limits * for other data-types in which case they * are interpreted as their modular counterparts. */ if (min != NULL) { if (PyArray_ISUNSIGNED(self)) { int cmp; zero = PyInt_FromLong(0); cmp = PyObject_RichCompareBool(min, zero, Py_LT); if (cmp == -1) { Py_DECREF(zero); goto fail; } if (cmp == 1) { min = zero; } else { Py_DECREF(zero); Py_INCREF(min); } } else { Py_INCREF(min); } /* Convert min to an array */ Py_INCREF(indescr); mina = (PyArrayObject *)PyArray_FromAny(min, indescr, 0, 0, NPY_ARRAY_DEFAULT, NULL); Py_DECREF(min); if (mina == NULL) { goto fail; } } /* * Check to see if input is single-segment, aligned, * and in native byteorder */ if (PyArray_ISONESEGMENT(self) && PyArray_CHKFLAGS(self, NPY_ARRAY_ALIGNED) && PyArray_ISNOTSWAPPED(self) && (PyArray_DESCR(self) == indescr)) { ingood = 1; } if (!ingood) { int flags; if (PyArray_ISFORTRAN(self)) { flags = NPY_ARRAY_FARRAY; } else { flags = NPY_ARRAY_CARRAY; } Py_INCREF(indescr); newin = (PyArrayObject *)PyArray_FromArray(self, indescr, flags); if (newin == NULL) { goto fail; } } else { newin = self; Py_INCREF(newin); } /* * At this point, newin is a single-segment, aligned, and correct * byte-order array of the correct type * * if ingood == 0, then it is a copy, otherwise, * it is the original input. */ /* * If we have already made a copy of the data, then use * that as the output array */ if (out == NULL && !ingood) { out = newin; } /* * Now, we know newin is a usable array for fastclip, * we need to make sure the output array is available * and usable */ if (out == NULL) { Py_INCREF(indescr); out = (PyArrayObject*)PyArray_NewFromDescr(Py_TYPE(self), indescr, PyArray_NDIM(self), PyArray_DIMS(self), NULL, NULL, PyArray_ISFORTRAN(self), (PyObject *)self); if (out == NULL) { goto fail; } outgood = 1; } else Py_INCREF(out); /* Input is good at this point */ if (out == newin) { outgood = 1; } /* make sure the shape of the output array is the same */ if (!PyArray_SAMESHAPE(newin, out)) { PyErr_SetString(PyExc_ValueError, "clip: Output array must have the" "same shape as the input."); goto fail; } if (!outgood && PyArray_EQUIVALENTLY_ITERABLE( self, out, PyArray_TRIVIALLY_ITERABLE_OP_READ, PyArray_TRIVIALLY_ITERABLE_OP_NOREAD) && PyArray_CHKFLAGS(out, NPY_ARRAY_ALIGNED) && PyArray_ISNOTSWAPPED(out) && PyArray_EquivTypes(PyArray_DESCR(out), indescr)) { outgood = 1; } /* * Do we still not have a suitable output array? * Create one, now. No matter why the array is not suitable a copy has * to be made. This may be just to avoid memory overlap though. */ if (!outgood) { int oflags; if (PyArray_ISFORTRAN(self)) { oflags = NPY_ARRAY_FARRAY; } else { oflags = NPY_ARRAY_CARRAY; } oflags |= (NPY_ARRAY_WRITEBACKIFCOPY | NPY_ARRAY_FORCECAST | NPY_ARRAY_ENSURECOPY); Py_INCREF(indescr); newout = (PyArrayObject*)PyArray_FromArray(out, indescr, oflags); if (newout == NULL) { goto fail; } } else { newout = out; Py_INCREF(newout); } /* Now we can call the fast-clip function */ min_data = max_data = NULL; if (mina != NULL) { min_data = PyArray_DATA(mina); } if (maxa != NULL) { max_data = PyArray_DATA(maxa); } func(PyArray_DATA(newin), PyArray_SIZE(newin), min_data, max_data, PyArray_DATA(newout)); /* Clean up temporary variables */ Py_XDECREF(indescr); Py_XDECREF(newdescr); Py_XDECREF(mina); Py_XDECREF(maxa); Py_DECREF(newin); /* Copy back into out if out was not already a nice array. */ PyArray_ResolveWritebackIfCopy(newout); Py_DECREF(newout); return (PyObject *)out; fail: Py_XDECREF(indescr); Py_XDECREF(newdescr); Py_XDECREF(maxa); Py_XDECREF(mina); Py_XDECREF(newin); PyArray_DiscardWritebackIfCopy(newout); Py_XDECREF(newout); return NULL; }
/*NUMPY_API * ArgMax */ NPY_NO_EXPORT PyObject * PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) { PyArrayObject *ap = NULL, *rp = NULL; PyArray_ArgFunc* arg_func; char *ip; npy_intp *rptr; npy_intp i, n, m; int elsize; NPY_BEGIN_THREADS_DEF; if ((ap = (PyArrayObject *)PyArray_CheckAxis(op, &axis, 0)) == NULL) { return NULL; } /* * We need to permute the array so that axis is placed at the end. * And all other dimensions are shifted left. */ if (axis != PyArray_NDIM(ap)-1) { PyArray_Dims newaxes; npy_intp dims[NPY_MAXDIMS]; int j; newaxes.ptr = dims; newaxes.len = PyArray_NDIM(ap); for (j = 0; j < axis; j++) { dims[j] = j; } for (j = axis; j < PyArray_NDIM(ap) - 1; j++) { dims[j] = j + 1; } dims[PyArray_NDIM(ap) - 1] = axis; op = (PyArrayObject *)PyArray_Transpose(ap, &newaxes); Py_DECREF(ap); if (op == NULL) { return NULL; } } else { op = ap; } /* Will get native-byte order contiguous copy. */ ap = (PyArrayObject *)PyArray_ContiguousFromAny((PyObject *)op, PyArray_DESCR(op)->type_num, 1, 0); Py_DECREF(op); if (ap == NULL) { return NULL; } arg_func = PyArray_DESCR(ap)->f->argmax; if (arg_func == NULL) { PyErr_SetString(PyExc_TypeError, "data type not ordered"); goto fail; } elsize = PyArray_DESCR(ap)->elsize; m = PyArray_DIMS(ap)[PyArray_NDIM(ap)-1]; if (m == 0) { PyErr_SetString(PyExc_ValueError, "attempt to get argmax of an empty sequence"); goto fail; } if (!out) { rp = (PyArrayObject *)PyArray_NewFromDescr( Py_TYPE(ap), PyArray_DescrFromType(NPY_INTP), PyArray_NDIM(ap) - 1, PyArray_DIMS(ap), NULL, NULL, 0, (PyObject *)ap); if (rp == NULL) { goto fail; } } else { if ((PyArray_NDIM(out) != PyArray_NDIM(ap) - 1) || !PyArray_CompareLists(PyArray_DIMS(out), PyArray_DIMS(ap), PyArray_NDIM(out))) { PyErr_SetString(PyExc_ValueError, "output array does not match result of np.argmax."); goto fail; } rp = (PyArrayObject *)PyArray_FromArray(out, PyArray_DescrFromType(NPY_INTP), NPY_ARRAY_CARRAY | NPY_ARRAY_WRITEBACKIFCOPY); if (rp == NULL) { goto fail; } } NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap)); n = PyArray_SIZE(ap)/m; rptr = (npy_intp *)PyArray_DATA(rp); for (ip = PyArray_DATA(ap), i = 0; i < n; i++, ip += elsize*m) { arg_func(ip, m, rptr, ap); rptr += 1; } NPY_END_THREADS_DESCR(PyArray_DESCR(ap)); Py_DECREF(ap); /* Trigger the UPDATEIFCOPY/WRTIEBACKIFCOPY if necessary */ if (out != NULL && out != rp) { PyArray_ResolveWritebackIfCopy(rp); Py_DECREF(rp); rp = out; Py_INCREF(rp); } return (PyObject *)rp; fail: Py_DECREF(ap); Py_XDECREF(rp); return NULL; }