Py_XINCREF(trueSelf);
return trueSelf;
}
+#ifdef WITH_NUMPY
+ PyObject *toNumPyMatrix() throw(INTERP_KERNEL::Exception) // not const. It is not a bug !
+ {
+ PyObject *obj(ToNumPyArrayUnderground<DataArrayDouble,double>(self->getData(),NPY_DOUBLE,"DataArrayDouble",self->getNumberOfRows(),self->getNumberOfCols()));
+ return obj;
+ }
+#endif
}
};
}
}
template<class MCData, class T>
-PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
+PyObject *ToNumPyArrayUnderground(MCData *self, int npyObjectType, const char *MCDataStr, int nbTuples, int nbComp)
{
if(!self->isAllocated())
{
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
ParaMEDMEM::MemArray<T>& mem=self->accessToMemArray();
- int nbComp=self->getNumberOfComponents();
if(nbComp==0)
{
std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !";
}
int nbDims=nbComp==1?1:2;
npy_intp dim[2];
- dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp;
+ dim[0]=(npy_intp)nbTuples; dim[1]=nbComp;
const T *bg=self->getConstPointer();
PyObject *ret(PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg)));
if(mem.isDeallocatorCalled())
return ret;
}
+template<class MCData, class T>
+PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
+{
+ return ToNumPyArrayUnderground<MCData,T>(self,npyObjectType,MCDataStr,self->getNumberOfTuples(),self->getNumberOfComponents());
+}
+
SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
self.assertEqual(a2.tolist(),[[0.,1.,2.],[3.,4.,5.],[6.,7.,8.],[9.,10.,11.],[12.,13.,14.]])
pass
+ @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
+ def test27(self):
+ m0=DenseMatrix(DataArrayDouble([2,3,4,5,1,6]),2,3)
+ m0np=m0.toNumPyMatrix()
+ self.assertEqual(m0np.shape,(2,3))
+ self.assertEqual(m0np.tolist(),[[2.0,3.0,4.0],[5.0,1.0,6.0]])
+ pass
+
def setUp(self):
pass
pass