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

Last change on this file since 14098 was 14098, checked in by jschierm, 12 years ago

NEW: Allow Python vectors to be input to modules directly rather than reshaping to Mx1 matrices.

File size: 16.3 KB
Line 
1/*\file FetchData.cpp:
2 * \brief: general I/O interface to fetch data in python
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include <config.h>
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
11#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
12#define NO_IMPORT
13
14#include "./pythonio.h"
15#include "../../c/include/include.h"
16#include "../../c/shared/shared.h"
17
18/*Primitive data types*/
19/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
20void 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){{{*/
45void 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){{{*/
70void 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){{{*/
93void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
94
95 /*output: */
96 double* dmatrix=NULL;
97 double* matrix=NULL;
98 int M,N;
99 int ndim;
100 npy_intp* dims=NULL;
101
102 /*intermediary:*/
103 long* lmatrix=NULL;
104 bool* bmatrix=NULL;
105 int i;
106
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_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
123 /*retrieve internal value: */
124 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
125
126 /*copy matrix: */
127 matrix=xNew<double>(M*N);
128 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
129 }
130
131 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
132 /*retrieve internal value: */
133 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
134
135 /*transform into double matrix: */
136 matrix=xNew<double>(M*N);
137 for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
138 }
139
140 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
141 /*retrieve internal value: */
142 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
143
144 /*transform into double matrix: */
145 matrix=xNew<double>(M*N);
146 for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
147 }
148
149 else
150 _error_("unrecognized float pyarray type in input!");
151 }
152 else
153 matrix=NULL;
154 }
155
156 else {
157 M=1;
158 N=1;
159 matrix=xNew<double>(M*N);
160 FetchData(&(matrix[0]),py_matrix);
161 }
162
163 /*output: */
164 if(pM)*pM=M;
165 if(pN)*pN=N;
166 if(pmatrix)*pmatrix=matrix;
167}
168/*}}}*/
169/*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
170void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
171
172 /*output: */
173 int* matrix=NULL;
174 int M,N;
175 int ndim;
176 npy_intp* dims=NULL;
177
178 /*intermediary:*/
179 double* dmatrix=NULL;
180 long* lmatrix=NULL;
181 bool* bmatrix=NULL;
182 int i;
183
184 if (PyArray_Check((PyArrayObject*)py_matrix)) {
185 /*retrieve dimensions: */
186 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
187 if (ndim==2) {
188 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
189 M=dims[0]; N=dims[1];
190 }
191 else if (ndim==1) {
192 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
193 M=dims[0]; N=1;
194 }
195 else
196 _error_("expecting an MxN matrix or M vector in input!");
197
198 if (M && N) {
199 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
200 /*retrieve internal value: */
201 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
202
203 /*transform into integer matrix: */
204 matrix=xNew<int>(M*N);
205 for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
206 }
207
208 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
209 /*retrieve internal value: */
210 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
211
212 /*transform into integer matrix: */
213 matrix=xNew<int>(M*N);
214 for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
215 }
216
217 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
218 /*retrieve internal value: */
219 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
220
221 /*transform into integer matrix: */
222 matrix=xNew<int>(M*N);
223 for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
224 }
225
226 else
227 _error_("unrecognized long pyarray type in input!");
228 }
229 else
230 matrix=NULL;
231 }
232
233 else {
234 M=1;
235 N=1;
236 matrix=xNew<int>(M*N);
237 FetchData(&(matrix[0]),py_matrix);
238 }
239
240 /*output: */
241 if(pM)*pM=M;
242 if(pN)*pN=N;
243 if(pmatrix)*pmatrix=matrix;
244}
245/*}}}*/
246/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
247void FetchData(double** pvector,int* pM,PyObject* py_vector){
248
249 /*output: */
250 double* dvector=NULL;
251 double* vector=NULL;
252 int M;
253 int ndim;
254 npy_intp* dims=NULL;
255
256 /*intermediary:*/
257 long* lvector=NULL;
258 bool* bvector=NULL;
259 int i;
260
261 /*retrieve dimensions: */
262 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
263 if(ndim!=1)_error_("expecting an Mx1 vector in input!");
264 dims=PyArray_DIMS((PyArrayObject*)py_vector);
265 M=dims[0];
266
267 if (M) {
268 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
269 /*retrieve internal value: */
270 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
271
272 /*copy vector: */
273 vector=xNew<double>(M);
274 memcpy(vector,dvector,(M)*sizeof(double));
275 }
276
277 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
278 /*retrieve internal value: */
279 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
280
281 /*transform into double vector: */
282 vector=xNew<double>(M);
283 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
284 }
285
286 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
287 /*retrieve internal value: */
288 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
289
290 /*transform into double vector: */
291 vector=xNew<double>(M);
292 for(i=0;i<M;i++)vector[i]=(double)bvector[i];
293 }
294
295 else
296 _error_("unrecognized vector type in input!");
297 }
298 else
299 vector=NULL;
300
301 /*output: */
302 if(pM)*pM=M;
303 if(pvector)*pvector=vector;
304}
305/*}}}*/
306
307/*ISSM objects*/
308/*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
309void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
310
311 /*Initialize output*/
312 BamgGeom* bamggeom=new BamgGeom();
313
314 /*Fetch all fields*/
315 FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
316 FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
317 FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
318 FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
319 FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
320 FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
321 FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
322
323 /*Assign output pointers:*/
324 *pbamggeom=bamggeom;
325}
326/*}}}*/
327/*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
328void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
329
330 /*Initialize output*/
331 BamgMesh* bamgmesh=new BamgMesh();
332
333 /*Fetch all fields*/
334 FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
335 FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
336 FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
337 FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
338 FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
339 FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
340 FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
341 FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
342
343 /*Assign output pointers:*/
344 *pbamgmesh=bamgmesh;
345}
346/*}}}*/
347/*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
348void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
349
350 /*Initialize output*/
351 BamgOpts* bamgopts=new BamgOpts();
352
353 /*Fetch all fields*/
354 FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
355 FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
356 FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
357 FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
358 FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
359 FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
360 FetchData(&bamgopts->MaxCornerAngle,PyDict_GetItemString(py_dict,"MaxCornerAngle"));
361 FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
362 FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
363 FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
364 FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
365 FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
366 FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
367 FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
368 FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
369
370 FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
371 FetchData(&bamgopts->geometricalmetric,PyDict_GetItemString(py_dict,"geometricalmetric"));
372 FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
373 FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
374
375 FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
376 FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
377 FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
378 FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
379 FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
380 FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
381 FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
382 FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
383
384 /*Additional checks*/
385 bamgopts->Check();
386
387 /*Assign output pointers:*/
388 *pbamgopts=bamgopts;
389}
390/*}}}*/
391/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
392void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
393
394 char *name = NULL;
395 Option *option = NULL;
396
397 /*Initialize output*/
398 Options* options=new Options();
399
400 /*Fetch all options*/
401 for (int i=istart; i<nrhs; i=i+2){
402 if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
403
404 FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
405 if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
406
407 _pprintLine_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!");
408
409// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
410// options->AddOption(option);
411// option=NULL;
412 }
413
414 /*Assign output pointers:*/
415 *poptions=options;
416}
417/*}}}*/
418/*FUNCTION FetchData(DataSet** pcontours,PyObject* py_list){{{*/
419void FetchData(DataSet** pcontours,PyObject* py_list){
420
421 int numcontours,test1,test2;
422 char *contourname = NULL;
423 DataSet *contours = NULL;
424 Contour<double> *contouri = NULL;
425 PyObject *py_dicti = NULL;
426 PyObject *py_item = NULL;
427
428 if (PyString_Check(py_list)){
429 FetchData(&contourname,py_list);
430 contours=DomainOutlineRead<double>(contourname);
431 }
432 else if(PyList_Check(py_list)){
433
434 contours=new DataSet(0);
435 numcontours=(int)PyList_Size(py_list);
436
437 for(int i=0;i<numcontours;i++){
438
439 contouri=xNew<Contour<double> >(1);
440 py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
441
442 py_item = PyDict_GetItemString(py_dicti,"nods");
443 if(!py_item) _error_("input structure does not have a 'nods' field");
444 FetchData(&contouri->nods,py_item);
445
446 py_item = PyDict_GetItemString(py_dicti,"x");
447 if(!py_item) _error_("input structure does not have a 'x' field");
448 FetchData(&contouri->x,&test1,&test2,py_item);
449 if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
450
451 py_item = PyDict_GetItemString(py_dicti,"y");
452 if(!py_item) _error_("input structure does not have a 'y' field");
453 FetchData(&contouri->y,&test1,&test2,py_item);
454 if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
455
456 contours->AddObject(contouri);
457 }
458 }
459 else{
460 _error_("Contour is neither a string nor a structure and cannot be loaded");
461 }
462
463 /*clean-up and assign output pointer*/
464 xDelete<char>(contourname);
465 *pcontours=contours;
466}
467/*}}}*/
468
469/*Python version dependent: */
470#if _PYTHON_MAJOR_ >= 3
471/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
472void FetchData(char** pstring,PyObject* py_unicode){
473
474 PyObject* py_bytes;
475 char* string=NULL;
476
477 /*convert to bytes format: */
478 PyUnicode_FSConverter(py_unicode,&py_bytes);
479
480 /*convert from bytes to string: */
481 string=PyBytes_AS_STRING(py_bytes);
482
483 *pstring=string;
484}
485/*}}}*/
486#else
487/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
488void FetchData(char** pstring,PyObject* py_string){
489
490 char* string=NULL;
491
492 /*extract internal string: */
493 string=PyString_AsString(py_string);
494
495 /*copy string (note strlen does not include trailing NULL): */
496 *pstring=xNew<char>(strlen(string)+1);
497 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
498}
499/*}}}*/
500#endif
Note: See TracBrowser for help on using the repository browser.