From 840dc432aad5351dda324f43f3d5c399df5e103c Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 18 Apr 2013 06:52:05 +0000 Subject: [PATCH] Numpy bindings --- src/MEDCoupling_Swig/MEDCouplingNumpyTest.py | 148 ++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/src/MEDCoupling_Swig/MEDCouplingNumpyTest.py b/src/MEDCoupling_Swig/MEDCouplingNumpyTest.py index ff5b3398b..d224a9f53 100644 --- a/src/MEDCoupling_Swig/MEDCouplingNumpyTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingNumpyTest.py @@ -91,7 +91,7 @@ class MEDCouplingNumpyTest(unittest.TestCase): def test4(self): a=zeros(20,dtype=int32) b = a[::-1] - #d=DataArrayInt(b) + self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b is not contiguous in memory pass @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") @@ -152,7 +152,153 @@ class MEDCouplingNumpyTest(unittest.TestCase): self.assertTrue(not e.isAllocated()) self.assertTrue(not f.isAllocated()) pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test8(self): + a=arange(20,dtype=int32) + self.assertTrue(a.flags["OWNDATA"]) + d=DataArrayInt(a) # d owns data of a + self.assertTrue(not a.flags["OWNDATA"]) + d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again + self.assertTrue(a.flags["OWNDATA"]) + self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]))) + self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test9(self): + sz=20 + a=array(0,dtype=float64) + a.resize(sz) + a[:]=4 + self.assertEqual(getrefcount(a),2) + a=a.cumsum(dtype=float64) + self.assertEqual(getrefcount(a),2) + d=DataArrayDouble(a) + d[:]=2 + # + e=DataArrayDouble(sz) ; e.fillWithValue(2) + self.assertTrue(d.isEqual(e,1e-14)) + # + a[:]=4 ; e.fillWithValue(4) + self.assertTrue(d.isEqual(e,1e-14)) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test10(self): + sz=20 + a=array(0,dtype=float64) + a.resize(sz,2) + self.assertEqual(getrefcount(a),2) + b=a.reshape(2*sz) + self.assertEqual(getrefcount(a),3) + self.assertEqual(getrefcount(b),2) + b[:]=5 + d=DataArrayDouble(b) + # + e=DataArrayDouble(sz*2) ; e.fillWithValue(5) + self.assertTrue(d.isEqual(e,1e-14)) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test11(self): + sz=10 + a=array(0,dtype=float64) + a.resize(sz,2) + b=a.reshape(2*sz) + c=a.reshape(2,sz) + b[:]=6 + b[7:17]=7 + d=DataArrayDouble(b) + self.assertTrue(d.isEqual(DataArrayDouble([6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,6,6,6]),1e-14)) + # + a=zeros((10,2),dtype=float64) + b=a.T + c=b.view() + a.shape=20 + a[3:]=10. + d=DataArrayDouble(a) + self.assertTrue(d.isEqual(DataArrayDouble([0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]),1e-14)) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test12(self): + a=zeros(20,dtype=float64) + b = a[::-1] + self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b is not contiguous in memory + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test13(self): + a=arange(20,dtype=float64) + self.assertEqual(weakref.getweakrefcount(a),0) + d=DataArrayDouble(a) + self.assertEqual(weakref.getweakrefcount(a),1) + self.assertTrue(not a.flags["OWNDATA"]) + self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + self.assertEqual(len(d),20) + a[:]=2 # modifying a and d because a and d share the same chunk of data + self.assertTrue(d.isUniform(2,1e-14)) + del d # d is destroyed, a retrieves its ownership of its initial chunk of data + self.assertTrue(a.flags["OWNDATA"]) + a[:]=4 # a can be used has usual + self.assertTrue(DataArrayDouble(a).isUniform(4,1e-14)) + pass + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test14(self): + a=arange(20,dtype=float64) + d=DataArrayDouble(a) # d owns data of a + e=DataArrayDouble(a) # a not owned -> e only an access to chunk of a + self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + self.assertTrue(e.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + a[:]=6 + self.assertTrue(d.isUniform(6,1e-14)) + self.assertTrue(e.isUniform(6,1e-14)) + del a # a destroyed -> d no change because owned and e array is has no more data set + self.assertTrue(d.isUniform(6,1e-14)) + self.assertTrue(not e.isAllocated()) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test15(self): + a=array(0,dtype=float64) ; a.resize(10,2) + b=a.reshape(20) + c=a.reshape(2,10) + d=DataArrayDouble(b) # d owns data of a + e=DataArrayDouble(b) # a not owned -> e only an access to chunk of a + f=DataArrayDouble(b) # a not owned -> e only an access to chunk of a + del d # d removed -> a ownes again data + self.assertTrue(e.isUniform(0,1e-14)) + e[:]=6 + self.assertTrue(e.isUniform(6,1e-14)) + self.assertTrue(f.isUniform(6,1e-14)) + self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6]) + self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]]) + b[:]=arange(20) + del b # no impact on e and f because a is the base of a. + self.assertTrue(f.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + self.assertTrue(e.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated + self.assertTrue(f.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + self.assertTrue(e.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-14)) + del c # c killed -> a killed -> e and d are put into not allocated state + self.assertTrue(not e.isAllocated()) + self.assertTrue(not f.isAllocated()) + pass + + @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy") + def test16(self): + a=arange(20,dtype=float64) + self.assertTrue(a.flags["OWNDATA"]) + d=DataArrayDouble(a) # d owns data of a + self.assertTrue(not a.flags["OWNDATA"]) + d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again + self.assertTrue(a.flags["OWNDATA"]) + self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),1e-14)) + self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]) + pass + def setUp(self): pass pass -- 2.39.2