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