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