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

« back to all changes in this revision

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