From 8789b74c2b857a64813f3db83ee86b1614ce3ac5 Mon Sep 17 00:00:00 2001 From: ageay Date: Mon, 23 Sep 2013 09:05:01 +0000 Subject: [PATCH] CSR output matrix MEDCouplingRemapper::getCrudeCSRMatrix --- src/MEDCoupling/MEDCouplingRemapper.cxx | 8 +++++ src/MEDCoupling/MEDCouplingRemapper.hxx | 1 + src/MEDCoupling_Swig/MEDCouplingRemapper.i | 7 ++++ src/MEDCoupling_Swig/MEDCouplingTypemaps.i | 40 ++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingRemapper.cxx b/src/MEDCoupling/MEDCouplingRemapper.cxx index 2412dddb0..dd837acda 100644 --- a/src/MEDCoupling/MEDCouplingRemapper.cxx +++ b/src/MEDCoupling/MEDCouplingRemapper.cxx @@ -1147,6 +1147,14 @@ const std::vector >& MEDCouplingRemapper::getCrudeMatrix() return _matrix; } +/*! + * Returns the number of columns of matrix returned by MEDCouplingRemapper::getCrudeMatrix method. + */ +int MEDCouplingRemapper::getNumberOfColsOfMatrix() const +{ + return (int)_deno_reverse_multiply.size(); +} + /*! * This method is supposed to be called , if needed, right after MEDCouplingRemapper::prepare or MEDCouplingRemapper::prepareEx. * If not the behaviour is unpredictable. diff --git a/src/MEDCoupling/MEDCouplingRemapper.hxx b/src/MEDCoupling/MEDCouplingRemapper.hxx index 5bc58e86d..36cb62369 100644 --- a/src/MEDCoupling/MEDCouplingRemapper.hxx +++ b/src/MEDCoupling/MEDCouplingRemapper.hxx @@ -72,6 +72,7 @@ namespace ParaMEDMEM MEDCOUPLINGREMAPPER_EXPORT double getMaxValueInCrudeMatrix() const; public: MEDCOUPLINGREMAPPER_EXPORT const std::vector >& getCrudeMatrix() const; + MEDCOUPLINGREMAPPER_EXPORT int getNumberOfColsOfMatrix() const; MEDCOUPLINGREMAPPER_EXPORT static void PrintMatrix(const std::vector >& m); private: int prepareInterpKernelOnly(); diff --git a/src/MEDCoupling_Swig/MEDCouplingRemapper.i b/src/MEDCoupling_Swig/MEDCouplingRemapper.i index cfb1129e1..5578506ac 100644 --- a/src/MEDCoupling_Swig/MEDCouplingRemapper.i +++ b/src/MEDCoupling_Swig/MEDCouplingRemapper.i @@ -73,6 +73,7 @@ namespace ParaMEDMEM int nullifiedTinyCoeffInCrudeMatrixAbs(double maxValAbs) throw(INTERP_KERNEL::Exception); int nullifiedTinyCoeffInCrudeMatrix(double scaleFactor) throw(INTERP_KERNEL::Exception); double getMaxValueInCrudeMatrix() const throw(INTERP_KERNEL::Exception); + int getNumberOfColsOfMatrix() const throw(INTERP_KERNEL::Exception); %extend { PyObject *getCrudeMatrix() const throw(INTERP_KERNEL::Exception) @@ -90,6 +91,12 @@ namespace ParaMEDMEM } return ret; } +#ifdef WITH_NUMPY + PyObject *getCrudeCSRMatrix() const throw(INTERP_KERNEL::Exception) + { + return ToCSRMatrix(self->getCrudeMatrix(),self->getNumberOfColsOfMatrix()); + } +#endif } }; } diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index dd61b8ee1..201bf6c0b 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -399,6 +399,46 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr) return ret; } +SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self); +SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self); + +PyObject *ToCSRMatrix(const std::vector >& m, int nbCols) throw(INTERP_KERNEL::Exception) +{ + int nbRows((int)m.size()); + ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr indPtr(ParaMEDMEM::DataArrayInt::New()),indices(ParaMEDMEM::DataArrayInt::New()); + ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr data(ParaMEDMEM::DataArrayDouble::New()); + indPtr->alloc(nbRows+1,1); + int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++; + int sz2(0); + for(std::vector >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++) + { + sz2+=(int)(*it0).size(); + *intPtr_ptr=sz2; + } + indices->alloc(sz2,1); data->alloc(sz2,1); + int *indices_ptr(indices->getPointer()); + double *data_ptr(data->getPointer()); + for(std::vector >::const_iterator it0=m.begin();it0!=m.end();it0++) + for(std::map::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++) + { + *indices_ptr=(*it1).first; + *data_ptr=(*it1).second; + } + PyObject *a(ParaMEDMEM_DataArrayDouble_toNumPyArray(data)),*b(ParaMEDMEM_DataArrayInt_toNumPyArray(indices)),*c(ParaMEDMEM_DataArrayInt_toNumPyArray(indPtr)); + // + PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2)); + PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0); + PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols)); + PyDict_SetItem(kw,PyString_FromString("shape"),kw1); + PyObject* pdict=PyDict_New(); + PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins()); + PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict); + PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix"); + PyObject *ret(PyObject_Call(csrMatrixCls,args,kw)); + Py_DECREF(pdict); + return ret; +} + #endif static PyObject *convertMesh(ParaMEDMEM::MEDCouplingMesh *mesh, int owner) throw(INTERP_KERNEL::Exception) -- 2.39.2