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