[12011] | 1 | /*\file FetchData.cpp:
|
---|
| 2 | * \brief: general I/O interface to fetch data in python
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #ifdef HAVE_CONFIG_H
|
---|
[23708] | 6 | #include <config.h>
|
---|
[12011] | 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 |
|
---|
[13749] | 14 | #include "./pythonio.h"
|
---|
| 15 | #include "../../c/shared/shared.h"
|
---|
[12011] | 16 |
|
---|
[12073] | 17 | /*Primitive data types*/
|
---|
[12365] | 18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
[12011] | 19 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
| 20 |
|
---|
[24112] | 21 | double dscalar;
|
---|
[12011] | 22 |
|
---|
| 23 | /*return internal value: */
|
---|
[24112] | 24 | #if _PYTHON_MAJOR_ == 3
|
---|
| 25 | if (PyFloat_Check(py_float))
|
---|
| 26 | dscalar=PyFloat_AsDouble(py_float);
|
---|
| 27 | else if (PyLong_Check(py_float))
|
---|
[19895] | 28 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[23708] | 29 | else if (PyBool_Check(py_float))
|
---|
| 30 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
| 31 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 32 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 33 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 34 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 35 | else
|
---|
[24112] | 36 | _error_("unrecognized float type py3 in input!");
|
---|
[23708] | 37 |
|
---|
| 38 | #else
|
---|
| 39 | if (PyFloat_Check(py_float))
|
---|
| 40 | dscalar=PyFloat_AsDouble(py_float);
|
---|
| 41 | else if (PyLong_Check(py_float))
|
---|
[14093] | 42 | dscalar=PyLong_AsDouble(py_float);
|
---|
[19896] | 43 | else if (PyInt_Check(py_float))
|
---|
[23231] | 44 | dscalar=(double)PyInt_AsLong(py_float);
|
---|
[14097] | 45 | else if (PyBool_Check(py_float))
|
---|
[14093] | 46 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[14097] | 47 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 48 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 49 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 50 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[14093] | 51 | else
|
---|
[24112] | 52 | _error_("unrecognized float type in py2 input!");
|
---|
[23708] | 53 | #endif
|
---|
[12011] | 54 | /*output: */
|
---|
[14093] | 55 | *pscalar=dscalar;
|
---|
[12011] | 56 | }
|
---|
| 57 | /*}}}*/
|
---|
[22674] | 58 | /*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
|
---|
| 59 | void FetchData(float* pscalar,PyObject* py_float){
|
---|
| 60 |
|
---|
| 61 | float fscalar;
|
---|
| 62 |
|
---|
| 63 | /*return internal value: */
|
---|
[23708] | 64 | #if _PYTHON_MAJOR_ == 3
|
---|
[22674] | 65 | if (PyFloat_Check(py_float))
|
---|
[24112] | 66 | fscalar=PyFloat_AsDouble(py_float);
|
---|
[23708] | 67 | else if (PyLong_Check(py_float))
|
---|
[22674] | 68 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[23708] | 69 | else if (PyBool_Check(py_float))
|
---|
| 70 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
| 71 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 72 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 73 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 74 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 75 | else
|
---|
| 76 | _error_("unrecognized float type in input!");
|
---|
| 77 | #else
|
---|
| 78 | if (PyFloat_Check(py_float))
|
---|
| 79 | fscalar=PyFloat_AsDouble(py_float);
|
---|
| 80 | else if (PyLong_Check(py_float))
|
---|
[22674] | 81 | fscalar=(float)PyLong_AsDouble(py_float);
|
---|
| 82 | else if (PyInt_Check(py_float))
|
---|
[23708] | 83 | fscalar=(float)PyInt_AsLong(py_float);
|
---|
[22674] | 84 | else if (PyBool_Check(py_float))
|
---|
[23708] | 85 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[22674] | 86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
[23708] | 87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
[23708] | 89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 90 | else
|
---|
[23708] | 91 | _error_("unrecognized float type in input!");
|
---|
| 92 | #endif
|
---|
[22674] | 93 | /*output: */
|
---|
| 94 | *pscalar=fscalar;
|
---|
| 95 | }
|
---|
| 96 | /*}}}*/
|
---|
[14093] | 97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
---|
| 98 | void FetchData(int* pscalar, PyObject* py_long){
|
---|
[12011] | 99 |
|
---|
[14093] | 100 | int iscalar;
|
---|
[12011] | 101 |
|
---|
| 102 | /*return internal value: */
|
---|
[23708] | 103 | #if _PYTHON_MAJOR_ == 3
|
---|
| 104 | if (PyLong_Check(py_long))
|
---|
[19895] | 105 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[23708] | 106 | else if (PyFloat_Check(py_long))
|
---|
| 107 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
| 108 | else if (PyBool_Check(py_long))
|
---|
| 109 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
| 110 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 111 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 112 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 113 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 114 | else
|
---|
| 115 | _error_("unrecognized long type in input!");
|
---|
| 116 |
|
---|
| 117 | #else
|
---|
| 118 | if (PyLong_Check(py_long))
|
---|
| 119 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[19896] | 120 | else if (PyInt_Check(py_long))
|
---|
| 121 | iscalar=(int)PyInt_AsLong(py_long);
|
---|
[14097] | 122 | else if (PyFloat_Check(py_long))
|
---|
[14093] | 123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
[14097] | 124 | else if (PyBool_Check(py_long))
|
---|
[14093] | 125 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[14097] | 126 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 127 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 128 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 129 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
[14093] | 130 | else
|
---|
| 131 | _error_("unrecognized long type in input!");
|
---|
[23708] | 132 | #endif
|
---|
[12011] | 133 | /*output: */
|
---|
[14093] | 134 | *pscalar=iscalar;
|
---|
[12011] | 135 | }
|
---|
| 136 | /*}}}*/
|
---|
[14093] | 137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
---|
| 138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
---|
[12011] | 139 |
|
---|
[14093] | 140 | bool bscalar;
|
---|
[13622] | 141 |
|
---|
[14097] | 142 | /*return internal value: */
|
---|
[23708] | 143 | #if _PYTHON_MAJOR_ == 3
|
---|
[24112] | 144 | if (PyBool_Check(py_boolean))
|
---|
[14097] | 145 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 146 | else if (PyLong_Check(py_boolean))
|
---|
| 147 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19895] | 148 | else if (PyLong_Check(py_boolean))
|
---|
| 149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[23708] | 150 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 151 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 152 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 153 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 154 | else
|
---|
| 155 | _error_("unrecognized boolean type in input!");
|
---|
| 156 |
|
---|
| 157 | #else
|
---|
| 158 | if (PyBool_Check(py_boolean))
|
---|
| 159 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 160 | else if (PyLong_Check(py_boolean))
|
---|
| 161 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 162 | else if (PyLong_Check(py_boolean))
|
---|
| 163 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19896] | 164 | else if (PyInt_Check(py_boolean))
|
---|
| 165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
---|
[14097] | 166 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 167 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 168 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 169 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 170 | else
|
---|
| 171 | _error_("unrecognized boolean type in input!");
|
---|
[23708] | 172 | #endif
|
---|
[14097] | 173 | /*output: */
|
---|
[14093] | 174 | *pscalar=bscalar;
|
---|
[12011] | 175 | }
|
---|
| 176 | /*}}}*/
|
---|
[12365] | 177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
[12112] | 178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
[12073] | 179 |
|
---|
[12112] | 180 | /*output: */
|
---|
[13372] | 181 | double* dmatrix=NULL;
|
---|
[12112] | 182 | double* matrix=NULL;
|
---|
[23231] | 183 | int M=0;
|
---|
| 184 | int N=0;
|
---|
[13373] | 185 | int ndim;
|
---|
[12112] | 186 | npy_intp* dims=NULL;
|
---|
| 187 |
|
---|
[13990] | 188 | /*intermediary:*/
|
---|
| 189 | long* lmatrix=NULL;
|
---|
| 190 | bool* bmatrix=NULL;
|
---|
[26565] | 191 | float* smatrix=NULL;
|
---|
[14317] | 192 | int i;
|
---|
| 193 | PyObject* py_matrix2=NULL;
|
---|
[13990] | 194 |
|
---|
[24112] | 195 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
[14093] | 196 | /*retrieve dimensions: */
|
---|
| 197 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[24112] | 198 | if (ndim==2) {
|
---|
[14098] | 199 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 200 | M=dims[0]; N=dims[1];
|
---|
| 201 | }
|
---|
| 202 | else if (ndim==1) {
|
---|
| 203 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 204 | M=dims[0]; N=1;
|
---|
| 205 | }
|
---|
| 206 | else
|
---|
| 207 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 208 |
|
---|
[14317] | 209 | if (M && N) {
|
---|
| 210 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 211 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
|
---|
| 212 | py_matrix=py_matrix2;
|
---|
| 213 | }
|
---|
[14316] | 214 |
|
---|
[26565] | 215 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_FLOAT) {
|
---|
[14093] | 216 | /*retrieve internal value: */
|
---|
[26565] | 217 | smatrix=(float*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 218 |
|
---|
| 219 | /*transform into double matrix: */
|
---|
| 220 | matrix=xNew<double>(M*N);
|
---|
| 221 | for(i=0;i<M*N;i++)matrix[i]=(double)smatrix[i];
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 225 | /*retrieve internal value: */
|
---|
[14093] | 226 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12112] | 227 |
|
---|
[14093] | 228 | /*copy matrix: */
|
---|
| 229 | matrix=xNew<double>(M*N);
|
---|
[24112] | 230 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
[14093] | 231 | }
|
---|
[13990] | 232 |
|
---|
[14316] | 233 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 234 | /*retrieve internal value: */
|
---|
| 235 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 236 |
|
---|
[14093] | 237 | /*transform into double matrix: */
|
---|
| 238 | matrix=xNew<double>(M*N);
|
---|
| 239 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
| 240 | }
|
---|
[13990] | 241 |
|
---|
[14093] | 242 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 243 | /*retrieve internal value: */
|
---|
| 244 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 245 |
|
---|
[14093] | 246 | /*transform into double matrix: */
|
---|
| 247 | matrix=xNew<double>(M*N);
|
---|
| 248 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | else
|
---|
[14234] | 252 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 253 |
|
---|
[19683] | 254 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
[13990] | 255 | }
|
---|
| 256 | else
|
---|
[14093] | 257 | matrix=NULL;
|
---|
[13484] | 258 | }
|
---|
[24112] | 259 | else if (PyList_Check(py_matrix)) {
|
---|
| 260 | /*retrieve dimensions: */
|
---|
| 261 | M=(int)PyList_Size(py_matrix);
|
---|
[24114] | 262 | N=1;
|
---|
[24112] | 263 | if (M) {
|
---|
| 264 | matrix=xNew<double>(M);
|
---|
| 265 | for (int index = 0; index < M; index++) {
|
---|
| 266 | PyObject *item;
|
---|
| 267 | item = PyList_GetItem(py_matrix, index);
|
---|
| 268 | if ((int)PyList_Size(item)>1)
|
---|
| 269 | _error_("2D lists are not suported");
|
---|
| 270 | FetchData(&(matrix[index]),item);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 274 | else
|
---|
| 275 | matrix=NULL;
|
---|
| 276 | }
|
---|
| 277 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 278 | /*retrieve dimensions: */
|
---|
| 279 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 280 | N=1;
|
---|
[24112] | 281 | if (M) {
|
---|
| 282 | matrix=xNew<double>(M);
|
---|
| 283 | for (int index = 0; index < M; index++) {
|
---|
| 284 | PyObject *item;
|
---|
| 285 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 286 | if ((int)PyTuple_Size(item)>1)
|
---|
| 287 | _error_("2D tuple are not suported");
|
---|
| 288 | FetchData(&(matrix[index]),item);
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 292 | else
|
---|
| 293 | matrix=NULL;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[14097] | 296 | else {
|
---|
[14093] | 297 | M=1;
|
---|
| 298 | N=1;
|
---|
| 299 | matrix=xNew<double>(M*N);
|
---|
| 300 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[12112] | 303 | /*output: */
|
---|
| 304 | if(pM)*pM=M;
|
---|
| 305 | if(pN)*pN=N;
|
---|
| 306 | if(pmatrix)*pmatrix=matrix;
|
---|
| 307 | }
|
---|
| 308 | /*}}}*/
|
---|
[12776] | 309 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 310 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 311 |
|
---|
| 312 | /*output: */
|
---|
| 313 | int* matrix=NULL;
|
---|
[23231] | 314 | int M=0;
|
---|
| 315 | int N=0;
|
---|
[13990] | 316 | int ndim;
|
---|
| 317 | npy_intp* dims=NULL;
|
---|
[12776] | 318 |
|
---|
| 319 | /*intermediary:*/
|
---|
[13990] | 320 | double* dmatrix=NULL;
|
---|
| 321 | long* lmatrix=NULL;
|
---|
| 322 | bool* bmatrix=NULL;
|
---|
[12776] | 323 | int i;
|
---|
[14317] | 324 | PyObject* py_matrix2=NULL;
|
---|
[12776] | 325 |
|
---|
[14093] | 326 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 327 | /*retrieve dimensions: */
|
---|
| 328 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[14098] | 329 | if (ndim==2) {
|
---|
| 330 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 331 | M=dims[0]; N=dims[1];
|
---|
| 332 | }
|
---|
| 333 | else if (ndim==1) {
|
---|
| 334 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 335 | M=dims[0]; N=1;
|
---|
| 336 | }
|
---|
| 337 | else
|
---|
| 338 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 339 |
|
---|
[14093] | 340 | if (M && N) {
|
---|
[14317] | 341 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 342 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
---|
| 343 | py_matrix=py_matrix2;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[14093] | 346 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 347 | /*retrieve internal value: */
|
---|
| 348 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12776] | 349 |
|
---|
[14093] | 350 | /*transform into integer matrix: */
|
---|
| 351 | matrix=xNew<int>(M*N);
|
---|
| 352 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
| 353 | }
|
---|
[13990] | 354 |
|
---|
[14316] | 355 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 356 | /*retrieve internal value: */
|
---|
| 357 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 358 |
|
---|
[14093] | 359 | /*transform into integer matrix: */
|
---|
| 360 | matrix=xNew<int>(M*N);
|
---|
| 361 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
| 362 | }
|
---|
[13990] | 363 |
|
---|
[14093] | 364 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 365 | /*retrieve internal value: */
|
---|
| 366 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 367 |
|
---|
[14093] | 368 | /*transform into integer matrix: */
|
---|
| 369 | matrix=xNew<int>(M*N);
|
---|
| 370 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | else
|
---|
[14234] | 374 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 375 |
|
---|
[21201] | 376 | /* These lines are causing a segfault
|
---|
[14317] | 377 | if (py_matrix2)
|
---|
| 378 | delete(py_matrix2);
|
---|
[21201] | 379 | */
|
---|
[13990] | 380 | }
|
---|
| 381 | else
|
---|
[14093] | 382 | matrix=NULL;
|
---|
[13484] | 383 | }
|
---|
[24112] | 384 | else if (PyList_Check(py_matrix)) {
|
---|
| 385 | /*retrieve dimensions: */
|
---|
| 386 | M=(int)PyList_Size(py_matrix);
|
---|
[24114] | 387 | N=1;
|
---|
[24112] | 388 | if (M) {
|
---|
| 389 | matrix=xNew<int>(M);
|
---|
| 390 | for (int index = 0; index < M; index++) {
|
---|
| 391 | PyObject *item;
|
---|
| 392 | item = PyList_GetItem(py_matrix, index);
|
---|
| 393 | if ((int)PyList_Size(item)>1)
|
---|
| 394 | _error_("2D lists are not suported");
|
---|
| 395 | FetchData(&(matrix[index]),item);
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 | else
|
---|
| 399 | matrix=NULL;
|
---|
| 400 | }
|
---|
| 401 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 402 | /*retrieve dimensions: */
|
---|
| 403 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 404 | N=1;
|
---|
[24112] | 405 | if (M) {
|
---|
| 406 | matrix=xNew<int>(M);
|
---|
| 407 | for (int index = 0; index < M; index++) {
|
---|
| 408 | PyObject *item;
|
---|
| 409 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 410 | if ((int)PyTuple_Size(item)>1)
|
---|
| 411 | _error_("2D tuple are not suported");
|
---|
| 412 | FetchData(&(matrix[index]),item);
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 416 | else
|
---|
| 417 | matrix=NULL;
|
---|
| 418 | }
|
---|
[14093] | 419 |
|
---|
[14097] | 420 | else {
|
---|
[14093] | 421 | M=1;
|
---|
| 422 | N=1;
|
---|
| 423 | matrix=xNew<int>(M*N);
|
---|
| 424 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[12776] | 427 | /*output: */
|
---|
| 428 | if(pM)*pM=M;
|
---|
| 429 | if(pN)*pN=N;
|
---|
| 430 | if(pmatrix)*pmatrix=matrix;
|
---|
| 431 | }
|
---|
| 432 | /*}}}*/
|
---|
[14234] | 433 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 434 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 435 |
|
---|
| 436 | /*output: */
|
---|
| 437 | bool* bmatrix=NULL;
|
---|
| 438 | bool* matrix=NULL;
|
---|
[23231] | 439 | int M=0;
|
---|
| 440 | int N=0;
|
---|
[14234] | 441 | int ndim;
|
---|
| 442 | npy_intp* dims=NULL;
|
---|
| 443 |
|
---|
| 444 | /*intermediary:*/
|
---|
| 445 | double* dmatrix=NULL;
|
---|
| 446 | long* lmatrix=NULL;
|
---|
| 447 | int i;
|
---|
[14317] | 448 | PyObject* py_matrix2=NULL;
|
---|
[14234] | 449 |
|
---|
| 450 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 451 | /*retrieve dimensions: */
|
---|
| 452 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
| 453 | if (ndim==2) {
|
---|
| 454 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 455 | M=dims[0]; N=dims[1];
|
---|
| 456 | }
|
---|
| 457 | else if (ndim==1) {
|
---|
| 458 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 459 | M=dims[0]; N=1;
|
---|
| 460 | }
|
---|
| 461 | else
|
---|
| 462 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
| 463 |
|
---|
| 464 | if (M && N) {
|
---|
[14317] | 465 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 466 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
---|
| 467 | py_matrix=py_matrix2;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[14234] | 470 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 471 | /*retrieve internal value: */
|
---|
| 472 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 473 |
|
---|
| 474 | /*transform into bool matrix: */
|
---|
| 475 | matrix=xNew<bool>(M*N);
|
---|
| 476 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[14316] | 479 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14234] | 480 | /*retrieve internal value: */
|
---|
| 481 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 482 |
|
---|
| 483 | /*transform into bool matrix: */
|
---|
| 484 | matrix=xNew<bool>(M*N);
|
---|
| 485 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 489 | /*retrieve internal value: */
|
---|
| 490 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 491 |
|
---|
| 492 | /*copy matrix: */
|
---|
| 493 | matrix=xNew<bool>(M*N);
|
---|
| 494 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | else
|
---|
| 498 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 499 |
|
---|
| 500 | if (py_matrix2)
|
---|
| 501 | delete(py_matrix2);
|
---|
[14234] | 502 | }
|
---|
| 503 | else
|
---|
| 504 | matrix=NULL;
|
---|
| 505 | }
|
---|
[24112] | 506 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
[14234] | 507 |
|
---|
[24112] | 508 | else if (PyList_Check(py_matrix)) {
|
---|
| 509 | /*retrieve dimensions: */
|
---|
| 510 | M=(int)PyList_Size(py_matrix);
|
---|
| 511 | N=1;
|
---|
| 512 | if (M) {
|
---|
| 513 | matrix=xNew<bool>(M);
|
---|
| 514 | for (int index = 0; index < M; index++) {
|
---|
| 515 | PyObject *item;
|
---|
| 516 | item = PyList_GetItem(py_matrix, index);
|
---|
| 517 | if ((int)PyList_Size(item)>1)
|
---|
| 518 | _error_("2D lists are not suported");
|
---|
| 519 | FetchData(&(matrix[index]),item);
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 | else
|
---|
| 523 | matrix=NULL;
|
---|
| 524 | }
|
---|
| 525 | else if (PyTuple_Check(py_matrix)) {
|
---|
| 526 | /*retrieve dimensions: */
|
---|
| 527 | M=(int)PyTuple_Size(py_matrix);
|
---|
[24114] | 528 | N=1;
|
---|
[24112] | 529 | if (M) {
|
---|
| 530 | matrix=xNew<bool>(M);
|
---|
| 531 | for (int index = 0; index < M; index++) {
|
---|
| 532 | PyObject *item;
|
---|
| 533 | item = PyTuple_GetItem(py_matrix, index);
|
---|
| 534 | if ((int)PyTuple_Size(item)>1)
|
---|
| 535 | _error_("2D tuples are not suported");
|
---|
| 536 | FetchData(&(matrix[index]),item);
|
---|
| 537 | }
|
---|
| 538 | }
|
---|
| 539 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 540 | else
|
---|
| 541 | matrix=NULL;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[14234] | 544 | else {
|
---|
| 545 | M=1;
|
---|
| 546 | N=1;
|
---|
| 547 | matrix=xNew<bool>(M*N);
|
---|
| 548 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | /*output: */
|
---|
| 552 | if(pM)*pM=M;
|
---|
| 553 | if(pN)*pN=N;
|
---|
| 554 | if(pmatrix)*pmatrix=matrix;
|
---|
| 555 | }
|
---|
| 556 | /*}}}*/
|
---|
[12365] | 557 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
[12120] | 558 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
[12112] | 559 |
|
---|
[12120] | 560 | /*output: */
|
---|
[13372] | 561 | double* dvector=NULL;
|
---|
[12120] | 562 | double* vector=NULL;
|
---|
[23231] | 563 | int M=0;
|
---|
[13373] | 564 | int ndim;
|
---|
[12120] | 565 | npy_intp* dims=NULL;
|
---|
| 566 |
|
---|
[13990] | 567 | /*intermediary:*/
|
---|
| 568 | long* lvector=NULL;
|
---|
| 569 | bool* bvector=NULL;
|
---|
[26565] | 570 | float* svector=NULL;
|
---|
[13990] | 571 | int i;
|
---|
[14317] | 572 | PyObject* py_vector2=NULL;
|
---|
[13990] | 573 |
|
---|
[14236] | 574 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 575 | /*retrieve dimensions: */
|
---|
| 576 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 577 | if (ndim==1) {
|
---|
| 578 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 579 | M=dims[0];
|
---|
[14236] | 580 | }
|
---|
| 581 | else if (ndim==2) {
|
---|
| 582 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 583 | if (dims[1]==1)
|
---|
[23708] | 584 | M=dims[0];
|
---|
[14236] | 585 | else
|
---|
| 586 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 587 | }
|
---|
| 588 | else
|
---|
| 589 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[13622] | 590 |
|
---|
[14236] | 591 | if (M) {
|
---|
[14317] | 592 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 593 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
|
---|
| 594 | py_vector=py_vector2;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[26565] | 597 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_FLOAT) {
|
---|
[14236] | 598 | /*retrieve internal value: */
|
---|
[26565] | 599 | svector=(float*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 600 |
|
---|
| 601 | /*transform into double matrix: */
|
---|
| 602 | vector=xNew<double>(M);
|
---|
| 603 | for(i=0;i<M;i++)vector[i]=(double)svector[i];
|
---|
| 604 | }
|
---|
| 605 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 606 | /*retrieve internal value: */
|
---|
[14236] | 607 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[12120] | 608 |
|
---|
[14236] | 609 | /*copy vector: */
|
---|
| 610 | vector=xNew<double>(M);
|
---|
| 611 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
| 612 | }
|
---|
[13990] | 613 |
|
---|
[14316] | 614 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 615 | /*retrieve internal value: */
|
---|
| 616 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 617 |
|
---|
[14236] | 618 | /*transform into double vector: */
|
---|
| 619 | vector=xNew<double>(M);
|
---|
| 620 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
---|
| 621 | }
|
---|
[13990] | 622 |
|
---|
[14236] | 623 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 624 | /*retrieve internal value: */
|
---|
| 625 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 626 |
|
---|
[14236] | 627 | /*transform into double vector: */
|
---|
| 628 | vector=xNew<double>(M);
|
---|
| 629 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | else
|
---|
| 633 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 634 |
|
---|
[21201] | 635 | /* Causing a seg fault.
|
---|
[14317] | 636 | if (py_vector2)
|
---|
| 637 | delete(py_vector2);
|
---|
[21201] | 638 | */
|
---|
[13990] | 639 | }
|
---|
| 640 | else
|
---|
[14236] | 641 | vector=NULL;
|
---|
[13484] | 642 | }
|
---|
[24112] | 643 | else if (PyList_Check(py_vector)) {
|
---|
| 644 | /*retrieve dimensions: */
|
---|
| 645 | M=(int)PyList_Size(py_vector);
|
---|
[13372] | 646 |
|
---|
[24112] | 647 | if (M) {
|
---|
| 648 | vector=xNew<double>(M);
|
---|
| 649 | for (int index = 0; index < M; index++) {
|
---|
| 650 | PyObject *item;
|
---|
| 651 | item = PyList_GetItem(py_vector, index);
|
---|
| 652 | FetchData(&(vector[index]),item);
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 | else
|
---|
| 656 | vector=NULL;
|
---|
| 657 | }
|
---|
| 658 | else if (PyTuple_Check(py_vector)) {
|
---|
| 659 | /*retrieve dimensions: */
|
---|
| 660 | M=(int)PyTuple_Size(py_vector);
|
---|
| 661 |
|
---|
| 662 | if (M) {
|
---|
| 663 | vector=xNew<double>(M);
|
---|
| 664 | for (int index = 0; index < M; index++) {
|
---|
| 665 | PyObject *item;
|
---|
| 666 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 667 | FetchData(&(vector[index]),item);
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 671 | else
|
---|
| 672 | vector=NULL;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
[14236] | 675 | else {
|
---|
| 676 | M=1;
|
---|
| 677 | vector=xNew<double>(M);
|
---|
| 678 | FetchData(&(vector[0]),py_vector);
|
---|
| 679 | }
|
---|
| 680 |
|
---|
[12120] | 681 | /*output: */
|
---|
| 682 | if(pM)*pM=M;
|
---|
| 683 | if(pvector)*pvector=vector;
|
---|
| 684 | }
|
---|
| 685 | /*}}}*/
|
---|
[22674] | 686 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 687 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
---|
| 688 |
|
---|
| 689 | /*output: */
|
---|
| 690 | float* vector=NULL;
|
---|
[23231] | 691 | int M=0;
|
---|
[22674] | 692 | int ndim;
|
---|
| 693 | npy_intp* dims=NULL;
|
---|
| 694 |
|
---|
| 695 | /*intermediary:*/
|
---|
| 696 | long* lvector=NULL;
|
---|
| 697 | bool* bvector=NULL;
|
---|
| 698 | double* dvector=NULL;
|
---|
| 699 | int i;
|
---|
| 700 | PyObject* py_vector2=NULL;
|
---|
| 701 |
|
---|
| 702 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 703 | /*retrieve dimensions: */
|
---|
| 704 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 705 | if (ndim==1) {
|
---|
| 706 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 707 | M=dims[0];
|
---|
[22674] | 708 | }
|
---|
| 709 | else if (ndim==2) {
|
---|
| 710 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 711 | if (dims[1]==1)
|
---|
[23708] | 712 | M=dims[0];
|
---|
[22674] | 713 | else
|
---|
| 714 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 715 | }
|
---|
| 716 | else
|
---|
| 717 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 718 |
|
---|
| 719 | if (M) {
|
---|
| 720 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 721 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 722 | py_vector=py_vector2;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 726 | /*retrieve internal value: */
|
---|
| 727 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 728 |
|
---|
| 729 | /*transform into int vector: */
|
---|
| 730 | vector=xNew<float>(M);
|
---|
[23231] | 731 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
---|
[22674] | 732 | }
|
---|
| 733 |
|
---|
| 734 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
| 735 | /*retrieve internal value: */
|
---|
| 736 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 737 |
|
---|
| 738 | /*transform into int vector: */
|
---|
| 739 | vector=xNew<float>(M);
|
---|
| 740 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 744 | /*retrieve internal value: */
|
---|
| 745 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 746 |
|
---|
| 747 | /*transform into int vector: */
|
---|
| 748 | vector=xNew<float>(M);
|
---|
| 749 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | else
|
---|
| 753 | _error_("unrecognized int pyarray type in input!");
|
---|
| 754 |
|
---|
| 755 | if(py_vector2) delete(py_vector2);
|
---|
| 756 | }
|
---|
| 757 | else
|
---|
[23231] | 758 | vector=NULL;
|
---|
[22674] | 759 | }
|
---|
[24112] | 760 | else if (PyList_Check(py_vector)) {
|
---|
| 761 | /*retrieve dimensions: */
|
---|
| 762 | M=(int)PyList_Size(py_vector);
|
---|
| 763 |
|
---|
| 764 | if (M) {
|
---|
| 765 | vector=xNew<float>(M);
|
---|
| 766 | for (int index = 0; index < M; index++) {
|
---|
| 767 | PyObject *item;
|
---|
| 768 | item = PyList_GetItem(py_vector, index);
|
---|
| 769 | FetchData(&(vector[index]),item);
|
---|
| 770 | }
|
---|
| 771 | }
|
---|
| 772 | else
|
---|
| 773 | vector=NULL;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | else if (PyTuple_Check(py_vector)) {
|
---|
| 777 | /*retrieve dimensions: */
|
---|
| 778 | M=(int)PyTuple_Size(py_vector);
|
---|
| 779 |
|
---|
| 780 | if (M) {
|
---|
| 781 | vector=xNew<float>(M);
|
---|
| 782 | for (int index = 0; index < M; index++) {
|
---|
| 783 | PyObject *item;
|
---|
| 784 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 785 | FetchData(&(vector[index]),item);
|
---|
| 786 | }
|
---|
| 787 | }
|
---|
| 788 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 789 | else
|
---|
| 790 | vector=NULL;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
[22674] | 793 | else{
|
---|
| 794 | M=1;
|
---|
| 795 | vector=xNew<float>(M);
|
---|
| 796 | FetchData(&(vector[0]),py_vector);
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | /*output: */
|
---|
| 800 | if(pM)*pM=M;
|
---|
| 801 | if(pvector)*pvector=vector;
|
---|
| 802 | }
|
---|
| 803 | /*}}}*/
|
---|
[14221] | 804 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 805 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
[13310] | 806 |
|
---|
[14221] | 807 | /*output: */
|
---|
| 808 | int* vector=NULL;
|
---|
[23231] | 809 | int M=0;
|
---|
[14221] | 810 | int ndim;
|
---|
| 811 | npy_intp* dims=NULL;
|
---|
| 812 |
|
---|
| 813 | /*intermediary:*/
|
---|
| 814 | long* lvector=NULL;
|
---|
| 815 | bool* bvector=NULL;
|
---|
| 816 | double* dvector=NULL;
|
---|
| 817 | int i;
|
---|
[14317] | 818 | PyObject* py_vector2=NULL;
|
---|
[14221] | 819 |
|
---|
[14236] | 820 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 821 | /*retrieve dimensions: */
|
---|
| 822 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 823 | if (ndim==1) {
|
---|
| 824 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 825 | M=dims[0];
|
---|
[14236] | 826 | }
|
---|
| 827 | else if (ndim==2) {
|
---|
| 828 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 829 | if (dims[1]==1)
|
---|
[23708] | 830 | M=dims[0];
|
---|
[14236] | 831 | else
|
---|
| 832 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 833 | }
|
---|
| 834 | else
|
---|
| 835 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14221] | 836 |
|
---|
[14236] | 837 | if (M) {
|
---|
[14317] | 838 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 839 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 840 | py_vector=py_vector2;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
[14236] | 843 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 844 | /*retrieve internal value: */
|
---|
| 845 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 846 |
|
---|
[14236] | 847 | /*transform into int vector: */
|
---|
| 848 | vector=xNew<int>(M);
|
---|
[23231] | 849 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
---|
[14236] | 850 | }
|
---|
[14221] | 851 |
|
---|
[14316] | 852 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 853 | /*retrieve internal value: */
|
---|
| 854 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 855 |
|
---|
[14236] | 856 | /*transform into int vector: */
|
---|
| 857 | vector=xNew<int>(M);
|
---|
| 858 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
| 859 | }
|
---|
[14221] | 860 |
|
---|
[14236] | 861 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 862 | /*retrieve internal value: */
|
---|
| 863 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 864 |
|
---|
[14236] | 865 | /*transform into int vector: */
|
---|
| 866 | vector=xNew<int>(M);
|
---|
| 867 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | else
|
---|
| 871 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 872 |
|
---|
| 873 | if (py_vector2)
|
---|
| 874 | delete(py_vector2);
|
---|
[14221] | 875 | }
|
---|
| 876 | else
|
---|
[14236] | 877 | vector=NULL;
|
---|
[14221] | 878 | }
|
---|
| 879 |
|
---|
[24112] | 880 | else if (PyList_Check(py_vector)) {
|
---|
| 881 | /*retrieve dimensions: */
|
---|
| 882 | M=(int)PyList_Size(py_vector);
|
---|
| 883 |
|
---|
| 884 | if (M) {
|
---|
| 885 | vector=xNew<int>(M);
|
---|
| 886 | for (int index = 0; index < M; index++) {
|
---|
| 887 | PyObject *item;
|
---|
| 888 | item = PyList_GetItem(py_vector, index);
|
---|
| 889 | FetchData(&(vector[index]),item);
|
---|
| 890 | }
|
---|
| 891 | }
|
---|
| 892 | else
|
---|
| 893 | vector=NULL;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | else if (PyTuple_Check(py_vector)) {
|
---|
| 897 | /*retrieve dimensions: */
|
---|
| 898 | M=(int)PyTuple_Size(py_vector);
|
---|
| 899 |
|
---|
| 900 | if (M) {
|
---|
| 901 | vector=xNew<int>(M);
|
---|
| 902 | for (int index = 0; index < M; index++) {
|
---|
| 903 | PyObject *item;
|
---|
| 904 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 905 | FetchData(&(vector[index]),item);
|
---|
| 906 | }
|
---|
| 907 | }
|
---|
| 908 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 909 | else
|
---|
| 910 | vector=NULL;
|
---|
| 911 | }
|
---|
| 912 |
|
---|
[14236] | 913 | else {
|
---|
| 914 | M=1;
|
---|
| 915 | vector=xNew<int>(M);
|
---|
| 916 | FetchData(&(vector[0]),py_vector);
|
---|
| 917 | }
|
---|
| 918 |
|
---|
[14221] | 919 | /*output: */
|
---|
| 920 | if(pM)*pM=M;
|
---|
| 921 | if(pvector)*pvector=vector;
|
---|
| 922 | }
|
---|
| 923 | /*}}}*/
|
---|
[14234] | 924 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 925 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
[14221] | 926 |
|
---|
[14234] | 927 | /*output: */
|
---|
| 928 | bool* bvector=NULL;
|
---|
| 929 | bool* vector=NULL;
|
---|
[23231] | 930 | int M=0;
|
---|
[14234] | 931 | int ndim;
|
---|
| 932 | npy_intp* dims=NULL;
|
---|
| 933 |
|
---|
| 934 | /*intermediary:*/
|
---|
| 935 | double* dvector=NULL;
|
---|
| 936 | long* lvector=NULL;
|
---|
| 937 | int i;
|
---|
[14317] | 938 | PyObject* py_vector2=NULL;
|
---|
[14234] | 939 |
|
---|
[14236] | 940 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 941 | /*retrieve dimensions: */
|
---|
| 942 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 943 | if (ndim==1) {
|
---|
| 944 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 945 | M=dims[0];
|
---|
[14236] | 946 | }
|
---|
| 947 | else if (ndim==2) {
|
---|
| 948 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 949 | if (dims[1]==1)
|
---|
[23708] | 950 | M=dims[0];
|
---|
[14236] | 951 | else
|
---|
| 952 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 953 | }
|
---|
| 954 | else
|
---|
| 955 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14234] | 956 |
|
---|
[14236] | 957 | if (M) {
|
---|
[14317] | 958 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 959 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
---|
| 960 | py_vector=py_vector2;
|
---|
| 961 | }
|
---|
| 962 |
|
---|
[14236] | 963 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 964 | /*retrieve internal value: */
|
---|
| 965 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 966 |
|
---|
[14236] | 967 | /*transform into bool vector: */
|
---|
| 968 | vector=xNew<bool>(M);
|
---|
| 969 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
| 970 | }
|
---|
[14234] | 971 |
|
---|
[14316] | 972 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 973 | /*retrieve internal value: */
|
---|
| 974 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 975 |
|
---|
[14236] | 976 | /*transform into bool vector: */
|
---|
| 977 | vector=xNew<bool>(M);
|
---|
| 978 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
| 979 | }
|
---|
[14234] | 980 |
|
---|
[14236] | 981 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 982 | /*retrieve internal value: */
|
---|
| 983 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 984 |
|
---|
[14236] | 985 | /*copy vector: */
|
---|
| 986 | vector=xNew<bool>(M);
|
---|
| 987 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
| 988 | }
|
---|
| 989 |
|
---|
| 990 | else
|
---|
| 991 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 992 |
|
---|
| 993 | if (py_vector2)
|
---|
| 994 | delete(py_vector2);
|
---|
[14234] | 995 | }
|
---|
| 996 | else
|
---|
[14236] | 997 | vector=NULL;
|
---|
[14234] | 998 | }
|
---|
[24112] | 999 | else if (PyList_Check(py_vector)) {
|
---|
| 1000 | /*retrieve dimensions: */
|
---|
| 1001 | M=(int)PyList_Size(py_vector);
|
---|
| 1002 | if (M) {
|
---|
| 1003 | vector=xNew<bool>(M);
|
---|
| 1004 | for (int index = 0; index < M; index++) {
|
---|
| 1005 | PyObject *item;
|
---|
| 1006 | item = PyList_GetItem(py_vector, index);
|
---|
| 1007 | FetchData(&(vector[index]),item);
|
---|
| 1008 | }
|
---|
| 1009 | }
|
---|
| 1010 | else
|
---|
| 1011 | vector=NULL;
|
---|
| 1012 | }
|
---|
[14234] | 1013 |
|
---|
[24112] | 1014 | else if (PyTuple_Check(py_vector)) {
|
---|
| 1015 | /*retrieve dimensions: */
|
---|
| 1016 | M=(int)PyTuple_Size(py_vector);
|
---|
| 1017 |
|
---|
| 1018 | if (M) {
|
---|
| 1019 | vector=xNew<bool>(M);
|
---|
| 1020 | for (int index = 0; index < M; index++) {
|
---|
| 1021 | PyObject *item;
|
---|
| 1022 | item = PyTuple_GetItem(py_vector, index);
|
---|
| 1023 | FetchData(&(vector[index]),item);
|
---|
| 1024 | }
|
---|
| 1025 | }
|
---|
| 1026 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
| 1027 | else
|
---|
| 1028 | vector=NULL;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
[14236] | 1031 | else {
|
---|
| 1032 | M=1;
|
---|
| 1033 | vector=xNew<bool>(M);
|
---|
| 1034 | FetchData(&(vector[0]),py_vector);
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
[14234] | 1037 | /*output: */
|
---|
| 1038 | if(pM)*pM=M;
|
---|
| 1039 | if(pvector)*pvector=vector;
|
---|
| 1040 | }
|
---|
| 1041 | /*}}}*/
|
---|
| 1042 |
|
---|
[13310] | 1043 | /*ISSM objects*/
|
---|
| 1044 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
| 1045 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
| 1046 |
|
---|
| 1047 | /*Initialize output*/
|
---|
| 1048 | BamgGeom* bamggeom=new BamgGeom();
|
---|
| 1049 |
|
---|
| 1050 | /*Fetch all fields*/
|
---|
| 1051 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 1052 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 1053 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
| 1054 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
| 1055 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
| 1056 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 1057 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
| 1058 |
|
---|
| 1059 | /*Assign output pointers:*/
|
---|
| 1060 | *pbamggeom=bamggeom;
|
---|
[13287] | 1061 | }
|
---|
| 1062 | /*}}}*/
|
---|
[13310] | 1063 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
| 1064 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
| 1065 |
|
---|
| 1066 | /*Initialize output*/
|
---|
| 1067 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
| 1068 |
|
---|
| 1069 | /*Fetch all fields*/
|
---|
| 1070 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 1071 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 1072 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
| 1073 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 1074 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
| 1075 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
| 1076 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
| 1077 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
| 1078 |
|
---|
| 1079 | /*Assign output pointers:*/
|
---|
| 1080 | *pbamgmesh=bamgmesh;
|
---|
[13287] | 1081 | }
|
---|
| 1082 | /*}}}*/
|
---|
[13310] | 1083 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
| 1084 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
| 1085 |
|
---|
| 1086 | /*Initialize output*/
|
---|
| 1087 | BamgOpts* bamgopts=new BamgOpts();
|
---|
| 1088 |
|
---|
| 1089 | /*Fetch all fields*/
|
---|
| 1090 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
| 1091 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
| 1092 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
| 1093 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
| 1094 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
| 1095 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
| 1096 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
| 1097 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
| 1098 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
| 1099 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
| 1100 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
| 1101 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
| 1102 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
| 1103 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
| 1104 |
|
---|
| 1105 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
| 1106 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
| 1107 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
| 1108 |
|
---|
| 1109 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
| 1110 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
| 1111 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
| 1112 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
[24112] | 1113 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,PyDict_GetItemString(py_dict,"hVertices"));
|
---|
[13310] | 1114 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
| 1115 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
| 1116 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
| 1117 |
|
---|
| 1118 | /*Additional checks*/
|
---|
| 1119 | bamgopts->Check();
|
---|
| 1120 |
|
---|
| 1121 | /*Assign output pointers:*/
|
---|
| 1122 | *pbamgopts=bamgopts;
|
---|
[13287] | 1123 | }
|
---|
| 1124 | /*}}}*/
|
---|
[13374] | 1125 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
[13373] | 1126 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
[12120] | 1127 |
|
---|
[13373] | 1128 | char *name = NULL;
|
---|
| 1129 | Option *option = NULL;
|
---|
| 1130 |
|
---|
[12776] | 1131 | /*Initialize output*/
|
---|
| 1132 | Options* options=new Options();
|
---|
| 1133 |
|
---|
[13373] | 1134 | /*Fetch all options*/
|
---|
| 1135 | for (int i=istart; i<nrhs; i=i+2){
|
---|
[19896] | 1136 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 1137 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
[19896] | 1138 | #else
|
---|
| 1139 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
| 1140 | #endif
|
---|
[12776] | 1141 |
|
---|
[13373] | 1142 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
| 1143 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
| 1144 |
|
---|
[15105] | 1145 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
---|
[13373] | 1146 |
|
---|
| 1147 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
| 1148 | // options->AddOption(option);
|
---|
| 1149 | // option=NULL;
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
[12776] | 1152 | /*Assign output pointers:*/
|
---|
| 1153 | *poptions=options;
|
---|
| 1154 | }
|
---|
| 1155 | /*}}}*/
|
---|
[15335] | 1156 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
---|
| 1157 | void FetchData(Contours** pcontours,PyObject* py_list){
|
---|
[12776] | 1158 |
|
---|
[13368] | 1159 | int numcontours,test1,test2;
|
---|
| 1160 | char *contourname = NULL;
|
---|
[15335] | 1161 | Contours *contours = NULL;
|
---|
[13368] | 1162 | Contour<double> *contouri = NULL;
|
---|
| 1163 | PyObject *py_dicti = NULL;
|
---|
| 1164 | PyObject *py_item = NULL;
|
---|
| 1165 |
|
---|
[19896] | 1166 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 1167 | if (PyUnicode_Check(py_list)){
|
---|
[19896] | 1168 | #else
|
---|
| 1169 | if (PyString_Check(py_list)){
|
---|
[24107] | 1170 | #endif
|
---|
[19896] | 1171 | FetchData(&contourname,py_list);
|
---|
| 1172 | contours=ExpRead<double>(contourname);
|
---|
| 1173 | }
|
---|
[13368] | 1174 | else if(PyList_Check(py_list)){
|
---|
| 1175 |
|
---|
[15335] | 1176 | contours=new Contours();
|
---|
[13368] | 1177 | numcontours=(int)PyList_Size(py_list);
|
---|
| 1178 |
|
---|
| 1179 | for(int i=0;i<numcontours;i++){
|
---|
| 1180 |
|
---|
[15335] | 1181 | contouri=new Contour<double>();
|
---|
[13368] | 1182 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
| 1183 |
|
---|
| 1184 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
| 1185 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
| 1186 | FetchData(&contouri->nods,py_item);
|
---|
| 1187 |
|
---|
| 1188 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
| 1189 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
| 1190 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
| 1191 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
| 1192 |
|
---|
| 1193 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
| 1194 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
| 1195 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
| 1196 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
| 1197 |
|
---|
| 1198 | contours->AddObject(contouri);
|
---|
| 1199 | }
|
---|
| 1200 | }
|
---|
| 1201 | else{
|
---|
| 1202 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
| 1205 | /*clean-up and assign output pointer*/
|
---|
[13372] | 1206 | xDelete<char>(contourname);
|
---|
[13368] | 1207 | *pcontours=contours;
|
---|
[13353] | 1208 | }
|
---|
[23231] | 1209 |
|
---|
| 1210 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
---|
| 1211 |
|
---|
| 1212 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
---|
| 1213 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
---|
| 1214 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
---|
| 1215 |
|
---|
| 1216 | /*Fetch adjacency matrix:*/
|
---|
| 1217 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
---|
| 1218 |
|
---|
| 1219 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
---|
| 1220 | pyGetJc(a,nvtxs,mwstart);
|
---|
| 1221 |
|
---|
| 1222 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
---|
| 1223 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
---|
[23708] | 1224 |
|
---|
[23231] | 1225 | int* start = xNew<int>(nvtxs+1);
|
---|
| 1226 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
---|
| 1227 | int* adjacency = xNew<int>(nzmax);
|
---|
| 1228 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
---|
| 1229 |
|
---|
| 1230 | /*Get edges weights*/
|
---|
| 1231 | int nedges = start[nvtxs];
|
---|
| 1232 | float* ewgts = NULL;
|
---|
| 1233 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
---|
| 1234 | //size may be 1 and still be empty;
|
---|
| 1235 | //presumably size of edge_weights input will never be exactly 1
|
---|
| 1236 | if(size != 1 && size != 0){
|
---|
| 1237 | ewgts = xNewZeroInit<float>(nedges);
|
---|
| 1238 | for(int i = 0; i<nedges;i++){
|
---|
| 1239 | ewgts[i] = (float)a[i];
|
---|
| 1240 | }
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | /*Assign output pointers*/
|
---|
| 1244 | *pnvtxs = nvtxs;
|
---|
| 1245 | *padjacency = adjacency;
|
---|
| 1246 | *pstart = start;
|
---|
| 1247 | *pewgts = ewgts;
|
---|
| 1248 | }
|
---|
[13353] | 1249 | /*}}}*/
|
---|
[23231] | 1250 |
|
---|
| 1251 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
---|
| 1252 | /*{{{*/
|
---|
| 1253 | //get the number of non-zero elements in each row, adding to the prior number;
|
---|
| 1254 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
---|
| 1255 | // eg: 0, 1, 3, 6, 8, 13, ...
|
---|
| 1256 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
---|
| 1257 | int c = 0;
|
---|
| 1258 | Jc[c++] = 0;
|
---|
| 1259 |
|
---|
| 1260 | for(int i=0;i<nvtxs;i++){
|
---|
| 1261 | for(int j=0;j<nvtxs;j++){
|
---|
| 1262 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1263 | Jc[c] += 1;
|
---|
| 1264 | }
|
---|
| 1265 | }
|
---|
| 1266 | c++;
|
---|
| 1267 | Jc[c] = Jc[c-1];
|
---|
| 1268 | }
|
---|
| 1269 | return;
|
---|
[22674] | 1270 | }
|
---|
[23231] | 1271 | /*}}}*/
|
---|
[13353] | 1272 |
|
---|
[23231] | 1273 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
---|
| 1274 | /*{{{*/
|
---|
| 1275 | //get the row-wise position of each non-zero element;
|
---|
| 1276 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
---|
| 1277 | // eg: 10, 24, 25, 4, ...
|
---|
| 1278 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
---|
| 1279 | // using pyGetJc to determine which indices correspond to which row
|
---|
| 1280 | int r = 0;
|
---|
| 1281 |
|
---|
| 1282 | for(int i=0;i<nvtxs;i++){
|
---|
| 1283 | for(int j=0;j<nvtxs;j++){
|
---|
| 1284 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1285 | Ir[r] = j;
|
---|
| 1286 | r++;
|
---|
| 1287 | }
|
---|
| 1288 | }
|
---|
| 1289 | }
|
---|
| 1290 | return;
|
---|
| 1291 | }
|
---|
| 1292 | /*}}}*/
|
---|
| 1293 |
|
---|
[12073] | 1294 | /*Python version dependent: */
|
---|
[23708] | 1295 | /* #if _PYTHON_MAJOR_ >= 3 */
|
---|
| 1296 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
---|
| 1297 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
---|
[12073] | 1298 |
|
---|
[23708] | 1299 | /* PyObject* py_bytes; */
|
---|
| 1300 | /* char* string=NULL; */
|
---|
[12073] | 1301 |
|
---|
[23708] | 1302 | /* /\*convert to bytes format: *\/ */
|
---|
| 1303 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
---|
[12073] | 1304 |
|
---|
[23708] | 1305 | /* /\*convert from bytes to string: *\/ */
|
---|
| 1306 | /* string=PyBytes_AsUTF8(py_bytes); */
|
---|
[13622] | 1307 |
|
---|
[23708] | 1308 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
---|
| 1309 | /* *pstring=xNew<char>(strlen(string)+1); */
|
---|
| 1310 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
---|
| 1311 | /* } */
|
---|
| 1312 | /* /\*}}}*\/ */
|
---|
| 1313 | /* #else */
|
---|
[12365] | 1314 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
[12073] | 1315 | void FetchData(char** pstring,PyObject* py_string){
|
---|
| 1316 |
|
---|
| 1317 | /*extract internal string: */
|
---|
[23708] | 1318 | #if _PYTHON_MAJOR_ == 3
|
---|
[24501] | 1319 | const char* string=PyUnicode_AsUTF8(py_string);
|
---|
[23708] | 1320 | #else
|
---|
[24502] | 1321 | char* string=PyString_AsString(py_string);
|
---|
[23708] | 1322 | #endif
|
---|
[13372] | 1323 | /*copy string (note strlen does not include trailing NULL): */
|
---|
| 1324 | *pstring=xNew<char>(strlen(string)+1);
|
---|
| 1325 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
[12073] | 1326 | }
|
---|
| 1327 | /*}}}*/
|
---|
[23708] | 1328 | //#endif
|
---|