~ubuntu-branches/ubuntu/natty/pytables/natty-updates

« back to all changes in this revision

Viewing changes to tables/tests/test_carray.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2006-06-28 10:45:03 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20060628104503-cc251q5o5j3e2k10
  * Fixed call to pyversions in debian/rules which failed on recent versions 
    of pyversions
  * Fixed clean rule in debian/rules which left the stamp files behind
  * Acknowledge NMU
  * Added Alexandre Fayolle to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Eh! python!, We are going to include isolatin characters here
 
2
# -*- coding: latin-1 -*-
 
3
 
 
4
import sys
 
5
import unittest
 
6
import os
 
7
import tempfile
 
8
 
 
9
from numarray import *
 
10
from numarray import strings
 
11
from tables import *
 
12
from tables.utils import convertNAToNumeric, convertNAToNumPy
 
13
 
 
14
try:
 
15
    import Numeric
 
16
    numeric = 1
 
17
except:
 
18
    numeric = 0
 
19
 
 
20
try:
 
21
    import numpy
 
22
    numpy_imported = 1
 
23
except:
 
24
    numpy_imported = 0
 
25
 
 
26
from common import verbose, allequal, cleanup, heavy
 
27
# To delete the internal attributes automagically
 
28
unittest.TestCase.tearDown = cleanup
 
29
 
 
30
class BasicTestCase(unittest.TestCase):
 
31
    # Default values
 
32
    flavor = "numarray"
 
33
    type = Int32
 
34
    shape = (2,2)
 
35
    start = 0
 
36
    stop = 10
 
37
    step = 1
 
38
    length = 1
 
39
    chunksize = (5,5)
 
40
    compress = 0
 
41
    complib = "zlib"  # Default compression library
 
42
    shuffle = 0
 
43
    fletcher32 = 0
 
44
    reopen = 1  # Tells whether the file has to be reopened on each test or not
 
45
 
 
46
    def setUp(self):
 
47
 
 
48
        # Create an instance of an HDF5 Table
 
49
        self.file = tempfile.mktemp(".h5")
 
50
        self.fileh = openFile(self.file, "w")
 
51
        self.rootgroup = self.fileh.root
 
52
        self.populateFile()
 
53
        if self.reopen:
 
54
            # Close the file
 
55
            self.fileh.close()
 
56
 
 
57
    def populateFile(self):
 
58
        group = self.rootgroup
 
59
        if self.flavor == "numarray":
 
60
            if str(self.type) == "CharType":
 
61
                flavor = "numarray"
 
62
            else:
 
63
                flavor = "numarray"
 
64
        elif self.flavor == "numpy":
 
65
            flavor = "numpy"
 
66
        else:
 
67
            flavor = "numeric"
 
68
        if self.type == "CharType":
 
69
            atom = StringAtom(shape=self.chunksize, length=self.length,
 
70
                              flavor=flavor)
 
71
        else:
 
72
            atom = Atom(dtype=self.type, shape=self.chunksize, flavor=flavor)
 
73
        title = self.__class__.__name__
 
74
        filters = Filters(complevel = self.compress,
 
75
                          complib = self.complib,
 
76
                          shuffle = self.shuffle,
 
77
                          fletcher32 = self.fletcher32)
 
78
        carray = self.fileh.createCArray(group, 'carray1', self.shape, atom,
 
79
                                         title, filters = filters)
 
80
 
 
81
        # Fill it with data
 
82
        self.rowshape = list(carray.shape)
 
83
        self.objsize = self.length*product(self.shape)
 
84
        if self.flavor == "numarray":
 
85
            if str(self.type) == "CharType":
 
86
                object = strings.array("a"*self.objsize, shape=self.shape,
 
87
                                       itemsize=carray.itemsize)
 
88
            else:
 
89
                object = arange(self.objsize, shape=self.shape,
 
90
                                type=carray.type)
 
91
        elif self.flavor == "numpy":
 
92
            object = numpy.arange(self.objsize,
 
93
                                  dtype=numpy.typeNA[carray.type])
 
94
            object = numpy.reshape(object, self.shape)
 
95
        else:  # Numeric flavor
 
96
            object = Numeric.arange(self.objsize,
 
97
                                    typecode=typecode[carray.type])
 
98
            object = Numeric.reshape(object, self.shape)
 
99
        if verbose:
 
100
            if self.flavor == "numarray":
 
101
                print "Object to append -->", object.info()
 
102
            else:
 
103
                print "Object to append -->", repr(object)
 
104
 
 
105
        if str(self.type) == "CharType":
 
106
            carray[...] = object
 
107
        elif self.flavor == "numarray":
 
108
            carray[...] = object
 
109
        else:
 
110
            # For Numeric arrays, we still have to undo the type upgrade
 
111
            #carray[...] = object.astype(typecode[carray.type])
 
112
            carray[...] = object
 
113
 
 
114
 
 
115
    def tearDown(self):
 
116
        self.fileh.close()
 
117
        os.remove(self.file)
 
118
        cleanup(self)
 
119
 
 
120
    #----------------------------------------
 
121
 
 
122
    def test01_readCArray(self):
 
123
        """Checking read() of chunked layout arrays"""
 
124
 
 
125
        rootgroup = self.rootgroup
 
126
        if verbose:
 
127
            print '\n', '-=' * 30
 
128
            print "Running %s.test01_readCArray..." % self.__class__.__name__
 
129
 
 
130
        # Create an instance of an HDF5 Table
 
131
        if self.reopen:
 
132
            self.fileh = openFile(self.file, "r")
 
133
        carray = self.fileh.getNode("/carray1")
 
134
 
 
135
        # Choose a small value for buffer size
 
136
        carray._v_maxTuples = 3
 
137
        if verbose:
 
138
            print "CArray descr:", repr(carray)
 
139
            print "shape of read array ==>", carray.shape
 
140
            print "reopening?:", self.reopen
 
141
 
 
142
        # Build the array to do comparisons
 
143
        if self.flavor == "numarray":
 
144
            if str(self.type) == "CharType":
 
145
                object_ = strings.array("a"*self.objsize, shape=self.shape,
 
146
                                        itemsize=carray.itemsize)
 
147
            else:
 
148
                object_ = arange(self.objsize, shape=self.shape,
 
149
                                 type=carray.type)
 
150
        elif self.flavor == "numpy":
 
151
            object_ = numpy.arange(self.objsize,
 
152
                                  dtype=numpy.typeNA[carray.type])
 
153
            object_ = numpy.reshape(object_, self.shape)
 
154
        else:
 
155
            object_ = Numeric.arange(self.objsize,
 
156
                                     typecode=typecode[carray.type])
 
157
            object_ = Numeric.reshape(object_, self.shape)
 
158
 
 
159
        stop = self.stop
 
160
        # stop == None means read only the element designed by start
 
161
        # (in read() contexts)
 
162
        if self.stop == None:
 
163
            if self.start == -1:  # corner case
 
164
                stop = carray.nrows
 
165
            else:
 
166
                stop = self.start + 1
 
167
        # Protection against number of elements less than existing
 
168
        #if rowshape[self.extdim] < self.stop or self.stop == 0:
 
169
        if carray.nrows < stop:
 
170
            # self.stop == 0 means last row only in read()
 
171
            # and not in [::] slicing notation
 
172
            stop = carray.nrows
 
173
        # do a copy() in order to ensure that len(object._data)
 
174
        # actually do a measure of its length
 
175
        # Numeric 23.8 will issue an error with slices like -1:20:20
 
176
        # but this is an error with this Numeric version (and perhaps
 
177
        # lower ones).
 
178
        object = object_[self.start:stop:self.step].copy()
 
179
 
 
180
        # Read all the array
 
181
        try:
 
182
            data = carray.read(self.start,stop,self.step)
 
183
        except IndexError:
 
184
            if self.flavor == "numarray":
 
185
                data = array(None, shape = self.shape, type=self.type)
 
186
            elif self.flavor == "numpy":
 
187
                data = numpy.empty(self.shape, numpy.typeNA[self.type])
 
188
            else:
 
189
                data = Numeric.zeros(self.shape, typecode[self.type])
 
190
 
 
191
        if verbose:
 
192
            if hasattr(object, "shape"):
 
193
                print "shape should look as:", object.shape
 
194
            print "Object read ==>", repr(data)
 
195
            print "Should look like ==>", repr(object)
 
196
 
 
197
        if hasattr(data, "shape"):
 
198
            assert len(data.shape) == len(self.shape)
 
199
        else:
 
200
            # Scalar case
 
201
            assert len(self.shape) == 1
 
202
        assert carray._v_chunksize == self.chunksize
 
203
        assert allequal(data, object, self.flavor)
 
204
 
 
205
    def test02_getitemCArray(self):
 
206
        """Checking chunked layout array __getitem__ special method"""
 
207
 
 
208
        rootgroup = self.rootgroup
 
209
        if verbose:
 
210
            print '\n', '-=' * 30
 
211
            print "Running %s.test02_getitemCArray..." % self.__class__.__name__
 
212
 
 
213
        if not hasattr(self, "slices"):
 
214
            # If there is not a slices attribute, create it
 
215
            self.slices = (slice(self.start, self.stop, self.step),)
 
216
 
 
217
        # Create an instance of an HDF5 Table
 
218
        if self.reopen:
 
219
            self.fileh = openFile(self.file, "r")
 
220
        carray = self.fileh.getNode("/carray1")
 
221
 
 
222
        # Choose a small value for buffer size
 
223
        #carray._v_maxTuples = 3   # this does not really changes the chunksize
 
224
        if verbose:
 
225
            print "CArray descr:", repr(carray)
 
226
            print "shape of read array ==>", carray.shape
 
227
            print "reopening?:", self.reopen
 
228
 
 
229
        # Build the array to do comparisons
 
230
        if str(self.type) == "CharType":
 
231
            object_ = strings.array("a"*self.objsize, shape=self.shape,
 
232
                                    itemsize=carray.itemsize)
 
233
        else:
 
234
            object_ = arange(self.objsize, shape=self.shape,
 
235
                             type=carray.type)
 
236
 
 
237
        stop = self.stop
 
238
        # do a copy() in order to ensure that len(object._data)
 
239
        # actually do a measure of its length
 
240
        object = object_.__getitem__(self.slices).copy()
 
241
 
 
242
        if self.flavor == "numpy":
 
243
            # Convert the object to Numeric
 
244
            object = convertNAToNumPy(object)
 
245
        elif self.flavor == "numeric":
 
246
            # Convert the object to Numeric
 
247
            object = convertNAToNumeric(object)
 
248
 
 
249
        # Read data from the array
 
250
        try:
 
251
            data = carray.__getitem__(self.slices)
 
252
        except IndexError:
 
253
            print "IndexError!"
 
254
            if self.flavor == "numarray":
 
255
                data = array(None, shape = self.shape, type=self.type)
 
256
            elif self.flavor == "numpy":
 
257
                data = numpy.empty(self.shape, numpy.typeNA[self.type])
 
258
            else:
 
259
                data = Numeric.zeros(self.shape, typecode[self.type])
 
260
 
 
261
        if verbose:
 
262
            print "Object read:\n", repr(data) #, data.info()
 
263
            print "Should look like:\n", repr(object) #, objact.info()
 
264
            if hasattr(object, "shape"):
 
265
                print "Original object shape:", self.shape
 
266
                print "Shape read:", data.shape
 
267
                print "shape should look as:", object.shape
 
