// Static helper functions
// =============================================================================
double * Epetra_NumPySerialSymDenseMatrix::getArray(PyObject * pyObject)
{
  // If tmp_array is NULL, build a PyArrayObject from the pyObject
  if (!tmp_array)
  {
    // If pyObject is an int, then emulate an int-int constructor
    if (PyInt_Check(pyObject))
    {
      int numRows = (int) PyInt_AsLong(pyObject);
      npy_intp dimensions[ ] = {numRows, numRows};
      tmp_array = (PyArrayObject *)
	PyArray_SimpleNew(2,dimensions,NPY_DOUBLE);
    }
    // If pyObject is not a bool nor an int, try to build a
    // contiguous 2D PyArrayObject from the pyObject
    else
    {
      // This function returns a borrowed ptr: no DECREF
      PyArray_Descr * dtype = 
	PyArray_DescrFromType(NPY_DOUBLE);
      tmp_array = (PyArrayObject *) PyArray_FromAny(pyObject, dtype, 2, 2,
						    NPY_ARRAY_FARRAY, NULL);
    }
  }
  // If no array has been correctly constructed, clean up and throw a
  // PythonException
  if (!tmp_array)
  {
    cleanup();
    throw PythonException();
  }

  return (double*)(PyArray_DATA(tmp_array));
}
Exemplo n.º 2
0
void Tomography::appendSinogram(PyObject *sinogram, PyObject *angles)
{
	hlp::python::Ref sinogramTmp =
		PyArray_FromAny(sinogram, PyArray_DescrFromType(NPY_FLOAT), 2, 2, NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, nullptr);
	hlp::python::Ref anglesTmp = PyArray_FromAny(angles, PyArray_DescrFromType(NPY_FLOAT), 1, 1, NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, nullptr);
	PyArrayObject *sinogramPy = (PyArrayObject *)sinogramTmp.ptr;
	PyArrayObject *anglesPy = (PyArrayObject *)anglesTmp.ptr;

	const float *sinogramData = reinterpret_cast<float *>(PyArray_DATA(sinogramPy));
	const float *anglesData = reinterpret_cast<float *>(PyArray_DATA(anglesPy));

	ndim::pointer<const float, 2> sinogramPtr(sinogramData, ndim::getShape<2>(sinogramPy), ndim::getStrides<2>(sinogramPy));
	ndim::pointer<const float, 1> anglesPtr(anglesData, ndim::getShape<1>(anglesPy), ndim::getStrides<1>(anglesPy));

	tomo::Tomography::appendSinogram(sinogramPtr, anglesPtr);
}
Exemplo n.º 3
0
PyObject*
PyThunk_AsUnevaluatedArray(PyObject* thunk) {
    if (PyThunk_CheckExact(thunk)) {
        if (((PyThunkObject*)thunk)->storage == NULL) {
            ((PyThunkObject*)thunk)->storage = (PyArrayObject*)PyArray_EMPTY(1, (npy_intp[1]) { ((PyThunkObject*)thunk)->cardinality }, ((PyThunkObject*)thunk)->type, 0);
        }
        return (PyObject*)((PyThunkObject*)thunk)->storage;
    }
    return PyArray_FromAny(thunk, NULL, 0, 0, 0, NULL);
}
Exemplo n.º 4
0
PyObject*
PyThunk_AsArray(PyObject* thunk) {
    if (PyThunk_CheckExact(thunk)) {
        if (PyThunk_Evaluate((PyThunkObject*)thunk) == NULL) {
            return NULL;
        }
        return (PyObject*)((PyThunkObject*)thunk)->storage;
    }
    return PyArray_FromAny(thunk, NULL, 0, 0, 0, NULL);
}
Exemplo n.º 5
0
hlp::python::Ref ndimToNumpy(ndim::pointer<const float, 2> data) {
	hlp::python::Gil gil;
	hlp::unused(gil);

	hlp::python::Ref containerRef = ndim::makePyArrayRef(data, gil);

	hlp::python::Ref result = PyArray_FromAny(containerRef.ptr, PyArray_DescrFromType(NPY_FLOAT), 2, 2, NPY_ARRAY_ENSURECOPY, nullptr);

	return result;
}
Exemplo n.º 6
0
PyObject*
PyThunk_AsTypeArray(PyObject *thunk) {
    if (PyThunk_CheckExact(thunk)) {
        if (PyThunk_IsEvaluated((PyThunkObject*)thunk)) {
            return (PyObject*) ((PyThunkObject*)thunk)->storage;
        }
        PyObject *type = (_thunk_arrays[((PyThunkObject*)thunk)->type]);
        return type;
    }
    return PyArray_FromAny(thunk, NULL, 0, 0, 0, NULL);
}
Exemplo n.º 7
0
/*NUMPY_API*/
NPY_NO_EXPORT int
PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object)
{
    PyArrayObject *src;
    PyObject *r;
    int ret;

    /*
     * Special code to mimic Numeric behavior for
     * character arrays.
     */
    if (dest->descr->type == PyArray_CHARLTR && dest->nd > 0 \
        && PyString_Check(src_object)) {
        intp n_new, n_old;
        char *new_string;
        PyObject *tmp;

        n_new = dest->dimensions[dest->nd-1];
        n_old = PyString_Size(src_object);
        if (n_new > n_old) {
            new_string = (char *)malloc(n_new);
            memmove(new_string, PyString_AS_STRING(src_object), n_old);
            memset(new_string + n_old, ' ', n_new - n_old);
            tmp = PyString_FromStringAndSize(new_string, n_new);
            free(new_string);
            src_object = tmp;
        }
    }

    if (PyArray_Check(src_object)) {
        src = (PyArrayObject *)src_object;
        Py_INCREF(src);
    }
    else if (!PyArray_IsScalar(src_object, Generic) &&
             PyArray_HasArrayInterface(src_object, r)) {
        src = (PyArrayObject *)r;
    }
    else {
        PyArray_Descr* dtype;
        dtype = dest->descr;
        Py_INCREF(dtype);
        src = (PyArrayObject *)PyArray_FromAny(src_object, dtype, 0,
                                               dest->nd,
                                               FORTRAN_IF(dest),
                                               NULL);
    }
    if (src == NULL) {
        return -1;
    }

    ret = PyArray_MoveInto(dest, src);
    Py_DECREF(src);
    return ret;
}
Exemplo n.º 8
0
void Tomography::setReconstruction(PyObject *reconstruction)
{
	hlp::python::Ref reconstructionTmp =
		PyArray_FromAny(reconstruction, PyArray_DescrFromType(NPY_FLOAT), 2, 2, NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, nullptr);
	PyArrayObject *reconstructionPy = (PyArrayObject *)reconstructionTmp.ptr;

	const float *reconstructionData = reinterpret_cast<float *>(PyArray_DATA(reconstructionPy));

	ndim::pointer<const float, 2> reconstructionPtr(reconstructionData, ndim::getShape<2>(reconstructionPy), ndim::getStrides<2>(reconstructionPy));

	tomo::Tomography::setReconstruction(reconstructionPtr);
}
Exemplo n.º 9
0
 /* Copy constructor... ensure clone */
 numpy_boost(const self_type &other) throw() :
   super(NULL, std::vector<typename super::index>(NDims, 0)),
   array(NULL)
 {
   PyObject* cloned = PyArray_FromAny(
       other.array,
       NULL, // dtype = NULL, obtain from array
       0, 0, // just use whatever depth is already there
       NPY_C_CONTIGUOUS|NPY_ENSURECOPY,
       NULL );
   //Py_INCREF(other.array);
   init_from_array(cloned);
 }
