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