Salome HOME
Merge from MrgToV7main1804
[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 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         self.assertEqual(getrefcount(a),2)
43         d=DataArrayInt(a)
44         d[:]=2
45         #
46         e=DataArrayInt(sz) ; e.fillWithValue(2)
47         self.assertTrue(d.isEqual(e))
48         #
49         a[:]=4 ; e.fillWithValue(4)
50         self.assertTrue(d.isEqual(e))
51         pass
52     
53     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
54     def test2(self):
55         sz=20
56         a=array(0,dtype=int32)
57         a.resize(sz,2)
58         self.assertEqual(getrefcount(a),2)
59         b=a.reshape(2*sz)
60         self.assertEqual(getrefcount(a),3)
61         self.assertEqual(getrefcount(b),2)
62         b[:]=5
63         d=DataArrayInt(b)
64         #
65         e=DataArrayInt(sz*2) ; e.fillWithValue(5)
66         self.assertTrue(d.isEqual(e))
67         pass
68     
69     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
70     def test3(self):
71         sz=10
72         a=array(0,dtype=int32)
73         a.resize(sz,2)
74         b=a.reshape(2*sz)
75         c=a.reshape(2,sz)
76         b[:]=6
77         b[7:17]=7
78         d=DataArrayInt(b)
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])))
80         #
81         a=zeros((10,2),dtype=int32)
82         b=a.T
83         c=b.view()
84         a.shape=20
85         a[3:]=10.
86         d=DataArrayInt(a)
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])))
88         pass
89     
90     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
91     def test4(self):
92         a=zeros(20,dtype=int32)
93         b = a[::-1]
94         self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b is not contiguous in memory
95         pass
96     
97     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
98     def test5(self):
99         a=arange(20,dtype=int32)
100         self.assertEqual(weakref.getweakrefcount(a),0)
101         d=DataArrayInt(a)
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))
112         pass
113     
114     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
115     def test6(self):
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())
121         a[:]=6
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())
127         pass
128
129     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
130     def test7(self):
131         a=array(0,dtype=int32) ; a.resize(10,2)
132         b=a.reshape(20)
133         c=a.reshape(2,10)
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))
139         e[:]=6
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]])
144         b[:]=arange(20)
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())
154         pass
155
156     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
157     def test8(self):
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])
166         pass
167
168     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
169     def test9(self):
170         sz=20
171         a=array(0,dtype=float64)
172         a.resize(sz)
173         a[:]=4
174         self.assertEqual(getrefcount(a),2)
175         a=a.cumsum(dtype=float64)
176         self.assertEqual(getrefcount(a),2)
177         d=DataArrayDouble(a)
178         d[:]=2
179         #
180         e=DataArrayDouble(sz) ; e.fillWithValue(2)
181         self.assertTrue(d.isEqual(e,1e-14))
182         #
183         a[:]=4 ; e.fillWithValue(4)
184         self.assertTrue(d.isEqual(e,1e-14))
185         pass
186     
187     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
188     def test10(self):
189         sz=20
190         a=array(0,dtype=float64)
191         a.resize(sz,2)
192         self.assertEqual(getrefcount(a),2)
193         b=a.reshape(2*sz)
194         self.assertEqual(getrefcount(a),3)
195         self.assertEqual(getrefcount(b),2)
196         b[:]=5
197         d=DataArrayDouble(b)
198         #
199         e=DataArrayDouble(sz*2) ; e.fillWithValue(5)
200         self.assertTrue(d.isEqual(e,1e-14))
201         pass
202     
203     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
204     def test11(self):
205         sz=10
206         a=array(0,dtype=float64)
207         a.resize(sz,2)
208         b=a.reshape(2*sz)
209         c=a.reshape(2,sz)
210         b[:]=6
211         b[7:17]=7
212         d=DataArrayDouble(b)
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))
214         #
215         a=zeros((10,2),dtype=float64)
216         b=a.T
217         c=b.view()
218         a.shape=20
219         a[3:]=10.
220         d=DataArrayDouble(a)
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))
222         pass
223     
224     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
225     def test12(self):
226         a=zeros(20,dtype=float64)
227         b = a[::-1]
228         self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b is not contiguous in memory
229         pass
230     
231     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
232     def test13(self):
233         a=arange(20,dtype=float64)
234         self.assertEqual(weakref.getweakrefcount(a),0)
235         d=DataArrayDouble(a)
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))
246         pass
247     
248     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
249     def test14(self):
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))
255         a[:]=6
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())
261         pass
262
263     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
264     def test15(self):
265         a=array(0,dtype=float64) ; a.resize(10,2)
266         b=a.reshape(20)
267         c=a.reshape(2,10)
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))
273         e[:]=6
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]])
278         b[:]=arange(20)
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())
288         pass
289
290     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
291     def test16(self):
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])
300         pass
301
302     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
303     def test17(self):
304         d=DataArrayInt.Range(0,20,1)
305         d.rearrange(10)
306         self.assertRaises(InterpKernelException,d.toNumPyArray)# forbidden one or two components of d is accepted
307         d.rearrange(1)
308         a=d.toNumPyArray()
309         self.assertTrue(not a.flags["OWNDATA"])
310         a[-2:]=100
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])
313         del a
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])))
315         #
316         d.rearrange(2)
317         a=d.toNumPyArray()
318         self.assertTrue(not a.flags["OWNDATA"])
319         a[-2:]=200
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]])
322         del a
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)))
324         pass
325
326     @unittest.skipUnless(MEDCouplingHasNumpyBindings(),"requires numpy")
327     def test18(self):
328         d=DataArrayInt.Range(0,20,1)
329         d=d.convertToDblArr()
330         d.rearrange(10)
331         self.assertRaises(InterpKernelException,d.toNumPyArray)# forbidden one or two components of d is accepted
332         d.rearrange(1)
333         a=d.toNumPyArray()
334         self.assertTrue(not a.flags["OWNDATA"])
335         a[-2:]=100
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])
338         del a
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))
340         #
341         d.rearrange(2)
342         a=d.toNumPyArray()
343         self.assertTrue(not a.flags["OWNDATA"])
344         a[-2:]=200
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]])
347         del a
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))
349         pass
350
351     def setUp(self):
352         pass
353     pass
354
355 #gc.set_debug(gc.DEBUG_LEAK)
356 unittest.main()