268
 
 
269
        if not hasattr(data, "shape"):
 
270
            # Scalar case
 
271
            assert len(self.shape) == 1
 
272
        assert carray._v_chunksize == self.chunksize
 
273
        assert allequal(data, object, self.flavor)
 
274
 
 
275
    def test03_setitemCArray(self):
 
276
        """Checking chunked layout array __setitem__ special method"""
 
277
 
 
278
        rootgroup = self.rootgroup
 
279
        if self.__class__.__name__ == "Ellipsis6CArrayTestCase":
 
280
            # see test_earray.py BasicTestCase.test03_setitemEArray
 
281
            return
 
282
        if verbose:
 
283
            print '\n', '-=' * 30
 
284
            print "Running %s.test03_setitemCArray..." % self.__class__.__name__
 
285
 
 
286
        if not hasattr(self, "slices"):
 
287
            # If there is not a slices attribute, create it
 
288
            self.slices = (slice(self.start, self.stop, self.step),)
 
289
 
 
290
        # Create an instance of an HDF5 Table
 
291
        if self.reopen:
 
292
            self.fileh = openFile(self.file, "a")
 
293
        carray = self.fileh.getNode("/carray1")
 
294
 
 
295
        # Choose a small value for buffer size
 
296
        #carray._v_maxTuples = 3   # this does not really changes the chunksize
 
297
        if verbose:
 
298
            print "CArray descr:", repr(carray)
 
299
            print "shape of read array ==>", carray.shape
 
300
            print "reopening?:", self.reopen
 
301
 
 
302
        # Build the array to do comparisons
 
303
        if str(self.type) == "CharType":
 
304
            object_ = strings.array("a"*self.objsize, shape=self.shape,
 
305
                                    itemsize=carray.itemsize)
 
306
        else:
 
307
            object_ = arange(self.objsize, shape=self.shape,
 
308
                             type=carray.type)
 
309
 
 
310
        stop = self.stop
 
311
        # do a copy() in order to ensure that len(object._data)
 
312
        # actually do a measure of its length
 
313
        object = object_.__getitem__(self.slices).copy()
 
314
 
 
315
        if self.flavor == "numpy":
 
316
            # Convert the object to NumPy
 
317
            object = convertNAToNumPy(object)
 
318
        elif self.flavor == "numeric":
 
319
            # Convert the object to Numeric
 
320
            object = convertNAToNumeric(object)
 
321
 
 
322
        if str(self.type) == "CharType":
 
323
            if hasattr(self, "wslice"):
 
324
                object[self.wslize] = object[self.wslice].pad("xXx")
 
325
                carray[self.wslice] = carray[self.wslice].pad("xXx")
 
326
            elif sum(object[self.slices].shape) <> 0 :
 
327
                object[:] = object.pad("xXx")
 
328
                if object.size() > 0:
 
329
                    carray[self.slices] = object
 
330
        else:
 
331
            if hasattr(self, "wslice"):
 
332
                object[self.wslice] = object[self.wslice] * 2 + 3
 
333
                carray[self.wslice] = carray[self.wslice] * 2 + 3
 
334
            elif sum(object[self.slices].shape) <> 0:
 
335
                object = object * 2 + 3
 
336
                if reduce(lambda x,y:x*y, object.shape) > 0:
 
337
                    carray[self.slices] = carray[self.slices] * 2 + 3
 
338
            # Cast again object to its original type
 
339
            object = array(object,type=carray.type)
 
340
        # Read datafrom the array
 
341
        try:
 
342
            data = carray.__getitem__(self.slices)
 
343
        except IndexError:
 
344
            print "IndexError!"
 
345
            if self.flavor == "numarray":
 
346
                data = array(None, shape = self.shape, type=self.type)
 
347
            elif self.flavor == "numarray":
 
348
                data = numpy.empty(self.shape, numpy.typeNA[self.type])
 
349
            else:
 
350
                data = Numeric.zeros(self.shape, typecode[self.type])
 
351
 
 
352
        if verbose:
 
353
            print "Object read:\n", repr(data) #, data.info()
 
354
            print "Should look like:\n", repr(object) #, object.info()
 
355
            if hasattr(object, "shape"):
 
356
                print "Original object shape:", self.shape
 
357
                print "Shape read:", data.shape
 
358
                print "shape should look as:", object.shape
 
359
 
 
360
        if not hasattr(data, "shape"):
 
361
            # Scalar case
 
362
            assert len(self.shape) == 1
 
363
        assert carray._v_chunksize == self.chunksize
 
364
        assert allequal(data, object, self.flavor)
 
365
 
 
366
 
 
367
class BasicWriteTestCase(BasicTestCase):
 
368
    type = Int32
 
369
    shape = (2,)
 
370
    chunksize = (5,)
 
371
    step = 1
 
372
    wslice = 1  # single element case
 
373
 
 
374
class BasicWrite2TestCase(BasicTestCase):
 
375
    type = Int32
 
376
    shape = (2,)
 
377
    chunksize = (5,)
 
378
    step = 1
 
379
    wslice = slice(shape[0]-2,shape[0],2)  # range of elements
 
380
    reopen = 0  # This case does not reopen files
 
381
 
 
382
class EmptyCArrayTestCase(BasicTestCase):
 
383
    type = Int32
 
384
    shape = (2, 2)
 
385
    chunksize = (5, 5)
 
386
    start = 0
 
387
    stop = 10
 
388
    step = 1
 
389
 
 
390
class EmptyCArray2TestCase(BasicTestCase):
 
391
    type = Int32
 
392
    shape = (2, 2)
 
393
    chunksize = (5, 5)
 
394
    start = 0
 
395
    stop = 10
 
396
    step = 1
 
397
    reopen = 0  # This case does not reopen files
 
398
 
 
399
class SlicesCArrayTestCase(BasicTestCase):
 
400
    compress = 1
 
401
    complib = "lzo"
 
402
    type = Int32
 
403
    shape = (2, 2)
 
404
    chunksize = (5, 5)
 
405
    slices = (slice(1,2,1), slice(1,3,1))
 
406
 
 
407
class EllipsisCArrayTestCase(BasicTestCase):
 
408
    type = Int32
 
409
    shape = (2, 2)
 
410
    chunksize = (5, 5)
 
411
    #slices = (slice(1,2,1), Ellipsis)
 
412
    slices = (Ellipsis, slice(1,2,1))
 
413
 
 
414
class Slices2CArrayTestCase(BasicTestCase):
 
415
    compress = 1
 
416
    complib = "lzo"
 
417
    type = Int32
 
418
    shape = (2, 2, 4)
 
419
    chunksize = (5, 5, 5)
 
420
    slices = (slice(1,2,1), slice(None, None, None), slice(1,4,2))
 
421
 
 
422
class Ellipsis2CArrayTestCase(BasicTestCase):
 
423
    type = Int32
 
424
    shape = (2, 2, 4)
 
425
    chunksize = (5, 5, 5)
 
426
    slices = (slice(1,2,1), Ellipsis, slice(1,4,2))
 
427
 
 
428
class Slices3CArrayTestCase(BasicTestCase):
 
429
    compress = 1      # To show the chunks id DEBUG is on
 
430
    complib = "lzo"
 
431
    type = Int32
 
432
    shape = (2, 3, 4, 2)
 
433
    chunksize = (5, 5, 5, 5)
 
434
    slices = (slice(1, 2, 1), slice(0, None, None), slice(1,4,2))  # Don't work
 
435
    #slices = (slice(None, None, None), slice(0, None, None), slice(1,4,1)) # W
 
436
    #slices = (slice(None, None, None), slice(None, None, None), slice(1,4,2)) # N
 
437
    #slices = (slice(1,2,1), slice(None, None, None), slice(1,4,2)) # N
 
438
    # Disable the failing test temporarily with a working test case
 
439
    slices = (slice(1,2,1), slice(1, 4, None), slice(1,4,2)) # Y
 
440
    #slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,1)) # Y
 
441
    slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,2)) # N
 
442
    #slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,2), slice(0,100,1)) # N
 
443
 
 
444
class Slices4CArrayTestCase(BasicTestCase):
 
445
    type = Int32
 
446
    shape = (2, 3, 4, 2, 5, 6)
 
447
    chunksize = (5,5, 5, 5, 5, 5)
 
448
    slices = (slice(1, 2, 1), slice(0, None, None), slice(1,4,2),
 
449
              slice(0,4,2), slice(3,5,2), slice(2,7,1))
 
450
 
 
451
class Ellipsis3CArrayTestCase(BasicTestCase):
 
452
    type = Int32
 
453
    shape = (2, 3, 4, 2)
 
454
    chunksize = (5, 5, 5, 5)
 
455
    slices = (Ellipsis, slice(0, 4, None), slice(1,4,2))
 
456
    slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,2), Ellipsis)
 
457
 
 
458
class Ellipsis4CArrayTestCase(BasicTestCase):
 
459
    type = Int32
 
460
    shape = (2, 3, 4, 5)
 
461
    chunksize = (5, 5, 5, 5)
 
462
    slices = (Ellipsis, slice(0, 4, None), slice(1,4,2))
 
463
    slices = (slice(1,2,1), Ellipsis, slice(1,4,2))
 
464
 
 
465
class Ellipsis5CArrayTestCase(BasicTestCase):
 
466
    type = Int32
 
467
    shape = (2, 3, 4, 5)
 
468
    chunksize = (5, 5, 5, 5)
 
469
    slices = (slice(1,2,1), slice(0, 4, None), Ellipsis)
 
470
 
 
471
class Ellipsis6CArrayTestCase(BasicTestCase):
 
472
    type = Int32
 
473
    shape = (2, 3, 4, 5)
 
474
    chunksize = (5, 5, 5, 5)
 
475
    # The next slices gives problems with setting values (test03)
 
476
    # This is a problem on the test design, not the Array.__setitem__
 
477
    # code, though. See # see test_earray.py Ellipsis6EArrayTestCase
 
478
    slices = (slice(1,2,1), slice(0, 4, None), 2, Ellipsis)
 
479
 
 
480
class Ellipsis7CArrayTestCase(BasicTestCase):
 
481
    type = Int32
 
482
    shape = (2, 3, 4, 5)
 
483
    chunksize = (5, 5, 5, 5)
 
484
    slices = (slice(1,2,1), slice(0, 4, None), slice(2,3), Ellipsis)
 
485
 
 
486
class MD3WriteTestCase(BasicTestCase):
 
487
    type = Int32
 
488
    shape = (2, 2, 3)
 
489
    chunksize = (4, 4, 4)
 
490
    step = 2
 
491
 
 
492
class MD5WriteTestCase(BasicTestCase):
 
493
    type = Int32
 
494
    shape = (2, 2, 3, 4, 5)  # ok
 
495
    #shape = (1, 1, 2, 1)  # Minimum shape that shows problems with HDF5 1.6.1
 
496
    #shape = (2, 3, 2, 4, 5)  # Floating point exception (HDF5 1.6.1)
 
497
    #shape = (2, 3, 3, 2, 5, 6) # Segmentation fault (HDF5 1.6.1)
 
498
    chunksize = (1, 1, 1, 1, 1)
 
499
    start = 1
 
500
    stop = 10
 
501
    step = 10
 
502
 
 
503
class MD6WriteTestCase(BasicTestCase):
 
504
    type = Int32
 
505
    shape = (2, 3, 3, 2, 5, 6)
 
506
    chunksize = (1, 1, 1, 1, 1, 1)
 