Exemplo n.º 10
0
void Tomography::setOpenBeam(PyObject *openBeam)
{
	hlp::python::Ref openBeamTmp =
		PyArray_FromAny(openBeam, PyArray_DescrFromType(NPY_FLOAT), 1, 1, NPY_ARRAY_ALIGNED | NPY_ARRAY_FORCECAST, nullptr);

	PyArrayObject *openBeamPy = (PyArrayObject *)openBeamTmp.ptr;

	const float *openBeamData = reinterpret_cast<float *>(PyArray_DATA(openBeamPy));

	ndim::pointer<const float, 1> openBeamPtr(openBeamData, ndim::getShape<1>(openBeamPy), ndim::getStrides<1>(openBeamPy));

	tomo::Tomography::setOpenBeam(openBeamPtr);
}
Exemplo n.º 11
0
static int
map_inc(PyArrayMapIterObject *mit, PyObject *op)
{
    PyObject *arr = NULL;
    PyArrayIterObject *it;
    int index;
    PyArray_Descr *descr;

    /* Unbound Map Iterator */
    if (mit->ait == NULL) {
        return -1;
    }
    descr = mit->ait->ao->descr;
    Py_INCREF(descr);
    arr = PyArray_FromAny(op, descr, 0, 0, NPY_FORCECAST, NULL);
    if (arr == NULL) {
        return -1;
    }
    if ((mit->subspace != NULL) && (mit->consec)) {
        if (mit->iteraxes[0] > 0) {  /* then we need to swap */
            _swap_axes(mit, (PyArrayObject **)&arr, 0);
            if (arr == NULL) {
                return -1;
            }
        }
    }

    /* Be sure values array is "broadcastable"
       to shape of mit->dimensions, mit->nd */

    if ((it = (PyArrayIterObject *)\
         PyArray_BroadcastToShape(arr, mit->dimensions, mit->nd))==NULL) {
        Py_DECREF(arr);
        return -1;
    }

    index = mit->size;

    PyArray_MapIterReset(mit);

    while(index--) {
        memmove(mit->dataptr, it->dataptr, PyArray_ITEMSIZE(arr));
        	*(double*)(mit->dataptr) = *(double*)(mit->dataptr) + *(double*)(it->dataptr); 
        	
        PyArray_MapIterNext(mit);
        PyArray_ITER_NEXT(it);
    }
    Py_DECREF(arr);
    Py_DECREF(it);
    return 0;
}
Exemplo n.º 12
0
static PyObject* spherematch_kdtree_get_data(PyObject* self, PyObject* args) {
  PyArrayObject* pyX;
  double* X;
  PyObject* rtn;
  npy_intp dims[2];
  long i;
  kdtree_t* kd;
  int k, D, N;
  //npy_int* I;
  npy_uint32* I;
  PyObject* pyO;
  PyObject* pyI;
  // this is the type returned by kdtree_rangesearch
  PyArray_Descr* dtype = PyArray_DescrFromType(NPY_UINT32);
  int req = NPY_C_CONTIGUOUS | NPY_ALIGNED | NPY_NOTSWAPPED | NPY_ELEMENTSTRIDES;

  if (!PyArg_ParseTuple(args, "lO", &i, &pyO)) {
    PyErr_SetString(PyExc_ValueError, "need two args: kdtree identifier (int), index array (numpy array of ints)");
    return NULL;
  }
  // Nasty!
  kd = (kdtree_t*)i;
  D = kd->ndim;

  Py_INCREF(dtype);
  pyI = PyArray_FromAny(pyO, dtype, 1, 1, req, NULL);
  if (!pyI) {
    PyErr_SetString(PyExc_ValueError, "Failed to convert index array to np array of int");
    Py_XDECREF(dtype);
    return NULL;
  }
  N = (int)PyArray_DIM(pyI, 0);

  dims[0] = N;
  dims[1] = D;

  pyX = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_DOUBLE);
  X = PyArray_DATA(pyX);
  I = PyArray_DATA(pyI);

  for (k=0; k<N; k++) {
    kdtree_copy_data_double(kd, I[k], 1, X);
    X += D;
  }
  Py_DECREF(pyI);
  Py_DECREF(dtype);
  rtn = Py_BuildValue("O", pyX);
  Py_DECREF(pyX);
  return rtn;
}
Exemplo n.º 13
0
/*NUMPY_API
 *
 * Useful to pass as converter function for O& processing in PyArgs_ParseTuple.
 *
 * This conversion function can be used with the "O&" argument for
 * PyArg_ParseTuple.  It will immediately return an object of array type
 * or will convert to a CARRAY any other object.
 *
 * If you use PyArray_Converter, you must DECREF the array when finished
 * as you get a new reference to it.
 */
