| 1 | /*\file FetchData.cpp:
|
|---|
| 2 | * \brief: general I/O interface to fetch data in python
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifdef HAVE_CONFIG_H
|
|---|
| 6 | #include <config.h>
|
|---|
| 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 |
|
|---|
| 14 | #include "./pythonio.h"
|
|---|
| 15 | #include "../../c/include/include.h"
|
|---|
| 16 | #include "../../c/shared/shared.h"
|
|---|
| 17 |
|
|---|
| 18 | /*Primitive data types*/
|
|---|
| 19 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
|---|
| 20 | void FetchData(double* pscalar,PyObject* py_float){
|
|---|
| 21 |
|
|---|
| 22 | double dscalar;
|
|---|
| 23 |
|
|---|
| 24 | /*return internal value: */
|
|---|
| 25 | if (PyFloat_Check(py_float))
|
|---|
| 26 | dscalar=PyFloat_AsDouble(py_float);
|
|---|
| 27 | else if (PyInt_Check(py_float))
|
|---|
| 28 | dscalar=(double)PyInt_AsLong(py_float);
|
|---|
| 29 | else if (PyLong_Check(py_float))
|
|---|
| 30 | dscalar=PyLong_AsDouble(py_float);
|
|---|
| 31 | else if (PyBool_Check(py_float))
|
|---|
| 32 | dscalar=(double)PyLong_AsLong(py_float);
|
|---|
| 33 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
|---|
| 34 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 35 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
|---|
| 36 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 37 | else
|
|---|
| 38 | _error_("unrecognized float type in input!");
|
|---|
| 39 |
|
|---|
| 40 | /*output: */
|
|---|
| 41 | *pscalar=dscalar;
|
|---|
| 42 | }
|
|---|
| 43 | /*}}}*/
|
|---|
| 44 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
|---|
| 45 | void FetchData(int* pscalar, PyObject* py_long){
|
|---|
| 46 |
|
|---|
| 47 | int iscalar;
|
|---|
| 48 |
|
|---|
| 49 | /*return internal value: */
|
|---|
| 50 | if (PyInt_Check(py_long))
|
|---|
| 51 | iscalar=(int)PyInt_AsLong(py_long);
|
|---|
| 52 | else if (PyLong_Check(py_long))
|
|---|
| 53 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 54 | else if (PyFloat_Check(py_long))
|
|---|
| 55 | iscalar=(int)PyFloat_AsDouble(py_long);
|
|---|
| 56 | else if (PyBool_Check(py_long))
|
|---|
| 57 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 58 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
|---|
| 59 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 60 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
|---|
| 61 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 62 | else
|
|---|
| 63 | _error_("unrecognized long type in input!");
|
|---|
| 64 |
|
|---|
| 65 | /*output: */
|
|---|
| 66 | *pscalar=iscalar;
|
|---|
| 67 | }
|
|---|
| 68 | /*}}}*/
|
|---|
| 69 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
|---|
| 70 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
|---|
| 71 |
|
|---|
| 72 | bool bscalar;
|
|---|
| 73 |
|
|---|
| 74 | /*return internal value: */
|
|---|
| 75 | if (PyBool_Check(py_boolean))
|
|---|
| 76 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 77 | else if (PyInt_Check(py_boolean))
|
|---|
| 78 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
|---|
| 79 | else if (PyLong_Check(py_boolean))
|
|---|
| 80 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 81 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
|---|
| 82 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 83 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
|---|
| 84 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 85 | else
|
|---|
| 86 | _error_("unrecognized boolean type in input!");
|
|---|
| 87 |
|
|---|
| 88 | /*output: */
|
|---|
| 89 | *pscalar=bscalar;
|
|---|
| 90 | }
|
|---|
| 91 | /*}}}*/
|
|---|
| 92 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 93 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 94 |
|
|---|
| 95 | /*output: */
|
|---|
| 96 | double* dmatrix=NULL;
|
|---|
| 97 | double* matrix=NULL;
|
|---|
| 98 | int M,N;
|
|---|
| 99 | int ndim;
|
|---|
| 100 | npy_intp* dims=NULL;
|
|---|
| 101 |
|
|---|
| 102 | /*intermediary:*/
|
|---|
| 103 | long* lmatrix=NULL;
|
|---|
| 104 | bool* bmatrix=NULL;
|
|---|
| 105 | int i;
|
|---|
| 106 |
|
|---|
| 107 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 108 | /*retrieve dimensions: */
|
|---|
| 109 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 110 | if (ndim==2) {
|
|---|
| 111 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 112 | M=dims[0]; N=dims[1];
|
|---|
| 113 | }
|
|---|
| 114 | else if (ndim==1) {
|
|---|
| 115 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 116 | M=dims[0]; N=1;
|
|---|
| 117 | }
|
|---|
| 118 | else
|
|---|
| 119 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 120 |
|
|---|
| 121 | if (M && N) {
|
|---|
| 122 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 123 | /*retrieve internal value: */
|
|---|
| 124 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 125 |
|
|---|
| 126 | /*copy matrix: */
|
|---|
| 127 | matrix=xNew<double>(M*N);
|
|---|
| 128 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
|---|
| 132 | /*retrieve internal value: */
|
|---|
| 133 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 134 |
|
|---|
| 135 | /*transform into double matrix: */
|
|---|
| 136 | matrix=xNew<double>(M*N);
|
|---|
| 137 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 141 | /*retrieve internal value: */
|
|---|
| 142 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 143 |
|
|---|
| 144 | /*transform into double matrix: */
|
|---|
| 145 | matrix=xNew<double>(M*N);
|
|---|
| 146 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | else
|
|---|
| 150 | _error_("unrecognized double pyarray type in input!");
|
|---|
| 151 | }
|
|---|
| 152 | else
|
|---|
| 153 | matrix=NULL;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | else {
|
|---|
| 157 | M=1;
|
|---|
| 158 | N=1;
|
|---|
| 159 | matrix=xNew<double>(M*N);
|
|---|
| 160 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /*output: */
|
|---|
| 164 | if(pM)*pM=M;
|
|---|
| 165 | if(pN)*pN=N;
|
|---|
| 166 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 167 | }
|
|---|
| 168 | /*}}}*/
|
|---|
| 169 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 170 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 171 |
|
|---|
| 172 | /*output: */
|
|---|
| 173 | int* matrix=NULL;
|
|---|
| 174 | int M,N;
|
|---|
| 175 | int ndim;
|
|---|
| 176 | npy_intp* dims=NULL;
|
|---|
| 177 |
|
|---|
| 178 | /*intermediary:*/
|
|---|
| 179 | double* dmatrix=NULL;
|
|---|
| 180 | long* lmatrix=NULL;
|
|---|
| 181 | bool* bmatrix=NULL;
|
|---|
| 182 | int i;
|
|---|
| 183 |
|
|---|
| 184 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 185 | /*retrieve dimensions: */
|
|---|
| 186 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 187 | if (ndim==2) {
|
|---|
| 188 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 189 | M=dims[0]; N=dims[1];
|
|---|
| 190 | }
|
|---|
| 191 | else if (ndim==1) {
|
|---|
| 192 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 193 | M=dims[0]; N=1;
|
|---|
| 194 | }
|
|---|
| 195 | else
|
|---|
| 196 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 197 |
|
|---|
| 198 | if (M && N) {
|
|---|
| 199 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 200 | /*retrieve internal value: */
|
|---|
| 201 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 202 |
|
|---|
| 203 | /*transform into integer matrix: */
|
|---|
| 204 | matrix=xNew<int>(M*N);
|
|---|
| 205 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
|---|
| 209 | /*retrieve internal value: */
|
|---|
| 210 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 211 |
|
|---|
| 212 | /*transform into integer matrix: */
|
|---|
| 213 | matrix=xNew<int>(M*N);
|
|---|
| 214 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 218 | /*retrieve internal value: */
|
|---|
| 219 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 220 |
|
|---|
| 221 | /*transform into integer matrix: */
|
|---|
| 222 | matrix=xNew<int>(M*N);
|
|---|
| 223 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | else
|
|---|
| 227 | _error_("unrecognized int pyarray type in input!");
|
|---|
| 228 | }
|
|---|
| 229 | else
|
|---|
| 230 | matrix=NULL;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | else {
|
|---|
| 234 | M=1;
|
|---|
| 235 | N=1;
|
|---|
| 236 | matrix=xNew<int>(M*N);
|
|---|
| 237 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /*output: */
|
|---|
| 241 | if(pM)*pM=M;
|
|---|
| 242 | if(pN)*pN=N;
|
|---|
| 243 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 244 | }
|
|---|
| 245 | /*}}}*/
|
|---|
| 246 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 247 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 248 |
|
|---|
| 249 | /*output: */
|
|---|
| 250 | bool* bmatrix=NULL;
|
|---|
| 251 | bool* matrix=NULL;
|
|---|
| 252 | int M,N;
|
|---|
| 253 | int ndim;
|
|---|
| 254 | npy_intp* dims=NULL;
|
|---|
| 255 |
|
|---|
| 256 | /*intermediary:*/
|
|---|
| 257 | double* dmatrix=NULL;
|
|---|
| 258 | long* lmatrix=NULL;
|
|---|
| 259 | int i;
|
|---|
| 260 |
|
|---|
| 261 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 262 | /*retrieve dimensions: */
|
|---|
| 263 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 264 | if (ndim==2) {
|
|---|
| 265 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 266 | M=dims[0]; N=dims[1];
|
|---|
| 267 | }
|
|---|
| 268 | else if (ndim==1) {
|
|---|
| 269 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 270 | M=dims[0]; N=1;
|
|---|
| 271 | }
|
|---|
| 272 | else
|
|---|
| 273 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 274 |
|
|---|
| 275 | if (M && N) {
|
|---|
| 276 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 277 | /*retrieve internal value: */
|
|---|
| 278 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 279 |
|
|---|
| 280 | /*transform into bool matrix: */
|
|---|
| 281 | matrix=xNew<bool>(M*N);
|
|---|
| 282 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
|---|
| 286 | /*retrieve internal value: */
|
|---|
| 287 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 288 |
|
|---|
| 289 | /*transform into bool matrix: */
|
|---|
| 290 | matrix=xNew<bool>(M*N);
|
|---|
| 291 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 295 | /*retrieve internal value: */
|
|---|
| 296 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 297 |
|
|---|
| 298 | /*copy matrix: */
|
|---|
| 299 | matrix=xNew<bool>(M*N);
|
|---|
| 300 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | else
|
|---|
| 304 | _error_("unrecognized bool pyarray type in input!");
|
|---|
| 305 | }
|
|---|
| 306 | else
|
|---|
| 307 | matrix=NULL;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | else {
|
|---|
| 311 | M=1;
|
|---|
| 312 | N=1;
|
|---|
| 313 | matrix=xNew<bool>(M*N);
|
|---|
| 314 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /*output: */
|
|---|
| 318 | if(pM)*pM=M;
|
|---|
| 319 | if(pN)*pN=N;
|
|---|
| 320 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 321 | }
|
|---|
| 322 | /*}}}*/
|
|---|
| 323 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 324 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
|---|
| 325 |
|
|---|
| 326 | /*output: */
|
|---|
| 327 | double* dvector=NULL;
|
|---|
| 328 | double* vector=NULL;
|
|---|
| 329 | int M;
|
|---|
| 330 | int ndim;
|
|---|
| 331 | npy_intp* dims=NULL;
|
|---|
| 332 |
|
|---|
| 333 | /*intermediary:*/
|
|---|
| 334 | long* lvector=NULL;
|
|---|
| 335 | bool* bvector=NULL;
|
|---|
| 336 | int i;
|
|---|
| 337 |
|
|---|
| 338 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 339 | /*retrieve dimensions: */
|
|---|
| 340 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 341 | if (ndim==1) {
|
|---|
| 342 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 343 | M=dims[0];
|
|---|
| 344 | }
|
|---|
| 345 | else if (ndim==2) {
|
|---|
| 346 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 347 | if (dims[1]==1)
|
|---|
| 348 | M=dims[0];
|
|---|
| 349 | else
|
|---|
| 350 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 351 | }
|
|---|
| 352 | else
|
|---|
| 353 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 354 |
|
|---|
| 355 | if (M) {
|
|---|
| 356 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 357 | /*retrieve internal value: */
|
|---|
| 358 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 359 |
|
|---|
| 360 | /*copy vector: */
|
|---|
| 361 | vector=xNew<double>(M);
|
|---|
| 362 | memcpy(vector,dvector,(M)*sizeof(double));
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
|---|
| 366 | /*retrieve internal value: */
|
|---|
| 367 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 368 |
|
|---|
| 369 | /*transform into double vector: */
|
|---|
| 370 | vector=xNew<double>(M);
|
|---|
| 371 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 375 | /*retrieve internal value: */
|
|---|
| 376 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 377 |
|
|---|
| 378 | /*transform into double vector: */
|
|---|
| 379 | vector=xNew<double>(M);
|
|---|
| 380 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | else
|
|---|
| 384 | _error_("unrecognized double pyarray type in input!");
|
|---|
| 385 | }
|
|---|
| 386 | else
|
|---|
| 387 | vector=NULL;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | else {
|
|---|
| 391 | M=1;
|
|---|
| 392 | vector=xNew<double>(M);
|
|---|
| 393 | FetchData(&(vector[0]),py_vector);
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /*output: */
|
|---|
| 397 | if(pM)*pM=M;
|
|---|
| 398 | if(pvector)*pvector=vector;
|
|---|
| 399 | }
|
|---|
| 400 | /*}}}*/
|
|---|
| 401 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 402 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
|---|
| 403 |
|
|---|
| 404 | /*output: */
|
|---|
| 405 | int* vector=NULL;
|
|---|
| 406 | int M;
|
|---|
| 407 | int ndim;
|
|---|
| 408 | npy_intp* dims=NULL;
|
|---|
| 409 |
|
|---|
| 410 | /*intermediary:*/
|
|---|
| 411 | long* lvector=NULL;
|
|---|
| 412 | bool* bvector=NULL;
|
|---|
| 413 | double* dvector=NULL;
|
|---|
| 414 | int i;
|
|---|
| 415 |
|
|---|
| 416 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 417 | /*retrieve dimensions: */
|
|---|
| 418 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 419 | if (ndim==1) {
|
|---|
| 420 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 421 | M=dims[0];
|
|---|
| 422 | }
|
|---|
| 423 | else if (ndim==2) {
|
|---|
| 424 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 425 | if (dims[1]==1)
|
|---|
| 426 | M=dims[0];
|
|---|
| 427 | else
|
|---|
| 428 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 429 | }
|
|---|
| 430 | else
|
|---|
| 431 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 432 |
|
|---|
| 433 | if (M) {
|
|---|
| 434 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 435 | /*retrieve internal value: */
|
|---|
| 436 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 437 |
|
|---|
| 438 | /*transform into int vector: */
|
|---|
| 439 | vector=xNew<int>(M);
|
|---|
| 440 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
|---|
| 444 | /*retrieve internal value: */
|
|---|
| 445 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 446 |
|
|---|
| 447 | /*transform into int vector: */
|
|---|
| 448 | vector=xNew<int>(M);
|
|---|
| 449 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 453 | /*retrieve internal value: */
|
|---|
| 454 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 455 |
|
|---|
| 456 | /*transform into int vector: */
|
|---|
| 457 | vector=xNew<int>(M);
|
|---|
| 458 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | else
|
|---|
| 462 | _error_("unrecognized int pyarray type in input!");
|
|---|
| 463 | }
|
|---|
| 464 | else
|
|---|
| 465 | vector=NULL;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | else {
|
|---|
| 469 | M=1;
|
|---|
| 470 | vector=xNew<int>(M);
|
|---|
| 471 | FetchData(&(vector[0]),py_vector);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /*output: */
|
|---|
| 475 | if(pM)*pM=M;
|
|---|
| 476 | if(pvector)*pvector=vector;
|
|---|
| 477 | }
|
|---|
| 478 | /*}}}*/
|
|---|
| 479 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 480 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
|---|
| 481 |
|
|---|
| 482 | /*output: */
|
|---|
| 483 | bool* bvector=NULL;
|
|---|
| 484 | bool* vector=NULL;
|
|---|
| 485 | int M;
|
|---|
| 486 | int ndim;
|
|---|
| 487 | npy_intp* dims=NULL;
|
|---|
| 488 |
|
|---|
| 489 | /*intermediary:*/
|
|---|
| 490 | double* dvector=NULL;
|
|---|
| 491 | long* lvector=NULL;
|
|---|
| 492 | int i;
|
|---|
| 493 |
|
|---|
| 494 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 495 | /*retrieve dimensions: */
|
|---|
| 496 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 497 | if (ndim==1) {
|
|---|
| 498 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 499 | M=dims[0];
|
|---|
| 500 | }
|
|---|
| 501 | else if (ndim==2) {
|
|---|
| 502 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 503 | if (dims[1]==1)
|
|---|
| 504 | M=dims[0];
|
|---|
| 505 | else
|
|---|
| 506 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 507 | }
|
|---|
| 508 | else
|
|---|
| 509 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 510 |
|
|---|
| 511 | if (M) {
|
|---|
| 512 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 513 | /*retrieve internal value: */
|
|---|
| 514 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 515 |
|
|---|
| 516 | /*transform into bool vector: */
|
|---|
| 517 | vector=xNew<bool>(M);
|
|---|
| 518 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
|---|
| 522 | /*retrieve internal value: */
|
|---|
| 523 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 524 |
|
|---|
| 525 | /*transform into bool vector: */
|
|---|
| 526 | vector=xNew<bool>(M);
|
|---|
| 527 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 531 | /*retrieve internal value: */
|
|---|
| 532 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 533 |
|
|---|
| 534 | /*copy vector: */
|
|---|
| 535 | vector=xNew<bool>(M);
|
|---|
| 536 | memcpy(vector,bvector,(M)*sizeof(bool));
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | else
|
|---|
| 540 | _error_("unrecognized bool pyarray type in input!");
|
|---|
| 541 | }
|
|---|
| 542 | else
|
|---|
| 543 | vector=NULL;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | else {
|
|---|
| 547 | M=1;
|
|---|
| 548 | vector=xNew<bool>(M);
|
|---|
| 549 | FetchData(&(vector[0]),py_vector);
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | /*output: */
|
|---|
| 553 | if(pM)*pM=M;
|
|---|
| 554 | if(pvector)*pvector=vector;
|
|---|
| 555 | }
|
|---|
| 556 | /*}}}*/
|
|---|
| 557 |
|
|---|
| 558 | /*ISSM objects*/
|
|---|
| 559 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
|---|
| 560 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
|---|
| 561 |
|
|---|
| 562 | /*Initialize output*/
|
|---|
| 563 | BamgGeom* bamggeom=new BamgGeom();
|
|---|
| 564 |
|
|---|
| 565 | /*Fetch all fields*/
|
|---|
| 566 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
|---|
| 567 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
|---|
| 568 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
|---|
| 569 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
|---|
| 570 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
|---|
| 571 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
|---|
| 572 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
|---|
| 573 |
|
|---|
| 574 | /*Assign output pointers:*/
|
|---|
| 575 | *pbamggeom=bamggeom;
|
|---|
| 576 | }
|
|---|
| 577 | /*}}}*/
|
|---|
| 578 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
|---|
| 579 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
|---|
| 580 |
|
|---|
| 581 | /*Initialize output*/
|
|---|
| 582 | BamgMesh* bamgmesh=new BamgMesh();
|
|---|
| 583 |
|
|---|
| 584 | /*Fetch all fields*/
|
|---|
| 585 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
|---|
| 586 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
|---|
| 587 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
|---|
| 588 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
|---|
| 589 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
|---|
| 590 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
|---|
| 591 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
|---|
| 592 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
|---|
| 593 |
|
|---|
| 594 | /*Assign output pointers:*/
|
|---|
| 595 | *pbamgmesh=bamgmesh;
|
|---|
| 596 | }
|
|---|
| 597 | /*}}}*/
|
|---|
| 598 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
|---|
| 599 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
|---|
| 600 |
|
|---|
| 601 | /*Initialize output*/
|
|---|
| 602 | BamgOpts* bamgopts=new BamgOpts();
|
|---|
| 603 |
|
|---|
| 604 | /*Fetch all fields*/
|
|---|
| 605 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
|---|
| 606 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
|---|
| 607 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
|---|
| 608 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
|---|
| 609 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
|---|
| 610 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
|---|
| 611 | FetchData(&bamgopts->MaxCornerAngle,PyDict_GetItemString(py_dict,"MaxCornerAngle"));
|
|---|
| 612 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
|---|
| 613 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
|---|
| 614 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
|---|
| 615 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
|---|
| 616 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
|---|
| 617 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
|---|
| 618 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
|---|
| 619 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
|---|
| 620 |
|
|---|
| 621 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
|---|
| 622 | FetchData(&bamgopts->geometricalmetric,PyDict_GetItemString(py_dict,"geometricalmetric"));
|
|---|
| 623 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
|---|
| 624 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
|---|
| 625 |
|
|---|
| 626 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
|---|
| 627 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
|---|
| 628 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
|---|
| 629 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
|---|
| 630 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
|
|---|
| 631 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
|---|
| 632 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
|---|
| 633 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
|---|
| 634 |
|
|---|
| 635 | /*Additional checks*/
|
|---|
| 636 | bamgopts->Check();
|
|---|
| 637 |
|
|---|
| 638 | /*Assign output pointers:*/
|
|---|
| 639 | *pbamgopts=bamgopts;
|
|---|
| 640 | }
|
|---|
| 641 | /*}}}*/
|
|---|
| 642 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
|---|
| 643 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
|---|
| 644 |
|
|---|
| 645 | char *name = NULL;
|
|---|
| 646 | Option *option = NULL;
|
|---|
| 647 |
|
|---|
| 648 | /*Initialize output*/
|
|---|
| 649 | Options* options=new Options();
|
|---|
| 650 |
|
|---|
| 651 | /*Fetch all options*/
|
|---|
| 652 | for (int i=istart; i<nrhs; i=i+2){
|
|---|
| 653 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
|---|
| 654 |
|
|---|
| 655 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
|---|
| 656 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
|---|
| 657 |
|
|---|
| 658 | _pprintLine_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!");
|
|---|
| 659 |
|
|---|
| 660 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
|---|
| 661 | // options->AddOption(option);
|
|---|
| 662 | // option=NULL;
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | /*Assign output pointers:*/
|
|---|
| 666 | *poptions=options;
|
|---|
| 667 | }
|
|---|
| 668 | /*}}}*/
|
|---|
| 669 | /*FUNCTION FetchData(DataSet** pcontours,PyObject* py_list){{{*/
|
|---|
| 670 | void FetchData(DataSet** pcontours,PyObject* py_list){
|
|---|
| 671 |
|
|---|
| 672 | int numcontours,test1,test2;
|
|---|
| 673 | char *contourname = NULL;
|
|---|
| 674 | DataSet *contours = NULL;
|
|---|
| 675 | Contour<double> *contouri = NULL;
|
|---|
| 676 | PyObject *py_dicti = NULL;
|
|---|
| 677 | PyObject *py_item = NULL;
|
|---|
| 678 |
|
|---|
| 679 | if (PyString_Check(py_list)){
|
|---|
| 680 | FetchData(&contourname,py_list);
|
|---|
| 681 | contours=DomainOutlineRead<double>(contourname);
|
|---|
| 682 | }
|
|---|
| 683 | else if(PyList_Check(py_list)){
|
|---|
| 684 |
|
|---|
| 685 | contours=new DataSet(0);
|
|---|
| 686 | numcontours=(int)PyList_Size(py_list);
|
|---|
| 687 |
|
|---|
| 688 | for(int i=0;i<numcontours;i++){
|
|---|
| 689 |
|
|---|
| 690 | contouri=xNew<Contour<double> >(1);
|
|---|
| 691 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
|---|
| 692 |
|
|---|
| 693 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
|---|
| 694 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
|---|
| 695 | FetchData(&contouri->nods,py_item);
|
|---|
| 696 |
|
|---|
| 697 | py_item = PyDict_GetItemString(py_dicti,"x");
|
|---|
| 698 | if(!py_item) _error_("input structure does not have a 'x' field");
|
|---|
| 699 | FetchData(&contouri->x,&test1,&test2,py_item);
|
|---|
| 700 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
|---|
| 701 |
|
|---|
| 702 | py_item = PyDict_GetItemString(py_dicti,"y");
|
|---|
| 703 | if(!py_item) _error_("input structure does not have a 'y' field");
|
|---|
| 704 | FetchData(&contouri->y,&test1,&test2,py_item);
|
|---|
| 705 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
|---|
| 706 |
|
|---|
| 707 | contours->AddObject(contouri);
|
|---|
| 708 | }
|
|---|
| 709 | }
|
|---|
| 710 | else{
|
|---|
| 711 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | /*clean-up and assign output pointer*/
|
|---|
| 715 | xDelete<char>(contourname);
|
|---|
| 716 | *pcontours=contours;
|
|---|
| 717 | }
|
|---|
| 718 | /*}}}*/
|
|---|
| 719 |
|
|---|
| 720 | /*Python version dependent: */
|
|---|
| 721 | #if _PYTHON_MAJOR_ >= 3
|
|---|
| 722 | /*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
|
|---|
| 723 | void FetchData(char** pstring,PyObject* py_unicode){
|
|---|
| 724 |
|
|---|
| 725 | PyObject* py_bytes;
|
|---|
| 726 | char* string=NULL;
|
|---|
| 727 |
|
|---|
| 728 | /*convert to bytes format: */
|
|---|
| 729 | PyUnicode_FSConverter(py_unicode,&py_bytes);
|
|---|
| 730 |
|
|---|
| 731 | /*convert from bytes to string: */
|
|---|
| 732 | string=PyBytes_AS_STRING(py_bytes);
|
|---|
| 733 |
|
|---|
| 734 | *pstring=string;
|
|---|
| 735 | }
|
|---|
| 736 | /*}}}*/
|
|---|
| 737 | #else
|
|---|
| 738 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
|---|
| 739 | void FetchData(char** pstring,PyObject* py_string){
|
|---|
| 740 |
|
|---|
| 741 | char* string=NULL;
|
|---|
| 742 |
|
|---|
| 743 | /*extract internal string: */
|
|---|
| 744 | string=PyString_AsString(py_string);
|
|---|
| 745 |
|
|---|
| 746 | /*copy string (note strlen does not include trailing NULL): */
|
|---|
| 747 | *pstring=xNew<char>(strlen(string)+1);
|
|---|
| 748 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
|---|
| 749 | }
|
|---|
| 750 | /*}}}*/
|
|---|
| 751 | #endif
|
|---|