예제 #1
0
static int Py_Filter1DFunc(double *iline, npy_intp ilen,
                           double *oline, npy_intp olen, void *data)
{
    PyArrayObject *py_ibuffer = NULL, *py_obuffer = NULL;
    PyObject *rv = NULL, *args = NULL, *tmp = NULL;
    npy_intp ii;
    double *po = NULL;
    NI_PythonCallbackData *cbdata = (NI_PythonCallbackData*)data;

    py_ibuffer = NA_NewArray(iline, PyArray_DOUBLE, 1, &ilen);
    py_obuffer = NA_NewArray(NULL, PyArray_DOUBLE, 1, &olen);
    if (!py_ibuffer || !py_obuffer)
        goto exit;
    tmp = Py_BuildValue("(OO)", py_ibuffer, py_obuffer);
    if (!tmp)
        goto exit;
    args = PySequence_Concat(tmp, cbdata->extra_arguments);
    if (!args)
        goto exit;
    rv = PyObject_Call(cbdata->function, args, cbdata->extra_keywords);
    if (!rv)
        goto exit;
    po = (double*)PyArray_DATA(py_obuffer);
    for(ii = 0; ii < olen; ii++)
        oline[ii] = po[ii];
exit:
    Py_XDECREF(py_ibuffer);
    Py_XDECREF(py_obuffer);
    Py_XDECREF(rv);
    Py_XDECREF(args);
    Py_XDECREF(tmp);
    return PyErr_Occurred() ? 0 : 1;
}
예제 #2
0
static int Py_FilterFunc(double *buffer, npy_intp filter_size,
                                                 double *output, void *data)
{
    PyArrayObject *py_buffer = NULL;
    PyObject *rv = NULL, *args = NULL, *tmp = NULL;
    NI_PythonCallbackData *cbdata = (NI_PythonCallbackData*)data;

    py_buffer = NA_NewArray(buffer, PyArray_DOUBLE, 1, &filter_size);
    if (!py_buffer)
        goto exit;
    tmp = Py_BuildValue("(O)", py_buffer);
    if (!tmp)
        goto exit;
    args = PySequence_Concat(tmp, cbdata->extra_arguments);
    if (!args)
        goto exit;
    rv = PyObject_Call(cbdata->function, args, cbdata->extra_keywords);
    if (!rv)
        goto exit;
    *output = PyFloat_AsDouble(rv);
exit:
    Py_XDECREF(py_buffer);
    Py_XDECREF(rv);
    Py_XDECREF(args);
    Py_XDECREF(tmp);
    return PyErr_Occurred() ? 0 : 1;
}
예제 #3
0
static PyObject *Py_Histogram(PyObject *obj, PyObject *args)
{
    PyArrayObject *input = NULL, *labels = NULL, **histograms = NULL;
    PyObject *indices_object, *result = NULL;
    maybelong min_label, max_label, *result_indices = NULL, n_results;
    maybelong jj, nbins;
    long nbins_in;
    double min, max;

    if (!PyArg_ParseTuple(args, "O&ddlO&O", NI_ObjectToInputArray, &input,
                                                &min, &max, &nbins_in, NI_ObjectToOptionalInputArray,
                                                &labels, &indices_object))
        goto exit;
    nbins = nbins_in;

    if (!_NI_GetIndices(indices_object, &result_indices, &min_label,
                                            &max_label, &n_results))
        goto exit;

    /* Set all pointers to NULL, so that freeing the memory */
    /* doesn't cause problems. */
    histograms = (PyArrayObject**)calloc(input->nd * n_results,
                                                                             sizeof(PyArrayObject*));
    if (!histograms) {
        PyErr_NoMemory();
        goto exit;
    }
    for(jj = 0; jj < n_results; jj++) {
        histograms[jj] = NA_NewArray(NULL, tInt32, 1, &nbins);
        if (!histograms[jj]) {
            PyErr_NoMemory();
            goto exit;
        }
    }

    if (!NI_Histogram(input, labels, min_label, max_label, result_indices,
                                        n_results, histograms, min, max, nbins))
        goto exit;

    result = _NI_BuildMeasurementResultArrayObject(n_results, histograms);

 exit:
    Py_XDECREF(input);
    Py_XDECREF(labels);
    if (result_indices)
        free(result_indices);
    if (histograms) {
        for(jj = 0; jj < n_results; jj++) {
            Py_XDECREF(histograms[jj]);
        }
        free(histograms);
    }
    return result;
}