507
    start = 1
 
508
    stop = 10
 
509
    step = 3
 
510
 
 
511
class MD6WriteTestCase__(BasicTestCase):
 
512
    type = Int32
 
513
    shape = (2, 2)
 
514
    chunksize = (1, 1)
 
515
    start = 1
 
516
    stop = 3
 
517
    step = 1
 
518
 
 
519
class MD7WriteTestCase(BasicTestCase):
 
520
    type = Int32
 
521
    shape = (2, 3, 3, 4, 5, 2, 3)
 
522
    chunksize = (10, 10, 10, 10, 10, 10, 10)
 
523
    start = 1
 
524
    stop = 10
 
525
    step = 2
 
526
 
 
527
class MD10WriteTestCase(BasicTestCase):
 
528
    type = Int32
 
529
    shape = (1, 2, 3, 4, 5, 5, 4, 3, 2, 2)
 
530
    chunksize = (5, 5, 5, 5, 5, 5, 5, 5, 5, 5)
 
531
    start = -1
 
532
    stop = -1
 
533
    step = 10
 
534
 
 
535
class ZlibComprTestCase(BasicTestCase):
 
536
    compress = 1
 
537
    complib = "zlib"
 
538
    start = 3
 
539
    #stop = 0   # means last row
 
540
    stop = None   # means last row from 0.8 on
 
541
    step = 10
 
542
 
 
543
class ZlibShuffleTestCase(BasicTestCase):
 
544
    shuffle = 1
 
545
    compress = 1
 
546
    complib = "zlib"
 
547
    # case start < stop , i.e. no rows read
 
548
    start = 3
 
549
    stop = 1
 
550
    step = 10
 
551
 
 
552
class LZOComprTestCase(BasicTestCase):
 
553
    compress = 1  # sss
 
554
    complib = "lzo"
 
555
    chunksize = (10,10)
 
556
    start = 3
 
557
    stop = 10
 
558
    step = 3
 
559
 
 
560
class LZOShuffleTestCase(BasicTestCase):
 
561
    shape = (20,30)
 
562
    compress = 1
 
563
    shuffle = 1
 
564
    complib = "lzo"
 
565
    chunksize = (100,100)
 
566
    start = 3
 
567
    stop = 10
 
568
    step = 7
 
569
 
 
570
class BZIP2ComprTestCase(BasicTestCase):
 
571
    shape = (20,30)
 
572
    compress = 1
 
573
    complib = "bzip2"
 
574
    chunksize = (100,100)
 
575
    start = 3
 
576
    stop = 10
 
577
    step = 8
 
578
 
 
579
class BZIP2ShuffleTestCase(BasicTestCase):
 
580
    shape = (20,30)
 
581
    compress = 1
 
582
    shuffle = 1
 
583
    complib = "bzip2"
 
584
    chunksize = (100,100)
 
585
    start = 3
 
586
    stop = 10
 
587
    step = 6
 
588
 
 
589
class Fletcher32TestCase(BasicTestCase):
 
590
    shape = (60,50)
 
591
    compress = 0
 
592
    fletcher32 = 1
 
593
    chunksize = (50,50)
 
594
    start = 4
 
595
    stop = 20
 
596
    step = 7
 
597
 
 
598
class AllFiltersTestCase(BasicTestCase):
 
599
    compress = 1
 
600
    shuffle = 1
 
601
    fletcher32 = 1
 
602
    complib = "zlib"
 
603
    chunksize = (20,20)  # sss
 
604
    start = 2
 
605
    stop = 99
 
606
    step = 6
 
607
 
 
608
class FloatTypeTestCase(BasicTestCase):
 
609
    type = Float64
 
610
    shape = (2,2)
 
611
    chunksize = (5,5)
 
612
    start = 3
 
613
    stop = 10
 
614
    step = 20
 
615
 
 
616
class ComplexTypeTestCase(BasicTestCase):
 
617
    type = Complex64
 
618
    shape = (2,2)
 
619
    chunksize = (5,5)
 
620
    start = 3
 
621
    stop = 10
 
622
    step = 20
 
623
 
 
624
class CharTypeTestCase(BasicTestCase):
 
625
    type = "CharType"
 
626
    length = 20
 
627
    shape = (2, 2)
 
628
    #shape = (2,2,20)
 
629
    chunksize = (5,5)
 
630
    start = 3
 
631
    stop = 10
 
632
    step = 20
 
633
    slices = (slice(0,1),slice(1,2))
 
634
 
 
635
class CharType2TestCase(BasicTestCase):
 
636
    type = "CharType"
 
637
    length = 20
 
638
    shape = (2, 20)
 
639
    chunksize = (5,5)
 
640
    start = 1
 
641
    stop = 10
 
642
    step = 2
 
643
 
 
644
class CharTypeComprTestCase(BasicTestCase):
 
645
    type = "CharType"
 
646
    length = 20
 
647
    shape = (20,2,10)
 
648
    #shape = (20,0,10,20)
 
649
    compr = 1
 
650
    #shuffle = 1  # this shouldn't do nothing on chars
 
651
    chunksize = (50,50,2)
 
652
    start = -1
 
653
    stop = 100
 
654
    step = 20
 
655
 
 
656
class NumpyInt8TestCase(BasicTestCase):
 
657
    flavor = "numpy"
 
658
    type = "Int8"
 
659
    shape = (2,2)
 
660
    compress = 1
 
661
    shuffle = 1
 
662
    chunksize = (50,50)
 
663
    start = -1
 
664
    stop = 100
 
665
    step = 20
 
666
 
 
667
class NumpyInt16TestCase(BasicTestCase):
 
668
    flavor = "numpy"
 
669
    type = "Int16"
 
670
    shape = (2,2)
 
671
    compress = 1
 
672
    shuffle = 1
 
673
    chunksize = (50,50)
 
674
    start = 1
 
675
    stop = 100
 
676
    step = 1
 
677
 
 
678
class NumpyInt32TestCase(BasicTestCase):
 
679
    flavor = "numpy"
 
680
    type = "Int32"
 
681
    shape = (2,2)
 
682
    compress = 1
 
683
    shuffle = 1
 
684
    chunksize = (50,50)
 
685
    start = -1
 
686
    stop = 100
 
687
    step = 20
 
688
 
 
689
class NumpyFloat32TestCase(BasicTestCase):
 
690
    flavor = "numpy"
 
691
    type = "Float32"
 
692
    shape = (200,)
 
693
    compress = 1
 
694
    shuffle = 1
 
695
    chunksize = (20,)
 
696
    start = -1
 
697
    stop = 100
 
698
    step = 20
 
699
 
 
700
class NumpyFloat64TestCase(BasicTestCase):
 
701
    flavor = "numpy"
 
702
    type = "Float64"
 
703
    shape = (200,)
 
704
    compress = 1
 
705
    shuffle = 1
 
706
    chunksize = (20,)
 
707
    start = -1
 
708
    stop = 100
 
709
    step = 20
 
710
 
 
711
class NumpyComplex32TestCase(BasicTestCase):
 
712
    flavor = "numpy"
 
713
    type = "Complex32"
 
714
    shape = (4,)
 
715
    compress = 1
 
716
    shuffle = 1
 
717
    chunksize = (2,)
 
718
    start = -1
 
719
    stop = 100
 
720
    step = 20
 
721
 
 
722
class NumpyComplex64TestCase(BasicTestCase):
 
723
    flavor = "numpy"
 
724
    type = "Complex64"
 
725
    shape = (20,)
 
726
    compress = 1
 
727
    shuffle = 1
 
728
    chunksize = (2,)
 
729
    start = -1
 
730
    stop = 100
 
731
    step = 20
 
732
 
 
733
class NumpyComprTestCase(BasicTestCase):
 
734
    flavor = "numpy"
 
735
    type = "Float64"
 
736
    compress = 1
 
737
    shuffle = 1
 
738
    shape = (200,)
 
739
    compr = 1
 
740
    chunksize = (21,)
 
741
    start = 51
 
742
    stop = 100
 
743
    step = 7
 
744
 
 
745
class NumericInt8TestCase(BasicTestCase):
 
746
    flavor = "numeric"
 
747
    type = "Int8"
 
748
    shape = (2,2)
 
749
    compress = 1
 
750
    shuffle = 1
 
751
    chunksize = (50,50)
 
752
    start = -1
 
753
    stop = 100
 
754
    step = 20
 
755
 
 
756
class NumericInt16TestCase(BasicTestCase):
 
757
    flavor = "numeric"
 
758
    type = "Int16"
 
759
    shape = (2,2)
 
760
    compress = 1
 
761
    shuffle = 1
 
762
    chunksize = (50,50)
 
763
    start = 1
 
764
    stop = 100
 
765
    step = 1
 
766
 
 
767
class NumericInt32TestCase(BasicTestCase):
 
768
    flavor = "numeric"
 
769
    type = "Int32"
 
770
    shape = (2,2)
 
771
    compress = 1
 
772
    shuffle = 1
 
773
    chunksize = (50,50)
 
774
    start = -1
 
775
    stop = 100
 
776
    step = 20
 
777
 
 
778
class NumericFloat32TestCase(BasicTestCase):
 
779
    flavor = "numeric"
 
780
    type = "Float32"
 
781
    shape = (200,)
 
782
    compress = 1
 
783
    shuffle = 1
 
784
    chunksize = (20,)
 
785
    start = -1
 
786
    stop = 100
 
787
    step = 20
 
788
 
 
789
class NumericFloat64TestCase(BasicTestCase):
 
790
    flavor = "numeric"
 
791
    type = "Float64"
 
792
    shape = (200,)
 
793
    compress = 1
 
794
    shuffle = 1
 
795
    chunksize = (20,)
 
796
    start = -1
 
797
    stop = 100
 
798
    step = 20
 
799
 
 
800
class NumericComplex32TestCase(BasicTestCase):
 
801
    flavor = "numeric"
 
802
    type = "Complex32"
 
803
    shape = (4,)
 
804
    compress = 1
 
805
    shuffle = 1
 
806
    chunksize = (2,)
 
807
    start = -1
 
808
    stop = 100
 
809
    step = 20
 
810
 
 
811
class NumericComplex64TestCase(BasicTestCase):
 
812
    flavor = "numeric"
 
813
    type = "Complex64"
 
814
    shape = (20,)
 
815
    compress = 1
 
816
    shuffle = 1
 
817
    chunksize = (2,)
 
818
    start = -1
 
819
    stop = 100
 
820
    step = 20
 
821
 
 
822
class NumericComprTestCase(BasicTestCase):
 
823
    flavor = "numeric"
 
824
    type = "Float64"
 
825
    compress = 1
 
826
    shuffle = 1
 
827
    shape = (200,)
 
828
    compr = 1
 
829
    chunksize = (21,)
 
830
    start = 51
 
831
    stop = 100
 
832
    step = 7
 
833
 
 
834
# It remains a test of Numeric char types, but the code is getting too messy
 
835
 
 
836
class OffsetStrideTestCase(unittest.TestCase):
 
837
    mode  = "w"
 
838
    compress = 0
 
839
    complib = "zlib"  # Default compression library
 
840
 
 
841
    def setUp(self):
 
842
 
 
843
        # Create an instance of an HDF5 Table
 
844
        self.file = tempfile.mktemp(".h5")
 
845
        self.fileh = openFile(self.file, self.mode)
 
846
        self.rootgroup = self.fileh.root
 
847
 
 
848
    def tearDown(self):
 