NPY_NO_EXPORT int
PyArray_Converter(PyObject *object, PyObject **address)
{
    if (PyArray_Check(object)) {
        *address = object;
        Py_INCREF(object);
        return PY_SUCCEED;
    }
    else {
        *address = PyArray_FromAny(object, NULL, 0, 0, CARRAY, NULL);
        if (*address == NULL) {
            return PY_FAIL;
        }
        return PY_SUCCEED;
    }
}
Exemplo n.º 14
0
static PyObject* spherematch_kdtree_permute(PyObject* self, PyObject* args) {
  PyArrayObject* pyX;
  npy_int* X;
  PyObject* rtn;
  npy_intp dims[1];
  long i;
  kdtree_t* kd;
  long k, N;
  npy_int* I;
  PyObject* pyO;
  PyObject* pyI;
  PyArray_Descr* dtype = PyArray_DescrFromType(NPY_INT);
  int req = NPY_C_CONTIGUOUS | NPY_ALIGNED | NPY_NOTSWAPPED | NPY_ELEMENTSTRIDES;

  if (!PyArg_ParseTuple(args, "lO", &i, &pyO)) {
    PyErr_SetString(PyExc_ValueError, "need two args: kdtree identifier (int), index array (numpy array of ints)");
    return NULL;
  }
  // Nasty!
  kd = (kdtree_t*)i;

  Py_INCREF(dtype);
  pyI = PyArray_FromAny(pyO, dtype, 1, 1, req, NULL);
  if (!pyI) {
    PyErr_SetString(PyExc_ValueError, "Failed to convert index array to np array of int");
    Py_XDECREF(dtype);
    return NULL;
  }
  N = PyArray_DIM(pyI, 0);

  dims[0] = N;

  pyX = (PyArrayObject*)PyArray_SimpleNew(1, dims, NPY_INT);
  X = PyArray_DATA(pyX);
  I = PyArray_DATA(pyI);

  for (k=0; k<N; k++) {
    npy_int ii = I[k];
    //printf("Permute: ii=%i\n", ii);
    X[k] = kdtree_permute(kd, ii);
  }
  Py_DECREF(pyI);
  Py_DECREF(dtype);
  rtn = Py_BuildValue("O", pyX);
  Py_DECREF(pyX);
  return rtn;
}
// Static helper functions
// =============================================================================
int * Epetra_NumPyIntSerialDenseMatrix::getArray(PyObject * pyObject)
{
  // If tmp_array is NULL, build a two-dimensional PyArrayObject from the pyObject
  if (tmp_array == NULL)
  {
    // This NumPy function returns a borrowed pointer: do not DECREF
    PyArray_Descr * dtype = PyArray_DescrFromType(NPY_INT);
    tmp_array = (PyArrayObject *)
      PyArray_FromAny(pyObject,dtype,2,2,NPY_ARRAY_FARRAY,NULL);
  }

  // If this fails, clean up and throw a PythonException
  if (!tmp_array)
  {
    cleanup();
    throw PythonException();
  }

  return (int*)(PyArray_DATA(tmp_array));
}
Exemplo n.º 16
0
//Array constructor function. Can be used to limit the dimensions
//of the resulting object. 
//dimarray(array data,dtype description,[max_dimensions] (default: 1), [min_dimensions] (default: 0))
static PyObject *
numpy_dimarray(PyObject *self, PyObject *args)
{
    PyObject * seq;
    PyArray_Descr * dtype = NULL;
    int max_dim = 1;
    int min_dim = 0;

    if(!PyArg_ParseTuple(args,"O|O&ii",&seq,PyArray_DescrConverter,(PyObject **) &dtype,&max_dim,&min_dim))
        return NULL;
   
    if(dtype == NULL)
    {
        if(!PyArray_DescrConverter((PyObject *) &PyBaseObject_Type, &dtype))
            return NULL;
    }
    
    //dtype has been increffed by PyArray_DescrConverter
    return (PyObject *) PyArray_FromAny(seq,dtype,min_dim,max_dim,0,NULL);
}
Exemplo n.º 17
0
Ref<> numpy_from_any(PyObject* op, PyArray_Descr* dtype, int min_rank, int max_rank, int requirements) {
  if (op==Py_None) // PyArray_FromAny silently converts None to a singleton nan, which is not cool
    throw TypeError("Expected array, got None");

  // Perform the conversion
  PyObject* const array = PyArray_FromAny(op,dtype,0,0,requirements,0);
  if (!array)
    throw_python_error();

  // Numpy produces uninformative error messages on rank mismatch, so we roll our own.
  const int rank = PyArray_NDIM((PyArrayObject*)array);
  if (   (min_rank && rank<min_rank)
      || (max_rank && rank>max_rank))
    throw ValueError(min_rank==max_rank ? format("Expected array with rank %d, got rank %d",min_rank,rank)
                            : !min_rank ? format("Expected array with rank <= %d, got rank %d",max_rank,rank)
                            : !max_rank ? format("Expected array with rank >= %d, got rank %d",min_rank,rank)
                            : format("Expected array with %d <= rank <= %d, got rank %d",min_rank,max_rank,rank));

  // Success!
  return steal_ref(*array);
}
Exemplo n.º 18
0
PyObject*
PyThunk_FromArray(PyObject *unused, PyObject *input) {
    register PyThunkObject *thunk;
    (void) unused;
    input = PyArray_FromAny(input, NULL, 0, 0, NPY_ARRAY_ENSURECOPY, NULL);
    if (input == NULL || !PyArray_CheckExact(input)) {
        PyErr_SetString(PyExc_TypeError, "Expected a NumPy array as parameter.");
        return NULL;
    }
    thunk = (PyThunkObject *)PyObject_MALLOC(sizeof(PyThunkObject));
    if (thunk == NULL)
        return PyErr_NoMemory();
    PyObject_Init((PyObject*)thunk, &PyThunk_Type);
    thunk->storage = (PyArrayObject*) input;
    thunk->evaluated = true;
    thunk->operation = NULL;
    thunk->cardinality =  PyArray_SIZE(thunk->storage);
    thunk->type = PyArray_TYPE(thunk->storage);
    thunk->options = THUNK_CARDINALITY_EXACT;
    thunk->blockmask = NULL;
    return (PyObject*)thunk;
}
Exemplo n.º 19
0
/*
 * Converts a Python sequence into 'count' PyArrayObjects
 *
 * seq       - Input Python object, usually a tuple but any sequence works.
 * op        - Where the arrays are placed.
 * count     - How many arrays there should be (errors if it doesn't match).
 * paramname - The name of the parameter that produced 'seq'.
 */
static int sequence_to_arrays(PyObject *seq,
                                PyArrayObject **op, int count,
                                char *paramname)
{
    int i;

    if (!PySequence_Check(seq) || PySequence_Size(seq) != count) {
        PyErr_Format(PyExc_ValueError,
                "parameter %s must be a sequence of length %d",
                paramname, count);
        return -1;
    }

    for (i = 0; i < count; ++i) {
        PyObject *item = PySequence_GetItem(seq, i);
        if (item == NULL) {
            while (--i >= 0) {
                Py_DECREF(op[i]);
                op[i] = NULL;
            }
            return -1;
        }

        op[i] = (PyArrayObject *)PyArray_FromAny(item, NULL, 0, 0, 0, NULL);
        if (op[i] == NULL) {
            while (--i >= 0) {
                Py_DECREF(op[i]);
                op[i] = NULL;
            }
            Py_DECREF(item);
            return -1;
        }

        Py_DECREF(item);
    }

    return 0;
}
Exemplo n.º 20
0
PyArrayObject *get_pyarray(PyObject *objInSound)
{
    // Convert to actual PyArray with proper type
    // PyArrayObject *inSound = (PyArrayObject *) NA_InputArray(objInSound, tFloat32, NUM_C_ARRAY);
    PyArrayObject *inSound = (PyArrayObject*) PyArray_FromAny(objInSound, PyArray_DescrFromType(PyArray_FLOAT), 1, 2, NPY_C_CONTIGUOUS, NULL);

    // Check that everything looks good
    if (!inSound)
    {
        Py_XDECREF(inSound);
        PyErr_Format(ActionError, "couldn't convert array to PyArrayObject.");
        return NULL;
    }

    if (inSound->nd != 1 && inSound->nd != 2)
    {
        Py_XDECREF(inSound);
        PyErr_Format(ActionError, "sound arrays must have 1 (mono) or 2 (stereo) dimensions.");
        return NULL;
    }

    return inSound;
}
Exemplo n.º 21
0
PyObject* 
PyThunk_Evaluate(PyThunkObject *thunk) {
	if (PyThunk_IsEvaluated(thunk)) {
		Py_RETURN_NONE;
	}
	if (PyThunkUnaryPipeline_CheckExact(thunk->operation) || PyThunkBinaryPipeline_CheckExact(thunk->operation)) {
        size_t blocks = PyThunk_BlockCount(thunk);
		for(size_t i = 0; i < blocks; i++) {
			PyThunk_EvaluateBlock(thunk, i);
		}
	} else if (PyThunkUnaryFunction_CheckExact(thunk->operation)) {
        PyArrayObject *arrays[NPY_MAXARGS];
		PyThunkOperation_UnaryFunction *operation = (PyThunkOperation_UnaryFunction*)thunk->operation;
		UnaryFunction function = (UnaryFunction)(operation->function);
        if (PyThunk_CheckExact(operation->left)) {
            PyThunk_Evaluate((PyThunkObject*)operation->left);
        }
		if (thunk->storage == NULL) {
			// no storage, have to obtain storage from somewhere
			if (PyThunk_MatchingStorage(thunk, operation->left)) {
				// the referenced object has only one reference (from here)
				// this means it will get destroyed after this operation
				// since it has the same type, we can use its storage directly
				thunk->storage = ((PyThunkObject*)operation->left)->storage;
                Py_INCREF(thunk->storage);
			} else {
				// we have to create storage for the operation
				thunk->storage = (PyArrayObject*)PyArray_EMPTY(1, (npy_intp[1]) { thunk->cardinality }, thunk->type, 0);
			}
		}
        arrays[0] = (PyArrayObject*) PyThunk_AsUnevaluatedArray(operation->left);
        arrays[1] = thunk->storage;
        function(arrays);
        thunk->evaluated = true;
        PyThunk_FinalizeEvaluation(thunk);
	} else if (PyThunkBinaryFunction_CheckExact(thunk->operation)) {
        PyArrayObject *arrays[NPY_MAXARGS];
		PyThunkOperation_BinaryFunction *operation = (PyThunkOperation_BinaryFunction*)thunk->operation;
		BinaryFunction function = (BinaryFunction)(operation->function);
        if (PyThunk_CheckExact(operation->left)) {
            PyThunk_Evaluate((PyThunkObject*)operation->left);
        }
        if (PyThunk_CheckExact(operation->right)) {
            PyThunk_Evaluate((PyThunkObject*)operation->right);
        }
		if (thunk->storage == NULL) {
			// no storage, have to obtain storage from somewhere
			if (PyThunk_MatchingStorage(thunk, operation->left)) {
				thunk->storage = ((PyThunkObject*)operation->left)->storage;
                Py_INCREF(thunk->storage);
			} else if (PyThunk_MatchingStorage(thunk, operation->right)) {
				thunk->storage = ((PyThunkObject*)operation->right)->storage;
                Py_INCREF(thunk->storage);
			} else {
				thunk->storage = (PyArrayObject*)PyArray_EMPTY(1, (npy_intp[1]) { thunk->cardinality }, thunk->type, 0);
			}
		}
        arrays[0] = (PyArrayObject*) PyThunk_AsUnevaluatedArray(operation->left);
        arrays[1] = (PyArrayObject*) PyThunk_AsUnevaluatedArray(operation->right);
        arrays[2] = thunk->storage;
		function(arrays);
        thunk->evaluated = true;
        PyThunk_FinalizeEvaluation(thunk);
	} else if (PyThunkAggregationPipeline_CheckExact(thunk->operation)) {
        PyThunkOperation_AggregationPipeline *operation = (PyThunkOperation_AggregationPipeline*)thunk->operation;
        size_t blocks = PyThunk_BlockCount((PyThunkObject*)operation->left);
        PyObject *list = PyList_New(blocks);
        PyObject *result = NULL;
        PyArrayObject *array;
        for(size_t i = 0; i < blocks; i++) {
            size_t start = i * BLOCK_SIZE;
            size_t end = min((i + 1) * BLOCK_SIZE, ((PyThunkObject*)operation->left)->cardinality);

            PyThunk_EvaluateBlock((PyThunkObject*)operation->left, i);
            PyReduceFunc_ExecuteBlock(operation->function, ((PyThunkObject*)operation->left)->storage, &result, start, end);
            if (result == NULL) {
                return NULL;
            }
            PyList_SetItem(list, i, result);
        }
        array = (PyArrayObject*) PyArray_FromAny(list, NULL, 0, 0, 0, NULL);
        PyReduceFunc_Execute(operation->function, array, &result);
        Py_DECREF(array);
        Py_DECREF(list);

        thunk->storage = (PyArrayObject*) PyArray_FromAny(result, NULL, 0, 0, 0, NULL);
    }
	Py_RETURN_NONE;
}
Exemplo n.º 22
0
/*
 * Converts a Python input into a non-normalized list of holidays.
 *
 * IMPORTANT: This function can't do the normalization, because it doesn't
 *            know the weekmask. You must call 'normalize_holiday_list'
 *            on the result before using it.
 */
