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