849
        self.fileh.close()
 
850
        os.remove(self.file)
 
851
        cleanup(self)
 
852
 
 
853
    #----------------------------------------
 
854
 
 
855
    def test01a_String(self):
 
856
        """Checking carray with offseted numarray strings appends"""
 
857
 
 
858
        root = self.rootgroup
 
859
        if verbose:
 
860
            print '\n', '-=' * 30
 
861
            print "Running %s.test01a_String..." % self.__class__.__name__
 
862
 
 
863
        shape = (3,2,2)
 
864
        # Create an string atom
 
865
        carray = self.fileh.createCArray(root, 'strings', shape,
 
866
                                         StringAtom(length=3, shape=(1,2,2)),
 
867
                                         "Array of strings")
 
868
        a=strings.array([[["a","b"],["123", "45"],["45", "123"]]], itemsize=3)
 
869
        carray[0] = a[:,1:]
 
870
        a=strings.array([[["s", "a"],["ab", "f"],["s", "abc"],["abc", "f"]]])
 
871
        carray[1] = a[:,2:]
 
872
 
 
873
        # Read all the data:
 
874
        data = carray.read()
 
875
        if verbose:
 
876
            print "Object read:", data
 
877
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
878
            print "Second row in carray ==>", data[1].tolist()
 
879
 
 
880
        assert carray.nrows == 3
 
881
        assert data[0].tolist() == [["123", "45"],["45", "123"]]
 
882
        assert data[1].tolist() == [["s", "abc"],["abc", "f"]]
 
883
        assert len(data[0]) == 2
 
884
        assert len(data[1]) == 2
 
885
 
 
886
    def test01b_String(self):
 
887
        """Checking carray with strided numarray strings appends"""
 
888
 
 
889
        root = self.rootgroup
 
890
        if verbose:
 
891
            print '\n', '-=' * 30
 
892
            print "Running %s.test01b_String..." % self.__class__.__name__
 
893
 
 
894
        shape = (3,2,2)
 
895
        # Create an string atom
 
896
        carray = self.fileh.createCArray(root, 'strings', shape,
 
897
                                         StringAtom(length=3, shape=(1,2,2)),
 
898
                                         "Array of strings")
 
899
        a=strings.array([[["a","b"],["123", "45"],["45", "123"]]], itemsize=3)
 
900
        carray[0] = a[:,::2]
 
901
        a=strings.array([[["s", "a"],["ab", "f"],["s", "abc"],["abc", "f"]]])
 
902
        carray[1] = a[:,::2]
 
903
 
 
904
        # Read all the rows:
 
905
        data = carray.read()
 
906
        if verbose:
 
907
            print "Object read:", data
 
908
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
909
            print "Second row in carray ==>", data[1].tolist()
 
910
 
 
911
        assert carray.nrows == 3
 
912
        assert data[0].tolist() == [["a","b"],["45", "123"]]
 
913
        assert data[1].tolist() == [["s", "a"],["s", "abc"]]
 
914
        assert len(data[0]) == 2
 
915
        assert len(data[1]) == 2
 
916
 
 
917
    def test02a_int(self):
 
918
        """Checking carray with offseted numarray ints appends"""
 
919
 
 
920
        root = self.rootgroup
 
921
        if verbose:
 
922
            print '\n', '-=' * 30
 
923
            print "Running %s.test02a_int..." % self.__class__.__name__
 
924
 
 
925
        shape = (3,3)
 
926
        # Create an string atom
 
927
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
928
                                         Int32Atom(shape=(1,3)),
 
929
                                         "array of ints")
 
930
        a=array([(0,0,0), (1,0,3), (1,1,1), (0,0,0)], type=Int32)
 
931
        carray[0:2] = a[2:]  # Introduce an offset
 
932
        a=array([(1,1,1), (-1,0,0)], type=Int32)
 
933
        carray[2:3] = a[1:]  # Introduce an offset
 
934
 
 
935
        # Read all the rows:
 
936
        data = carray.read()
 
937
        if verbose:
 
938
            print "Object read:", data
 
939
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
940
            print "Third row in carray ==>", data[2]
 
941
 
 
942
        assert carray.nrows == 3
 
943
        assert allequal(data[0], array([1,1,1], type=Int32))
 
944
        assert allequal(data[1], array([0,0,0], type=Int32))
 
945
        assert allequal(data[2], array([-1,0,0], type=Int32))
 
946
 
 
947
    def test02b_int(self):
 
948
        """Checking carray with strided numarray ints appends"""
 
949
 
 
950
        root = self.rootgroup
 
951
        if verbose:
 
952
            print '\n', '-=' * 30
 
953
            print "Running %s.test02b_int..." % self.__class__.__name__
 
954
 
 
955
        shape = (3,3)
 
956
        # Create an string atom
 
957
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
958
                                         Int32Atom(shape=(1,3)),
 
959
                                         "array of ints")
 
960
        a=array([(0,0,0), (1,0,3), (1,1,1), (3,3,3)], type=Int32)
 
961
        carray[0:2] = a[::3]  # Create an offset
 
962
        a=array([(1,1,1), (-1,0,0)], type=Int32)
 
963
        carray[2:3] = a[::2]  # Create an offset
 
964
 
 
965
        # Read all the rows:
 
966
        data = carray.read()
 
967
        if verbose:
 
968
            print "Object read:", data
 
969
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
970
            print "Third row in carray ==>", data[2]
 
971
 
 
972
        assert carray.nrows == 3
 
973
        assert allequal(data[0], array([0,0,0], type=Int32))
 
974
        assert allequal(data[1], array([3,3,3], type=Int32))
 
975
        assert allequal(data[2], array([1,1,1], type=Int32))
 
976
 
 
977
 
 
978
class NumpyOffsetStrideTestCase(unittest.TestCase):
 
979
    mode  = "w"
 
980
    compress = 0
 
981
    complib = "zlib"  # Default compression library
 
982
 
 
983
    def setUp(self):
 
984
 
 
985
        # Create an instance of an HDF5 Table
 
986
        self.file = tempfile.mktemp(".h5")
 
987
        self.fileh = openFile(self.file, self.mode)
 
988
        self.rootgroup = self.fileh.root
 
989
 
 
990
    def tearDown(self):
 
991
        self.fileh.close()
 
992
        os.remove(self.file)
 
993
        cleanup(self)
 
994
 
 
995
    #----------------------------------------
 
996
 
 
997
    def test02a_int(self):
 
998
        """Checking carray with offseted NumPy ints appends"""
 
999
 
 
1000
        root = self.rootgroup
 
1001
        if verbose:
 
1002
            print '\n', '-=' * 30
 
1003
            print "Running %s.test02a_int..." % self.__class__.__name__
 
1004
 
 
1005
        shape = (3,3)
 
1006
        # Create an string atom
 
1007
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
1008
                                         Int32Atom(shape=(1,3)),
 
1009
                                         "array of ints")
 
1010
        a=numpy.array([(0,0,0), (1,0,3), (1,1,1), (0,0,0)], dtype='int32')
 
1011
        carray[0:2] = a[2:]  # Introduce an offset
 
1012
        a=numpy.array([(1,1,1), (-1,0,0)], dtype='int32')
 
1013
        carray[2:3] = a[1:]  # Introduce an offset
 
1014
 
 
1015
        # Read all the rows:
 
1016
        data = carray.read()
 
1017
        if verbose:
 
1018
            print "Object read:", data
 
1019
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
1020
            print "Third row in carray ==>", data[2]
 
1021
 
 
1022
        assert carray.nrows == 3
 
1023
        assert allequal(data[0], array([1,1,1], typecode='i'))
 
1024
        assert allequal(data[1], array([0,0,0], typecode='i'))
 
1025
        assert allequal(data[2], array([-1,0,0], typecode='i'))
 
1026
 
 
1027
    def test02b_int(self):
 
1028
        """Checking carray with strided NumPy ints appends"""
 
1029
 
 
1030
        root = self.rootgroup
 
1031
        if verbose:
 
1032
            print '\n', '-=' * 30
 
1033
            print "Running %s.test02b_int..." % self.__class__.__name__
 
1034
 
 
1035
        shape = (3,3)
 
1036
        # Create an string atom
 
1037
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
1038
                                         Int32Atom(shape=(1,3)),
 
1039
                                         "array of ints")
 
1040
        a=numpy.array([(0,0,0), (1,0,3), (1,2,1), (3,2,3)], dtype='int32')
 
1041
        carray[0:2] = a[::3]  # Create a strided object
 
1042
        a=numpy.array([(1,0,1), (-1,0,0)], dtype='int32')
 
1043
        carray[2:3] = a[::2]  # Create a strided object
 
1044
 
 
1045
        # Read all the rows:
 
1046
        data = carray.read()
 
1047
        if verbose:
 
1048
            print "Object read:", data
 
1049
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
1050
            print "Third row in carray ==>", data[2]
 
1051
 
 
1052
        assert carray.nrows == 3
 
1053
        assert allequal(data[0], array([0,0,0], typecode='i'))
 
1054
        assert allequal(data[1], array([3,2,3], typecode='i'))
 
1055
        assert allequal(data[2], array([1,0,1], typecode='i'))
 
1056
 
 
1057
 
 
1058
class NumericOffsetStrideTestCase(unittest.TestCase):
 
1059
    mode  = "w"
 
1060
    compress = 0
 
1061
    complib = "zlib"  # Default compression library
 
1062
 
 
1063
    def setUp(self):
 
1064
 
 
1065
        # Create an instance of an HDF5 Table
 
1066
        self.file = tempfile.mktemp(".h5")
 
1067
        self.fileh = openFile(self.file, self.mode)
 
1068
        self.rootgroup = self.fileh.root
 
1069
 
 
1070
    def tearDown(self):
 
1071
        self.fileh.close()
 
1072
        os.remove(self.file)
 
1073
        cleanup(self)
 
1074
 
 
1075
    #----------------------------------------
 
1076
 
 
1077
    def test02a_int(self):
 
1078
        """Checking carray with offseted Numeric ints appends"""
 
1079
 
 
1080
        root = self.rootgroup
 
1081
        if verbose:
 
1082
            print '\n', '-=' * 30
 
1083
            print "Running %s.test02a_int..." % self.__class__.__name__
 
1084
 
 
1085
        shape = (3,3)
 
1086
        # Create an string atom
 
1087
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
1088
                                         Int32Atom(shape=(1,3)),
 
1089
                                         "array of ints")
 
1090
        a=Numeric.array([(0,0,0), (1,0,3), (1,1,1), (0,0,0)], typecode='i')
 
1091
        carray[0:2] = a[2:]  # Introduce an offset
 
1092
        a=Numeric.array([(1,1,1), (-1,0,0)], typecode='i')
 
1093
        carray[2:3] = a[1:]  # Introduce an offset
 
1094
 
 
1095
        # Read all the rows:
 
1096
        data = carray.read()
 
1097
        if verbose:
 
1098
            print "Object read:", data
 
1099
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
1100
            print "Third row in carray ==>", data[2]
 
1101
 
 
1102
        assert carray.nrows == 3
 
1103
        assert allequal(data[0], array([1,1,1], typecode='i'))
 
1104
        assert allequal(data[1], array([0,0,0], typecode='i'))
 
1105
        assert allequal(data[2], array([-1,0,0], typecode='i'))
 
1106
 
 
1107
    def test02b_int(self):
 
1108
        """Checking carray with strided Numeric ints appends"""
 
