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