NPY_NO_EXPORT int
PyArray_HolidaysConverter(PyObject *dates_in, npy_holidayslist *holidays)
{
    PyArrayObject *dates = NULL;
    PyArray_Descr *date_dtype = NULL;
    npy_intp count;

    /* Make 'dates' into an array */
    if (PyArray_Check(dates_in)) {
        dates = (PyArrayObject *)dates_in;
        Py_INCREF(dates);
    }
    else {
        PyArray_Descr *datetime_dtype;

        /* Use the datetime dtype with generic units so it fills it in */
        datetime_dtype = PyArray_DescrFromType(NPY_DATETIME);
        if (datetime_dtype == NULL) {
            goto fail;
        }

        /* This steals the datetime_dtype reference */
        dates = (PyArrayObject *)PyArray_FromAny(dates_in, datetime_dtype,
                                                0, 0, 0, dates_in);
        if (dates == NULL) {
            goto fail;
        }
    }

    date_dtype = create_datetime_dtype_with_unit(NPY_DATETIME, NPY_FR_D);
    if (date_dtype == NULL) {
        goto fail;
    }

    if (!PyArray_CanCastTypeTo(PyArray_DESCR(dates),
                                    date_dtype, NPY_SAFE_CASTING)) {
        PyErr_SetString(PyExc_ValueError, "Cannot safely convert "
                        "provided holidays input into an array of dates");
        goto fail;
    }
    if (PyArray_NDIM(dates) != 1) {
        PyErr_SetString(PyExc_ValueError, "holidays must be a provided "
                        "as a one-dimensional array");
        goto fail;
    }

    /* Allocate the memory for the dates */
    count = PyArray_DIM(dates, 0);
    holidays->begin = PyArray_malloc(sizeof(npy_datetime) * count);
    if (holidays->begin == NULL) {
        PyErr_NoMemory();
        goto fail;
    }
    holidays->end = holidays->begin + count;

    /* Cast the data into a raw date array */
    if (PyArray_CastRawArrays(count,
                            PyArray_BYTES(dates), (char *)holidays->begin,
                            PyArray_STRIDE(dates, 0), sizeof(npy_datetime),
                            PyArray_DESCR(dates), date_dtype,
                            0) != NPY_SUCCEED) {
        goto fail;
    }

    Py_DECREF(dates);
    Py_DECREF(date_dtype);

    return 1;

fail:
    Py_XDECREF(dates);
    Py_XDECREF(date_dtype);
    return 0;
}
Exemplo n.º 23
0
/*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;
}
Exemplo n.º 24
0
/* unravel_index implementation - see add_newdocs.py */
NPY_NO_EXPORT PyObject *
arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
{
    PyObject *indices0 = NULL, *ret_tuple = NULL;
    PyArrayObject *ret_arr = NULL;
    PyArrayObject *indices = NULL;
    PyArray_Descr *dtype = NULL;
    PyArray_Dims dimensions={0,0};
    NPY_ORDER order = NPY_CORDER;
    npy_intp unravel_size;

    NpyIter *iter = NULL;
    int i, ret_ndim;
    npy_intp ret_dims[NPY_MAXDIMS], ret_strides[NPY_MAXDIMS];

    char *kwlist[] = {"indices", "dims", "order", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|O&:unravel_index",
                    kwlist,
                    &indices0,
                    PyArray_IntpConverter, &dimensions,
                    PyArray_OrderConverter, &order)) {
        goto fail;
    }

    if (dimensions.len == 0) {
        PyErr_SetString(PyExc_ValueError,
                "dims must have at least one value");
        goto fail;
    }

    unravel_size = PyArray_MultiplyList(dimensions.ptr, dimensions.len);

    if (!PyArray_Check(indices0)) {
        indices = (PyArrayObject*)PyArray_FromAny(indices0,
                                                    NULL, 0, 0, 0, NULL);
        if (indices == NULL) {
            goto fail;
        }
    }
    else {
        indices = (PyArrayObject *)indices0;
        Py_INCREF(indices);
    }

    dtype = PyArray_DescrFromType(NPY_INTP);
    if (dtype == NULL) {
        goto fail;
    }

    iter = NpyIter_New(indices, NPY_ITER_READONLY|
                                NPY_ITER_ALIGNED|
                                NPY_ITER_BUFFERED|
                                NPY_ITER_ZEROSIZE_OK|
                                NPY_ITER_DONT_NEGATE_STRIDES|
                                NPY_ITER_MULTI_INDEX,
                                NPY_KEEPORDER, NPY_SAME_KIND_CASTING,
                                dtype);
    if (iter == NULL) {
        goto fail;
    }

    /*
     * Create the return array with a layout compatible with the indices
     * and with a dimension added to the end for the multi-index
     */
    ret_ndim = PyArray_NDIM(indices) + 1;
    if (NpyIter_GetShape(iter, ret_dims) != NPY_SUCCEED) {
        goto fail;
    }
    ret_dims[ret_ndim-1] = dimensions.len;
    if (NpyIter_CreateCompatibleStrides(iter,
                dimensions.len*sizeof(npy_intp), ret_strides) != NPY_SUCCEED) {
        goto fail;
    }
    ret_strides[ret_ndim-1] = sizeof(npy_intp);

    /* Remove the multi-index and inner loop */
    if (NpyIter_RemoveMultiIndex(iter) != NPY_SUCCEED) {
        goto fail;
    }
    if (NpyIter_EnableExternalLoop(iter) != NPY_SUCCEED) {
        goto fail;
    }

    ret_arr = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype,
                            ret_ndim, ret_dims, ret_strides, NULL, 0, NULL);
    dtype = NULL;
    if (ret_arr == NULL) {
        goto fail;
    }

    if (order == NPY_CORDER) {
        if (NpyIter_GetIterSize(iter) != 0) {
            NpyIter_IterNextFunc *iternext;
            char **dataptr;
            npy_intp *strides;
            npy_intp *countptr, count;
            npy_intp *coordsptr = (npy_intp *)PyArray_DATA(ret_arr);

            iternext = NpyIter_GetIterNext(iter, NULL);
            if (iternext == NULL) {
                goto fail;
            }
            dataptr = NpyIter_GetDataPtrArray(iter);
            strides = NpyIter_GetInnerStrideArray(iter);
            countptr = NpyIter_GetInnerLoopSizePtr(iter);

            do {
                count = *countptr;
                if (unravel_index_loop_corder(dimensions.len, dimensions.ptr,
                            unravel_size, count, *dataptr, *strides,
                            coordsptr) != NPY_SUCCEED) {
                    goto fail;
                }
                coordsptr += count*dimensions.len;
            } while(iternext(iter));
        }
    }
    else if (order == NPY_FORTRANORDER) {
        if (NpyIter_GetIterSize(iter) != 0) {
            NpyIter_IterNextFunc *iternext;
            char **dataptr;
            npy_intp *strides;
            npy_intp *countptr, count;
            npy_intp *coordsptr = (npy_intp *)PyArray_DATA(ret_arr);

            iternext = NpyIter_GetIterNext(iter, NULL);
            if (iternext == NULL) {
                goto fail;
            }
            dataptr = NpyIter_GetDataPtrArray(iter);
            strides = NpyIter_GetInnerStrideArray(iter);
            countptr = NpyIter_GetInnerLoopSizePtr(iter);

            do {
                count = *countptr;
                if (unravel_index_loop_forder(dimensions.len, dimensions.ptr,
                            unravel_size, count, *dataptr, *strides,
                            coordsptr) != NPY_SUCCEED) {
                    goto fail;
                }
                coordsptr += count*dimensions.len;
            } while(iternext(iter));
        }
    }
    else {
        PyErr_SetString(PyExc_ValueError,
                        "only 'C' or 'F' order is permitted");
        goto fail;
    }

    /* Now make a tuple of views, one per index */
    ret_tuple = PyTuple_New(dimensions.len);
    if (ret_tuple == NULL) {
        goto fail;
    }
    for (i = 0; i < dimensions.len; ++i) {
        PyArrayObject *view;

        view = (PyArrayObject *)PyArray_New(&PyArray_Type, ret_ndim-1,
                                ret_dims, NPY_INTP,
                                ret_strides,
                                PyArray_BYTES(ret_arr) + i*sizeof(npy_intp),
                                0, NPY_ARRAY_WRITEABLE, NULL);
        if (view == NULL) {
            goto fail;
        }
        Py_INCREF(ret_arr);
        if (PyArray_SetBaseObject(view, (PyObject *)ret_arr) < 0) {
            Py_DECREF(view);
            goto fail;
        }
        PyTuple_SET_ITEM(ret_tuple, i, PyArray_Return(view));
    }

    Py_DECREF(ret_arr);
    Py_XDECREF(indices);
    PyDimMem_FREE(dimensions.ptr);
    NpyIter_Deallocate(iter);

    return ret_tuple;

