1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 from MEDCoupling import *
24 if MEDCouplingHasNumpyBindings():
28 from sys import getrefcount
30 import os,gc,weakref,unittest
32 class MEDCouplingNumpyTest(unittest.TestCase):
34 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
37 a=array(0,dtype=int32)
40 self.assertEqual(getrefcount(a),2)
41 a=a.cumsum(dtype=int32)
42 self.assertEqual(getrefcount(a),2)
46 e=DataArrayInt(sz) ; e.fillWithValue(2)
47 self.assertTrue(d.isEqual(e))
49 a[:]=4 ; e.fillWithValue(4)
50 self.assertTrue(d.isEqual(e))
53 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
56 a=array(0,dtype=int32)
58 self.assertEqual(getrefcount(a),2)
60 self.assertEqual(getrefcount(a),3)
61 self.assertEqual(getrefcount(b),2)
65 e=DataArrayInt(sz*2) ; e.fillWithValue(5)
66 self.assertTrue(d.isEqual(e))
69 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
72 a=array(0,dtype=int32)
79 self.assertTrue(d.isEqual(DataArrayInt([6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,6,6,6])))
81 a=zeros((10,2),dtype=int32)
87 self.assertTrue(d.isEqual(DataArrayInt([0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10])))
90 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
92 a=zeros(20,dtype=int32)
94 self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b is not contiguous in memory
97 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
99 a=arange(20,dtype=int32)
100 self.assertEqual(weakref.getweakrefcount(a),0)
102 self.assertEqual(weakref.getweakrefcount(a),1)
103 self.assertTrue(not a.flags["OWNDATA"])
104 self.assertTrue(d.isIdentity())
105 self.assertEqual(len(d),20)
106 a[:]=2 # modifying a and d because a and d share the same chunk of data
107 self.assertTrue(d.isUniform(2))
108 del d # d is destroyed, a retrieves its ownership of its initial chunk of data
109 self.assertTrue(a.flags["OWNDATA"])
110 a[:]=4 # a can be used has usual
111 self.assertTrue(DataArrayInt(a).isUniform(4))
114 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
116 a=arange(20,dtype=int32)
117 d=DataArrayInt(a) # d owns data of a
118 e=DataArrayInt(a) # a not owned -> e only an access to chunk of a
119 self.assertTrue(d.isIdentity())
120 self.assertTrue(e.isIdentity())
122 self.assertTrue(d.isUniform(6))
123 self.assertTrue(e.isUniform(6))
124 del a # a destroyed -> d no change because owned and e array is has no more data set
125 self.assertTrue(d.isUniform(6))
126 self.assertTrue(not e.isAllocated())
129 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
131 a=array(0,dtype=int32) ; a.resize(10,2)
134 d=DataArrayInt(b) # d owns data of a
135 e=DataArrayInt(b) # a not owned -> e only an access to chunk of a
136 f=DataArrayInt(b) # a not owned -> e only an access to chunk of a
137 del d # d removed -> a ownes again data
138 self.assertTrue(e.isUniform(0))
140 self.assertTrue(e.isUniform(6))
141 self.assertTrue(f.isUniform(6))
142 self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6])
143 self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]])
145 del b # no impact on e and f because a is the base of a.
146 self.assertTrue(f.isIdentity())
147 self.assertTrue(e.isIdentity())
148 del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated
149 self.assertTrue(f.isIdentity())
150 self.assertTrue(e.isIdentity())
151 del c # c killed -> a killed -> e and d are put into not allocated state
152 self.assertTrue(not e.isAllocated())
153 self.assertTrue(not f.isAllocated())
156 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
158 a=arange(20,dtype=int32)
159 self.assertTrue(a.flags["OWNDATA"])
160 d=DataArrayInt(a) # d owns data of a
161 self.assertTrue(not a.flags["OWNDATA"])
162 d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again
163 self.assertTrue(a.flags["OWNDATA"])
164 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])))
165 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
168 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
171 a=array(0,dtype=float64)
174 self.assertEqual(getrefcount(a),2)
175 a=a.cumsum(dtype=float64)
176 self.assertEqual(getrefcount(a),2)
180 e=DataArrayDouble(sz) ; e.fillWithValue(2)
181 self.assertTrue(d.isEqual(e,1e-14))
183 a[:]=4 ; e.fillWithValue(4)
184 self.assertTrue(d.isEqual(e,1e-14))
187 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
190 a=array(0,dtype=float64)
192 self.assertEqual(getrefcount(a),2)
194 self.assertEqual(getrefcount(a),3)
195 self.assertEqual(getrefcount(b),2)
199 e=DataArrayDouble(sz*2) ; e.fillWithValue(5)
200 self.assertTrue(d.isEqual(e,1e-14))
203 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
206 a=array(0,dtype=float64)
213 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))
215 a=zeros((10,2),dtype=float64)
221 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))
224 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
226 a=zeros(20,dtype=float64)
228 self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b is not contiguous in memory
231 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
233 a=arange(20,dtype=float64)
234 self.assertEqual(weakref.getweakrefcount(a),0)
236 self.assertEqual(weakref.getweakrefcount(a),1)
237 self.assertTrue(not a.flags["OWNDATA"])
238 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))
239 self.assertEqual(len(d),20)
240 a[:]=2 # modifying a and d because a and d share the same chunk of data
241 self.assertTrue(d.isUniform(2,1e-14))
242 del d # d is destroyed, a retrieves its ownership of its initial chunk of data
243 self.assertTrue(a.flags["OWNDATA"])
244 a[:]=4 # a can be used has usual
245 self.assertTrue(DataArrayDouble(a).isUniform(4,1e-14))
248 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
250 a=arange(20,dtype=float64)
251 d=DataArrayDouble(a) # d owns data of a
252 e=DataArrayDouble(a) # a not owned -> e only an access to chunk of a
253 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))
254 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))
256 self.assertTrue(d.isUniform(6,1e-14))
257 self.assertTrue(e.isUniform(6,1e-14))
258 del a # a destroyed -> d no change because owned and e array is has no more data set
259 self.assertTrue(d.isUniform(6,1e-14))
260 self.assertTrue(not e.isAllocated())
263 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
265 a=array(0,dtype=float64) ; a.resize(10,2)
268 d=DataArrayDouble(b) # d owns data of a
269 e=DataArrayDouble(b) # a not owned -> e only an access to chunk of a
270 f=DataArrayDouble(b) # a not owned -> e only an access to chunk of a
271 del d # d removed -> a ownes again data
272 self.assertTrue(e.isUniform(0,1e-14))
274 self.assertTrue(e.isUniform(6,1e-14))
275 self.assertTrue(f.isUniform(6,1e-14))
276 self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6])
277 self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]])
279 del b # no impact on e and f because a is the base of a.
280 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))
281 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))
282 del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated
283 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))
284 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))
285 del c # c killed -> a killed -> e and d are put into not allocated state
286 self.assertTrue(not e.isAllocated())
287 self.assertTrue(not f.isAllocated())
290 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
292 a=arange(20,dtype=float64)
293 self.assertTrue(a.flags["OWNDATA"])
294 d=DataArrayDouble(a) # d owns data of a
295 self.assertTrue(not a.flags["OWNDATA"])
296 d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again
297 self.assertTrue(a.flags["OWNDATA"])
298 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))
299 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
302 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
304 d=DataArrayInt.Range(0,20,1)
306 self.assertRaises(InterpKernelException,d.toNumPyArray)# forbidden one or two components of d is accepted
309 self.assertTrue(not a.flags["OWNDATA"])
311 self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])))
312 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])
314 self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])))
318 self.assertTrue(not a.flags["OWNDATA"])
320 self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,200,200,200,200],10,2)))
321 self.assertEqual(a.tolist(),[[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[200,200],[200,200]])
323 self.assertTrue(d.isEqual(DataArrayInt([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,200,200,200,200],10,2)))
326 @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
328 d=DataArrayInt.Range(0,20,1)
329 d=d.convertToDblArr()
331 self.assertRaises(InterpKernelException,d.toNumPyArray)# forbidden one or two components of d is accepted
334 self.assertTrue(not a.flags["OWNDATA"])
336 self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100]),1e-14))
337 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])
339 self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100]),1e-14))
343 self.assertTrue(not a.flags["OWNDATA"])
345 self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,200,200,200,200],10,2),1e-14))
346 self.assertEqual(a.tolist(),[[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[200,200],[200,200]])
348 self.assertTrue(d.isEqual(DataArrayDouble([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,200,200,200,200],10,2),1e-14))
355 #gc.set_debug(gc.DEBUG_LEAK)