1109
 
 
1110
        root = self.rootgroup
 
1111
        if verbose:
 
1112
            print '\n', '-=' * 30
 
1113
            print "Running %s.test02b_int..." % self.__class__.__name__
 
1114
 
 
1115
        shape = (3,3)
 
1116
        # Create an string atom
 
1117
        carray = self.fileh.createCArray(root, 'CAtom', shape,
 
1118
                                         Int32Atom(shape=(1,3)),
 
1119
                                         "array of ints")
 
1120
        a=Numeric.array([(0,0,0), (1,0,3), (1,2,1), (3,2,3)], typecode='i')
 
1121
        carray[0:2] = a[::3]  # Create a strided object
 
1122
        a=Numeric.array([(1,0,1), (-1,0,0)], typecode='i')
 
1123
        carray[2:3] = a[::2]  # Create a strided object
 
1124
 
 
1125
        # Read all the rows:
 
1126
        data = carray.read()
 
1127
        if verbose:
 
1128
            print "Object read:", data
 
1129
            print "Nrows in", carray._v_pathname, ":", carray.nrows
 
1130
            print "Third row in carray ==>", data[2]
 
1131
 
 
1132
        assert carray.nrows == 3
 
1133
        assert allequal(data[0], array([0,0,0], typecode='i'))
 
1134
        assert allequal(data[1], array([3,2,3], typecode='i'))
 
1135
        assert allequal(data[2], array([1,0,1], typecode='i'))
 
1136
 
 
1137
 
 
1138
class CopyTestCase(unittest.TestCase):
 
1139
 
 
1140
    def test01a_copy(self):
 
1141
        """Checking CArray.copy() method """
 
1142
 
 
1143
        if verbose:
 
1144
            print '\n', '-=' * 30
 
1145
            print "Running %s.test01a_copy..." % self.__class__.__name__
 
1146
 
 
1147
        # Create an instance of an HDF5 Table
 
1148
        file = tempfile.mktemp(".h5")
 
1149
        fileh = openFile(file, "w")
 
1150
 
 
1151
        # Create an CArray
 
1152
        shape = (2,2)
 
1153
        arr = Atom(shape=(2, 2), dtype=Int16)
 
1154
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1155
                                    "title array1")
 
1156
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1157
 
 
1158
        if self.close:
 
1159
            if verbose:
 
1160
                print "(closing file version)"
 
1161
            fileh.close()
 
1162
            fileh = openFile(file, mode = "a")
 
1163
            array1 = fileh.root.array1
 
1164
 
 
1165
        # Copy it to another location
 
1166
        array2 = array1.copy('/', 'array2')
 
1167
 
 
1168
        if self.close:
 
1169
            if verbose:
 
1170
                print "(closing file version)"
 
1171
            fileh.close()
 
1172
            fileh = openFile(file, mode = "r")
 
1173
            array1 = fileh.root.array1
 
1174
            array2 = fileh.root.array2
 
1175
 
 
1176
        if verbose:
 
1177
            print "array1-->", array1.read()
 
1178
            print "array2-->", array2.read()
 
1179
            #print "dirs-->", dir(array1), dir(array2)
 
1180
            print "attrs array1-->", repr(array1.attrs)
 
1181
            print "attrs array2-->", repr(array2.attrs)
 
1182
 
 
1183
        # Check that all the elements are equal
 
1184
        assert allequal(array1.read(), array2.read())
 
1185
 
 
1186
        # Assert other properties in array
 
1187
        assert array1.nrows == array2.nrows
 
1188
        assert array1.shape == array2.shape
 
1189
        assert array1.extdim == array2.extdim
 
1190
        assert array1.flavor == array2.flavor
 
1191
        assert array1.type == array2.type
 
1192
        assert array1.itemsize == array2.itemsize
 
1193
        assert array1.title == array2.title
 
1194
        assert str(array1.atom) == str(array2.atom)
 
1195
        assert array1._v_chunksize == array2._v_chunksize
 
1196
 
 
1197
        # Close the file
 
1198
        fileh.close()
 
1199
        os.remove(file)
 
1200
 
 
1201
    def test01b_copy(self):
 
1202
        """Checking CArray.copy() method """
 
1203
 
 
1204
        if verbose:
 
1205
            print '\n', '-=' * 30
 
1206
            print "Running %s.test01b_copy..." % self.__class__.__name__
 
1207
 
 
1208
        # Create an instance of an HDF5 Table
 
1209
        file = tempfile.mktemp(".h5")
 
1210
        fileh = openFile(file, "w")
 
1211
 
 
1212
        # Create an CArray
 
1213
        shape = (2,2)
 
1214
        arr = Atom(shape=(5, 5), dtype=Int16)
 
1215
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1216
                                    "title array1")
 
1217
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1218
 
 
1219
        if self.close:
 
1220
            if verbose:
 
1221
                print "(closing file version)"
 
1222
            fileh.close()
 
1223
            fileh = openFile(file, mode = "a")
 
1224
            array1 = fileh.root.array1
 
1225
 
 
1226
        # Copy it to another location
 
1227
        array2 = array1.copy('/', 'array2')
 
1228
 
 
1229
        if self.close:
 
1230
            if verbose:
 
1231
                print "(closing file version)"
 
1232
            fileh.close()
 
1233
            fileh = openFile(file, mode = "r")
 
1234
            array1 = fileh.root.array1
 
1235
            array2 = fileh.root.array2
 
1236
 
 
1237
        if verbose:
 
1238
            print "array1-->", array1.read()
 
1239
            print "array2-->", array2.read()
 
1240
            #print "dirs-->", dir(array1), dir(array2)
 
1241
            print "attrs array1-->", repr(array1.attrs)
 
1242
            print "attrs array2-->", repr(array2.attrs)
 
1243
 
 
1244
        # Check that all the elements are equal
 
1245
        assert allequal(array1.read(), array2.read())
 
1246
 
 
1247
        # Assert other properties in array
 
1248
        assert array1.nrows == array2.nrows
 
1249
        assert array1.shape == array2.shape
 
1250
        assert array1.extdim == array2.extdim
 
1251
        assert array1.flavor == array2.flavor
 
1252
        assert array1.type == array2.type
 
1253
        assert array1.itemsize == array2.itemsize
 
1254
        assert array1.title == array2.title
 
1255
        assert str(array1.atom) == str(array2.atom)
 
1256
        assert array1._v_chunksize == array2._v_chunksize
 
1257
 
 
1258
        # Close the file
 
1259
        fileh.close()
 
1260
        os.remove(file)
 
1261
 
 
1262
    def test01c_copy(self):
 
1263
        """Checking CArray.copy() method """
 
1264
 
 
1265
        if verbose:
 
1266
            print '\n', '-=' * 30
 
1267
            print "Running %s.test01c_copy..." % self.__class__.__name__
 
1268
 
 
1269
        # Create an instance of an HDF5 Table
 
1270
        file = tempfile.mktemp(".h5")
 
1271
        fileh = openFile(file, "w")
 
1272
 
 
1273
        # Create an CArray
 
1274
        shape = (5,5)
 
1275
        arr = Atom(shape=(2, 2), dtype=Int16)
 
1276
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1277
                                    "title array1")
 
1278
        array1[:2,:2] = array([[456, 2],[3, 457]], type=Int16)
 
1279
 
 
1280
        if self.close:
 
1281
            if verbose:
 
1282
                print "(closing file version)"
 
1283
            fileh.close()
 
1284
            fileh = openFile(file, mode = "a")
 
1285
            array1 = fileh.root.array1
 
1286
 
 
1287
        # Copy it to another location
 
1288
        array2 = array1.copy('/', 'array2')
 
1289
 
 
1290
        if self.close:
 
1291
            if verbose:
 
1292
                print "(closing file version)"
 
1293
            fileh.close()
 
1294
            fileh = openFile(file, mode = "r")
 
1295
            array1 = fileh.root.array1
 
1296
            array2 = fileh.root.array2
 
1297
 
 
1298
        if verbose:
 
1299
            print "array1-->", array1.read()
 
1300
            print "array2-->", array2.read()
 
1301
            #print "dirs-->", dir(array1), dir(array2)
 
1302
            print "attrs array1-->", repr(array1.attrs)
 
1303
            print "attrs array2-->", repr(array2.attrs)
 
1304
 
 
1305
        # Check that all the elements are equal
 
1306
        assert allequal(array1.read(), array2.read())
 
1307
 
 
1308
        # Assert other properties in array
 
1309
        assert array1.nrows == array2.nrows
 
1310
        assert array1.shape == array2.shape
 
1311
        assert array1.extdim == array2.extdim
 
1312
        assert array1.flavor == array2.flavor
 
1313
        assert array1.type == array2.type
 
1314
        assert array1.itemsize == array2.itemsize
 
1315
        assert array1.title == array2.title
 
1316
        assert str(array1.atom) == str(array2.atom)
 
1317
        assert array1._v_chunksize == array2._v_chunksize
 
1318
 
 
1319
        # Close the file
 
1320
        fileh.close()
 
1321
        os.remove(file)
 
1322
 
 
1323
    def test02_copy(self):
 
1324
        """Checking CArray.copy() method (where specified)"""
 
1325
 
 
1326
        if verbose:
 
1327
            print '\n', '-=' * 30
 
1328
            print "Running %s.test02_copy..." % self.__class__.__name__
 
1329
 
 
1330
        # Create an instance of an HDF5 Table
 
1331
        file = tempfile.mktemp(".h5")
 
1332
        fileh = openFile(file, "w")
 
1333
 
 
1334
        # Create an CArray
 
1335
        shape = (5,5)
 
1336
        arr = Atom(shape=(2, 2), dtype=Int16)
 
1337
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1338
                                    "title array1")
 
1339
        array1[:2,:2] = array([[456, 2],[3, 457]], type=Int16)
 
1340
 
 
1341
        if self.close:
 
1342
            if verbose:
 
1343
                print "(closing file version)"
 
1344
            fileh.close()
 
1345
            fileh = openFile(file, mode = "a")
 
1346
            array1 = fileh.root.array1
 
1347
 
 
1348
        # Copy to another location
 
1349
        group1 = fileh.createGroup("/", "group1")
 
1350
        array2 = array1.copy(group1, 'array2')
 
1351
 
 
1352
        if self.close:
 
1353
            if verbose:
 
1354
                print "(closing file version)"
 
1355
            fileh.close()
 
1356
            fileh = openFile(file, mode = "r")
 
1357
            array1 = fileh.root.array1
 
1358
            array2 = fileh.root.group1.array2
 
1359
 
 
1360
        if verbose:
 
1361
            print "array1-->", array1.read()
 
1362
            print "array2-->", array2.read()
 
1363
            #print "dirs-->", dir(array1), dir(array2)
 
1364
            print "attrs array1-->", repr(array1.attrs)
 
1365
            print "attrs array2-->", repr(array2.attrs)
 
1366
 
 
1367
        # Check that all the elements are equal
 
1368
        assert allequal(array1.read(), array2.read())
 
1369
 
 
1370
        # Assert other properties in array
 
1371
        assert array1.nrows == array2.nrows
 
1372
        assert array1.shape == array2.shape
 
1373
        assert array1.extdim == array2.extdim
 
1374
        assert array1.flavor == array2.flavor
 
1375
        assert array1.type == array2.type
 
1376
        assert array1.itemsize == array2.itemsize
 
1377
        assert array1.title == array2.title
 
1378
        assert str(array1.atom) == str(array2.atom)
 