fail:
    Py_XDECREF(ret_tuple);
    Py_XDECREF(ret_arr);
    Py_XDECREF(dtype);
    Py_XDECREF(indices);
    PyDimMem_FREE(dimensions.ptr);
    NpyIter_Deallocate(iter);
    return NULL;
}
Exemplo n.º 25
0
extern
PyArrayObject* array_from_pyobj(const int type_num,
                                npy_intp *dims,
                                const int rank,
                                const int intent,
                                PyObject *obj) {
    /* Note about reference counting
       -----------------------------
       If the caller returns the array to Python, it must be done with
       Py_BuildValue("N",arr).
       Otherwise, if obj!=arr then the caller must call Py_DECREF(arr).

       Note on intent(cache,out,..)
       ---------------------
       Don't expect correct data when returning intent(cache) array.

    */
    char mess[200];
    PyArrayObject *arr = NULL;
    PyArray_Descr *descr;
    char typechar;
    int elsize;

    if ((intent & F2PY_INTENT_HIDE)
        || ((intent & F2PY_INTENT_CACHE) && (obj==Py_None))
        || ((intent & F2PY_OPTIONAL) && (obj==Py_None))
        ) {
        /* intent(cache), optional, intent(hide) */
        if (count_nonpos(rank,dims)) {
            int i;
            strcpy(mess, "failed to create intent(cache|hide)|optional array"
                   "-- must have defined dimensions but got (");
            for(i=0;i<rank;++i)
                sprintf(mess+strlen(mess),"%" NPY_INTP_FMT ",",dims[i]);
            strcat(mess, ")");
            PyErr_SetString(PyExc_ValueError,mess);
            return NULL;
        }
        arr = (PyArrayObject *)
            PyArray_New(&PyArray_Type, rank, dims, type_num,
                        NULL,NULL,0,
                        !(intent&F2PY_INTENT_C),
                        NULL);
        if (arr==NULL) return NULL;
        if (!(intent & F2PY_INTENT_CACHE))
            PyArray_FILLWBYTE(arr, 0);
        return arr;
    }

    descr = PyArray_DescrFromType(type_num);
    elsize = descr->elsize;
    typechar = descr->type;
    Py_DECREF(descr);
    if (PyArray_Check(obj)) {
        arr = (PyArrayObject *)obj;

        if (intent & F2PY_INTENT_CACHE) {
            /* intent(cache) */
            if (PyArray_ISONESEGMENT(arr)
                && PyArray_ITEMSIZE(arr)>=elsize) {
                if (check_and_fix_dimensions(arr,rank,dims)) {
                    return NULL; /*XXX: set exception */
                }
                if (intent & F2PY_INTENT_OUT)
                    Py_INCREF(arr);
                return arr;
            }
            strcpy(mess, "failed to initialize intent(cache) array");
            if (!PyArray_ISONESEGMENT(arr))
                strcat(mess, " -- input must be in one segment");
            if (PyArray_ITEMSIZE(arr)<elsize)
                sprintf(mess+strlen(mess),
                        " -- expected at least elsize=%d but got %" NPY_INTP_FMT,
                        elsize,
                        (npy_intp)PyArray_ITEMSIZE(arr)
                        );
            PyErr_SetString(PyExc_ValueError,mess);
            return NULL;
        }

        /* here we have always intent(in) or intent(inout) or intent(inplace) */

        if (check_and_fix_dimensions(arr,rank,dims)) {
            return NULL; /*XXX: set exception */
        }
	/*
	printf("intent alignement=%d\n", F2PY_GET_ALIGNMENT(intent));
	printf("alignement check=%d\n", F2PY_CHECK_ALIGNMENT(arr, intent));
	int i;
	for (i=1;i<=16;i++)
	  printf("i=%d isaligned=%d\n", i, ARRAY_ISALIGNED(arr, i));
	*/
        if ((! (intent & F2PY_INTENT_COPY))
            && PyArray_ITEMSIZE(arr)==elsize
            && ARRAY_ISCOMPATIBLE(arr,type_num)
	    && F2PY_CHECK_ALIGNMENT(arr, intent)
            ) {
            if ((intent & F2PY_INTENT_C)?PyArray_ISCARRAY(arr):PyArray_ISFARRAY(arr)) {
                if ((intent & F2PY_INTENT_OUT)) {
                    Py_INCREF(arr);
                }
                /* Returning input array */
                return arr;
            }
        }

        if (intent & F2PY_INTENT_INOUT) {
            strcpy(mess, "failed to initialize intent(inout) array");
            if ((intent & F2PY_INTENT_C) && !PyArray_ISCARRAY(arr))
                strcat(mess, " -- input not contiguous");
            if (!(intent & F2PY_INTENT_C) && !PyArray_ISFARRAY(arr))
                strcat(mess, " -- input not fortran contiguous");
            if (PyArray_ITEMSIZE(arr)!=elsize)
                sprintf(mess+strlen(mess),
                        " -- expected elsize=%d but got %" NPY_INTP_FMT,
                        elsize,
                        (npy_intp)PyArray_ITEMSIZE(arr)
                        );
            if (!(ARRAY_ISCOMPATIBLE(arr,type_num)))
                sprintf(mess+strlen(mess)," -- input '%c' not compatible to '%c'",
                        PyArray_DESCR(arr)->type,typechar);
	    if (!(F2PY_CHECK_ALIGNMENT(arr, intent)))
	      sprintf(mess+strlen(mess)," -- input not %d-aligned", F2PY_GET_ALIGNMENT(intent));
            PyErr_SetString(PyExc_ValueError,mess);
            return NULL;
        }

        /* here we have always intent(in) or intent(inplace) */

        {
            PyArrayObject *retarr = (PyArrayObject *) \
                PyArray_New(&PyArray_Type, PyArray_NDIM(arr), PyArray_DIMS(arr), type_num,
                            NULL,NULL,0,
                            !(intent&F2PY_INTENT_C),
                            NULL);
            if (retarr==NULL)
                return NULL;
            F2PY_REPORT_ON_ARRAY_COPY_FROMARR;
            if (PyArray_CopyInto(retarr, arr)) {
                Py_DECREF(retarr);
                return NULL;
            }
            if (intent & F2PY_INTENT_INPLACE) {
                if (swap_arrays(arr,retarr))
                    return NULL; /* XXX: set exception */
                Py_XDECREF(retarr);
                if (intent & F2PY_INTENT_OUT)
                    Py_INCREF(arr);
            } else {
                arr = retarr;
            }
        }
        return arr;
    }

    if ((intent & F2PY_INTENT_INOUT) ||
            (intent & F2PY_INTENT_INPLACE) ||
            (intent & F2PY_INTENT_CACHE)) {
        PyErr_SetString(PyExc_TypeError,
                        "failed to initialize intent(inout|inplace|cache) "
                        "array, input not an array");
        return NULL;
    }

    {
        F2PY_REPORT_ON_ARRAY_COPY_FROMANY;
        arr = (PyArrayObject *) \
            PyArray_FromAny(obj,PyArray_DescrFromType(type_num), 0,0,
                            ((intent & F2PY_INTENT_C)?NPY_ARRAY_CARRAY:NPY_ARRAY_FARRAY) \
                            | NPY_ARRAY_FORCECAST, NULL);
        if (arr==NULL)
            return NULL;
        if (check_and_fix_dimensions(arr,rank,dims))
            return NULL; /*XXX: set exception */
        return arr;
    }

}
Exemplo n.º 26
0
static PyObject *Py_is_sorted(PyObject *self, PyObject *obj)
{
    npy_intp size;
    bool result;

    PyArrayObject *array = (PyArrayObject *)PyArray_FromAny(
        obj, NULL, 1, 1, 0, NULL);

    if (array == NULL) {
        return NULL;
    }

    size = PyArray_DIM(array, 0);

    if (size < 2) {
        Py_DECREF(array);
        Py_RETURN_TRUE;
    }

    /* Handle just the most common types here, otherwise coerce to
    double */
    switch(PyArray_TYPE(array)) {
    case NPY_INT:
        {
            _is_sorted_int<npy_int> is_sorted;
            result = is_sorted(array);
        }
        break;

    case NPY_LONG:
        {
            _is_sorted_int<npy_long> is_sorted;
            result = is_sorted(array);
        }
        break;

    case NPY_LONGLONG:
        {
            _is_sorted_int<npy_longlong> is_sorted;
            result = is_sorted(array);
        }
        break;

    case NPY_FLOAT:
        {
            _is_sorted<npy_float> is_sorted;
            result = is_sorted(array);
        }
        break;

    case NPY_DOUBLE:
        {
            _is_sorted<npy_double> is_sorted;
            result = is_sorted(array);
        }
        break;

    default:
        {
            Py_DECREF(array);
            array = (PyArrayObject *)PyArray_FromObject(obj, NPY_DOUBLE, 1, 1);

            if (array == NULL) {
                return NULL;
            }

            _is_sorted<npy_double> is_sorted;
            result = is_sorted(array);
        }
    }

    Py_DECREF(array);

    if (result) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}
