Salome HOME
Copyrights update 2015.
[modules/med.git] / src / MEDCoupling_Swig / MEDCouplingNumPyTest.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2015  CEA/DEN, EDF R&D
3 #
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, or (at your option) any later version.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from MEDCoupling import *
22
23 if MEDCouplingHasNumPyBindings():
24     from numpy import *
25     pass
26
27 from platform import architecture
28 from sys import getrefcount
29
30 import os,gc,weakref,unittest
31
32 class MEDCouplingNumPyTest(unittest.TestCase):
33     
34     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
35     def test1(self):
36         sz=20
37         a=array(0,dtype=int32)
38         a.resize(sz)
39         a[:]=4
40         self.assertEqual(getrefcount(a),2)
41         a=a.cumsum(dtype=int32)
42         a=array(a,dtype=int64) ; a=array(a,dtype=int32)
43         self.assertEqual(getrefcount(a),2)
44         d=DataArrayInt(a)
45         d[:]=2
46         #
47         e=DataArrayInt(sz) ; e.fillWithValue(2)
48         self.assertTrue(d.isEqual(e))
49         #
50         a[:]=4 ; e.fillWithValue(4)
51         self.assertTrue(d.isEqual(e))
52         pass
53     
54     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
55     def test2(self):
56         sz=20
57         a=array(0,dtype=int32)
58         a.resize(sz,2)
59         self.assertEqual(getrefcount(a),2)
60         b=a.reshape(2*sz)
61         self.assertEqual(getrefcount(a),3)
62         self.assertEqual(getrefcount(b),2)
63         b[:]=5
64         d=DataArrayInt(b)
65         #
66         e=DataArrayInt(sz*2) ; e.fillWithValue(5)
67         self.assertTrue(d.isEqual(e))
68         pass
69     
70     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
71     def test3(self):
72         sz=10
73         a=array(0,dtype=int32)
74         a.resize(sz,2)
75         b=a.reshape(2*sz)
76         c=a.reshape(2,sz)
77         b[:]=6
78         b[7:17]=7
79         d=DataArrayInt(b)
80         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         #
82         a=zeros((10,2),dtype=int32)
83         b=a.T
84         c=b.view()
85         a.shape=20
86         a[3:]=10.
87         d=DataArrayInt(a)
88         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])))
89         pass
90     
91     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
92     def test4(self):
93         a=zeros(20,dtype=int32)
94         b = a[::-1]
95         self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b is not contiguous in memory
96         pass
97     
98     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
99     def test5(self):
100         a=arange(20,dtype=int32)
101         self.assertEqual(weakref.getweakrefcount(a),0)
102         d=DataArrayInt(a)
103         self.assertEqual(weakref.getweakrefcount(a),1)
104         self.assertTrue(not a.flags["OWNDATA"])
105         self.assertTrue(d.isIdentity())
106         self.assertEqual(len(d),20)
107         a[:]=2 # modifying a and d because a and d share the same chunk of data
108         self.assertTrue(d.isUniform(2))
109         del d # d is destroyed, a retrieves its ownership of its initial chunk of data
110         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called 
111         import gc
112         gc.collect()
113         self.assertTrue(a.flags["OWNDATA"])
114         a[:]=4 # a can be used has usual
115         self.assertTrue(DataArrayInt(a).isUniform(4))
116         pass
117     
118     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
119     def test6(self):
120         a=arange(20,dtype=int32)
121         d=DataArrayInt(a) # d owns data of a
122         e=DataArrayInt(a) # a not owned -> e only an access to chunk of a 
123         self.assertTrue(d.isIdentity())
124         self.assertTrue(e.isIdentity())
125         a[:]=6
126         self.assertTrue(d.isUniform(6))
127         self.assertTrue(e.isUniform(6))
128         del a # a destroyed -> d no change because owned and e array is has no more data set
129         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called 
130         import gc
131         gc.collect()
132         self.assertTrue(d.isUniform(6))
133         self.assertTrue(not e.isAllocated())
134         pass
135
136     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
137     def test7(self):
138         a=array(0,dtype=int32) ; a.resize(10,2)
139         b=a.reshape(20)
140         c=a.reshape(2,10)
141         d=DataArrayInt(b) # d owns data of a
142         e=DataArrayInt(b) # a not owned -> e only an access to chunk of a
143         f=DataArrayInt(b) # a not owned -> e only an access to chunk of a
144         del d # d removed -> a ownes again data
145         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called 
146         import gc
147         gc.collect()
148         self.assertTrue(e.isUniform(0))
149         e[:]=6
150         self.assertTrue(e.isUniform(6))
151         self.assertTrue(f.isUniform(6))
152         self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6])
153         self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]])
154         b[:]=arange(20)
155         del b # no impact on e and f because a is the base of a.
156         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
157         gc.collect()
158         self.assertTrue(f.isIdentity())
159         self.assertTrue(e.isIdentity())
160         del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated
161         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
162         gc.collect()
163         self.assertTrue(f.isIdentity())
164         self.assertTrue(e.isIdentity())
165         del c # c killed -> a killed -> e and d are put into not allocated state
166         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
167         gc.collect()        
168         self.assertTrue(not e.isAllocated())
169         self.assertTrue(not f.isAllocated())
170         pass
171
172     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
173     def test8(self):
174         a=arange(20,dtype=int32)
175         self.assertTrue(a.flags["OWNDATA"])
176         d=DataArrayInt(a) # d owns data of a
177         self.assertTrue(not a.flags["OWNDATA"])
178         d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again
179         self.assertTrue(a.flags["OWNDATA"])
180         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])))
181         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
182         pass
183
184     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
185     def test9(self):
186         sz=20
187         a=array(0,dtype=float64)
188         a.resize(sz)
189         a[:]=4
190         self.assertEqual(getrefcount(a),2)
191         a=a.cumsum(dtype=float64)
192         self.assertEqual(getrefcount(a),2)
193         d=DataArrayDouble(a)
194         d[:]=2
195         #
196         e=DataArrayDouble(sz) ; e.fillWithValue(2)
197         self.assertTrue(d.isEqual(e,1e-14))
198         #
199         a[:]=4 ; e.fillWithValue(4)
200         self.assertTrue(d.isEqual(e,1e-14))
201         pass
202     
203     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
204     def test10(self):
205         sz=20
206         a=array(0,dtype=float64)
207         a.resize(sz,2)
208         self.assertEqual(getrefcount(a),2)
209         b=a.reshape(2*sz)
210         self.assertEqual(getrefcount(a),3)
211         self.assertEqual(getrefcount(b),2)
212         b[:]=5
213         d=DataArrayDouble(b)
214         #
215         e=DataArrayDouble(sz*2) ; e.fillWithValue(5)
216         self.assertTrue(d.isEqual(e,1e-14))
217         pass
218     
219     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
220     def test11(self):
221         sz=10
222         a=array(0,dtype=float64)
223         a.resize(sz,2)
224         b=a.reshape(2*sz)
225         c=a.reshape(2,sz)
226         b[:]=6
227         b[7:17]=7
228         d=DataArrayDouble(b)
229         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))
230         #
231         a=zeros((10,2),dtype=float64)
232         b=a.T
233         c=b.view()
234         a.shape=20
235         a[3:]=10.
236         d=DataArrayDouble(a)
237         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))
238         pass
239     
240     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
241     def test12(self):
242         a=zeros(20,dtype=float64)
243         b = a[::-1]
244         self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b is not contiguous in memory
245         pass
246     
247     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
248     def test13(self):
249         a=arange(20,dtype=float64)
250         self.assertEqual(weakref.getweakrefcount(a),0)
251         d=DataArrayDouble(a)
252         self.assertEqual(weakref.getweakrefcount(a),1)
253         self.assertTrue(not a.flags["OWNDATA"])
254         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))
255         self.assertEqual(len(d),20)
256         a[:]=2 # modifying a and d because a and d share the same chunk of data
257         self.assertTrue(d.isUniform(2,1e-14))
258         del d # d is destroyed, a retrieves its ownership of its initial chunk of data
259         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
260         import gc
261         gc.collect()
262         self.assertTrue(a.flags["OWNDATA"])
263         a[:]=4 # a can be used has usual
264         self.assertTrue(DataArrayDouble(a).isUniform(4,1e-14))
265         pass
266     
267     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
268     def test14(self):
269         a=arange(20,dtype=float64)
270         d=DataArrayDouble(a) # d owns data of a
271         e=DataArrayDouble(a) # a not owned -> e only an access to chunk of a 
272         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))
273         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))
274         a[:]=6
275         self.assertTrue(d.isUniform(6,1e-14))
276         self.assertTrue(e.isUniform(6,1e-14))
277         del a # a destroyed -> d no change because owned and e array is has no more data set
278         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
279         import gc
280         gc.collect()
281         self.assertTrue(d.isUniform(6,1e-14))
282         self.assertTrue(not e.isAllocated())
283         pass
284
285     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
286     def test15(self):
287         a=array(0,dtype=float64) ; a.resize(10,2)
288         b=a.reshape(20)
289         c=a.reshape(2,10)
290         d=DataArrayDouble(b) # d owns data of a
291         e=DataArrayDouble(b) # a not owned -> e only an access to chunk of a
292         f=DataArrayDouble(b) # a not owned -> e only an access to chunk of a
293         del d # d removed -> a ownes again data
294         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
295         import gc
296         gc.collect()
297         self.assertTrue(e.isUniform(0,1e-14))
298         e[:]=6
299         self.assertTrue(e.isUniform(6,1e-14))
300         self.assertTrue(f.isUniform(6,1e-14))
301         self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6])
302         self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]])
303         b[:]=arange(20)
304         del b # no impact on e and f because a is the base of a.
305         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
306         gc.collect()
307         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))
308         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))
309         del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated
310         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
311         gc.collect()
312         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))
313         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))
314         del c # c killed -> a killed -> e and d are put into not allocated state
315         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
316         gc.collect()
317         self.assertTrue(not e.isAllocated())
318         self.assertTrue(not f.isAllocated())
319         pass
320
321     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
322     def test16(self):
323         a=arange(20,dtype=float64)
324         self.assertTrue(a.flags["OWNDATA"])
325         d=DataArrayDouble(a) # d owns data of a
326         self.assertTrue(not a.flags["OWNDATA"])
327         d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again
328         self.assertTrue(a.flags["OWNDATA"])
329         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))
330         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
331         pass
332
333     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
334     def test17(self):
335         d=DataArrayInt.Range(0,20,1)
336         a=d.toNumPyArray()
337         self.assertTrue(not a.flags["OWNDATA"])
338         a[-2:]=100
339         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])))
340         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])
341         del a
342         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
343         import gc
344         gc.collect()
345         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])))
346         #
347         d.rearrange(2)
348         a=d.toNumPyArray()
349         self.assertTrue(not a.flags["OWNDATA"])
350         a[-2:]=200
351         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)))
352         self.assertEqual(a.tolist(),[[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[200,200],[200,200]])
353         del a
354         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
355         gc.collect()
356         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)))
357         pass
358
359     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
360     def test18(self):
361         d=DataArrayInt.Range(0,20,1)
362         d=d.convertToDblArr()
363         a=d.toNumPyArray()
364         self.assertTrue(not a.flags["OWNDATA"])
365         a[-2:]=100
366         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))
367         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,100,100])
368         del a
369         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
370         import gc
371         gc.collect()
372         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))
373         #
374         d.rearrange(2)
375         a=d.toNumPyArray()
376         self.assertTrue(not a.flags["OWNDATA"])
377         a[-2:]=200
378         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))
379         self.assertEqual(a.tolist(),[[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[200,200],[200,200]])
380         del a
381         ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
382         gc.collect()
383         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))
384         pass
385
386     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
387     def test19(self):
388         sz=20
389         a=array(0,dtype=int32)
390         a.resize(sz/2,2)
391         a[:]=4
392         self.assertEqual(getrefcount(a),2)
393         d=DataArrayInt(a)
394         self.assertEqual(10,d.getNumberOfTuples())
395         self.assertEqual(2,d.getNumberOfComponents())
396         self.assertEqual(sz,d.getNbOfElems())
397         self.assertTrue(d.isEqual(DataArrayInt([(4,4),(4,4),(4,4),(4,4),(4,4),(4,4),(4,4),(4,4),(4,4),(4,4)])))
398         a[:]=7
399         self.assertTrue(d.isEqual(DataArrayInt([(7,7),(7,7),(7,7),(7,7),(7,7),(7,7),(7,7),(7,7),(7,7),(7,7)])))
400         #
401         b=a.reshape((2,5,2))
402         self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b has not dimension in [0,1] !
403         pass
404
405     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
406     def test20(self):
407         sz=20
408         a=array(0,dtype=float64)
409         a.resize(sz/2,2)
410         a[:]=4
411         self.assertEqual(getrefcount(a),2)
412         d=DataArrayDouble(a)
413         self.assertEqual(10,d.getNumberOfTuples())
414         self.assertEqual(2,d.getNumberOfComponents())
415         self.assertEqual(sz,d.getNbOfElems())
416         self.assertTrue(d.isEqual(DataArrayDouble([(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.)]),1e-14))
417         a[:]=7
418         self.assertTrue(d.isEqual(DataArrayDouble([(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.)]),1e-14))
419         #
420         b=a.reshape((2,5,2))
421         self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b has not dimension in [0,1] !
422         pass
423
424     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
425     def test21(self):
426         #tests that only DataArray*(npArray) contructor is available
427         a=array(0,dtype=int32)
428         a.resize(20)
429         DataArrayInt(a)
430         self.assertRaises(InterpKernelException,DataArrayInt.New,a,20)
431         self.assertRaises(InterpKernelException,DataArrayInt.New,a,20,1)
432         a=array(0,dtype=float64)
433         a.resize(20)
434         DataArrayDouble(a)
435         self.assertRaises(InterpKernelException,DataArrayDouble.New,a,20)
436         self.assertRaises(InterpKernelException,DataArrayDouble.New,a,20,1)
437         pass
438
439     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
440     def test22(self):
441         d=DataArrayDouble(10)
442         d.iota()
443         a=d.toNumPyArray()
444         self.assertTrue(not a.flags["OWNDATA"])
445         del d
446         gc.collect()
447         self.assertTrue(a.flags["OWNDATA"])
448         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
449         #
450         d=DataArrayInt(10)
451         d.iota()
452         a=d.toNumPyArray()
453         self.assertTrue(not a.flags["OWNDATA"])
454         del d
455         gc.collect()
456         self.assertTrue(a.flags["OWNDATA"])
457         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
458         pass
459
460     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
461     def test23(self):
462         d=DataArrayDouble(10)
463         d.iota()
464         a=d.toNumPyArray()
465         b=d.toNumPyArray()
466         c=d.toNumPyArray()
467         self.assertTrue(not a.flags["OWNDATA"])
468         self.assertTrue(not b.flags["OWNDATA"])
469         self.assertTrue(not c.flags["OWNDATA"])
470         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
471         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
472         self.assertEqual(c.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
473         del d
474         gc.collect()
475         self.assertTrue(a.flags["OWNDATA"])
476         self.assertTrue(not b.flags["OWNDATA"])
477         self.assertTrue(not c.flags["OWNDATA"])
478         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
479         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
480         self.assertEqual(c.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
481         #
482         d=DataArrayInt(10)
483         d.iota()
484         a=d.toNumPyArray()
485         b=d.toNumPyArray()
486         c=d.toNumPyArray()
487         self.assertTrue(not a.flags["OWNDATA"])
488         self.assertTrue(not b.flags["OWNDATA"])
489         self.assertTrue(not c.flags["OWNDATA"])
490         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
491         self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
492         self.assertEqual(c.tolist(),[0,1,2,3,4,5,6,7,8,9])
493         del d
494         gc.collect()
495         self.assertTrue(a.flags["OWNDATA"])
496         self.assertTrue(not b.flags["OWNDATA"])
497         self.assertTrue(not c.flags["OWNDATA"])
498         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
499         self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
500         self.assertEqual(c.tolist(),[0,1,2,3,4,5,6,7,8,9])
501         pass
502
503     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
504     def test24(self):
505         d=DataArrayDouble(10)
506         d.iota()
507         a=d.toNumPyArray()
508         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
509         self.assertTrue(not a.flags["OWNDATA"])
510         self.assertTrue(a.base is None)
511         del a
512         gc.collect()
513         a=d.toNumPyArray()
514         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
515         self.assertTrue(not a.flags["OWNDATA"])
516         self.assertTrue(a.base is None)
517         b=d.toNumPyArray()
518         self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
519         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
520         self.assertTrue(not a.flags["OWNDATA"])
521         self.assertTrue(not b.flags["OWNDATA"])
522         self.assertTrue(b.base is a)
523         del a
524         gc.collect()
525         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
526         self.assertTrue(not b.flags["OWNDATA"])
527         del d
528         gc.collect()
529         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
530         self.assertTrue(not b.flags["OWNDATA"])
531         #
532         d=DataArrayInt(10)
533         d.iota()
534         a=d.toNumPyArray()
535         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
536         self.assertTrue(not a.flags["OWNDATA"])
537         self.assertTrue(a.base is None)
538         del a
539         gc.collect()
540         a=d.toNumPyArray()
541         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
542         self.assertTrue(not a.flags["OWNDATA"])
543         self.assertTrue(a.base is None)
544         b=d.toNumPyArray()
545         self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
546         self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
547         self.assertTrue(not a.flags["OWNDATA"])
548         self.assertTrue(not b.flags["OWNDATA"])
549         self.assertTrue(b.base is a)
550         del a
551         gc.collect()
552         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
553         self.assertTrue(not b.flags["OWNDATA"])
554         del d
555         gc.collect()
556         self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
557         self.assertTrue(not b.flags["OWNDATA"])
558         pass
559     
560     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
561     def test25(self):
562         a=arange(10,dtype=int32)
563         b=DataArrayInt(a)
564         c=DataArrayInt(a)
565         d=DataArrayInt(a)
566         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),10)
567         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),10)
568         self.assertTrue(d.isIdentity()) ; self.assertEqual(len(d),10)
569         c.pushBackSilent(10) # c and a,b are dissociated
570         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),10)
571         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),11)
572         self.assertTrue(d.isIdentity()) ; self.assertEqual(len(d),10)
573         del a
574         gc.collect()
575         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),10)
576         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),11)
577         self.assertTrue(not d.isAllocated())
578         del b
579         gc.collect()
580         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),11)
581         #
582         a=arange(10,dtype=int32)
583         b=DataArrayInt(a)
584         c=DataArrayInt(a)
585         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),10)
586         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),10)
587         b.pushBackSilent(10) # c and a,b are dissociated
588         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),11)
589         self.assertTrue(c.isIdentity()) ; self.assertEqual(len(c),10)
590         del a
591         gc.collect()
592         self.assertTrue(b.isIdentity()) ; self.assertEqual(len(b),11)
593         self.assertTrue(not c.isAllocated())
594         del b
595         gc.collect()
596         self.assertTrue(not c.isAllocated())
597         #
598         a=float64(arange(5,dtype=int32))
599         b=DataArrayDouble(a)
600         c=DataArrayDouble(a)
601         d=DataArrayDouble(a)
602         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
603         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
604         self.assertTrue(d.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
605         c.pushBackSilent(10.) # c and a,b are dissociated
606         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
607         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
608         self.assertTrue(d.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
609         del a
610         gc.collect()
611         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
612         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
613         self.assertTrue(not d.isAllocated())
614         del b
615         gc.collect()
616         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
617         #
618         a=float64(arange(5,dtype=int32))
619         b=DataArrayDouble(a)
620         c=DataArrayDouble(a)
621         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
622         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
623         b.pushBackSilent(10.) # c and a,b are dissociated
624         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
625         self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.]),1e-12))
626         del a
627         gc.collect()
628         self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
629         self.assertTrue(not c.isAllocated())
630         del b
631         gc.collect()
632         self.assertTrue(not c.isAllocated())
633         pass
634
635     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
636     def test26(self):
637         d=DataArrayInt(15) ; d.iota()
638         d.rearrange(3)
639         a=d.toNumPyArray()
640         self.assertEqual(a.ndim,2)
641         self.assertEqual(a.size,15)
642         self.assertEqual(a.shape,(5,3))
643         self.assertEqual(a.strides,(12,4))
644         self.assertEqual(a.nbytes,60)
645         self.assertEqual(a.itemsize,4)
646         self.assertEqual(a.tolist(),[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14]])
647         #
648         d2=d.convertToDblArr()
649         a2=d2.toNumPyArray()
650         self.assertEqual(a2.ndim,2)
651         self.assertEqual(a2.size,15)
652         self.assertEqual(a2.shape,(5,3))
653         self.assertEqual(a2.strides,(24,8))
654         self.assertEqual(a2.nbytes,120)
655         self.assertEqual(a2.itemsize,8)
656         self.assertEqual(a2.tolist(),[[0.,1.,2.],[3.,4.,5.],[6.,7.,8.],[9.,10.,11.],[12.,13.,14.]])
657         pass
658
659     @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
660     def test27(self):
661         m0=DenseMatrix(DataArrayDouble([2,3,4,5,1,6]),2,3)
662         m0np=m0.toNumPyMatrix()
663         self.assertEqual(m0np.shape,(2,3))
664         self.assertEqual(m0np.tolist(),[[2.0,3.0,4.0],[5.0,1.0,6.0]])
665         pass
666
667     def setUp(self):
668         pass
669     pass
670
671 #gc.set_debug(gc.DEBUG_LEAK)
672 unittest.main()