1379
        assert array1._v_chunksize == array2._v_chunksize
 
1380
 
 
1381
        # Close the file
 
1382
        fileh.close()
 
1383
        os.remove(file)
 
1384
 
 
1385
    def test03_copy(self):
 
1386
        """Checking CArray.copy() method (Numeric flavor)"""
 
1387
 
 
1388
        if verbose:
 
1389
            print '\n', '-=' * 30
 
1390
            print "Running %s.test03_copy..." % self.__class__.__name__
 
1391
 
 
1392
        # Create an instance of an HDF5 Table
 
1393
        file = tempfile.mktemp(".h5")
 
1394
        fileh = openFile(file, "w")
 
1395
 
 
1396
        if numeric:
 
1397
            arr = Atom(shape=(2, 2), dtype=Int16, flavor="numeric")
 
1398
        else:
 
1399
            arr = Atom(shape=(2, 2), dtype=Int16)
 
1400
 
 
1401
        shape = (2,2)
 
1402
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1403
                                    "title array1")
 
1404
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1405
 
 
1406
        if self.close:
 
1407
            if verbose:
 
1408
                print "(closing file version)"
 
1409
            fileh.close()
 
1410
            fileh = openFile(file, mode = "a")
 
1411
            array1 = fileh.root.array1
 
1412
 
 
1413
        # Copy to another location
 
1414
        array2 = array1.copy('/', 'array2')
 
1415
 
 
1416
        if self.close:
 
1417
            if verbose:
 
1418
                print "(closing file version)"
 
1419
            fileh.close()
 
1420
            fileh = openFile(file, mode = "r")
 
1421
            array1 = fileh.root.array1
 
1422
            array2 = fileh.root.array2
 
1423
 
 
1424
        if verbose:
 
1425
            print "attrs array1-->", repr(array1.attrs)
 
1426
            print "attrs array2-->", repr(array2.attrs)
 
1427
 
 
1428
        # Assert other properties in array
 
1429
        assert array1.nrows == array2.nrows
 
1430
        assert array1.shape == array2.shape
 
1431
        assert array1.extdim == array2.extdim
 
1432
        assert array1.flavor == array2.flavor   # Very important here!
 
1433
        assert array1.type == array2.type
 
1434
        assert array1.itemsize == array2.itemsize
 
1435
        assert array1.title == array2.title
 
1436
        assert str(array1.atom) == str(array2.atom)
 
1437
        assert array1._v_chunksize == array2._v_chunksize
 
1438
 
 
1439
        # Close the file
 
1440
        fileh.close()
 
1441
        os.remove(file)
 
1442
 
 
1443
    def test03c_copy(self):
 
1444
        """Checking CArray.copy() method (python flavor)"""
 
1445
 
 
1446
        if verbose:
 
1447
            print '\n', '-=' * 30
 
1448
            print "Running %s.test03c_copy..." % self.__class__.__name__
 
1449
 
 
1450
        # Create an instance of an HDF5 Table
 
1451
        file = tempfile.mktemp(".h5")
 
1452
        fileh = openFile(file, "w")
 
1453
 
 
1454
        shape = (2,2)
 
1455
        arr = Atom(shape=(2, 2), dtype=Int16, flavor="python")
 
1456
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1457
                                    "title array1")
 
1458
        array1[...] = [[456, 2],[3, 457]]
 
1459
 
 
1460
        if self.close:
 
1461
            if verbose:
 
1462
                print "(closing file version)"
 
1463
            fileh.close()
 
1464
            fileh = openFile(file, mode = "a")
 
1465
            array1 = fileh.root.array1
 
1466
 
 
1467
        # Copy to another location
 
1468
        array2 = array1.copy('/', 'array2')
 
1469
 
 
1470
        if self.close:
 
1471
            if verbose:
 
1472
                print "(closing file version)"
 
1473
            fileh.close()
 
1474
            fileh = openFile(file, mode = "r")
 
1475
            array1 = fileh.root.array1
 
1476
            array2 = fileh.root.array2
 
1477
 
 
1478
        if verbose:
 
1479
            print "attrs array1-->", repr(array1.attrs)
 
1480
            print "attrs array2-->", repr(array2.attrs)
 
1481
 
 
1482
        # Check that all elements are equal
 
1483
        assert array1.read() == array2.read()
 
1484
        # Assert other properties in array
 
1485
        assert array1.nrows == array2.nrows
 
1486
        assert array1.shape == array2.shape
 
1487
        assert array1.extdim == array2.extdim
 
1488
        assert array1.flavor == array2.flavor   # Very important here!
 
1489
        assert array1.type == array2.type
 
1490
        assert array1.itemsize == array2.itemsize
 
1491
        assert array1.title == array2.title
 
1492
        assert str(array1.atom) == str(array2.atom)
 
1493
        assert array1._v_chunksize == array2._v_chunksize
 
1494
 
 
1495
        # Close the file
 
1496
        fileh.close()
 
1497
        os.remove(file)
 
1498
 
 
1499
    def test03d_copy(self):
 
1500
        """Checking CArray.copy() method (string python flavor)"""
 
1501
 
 
1502
        if verbose:
 
1503
            print '\n', '-=' * 30
 
1504
            print "Running %s.test03d_copy..." % self.__class__.__name__
 
1505
 
 
1506
        # Create an instance of an HDF5 Table
 
1507
        file = tempfile.mktemp(".h5")
 
1508
        fileh = openFile(file, "w")
 
1509
 
 
1510
        shape = (2,2)
 
1511
        arr = StringAtom(shape=(2, 2), length=4, flavor="python")
 
1512
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1513
                                    "title array1")
 
1514
        array1[...] = [["456", "2"],["3", "457"]]
 
1515
 
 
1516
        if self.close:
 
1517
            if verbose:
 
1518
                print "(closing file version)"
 
1519
            fileh.close()
 
1520
            fileh = openFile(file, mode = "a")
 
1521
            array1 = fileh.root.array1
 
1522
 
 
1523
        # Copy to another location
 
1524
        array2 = array1.copy('/', 'array2')
 
1525
 
 
1526
        if self.close:
 
1527
            if verbose:
 
1528
                print "(closing file version)"
 
1529
            fileh.close()
 
1530
            fileh = openFile(file, mode = "r")
 
1531
            array1 = fileh.root.array1
 
1532
            array2 = fileh.root.array2
 
1533
 
 
1534
        if verbose:
 
1535
            print "type value-->", type(array2[:][0][0])
 
1536
            print "value-->", array2[:]
 
1537
            print "attrs array1-->", repr(array1.attrs)
 
1538
            print "attrs array2-->", repr(array2.attrs)
 
1539
 
 
1540
        # Check that all elements are equal
 
1541
        assert array1.read() == array2.read()
 
1542
 
 
1543
        # Assert other properties in array
 
1544
        assert array1.nrows == array2.nrows
 
1545
        assert array1.shape == array2.shape
 
1546
        assert array1.extdim == array2.extdim
 
1547
        assert array1.flavor == array2.flavor   # Very important here!
 
1548
        assert array1.type == array2.type
 
1549
        assert array1.itemsize == array2.itemsize
 
1550
        assert array1.title == array2.title
 
1551
        assert str(array1.atom) == str(array2.atom)
 
1552
        assert array1._v_chunksize == array2._v_chunksize
 
1553
 
 
1554
        # Close the file
 
1555
        fileh.close()
 
1556
        os.remove(file)
 
1557
 
 
1558
    def test03e_copy(self):
 
1559
        """Checking CArray.copy() method (CharArray flavor)"""
 
1560
 
 
1561
        if verbose:
 
1562
            print '\n', '-=' * 30
 
1563
            print "Running %s.test03e_copy..." % self.__class__.__name__
 
1564
 
 
1565
        # Create an instance of an HDF5 Table
 
1566
        file = tempfile.mktemp(".h5")
 
1567
        fileh = openFile(file, "w")
 
1568
 
 
1569
        shape = (2,2)
 
1570
        arr = StringAtom(shape=(2, 2), length=4, flavor="numarray")
 
1571
        array1 = fileh.createCArray(fileh.root, 'array1', shape, arr,
 
1572
                                    "title array1")
 
1573
        array1[...] = strings.array([["456", "2"],["3", "457"]], itemsize=4)
 
1574
 
 
1575
        if self.close:
 
1576
            if verbose:
 
1577
                print "(closing file version)"
 
1578
            fileh.close()
 
1579
            fileh = openFile(file, mode = "a")
 
1580
            array1 = fileh.root.array1
 
1581
 
 
1582
        # Copy to another location
 
1583
        array2 = array1.copy('/', 'array2')
 
1584
 
 
1585
        if self.close:
 
1586
            if verbose:
 
1587
                print "(closing file version)"
 
1588
            fileh.close()
 
1589
            fileh = openFile(file, mode = "r")
 
1590
            array1 = fileh.root.array1
 
1591
            array2 = fileh.root.array2
 
1592
 
 
1593
        if verbose:
 
1594
            print "attrs array1-->", repr(array1.attrs)
 
1595
            print "attrs array2-->", repr(array2.attrs)
 
1596
 
 
1597
        # Check that all elements are equal
 
1598
        assert allequal(array1.read(), array2.read())
 
1599
        # Assert other properties in array
 
1600
        assert array1.nrows == array2.nrows
 
1601
        assert array1.shape == array2.shape
 
1602
        assert array1.extdim == array2.extdim
 
1603
        assert array1.flavor == array2.flavor   # Very important here!
 
1604
        assert array1.type == array2.type
 
1605
        assert array1.itemsize == array2.itemsize
 
1606
        assert array1.title == array2.title
 
1607
        assert str(array1.atom) == str(array2.atom)
 
1608
        assert array1._v_chunksize == array2._v_chunksize
 
1609
 
 
1610
        # Close the file
 
1611
        fileh.close()
 
1612
        os.remove(file)
 
1613
 
 
1614
    def test04_copy(self):
 
1615
        """Checking CArray.copy() method (checking title copying)"""
 
1616
 
 
1617
        if verbose:
 
1618
            print '\n', '-=' * 30
 
1619
            print "Running %s.test04_copy..." % self.__class__.__name__
 
1620
 
 
1621
        # Create an instance of an HDF5 Table
 
1622
        file = tempfile.mktemp(".h5")
 
1623
        fileh = openFile(file, "w")
 
1624
 
 
1625
        # Create an CArray
 
1626
        shape = (2,2)
 
1627
        atom=Int16Atom(shape=(2,2))
 
1628
        array1 = fileh.createCArray(fileh.root, 'array1', shape, atom,
 
1629
                                    "title array1")
 
1630
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1631
        # Append some user attrs
 
1632
        array1.attrs.attr1 = "attr1"
 
1633
        array1.attrs.attr2 = 2
 
1634
 
 
1635
        if self.close:
 
1636
            if verbose:
 
1637
                print "(closing file version)"
 
1638
            fileh.close()
 
1639
            fileh = openFile(file, mode = "a")
 
1640
            array1 = fileh.root.array1
 
1641
 
 
1642
        # Copy it to another Array
 
1643
        array2 = array1.copy('/', 'array2', title="title array2")
 
1644
 
 
1645
        if self.close:
 
1646
            if verbose:
 
1647
                print "(closing file version)"
 
1648
            fileh.close()
 
1649
            fileh = openFile(file, mode = "r")
 
1650
            array1 = fileh.root.array1
 
1651
            array2 = fileh.root.array2
 
