source: issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp@ 23231

Last change on this file since 23231 was 23231, checked in by kruegern, 7 years ago

NEW: added Dakota/QMU Chaco tests (234,235,413,414,417,444), minor bug fixes, left out 418 due to unresolved memory issues

File size: 29.7 KB
Line 
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/shared/shared.h"
16
17/*Primitive data types*/
18/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
19void FetchData(double* pscalar,PyObject* py_float){
20
21 double dscalar;
22
23 /*return internal value: */
24 if (PyFloat_Check(py_float))
25 dscalar=PyFloat_AsDouble(py_float);
26 else if (PyLong_Check(py_float)){
27 #if _PYTHON_MAJOR_ == 3
28 dscalar=(double)PyLong_AsLong(py_float);
29 #else
30 dscalar=PyLong_AsDouble(py_float);
31 #endif
32 }
33 else if (PyInt_Check(py_float))
34 dscalar=(double)PyInt_AsLong(py_float);
35 else if (PyBool_Check(py_float))
36 dscalar=(double)PyLong_AsLong(py_float);
37 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
38 FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
39 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
40 FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
41 else
42 _error_("unrecognized float type in input!");
43
44 /*output: */
45 *pscalar=dscalar;
46}
47/*}}}*/
48/*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
49void FetchData(float* pscalar,PyObject* py_float){
50
51 float fscalar;
52
53 /*return internal value: */
54 if (PyFloat_Check(py_float))
55 fscalar=PyFloat_AsDouble(py_float);
56 else if (PyLong_Check(py_float)){
57 #if _PYTHON_MAJOR_ == 3
58 fscalar=(float)PyLong_AsLong(py_float);
59 #else
60 fscalar=(float)PyLong_AsDouble(py_float);
61 #endif
62 }
63 else if (PyInt_Check(py_float))
64 fscalar=(float)PyInt_AsLong(py_float);
65 else if (PyBool_Check(py_float))
66 fscalar=(float)PyLong_AsLong(py_float);
67 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
68 FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
69 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
70 FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
71 else
72 _error_("unrecognized float type in input!");
73
74 /*output: */
75 *pscalar=fscalar;
76}
77/*}}}*/
78/*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
79void FetchData(int* pscalar, PyObject* py_long){
80
81 int iscalar;
82
83 /*return internal value: */
84 if (PyLong_Check(py_long))
85 iscalar=(int)PyLong_AsLong(py_long);
86 else if (PyInt_Check(py_long))
87 iscalar=(int)PyInt_AsLong(py_long);
88 else if (PyFloat_Check(py_long))
89 iscalar=(int)PyFloat_AsDouble(py_long);
90 else if (PyBool_Check(py_long))
91 iscalar=(int)PyLong_AsLong(py_long);
92 else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
93 FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
94 else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
95 FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
96 else
97 _error_("unrecognized long type in input!");
98
99 /*output: */
100 *pscalar=iscalar;
101}
102/*}}}*/
103/*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
104void FetchData(bool* pscalar,PyObject* py_boolean){
105
106 bool bscalar;
107
108 /*return internal value: */
109 if (PyBool_Check(py_boolean))
110 bscalar=(bool)PyLong_AsLong(py_boolean);
111 else if (PyLong_Check(py_boolean))
112 bscalar=(bool)PyLong_AsLong(py_boolean);
113 else if (PyLong_Check(py_boolean))
114 bscalar=(bool)PyLong_AsLong(py_boolean);
115 else if (PyInt_Check(py_boolean))
116 bscalar=(bool)PyInt_AsLong(py_boolean);
117 else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
118 FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
119 else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
120 FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
121 else
122 _error_("unrecognized boolean type in input!");
123
124 /*output: */
125 *pscalar=bscalar;
126}
127/*}}}*/
128/*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
129void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
130
131 /*output: */
132 double* dmatrix=NULL;
133 double* matrix=NULL;
134 int M=0;
135 int N=0;
136 int ndim;
137 npy_intp* dims=NULL;
138
139 /*intermediary:*/
140 long* lmatrix=NULL;
141 bool* bmatrix=NULL;
142 int i;
143 PyObject* py_matrix2=NULL;
144
145 if (PyArray_Check((PyArrayObject*)py_matrix)) {
146 /*retrieve dimensions: */
147 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
148 if (ndim==2) {
149 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
150 M=dims[0]; N=dims[1];
151 }
152 else if (ndim==1) {
153 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
154 M=dims[0]; N=1;
155 }
156 else
157 _error_("expecting an MxN matrix or M vector in input!");
158
159 if (M && N) {
160 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
161 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
162 py_matrix=py_matrix2;
163 }
164
165 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
166 /*retrieve internal value: */
167 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
168
169 /*copy matrix: */
170 matrix=xNew<double>(M*N);
171// if (PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
172 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
173// }
174
175// else {
176// int j,k,ipt=0;
177// int mstride,nstride;
178// mstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,0)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
179// if (ndim > 1)
180// nstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,1)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
181// else
182// nstride=1;
183// for (i=0; i<M; i++) {
184// k=i*mstride;
185// for (j=0; j<N; j++) {
186// matrix[ipt++]=dmatrix[k];
187// k+=nstride;
188// }
189// }
190// }
191
192 }
193
194 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
195 /*retrieve internal value: */
196 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
197
198 /*transform into double matrix: */
199 matrix=xNew<double>(M*N);
200 for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
201 }
202
203 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
204 /*retrieve internal value: */
205 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
206
207 /*transform into double matrix: */
208 matrix=xNew<double>(M*N);
209 for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
210 }
211
212 else
213 _error_("unrecognized double pyarray type in input!");
214
215 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
216 }
217 else
218 matrix=NULL;
219 }
220
221 else {
222 M=1;
223 N=1;
224 matrix=xNew<double>(M*N);
225 FetchData(&(matrix[0]),py_matrix);
226 }
227
228 /*output: */
229 if(pM)*pM=M;
230 if(pN)*pN=N;
231 if(pmatrix)*pmatrix=matrix;
232}
233/*}}}*/
234/*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
235void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
236
237 /*output: */
238 int* matrix=NULL;
239 int M=0;
240 int N=0;
241 int ndim;
242 npy_intp* dims=NULL;
243
244 /*intermediary:*/
245 double* dmatrix=NULL;
246 long* lmatrix=NULL;
247 bool* bmatrix=NULL;
248 int i;
249 PyObject* py_matrix2=NULL;
250
251 if (PyArray_Check((PyArrayObject*)py_matrix)) {
252 /*retrieve dimensions: */
253 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
254 if (ndim==2) {
255 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
256 M=dims[0]; N=dims[1];
257 }
258 else if (ndim==1) {
259 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
260 M=dims[0]; N=1;
261 }
262 else
263 _error_("expecting an MxN matrix or M vector in input!");
264
265 if (M && N) {
266 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
267 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
268 py_matrix=py_matrix2;
269 }
270
271 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
272 /*retrieve internal value: */
273 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
274
275 /*transform into integer matrix: */
276 matrix=xNew<int>(M*N);
277 for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
278 }
279
280 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
281 /*retrieve internal value: */
282 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
283
284 /*transform into integer matrix: */
285 matrix=xNew<int>(M*N);
286 for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
287 }
288
289 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
290 /*retrieve internal value: */
291 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
292
293 /*transform into integer matrix: */
294 matrix=xNew<int>(M*N);
295 for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
296 }
297
298 else
299 _error_("unrecognized int pyarray type in input!");
300
301 /* These lines are causing a segfault
302 if (py_matrix2)
303 delete(py_matrix2);
304 */
305 }
306 else
307 matrix=NULL;
308 }
309
310 else {
311 M=1;
312 N=1;
313 matrix=xNew<int>(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(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
324void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
325
326 /*output: */
327 bool* bmatrix=NULL;
328 bool* matrix=NULL;
329 int M=0;
330 int N=0;
331 int ndim;
332 npy_intp* dims=NULL;
333
334 /*intermediary:*/
335 double* dmatrix=NULL;
336 long* lmatrix=NULL;
337 int i;
338 PyObject* py_matrix2=NULL;
339
340 if (PyArray_Check((PyArrayObject*)py_matrix)) {
341 /*retrieve dimensions: */
342 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
343 if (ndim==2) {
344 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
345 M=dims[0]; N=dims[1];
346 }
347 else if (ndim==1) {
348 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
349 M=dims[0]; N=1;
350 }
351 else
352 _error_("expecting an MxN matrix or M vector in input!");
353
354 if (M && N) {
355 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
356 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
357 py_matrix=py_matrix2;
358 }
359
360 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
361 /*retrieve internal value: */
362 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
363
364 /*transform into bool matrix: */
365 matrix=xNew<bool>(M*N);
366 for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
367 }
368
369 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
370 /*retrieve internal value: */
371 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
372
373 /*transform into bool matrix: */
374 matrix=xNew<bool>(M*N);
375 for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
376 }
377
378 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
379 /*retrieve internal value: */
380 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
381
382 /*copy matrix: */
383 matrix=xNew<bool>(M*N);
384 memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
385 }
386
387 else
388 _error_("unrecognized bool pyarray type in input!");
389
390 if (py_matrix2)
391 delete(py_matrix2);
392 }
393 else
394 matrix=NULL;
395 }
396
397 else {
398 M=1;
399 N=1;
400 matrix=xNew<bool>(M*N);
401 FetchData(&(matrix[0]),py_matrix);
402 }
403
404 /*output: */
405 if(pM)*pM=M;
406 if(pN)*pN=N;
407 if(pmatrix)*pmatrix=matrix;
408}
409/*}}}*/
410/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
411void FetchData(double** pvector,int* pM,PyObject* py_vector){
412
413 /*output: */
414 double* dvector=NULL;
415 double* vector=NULL;
416 int M=0;
417 int ndim;
418 npy_intp* dims=NULL;
419
420 /*intermediary:*/
421 long* lvector=NULL;
422 bool* bvector=NULL;
423 int i;
424 PyObject* py_vector2=NULL;
425
426 if (PyArray_Check((PyArrayObject*)py_vector)) {
427 /*retrieve dimensions: */
428 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
429 if (ndim==1) {
430 dims=PyArray_DIMS((PyArrayObject*)py_vector);
431 M=dims[0];
432 }
433 else if (ndim==2) {
434 dims=PyArray_DIMS((PyArrayObject*)py_vector);
435 if (dims[1]==1)
436 M=dims[0];
437 else
438 _error_("expecting an Mx1 matrix or M vector in input!");
439 }
440 else
441 _error_("expecting an Mx1 matrix or M vector in input!");
442
443 if (M) {
444 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
445 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
446 py_vector=py_vector2;
447 }
448
449 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
450 /*retrieve internal value: */
451 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
452
453 /*copy vector: */
454 vector=xNew<double>(M);
455 memcpy(vector,dvector,(M)*sizeof(double));
456 }
457
458 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
459 /*retrieve internal value: */
460 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
461
462 /*transform into double vector: */
463 vector=xNew<double>(M);
464 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
465 }
466
467 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
468 /*retrieve internal value: */
469 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
470
471 /*transform into double vector: */
472 vector=xNew<double>(M);
473 for(i=0;i<M;i++)vector[i]=(double)bvector[i];
474 }
475
476 else
477 _error_("unrecognized double pyarray type in input!");
478
479 /* Causing a seg fault.
480 if (py_vector2)
481 delete(py_vector2);
482 */
483 }
484 else
485 vector=NULL;
486 }
487
488 else {
489 M=1;
490 vector=xNew<double>(M);
491 FetchData(&(vector[0]),py_vector);
492 }
493
494 /*output: */
495 if(pM)*pM=M;
496 if(pvector)*pvector=vector;
497}
498/*}}}*/
499/*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
500void FetchData(float** pvector,int* pM,PyObject* py_vector){
501
502 /*output: */
503 float* vector=NULL;
504 int M=0;
505 int ndim;
506 npy_intp* dims=NULL;
507
508 /*intermediary:*/
509 long* lvector=NULL;
510 bool* bvector=NULL;
511 double* dvector=NULL;
512 int i;
513 PyObject* py_vector2=NULL;
514
515 if (PyArray_Check((PyArrayObject*)py_vector)) {
516 /*retrieve dimensions: */
517 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
518 if (ndim==1) {
519 dims=PyArray_DIMS((PyArrayObject*)py_vector);
520 M=dims[0];
521 }
522 else if (ndim==2) {
523 dims=PyArray_DIMS((PyArrayObject*)py_vector);
524 if (dims[1]==1)
525 M=dims[0];
526 else
527 _error_("expecting an Mx1 matrix or M vector in input!");
528 }
529 else
530 _error_("expecting an Mx1 matrix or M vector in input!");
531
532 if (M) {
533 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
534 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
535 py_vector=py_vector2;
536 }
537
538 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
539 /*retrieve internal value: */
540 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
541
542 /*transform into int vector: */
543 vector=xNew<float>(M);
544 for(i=0;i<M;i++)vector[i]=(float)dvector[i];
545 }
546
547 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
548 /*retrieve internal value: */
549 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
550
551 /*transform into int vector: */
552 vector=xNew<float>(M);
553 for(i=0;i<M;i++)vector[i]=(float)lvector[i];
554 }
555
556 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
557 /*retrieve internal value: */
558 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
559
560 /*transform into int vector: */
561 vector=xNew<float>(M);
562 for(i=0;i<M;i++)vector[i]=(float)bvector[i];
563 }
564
565 else
566 _error_("unrecognized int pyarray type in input!");
567
568 if(py_vector2) delete(py_vector2);
569 }
570 else
571 vector=NULL;
572 }
573 else{
574 M=1;
575 vector=xNew<float>(M);
576 FetchData(&(vector[0]),py_vector);
577 }
578
579 /*output: */
580 if(pM)*pM=M;
581 if(pvector)*pvector=vector;
582}
583/*}}}*/
584/*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
585void FetchData(int** pvector,int* pM,PyObject* py_vector){
586
587 /*output: */
588 int* vector=NULL;
589 int M=0;
590 int ndim;
591 npy_intp* dims=NULL;
592
593 /*intermediary:*/
594 long* lvector=NULL;
595 bool* bvector=NULL;
596 double* dvector=NULL;
597 int i;
598 PyObject* py_vector2=NULL;
599
600 if (PyArray_Check((PyArrayObject*)py_vector)) {
601 /*retrieve dimensions: */
602 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
603 if (ndim==1) {
604 dims=PyArray_DIMS((PyArrayObject*)py_vector);
605 M=dims[0];
606 }
607 else if (ndim==2) {
608 dims=PyArray_DIMS((PyArrayObject*)py_vector);
609 if (dims[1]==1)
610 M=dims[0];
611 else
612 _error_("expecting an Mx1 matrix or M vector in input!");
613 }
614 else
615 _error_("expecting an Mx1 matrix or M vector in input!");
616
617 if (M) {
618 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
619 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
620 py_vector=py_vector2;
621 }
622
623 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
624 /*retrieve internal value: */
625 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
626
627 /*transform into int vector: */
628 vector=xNew<int>(M);
629 for(i=0;i<M;i++)vector[i]=(int)dvector[i];
630 }
631
632 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
633 /*retrieve internal value: */
634 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
635
636 /*transform into int vector: */
637 vector=xNew<int>(M);
638 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
639 }
640
641 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
642 /*retrieve internal value: */
643 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
644
645 /*transform into int vector: */
646 vector=xNew<int>(M);
647 for(i=0;i<M;i++)vector[i]=(int)bvector[i];
648 }
649
650 else
651 _error_("unrecognized int pyarray type in input!");
652
653 if (py_vector2)
654 delete(py_vector2);
655 }
656 else
657 vector=NULL;
658 }
659
660 else {
661 M=1;
662 vector=xNew<int>(M);
663 FetchData(&(vector[0]),py_vector);
664 }
665
666 /*output: */
667 if(pM)*pM=M;
668 if(pvector)*pvector=vector;
669}
670/*}}}*/
671/*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
672void FetchData(bool** pvector,int* pM,PyObject* py_vector){
673
674 /*output: */
675 bool* bvector=NULL;
676 bool* vector=NULL;
677 int M=0;
678 int ndim;
679 npy_intp* dims=NULL;
680
681 /*intermediary:*/
682 double* dvector=NULL;
683 long* lvector=NULL;
684 int i;
685 PyObject* py_vector2=NULL;
686
687 if (PyArray_Check((PyArrayObject*)py_vector)) {
688 /*retrieve dimensions: */
689 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
690 if (ndim==1) {
691 dims=PyArray_DIMS((PyArrayObject*)py_vector);
692 M=dims[0];
693 }
694 else if (ndim==2) {
695 dims=PyArray_DIMS((PyArrayObject*)py_vector);
696 if (dims[1]==1)
697 M=dims[0];
698 else
699 _error_("expecting an Mx1 matrix or M vector in input!");
700 }
701 else
702 _error_("expecting an Mx1 matrix or M vector in input!");
703
704 if (M) {
705 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
706 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
707 py_vector=py_vector2;
708 }
709
710 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
711 /*retrieve internal value: */
712 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
713
714 /*transform into bool vector: */
715 vector=xNew<bool>(M);
716 for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
717 }
718
719 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
720 /*retrieve internal value: */
721 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
722
723 /*transform into bool vector: */
724 vector=xNew<bool>(M);
725 for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
726 }
727
728 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
729 /*retrieve internal value: */
730 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
731
732 /*copy vector: */
733 vector=xNew<bool>(M);
734 memcpy(vector,bvector,(M)*sizeof(bool));
735 }
736
737 else
738 _error_("unrecognized bool pyarray type in input!");
739
740 if (py_vector2)
741 delete(py_vector2);
742 }
743 else
744 vector=NULL;
745 }
746
747 else {
748 M=1;
749 vector=xNew<bool>(M);
750 FetchData(&(vector[0]),py_vector);
751 }
752
753 /*output: */
754 if(pM)*pM=M;
755 if(pvector)*pvector=vector;
756}
757/*}}}*/
758
759/*ISSM objects*/
760/*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
761void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
762
763 /*Initialize output*/
764 BamgGeom* bamggeom=new BamgGeom();
765
766 /*Fetch all fields*/
767 FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
768 FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
769 FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
770 FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
771 FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
772 FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
773 FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
774
775 /*Assign output pointers:*/
776 *pbamggeom=bamggeom;
777}
778/*}}}*/
779/*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
780void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
781
782 /*Initialize output*/
783 BamgMesh* bamgmesh=new BamgMesh();
784
785 /*Fetch all fields*/
786 FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
787 FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
788 FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
789 FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
790 FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
791 FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
792 FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
793 FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
794
795 /*Assign output pointers:*/
796 *pbamgmesh=bamgmesh;
797}
798/*}}}*/
799/*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
800void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
801
802 /*Initialize output*/
803 BamgOpts* bamgopts=new BamgOpts();
804
805 /*Fetch all fields*/
806 FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
807 FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
808 FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
809 FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
810 FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
811 FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
812 FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
813 FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
814 FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
815 FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
816 FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
817 FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
818 FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
819 FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
820
821 FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
822 FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
823 FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
824
825 FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
826 FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
827 FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
828 FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
829 FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
830 FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
831 FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
832 FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
833
834 /*Additional checks*/
835 bamgopts->Check();
836
837 /*Assign output pointers:*/
838 *pbamgopts=bamgopts;
839}
840/*}}}*/
841/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
842void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
843
844 char *name = NULL;
845 Option *option = NULL;
846
847 /*Initialize output*/
848 Options* options=new Options();
849
850 /*Fetch all options*/
851 for (int i=istart; i<nrhs; i=i+2){
852 #if _PYTHON_MAJOR_ >= 3
853 if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
854 #else
855 if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
856 #endif
857
858 FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
859 if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
860
861 _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
862
863// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
864// options->AddOption(option);
865// option=NULL;
866 }
867
868 /*Assign output pointers:*/
869 *poptions=options;
870}
871/*}}}*/
872/*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
873void FetchData(Contours** pcontours,PyObject* py_list){
874
875 int numcontours,test1,test2;
876 char *contourname = NULL;
877 Contours *contours = NULL;
878 Contour<double> *contouri = NULL;
879 PyObject *py_dicti = NULL;
880 PyObject *py_item = NULL;
881
882 #if _PYTHON_MAJOR_ >= 3
883 if (PyUnicode_Check(py_list)){
884 FetchData(&contourname,py_list);
885 contours=ExpRead<double>(contourname);
886 }
887 #else
888 if (PyString_Check(py_list)){
889 FetchData(&contourname,py_list);
890 contours=ExpRead<double>(contourname);
891 }
892 #endif
893 else if(PyList_Check(py_list)){
894
895 contours=new Contours();
896 numcontours=(int)PyList_Size(py_list);
897
898 for(int i=0;i<numcontours;i++){
899
900 contouri=new Contour<double>();
901 py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
902
903 py_item = PyDict_GetItemString(py_dicti,"nods");
904 if(!py_item) _error_("input structure does not have a 'nods' field");
905 FetchData(&contouri->nods,py_item);
906
907 py_item = PyDict_GetItemString(py_dicti,"x");
908 if(!py_item) _error_("input structure does not have a 'x' field");
909 FetchData(&contouri->x,&test1,&test2,py_item);
910 if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
911
912 py_item = PyDict_GetItemString(py_dicti,"y");
913 if(!py_item) _error_("input structure does not have a 'y' field");
914 FetchData(&contouri->y,&test1,&test2,py_item);
915 if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
916
917 contours->AddObject(contouri);
918 }
919 }
920 else{
921 _error_("Contour is neither a string nor a structure and cannot be loaded");
922 }
923
924 /*clean-up and assign output pointer*/
925 xDelete<char>(contourname);
926 *pcontours=contours;
927}
928
929void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
930
931 double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
932 int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
933 int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
934
935 /*Fetch adjacency matrix:*/
936 int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
937
938 int* mwstart = xNewZeroInit<int>(nvtxs+1);
939 pyGetJc(a,nvtxs,mwstart);
940
941 int* mwadjacency = xNewZeroInit<int>(nzmax);
942 pyGetIr(a,nvtxs,nzmax,mwadjacency);
943
944 int* start = xNew<int>(nvtxs+1);
945 for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
946 int* adjacency = xNew<int>(nzmax);
947 for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
948
949 /*Get edges weights*/
950 int nedges = start[nvtxs];
951 float* ewgts = NULL;
952 int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
953 //size may be 1 and still be empty;
954 //presumably size of edge_weights input will never be exactly 1
955 if(size != 1 && size != 0){
956 ewgts = xNewZeroInit<float>(nedges);
957 for(int i = 0; i<nedges;i++){
958 ewgts[i] = (float)a[i];
959 }
960 }
961
962 /*Assign output pointers*/
963 *pnvtxs = nvtxs;
964 *padjacency = adjacency;
965 *pstart = start;
966 *pewgts = ewgts;
967}
968/*}}}*/
969
970void pyGetJc(double* a, int nvtxs, int* Jc){
971/*{{{*/
972 //get the number of non-zero elements in each row, adding to the prior number;
973 //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
974 // eg: 0, 1, 3, 6, 8, 13, ...
975 // for: 1 in the first row, 2 in the second, 3 in the third, etc.
976 int c = 0;
977 Jc[c++] = 0;
978
979 for(int i=0;i<nvtxs;i++){
980 for(int j=0;j<nvtxs;j++){
981 if ((int)a[i+(j*nvtxs)] != 0){
982 Jc[c] += 1;
983 }
984 }
985 c++;
986 Jc[c] = Jc[c-1];
987 }
988 return;
989}
990/*}}}*/
991
992void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
993/*{{{*/
994 //get the row-wise position of each non-zero element;
995 //has length: number_of_non_zero_elements_in(a) == nzmax
996 // eg: 10, 24, 25, 4, ...
997 // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
998 // using pyGetJc to determine which indices correspond to which row
999 int r = 0;
1000
1001 for(int i=0;i<nvtxs;i++){
1002 for(int j=0;j<nvtxs;j++){
1003 if ((int)a[i+(j*nvtxs)] != 0){
1004 Ir[r] = j;
1005 r++;
1006 }
1007 }
1008 }
1009 return;
1010}
1011/*}}}*/
1012
1013/*Python version dependent: */
1014#if _PYTHON_MAJOR_ >= 3
1015/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
1016void FetchData(char** pstring,PyObject* py_unicode){
1017
1018 PyObject* py_bytes;
1019 char* string=NULL;
1020
1021 /*convert to bytes format: */
1022 PyUnicode_FSConverter(py_unicode,&py_bytes);
1023
1024 /*convert from bytes to string: */
1025 string=PyBytes_AS_STRING(py_bytes);
1026
1027 /*copy string (note strlen does not include trailing NULL): */
1028 *pstring=xNew<char>(strlen(string)+1);
1029 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
1030}
1031/*}}}*/
1032#else
1033/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
1034void FetchData(char** pstring,PyObject* py_string){
1035
1036 char* string=NULL;
1037
1038 /*extract internal string: */
1039 string=PyString_AsString(py_string);
1040
1041 /*copy string (note strlen does not include trailing NULL): */
1042 *pstring=xNew<char>(strlen(string)+1);
1043 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
1044}
1045/*}}}*/
1046#endif
Note: See TracBrowser for help on using the repository browser.