[12011] | 1 | /*\file FetchData.cpp:
|
---|
| 2 | * \brief: general I/O interface to fetch data in python
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #ifdef HAVE_CONFIG_H
|
---|
[23708] | 6 | #include <config.h>
|
---|
[12011] | 7 | #else
|
---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | #define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
|
---|
| 12 | #define NO_IMPORT
|
---|
| 13 |
|
---|
[13749] | 14 | #include "./pythonio.h"
|
---|
| 15 | #include "../../c/shared/shared.h"
|
---|
[12011] | 16 |
|
---|
[12073] | 17 | /*Primitive data types*/
|
---|
[12365] | 18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
[12011] | 19 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
| 20 |
|
---|
[24112] | 21 | double dscalar;
|
---|
[12011] | 22 |
|
---|
| 23 | /*return internal value: */
|
---|
[24112] | 24 | #if _PYTHON_MAJOR_ == 3
|
---|
| 25 | if (PyFloat_Check(py_float))
|
---|
| 26 | dscalar=PyFloat_AsDouble(py_float);
|
---|
| 27 | else if (PyLong_Check(py_float))
|
---|
[19895] | 28 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[23708] | 29 | else if (PyBool_Check(py_float))
|
---|
| 30 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
| 31 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 32 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 33 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 34 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 35 | else
|
---|
[24112] | 36 | _error_("unrecognized float type py3 in input!");
|
---|
[23708] | 37 |
|
---|
| 38 | #else
|
---|
| 39 | if (PyFloat_Check(py_float))
|
---|
| 40 | dscalar=PyFloat_AsDouble(py_float);
|
---|
| 41 | else if (PyLong_Check(py_float))
|
---|
[14093] | 42 | dscalar=PyLong_AsDouble(py_float);
|
---|
[19896] | 43 | else if (PyInt_Check(py_float))
|
---|
[23231] | 44 | dscalar=(double)PyInt_AsLong(py_float);
|
---|
[14097] | 45 | else if (PyBool_Check(py_float))
|
---|
[14093] | 46 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[14097] | 47 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 48 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 49 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 50 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[14093] | 51 | else
|
---|
[24112] | 52 | _error_("unrecognized float type in py2 input!");
|
---|
[23708] | 53 | #endif
|
---|
[12011] | 54 | /*output: */
|
---|
[14093] | 55 | *pscalar=dscalar;
|
---|
[12011] | 56 | }
|
---|
| 57 | /*}}}*/
|
---|
[22674] | 58 | /*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
|
---|
| 59 | void FetchData(float* pscalar,PyObject* py_float){
|
---|
| 60 |
|
---|
| 61 | float fscalar;
|
---|
| 62 |
|
---|
| 63 | /*return internal value: */
|
---|
[23708] | 64 | #if _PYTHON_MAJOR_ == 3
|
---|
[22674] | 65 | if (PyFloat_Check(py_float))
|
---|
[24112] | 66 | fscalar=PyFloat_AsDouble(py_float);
|
---|
[23708] | 67 | else if (PyLong_Check(py_float))
|
---|
[22674] | 68 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[23708] | 69 | else if (PyBool_Check(py_float))
|
---|
| 70 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
| 71 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 72 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 73 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 74 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 75 | else
|
---|
| 76 | _error_("unrecognized float type in input!");
|
---|
| 77 | #else
|
---|
| 78 | if (PyFloat_Check(py_float))
|
---|
| 79 | fscalar=PyFloat_AsDouble(py_float);
|
---|
| 80 | else if (PyLong_Check(py_float))
|
---|
[22674] | 81 | fscalar=(float)PyLong_AsDouble(py_float);
|
---|
| 82 | else if (PyInt_Check(py_float))
|
---|
[23708] | 83 | fscalar=(float)PyInt_AsLong(py_float);
|
---|
[22674] | 84 | else if (PyBool_Check(py_float))
|
---|
[23708] | 85 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[22674] | 86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
[23708] | 87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
[23708] | 89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 90 | else
|
---|
[23708] | 91 | _error_("unrecognized float type in input!");
|
---|
| 92 | #endif
|
---|
[22674] | 93 | /*output: */
|
---|
| 94 | *pscalar=fscalar;
|
---|
| 95 | }
|
---|
| 96 | /*}}}*/
|
---|
[14093] | 97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
---|
| 98 | void FetchData(int* pscalar, PyObject* py_long){
|
---|
[12011] | 99 |
|
---|
[14093] | 100 | int iscalar;
|
---|
[12011] | 101 |
|
---|
| 102 | /*return internal value: */
|
---|
[23708] | 103 | #if _PYTHON_MAJOR_ == 3
|
---|
| 104 | if (PyLong_Check(py_long))
|
---|
[19895] | 105 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[23708] | 106 | else if (PyFloat_Check(py_long))
|
---|
| 107 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
| 108 | else if (PyBool_Check(py_long))
|
---|
| 109 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
| 110 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 111 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 112 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 113 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 114 | else
|
---|
| 115 | _error_("unrecognized long type in input!");
|
---|
| 116 |
|
---|
| 117 | #else
|
---|
| 118 | if (PyLong_Check(py_long))
|
---|
| 119 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[19896] | 120 | else if (PyInt_Check(py_long))
|
---|
| 121 | iscalar=(int)PyInt_AsLong(py_long);
|
---|
[14097] | 122 | else if (PyFloat_Check(py_long))
|
---|
[14093] | 123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
[14097] | 124 | else if (PyBool_Check(py_long))
|
---|
[14093] | 125 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[14097] | 126 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 127 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 128 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 129 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
[14093] | 130 | else
|
---|
| 131 | _error_("unrecognized long type in input!");
|
---|
[23708] | 132 | #endif
|
---|
[12011] | 133 | /*output: */
|
---|
[14093] | 134 | *pscalar=iscalar;
|
---|
[12011] | 135 | }
|
---|
| 136 | /*}}}*/
|
---|
[14093] | 137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
---|
| 138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
---|
[12011] | 139 |
|
---|
[14093] | 140 | bool bscalar;
|
---|
[13622] | 141 |
|
---|
[14097] | 142 | /*return internal value: */
|
---|
[23708] | 143 | #if _PYTHON_MAJOR_ == 3
|
---|
[24112] | 144 | if (PyBool_Check(py_boolean))
|
---|
[14097] | 145 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 146 | else if (PyLong_Check(py_boolean))
|
---|
| 147 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19895] | 148 | else if (PyLong_Check(py_boolean))
|
---|
| 149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[23708] | 150 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 151 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 152 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 153 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 154 | else
|
---|
| 155 | _error_("unrecognized boolean type in input!");
|
---|
| 156 |
|
---|
| 157 | #else
|
---|
| 158 | if (PyBool_Check(py_boolean))
|
---|
| 159 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 160 | else if (PyLong_Check(py_boolean))
|
---|
| 161 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 162 | else if (PyLong_Check(py_boolean))
|
---|
| 163 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19896] | 164 | else if (PyInt_Check(py_boolean))
|
---|
| 165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
---|
[14097] | 166 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 167 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 168 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 169 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 170 | else
|
---|
| 171 | _error_("unrecognized boolean type in input!");
|
---|
[23708] | 172 | #endif
|
---|
[14097] | 173 | /*output: */
|
---|
[14093] | 174 | *pscalar=bscalar;
|
---|
[12011] | 175 | }
|
---|
| 176 | /*}}}*/
|
---|
[12365] | 177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
[12112] | 178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
[12073] | 179 |
|
---|
[12112] | 180 | /*output: */
|
---|
[13372] | 181 | double* dmatrix=NULL;
|
---|
[12112] | 182 | double* matrix=NULL;
|
---|
[23231] | 183 | int M=0;
|
---|
| 184 | int N=0;
|
---|
[13373] | 185 | int ndim;
|
---|
[12112] | 186 | npy_intp* dims=NULL;
|
---|
| 187 |
|
---|
[13990] | 188 | /*intermediary:*/
|
---|
| 189 | long* lmatrix=NULL;
|
---|
| 190 | bool* bmatrix=NULL;
|
---|
[14317] | 191 | int i;
|
---|
| 192 | PyObject* py_matrix2=NULL;
|
---|
[13990] | 193 |
|
---|
[24112] | 194 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
[14093] | 195 | /*retrieve dimensions: */
|
---|
| 196 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[24112] | 197 | if (ndim==2) {
|
---|
[14098] | 198 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 199 | M=dims[0]; N=dims[1];
|
---|
| 200 | }
|
---|
| 201 | else if (ndim==1) {
|
---|
| 202 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 203 | M=dims[0]; N=1;
|
---|
| 204 | }
|
---|
| 205 | else
|
---|
| 206 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 207 |
|
---|
[14317] | 208 | if (M && N) {
|
---|
| 209 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 210 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
|
---|
| 211 | py_matrix=py_matrix2;
|
---|
| 212 | }
|
---|
[14316] | 213 |
|
---|
[24112] | 214 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
[14093] | 215 | /*retrieve internal value: */
|
---|
| 216 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12112] | 217 |
|
---|
[14093] | 218 | /*copy matrix: */
|
---|
| 219 | matrix=xNew<double>(M*N);
|
---|
[24112] | 220 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
[14093] | 221 | }
|
---|
[13990] | 222 |
|
---|
[14316] | 223 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 224 | /*retrieve internal value: */
|
---|
| 225 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 226 |
|
---|
[14093] | 227 | /*transform into double matrix: */
|
---|
| 228 | matrix=xNew<double>(M*N);
|
---|
| 229 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
| 230 | }
|
---|
[13990] | 231 |
|
---|
[14093] | 232 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 233 | /*retrieve internal value: */
|
---|
| 234 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 235 |
|
---|
[14093] | 236 | /*transform into double matrix: */
|
---|
| 237 | matrix=xNew<double>(M*N);
|
---|
| 238 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | else
|
---|
[14234] | 242 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 243 |
|
---|
[19683] | 244 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
[13990] | 245 | }
|
---|
| 246 | else
|
---|
[14093] | 247 | matrix=NULL;
|
---|
[13484] | 248 | }
|
---|
[24112] | 249 | else if (PyList_Check(py_matrix)) {
|
---|
| 250 | /*retrieve dimensions: */
|
---|
| 251 | M=(int)PyList_Size(py_matrix);
|
---|
[24114] | 252 | N=1;
|
---|
[24112] | 253 | if (M) {
|
---|
| 254 | matrix=xNew<double>(M);
|
---|
| 255 | for (int index = 0; index < M; index++) {
|
---|
| 256 | PyObject *item;
|
---|
| 257 | item = PyList_GetItem(py_matrix, index);
|
---|
| 258 | if ((int)PyList_Size(item)>1)
|
---|
| 259 | _error_("2D lists are not suported");
|
---|
| 260 | FetchData(&(matrix[index]),item);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 264 | else
|
---|
| 265 | matrix=NULL;
|
---|
| 266 | }
|
---|
| 267 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 268 | /*retrieve dimensions: */
|
---|
| 269 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 270 | N=1;
|
---|
[24112] | 271 | if (M) {
|
---|
| 272 | matrix=xNew<double>(M);
|
---|
| 273 | for (int index = 0; index < M; index++) {
|
---|
| 274 | PyObject *item;
|
---|
| 275 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 276 | if ((int)PyTuple_Size(item)>1)
|
---|
| 277 | _error_("2D tuple are not suported");
|
---|
| 278 | FetchData(&(matrix[index]),item);
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 282 | else
|
---|
| 283 | matrix=NULL;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[14097] | 286 | else {
|
---|
[14093] | 287 | M=1;
|
---|
| 288 | N=1;
|
---|
| 289 | matrix=xNew<double>(M*N);
|
---|
| 290 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[12112] | 293 | /*output: */
|
---|
| 294 | if(pM)*pM=M;
|
---|
| 295 | if(pN)*pN=N;
|
---|
| 296 | if(pmatrix)*pmatrix=matrix;
|
---|
| 297 | }
|
---|
| 298 | /*}}}*/
|
---|
[12776] | 299 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 300 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 301 |
|
---|
| 302 | /*output: */
|
---|
| 303 | int* matrix=NULL;
|
---|
[23231] | 304 | int M=0;
|
---|
| 305 | int N=0;
|
---|
[13990] | 306 | int ndim;
|
---|
| 307 | npy_intp* dims=NULL;
|
---|
[12776] | 308 |
|
---|
| 309 | /*intermediary:*/
|
---|
[13990] | 310 | double* dmatrix=NULL;
|
---|
| 311 | long* lmatrix=NULL;
|
---|
| 312 | bool* bmatrix=NULL;
|
---|
[12776] | 313 | int i;
|
---|
[14317] | 314 | PyObject* py_matrix2=NULL;
|
---|
[12776] | 315 |
|
---|
[14093] | 316 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 317 | /*retrieve dimensions: */
|
---|
| 318 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[14098] | 319 | if (ndim==2) {
|
---|
| 320 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 321 | M=dims[0]; N=dims[1];
|
---|
| 322 | }
|
---|
| 323 | else if (ndim==1) {
|
---|
| 324 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 325 | M=dims[0]; N=1;
|
---|
| 326 | }
|
---|
| 327 | else
|
---|
| 328 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 329 |
|
---|
[14093] | 330 | if (M && N) {
|
---|
[14317] | 331 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 332 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
---|
| 333 | py_matrix=py_matrix2;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[14093] | 336 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 337 | /*retrieve internal value: */
|
---|
| 338 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12776] | 339 |
|
---|
[14093] | 340 | /*transform into integer matrix: */
|
---|
| 341 | matrix=xNew<int>(M*N);
|
---|
| 342 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
| 343 | }
|
---|
[13990] | 344 |
|
---|
[14316] | 345 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 346 | /*retrieve internal value: */
|
---|
| 347 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 348 |
|
---|
[14093] | 349 | /*transform into integer matrix: */
|
---|
| 350 | matrix=xNew<int>(M*N);
|
---|
| 351 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
| 352 | }
|
---|
[13990] | 353 |
|
---|
[14093] | 354 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 355 | /*retrieve internal value: */
|
---|
| 356 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 357 |
|
---|
[14093] | 358 | /*transform into integer matrix: */
|
---|
| 359 | matrix=xNew<int>(M*N);
|
---|
| 360 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | else
|
---|
[14234] | 364 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 365 |
|
---|
[21201] | 366 | /* These lines are causing a segfault
|
---|
[14317] | 367 | if (py_matrix2)
|
---|
| 368 | delete(py_matrix2);
|
---|
[21201] | 369 | */
|
---|
[13990] | 370 | }
|
---|
| 371 | else
|
---|
[14093] | 372 | matrix=NULL;
|
---|
[13484] | 373 | }
|
---|
[24112] | 374 | else if (PyList_Check(py_matrix)) {
|
---|
| 375 | /*retrieve dimensions: */
|
---|
| 376 | M=(int)PyList_Size(py_matrix);
|
---|
[24114] | 377 | N=1;
|
---|
[24112] | 378 | if (M) {
|
---|
| 379 | matrix=xNew<int>(M);
|
---|
| 380 | for (int index = 0; index < M; index++) {
|
---|
| 381 | PyObject *item;
|
---|
| 382 | item = PyList_GetItem(py_matrix, index);
|
---|
| 383 | if ((int)PyList_Size(item)>1)
|
---|
| 384 | _error_("2D lists are not suported");
|
---|
| 385 | FetchData(&(matrix[index]),item);
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | else
|
---|
| 389 | matrix=NULL;
|
---|
| 390 | }
|
---|
| 391 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 392 | /*retrieve dimensions: */
|
---|
| 393 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 394 | N=1;
|
---|
[24112] | 395 | if (M) {
|
---|
| 396 | matrix=xNew<int>(M);
|
---|
| 397 | for (int index = 0; index < M; index++) {
|
---|
| 398 | PyObject *item;
|
---|
| 399 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 400 | if ((int)PyTuple_Size(item)>1)
|
---|
| 401 | _error_("2D tuple are not suported");
|
---|
| 402 | FetchData(&(matrix[index]),item);
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 406 | else
|
---|
| 407 | matrix=NULL;
|
---|
| 408 | }
|
---|
[14093] | 409 |
|
---|
[14097] | 410 | else {
|
---|
[14093] | 411 | M=1;
|
---|
| 412 | N=1;
|
---|
| 413 | matrix=xNew<int>(M*N);
|
---|
| 414 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[12776] | 417 | /*output: */
|
---|
| 418 | if(pM)*pM=M;
|
---|
| 419 | if(pN)*pN=N;
|
---|
| 420 | if(pmatrix)*pmatrix=matrix;
|
---|
| 421 | }
|
---|
| 422 | /*}}}*/
|
---|
[14234] | 423 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 424 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 425 |
|
---|
| 426 | /*output: */
|
---|
| 427 | bool* bmatrix=NULL;
|
---|
| 428 | bool* matrix=NULL;
|
---|
[23231] | 429 | int M=0;
|
---|
| 430 | int N=0;
|
---|
[14234] | 431 | int ndim;
|
---|
| 432 | npy_intp* dims=NULL;
|
---|
| 433 |
|
---|
| 434 | /*intermediary:*/
|
---|
| 435 | double* dmatrix=NULL;
|
---|
| 436 | long* lmatrix=NULL;
|
---|
| 437 | int i;
|
---|
[14317] | 438 | PyObject* py_matrix2=NULL;
|
---|
[14234] | 439 |
|
---|
| 440 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 441 | /*retrieve dimensions: */
|
---|
| 442 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
| 443 | if (ndim==2) {
|
---|
| 444 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 445 | M=dims[0]; N=dims[1];
|
---|
| 446 | }
|
---|
| 447 | else if (ndim==1) {
|
---|
| 448 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 449 | M=dims[0]; N=1;
|
---|
| 450 | }
|
---|
| 451 | else
|
---|
| 452 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
| 453 |
|
---|
| 454 | if (M && N) {
|
---|
[14317] | 455 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 456 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
---|
| 457 | py_matrix=py_matrix2;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[14234] | 460 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 461 | /*retrieve internal value: */
|
---|
| 462 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 463 |
|
---|
| 464 | /*transform into bool matrix: */
|
---|
| 465 | matrix=xNew<bool>(M*N);
|
---|
| 466 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[14316] | 469 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14234] | 470 | /*retrieve internal value: */
|
---|
| 471 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 472 |
|
---|
| 473 | /*transform into bool matrix: */
|
---|
| 474 | matrix=xNew<bool>(M*N);
|
---|
| 475 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 479 | /*retrieve internal value: */
|
---|
| 480 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 481 |
|
---|
| 482 | /*copy matrix: */
|
---|
| 483 | matrix=xNew<bool>(M*N);
|
---|
| 484 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | else
|
---|
| 488 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 489 |
|
---|
| 490 | if (py_matrix2)
|
---|
| 491 | delete(py_matrix2);
|
---|
[14234] | 492 | }
|
---|
| 493 | else
|
---|
| 494 | matrix=NULL;
|
---|
| 495 | }
|
---|
[24112] | 496 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
[14234] | 497 |
|
---|
[24112] | 498 | else if (PyList_Check(py_matrix)) {
|
---|
| 499 | /*retrieve dimensions: */
|
---|
| 500 | M=(int)PyList_Size(py_matrix);
|
---|
| 501 | N=1;
|
---|
| 502 | if (M) {
|
---|
| 503 | matrix=xNew<bool>(M);
|
---|
| 504 | for (int index = 0; index < M; index++) {
|
---|
| 505 | PyObject *item;
|
---|
| 506 | item = PyList_GetItem(py_matrix, index);
|
---|
| 507 | if ((int)PyList_Size(item)>1)
|
---|
| 508 | _error_("2D lists are not suported");
|
---|
| 509 | FetchData(&(matrix[index]),item);
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 | else
|
---|
| 513 | matrix=NULL;
|
---|
| 514 | }
|
---|
| 515 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 516 | /*retrieve dimensions: */
|
---|
| 517 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 518 | N=1;
|
---|
[24112] | 519 | if (M) {
|
---|
| 520 | matrix=xNew<bool>(M);
|
---|
| 521 | for (int index = 0; index < M; index++) {
|
---|
| 522 | PyObject *item;
|
---|
| 523 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 524 | if ((int)PyTuple_Size(item)>1)
|
---|
| 525 | _error_("2D tuples are not suported");
|
---|
| 526 | FetchData(&(matrix[index]),item);
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 530 | else
|
---|
| 531 | matrix=NULL;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
[14234] | 534 | else {
|
---|
| 535 | M=1;
|
---|
| 536 | N=1;
|
---|
| 537 | matrix=xNew<bool>(M*N);
|
---|
| 538 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | /*output: */
|
---|
| 542 | if(pM)*pM=M;
|
---|
| 543 | if(pN)*pN=N;
|
---|
| 544 | if(pmatrix)*pmatrix=matrix;
|
---|
| 545 | }
|
---|
| 546 | /*}}}*/
|
---|
[12365] | 547 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
[12120] | 548 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
[12112] | 549 |
|
---|
[12120] | 550 | /*output: */
|
---|
[13372] | 551 | double* dvector=NULL;
|
---|
[12120] | 552 | double* vector=NULL;
|
---|
[23231] | 553 | int M=0;
|
---|
[13373] | 554 | int ndim;
|
---|
[12120] | 555 | npy_intp* dims=NULL;
|
---|
| 556 |
|
---|
[13990] | 557 | /*intermediary:*/
|
---|
| 558 | long* lvector=NULL;
|
---|
| 559 | bool* bvector=NULL;
|
---|
| 560 | int i;
|
---|
[14317] | 561 | PyObject* py_vector2=NULL;
|
---|
[13990] | 562 |
|
---|
[14236] | 563 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 564 | /*retrieve dimensions: */
|
---|
| 565 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 566 | if (ndim==1) {
|
---|
| 567 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 568 | M=dims[0];
|
---|
[14236] | 569 | }
|
---|
| 570 | else if (ndim==2) {
|
---|
| 571 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 572 | if (dims[1]==1)
|
---|
[23708] | 573 | M=dims[0];
|
---|
[14236] | 574 | else
|
---|
| 575 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 576 | }
|
---|
| 577 | else
|
---|
| 578 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[13622] | 579 |
|
---|
[14236] | 580 | if (M) {
|
---|
[14317] | 581 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 582 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
|
---|
| 583 | py_vector=py_vector2;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[14236] | 586 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 587 | /*retrieve internal value: */
|
---|
| 588 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[12120] | 589 |
|
---|
[14236] | 590 | /*copy vector: */
|
---|
| 591 | vector=xNew<double>(M);
|
---|
| 592 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
| 593 | }
|
---|
[13990] | 594 |
|
---|
[14316] | 595 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 596 | /*retrieve internal value: */
|
---|
| 597 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 598 |
|
---|
[14236] | 599 | /*transform into double vector: */
|
---|
| 600 | vector=xNew<double>(M);
|
---|
| 601 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
---|
| 602 | }
|
---|
[13990] | 603 |
|
---|
[14236] | 604 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 605 | /*retrieve internal value: */
|
---|
| 606 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 607 |
|
---|
[14236] | 608 | /*transform into double vector: */
|
---|
| 609 | vector=xNew<double>(M);
|
---|
| 610 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | else
|
---|
| 614 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 615 |
|
---|
[21201] | 616 | /* Causing a seg fault.
|
---|
[14317] | 617 | if (py_vector2)
|
---|
| 618 | delete(py_vector2);
|
---|
[21201] | 619 | */
|
---|
[13990] | 620 | }
|
---|
| 621 | else
|
---|
[14236] | 622 | vector=NULL;
|
---|
[13484] | 623 | }
|
---|
[24112] | 624 | else if (PyList_Check(py_vector)) {
|
---|
| 625 | /*retrieve dimensions: */
|
---|
| 626 | M=(int)PyList_Size(py_vector);
|
---|
[13372] | 627 |
|
---|
[24112] | 628 | if (M) {
|
---|
| 629 | vector=xNew<double>(M);
|
---|
| 630 | for (int index = 0; index < M; index++) {
|
---|
| 631 | PyObject *item;
|
---|
| 632 | item = PyList_GetItem(py_vector, index);
|
---|
| 633 | FetchData(&(vector[index]),item);
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 | else
|
---|
| 637 | vector=NULL;
|
---|
| 638 | }
|
---|
| 639 | else if (PyTuple_Check(py_vector)) {
|
---|
| 640 | /*retrieve dimensions: */
|
---|
| 641 | M=(int)PyTuple_Size(py_vector);
|
---|
| 642 |
|
---|
| 643 | if (M) {
|
---|
| 644 | vector=xNew<double>(M);
|
---|
| 645 | for (int index = 0; index < M; index++) {
|
---|
| 646 | PyObject *item;
|
---|
| 647 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 648 | FetchData(&(vector[index]),item);
|
---|
| 649 | }
|
---|
| 650 | }
|
---|
| 651 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 652 | else
|
---|
| 653 | vector=NULL;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[14236] | 656 | else {
|
---|
| 657 | M=1;
|
---|
| 658 | vector=xNew<double>(M);
|
---|
| 659 | FetchData(&(vector[0]),py_vector);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
[12120] | 662 | /*output: */
|
---|
| 663 | if(pM)*pM=M;
|
---|
| 664 | if(pvector)*pvector=vector;
|
---|
| 665 | }
|
---|
| 666 | /*}}}*/
|
---|
[22674] | 667 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 668 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
---|
| 669 |
|
---|
| 670 | /*output: */
|
---|
| 671 | float* vector=NULL;
|
---|
[23231] | 672 | int M=0;
|
---|
[22674] | 673 | int ndim;
|
---|
| 674 | npy_intp* dims=NULL;
|
---|
| 675 |
|
---|
| 676 | /*intermediary:*/
|
---|
| 677 | long* lvector=NULL;
|
---|
| 678 | bool* bvector=NULL;
|
---|
| 679 | double* dvector=NULL;
|
---|
| 680 | int i;
|
---|
| 681 | PyObject* py_vector2=NULL;
|
---|
| 682 |
|
---|
| 683 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 684 | /*retrieve dimensions: */
|
---|
| 685 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 686 | if (ndim==1) {
|
---|
| 687 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 688 | M=dims[0];
|
---|
[22674] | 689 | }
|
---|
| 690 | else if (ndim==2) {
|
---|
| 691 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 692 | if (dims[1]==1)
|
---|
[23708] | 693 | M=dims[0];
|
---|
[22674] | 694 | else
|
---|
| 695 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 696 | }
|
---|
| 697 | else
|
---|
| 698 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 699 |
|
---|
| 700 | if (M) {
|
---|
| 701 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 702 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 703 | py_vector=py_vector2;
|
---|
| 704 | }
|
---|
| 705 |
|
---|
| 706 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 707 | /*retrieve internal value: */
|
---|
| 708 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 709 |
|
---|
| 710 | /*transform into int vector: */
|
---|
| 711 | vector=xNew<float>(M);
|
---|
[23231] | 712 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
---|
[22674] | 713 | }
|
---|
| 714 |
|
---|
| 715 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
| 716 | /*retrieve internal value: */
|
---|
| 717 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 718 |
|
---|
| 719 | /*transform into int vector: */
|
---|
| 720 | vector=xNew<float>(M);
|
---|
| 721 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 725 | /*retrieve internal value: */
|
---|
| 726 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 727 |
|
---|
| 728 | /*transform into int vector: */
|
---|
| 729 | vector=xNew<float>(M);
|
---|
| 730 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | else
|
---|
| 734 | _error_("unrecognized int pyarray type in input!");
|
---|
| 735 |
|
---|
| 736 | if(py_vector2) delete(py_vector2);
|
---|
| 737 | }
|
---|
| 738 | else
|
---|
[23231] | 739 | vector=NULL;
|
---|
[22674] | 740 | }
|
---|
[24112] | 741 | else if (PyList_Check(py_vector)) {
|
---|
| 742 | /*retrieve dimensions: */
|
---|
| 743 | M=(int)PyList_Size(py_vector);
|
---|
| 744 |
|
---|
| 745 | if (M) {
|
---|
| 746 | vector=xNew<float>(M);
|
---|
| 747 | for (int index = 0; index < M; index++) {
|
---|
| 748 | PyObject *item;
|
---|
| 749 | item = PyList_GetItem(py_vector, index);
|
---|
| 750 | FetchData(&(vector[index]),item);
|
---|
| 751 | }
|
---|
| 752 | }
|
---|
| 753 | else
|
---|
| 754 | vector=NULL;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | else if (PyTuple_Check(py_vector)) {
|
---|
| 758 | /*retrieve dimensions: */
|
---|
| 759 | M=(int)PyTuple_Size(py_vector);
|
---|
| 760 |
|
---|
| 761 | if (M) {
|
---|
| 762 | vector=xNew<float>(M);
|
---|
| 763 | for (int index = 0; index < M; index++) {
|
---|
| 764 | PyObject *item;
|
---|
| 765 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 766 | FetchData(&(vector[index]),item);
|
---|
| 767 | }
|
---|
| 768 | }
|
---|
| 769 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 770 | else
|
---|
| 771 | vector=NULL;
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[22674] | 774 | else{
|
---|
| 775 | M=1;
|
---|
| 776 | vector=xNew<float>(M);
|
---|
| 777 | FetchData(&(vector[0]),py_vector);
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | /*output: */
|
---|
| 781 | if(pM)*pM=M;
|
---|
| 782 | if(pvector)*pvector=vector;
|
---|
| 783 | }
|
---|
| 784 | /*}}}*/
|
---|
[14221] | 785 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 786 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
[13310] | 787 |
|
---|
[14221] | 788 | /*output: */
|
---|
| 789 | int* vector=NULL;
|
---|
[23231] | 790 | int M=0;
|
---|
[14221] | 791 | int ndim;
|
---|
| 792 | npy_intp* dims=NULL;
|
---|
| 793 |
|
---|
| 794 | /*intermediary:*/
|
---|
| 795 | long* lvector=NULL;
|
---|
| 796 | bool* bvector=NULL;
|
---|
| 797 | double* dvector=NULL;
|
---|
| 798 | int i;
|
---|
[14317] | 799 | PyObject* py_vector2=NULL;
|
---|
[14221] | 800 |
|
---|
[14236] | 801 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 802 | /*retrieve dimensions: */
|
---|
| 803 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 804 | if (ndim==1) {
|
---|
| 805 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 806 | M=dims[0];
|
---|
[14236] | 807 | }
|
---|
| 808 | else if (ndim==2) {
|
---|
| 809 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 810 | if (dims[1]==1)
|
---|
[23708] | 811 | M=dims[0];
|
---|
[14236] | 812 | else
|
---|
| 813 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 814 | }
|
---|
| 815 | else
|
---|
| 816 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14221] | 817 |
|
---|
[14236] | 818 | if (M) {
|
---|
[14317] | 819 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 820 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 821 | py_vector=py_vector2;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
[14236] | 824 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 825 | /*retrieve internal value: */
|
---|
| 826 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 827 |
|
---|
[14236] | 828 | /*transform into int vector: */
|
---|
| 829 | vector=xNew<int>(M);
|
---|
[23231] | 830 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
---|
[14236] | 831 | }
|
---|
[14221] | 832 |
|
---|
[14316] | 833 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 834 | /*retrieve internal value: */
|
---|
| 835 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 836 |
|
---|
[14236] | 837 | /*transform into int vector: */
|
---|
| 838 | vector=xNew<int>(M);
|
---|
| 839 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
| 840 | }
|
---|
[14221] | 841 |
|
---|
[14236] | 842 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 843 | /*retrieve internal value: */
|
---|
| 844 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 845 |
|
---|
[14236] | 846 | /*transform into int vector: */
|
---|
| 847 | vector=xNew<int>(M);
|
---|
| 848 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 | else
|
---|
| 852 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 853 |
|
---|
| 854 | if (py_vector2)
|
---|
| 855 | delete(py_vector2);
|
---|
[14221] | 856 | }
|
---|
| 857 | else
|
---|
[14236] | 858 | vector=NULL;
|
---|
[14221] | 859 | }
|
---|
| 860 |
|
---|
[24112] | 861 | else if (PyList_Check(py_vector)) {
|
---|
| 862 | /*retrieve dimensions: */
|
---|
| 863 | M=(int)PyList_Size(py_vector);
|
---|
| 864 |
|
---|
| 865 | if (M) {
|
---|
| 866 | vector=xNew<int>(M);
|
---|
| 867 | for (int index = 0; index < M; index++) {
|
---|
| 868 | PyObject *item;
|
---|
| 869 | item = PyList_GetItem(py_vector, index);
|
---|
| 870 | FetchData(&(vector[index]),item);
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 | else
|
---|
| 874 | vector=NULL;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | else if (PyTuple_Check(py_vector)) {
|
---|
| 878 | /*retrieve dimensions: */
|
---|
| 879 | M=(int)PyTuple_Size(py_vector);
|
---|
| 880 |
|
---|
| 881 | if (M) {
|
---|
| 882 | vector=xNew<int>(M);
|
---|
| 883 | for (int index = 0; index < M; index++) {
|
---|
| 884 | PyObject *item;
|
---|
| 885 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 886 | FetchData(&(vector[index]),item);
|
---|
| 887 | }
|
---|
| 888 | }
|
---|
| 889 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 890 | else
|
---|
| 891 | vector=NULL;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
[14236] | 894 | else {
|
---|
| 895 | M=1;
|
---|
| 896 | vector=xNew<int>(M);
|
---|
| 897 | FetchData(&(vector[0]),py_vector);
|
---|
| 898 | }
|
---|
| 899 |
|
---|
[14221] | 900 | /*output: */
|
---|
| 901 | if(pM)*pM=M;
|
---|
| 902 | if(pvector)*pvector=vector;
|
---|
| 903 | }
|
---|
| 904 | /*}}}*/
|
---|
[14234] | 905 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 906 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
[14221] | 907 |
|
---|
[14234] | 908 | /*output: */
|
---|
| 909 | bool* bvector=NULL;
|
---|
| 910 | bool* vector=NULL;
|
---|
[23231] | 911 | int M=0;
|
---|
[14234] | 912 | int ndim;
|
---|
| 913 | npy_intp* dims=NULL;
|
---|
| 914 |
|
---|
| 915 | /*intermediary:*/
|
---|
| 916 | double* dvector=NULL;
|
---|
| 917 | long* lvector=NULL;
|
---|
| 918 | int i;
|
---|
[14317] | 919 | PyObject* py_vector2=NULL;
|
---|
[14234] | 920 |
|
---|
[14236] | 921 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 922 | /*retrieve dimensions: */
|
---|
| 923 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 924 | if (ndim==1) {
|
---|
| 925 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 926 | M=dims[0];
|
---|
[14236] | 927 | }
|
---|
| 928 | else if (ndim==2) {
|
---|
| 929 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 930 | if (dims[1]==1)
|
---|
[23708] | 931 | M=dims[0];
|
---|
[14236] | 932 | else
|
---|
| 933 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 934 | }
|
---|
| 935 | else
|
---|
| 936 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14234] | 937 |
|
---|
[14236] | 938 | if (M) {
|
---|
[14317] | 939 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 940 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
---|
| 941 | py_vector=py_vector2;
|
---|
| 942 | }
|
---|
| 943 |
|
---|
[14236] | 944 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 945 | /*retrieve internal value: */
|
---|
| 946 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 947 |
|
---|
[14236] | 948 | /*transform into bool vector: */
|
---|
| 949 | vector=xNew<bool>(M);
|
---|
| 950 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
| 951 | }
|
---|
[14234] | 952 |
|
---|
[14316] | 953 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 954 | /*retrieve internal value: */
|
---|
| 955 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 956 |
|
---|
[14236] | 957 | /*transform into bool vector: */
|
---|
| 958 | vector=xNew<bool>(M);
|
---|
| 959 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
| 960 | }
|
---|
[14234] | 961 |
|
---|
[14236] | 962 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 963 | /*retrieve internal value: */
|
---|
| 964 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 965 |
|
---|
[14236] | 966 | /*copy vector: */
|
---|
| 967 | vector=xNew<bool>(M);
|
---|
| 968 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
| 969 | }
|
---|
| 970 |
|
---|
| 971 | else
|
---|
| 972 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 973 |
|
---|
| 974 | if (py_vector2)
|
---|
| 975 | delete(py_vector2);
|
---|
[14234] | 976 | }
|
---|
| 977 | else
|
---|
[14236] | 978 | vector=NULL;
|
---|
[14234] | 979 | }
|
---|
[24112] | 980 | else if (PyList_Check(py_vector)) {
|
---|
| 981 | /*retrieve dimensions: */
|
---|
| 982 | M=(int)PyList_Size(py_vector);
|
---|
| 983 | if (M) {
|
---|
| 984 | vector=xNew<bool>(M);
|
---|
| 985 | for (int index = 0; index < M; index++) {
|
---|
| 986 | PyObject *item;
|
---|
| 987 | item = PyList_GetItem(py_vector, index);
|
---|
| 988 | FetchData(&(vector[index]),item);
|
---|
| 989 | }
|
---|
| 990 | }
|
---|
| 991 | else
|
---|
| 992 | vector=NULL;
|
---|
| 993 | }
|
---|
[14234] | 994 |
|
---|
[24112] | 995 | else if (PyTuple_Check(py_vector)) {
|
---|
| 996 | /*retrieve dimensions: */
|
---|
| 997 | M=(int)PyTuple_Size(py_vector);
|
---|
| 998 |
|
---|
| 999 | if (M) {
|
---|
| 1000 | vector=xNew<bool>(M);
|
---|
| 1001 | for (int index = 0; index < M; index++) {
|
---|
| 1002 | PyObject *item;
|
---|
| 1003 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 1004 | FetchData(&(vector[index]),item);
|
---|
| 1005 | }
|
---|
| 1006 | }
|
---|
| 1007 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 1008 | else
|
---|
| 1009 | vector=NULL;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
[14236] | 1012 | else {
|
---|
| 1013 | M=1;
|
---|
| 1014 | vector=xNew<bool>(M);
|
---|
| 1015 | FetchData(&(vector[0]),py_vector);
|
---|
| 1016 | }
|
---|
| 1017 |
|
---|
[14234] | 1018 | /*output: */
|
---|
| 1019 | if(pM)*pM=M;
|
---|
| 1020 | if(pvector)*pvector=vector;
|
---|
| 1021 | }
|
---|
| 1022 | /*}}}*/
|
---|
| 1023 |
|
---|
[13310] | 1024 | /*ISSM objects*/
|
---|
| 1025 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
| 1026 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
| 1027 |
|
---|
| 1028 | /*Initialize output*/
|
---|
| 1029 | BamgGeom* bamggeom=new BamgGeom();
|
---|
| 1030 |
|
---|
| 1031 | /*Fetch all fields*/
|
---|
| 1032 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 1033 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 1034 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
| 1035 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
| 1036 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
| 1037 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 1038 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
| 1039 |
|
---|
| 1040 | /*Assign output pointers:*/
|
---|
| 1041 | *pbamggeom=bamggeom;
|
---|
[13287] | 1042 | }
|
---|
| 1043 | /*}}}*/
|
---|
[13310] | 1044 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
| 1045 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
| 1046 |
|
---|
| 1047 | /*Initialize output*/
|
---|
| 1048 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
| 1049 |
|
---|
| 1050 | /*Fetch all fields*/
|
---|
| 1051 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 1052 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 1053 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
| 1054 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 1055 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
| 1056 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
| 1057 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
| 1058 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
| 1059 |
|
---|
| 1060 | /*Assign output pointers:*/
|
---|
| 1061 | *pbamgmesh=bamgmesh;
|
---|
[13287] | 1062 | }
|
---|
| 1063 | /*}}}*/
|
---|
[13310] | 1064 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
| 1065 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
| 1066 |
|
---|
| 1067 | /*Initialize output*/
|
---|
| 1068 | BamgOpts* bamgopts=new BamgOpts();
|
---|
| 1069 |
|
---|
| 1070 | /*Fetch all fields*/
|
---|
| 1071 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
| 1072 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
| 1073 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
| 1074 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
| 1075 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
| 1076 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
| 1077 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
| 1078 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
| 1079 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
| 1080 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
| 1081 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
| 1082 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
| 1083 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
| 1084 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
| 1085 |
|
---|
| 1086 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
| 1087 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
| 1088 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
| 1089 |
|
---|
| 1090 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
| 1091 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
| 1092 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
| 1093 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
[24112] | 1094 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,PyDict_GetItemString(py_dict,"hVertices"));
|
---|
[13310] | 1095 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
| 1096 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
| 1097 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
| 1098 |
|
---|
| 1099 | /*Additional checks*/
|
---|
| 1100 | bamgopts->Check();
|
---|
| 1101 |
|
---|
| 1102 | /*Assign output pointers:*/
|
---|
| 1103 | *pbamgopts=bamgopts;
|
---|
[13287] | 1104 | }
|
---|
| 1105 | /*}}}*/
|
---|
[13374] | 1106 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
[13373] | 1107 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
[12120] | 1108 |
|
---|
[13373] | 1109 | char *name = NULL;
|
---|
| 1110 | Option *option = NULL;
|
---|
| 1111 |
|
---|
[12776] | 1112 | /*Initialize output*/
|
---|
| 1113 | Options* options=new Options();
|
---|
| 1114 |
|
---|
[13373] | 1115 | /*Fetch all options*/
|
---|
| 1116 | for (int i=istart; i<nrhs; i=i+2){
|
---|
[19896] | 1117 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 1118 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
[19896] | 1119 | #else
|
---|
| 1120 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
| 1121 | #endif
|
---|
[12776] | 1122 |
|
---|
[13373] | 1123 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
| 1124 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
| 1125 |
|
---|
[15105] | 1126 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
---|
[13373] | 1127 |
|
---|
| 1128 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
| 1129 | // options->AddOption(option);
|
---|
| 1130 | // option=NULL;
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
[12776] | 1133 | /*Assign output pointers:*/
|
---|
| 1134 | *poptions=options;
|
---|
| 1135 | }
|
---|
| 1136 | /*}}}*/
|
---|
[15335] | 1137 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
---|
| 1138 | void FetchData(Contours** pcontours,PyObject* py_list){
|
---|
[12776] | 1139 |
|
---|
[13368] | 1140 | int numcontours,test1,test2;
|
---|
| 1141 | char *contourname = NULL;
|
---|
[15335] | 1142 | Contours *contours = NULL;
|
---|
[13368] | 1143 | Contour<double> *contouri = NULL;
|
---|
| 1144 | PyObject *py_dicti = NULL;
|
---|
| 1145 | PyObject *py_item = NULL;
|
---|
| 1146 |
|
---|
[19896] | 1147 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 1148 | if (PyUnicode_Check(py_list)){
|
---|
[19896] | 1149 | #else
|
---|
| 1150 | if (PyString_Check(py_list)){
|
---|
[24107] | 1151 | #endif
|
---|
[19896] | 1152 | FetchData(&contourname,py_list);
|
---|
| 1153 | contours=ExpRead<double>(contourname);
|
---|
| 1154 | }
|
---|
[13368] | 1155 | else if(PyList_Check(py_list)){
|
---|
| 1156 |
|
---|
[15335] | 1157 | contours=new Contours();
|
---|
[13368] | 1158 | numcontours=(int)PyList_Size(py_list);
|
---|
| 1159 |
|
---|
| 1160 | for(int i=0;i<numcontours;i++){
|
---|
| 1161 |
|
---|
[15335] | 1162 | contouri=new Contour<double>();
|
---|
[13368] | 1163 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
| 1164 |
|
---|
| 1165 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
| 1166 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
| 1167 | FetchData(&contouri->nods,py_item);
|
---|
| 1168 |
|
---|
| 1169 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
| 1170 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
| 1171 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
| 1172 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
| 1173 |
|
---|
| 1174 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
| 1175 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
| 1176 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
| 1177 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
| 1178 |
|
---|
| 1179 | contours->AddObject(contouri);
|
---|
| 1180 | }
|
---|
| 1181 | }
|
---|
| 1182 | else{
|
---|
| 1183 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | /*clean-up and assign output pointer*/
|
---|
[13372] | 1187 | xDelete<char>(contourname);
|
---|
[13368] | 1188 | *pcontours=contours;
|
---|
[13353] | 1189 | }
|
---|
[23231] | 1190 |
|
---|
| 1191 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
---|
| 1192 |
|
---|
| 1193 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
---|
| 1194 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
---|
| 1195 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
---|
| 1196 |
|
---|
| 1197 | /*Fetch adjacency matrix:*/
|
---|
| 1198 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
---|
| 1199 |
|
---|
| 1200 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
---|
| 1201 | pyGetJc(a,nvtxs,mwstart);
|
---|
| 1202 |
|
---|
| 1203 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
---|
| 1204 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
---|
[23708] | 1205 |
|
---|
[23231] | 1206 | int* start = xNew<int>(nvtxs+1);
|
---|
| 1207 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
---|
| 1208 | int* adjacency = xNew<int>(nzmax);
|
---|
| 1209 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
---|
| 1210 |
|
---|
| 1211 | /*Get edges weights*/
|
---|
| 1212 | int nedges = start[nvtxs];
|
---|
| 1213 | float* ewgts = NULL;
|
---|
| 1214 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
---|
| 1215 | //size may be 1 and still be empty;
|
---|
| 1216 | //presumably size of edge_weights input will never be exactly 1
|
---|
| 1217 | if(size != 1 && size != 0){
|
---|
| 1218 | ewgts = xNewZeroInit<float>(nedges);
|
---|
| 1219 | for(int i = 0; i<nedges;i++){
|
---|
| 1220 | ewgts[i] = (float)a[i];
|
---|
| 1221 | }
|
---|
| 1222 | }
|
---|
| 1223 |
|
---|
| 1224 | /*Assign output pointers*/
|
---|
| 1225 | *pnvtxs = nvtxs;
|
---|
| 1226 | *padjacency = adjacency;
|
---|
| 1227 | *pstart = start;
|
---|
| 1228 | *pewgts = ewgts;
|
---|
| 1229 | }
|
---|
[13353] | 1230 | /*}}}*/
|
---|
[23231] | 1231 |
|
---|
| 1232 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
---|
| 1233 | /*{{{*/
|
---|
| 1234 | //get the number of non-zero elements in each row, adding to the prior number;
|
---|
| 1235 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
---|
| 1236 | // eg: 0, 1, 3, 6, 8, 13, ...
|
---|
| 1237 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
---|
| 1238 | int c = 0;
|
---|
| 1239 | Jc[c++] = 0;
|
---|
| 1240 |
|
---|
| 1241 | for(int i=0;i<nvtxs;i++){
|
---|
| 1242 | for(int j=0;j<nvtxs;j++){
|
---|
| 1243 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1244 | Jc[c] += 1;
|
---|
| 1245 | }
|
---|
| 1246 | }
|
---|
| 1247 | c++;
|
---|
| 1248 | Jc[c] = Jc[c-1];
|
---|
| 1249 | }
|
---|
| 1250 | return;
|
---|
[22674] | 1251 | }
|
---|
[23231] | 1252 | /*}}}*/
|
---|
[13353] | 1253 |
|
---|
[23231] | 1254 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
---|
| 1255 | /*{{{*/
|
---|
| 1256 | //get the row-wise position of each non-zero element;
|
---|
| 1257 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
---|
| 1258 | // eg: 10, 24, 25, 4, ...
|
---|
| 1259 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
---|
| 1260 | // using pyGetJc to determine which indices correspond to which row
|
---|
| 1261 | int r = 0;
|
---|
| 1262 |
|
---|
| 1263 | for(int i=0;i<nvtxs;i++){
|
---|
| 1264 | for(int j=0;j<nvtxs;j++){
|
---|
| 1265 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1266 | Ir[r] = j;
|
---|
| 1267 | r++;
|
---|
| 1268 | }
|
---|
| 1269 | }
|
---|
| 1270 | }
|
---|
| 1271 | return;
|
---|
| 1272 | }
|
---|
| 1273 | /*}}}*/
|
---|
| 1274 |
|
---|
[12073] | 1275 | /*Python version dependent: */
|
---|
[23708] | 1276 | /* #if _PYTHON_MAJOR_ >= 3 */
|
---|
| 1277 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
---|
| 1278 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
---|
[12073] | 1279 |
|
---|
[23708] | 1280 | /* PyObject* py_bytes; */
|
---|
| 1281 | /* char* string=NULL; */
|
---|
[12073] | 1282 |
|
---|
[23708] | 1283 | /* /\*convert to bytes format: *\/ */
|
---|
| 1284 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
---|
[12073] | 1285 |
|
---|
[23708] | 1286 | /* /\*convert from bytes to string: *\/ */
|
---|
| 1287 | /* string=PyBytes_AsUTF8(py_bytes); */
|
---|
[13622] | 1288 |
|
---|
[23708] | 1289 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
---|
| 1290 | /* *pstring=xNew<char>(strlen(string)+1); */
|
---|
| 1291 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
---|
| 1292 | /* } */
|
---|
| 1293 | /* /\*}}}*\/ */
|
---|
| 1294 | /* #else */
|
---|
[12365] | 1295 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
[12073] | 1296 | void FetchData(char** pstring,PyObject* py_string){
|
---|
| 1297 |
|
---|
| 1298 | /*extract internal string: */
|
---|
[23708] | 1299 | #if _PYTHON_MAJOR_ == 3
|
---|
[24501] | 1300 | const char* string=PyUnicode_AsUTF8(py_string);
|
---|
[23708] | 1301 | #else
|
---|
[24501] | 1302 | const char* string=PyString_AsString(py_string);
|
---|
[23708] | 1303 | #endif
|
---|
[13372] | 1304 | /*copy string (note strlen does not include trailing NULL): */
|
---|
| 1305 | *pstring=xNew<char>(strlen(string)+1);
|
---|
| 1306 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
[12073] | 1307 | }
|
---|
| 1308 | /*}}}*/
|
---|
[23708] | 1309 | //#endif
|
---|