From 39d74596dea45362a1b59821b381272c9c928e72 Mon Sep 17 00:00:00 2001 From: eap Date: Fri, 6 Dec 2019 15:46:41 +0300 Subject: [PATCH] LOT$: fix python tests --- src/MEDCoupling_Swig/DataArrayInt.i | 2 +- src/MEDCoupling_Swig/MEDCouplingCommon.i | 4 +- .../MEDCouplingDataArrayTraits.hxx | 97 +++++++++++++++++-- .../MEDCouplingDataArrayTypemaps.i | 9 +- src/MEDCoupling_Swig/MEDCouplingPickleTest.py | 2 +- .../MEDCouplingRemapperCommon.i | 13 +-- .../MEDCouplingRemapperTest.py | 6 +- .../UsersGuideExamplesTest_numpy.py | 4 +- 8 files changed, 112 insertions(+), 25 deletions(-) diff --git a/src/MEDCoupling_Swig/DataArrayInt.i b/src/MEDCoupling_Swig/DataArrayInt.i index f24a21d11..1f462d7a1 100644 --- a/src/MEDCoupling_Swig/DataArrayInt.i +++ b/src/MEDCoupling_Swig/DataArrayInt.i @@ -257,7 +257,7 @@ } else if(MEDCouplingHasNumPyBindings() && PyArray_Check(elt0) && nbOfTuples==NULL && nbOfComp==NULL) {//ARRAY.New(numpyArray) - return BuildNewInstance(elt0,NPY_INT32,&PyCallBackDataArrayInt_RefType,"INT32"); + return BuildNewInstance(elt0,NPYTraits::NPYObjectType,NPYTraits::NPYFunc,MEDCoupling::Traits::NPYStr); } else throw INTERP_KERNEL::Exception(msg.c_str()); diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index f0106b668..706b60df2 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -61,8 +61,6 @@ using namespace INTERP_KERNEL; %} -%template(ivec) std::vector; -%template(lvec) std::vector; %template(dvec) std::vector; %template(svec) std::vector; @@ -72,10 +70,12 @@ using namespace INTERP_KERNEL; //typedef std::int32_t mcIdType; typedef int mcIdType; typedef DataArrayInt32 DataArrayIdType; +%template(ivec) std::vector; #else //typedef std::int64_t mcIdType; typedef long int mcIdType; typedef DataArrayInt64 DataArrayIdType; +%template(ivec) std::vector; #endif diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx b/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx index ee04ca39a..3187b80eb 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx @@ -71,7 +71,8 @@ struct PyCallBackDataArraySt { }; typedef struct PyCallBackDataArraySt PyCallBackDataArrayChar; -typedef struct PyCallBackDataArraySt PyCallBackDataArrayInt; +typedef struct PyCallBackDataArraySt PyCallBackDataArrayInt32; +typedef struct PyCallBackDataArraySt PyCallBackDataArrayInt64; typedef struct PyCallBackDataArraySt PyCallBackDataArrayFloat; typedef struct PyCallBackDataArraySt PyCallBackDataArrayDouble; @@ -85,9 +86,15 @@ extern "C" return (PyObject *)self; } - static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) + static PyObject *callbackmcdataarrayint32___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) { - PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) ); + PyCallBackDataArrayInt32 *self = (PyCallBackDataArrayInt32 *) ( type->tp_alloc(type, 0) ); + return (PyObject *)self; + } + + static PyObject *callbackmcdataarrayint64___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) + { + PyCallBackDataArrayInt64 *self = (PyCallBackDataArrayInt64 *) ( type->tp_alloc(type, 0) ); return (PyObject *)self; } @@ -124,7 +131,7 @@ extern "C" // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed. // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients. - static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw) + static PyObject *callbackmcdataarrayint32_call(PyCallBackDataArrayInt32 *self, PyObject *args, PyObject *kw) { if(self->_pt_mc) { @@ -135,6 +142,19 @@ extern "C" return Py_None; } + // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed. + // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients. + static PyObject *callbackmcdataarrayint64_call(PyCallBackDataArrayInt64 *self, PyObject *args, PyObject *kw) + { + if(self->_pt_mc) + { + MEDCoupling::MemArray& mma=self->_pt_mc->accessToMemArray(); + mma.destroy(); + } + Py_XINCREF(Py_None); + return Py_None; + } + // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed. // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients. static PyObject *callbackmcdataarrayfloat_call(PyCallBackDataArrayFloat *self, PyObject *args, PyObject *kw) @@ -205,10 +225,10 @@ PyTypeObject PyCallBackDataArrayChar_RefType = { }; -PyTypeObject PyCallBackDataArrayInt_RefType = { +PyTypeObject PyCallBackDataArrayInt32_RefType = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "callbackmcdataarrayint", - sizeof(PyCallBackDataArrayInt), + "callbackmcdataarrayint32", + sizeof(PyCallBackDataArrayInt32), 0, callbackmcdataarray_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -220,7 +240,7 @@ PyTypeObject PyCallBackDataArrayInt_RefType = { 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash*/ - (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/ + (ternaryfunc)callbackmcdataarrayint32_call, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ @@ -243,7 +263,50 @@ PyTypeObject PyCallBackDataArrayInt_RefType = { 0, /*tp_dictoffset*/ callbackmcdataarray___init__, /*tp_init*/ PyType_GenericAlloc, /*tp_alloc*/ - callbackmcdataarrayint___new__, /*tp_new*/ + callbackmcdataarrayint32___new__, /*tp_new*/ + PyObject_GC_Del, /*tp_free*/ +}; + + +PyTypeObject PyCallBackDataArrayInt64_RefType = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "callbackmcdataarrayint64", + sizeof(PyCallBackDataArrayInt64), + 0, + callbackmcdataarray_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + (ternaryfunc)callbackmcdataarrayint64_call, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + 0, /*tp_methods*/ + 0, /*tp_members*/ + 0, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + callbackmcdataarray___init__, /*tp_init*/ + PyType_GenericAlloc, /*tp_alloc*/ + callbackmcdataarrayint64___new__, /*tp_new*/ PyObject_GC_Del, /*tp_free*/ }; @@ -352,6 +415,22 @@ struct NPYTraits static PyTypeObject *NPYFunc; static PyObject *Array_SWIGTYPE; }; + +template<> +struct NPYTraits +{ + static const int NPYObjectType=NPY_INT32; + static PyTypeObject *NPYFunc; + static PyObject *Array_SWIGTYPE; +}; + +template<> +struct NPYTraits +{ + static const int NPYObjectType=NPY_INT64; + static PyTypeObject *NPYFunc; + static PyObject *Array_SWIGTYPE; +}; #endif #endif diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index b23df939f..85a7a9d16 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -167,6 +167,9 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype if(PyArray_DESCR(elt0)->type_num != npyObjectType) { std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!"; +#ifdef _DEBUG_ + oss << " type_num == " << PyArray_DESCR(elt0)->type_num; +#endif throw INTERP_KERNEL::Exception(oss.str().c_str()); } npy_intp sz0=PyArray_DIM(elt0,0); @@ -3122,7 +3125,7 @@ bool isCSRMatrix(PyObject *m) #endif } -void convertCSR_MCDataToVectMapIntDouble(const MEDCoupling::DataArrayIdType *indptrPtr, const MEDCoupling::DataArrayIdType *indicesPtr, const MEDCoupling::DataArrayDouble *dataPtr, std::vector >& mCpp) +void convertCSR_MCDataToVectMapIntDouble(const MEDCoupling::DataArrayInt32 *indptrPtr, const MEDCoupling::DataArrayInt32 *indicesPtr, const MEDCoupling::DataArrayDouble *dataPtr, std::vector >& mCpp) { auto nbOfRows(indptrPtr->getNumberOfTuples()-1); if(nbOfRows<0) @@ -3354,6 +3357,10 @@ PyObject *DataArrayT_isub__internal(PyObject *trueSelf, PyObject *obj, typename PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayDouble_RefType; PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayFloat_RefType; + +PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayInt32_RefType; + +PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayInt64_RefType; #endif template diff --git a/src/MEDCoupling_Swig/MEDCouplingPickleTest.py b/src/MEDCoupling_Swig/MEDCouplingPickleTest.py index e5f6bb732..c0c633e1a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingPickleTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingPickleTest.py @@ -347,7 +347,7 @@ class MEDCouplingPickleTest(unittest.TestCase): f.setName("aname") a=f.getArray() b=a[:] ; b.iota(7000) - f.setArray(DataArrayInt.Meld(a,b)) + f.setArray(DataArrayInt32.Meld(a,b)) f.getArray().setInfoOnComponents(["u1","vv2"]) f.checkConsistencyLight(); # diff --git a/src/MEDCoupling_Swig/MEDCouplingRemapperCommon.i b/src/MEDCoupling_Swig/MEDCouplingRemapperCommon.i index 6c4e27c31..8368ec234 100644 --- a/src/MEDCoupling_Swig/MEDCouplingRemapperCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingRemapperCommon.i @@ -97,14 +97,15 @@ namespace MEDCoupling PyObject *indptr(PyObject_GetAttrString(m,"indptr")); PyObject *indices(PyObject_GetAttrString(m,"indices")); PyObject *data(PyObject_GetAttrString(m,"data")); - MCAuto indptrPtr, indicesPtr; -#if defined(MEDCOUPLING_USE_64BIT_IDS) - indptrPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indptr,NULL,NULL); - indicesPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indices,NULL,NULL); -#else + MCAuto indptrPtr, indicesPtr; + // csr_matrix.indptr and csr_matrix.indices are always dtype=int32 +// #if defined(MEDCOUPLING_USE_64BIT_IDS) +// indptrPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indptr,NULL,NULL); +// indicesPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indices,NULL,NULL); +// #else indptrPtr = MEDCoupling_DataArrayInt32_New__SWIG_1(indptr,NULL,NULL); indicesPtr = MEDCoupling_DataArrayInt32_New__SWIG_1(indices,NULL,NULL); -#endif +//#endif MCAuto dataPtr(MEDCoupling_DataArrayDouble_New__SWIG_1(data,NULL,NULL)); convertCSR_MCDataToVectMapIntDouble(indptrPtr,indicesPtr,dataPtr,mCpp); Py_XDECREF(data); Py_XDECREF(indptr); Py_XDECREF(indices); diff --git a/src/MEDCoupling_Swig/MEDCouplingRemapperTest.py b/src/MEDCoupling_Swig/MEDCouplingRemapperTest.py index e6aedde61..58054d519 100644 --- a/src/MEDCoupling_Swig/MEDCouplingRemapperTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingRemapperTest.py @@ -806,8 +806,8 @@ class MEDCouplingBasicsTest(unittest.TestCase): vals*=1e-5 eps0=DataArrayDouble(m0.data)-vals ; eps0.abs() self.assertTrue(eps0.findIdsInRange(1e-17,1e300).empty()) - self.assertTrue(DataArrayInt(m0.indices).isEqual(DataArrayInt([0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27]))) - self.assertTrue(DataArrayInt(m0.indptr).isEqual(DataArrayInt([0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312]))) + self.assertTrue(DataArrayInt32(m0.indices).isEqual(DataArrayInt32([0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,2,3,1,3,5,4,5,7,6,7,9,8,9,11,10,11,13,12,13,15,14,15,17,16,17,19,18,19,21,20,21,23,22,23,25,24,25,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27,0,1,3,1,4,5,4,6,7,6,8,9,8,10,11,10,12,13,12,14,15,14,16,17,16,18,19,18,20,21,20,22,23,22,24,25,24,26,27]))) + self.assertTrue(DataArrayInt32(m0.indptr).isEqual(DataArrayInt32([0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312]))) # rem2=MEDCouplingRemapper() ; rem2.setIntersectionType(Barycentric) rem2.prepare(b,a,"P0P1") @@ -839,7 +839,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): rem.setMinDotBtwPlane3DSurfIntersect(0.99)# this line is important it is to tell to remapper to select only cells with very close orientation rem.prepare(skinAndNonConformCells,skinAndNonConformCells,"P0P0") mat=rem.getCrudeCSRMatrix() - indptr=DataArrayInt(mat.indptr) + indptr=DataArrayInt32(mat.indptr) #not depend on MEDCouplingUse64BitIDs() indptr2=indptr.deltaShiftIndex() cellIdsOfNonConformCells=indptr2.findIdsNotEqual(1) cellIdsOfSkin=indptr2.findIdsEqual(1) diff --git a/src/MEDCoupling_Swig/UsersGuideExamplesTest_numpy.py b/src/MEDCoupling_Swig/UsersGuideExamplesTest_numpy.py index 6ad489506..cdca7ec5f 100644 --- a/src/MEDCoupling_Swig/UsersGuideExamplesTest_numpy.py +++ b/src/MEDCoupling_Swig/UsersGuideExamplesTest_numpy.py @@ -35,7 +35,7 @@ else: # NumPy is an optional pre-requisite! assert(MEDCoupling.MEDCouplingHasNumPyBindings()) a=numpy.arange(20,dtype=numpy.int32) -d=DataArrayInt(a) # d owns data of a -e=DataArrayInt(a) # a not owned -> e only an access to chunk of a +d=DataArrayInt32(a) # d owns data of a +e=DataArrayInt32(a) # a not owned -> e only an access to chunk of a a1=d.toNumPyArray() #! [UG_DataArrayNumpy_0] -- 2.39.2