Changeset 14234


Ignore:
Timestamp:
01/10/13 11:07:29 (12 years ago)
Author:
jschierm
Message:

NEW: Added python read/write functions for booleans.

Location:
issm/trunk-jpl/src/wrappers/python/io
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp

    r14221 r14234  
    148148
    149149                        else
    150                                 _error_("unrecognized float pyarray type in input!");
     150                                _error_("unrecognized double pyarray type in input!");
    151151                }
    152152                else
     
    225225
    226226                        else
    227                                 _error_("unrecognized long pyarray type in input!");
     227                                _error_("unrecognized int pyarray type in input!");
    228228                }
    229229                else
     
    244244}
    245245/*}}}*/
     246/*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
     247void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
     248
     249        /*output: */
     250        bool* bmatrix=NULL;
     251        bool* matrix=NULL;
     252        int M,N;
     253        int ndim;
     254        npy_intp*  dims=NULL;
     255
     256        /*intermediary:*/
     257        double* dmatrix=NULL;
     258        long* lmatrix=NULL;
     259        int i;
     260
     261        if     (PyArray_Check((PyArrayObject*)py_matrix)) {
     262                /*retrieve dimensions: */
     263                ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
     264                if      (ndim==2) {
     265                        dims=PyArray_DIMS((PyArrayObject*)py_matrix);
     266                        M=dims[0]; N=dims[1];
     267                }
     268                else if (ndim==1) {
     269                        dims=PyArray_DIMS((PyArrayObject*)py_matrix);
     270                        M=dims[0]; N=1;
     271                }
     272                else
     273                        _error_("expecting an MxN matrix or M vector in input!");
     274
     275                if (M && N) {
     276                        if      (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
     277                                /*retrieve internal value: */
     278                                dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
     279
     280                                /*transform into bool matrix: */
     281                                matrix=xNew<bool>(M*N);
     282                                for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
     283                        }
     284
     285                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
     286                                /*retrieve internal value: */
     287                                lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
     288
     289                                /*transform into bool matrix: */
     290                                matrix=xNew<bool>(M*N);
     291                                for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
     292                        }
     293
     294                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
     295                                /*retrieve internal value: */
     296                                bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
     297
     298                                /*copy matrix: */
     299                                matrix=xNew<bool>(M*N);
     300                                memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
     301                        }
     302
     303                        else
     304                                _error_("unrecognized bool pyarray type in input!");
     305                }
     306                else
     307                        matrix=NULL;
     308        }
     309
     310        else {
     311                M=1;
     312                N=1;
     313                matrix=xNew<bool>(M*N);
     314                FetchData(&(matrix[0]),py_matrix);
     315        }
     316
     317        /*output: */
     318        if(pM)*pM=M;
     319        if(pN)*pN=N;
     320        if(pmatrix)*pmatrix=matrix;
     321}
     322/*}}}*/
    246323/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
    247324void FetchData(double** pvector,int* pM,PyObject* py_vector){
     
    294371
    295372                else
    296                         _error_("unrecognized vector type in input!");
     373                        _error_("unrecognized double pyarray type in input!");
    297374        }
    298375        else
     
    354431
    355432                else
    356                  _error_("unrecognized vector type in input!");
     433                 _error_("unrecognized int pyarray type in input!");
    357434        }
    358435        else
    359436         vector=NULL;
     437
     438        /*output: */
     439        if(pM)*pM=M;
     440        if(pvector)*pvector=vector;
     441}
     442/*}}}*/
     443/*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
     444void FetchData(bool** pvector,int* pM,PyObject* py_vector){
     445
     446        /*output: */
     447        bool* bvector=NULL;
     448        bool* vector=NULL;
     449        int M;
     450        int ndim;
     451        npy_intp*  dims=NULL;
     452
     453        /*intermediary:*/
     454        double* dvector=NULL;
     455        long* lvector=NULL;
     456        int i;
     457
     458        /*retrieve dimensions: */
     459        ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
     460        if(ndim!=1)_error_("expecting an Mx1 vector in input!");
     461        dims=PyArray_DIMS((PyArrayObject*)py_vector);
     462        M=dims[0];
     463
     464        if (M) {
     465                if      (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
     466                        /*retrieve internal value: */
     467                        dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
     468
     469                        /*transform into bool vector: */
     470                        vector=xNew<bool>(M);
     471                        for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
     472                }
     473
     474                else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
     475                        /*retrieve internal value: */
     476                        lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
     477
     478                        /*transform into bool vector: */
     479                        vector=xNew<bool>(M);
     480                        for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
     481                }
     482
     483                else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
     484                        /*retrieve internal value: */
     485                        bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
     486
     487                        /*copy vector: */
     488                        vector=xNew<bool>(M);
     489                        memcpy(vector,bvector,(M)*sizeof(bool));
     490                }
     491
     492                else
     493                        _error_("unrecognized bool pyarray type in input!");
     494        }
     495        else
     496                vector=NULL;
    360497
    361498        /*output: */
  • TabularUnified issm/trunk-jpl/src/wrappers/python/io/WritePythonData.cpp

    r14232 r14234  
    209209}
    210210/*}}}*/
     211/*FUNCTION WriteData(PyObject* py_tuple,int index,SeqMat<bool>* matrix){{{*/
     212void WriteData(PyObject* py_tuple,int index,SeqMat<bool>* matrix){
     213
     214        int M,N;
     215        bool* buffer=NULL;
     216        npy_intp dims[2]={0,0};
     217        PyObject* array=NULL;
     218
     219        matrix->GetSize(&M,&N);
     220        buffer=matrix->ToSerial();
     221
     222        dims[0]=(npy_intp)M;
     223        dims[1]=(npy_intp)N;
     224        array=PyArray_SimpleNewFromData(2,dims,NPY_BOOL,buffer);
     225
     226        PyTuple_SetItem(py_tuple, index, array);
     227
     228}/*}}}*/
     229/*FUNCTION WriteData(PyObject* py_tuple,int index,SeqVec<bool>* vector){{{*/
     230void WriteData(PyObject* py_tuple,int index,SeqVec<bool>* vector){
     231
     232        int M;
     233        bool* buffer=NULL;
     234        npy_intp dim=10;
     235        PyObject* array=NULL;
     236
     237        vector->GetSize(&M);
     238        buffer=vector->ToMPISerial();
     239
     240        dim=(npy_intp)M;
     241        array=PyArray_SimpleNewFromData(1,&dim,NPY_BOOL,buffer);
     242
     243        PyTuple_SetItem(py_tuple, index, array);
     244}
     245/*}}}*/
    211246/*FUNCTION WriteData(PyObject* py_tuple,int index,RiftStruct* riftstruct){{{*/
    212247void WriteData(PyObject* py_tuple,int index,RiftStruct* riftstruct){
     
    288323}
    289324/*}}}*/
     325/*FUNCTION PyArrayFromCopiedData(int dimi,int dimj,bool* data){{{*/
     326PyObject* PyArrayFromCopiedData(int dimi,int dimj,bool* data){
     327
     328        bool* pydata;
     329        npy_intp pydims[2]={0,0};
     330
     331        /*  note that PyArray_SimpleNewFromData does not copy the data, so that when the original
     332                 object (e.g. bamggeom,bamgmesh) is deleted, the data is gone.  */
     333
     334        pydims[0]=(npy_intp)dimi;
     335        pydims[1]=(npy_intp)dimj;
     336        pydata=xNew<bool>(dimi*dimj);
     337        memcpy(pydata,data,dimi*dimj*sizeof(bool));
     338        return PyArray_SimpleNewFromData(2,pydims,NPY_BOOL,pydata);
     339}
     340/*}}}*/
  • TabularUnified issm/trunk-jpl/src/wrappers/python/io/pythonio.h

    r14221 r14234  
    2727void WriteData(PyObject* py_tuple,int index, SeqMat<int>* matrix);
    2828void WriteData(PyObject* py_tuple,int index, SeqVec<int>* vector);
     29void WriteData(PyObject* py_tuple,int index, SeqMat<bool>* matrix);
     30void WriteData(PyObject* py_tuple,int index, SeqVec<bool>* vector);
    2931void WriteData(PyObject* py_tuple,int index, BamgGeom* bamggeom);
    3032void WriteData(PyObject* py_tuple,int index, BamgMesh* bamgmesh);
    3133void WriteData(PyObject* py_tuple,int index, RiftStruct* riftstruct);
    3234
    33 void FetchData(int** pvector,int* pM,PyObject* py_ref);
    34 void FetchData(double** pvector,int* pM,PyObject* py_ref);
    3535void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_array);
    3636void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix);
     37void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix);
     38void FetchData(double** pvector,int* pM,PyObject* py_ref);
     39void FetchData(int** pvector,int* pM,PyObject* py_ref);
     40void FetchData(bool** pvector,int* pM,PyObject* py_ref);
    3741void FetchData(char** pstring,PyObject* py_unicode);
    3842void FetchData(double* pscalar,PyObject* py_float);
     
    5155PyObject* PyArrayFromCopiedData(int dimi,int dimj,double* data);
    5256PyObject* PyArrayFromCopiedData(int dimi,int dimj,int* data);
     57PyObject* PyArrayFromCopiedData(int dimi,int dimj,bool* data);
    5358
    5459#endif  /* _IO_H_ */
Note: See TracChangeset for help on using the changeset viewer.