Exemplo n.º 27
0
  numpy_extractor ( PyObject * X, bool allow_copy) {
   if (X==NULL) TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is NULL !";
   if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";

   // make sure IndexMap is cuboid ?s
   static const char Order = IndexMapType::index_order_type::C_or_F;
   static_assert( ((Order=='F')||(Order=='C')), "Ordering must be C or Fortran");

   if ((!PyArray_Check(X)) && (Order=='D'))
    TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is not a numpy and you ask me to deduce the ordering in memory !";

   const int elementsType (numpy_to_C_type<typename boost::remove_const<ValueType>::type>::arraytype);
   int rank = IndexMapType::rank;
   static const char * error_msg = "   A deep copy of the object would be necessary while views are supposed to guarantee to present a *view* of the python data.\n";

   if (!allow_copy) {
    // in case of a view, we decide ourselves if we can do it...
    // a previous uses PyArray_FromAny, but behaviour changes between different version of numpy, so it is not portable...
    if (!PyArray_Check(X)) 
     throw copy_exception () << error_msg<<"   Indeed the object was not even an array !\n";  

    if ( elementsType != PyArray_TYPE((PyArrayObject*)X)) 
     throw copy_exception () << error_msg<<"   The deep copy is caused by a type mismatch of the elements. \n";

    PyArrayObject *arr = (PyArrayObject *)X;    
    if ( arr->nd != rank)
     throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<< arr->nd << "while you ask for rank "<< rank<<". \n";

    if ((Order == 'C') && (PyArray_ISFORTRAN(arr))) 
     throw copy_exception () << error_msg<<"     The numpy is in Fortran order while it is expected in C order. \n";

    if ((Order == 'F') && (!PyArray_ISFORTRAN(arr))) 
     throw copy_exception () << error_msg<<"     The numpy is not in Fortran order as it is expected. \n";

    numpy_obj = X; Py_INCREF(X); 
   }
   else { 
    // From X, we ask the numpy library to make a numpy, and of the correct type.
    // This handles automatically the cases where : 
    //   - we have list, or list of list/tuple
    //   - the numpy type is not the one we want.
    //   - adjust the dimension if needed
    // If X is an array : 
    //   - if Order is same, don't change it
    //   - else impose it (may provoque a copy).
    // if X is not array : 
    //   - Order = FortranOrder or SameOrder - > Fortran order otherwise C
    bool ForceCast = false;// Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object.
    int flags = (ForceCast ? NPY_FORCECAST : 0) ;// do NOT force a copy | (make_copy ?  NPY_ENSURECOPY : 0);
    if (!(PyArray_Check(X) && (Order=='D'))) flags |= (Order =='F' ? NPY_F_CONTIGUOUS : NPY_C_CONTIGUOUS); //impose mem order
    //if (!(PyArray_Check(X) && (Order=='D'))) flags |= (Order =='F' ? NPY_FARRAY : NPY_CARRAY); //impose mem order
    numpy_obj= PyArray_FromAny(X,PyArray_DescrFromType(elementsType), rank,rank, flags , NULL );

    // do several checks
    if (!numpy_obj) {// The convertion of X to a numpy has failed !
     if (PyErr_Occurred()) {PyErr_Print();PyErr_Clear();}
     TRIQS_RUNTIME_ERROR<<"numpy interface : the python object  is not convertible to a numpy. ";
    }
    assert (PyArray_Check(numpy_obj)); assert((numpy_obj->ob_refcnt==1) || ((numpy_obj ==X)));

    arr_obj = (PyArrayObject *)numpy_obj;
    try {
     if (arr_obj->nd!=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
     if (arr_obj->descr->type_num != elementsType) 
      TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<arr_obj->descr->type_num <<" vs "<<elementsType;
     if (Order == 'F') { if (!PyArray_ISFORTRAN(numpy_obj)) TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : should be Fortran array";}
     else {if (!PyArray_ISCONTIGUOUS(numpy_obj)) TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : should be contiguous";}
    }
    catch(...) { Py_DECREF(numpy_obj); throw;} // make sure that in case of problem, the reference counting of python is still ok...
   }

   arr_obj = (PyArrayObject *)numpy_obj;
  } 
PyObject * allstats(PyObject *self, PyObject *args, PyObject *kw) {
	static char *kwlist[] = {"input", "min", "max", "mean", "std", NULL};
	PyObject *input, *inputarray;
	PyObject *switch_min, *switch_max, *switch_mean, *switch_std;
	PyObject *outputdict, *value;
	int input_typenum;
	stats result;

	/* Parse input args.
	 * "input":  a numpy array or a python object that converts to an array.
	 * Optionally:  "min", "max", "mean", "std" truth values, which default to False
	 */
	switch_min = switch_max = switch_mean = switch_std = Py_None;
	if (!PyArg_ParseTupleAndKeywords(args, kw, "O|OOOO", kwlist, &input, &switch_min, &switch_max, &switch_mean, &switch_std))
		return NULL;

	/*
	 * Create proper PyArrayObject from input python object.
	 * This does the bare minimum to create a numpy array.  If the input
	 * python object is already a numpy array, then no copy will be made.
	 * It will not ensure contiguous, aligned, etc, so operations on the
	 * array object should account for that.
	 */
	inputarray = PyArray_FromAny(input, NULL, 0, 0, 0, NULL);
	if (inputarray == NULL) {
		Py_XDECREF(inputarray);
		return NULL;
	}

	/*
	 * Initialize the result and switch stats on/off depending on args
	 */
	initStats(&result);
	if (PyObject_IsTrue(switch_min))
		result.switch_min = 1;
	if (PyObject_IsTrue(switch_max))
		result.switch_max = 1;
	if (PyObject_IsTrue(switch_mean))
		result.switch_mean = 1;
	if (PyObject_IsTrue(switch_std))
		result.switch_std = 1;

	/*
	 * Delegate to type specific function.
	 */
	input_typenum = PyArray_TYPE(inputarray);
	switch (input_typenum) {
		case NPY_BYTE:
			allstats_byte(inputarray, &result);
			break;
		case NPY_UBYTE:
			allstats_ubyte(inputarray, &result);
			break;
		case NPY_SHORT:
			allstats_short(inputarray, &result);
			break;
		case NPY_USHORT:
			allstats_ushort(inputarray, &result);
			break;
		case NPY_INT:
			allstats_int(inputarray, &result);
			break;
		case NPY_UINT:
			allstats_uint(inputarray, &result);
			break;
		case NPY_LONG:
			allstats_long(inputarray, &result);
			break;
		case NPY_ULONG:
			allstats_ulong(inputarray, &result);
			break;
		case NPY_LONGLONG:
			allstats_longlong(inputarray, &result);
			break;
		case NPY_ULONGLONG:
			allstats_ulonglong(inputarray, &result);
			break;
		case NPY_FLOAT:
			allstats_float(inputarray, &result);
			break;
		case NPY_DOUBLE:
			allstats_double(inputarray, &result);
			break;
		case NPY_LONGDOUBLE:
			allstats_longdouble(inputarray, &result);
			break;
		case NPY_CFLOAT:
		case NPY_CDOUBLE:
		case NPY_CLONGDOUBLE:
		default:
			PyErr_Format(PyExc_TypeError, "no allstats support for typenum %d", input_typenum);
			Py_XDECREF(inputarray);
			return NULL;
	}
	Py_XDECREF(inputarray);

	/* Create return value:  a python dict containing the stats. */
	outputdict = PyDict_New();

	if (result.switch_min) {
		value = PyFloat_FromDouble(result.min);
		PyMapping_SetItemString(outputdict, "min", value);
		Py_XDECREF(value);
	}

	if (result.switch_max) {
		value = PyFloat_FromDouble(result.max);
		PyMapping_SetItemString(outputdict, "max", value);
		Py_XDECREF(value);
	}

	if (result.switch_mean) {
		value = PyFloat_FromDouble(result.mean);
		PyMapping_SetItemString(outputdict, "mean", value);
		Py_XDECREF(value);
	}

	/* Need final calculation of standard deviation from variance */
	if (result.switch_std) {
		result.std = sqrt(result.variance_n);
		value = PyFloat_FromDouble(result.std);
		PyMapping_SetItemString(outputdict, "std", value);
		Py_XDECREF(value);
	}
	return outputdict;
}
Exemplo n.º 29
0
/*
 * Returns input array with values inserted sequentially into places
 * indicated by the mask
 */
NPY_NO_EXPORT PyObject *
arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
{
    char *src, *dest;
    npy_bool *mask_data;
    PyArray_Descr *dtype;
    PyArray_CopySwapFunc *copyswap;
    PyObject *array0, *mask0, *values0;
    PyArrayObject *array, *mask, *values;
    npy_intp i, j, chunk, nm, ni, nv;

    static char *kwlist[] = {"input", "mask", "vals", NULL};
    NPY_BEGIN_THREADS_DEF;
    values = mask = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O!OO:place", kwlist,
                &PyArray_Type, &array0, &mask0, &values0)) {
        return NULL;
    }

    array = (PyArrayObject *)PyArray_FromArray((PyArrayObject *)array0, NULL,
                                    NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY);
    if (array == NULL) {
        goto fail;
    }

    ni = PyArray_SIZE(array);
    dest = PyArray_DATA(array);
    chunk = PyArray_DESCR(array)->elsize;
    mask = (PyArrayObject *)PyArray_FROM_OTF(mask0, NPY_BOOL,
                                NPY_ARRAY_CARRAY | NPY_ARRAY_FORCECAST);
    if (mask == NULL) {
        goto fail;
    }

    nm = PyArray_SIZE(mask);
    if (nm != ni) {
        PyErr_SetString(PyExc_ValueError,
                        "place: mask and data must be "
                        "the same size");
        goto fail;
    }

    mask_data = PyArray_DATA(mask);
    dtype = PyArray_DESCR(array);
    Py_INCREF(dtype);

    values = (PyArrayObject *)PyArray_FromAny(values0, dtype,
                                    0, 0, NPY_ARRAY_CARRAY, NULL);
    if (values == NULL) {
        goto fail;
    }

    nv = PyArray_SIZE(values); /* zero if null array */
    if (nv <= 0) {
        npy_bool allFalse = 1;
        i = 0;

        while (allFalse && i < ni) {
            if (mask_data[i]) {
                allFalse = 0;
            } else {
                i++;
            }
        }
        if (!allFalse) {
            PyErr_SetString(PyExc_ValueError,
                            "Cannot insert from an empty array!");
            goto fail;
        } else {
            Py_XDECREF(values);
            Py_XDECREF(mask);
            Py_RETURN_NONE;
        }
    }

    src = PyArray_DATA(values);
    j = 0;

    copyswap = PyArray_DESCR(array)->f->copyswap;
    NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(array));
    for (i = 0; i < ni; i++) {
        if (mask_data[i]) {
            if (j >= nv) {
                j = 0;
            }

            copyswap(dest + i*chunk, src + j*chunk, 0, array);
            j++;
        }
    }
    NPY_END_THREADS;

    Py_XDECREF(values);
    Py_XDECREF(mask);
    Py_DECREF(array);
    Py_RETURN_NONE;

 fail:
    Py_XDECREF(mask);
    Py_XDECREF(array);
    Py_XDECREF(values);
    return NULL;
}
Exemplo n.º 30
0
 // return a NEW (owned) reference
 //
 inline PyObject * numpy_extractor_impl ( PyObject * X, bool allow_copy, std::string type_name, 
   int elementsType, int rank, size_t * lengths, std::ptrdiff_t * strides, size_t size_of_ValueType) {

  PyObject * numpy_obj;

  if (X==NULL) TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is NULL !";
  if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";

  static const char * error_msg = "   A deep copy of the object would be necessary while views are supposed to guarantee to present a *view* of the python data.\n";

  if (!allow_copy) {
   if (!PyArray_Check(X)) throw copy_exception () << error_msg<<"   Indeed the object was not even an array !\n";
   if ( elementsType != PyArray_TYPE((PyArrayObject*)X))
    throw copy_exception () << error_msg<<"   The deep copy is caused by a type mismatch of the elements. Expected "<< type_name<< " and found XXX \n";
   PyArrayObject *arr = (PyArrayObject *)X;
#ifdef TRIQS_NUMPY_VERSION_LT_17
   if ( arr->nd != rank) throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<< arr->nd << "while you ask for rank "<< rank<<". \n";
#else
   if ( PyArray_NDIM(arr) != rank) throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<<  PyArray_NDIM(arr) << "while you ask for rank "<< rank<<". \n";
#endif
   numpy_obj = X; Py_INCREF(X);
  }
  else {
   // From X, we ask the numpy library to make a numpy, and of the correct type.
   // This handles automatically the cases where :
   //   - we have list, or list of list/tuple
   //   - the numpy type is not the one we want.
   //   - adjust the dimension if needed
   // If X is an array :
   //   - if Order is same, don't change it
   //   - else impose it (may provoque a copy).
   // if X is not array :
   //   - Order = FortranOrder or SameOrder - > Fortran order otherwise C

   //bool ForceCast = false;// Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object.
   int flags = 0; //(ForceCast ? NPY_FORCECAST : 0) ;// do NOT force a copy | (make_copy ?  NPY_ENSURECOPY : 0);
   if (!(PyArray_Check(X) ))
    //flags |= ( IndexMapType::traversal_order == indexmaps::mem_layout::c_order(rank) ? NPY_C_CONTIGUOUS : NPY_F_CONTIGUOUS); //impose mem order
#ifdef TRIQS_NUMPY_VERSION_LT_17
    flags |= (NPY_C_CONTIGUOUS); //impose mem order
#else
    flags |= (NPY_ARRAY_C_CONTIGUOUS); //impose mem order
#endif
   numpy_obj= PyArray_FromAny(X,PyArray_DescrFromType(elementsType), rank,rank, flags , NULL );

   // do several checks
   if (!numpy_obj) {// The convertion of X to a numpy has failed !
    if (PyErr_Occurred()) {PyErr_Print();PyErr_Clear();}
    TRIQS_RUNTIME_ERROR<<"numpy interface : the python object  is not convertible to a numpy. ";
   }
   assert (PyArray_Check(numpy_obj)); assert((numpy_obj->ob_refcnt==1) || ((numpy_obj ==X)));

   PyArrayObject *arr_obj;
   arr_obj = (PyArrayObject *)numpy_obj;
   try {
#ifdef TRIQS_NUMPY_VERSION_LT_17
    if (arr_obj->nd!=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
    if (arr_obj->descr->type_num != elementsType)
     TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<arr_obj->descr->type_num <<" vs "<<elementsType;
#else
    if ( PyArray_NDIM(arr_obj) !=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
    if ( PyArray_DESCR(arr_obj)->type_num != elementsType)
     TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<PyArray_DESCR(arr_obj)->type_num <<" vs "<<elementsType;
#endif
   }
   catch(...) { Py_DECREF(numpy_obj); throw;} // make sure that in case of problem, the reference counting of python is still ok...
  }

  // extract strides and lengths
  PyArrayObject *arr_obj;
  arr_obj = (PyArrayObject *)numpy_obj;
#ifdef TRIQS_NUMPY_VERSION_LT_17
  const size_t dim =arr_obj->nd; // we know that dim == rank
  for (size_t i=0; i< dim ; ++i) {
   lengths[i] = size_t(arr_obj-> dimensions[i]);
   strides[i] = std::ptrdiff_t(arr_obj-> strides[i])/ size_of_ValueType;
  }
#else
  const size_t dim = PyArray_NDIM(arr_obj); // we know that dim == rank
  for (size_t i=0; i< dim ; ++i) {
   lengths[i] = size_t( PyArray_DIMS(arr_obj)[i]);
   strides[i] = std::ptrdiff_t( PyArray_STRIDES(arr_obj)[i])/ size_of_ValueType;
  }
#endif

  return numpy_obj;
 }