1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from MEDCoupling import *
23 if MEDCouplingHasNumPyBindings():
27 from platform import architecture
28 from sys import getrefcount
30 import os,gc,weakref,unittest
32 class MEDCouplingNumPyTest(unittest.TestCase):
34 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
37 a=array(0,dtype=int32)
40 self.assertEqual(getrefcount(a),2)
41 a=a.cumsum(dtype=int32)
42 a=array(a,dtype=int64) ; a=array(a,dtype=int32)
43 self.assertEqual(getrefcount(a),2)
47 e=DataArrayInt(sz) ; e.fillWithValue(2)
48 self.assertTrue(d.isEqual(e))
50 a[:]=4 ; e.fillWithValue(4)
51 self.assertTrue(d.isEqual(e))
54 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
57 a=array(0,dtype=int32)
59 self.assertEqual(getrefcount(a),2)
61 self.assertEqual(getrefcount(a),3)
62 self.assertEqual(getrefcount(b),2)
66 e=DataArrayInt(sz*2) ; e.fillWithValue(5)
67 self.assertTrue(d.isEqual(e))
70 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
73 a=array(0,dtype=int32)
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])))
82 a=zeros((10,2),dtype=int32)
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])))
91 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
93 a=zeros(20,dtype=int32)
95 self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b is not contiguous in memory
98 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
100 a=arange(20,dtype=int32)
101 self.assertEqual(weakref.getweakrefcount(a),0)
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
112 self.assertTrue(a.flags["OWNDATA"])
113 a[:]=4 # a can be used has usual
114 self.assertTrue(DataArrayInt(a).isUniform(4))
117 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
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()))
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
131 self.assertTrue(d.isUniform(6))
132 self.assertTrue(not e.isAllocated())
135 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
137 a=array(0,dtype=int32) ; a.resize(10,2)
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
147 self.assertTrue(e.isUniform(0))
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]])
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
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
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
167 self.assertTrue(not e.isAllocated())
168 self.assertTrue(not f.isAllocated())
171 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
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])
183 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
186 a=array(0,dtype=float64)
189 self.assertEqual(getrefcount(a),2)
190 a=a.cumsum(dtype=float64)
191 self.assertEqual(getrefcount(a),2)
195 e=DataArrayDouble(sz) ; e.fillWithValue(2)
196 self.assertTrue(d.isEqual(e,1e-14))
198 a[:]=4 ; e.fillWithValue(4)
199 self.assertTrue(d.isEqual(e,1e-14))
202 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
205 a=array(0,dtype=float64)
207 self.assertEqual(getrefcount(a),2)
209 self.assertEqual(getrefcount(a),3)
210 self.assertEqual(getrefcount(b),2)
214 e=DataArrayDouble(sz*2) ; e.fillWithValue(5)
215 self.assertTrue(d.isEqual(e,1e-14))
218 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
221 a=array(0,dtype=float64)
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))
230 a=zeros((10,2),dtype=float64)
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))
239 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
241 a=zeros(20,dtype=float64)
243 self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b is not contiguous in memory
246 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
248 a=arange(20,dtype=float64)
249 self.assertEqual(weakref.getweakrefcount(a),0)
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
261 self.assertTrue(a.flags["OWNDATA"])
262 a[:]=4 # a can be used has usual
263 self.assertTrue(DataArrayDouble(a).isUniform(4,1e-14))
266 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
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))
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
280 self.assertTrue(d.isUniform(6,1e-14))
281 self.assertTrue(not e.isAllocated())
284 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
286 a=array(0,dtype=float64) ; a.resize(10,2)
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
296 self.assertTrue(e.isUniform(0,1e-14))
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]])
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
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
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
316 self.assertTrue(not e.isAllocated())
317 self.assertTrue(not f.isAllocated())
320 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
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])
332 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
334 d=DataArrayInt.Range(0,20,1)
336 self.assertTrue(not a.flags["OWNDATA"])
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])
341 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
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])))
348 self.assertTrue(not a.flags["OWNDATA"])
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]])
353 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
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)))
358 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
360 d=DataArrayInt.Range(0,20,1)
361 d=d.convertToDblArr()
363 self.assertTrue(not a.flags["OWNDATA"])
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])
368 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
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))
375 self.assertTrue(not a.flags["OWNDATA"])
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]])
380 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
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))
385 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
388 a=array(0,dtype=int32)
391 self.assertEqual(getrefcount(a),2)
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)])))
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)])))
401 self.assertRaises(InterpKernelException,DataArrayInt.New,b) # b has not dimension in [0,1] !
404 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
407 a=array(0,dtype=float64)
410 self.assertEqual(getrefcount(a),2)
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))
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))
420 self.assertRaises(InterpKernelException,DataArrayDouble.New,b) # b has not dimension in [0,1] !
423 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
425 #tests that only DataArray*(npArray) contructor is available
426 a=array(0,dtype=int32)
429 self.assertRaises(InterpKernelException,DataArrayInt.New,a,20)
430 self.assertRaises(InterpKernelException,DataArrayInt.New,a,20,1)
431 a=array(0,dtype=float64)
434 self.assertRaises(InterpKernelException,DataArrayDouble.New,a,20)
435 self.assertRaises(InterpKernelException,DataArrayDouble.New,a,20,1)
438 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
440 d=DataArrayDouble(10)
443 self.assertTrue(not a.flags["OWNDATA"])
446 self.assertTrue(a.flags["OWNDATA"])
447 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
452 self.assertTrue(not a.flags["OWNDATA"])
455 self.assertTrue(a.flags["OWNDATA"])
456 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
459 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
461 d=DataArrayDouble(10)
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.])
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.])
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])
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])
502 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
504 d=DataArrayDouble(10)
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)
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)
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)
524 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
525 self.assertTrue(not b.flags["OWNDATA"])
528 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
529 self.assertTrue(not b.flags["OWNDATA"])
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)
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)
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)
551 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
552 self.assertTrue(not b.flags["OWNDATA"])
555 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
556 self.assertTrue(not b.flags["OWNDATA"])
559 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
561 a=arange(10,dtype=int32)
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))
574 self.assertTrue(b.isIota(10))
575 self.assertTrue(c.isIota(11))
576 self.assertTrue(not d.isAllocated())
579 self.assertTrue(c.isIota(11))
581 a=arange(10,dtype=int32)
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))
591 self.assertTrue(b.isIota(11))
592 self.assertTrue(not c.isAllocated())
595 self.assertTrue(not c.isAllocated())
597 a=float64(arange(5,dtype=int32))
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))
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())
615 self.assertTrue(c.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
617 a=float64(arange(5,dtype=int32))
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))
627 self.assertTrue(b.isEqual(DataArrayDouble([0.,1.,2.,3.,4.,10.]),1e-12))
628 self.assertTrue(not c.isAllocated())
631 self.assertTrue(not c.isAllocated())
634 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
636 d=DataArrayInt(15) ; d.iota()
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]])
647 d2=d.convertToDblArr()
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.]])
658 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
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]])
666 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
668 """Test on DataArrayBytes"""
671 for i in range(len(d)):
675 for i in range(len(d)):
676 self.assertEqual(int(arr[i]),-128+i)
679 self.assertEqual(int(arr[0]),7)
681 self.assertEqual(int(d.getIJ(0,0)),8)
688 for i in range(len(d)):
692 for i in range(len(d)):
693 self.assertEqual(int(arr[i]),-128+i)
701 for i in range(len(d)):
704 arr1=d.toNumPyArray()
705 arr2=d.toNumPyArray()
706 arr3=d.toNumPyArray()
708 self.assertEqual(int(arr1[0]),10) ; self.assertEqual(int(arr2[0]),10) ; self.assertEqual(int(arr3[0]),10)
709 arr2[0]=15 ; self.assertEqual(int(d.getIJ(0,0)),15) ; self.assertEqual(int(arr1[0]),15) ; self.assertEqual(int(arr3[0]),15)
711 for i in range(len(d)):
712 self.assertEqual(int(arr1[i]),-128+i)
713 self.assertEqual(int(arr2[i]),-128+i)
714 self.assertEqual(int(arr3[i]),-128+i)
718 for i in range(len(d)):
719 self.assertEqual(int(arr1[i]),-128+i)
720 self.assertEqual(int(arr3[i]),-128+i)
724 for i in range(len(d)):
725 self.assertEqual(int(arr3[i]),-128+i)
730 arr=array(0,dtype=int8)
737 self.assertEqual(int(d.getIJ(i,0)),-128+i)
744 arr=array(0,dtype=int8)
751 self.assertEqual(int(d.getIJ(i,0)),-128+i)
759 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
761 """Same as test9 with float32"""
763 a=array(0,dtype=float32)
766 self.assertEqual(getrefcount(a),2)
767 a=a.cumsum(dtype=float32)
768 self.assertEqual(getrefcount(a),2)
772 e=DataArrayFloat(sz) ; e.fillWithValue(2)
773 self.assertTrue(d.isEqual(e,1e-7))
775 a[:]=4 ; e.fillWithValue(4)
776 self.assertTrue(d.isEqual(e,1e-7))
779 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
781 """Same as test10 with float32"""
783 a=array(0,dtype=float32)
785 self.assertEqual(getrefcount(a),2)
787 self.assertEqual(getrefcount(a),3)
788 self.assertEqual(getrefcount(b),2)
792 e=DataArrayFloat(sz*2) ; e.fillWithValue(5)
793 self.assertTrue(d.isEqual(e,1e-7))
796 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
798 """Same as test11 with float32"""
800 a=array(0,dtype=float32)
807 self.assertTrue(d.isEqual(DataArrayFloat([6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,6,6,6]),1e-7))
809 a=zeros((10,2),dtype=float32)
815 self.assertTrue(d.isEqual(DataArrayFloat([0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]),1e-7))
818 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
820 """Same as test12 with float32"""
821 a=zeros(20,dtype=float32)
823 self.assertRaises(InterpKernelException,DataArrayFloat.New,b) # b is not contiguous in memory
826 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
828 """Same as test13 with float32"""
829 a=arange(20,dtype=float32)
830 self.assertEqual(weakref.getweakrefcount(a),0)
832 self.assertEqual(weakref.getweakrefcount(a),1)
833 self.assertTrue(not a.flags["OWNDATA"])
834 self.assertTrue(d.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
835 self.assertEqual(len(d),20)
836 a[:]=2 # modifying a and d because a and d share the same chunk of data
837 self.assertTrue(d.isUniform(2,1e-7))
838 del d # d is destroyed, a retrieves its ownership of its initial chunk of data
839 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
842 self.assertTrue(a.flags["OWNDATA"])
843 a[:]=4 # a can be used has usual
844 self.assertTrue(DataArrayFloat(a).isUniform(4,1e-7))
847 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
849 """Same as test14 with float32"""
850 a=arange(20,dtype=float32)
851 d=DataArrayFloat(a) # d owns data of a
852 e=DataArrayFloat(a) # a not owned -> e only an access to chunk of a
853 self.assertTrue(d.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
854 self.assertTrue(e.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
856 self.assertTrue(d.isUniform(6,1e-7))
857 self.assertTrue(e.isUniform(6,1e-7))
858 del a # a destroyed -> d no change because owned and e array is has no more data set
859 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
862 self.assertTrue(d.isUniform(6,1e-7))
863 self.assertTrue(not e.isAllocated())
866 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
868 """Same as test15 with float32"""
869 a=array(0,dtype=float32) ; a.resize(10,2)
872 d=DataArrayFloat(b) # d owns data of a
873 e=DataArrayFloat(b) # a not owned -> e only an access to chunk of a
874 f=DataArrayFloat(b) # a not owned -> e only an access to chunk of a
875 del d # d removed -> a ownes again data
876 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
879 self.assertTrue(e.isUniform(0,1e-7))
881 self.assertTrue(e.isUniform(6,1e-7))
882 self.assertTrue(f.isUniform(6,1e-7))
883 self.assertEqual(b.tolist(),[6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6])
884 self.assertEqual(a.tolist(),[[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6],[6,6]])
886 del b # no impact on e and f because a is the base of a.
887 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
889 self.assertTrue(f.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
890 self.assertTrue(e.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
891 del a # a destroyed, but as c has its base set to a, a exists -> e and f not allocated
892 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
894 self.assertTrue(f.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
895 self.assertTrue(e.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]),1e-7))
896 del c # c killed -> a killed -> e and d are put into not allocated state
897 ##@@ Ensure a pass of the garbage collector so that the de-allocator of d is called
899 self.assertTrue(not e.isAllocated())
900 self.assertTrue(not f.isAllocated())
903 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
905 """Same as test16 with float32"""
906 a=arange(20,dtype=float32)
907 self.assertTrue(a.flags["OWNDATA"])
908 d=DataArrayFloat(a) # d owns data of a
909 self.assertTrue(not a.flags["OWNDATA"])
910 d.pushBackSilent(20)# d pushBack so release of chunk of data -> a becomes owner of its data again
911 self.assertTrue(a.flags["OWNDATA"])
912 self.assertTrue(d.isEqual(DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),1e-7))
913 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
916 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
918 """Same as test20 with float32"""
920 a=array(0,dtype=float32)
923 self.assertEqual(getrefcount(a),2)
925 self.assertEqual(10,d.getNumberOfTuples())
926 self.assertEqual(2,d.getNumberOfComponents())
927 self.assertEqual(sz,d.getNbOfElems())
928 self.assertTrue(d.isEqual(DataArrayFloat([(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.),(4.,4.)]),1e-7))
930 self.assertTrue(d.isEqual(DataArrayFloat([(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.),(7.,7.)]),1e-7))
933 self.assertRaises(InterpKernelException,DataArrayFloat.New,b) # b has not dimension in [0,1] !
936 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
938 """Same as test22 with float32"""
942 self.assertTrue(not a.flags["OWNDATA"])
945 self.assertTrue(a.flags["OWNDATA"])
946 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
951 self.assertTrue(not a.flags["OWNDATA"])
954 self.assertTrue(a.flags["OWNDATA"])
955 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
958 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
960 """Same as test23 with float32"""
966 self.assertTrue(not a.flags["OWNDATA"])
967 self.assertTrue(not b.flags["OWNDATA"])
968 self.assertTrue(not c.flags["OWNDATA"])
969 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
970 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
971 self.assertEqual(c.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
974 self.assertTrue(a.flags["OWNDATA"])
975 self.assertTrue(not b.flags["OWNDATA"])
976 self.assertTrue(not c.flags["OWNDATA"])
977 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
978 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
979 self.assertEqual(c.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
986 self.assertTrue(not a.flags["OWNDATA"])
987 self.assertTrue(not b.flags["OWNDATA"])
988 self.assertTrue(not c.flags["OWNDATA"])
989 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
990 self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
991 self.assertEqual(c.tolist(),[0,1,2,3,4,5,6,7,8,9])
994 self.assertTrue(a.flags["OWNDATA"])
995 self.assertTrue(not b.flags["OWNDATA"])
996 self.assertTrue(not c.flags["OWNDATA"])
997 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
998 self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
999 self.assertEqual(c.tolist(),[0,1,2,3,4,5,6,7,8,9])
1002 @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
1004 """Same as test24 with float32"""
1005 d=DataArrayFloat(10)
1008 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1009 self.assertTrue(not a.flags["OWNDATA"])
1010 self.assertTrue(a.base is None)
1014 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1015 self.assertTrue(not a.flags["OWNDATA"])
1016 self.assertTrue(a.base is None)
1018 self.assertEqual(a.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1019 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1020 self.assertTrue(not a.flags["OWNDATA"])
1021 self.assertTrue(not b.flags["OWNDATA"])
1022 self.assertTrue(b.base is a)
1025 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1026 self.assertTrue(not b.flags["OWNDATA"])
1029 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1030 self.assertTrue(not b.flags["OWNDATA"])
1035 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
1036 self.assertTrue(not a.flags["OWNDATA"])
1037 self.assertTrue(a.base is None)
1041 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
1042 self.assertTrue(not a.flags["OWNDATA"])
1043 self.assertTrue(a.base is None)
1045 self.assertEqual(a.tolist(),[0,1,2,3,4,5,6,7,8,9])
1046 self.assertEqual(b.tolist(),[0,1,2,3,4,5,6,7,8,9])
1047 self.assertTrue(not a.flags["OWNDATA"])
1048 self.assertTrue(not b.flags["OWNDATA"])
1049 self.assertTrue(b.base is a)
1052 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1053 self.assertTrue(not b.flags["OWNDATA"])
1056 self.assertEqual(b.tolist(),[0.,1.,2.,3.,4.,5.,6.,7.,8.,9.])
1057 self.assertTrue(not b.flags["OWNDATA"])
1064 #gc.set_debug(gc.DEBUG_LEAK)