1652
 
 
1653
        # Assert user attributes
 
1654
        if verbose:
 
1655
            print "title of destination array-->", array2.title
 
1656
        array2.title == "title array2"
 
1657
 
 
1658
        # Close the file
 
1659
        fileh.close()
 
1660
        os.remove(file)
 
1661
 
 
1662
    def test05_copy(self):
 
1663
        """Checking CArray.copy() method (user attributes copied)"""
 
1664
 
 
1665
        if verbose:
 
1666
            print '\n', '-=' * 30
 
1667
            print "Running %s.test05_copy..." % self.__class__.__name__
 
1668
 
 
1669
        # Create an instance of an HDF5 Table
 
1670
        file = tempfile.mktemp(".h5")
 
1671
        fileh = openFile(file, "w")
 
1672
 
 
1673
        # Create an CArray
 
1674
        shape = (2,2)
 
1675
        atom=Int16Atom(shape=(2,2))
 
1676
        array1 = fileh.createCArray(fileh.root, 'array1', shape, atom,
 
1677
                                    "title array1")
 
1678
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1679
        # Append some user attrs
 
1680
        array1.attrs.attr1 = "attr1"
 
1681
        array1.attrs.attr2 = 2
 
1682
 
 
1683
        if self.close:
 
1684
            if verbose:
 
1685
                print "(closing file version)"
 
1686
            fileh.close()
 
1687
            fileh = openFile(file, mode = "a")
 
1688
            array1 = fileh.root.array1
 
1689
 
 
1690
        # Copy it to another Array
 
1691
        array2 = array1.copy('/', 'array2', copyuserattrs=1)
 
1692
 
 
1693
        if self.close:
 
1694
            if verbose:
 
1695
                print "(closing file version)"
 
1696
            fileh.close()
 
1697
            fileh = openFile(file, mode = "r")
 
1698
            array1 = fileh.root.array1
 
1699
            array2 = fileh.root.array2
 
1700
 
 
1701
        if verbose:
 
1702
            print "attrs array1-->", repr(array1.attrs)
 
1703
            print "attrs array2-->", repr(array2.attrs)
 
1704
 
 
1705
        # Assert user attributes
 
1706
        array2.attrs.attr1 == "attr1"
 
1707
        array2.attrs.attr2 == 2
 
1708
 
 
1709
        # Close the file
 
1710
        fileh.close()
 
1711
        os.remove(file)
 
1712
 
 
1713
    def test05b_copy(self):
 
1714
        """Checking CArray.copy() method (user attributes not copied)"""
 
1715
 
 
1716
        if verbose:
 
1717
            print '\n', '-=' * 30
 
1718
            print "Running %s.test05b_copy..." % self.__class__.__name__
 
1719
 
 
1720
        # Create an instance of an HDF5 Table
 
1721
        file = tempfile.mktemp(".h5")
 
1722
        fileh = openFile(file, "w")
 
1723
 
 
1724
        # Create an Array
 
1725
        shape = (2,2)
 
1726
        atom=Int16Atom(shape=(2,2))
 
1727
        array1 = fileh.createCArray(fileh.root, 'array1', shape, atom,
 
1728
                                    "title array1")
 
1729
        array1[...] = array([[456, 2],[3, 457]], type=Int16)
 
1730
        # Append some user attrs
 
1731
        array1.attrs.attr1 = "attr1"
 
1732
        array1.attrs.attr2 = 2
 
1733
 
 
1734
        if self.close:
 
1735
            if verbose:
 
1736
                print "(closing file version)"
 
1737
            fileh.close()
 
1738
            fileh = openFile(file, mode = "a")
 
1739
            array1 = fileh.root.array1
 
1740
 
 
1741
        # Copy it to another Array
 
1742
        array2 = array1.copy('/', 'array2', copyuserattrs=0)
 
1743
 
 
1744
        if self.close:
 
1745
            if verbose:
 
1746
                print "(closing file version)"
 
1747
            fileh.close()
 
1748
            fileh = openFile(file, mode = "r")
 
1749
            array1 = fileh.root.array1
 
1750
            array2 = fileh.root.array2
 
1751
 
 
1752
        if verbose:
 
1753
            print "attrs array1-->", repr(array1.attrs)
 
1754
            print "attrs array2-->", repr(array2.attrs)
 
1755
 
 
1756
        # Assert user attributes
 
1757
        hasattr(array2.attrs, "attr1") == 0
 
1758
        hasattr(array2.attrs, "attr2") == 0
 
1759
 
 
1760
        # Close the file
 
1761
        fileh.close()
 
1762
        os.remove(file)
 
1763
 
 
1764
 
 
1765
class CloseCopyTestCase(CopyTestCase):
 
1766
    close = 1
 
1767
 
 
1768
class OpenCopyTestCase(CopyTestCase):
 
1769
    close = 0
 
1770
 
 
1771
class CopyIndexTestCase(unittest.TestCase):
 
1772
    maxTuples = 2
 
1773
 
 
1774
    def test01_index(self):
 
1775
        """Checking CArray.copy() method with indexes"""
 
1776
 
 
1777
        if verbose:
 
1778
            print '\n', '-=' * 30
 
1779
            print "Running %s.test01_index..." % self.__class__.__name__
 
1780
 
 
1781
        # Create an instance of an HDF5 Array
 
1782
        file = tempfile.mktemp(".h5")
 
1783
        fileh = openFile(file, "w")
 
1784
 
 
1785
        # Create an CArray
 
1786
        shape = (100,2)
 
1787
        atom = Int32Atom(shape=(2,2))
 
1788
        array1 = fileh.createCArray(fileh.root, 'array1', shape, atom,
 
1789
                                    "title array1")
 
1790
        r = arange(200, type=Int32, shape=(100,2))
 
1791
        array1[...] = r
 
1792
 
 
1793
        # Select a different buffer size:
 
1794
        array1._v_maxTuples = self.maxTuples
 
1795
 
 
1796
        # Copy to another array
 
1797
        array2 = array1.copy("/", 'array2',
 
1798
                             start=self.start,
 
1799
                             stop=self.stop,
 
1800
                             step=self.step)
 
1801
        if verbose:
 
1802
            print "array1-->", array1.read()
 
1803
            print "array2-->", array2.read()
 
1804
            print "attrs array1-->", repr(array1.attrs)
 
1805
            print "attrs array2-->", repr(array2.attrs)
 
1806
 
 
1807
        # Check that all the elements are equal
 
1808
        r2 = r[self.start:self.stop:self.step]
 
1809
        assert allequal(r2, array2.read())
 
1810
 
 
1811
        # Assert the number of rows in array
 
1812
        if verbose:
 
1813
            print "nrows in array2-->", array2.nrows
 
1814
            print "and it should be-->", r2.shape[0]
 
1815
 
 
1816
        assert array1._v_chunksize == array2._v_chunksize
 
1817
        assert r2.shape[0] == array2.nrows
 
1818
 
 
1819
        # Close the file
 
1820
        fileh.close()
 
1821
        os.remove(file)
 
1822
 
 
1823
    def _test02_indexclosef(self):
 
1824
        """Checking CArray.copy() method with indexes (close file version)"""
 
1825
 
 
1826
        if verbose:
 
1827
            print '\n', '-=' * 30
 
1828
            print "Running %s.test02_indexclosef..." % self.__class__.__name__
 
1829
 
 
1830
        # Create an instance of an HDF5 Array
 
1831
        file = tempfile.mktemp(".h5")
 
1832
        fileh = openFile(file, "w")
 
1833
 
 
1834
        # Create an CArray
 
1835
        shape = (100,2)
 
1836
        atom = Int32Atom(shape=(2,2))
 
1837
        array1 = fileh.createCArray(fileh.root, 'array1', shape, atom,
 
1838
                                    "title array1")
 
1839
        r = arange(200, type=Int32, shape=(100,2))
 
1840
        array1[...] = r
 
1841
 
 
1842
        # Select a different buffer size:
 
1843
        array1._v_maxTuples = self.maxTuples
 
1844
 
 
1845
        # Copy to another array
 
1846
        array2 = array1.copy("/", 'array2',
 
1847
                             start=self.start,
 
1848
                             stop=self.stop,
 
1849
                             step=self.step)
 
1850
        # Close and reopen the file
 
1851
        fileh.close()
 
1852
        fileh = openFile(file, mode = "r")
 
1853
        array1 = fileh.root.array1
 
1854
        array2 = fileh.root.array2
 
1855
 
 
1856
        if verbose:
 
1857
            print "array1-->", array1.read()
 
1858
            print "array2-->", array2.read()
 
1859
            print "attrs array1-->", repr(array1.attrs)
 
1860
            print "attrs array2-->", repr(array2.attrs)
 
1861
 
 
1862
        # Check that all the elements are equal
 
1863
        r2 = r[self.start:self.stop:self.step]
 
1864
        assert array1._v_chunksize == array2._v_chunksize
 
1865
        assert allequal(r2, array2.read())
 
1866
 
 
1867
        # Assert the number of rows in array
 
1868
        if verbose:
 
1869
            print "nrows in array2-->", array2.nrows
 
1870
            print "and it should be-->", r2.shape[0]
 
1871
        assert r2.shape[0] == array2.nrows
 
1872
 
 
1873
        # Close the file
 
1874
        fileh.close()
 
1875
        os.remove(file)
 
1876
 
 
1877
class CopyIndex1TestCase(CopyIndexTestCase):
 
1878
    maxTuples = 1
 
1879
    start = 0
 
1880
    stop = 7
 
1881
    step = 1
 
1882
 
 
1883
class CopyIndex2TestCase(CopyIndexTestCase):
 
1884
    maxTuples = 2
 
1885
    start = 0
 
1886
    stop = -1
 
1887
    step = 1
 
1888
 
 
1889
class CopyIndex3TestCase(CopyIndexTestCase):
 
1890
    maxTuples = 3
 
1891
    start = 1
 
1892
    stop = 7
 
1893
    step = 1
 
1894
 
 
1895
class CopyIndex4TestCase(CopyIndexTestCase):
 
1896
    maxTuples = 4
 
1897
    start = 0
 
1898
    stop = 6
 
1899
    step = 1
 
1900
 
 
1901
class CopyIndex5TestCase(CopyIndexTestCase):
 
1902
    maxTuples = 2
 
1903
    start = 3
 
1904
    stop = 7
 
1905
    step = 1
 
1906
 
 
1907
class CopyIndex6TestCase(CopyIndexTestCase):
 
1908
    maxTuples = 2
 
1909
    start = 3
 
1910
    stop = 6
 
1911
    step = 2
 
1912
 
 
1913
class CopyIndex7TestCase(CopyIndexTestCase):
 
1914
    start = 0
 
1915
    stop = 7
 
1916
    step = 10
 
1917
 
 
1918
class CopyIndex8TestCase(CopyIndexTestCase):
 
1919
    start = 6
 
1920
    stop = -1  # Negative values means starting from the end
 
1921
    step = 1
 
1922
 
 
1923
class CopyIndex9TestCase(CopyIndexTestCase):
 
1924
    start = 3
 
1925
    stop = 4
 
1926
    step = 1
 
1927
 
 
1928
class CopyIndex10TestCase(CopyIndexTestCase):
 
1929
    maxTuples = 1
 
1930
    start = 3
 
1931
    stop = 4
 
1932
    step = 2
 
1933
 
 
1934
class CopyIndex11TestCase(CopyIndexTestCase):
 
