コード例 #1
0
ファイル: _cky.c プロジェクト: pombredanne/nlp
static PyObject
*cky_decode (_cky *self, PyObject *args)
{
    double theta;
    int i, ind, n, ntags = self->ntags;
    PyObject *score_obj, *back_obj;
    if (!PyArg_ParseTuple(args, "iOOd", &n, &score_obj, &back_obj, &theta))
        return NULL;

    PyArrayObject *score_array = PARSE_ARRAY(score_obj),
                  *back_array = (PyArrayObject*) PyArray_FROM_OTF(back_obj, NPY_OBJECT, NPY_INOUT_ARRAY);
    double *score = PyArray_DATA(score_array);
    PyObject **back = PyArray_DATA(back_array);

    for (i = 0; i < n; ++i)
        update_unaries(n, ntags, i, 0, self->nunaries, self->unaries, score, back, theta, 0);

    PyObject *list;
    double prob, lp, rp, p, tmp, *r, *l;
    int span, begin, end, split, parent, lchild, rchild;
    for (span = 0; span < n; ++span) {
        for (begin = 0; begin < n-span-1; ++begin) {
            end = span + 1;
            ind = (begin*n+end)*ntags;
            for (split = 0; split < span+1; ++split) {
                l = &(score[(begin*n+split)*ntags]);
                r = &(score[((begin+split+1)*n+(span-split))*ntags]);
                for (i = 0; i < self->nbinaries; ++i) {
                    parent = self->binaries[i]->parent;
                    lchild = self->binaries[i]->lchild;
                    rchild = self->binaries[i]->rchild;
                    lp = l[lchild];
                    rp = r[rchild];
                    if (lp <= 0.0 && rp <= 0.0) {
                        prob = self->binaries[i]->value;
                        if (prob <= 0.0) {
                            p = lp + rp + prob;
                            tmp = score[ind+parent];
                            if (tmp > 0 || p > tmp) {
                                score[ind+parent] = p;

                                list = PyList_New(2);
                                PyList_SetItem(list, 0, Py_BuildValue("iiii", begin, split, lchild, 0));
                                PyList_SetItem(list, 1, Py_BuildValue("iiii", begin+split+1, span-split, rchild, 0));
                                Py_DECREF(back[ind+parent]);
                                back[ind+parent] = list;
                            }
                        }
                    }
                }
            }
            update_unaries(n, ntags, begin, end, self->nunaries, self->unaries, score, back, theta, 1);
        }
    }

    Py_DECREF(score_array);
    Py_DECREF(back_array);

    Py_INCREF(Py_None);
    return Py_None;
}
コード例 #2
0
ファイル: _turnstile.c プロジェクト: jeffgertler/bart
static PyObject
*turnstile_period_search (PyObject *self, PyObject *args)
{
    int i, j;

    int nperiods;
    double min_period, max_period,  amp, var;
    PyObject *datasets;

    // Parse the input arguments.
    if (!PyArg_ParseTuple(args, "Oddidd", &datasets, &min_period,
                          &max_period, &nperiods, &amp, &var))
        return NULL;

    // Parse and extract the necessary information from the datasets.
    if (!PyList_Check(datasets)) {
        PyErr_SetString(PyExc_TypeError, "Expected a list.");
        return NULL;
    }
    int max_sets, nsets = (int)PyList_Size(datasets),
        *ndata = malloc(nsets * sizeof(int));
    double **time = malloc(nsets * sizeof(double*)),
           **flux = malloc(nsets * sizeof(double*)),
           **ferr = malloc(nsets * sizeof(double*));
    PyObject *timeobj = NULL, *fluxobj = NULL, *ferrobj = NULL;
    PyArrayObject *timearray = NULL, *fluxarray = NULL, *ferrarray = NULL;

    for (max_sets = 0; max_sets < nsets; ++max_sets) {
        // Get the dataset.
        PyObject *ds = PyList_GetItem(datasets, max_sets);
        if (ds == NULL) goto fail;

        // Access the attributes that we need.
        timeobj = PyObject_GetAttrString(ds, "time");
        fluxobj = PyObject_GetAttrString(ds, "flux");
        ferrobj = PyObject_GetAttrString(ds, "ferr");

        // Clean up properly if anything went wrong.
        if (timeobj == NULL || fluxobj == NULL || ferrobj == NULL)
            goto ds_fail;

        // Parse the objects as numpy arrays.
        timearray = PARSE_ARRAY(timeobj),
        fluxarray = PARSE_ARRAY(fluxobj),
        ferrarray = PARSE_ARRAY(ferrobj);

        // Clean up properly if anything went wrong.
        if (timearray == NULL || fluxarray == NULL || ferrarray == NULL)
            goto ds_fail;

        // Figure out the size of the dataset and allocate the needed memory.
        ndata[max_sets] = (int)PyArray_DIM(timearray, 0);
        if (PyArray_DIM(fluxarray, 0) != ndata[max_sets] ||
            PyArray_DIM(ferrarray, 0) != ndata[max_sets]) {
            PyErr_SetString(PyExc_ValueError, "Dimension mismatch.");
            goto ds_fail;
        }
        time[max_sets] = malloc(ndata[max_sets] * sizeof(double));
        flux[max_sets] = malloc(ndata[max_sets] * sizeof(double));
        ferr[max_sets] = malloc(ndata[max_sets] * sizeof(double));

        // Copy the data over.
        double *t =  PyArray_DATA(timearray),
               *f =  PyArray_DATA(fluxarray),
               *fe =  PyArray_DATA(ferrarray);
        for (i = 0; i < ndata[max_sets]; ++i) {
            time[max_sets][i] = t[i];
            flux[max_sets][i] = f[i];
            ferr[max_sets][i] = fe[i];
        }

        // Reference counting.
        Py_DECREF(timeobj);
        Py_DECREF(fluxobj);
        Py_DECREF(ferrobj);
        Py_DECREF(timearray);
        Py_DECREF(fluxarray);
        Py_DECREF(ferrarray);
    }

    int *nepochs = NULL;
    double *periods = NULL, **epochs = NULL, **depths = NULL, **dvar = NULL;
    turnstile (nsets, ndata, time, flux, ferr, amp, var,
               min_period, max_period, nperiods,
               &nepochs, &periods, &epochs, &depths, &dvar);

    // Construct the output period array.
    npy_intp d1[1] = {nperiods};
    PyObject *periodarray = PyArray_SimpleNewFromData(1, d1, NPY_DOUBLE,
                                                      periods);
    if (periodarray == NULL) {
        Py_XDECREF(periodarray);
        goto fail;
    }

    // Construct the lists of epoch, depth and depth uncertainty arrays.
    PyObject *epochlist = PyList_New(nperiods),
             *depthlist = PyList_New(nperiods),
             *dvarlist = PyList_New(nperiods);
    if (epochlist == NULL || depthlist == NULL || dvarlist == NULL) {
        Py_DECREF(periodarray);
        Py_XDECREF(epochlist);
        Py_XDECREF(depthlist);
        Py_XDECREF(dvarlist);
        goto fail;
    }
    for (i = 0; i < nperiods; ++i) {
        npy_intp dim[1] = {nepochs[i]};
        PyObject *ea = PyArray_SimpleNewFromData(1, dim, NPY_DOUBLE, epochs[i]),
                 *da = PyArray_SimpleNewFromData(1, dim, NPY_DOUBLE, depths[i]),
                 *va = PyArray_SimpleNewFromData(1, dim, NPY_DOUBLE, dvar[i]);
        if (ea == NULL || da == NULL || va == NULL) {
            Py_DECREF(periodarray);
            Py_DECREF(epochlist);
            Py_DECREF(depthlist);
            Py_DECREF(dvarlist);
            Py_XDECREF(ea);
            Py_XDECREF(da);
            Py_XDECREF(va);
            goto fail;
        }
        int info = PyList_SetItem(epochlist, i, ea)
                 + PyList_SetItem(depthlist, i, da)
                 + PyList_SetItem(dvarlist, i, va);
        if (info != 0) {
            Py_DECREF(periodarray);
            Py_DECREF(epochlist);
            Py_DECREF(depthlist);
            Py_DECREF(dvarlist);
            Py_XDECREF(ea);
            Py_XDECREF(da);
            Py_XDECREF(va);
            goto fail;
        }
    }

    return Py_BuildValue("OOOO", periodarray, epochlist, depthlist, dvarlist);

ds_fail:

    Py_XDECREF(timeobj);
    Py_XDECREF(fluxobj);
    Py_XDECREF(ferrobj);
    Py_XDECREF(timearray);
    Py_XDECREF(fluxarray);
    Py_XDECREF(ferrarray);

fail:

    for (j = 0; j < max_sets; ++j) {
        free(time[j]);
        free(flux[j]);
        free(ferr[j]);
    }
    free(time);
    free(flux);
    free(ferr);
    return NULL;
}