1935
    start = -3
 
1936
    stop = -1
 
1937
    step = 2
 
1938
 
 
1939
class CopyIndex12TestCase(CopyIndexTestCase):
 
1940
    start = -1   # Should point to the last element
 
1941
    stop = None  # None should mean the last element (including it)
 
1942
    step = 1
 
1943
 
 
1944
# The next test should be run only in **heavy** mode
 
1945
class Rows64bitsTestCase(unittest.TestCase):
 
1946
    narows = 1000*1000L   # each numarray will have 1 million entries
 
1947
    #narows = 1000L        # for testing only
 
1948
    nanumber = 1000*3L    # That should account for more than 2**31-1
 
1949
 
 
1950
    def setUp(self):
 
1951
 
 
1952
        # Create an instance of an HDF5 Table
 
1953
        self.file = tempfile.mktemp(".h5")
 
1954
        fileh = self.fileh = openFile(self.file, "a")
 
1955
        # Create an CArray
 
1956
        shape = (self.narows*self.nanumber,)
 
1957
        array = fileh.createCArray(fileh.root, 'array', shape, Int8Atom((1024,)),
 
1958
                                   filters=Filters(complib='lzo', complevel=1))
 
1959
 
 
1960
        # Fill the array
 
1961
        na = arange(self.narows, type='Int8')
 
1962
        #~ for i in xrange(self.nanumber):
 
1963
            #~ s = slice(i*self.narows, (i+1)*self.narows)
 
1964
            #~ array[s] = na
 
1965
        s = slice(0, self.narows)
 
1966
        array[s] = na
 
1967
        s = slice((self.nanumber-1)*self.narows, self.nanumber*self.narows)
 
1968
        array[s] = na
 
1969
 
 
1970
 
 
1971
    def tearDown(self):
 
1972
        self.fileh.close()
 
1973
        os.remove(self.file)
 
1974
        cleanup(self)
 
1975
 
 
1976
    #----------------------------------------
 
1977
 
 
1978
    def test01_basiccheck(self):
 
1979
        "Some basic checks for carrays exceeding 2**31 rows"
 
1980
 
 
1981
        fileh = self.fileh
 
1982
        array = fileh.root.array
 
1983
 
 
1984
        if self.close:
 
1985
            if verbose:
 
1986
                # Check how many entries there are in the array
 
1987
                print "Before closing"
 
1988
                print "Entries:", array.nrows, type(array.nrows)
 
1989
                print "Entries:", array.nrows / (1000*1000), "Millions"
 
1990
                print "Shape:", array.shape
 
1991
            # Close the file
 
1992
            fileh.close()
 
1993
            # Re-open the file
 
1994
            fileh = self.fileh = openFile(self.file)
 
1995
            array = fileh.root.array
 
1996
            if verbose:
 
1997
                print "After re-open"
 
1998
 
 
1999
        # Check how many entries there are in the array
 
2000
        if verbose:
 
2001
            print "Entries:", array.nrows, type(array.nrows)
 
2002
            print "Entries:", array.nrows / (1000*1000), "Millions"
 
2003
            print "Shape:", array.shape
 
2004
            print "Last 10 elements-->", array[-10:]
 
2005
            stop = self.narows%256
 
2006
            if stop > 127:
 
2007
                stop -= 256
 
2008
            start = stop - 10
 
2009
            #print "start, stop-->", start, stop
 
2010
            print "Should look like-->", arange(start, stop, type='Int8')
 
2011
 
 
2012
        nrows = self.narows*self.nanumber
 
2013
        # check nrows
 
2014
        assert array.nrows == nrows
 
2015
        # Check shape
 
2016
        assert array.shape == (nrows,)
 
2017
        # check the 10 first elements
 
2018
        assert allequal(array[:10], arange(10, type='Int8'))
 
2019
        # check the 10 last elements
 
2020
        stop = self.narows%256
 
2021
        if stop > 127:
 
2022
            stop -= 256
 
2023
        start = stop - 10
 
2024
        assert allequal(array[-10:], arange(start, stop, type='Int8'))
 
2025
 
 
2026
 
 
2027
class Rows64bitsTestCase1(Rows64bitsTestCase):
 
2028
    close = 0
 
2029
 
 
2030
class Rows64bitsTestCase2(Rows64bitsTestCase):
 
2031
    close = 1
 
2032
 
 
2033
#----------------------------------------------------------------------
 
2034
 
 
2035
 
 
2036
def suite():
 
2037
    theSuite = unittest.TestSuite()
 
2038
    global numeric
 
2039
    niter = 1
 
2040
    #heavy = 1  # uncomment this only for testing purposes
 
2041
 
 
2042
    #theSuite.addTest(unittest.makeSuite(BasicTestCase))
 
2043
    for n in range(niter):
 
2044
        theSuite.addTest(unittest.makeSuite(BasicWriteTestCase))
 
2045
        theSuite.addTest(unittest.makeSuite(BasicWrite2TestCase))
 
2046
        theSuite.addTest(unittest.makeSuite(EmptyCArrayTestCase))
 
2047
        theSuite.addTest(unittest.makeSuite(EmptyCArray2TestCase))
 
2048
        theSuite.addTest(unittest.makeSuite(SlicesCArrayTestCase))
 
2049
        theSuite.addTest(unittest.makeSuite(Slices2CArrayTestCase))
 
2050
        theSuite.addTest(unittest.makeSuite(EllipsisCArrayTestCase))
 
2051
        theSuite.addTest(unittest.makeSuite(Ellipsis2CArrayTestCase))
 
2052
        theSuite.addTest(unittest.makeSuite(Ellipsis3CArrayTestCase))
 
2053
        theSuite.addTest(unittest.makeSuite(ZlibComprTestCase))
 
2054
        theSuite.addTest(unittest.makeSuite(ZlibShuffleTestCase))
 
2055
        theSuite.addTest(unittest.makeSuite(LZOComprTestCase))
 
2056
        theSuite.addTest(unittest.makeSuite(LZOShuffleTestCase))
 
2057
        theSuite.addTest(unittest.makeSuite(BZIP2ComprTestCase))
 
2058
        theSuite.addTest(unittest.makeSuite(BZIP2ShuffleTestCase))
 
2059
        theSuite.addTest(unittest.makeSuite(FloatTypeTestCase))
 
2060
        theSuite.addTest(unittest.makeSuite(ComplexTypeTestCase))
 
2061
        theSuite.addTest(unittest.makeSuite(CharTypeTestCase))
 
2062
        theSuite.addTest(unittest.makeSuite(CharType2TestCase))
 
2063
        theSuite.addTest(unittest.makeSuite(CharTypeComprTestCase))
 
2064
        if numpy_imported:
 
2065
            theSuite.addTest(unittest.makeSuite(NumpyInt8TestCase))
 
2066
            theSuite.addTest(unittest.makeSuite(NumpyInt16TestCase))
 
2067
            theSuite.addTest(unittest.makeSuite(NumpyInt32TestCase))
 
2068
            theSuite.addTest(unittest.makeSuite(NumpyFloat32TestCase))
 
2069
            theSuite.addTest(unittest.makeSuite(NumpyFloat64TestCase))
 
2070
            theSuite.addTest(unittest.makeSuite(NumpyComplex32TestCase))
 
2071
            theSuite.addTest(unittest.makeSuite(NumpyComplex64TestCase))
 
2072
            theSuite.addTest(unittest.makeSuite(NumpyComprTestCase))
 
2073
            theSuite.addTest(unittest.makeSuite(NumpyOffsetStrideTestCase))
 
2074
        if numeric:
 
2075
            theSuite.addTest(unittest.makeSuite(NumericInt8TestCase))
 
2076
            theSuite.addTest(unittest.makeSuite(NumericInt16TestCase))
 
2077
            theSuite.addTest(unittest.makeSuite(NumericInt32TestCase))
 
2078
            theSuite.addTest(unittest.makeSuite(NumericFloat32TestCase))
 
2079
            theSuite.addTest(unittest.makeSuite(NumericFloat64TestCase))
 
2080
            theSuite.addTest(unittest.makeSuite(NumericComplex32TestCase))
 
2081
            theSuite.addTest(unittest.makeSuite(NumericComplex64TestCase))
 
2082
            theSuite.addTest(unittest.makeSuite(NumericComprTestCase))
 
2083
            theSuite.addTest(unittest.makeSuite(NumericOffsetStrideTestCase))
 
2084
 
 
2085
        theSuite.addTest(unittest.makeSuite(OffsetStrideTestCase))
 
2086
        theSuite.addTest(unittest.makeSuite(Fletcher32TestCase))
 
2087
        theSuite.addTest(unittest.makeSuite(AllFiltersTestCase))
 
2088
        theSuite.addTest(unittest.makeSuite(CloseCopyTestCase))
 
2089
        theSuite.addTest(unittest.makeSuite(OpenCopyTestCase))
 
2090
        theSuite.addTest(unittest.makeSuite(CopyIndex1TestCase))
 
2091
        theSuite.addTest(unittest.makeSuite(CopyIndex2TestCase))
 
2092
        theSuite.addTest(unittest.makeSuite(CopyIndex3TestCase))
 
2093
        theSuite.addTest(unittest.makeSuite(CopyIndex4TestCase))
 
2094
        theSuite.addTest(unittest.makeSuite(CopyIndex5TestCase))
 
2095
    if heavy:
 
2096
        theSuite.addTest(unittest.makeSuite(Slices3CArrayTestCase))
 
2097
        theSuite.addTest(unittest.makeSuite(Slices4CArrayTestCase))
 
2098
        theSuite.addTest(unittest.makeSuite(Ellipsis4CArrayTestCase))
 
2099
        theSuite.addTest(unittest.makeSuite(Ellipsis5CArrayTestCase))
 
2100
        theSuite.addTest(unittest.makeSuite(Ellipsis6CArrayTestCase))
 
2101
        theSuite.addTest(unittest.makeSuite(Ellipsis7CArrayTestCase))
 
2102
        theSuite.addTest(unittest.makeSuite(MD3WriteTestCase))
 
2103
        theSuite.addTest(unittest.makeSuite(MD5WriteTestCase))
 
2104
        theSuite.addTest(unittest.makeSuite(MD6WriteTestCase))
 
2105
        theSuite.addTest(unittest.makeSuite(MD7WriteTestCase))
 
2106
        theSuite.addTest(unittest.makeSuite(MD10WriteTestCase))
 
2107
        theSuite.addTest(unittest.makeSuite(CopyIndex6TestCase))
 
2108
        theSuite.addTest(unittest.makeSuite(CopyIndex7TestCase))
 
2109
        theSuite.addTest(unittest.makeSuite(CopyIndex8TestCase))
 
2110
        theSuite.addTest(unittest.makeSuite(CopyIndex9TestCase))
 
2111
        theSuite.addTest(unittest.makeSuite(CopyIndex10TestCase))
 
2112
        theSuite.addTest(unittest.makeSuite(CopyIndex11TestCase))
 
2113
        theSuite.addTest(unittest.makeSuite(CopyIndex12TestCase))
 
2114
        theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase1))
 
2115
        theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase2))
 
2116
 
 
2117
    return theSuite
 
2118
 
 
2119
if __name__ == '__main__':
 
2120
    unittest.main( defaultTest='suite' )
 
2121
 
 
2122
## Local Variables:
 
2123
## mode: python
 
2124
## py-indent-offset: 4
 
2125
## tab-width: 4
 
2126
## End: