1# -*- coding: utf-8 -*- 2 3import os 4import sys 5 6import numpy 7 8import tables 9from tables import Int16Atom, Int32Atom, Float64Atom, StringAtom 10from tables.utils import byteorders 11from tables.tests import common 12from tables.tests.common import allequal 13from tables.tests.common import unittest 14from tables.tests.common import PyTablesTestCase as TestCase 15 16 17class BasicTestCase(common.TempFileMixin, TestCase): 18 # Default values 19 obj = None 20 flavor = "numpy" 21 type = 'int32' 22 dtype = 'int32' 23 shape = (2, 0) 24 start = 0 25 stop = 10 26 step = 1 27 length = 1 28 chunksize = 5 29 nappends = 10 30 compress = 0 31 complib = "zlib" # Default compression library 32 shuffle = 0 33 fletcher32 = 0 34 reopen = 1 # Tells whether the file has to be reopened on each test or not 35 36 def setUp(self): 37 super(BasicTestCase, self).setUp() 38 39 # Create an instance of an HDF5 Table 40 self.rootgroup = self.h5file.root 41 self.populateFile() 42 if self.reopen: 43 # Close the file 44 self.h5file.close() 45 46 def populateFile(self): 47 group = self.rootgroup 48 obj = self.obj 49 if obj is None: 50 if self.type == "string": 51 atom = StringAtom(itemsize=self.length) 52 else: 53 atom = tables.Atom.from_type(self.type) 54 else: 55 atom = None 56 title = self.__class__.__name__ 57 filters = tables.Filters(complevel=self.compress, 58 complib=self.complib, 59 shuffle=self.shuffle, 60 fletcher32=self.fletcher32) 61 earray = self.h5file.create_earray(group, 'earray1', 62 atom=atom, shape=self.shape, 63 title=title, filters=filters, 64 expectedrows=1, obj=obj) 65 earray.flavor = self.flavor 66 67 # Fill it with rows 68 self.rowshape = list(earray.shape) 69 if obj is not None: 70 self.rowshape[0] = 0 71 self.objsize = self.length 72 for i in self.rowshape: 73 if i != 0: 74 self.objsize *= i 75 self.extdim = earray.extdim 76 self.objsize *= self.chunksize 77 self.rowshape[earray.extdim] = self.chunksize 78 79 if self.type == "string": 80 object = numpy.ndarray(buffer=b"a"*self.objsize, 81 shape=self.rowshape, 82 dtype="S%s" % earray.atom.itemsize) 83 else: 84 object = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 85 object.shape = self.rowshape 86 87 if common.verbose: 88 if self.flavor == "numpy": 89 print("Object to append -->", object) 90 else: 91 print("Object to append -->", repr(object)) 92 for i in range(self.nappends): 93 if self.type == "string": 94 earray.append(object) 95 else: 96 earray.append(object * i) 97 98 def _get_shape(self): 99 if self.shape is not None: 100 shape = self.shape 101 else: 102 shape = numpy.asarray(self.obj).shape 103 104 return shape 105 106 def test00_attributes(self): 107 if self.reopen: 108 self._reopen() 109 obj = self.h5file.get_node("/earray1") 110 111 shape = self._get_shape() 112 shape = list(shape) 113 shape[self.extdim] = self.chunksize * self.nappends 114 if self.obj is not None: 115 shape[self.extdim] += len(self.obj) 116 shape = tuple(shape) 117 118 self.assertEqual(obj.flavor, self.flavor) 119 self.assertEqual(obj.shape, shape) 120 self.assertEqual(obj.ndim, len(shape)) 121 self.assertEqual(obj.nrows, shape[self.extdim]) 122 self.assertEqual(obj.atom.type, self.type) 123 124 def test01_iterEArray(self): 125 """Checking enlargeable array iterator.""" 126 127 if common.verbose: 128 print('\n', '-=' * 30) 129 print("Running %s.test01_iterEArray..." % self.__class__.__name__) 130 131 # Create an instance of an HDF5 Table 132 if self.reopen: 133 self._reopen() 134 earray = self.h5file.get_node("/earray1") 135 136 # Choose a small value for buffer size 137 earray.nrowsinbuf = 3 138 if common.verbose: 139 print("EArray descr:", repr(earray)) 140 print("shape of read array ==>", earray.shape) 141 print("reopening?:", self.reopen) 142 143 # Build the array to do comparisons 144 if self.type == "string": 145 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 146 shape=self.rowshape, 147 dtype="S%s" % earray.atom.itemsize) 148 else: 149 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 150 object_.shape = self.rowshape 151 object_ = object_.swapaxes(earray.extdim, 0) 152 153 if self.obj is not None: 154 initialrows = len(self.obj) 155 else: 156 initialrows = 0 157 158 shape = self._get_shape() 159 160 # Read all the array 161 for idx, row in enumerate(earray): 162 if idx < initialrows: 163 self.assertTrue( 164 allequal(row, numpy.asarray(self.obj[idx]), self.flavor)) 165 continue 166 167 chunk = int((earray.nrow - initialrows) % self.chunksize) 168 if chunk == 0: 169 if self.type == "string": 170 object__ = object_ 171 else: 172 i = int(earray.nrow - initialrows) 173 object__ = object_ * (i // self.chunksize) 174 175 object = object__[chunk] 176 # The next adds much more verbosity 177 if common.verbose and 0: 178 print("number of row ==>", earray.nrow) 179 if hasattr(object, "shape"): 180 print("shape should look as:", object.shape) 181 print("row in earray ==>", repr(row)) 182 print("Should look like ==>", repr(object)) 183 184 self.assertEqual(initialrows + self.nappends * self.chunksize, 185 earray.nrows) 186 self.assertTrue(allequal(row, object, self.flavor)) 187 if hasattr(row, "shape"): 188 self.assertEqual(len(row.shape), len(shape) - 1) 189 else: 190 # Scalar case 191 self.assertEqual(len(shape), 1) 192 193 # Check filters: 194 if self.compress != earray.filters.complevel and common.verbose: 195 print("Error in compress. Class:", self.__class__.__name__) 196 print("self, earray:", self.compress, earray.filters.complevel) 197 self.assertEqual(earray.filters.complevel, self.compress) 198 if self.compress > 0 and tables.which_lib_version(self.complib): 199 self.assertEqual(earray.filters.complib, self.complib) 200 if self.shuffle != earray.filters.shuffle and common.verbose: 201 print("Error in shuffle. Class:", self.__class__.__name__) 202 print("self, earray:", self.shuffle, earray.filters.shuffle) 203 self.assertEqual(self.shuffle, earray.filters.shuffle) 204 if self.fletcher32 != earray.filters.fletcher32 and common.verbose: 205 print("Error in fletcher32. Class:", self.__class__.__name__) 206 print("self, earray:", self.fletcher32, 207 earray.filters.fletcher32) 208 self.assertEqual(self.fletcher32, earray.filters.fletcher32) 209 210 def test02_sssEArray(self): 211 """Checking enlargeable array iterator with (start, stop, step)""" 212 213 if common.verbose: 214 print('\n', '-=' * 30) 215 print("Running %s.test02_sssEArray..." % self.__class__.__name__) 216 217 # Create an instance of an HDF5 Table 218 if self.reopen: 219 self._reopen() 220 earray = self.h5file.get_node("/earray1") 221 222 # Choose a small value for buffer size 223 earray.nrowsinbuf = 3 224 if common.verbose: 225 print("EArray descr:", repr(earray)) 226 print("shape of read array ==>", earray.shape) 227 print("reopening?:", self.reopen) 228 229 # Build the array to do comparisons 230 if self.type == "string": 231 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 232 shape=self.rowshape, 233 dtype="S%s" % earray.atom.itemsize) 234 else: 235 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 236 object_.shape = self.rowshape 237 object_ = object_.swapaxes(earray.extdim, 0) 238 239 if self.obj is not None: 240 initialrows = len(self.obj) 241 else: 242 initialrows = 0 243 244 shape = self._get_shape() 245 246 # Read all the array 247 for idx, row in enumerate(earray.iterrows(start=self.start, 248 stop=self.stop, 249 step=self.step)): 250 if idx < initialrows: 251 self.assertTrue( 252 allequal(row, numpy.asarray(self.obj[idx]), self.flavor)) 253 continue 254 255 if self.chunksize == 1: 256 index = 0 257 else: 258 index = int((earray.nrow - initialrows) % self.chunksize) 259 260 if self.type == "string": 261 object__ = object_ 262 else: 263 i = int(earray.nrow - initialrows) 264 object__ = object_ * (i // self.chunksize) 265 object = object__[index] 266 267 # The next adds much more verbosity 268 if common.verbose and 0: 269 print("number of row ==>", earray.nrow) 270 if hasattr(object, "shape"): 271 print("shape should look as:", object.shape) 272 print("row in earray ==>", repr(row)) 273 print("Should look like ==>", repr(object)) 274 275 self.assertEqual(initialrows + self.nappends * self.chunksize, 276 earray.nrows) 277 self.assertTrue(allequal(row, object, self.flavor)) 278 if hasattr(row, "shape"): 279 self.assertEqual(len(row.shape), len(shape) - 1) 280 else: 281 # Scalar case 282 self.assertEqual(len(shape), 1) 283 284 def test03_readEArray(self): 285 """Checking read() of enlargeable arrays.""" 286 287 if common.verbose: 288 print('\n', '-=' * 30) 289 print("Running %s.test03_readEArray..." % self.__class__.__name__) 290 291 # This conversion made just in case indices are numpy scalars 292 if self.start is not None: 293 self.start = int(self.start) 294 if self.stop is not None: 295 self.stop = int(self.stop) 296 if self.step is not None: 297 self.step = int(self.step) 298 299 # Create an instance of an HDF5 Table 300 if self.reopen: 301 self._reopen() 302 earray = self.h5file.get_node("/earray1") 303 304 # Choose a small value for buffer size 305 earray.nrowsinbuf = 3 306 if common.verbose: 307 print("EArray descr:", repr(earray)) 308 print("shape of read array ==>", earray.shape) 309 print("reopening?:", self.reopen) 310 311 # Build the array to do comparisons 312 if self.type == "string": 313 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 314 shape=self.rowshape, 315 dtype="S%s" % earray.atom.itemsize) 316 else: 317 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 318 object_.shape = self.rowshape 319 object_ = object_.swapaxes(earray.extdim, 0) 320 321 if self.obj is not None: 322 initialrows = len(self.obj) 323 else: 324 initialrows = 0 325 326 rowshape = self.rowshape 327 rowshape[self.extdim] *= (self.nappends + initialrows) 328 if self.type == "string": 329 object__ = numpy.empty( 330 shape=rowshape, dtype="S%s" % earray.atom.itemsize) 331 else: 332 object__ = numpy.empty(shape=rowshape, dtype=self.dtype) 333 334 object__ = object__.swapaxes(0, self.extdim) 335 336 if initialrows: 337 object__[0:initialrows] = self.obj 338 339 for i in range(self.nappends): 340 j = initialrows + i * self.chunksize 341 if self.type == "string": 342 object__[j:j + self.chunksize] = object_ 343 else: 344 object__[j:j + self.chunksize] = object_ * i 345 346 stop = self.stop 347 348 if self.nappends: 349 # stop == None means read only the element designed by start 350 # (in read() contexts) 351 if self.stop is None: 352 if self.start == -1: # corner case 353 stop = earray.nrows 354 else: 355 stop = self.start + 1 356 # Protection against number of elements less than existing 357 # if rowshape[self.extdim] < self.stop or self.stop == 0: 358 if rowshape[self.extdim] < stop: 359 # self.stop == 0 means last row only in read() 360 # and not in [::] slicing notation 361 stop = rowshape[self.extdim] 362 # do a copy() in order to ensure that len(object._data) 363 # actually do a measure of its length 364 #object = object__[self.start:stop:self.step].copy() 365 object = object__[self.start:self.stop:self.step].copy() 366 # Swap the axes again to have normal ordering 367 if self.flavor == "numpy": 368 object = object.swapaxes(0, self.extdim) 369 else: 370 object = numpy.empty(shape=self.shape, dtype=self.dtype) 371 372 # Read all the array 373 try: 374 row = earray.read(self.start, self.stop, self.step) 375 except IndexError: 376 row = numpy.empty(shape=self.shape, dtype=self.dtype) 377 378 if common.verbose: 379 if hasattr(object, "shape"): 380 print("shape should look as:", object.shape) 381 print("Object read ==>", repr(row)) 382 print("Should look like ==>", repr(object)) 383 384 self.assertEqual(initialrows + self.nappends * self.chunksize, 385 earray.nrows) 386 self.assertTrue(allequal(row, object, self.flavor)) 387 388 shape = self._get_shape() 389 if hasattr(row, "shape"): 390 self.assertEqual(len(row.shape), len(shape)) 391 if self.flavor == "numpy": 392 self.assertEqual(row.itemsize, earray.atom.itemsize) 393 else: 394 # Scalar case 395 self.assertEqual(len(shape), 1) 396 397 def test03_readEArray_out_argument(self): 398 """Checking read() of enlargeable arrays.""" 399 400 # This conversion made just in case indices are numpy scalars 401 if self.start is not None: 402 self.start = int(self.start) 403 if self.stop is not None: 404 self.stop = int(self.stop) 405 if self.step is not None: 406 self.step = int(self.step) 407 408 # Create an instance of an HDF5 Table 409 if self.reopen: 410 self._reopen() 411 earray = self.h5file.get_node("/earray1") 412 413 # Choose a small value for buffer size 414 earray.nrowsinbuf = 3 415 # Build the array to do comparisons 416 if self.type == "string": 417 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 418 shape=self.rowshape, 419 dtype="S%s" % earray.atom.itemsize) 420 else: 421 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 422 object_.shape = self.rowshape 423 object_ = object_.swapaxes(earray.extdim, 0) 424 425 if self.obj is not None: 426 initialrows = len(self.obj) 427 else: 428 initialrows = 0 429 430 rowshape = self.rowshape 431 rowshape[self.extdim] *= (self.nappends + initialrows) 432 if self.type == "string": 433 object__ = numpy.empty( 434 shape=rowshape, dtype="S%s" % earray.atom.itemsize) 435 else: 436 object__ = numpy.empty(shape=rowshape, dtype=self.dtype) 437 438 object__ = object__.swapaxes(0, self.extdim) 439 440 if initialrows: 441 object__[0:initialrows] = self.obj 442 443 for i in range(self.nappends): 444 j = initialrows + i * self.chunksize 445 if self.type == "string": 446 object__[j:j + self.chunksize] = object_ 447 else: 448 object__[j:j + self.chunksize] = object_ * i 449 450 stop = self.stop 451 452 if self.nappends: 453 # stop == None means read only the element designed by start 454 # (in read() contexts) 455 if self.stop is None: 456 if self.start == -1: # corner case 457 stop = earray.nrows 458 else: 459 stop = self.start + 1 460 # Protection against number of elements less than existing 461 # if rowshape[self.extdim] < self.stop or self.stop == 0: 462 if rowshape[self.extdim] < stop: 463 # self.stop == 0 means last row only in read() 464 # and not in [::] slicing notation 465 stop = rowshape[self.extdim] 466 # do a copy() in order to ensure that len(object._data) 467 # actually do a measure of its length 468 #object = object__[self.start:stop:self.step].copy() 469 object = object__[self.start:self.stop:self.step].copy() 470 # Swap the axes again to have normal ordering 471 if self.flavor == "numpy": 472 object = object.swapaxes(0, self.extdim) 473 else: 474 object = numpy.empty(shape=self.shape, dtype=self.dtype) 475 476 # Read all the array 477 try: 478 row = numpy.empty(earray.shape, dtype=earray.atom.dtype) 479 slice_obj = [slice(None)] * len(earray.shape) 480 #slice_obj[earray.maindim] = slice(self.start, stop, self.step) 481 slice_obj[earray.maindim] = slice(self.start, self.stop, self.step) 482 row = row[tuple(slice_obj)].copy() 483 earray.read(self.start, self.stop, self.step, out=row) 484 except IndexError: 485 row = numpy.empty(shape=self.shape, dtype=self.dtype) 486 487 if common.verbose: 488 if hasattr(object, "shape"): 489 print("shape should look as:", object.shape) 490 print("Object read ==>", repr(row)) 491 print("Should look like ==>", repr(object)) 492 493 self.assertEqual(initialrows + self.nappends * self.chunksize, 494 earray.nrows) 495 self.assertTrue(allequal(row, object, self.flavor)) 496 497 shape = self._get_shape() 498 if hasattr(row, "shape"): 499 self.assertEqual(len(row.shape), len(shape)) 500 if self.flavor == "numpy": 501 self.assertEqual(row.itemsize, earray.atom.itemsize) 502 else: 503 # Scalar case 504 self.assertEqual(len(shape), 1) 505 506 def test04_getitemEArray(self): 507 """Checking enlargeable array __getitem__ special method.""" 508 509 if common.verbose: 510 print('\n', '-=' * 30) 511 print("Running %s.test04_getitemEArray..." % 512 self.__class__.__name__) 513 514 if not hasattr(self, "slices"): 515 # If there is not a slices attribute, create it 516 # This conversion made just in case indices are numpy scalars 517 if self.start is not None: 518 self.start = int(self.start) 519 if self.stop is not None: 520 self.stop = int(self.stop) 521 if self.step is not None: 522 self.step = int(self.step) 523 self.slices = (slice(self.start, self.stop, self.step),) 524 525 # Create an instance of an HDF5 Table 526 if self.reopen: 527 self._reopen() 528 earray = self.h5file.get_node("/earray1") 529 530 # Choose a small value for buffer size 531 # earray.nrowsinbuf = 3 # this does not really changes the chunksize 532 if common.verbose: 533 print("EArray descr:", repr(earray)) 534 print("shape of read array ==>", earray.shape) 535 print("reopening?:", self.reopen) 536 537 # Build the array to do comparisons 538 if self.type == "string": 539 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 540 shape=self.rowshape, 541 dtype="S%s" % earray.atom.itemsize) 542 else: 543 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 544 object_.shape = self.rowshape 545 546 object_ = object_.swapaxes(earray.extdim, 0) 547 548 if self.obj is not None: 549 initialrows = len(self.obj) 550 else: 551 initialrows = 0 552 553 rowshape = self.rowshape 554 rowshape[self.extdim] *= (self.nappends + initialrows) 555 if self.type == "string": 556 object__ = numpy.empty( 557 shape=rowshape, dtype="S%s" % earray.atom.itemsize) 558 else: 559 object__ = numpy.empty(shape=rowshape, dtype=self.dtype) 560 # Additional conversion for the numpy case 561 object__ = object__.swapaxes(0, earray.extdim) 562 563 if initialrows: 564 object__[0:initialrows] = self.obj 565 566 for i in range(self.nappends): 567 j = initialrows + i * self.chunksize 568 if self.type == "string": 569 object__[j:j + self.chunksize] = object_ 570 else: 571 object__[j:j + self.chunksize] = object_ * i 572 573 if self.nappends: 574 # Swap the axes again to have normal ordering 575 if self.flavor == "numpy": 576 object__ = object__.swapaxes(0, self.extdim) 577 else: 578 object__.swapaxes(0, self.extdim) 579 # do a copy() in order to ensure that len(object._data) 580 # actually do a measure of its length 581 object = object__.__getitem__(self.slices).copy() 582 else: 583 object = numpy.empty(shape=self.shape, dtype=self.dtype) 584 585 # Read all the array 586 try: 587 row = earray.__getitem__(self.slices) 588 except IndexError: 589 row = numpy.empty(shape=self.shape, dtype=self.dtype) 590 591 if common.verbose: 592 print("Object read:\n", repr(row)) 593 print("Should look like:\n", repr(object)) 594 if hasattr(object, "shape"): 595 print("Original object shape:", self.shape) 596 print("Shape read:", row.shape) 597 print("shape should look as:", object.shape) 598 599 self.assertEqual(initialrows + self.nappends * self.chunksize, 600 earray.nrows) 601 self.assertTrue(allequal(row, object, self.flavor)) 602 if not hasattr(row, "shape"): 603 # Scalar case 604 self.assertEqual(len(self.shape), 1) 605 606 def test05_setitemEArray(self): 607 """Checking enlargeable array __setitem__ special method.""" 608 609 if self.__class__.__name__ == "Ellipsis6EArrayTestCase": 610 # We have a problem with test design here, but I think 611 # it is not worth the effort to solve it 612 # F.Alted 2004-10-27 613 return 614 615 if common.verbose: 616 print('\n', '-=' * 30) 617 print("Running %s.test05_setitemEArray..." % 618 self.__class__.__name__) 619 620 if not hasattr(self, "slices"): 621 # If there is not a slices attribute, create it 622 # This conversion made just in case indices are numpy scalars 623 if self.start is not None: 624 self.start = int(self.start) 625 if self.stop is not None: 626 self.stop = int(self.stop) 627 if self.step is not None: 628 self.step = int(self.step) 629 self.slices = (slice(self.start, self.stop, self.step),) 630 631 # Create an instance of an HDF5 Table 632 if self.reopen: 633 self._reopen(mode="a") 634 earray = self.h5file.get_node("/earray1") 635 636 # Choose a small value for buffer size 637 # earray.nrowsinbuf = 3 # this does not really changes the chunksize 638 if common.verbose: 639 print("EArray descr:", repr(earray)) 640 print("shape of read array ==>", earray.shape) 641 print("reopening?:", self.reopen) 642 643 # Build the array to do comparisons 644 if self.type == "string": 645 object_ = numpy.ndarray(buffer=b"a"*self.objsize, 646 shape=self.rowshape, 647 dtype="S%s" % earray.atom.itemsize) 648 else: 649 object_ = numpy.arange(self.objsize, dtype=earray.atom.dtype.base) 650 object_.shape = self.rowshape 651 652 object_ = object_.swapaxes(earray.extdim, 0) 653 654 if self.obj is not None: 655 initialrows = len(self.obj) 656 else: 657 initialrows = 0 658 659 rowshape = self.rowshape 660 rowshape[self.extdim] *= (self.nappends + initialrows) 661 if self.type == "string": 662 object__ = numpy.empty( 663 shape=rowshape, dtype="S%s" % earray.atom.itemsize) 664 else: 665 object__ = numpy.empty(shape=rowshape, dtype=self.dtype) 666 # Additional conversion for the numpy case 667 object__ = object__.swapaxes(0, earray.extdim) 668 669 for i in range(self.nappends): 670 j = initialrows + i * self.chunksize 671 if self.type == "string": 672 object__[j:j + self.chunksize] = object_ 673 else: 674 object__[j:j + self.chunksize] = object_ * i 675 # Modify the earray 676 # earray[j:j + self.chunksize] = object_ * i 677 # earray[self.slices] = 1 678 679 if initialrows: 680 object__[0:initialrows] = self.obj 681 682 if self.nappends: 683 # Swap the axes again to have normal ordering 684 if self.flavor == "numpy": 685 object__ = object__.swapaxes(0, self.extdim) 686 else: 687 object__.swapaxes(0, self.extdim) 688 # do a copy() in order to ensure that len(object._data) 689 # actually do a measure of its length 690 object = object__.__getitem__(self.slices).copy() 691 else: 692 object = numpy.empty(shape=self.shape, dtype=self.dtype) 693 694 if self.flavor == "numpy": 695 object = numpy.asarray(object) 696 697 if self.type == "string": 698 if hasattr(self, "wslice"): 699 # These sentences should be equivalent 700 # object[self.wslize] = object[self.wslice].pad("xXx") 701 # earray[self.wslice] = earray[self.wslice].pad("xXx") 702 object[self.wslize] = "xXx" 703 earray[self.wslice] = "xXx" 704 elif sum(object[self.slices].shape) != 0: 705 # object[:] = object.pad("xXx") 706 object[:] = "xXx" 707 if object.size > 0: 708 earray[self.slices] = object 709 else: 710 if hasattr(self, "wslice"): 711 object[self.wslice] = object[self.wslice] * 2 + 3 712 earray[self.wslice] = earray[self.wslice] * 2 + 3 713 elif sum(object[self.slices].shape) != 0: 714 object = object * 2 + 3 715 if numpy.prod(object.shape) > 0: 716 earray[self.slices] = earray[self.slices] * 2 + 3 717 # Read all the array 718 row = earray.__getitem__(self.slices) 719 try: 720 row = earray.__getitem__(self.slices) 721 except IndexError: 722 print("IndexError!") 723 row = numpy.empty(shape=self.shape, dtype=self.dtype) 724 725 if common.verbose: 726 print("Object read:\n", repr(row)) 727 print("Should look like:\n", repr(object)) 728 if hasattr(object, "shape"): 729 print("Original object shape:", self.shape) 730 print("Shape read:", row.shape) 731 print("shape should look as:", object.shape) 732 733 self.assertEqual(initialrows + self.nappends * self.chunksize, 734 earray.nrows) 735 self.assertTrue(allequal(row, object, self.flavor)) 736 if not hasattr(row, "shape"): 737 # Scalar case 738 self.assertEqual(len(self.shape), 1) 739 740 741class BasicWriteTestCase(BasicTestCase): 742 type = 'int32' 743 shape = (0,) 744 chunksize = 5 745 nappends = 10 746 step = 1 747 # wslice = slice(1,nappends,2) 748 wslice = 1 # single element case 749 750 751class Basic2WriteTestCase(BasicTestCase): 752 type = 'int32' 753 dtype = 'i4' 754 shape = (0,) 755 chunksize = 5 756 nappends = 10 757 step = 1 758 wslice = slice(chunksize-2, nappends, 2) # range of elements 759 reopen = 0 # This case does not reopen files 760 761 762class Basic3WriteTestCase(BasicTestCase): 763 obj = [1, 2] 764 type = numpy.asarray(obj).dtype.name 765 dtype = numpy.asarray(obj).dtype.str 766 shape = (0,) 767 chunkshape = (5,) 768 step = 1 769 reopen = 0 # This case does not reopen files 770 771 772class Basic4WriteTestCase(BasicTestCase): 773 obj = numpy.array([1, 2]) 774 type = obj.dtype.name 775 dtype = obj.dtype.str 776 shape = None 777 chunkshape = (5,) 778 step = 1 779 reopen = 0 # This case does not reopen files 780 781 782class Basic5WriteTestCase(BasicTestCase): 783 obj = [1, 2] 784 type = numpy.asarray(obj).dtype.name 785 dtype = numpy.asarray(obj).dtype.str 786 shape = (0,) 787 chunkshape = (5,) 788 step = 1 789 reopen = 1 # This case does reopen files 790 791 792class Basic6WriteTestCase(BasicTestCase): 793 obj = numpy.array([1, 2]) 794 type = obj.dtype.name 795 dtype = obj.dtype.str 796 shape = None 797 chunkshape = (5,) 798 step = 1 799 reopen = 1 # This case does reopen files 800 801 802class Basic7WriteTestCase(BasicTestCase): 803 obj = [[1, 2], [3, 4]] 804 type = numpy.asarray(obj).dtype.name 805 dtype = numpy.asarray(obj).dtype.str 806 shape = (0, 2) 807 chunkshape = (5,) 808 step = 1 809 reopen = 0 # This case does not reopen files 810 811 812class Basic8WriteTestCase(BasicTestCase): 813 obj = [[1, 2], [3, 4]] 814 type = numpy.asarray(obj).dtype.name 815 dtype = numpy.asarray(obj).dtype.str 816 shape = (0, 2) 817 chunkshape = (5,) 818 step = 1 819 reopen = 1 # This case does reopen files 820 821 822class EmptyEArrayTestCase(BasicTestCase): 823 type = 'int32' 824 dtype = numpy.dtype('int32') 825 shape = (2, 0) 826 chunksize = 5 827 nappends = 0 828 start = 0 829 stop = 10 830 step = 1 831 832 833class NP_EmptyEArrayTestCase(BasicTestCase): 834 type = 'int32' 835 dtype = numpy.dtype('()int32') 836 shape = (2, 0) 837 chunksize = 5 838 nappends = 0 839 840 841class Empty2EArrayTestCase(BasicTestCase): 842 type = 'int32' 843 dtype = 'int32' 844 shape = (2, 0) 845 chunksize = 5 846 nappends = 0 847 start = 0 848 stop = 10 849 step = 1 850 reopen = 0 # This case does not reopen files 851 852 853@unittest.skipIf(not common.lzo_avail, 'LZO compression library not available') 854class SlicesEArrayTestCase(BasicTestCase): 855 compress = 1 856 complib = "lzo" 857 type = 'int32' 858 shape = (2, 0) 859 chunksize = 5 860 nappends = 2 861 slices = (slice(1, 2, 1), slice(1, 3, 1)) 862 863 864@unittest.skipIf(not common.blosc_avail, 865 'BLOSC compression library not available') 866class Slices2EArrayTestCase(BasicTestCase): 867 compress = 1 868 complib = "blosc" 869 type = 'int32' 870 shape = (2, 0, 4) 871 chunksize = 5 872 nappends = 20 873 slices = (slice(1, 2, 1), slice(None, None, None), slice(1, 4, 2)) 874 875 876class EllipsisEArrayTestCase(BasicTestCase): 877 type = 'int32' 878 shape = (2, 0) 879 chunksize = 5 880 nappends = 2 881 # slices = (slice(1,2,1), Ellipsis) 882 slices = (Ellipsis, slice(1, 2, 1)) 883 884 885class Ellipsis2EArrayTestCase(BasicTestCase): 886 type = 'int32' 887 shape = (2, 0, 4) 888 chunksize = 5 889 nappends = 20 890 slices = (slice(1, 2, 1), Ellipsis, slice(1, 4, 2)) 891 892 893@unittest.skipIf(not common.blosc_avail, 894 'BLOSC compression library not available') 895class Slices3EArrayTestCase(BasicTestCase): 896 compress = 1 # To show the chunks id DEBUG is on 897 complib = "blosc" 898 type = 'int32' 899 shape = (2, 3, 4, 0) 900 chunksize = 5 901 nappends = 20 902 slices = (slice(1, 2, 1), slice(0, None, None), 903 slice(1, 4, 2)) # Don't work 904 # slices = (slice(None, None, None), slice(0, None, None), 905 # slice(1,4,1)) # W 906 # slices = (slice(None, None, None), slice(None, None, None), 907 # slice(1,4,2)) # N 908 # slices = (slice(1,2,1), slice(None, None, None), slice(1,4,2)) # N 909 # Disable the failing test temporarily with a working test case 910 slices = (slice(1, 2, 1), slice(1, 4, None), slice(1, 4, 2)) # Y 911 # slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,1)) # Y 912 slices = (slice(1, 2, 1), slice(0, 4, None), slice(1, 4, 2)) # N 913 # slices = (slice(1,2,1), slice(0, 4, None), slice(1,4,2), 914 # slice(0,100,1)) # N 915 916 917class Slices4EArrayTestCase(BasicTestCase): 918 type = 'int32' 919 shape = (2, 3, 4, 0, 5, 6) 920 chunksize = 5 921 nappends = 20 922 slices = (slice(1, 2, 1), slice(0, None, None), slice(1, 4, 2), 923 slice(0, 4, 2), slice(3, 5, 2), slice(2, 7, 1)) 924 925 926class Ellipsis3EArrayTestCase(BasicTestCase): 927 type = 'int32' 928 shape = (2, 3, 4, 0) 929 chunksize = 5 930 nappends = 20 931 slices = (Ellipsis, slice(0, 4, None), slice(1, 4, 2)) 932 slices = (slice(1, 2, 1), slice(0, 4, None), slice(1, 4, 2), Ellipsis) 933 934 935class Ellipsis4EArrayTestCase(BasicTestCase): 936 type = 'int32' 937 shape = (2, 3, 4, 0) 938 chunksize = 5 939 nappends = 20 940 slices = (Ellipsis, slice(0, 4, None), slice(1, 4, 2)) 941 slices = (slice(1, 2, 1), Ellipsis, slice(1, 4, 2)) 942 943 944class Ellipsis5EArrayTestCase(BasicTestCase): 945 type = 'int32' 946 shape = (2, 3, 4, 0) 947 chunksize = 5 948 nappends = 20 949 slices = (slice(1, 2, 1), slice(0, 4, None), Ellipsis) 950 951 952class Ellipsis6EArrayTestCase(BasicTestCase): 953 type = 'int32' 954 shape = (2, 3, 4, 0) 955 chunksize = 5 956 nappends = 2 957 # The next slices gives problems with setting values (test05) 958 # This is a problem on the test design, not the Array.__setitem__ 959 # code, though. 960 slices = (slice(1, 2, 1), slice(0, 4, None), 2, Ellipsis) 961 962 963class Ellipsis7EArrayTestCase(BasicTestCase): 964 type = 'int32' 965 shape = (2, 3, 4, 0) 966 chunksize = 5 967 nappends = 2 968 slices = (slice(1, 2, 1), slice(0, 4, None), slice(2, 3), Ellipsis) 969 970 971class MD3WriteTestCase(BasicTestCase): 972 type = 'int32' 973 shape = (2, 0, 3) 974 chunksize = 4 975 step = 2 976 977 978class MD5WriteTestCase(BasicTestCase): 979 type = 'int32' 980 shape = (2, 0, 3, 4, 5) # ok 981 # shape = (1, 1, 0, 1) # Minimum shape that shows problems with HDF5 1.6.1 982 # shape = (2, 3, 0, 4, 5) # Floating point exception (HDF5 1.6.1) 983 # shape = (2, 3, 3, 0, 5, 6) # Segmentation fault (HDF5 1.6.1) 984 chunksize = 1 985 nappends = 1 986 start = 1 987 stop = 10 988 step = 10 989 990 991class MD6WriteTestCase(BasicTestCase): 992 type = 'int32' 993 shape = (2, 3, 3, 0, 5, 6) 994 chunksize = 1 995 nappends = 10 996 start = 1 997 stop = 10 998 step = 3 999 1000 1001class NP_MD6WriteTestCase(BasicTestCase): 1002 "Testing NumPy scalars as indexes" 1003 type = 'int32' 1004 shape = (2, 3, 3, 0, 5, 6) 1005 chunksize = 1 1006 nappends = 10 1007 1008 1009class MD6WriteTestCase__(BasicTestCase): 1010 type = 'int32' 1011 shape = (2, 0) 1012 chunksize = 1 1013 nappends = 3 1014 start = 1 1015 stop = 3 1016 step = 1 1017 1018 1019class MD7WriteTestCase(BasicTestCase): 1020 type = 'int32' 1021 shape = (2, 3, 3, 4, 5, 0, 3) 1022 chunksize = 10 1023 nappends = 1 1024 start = 1 1025 stop = 10 1026 step = 2 1027 1028 1029class MD10WriteTestCase(BasicTestCase): 1030 type = 'int32' 1031 shape = (1, 2, 3, 4, 5, 5, 4, 3, 2, 0) 1032 chunksize = 5 1033 nappends = 10 1034 start = -1 1035 stop = -1 1036 step = 10 1037 1038 1039class NP_MD10WriteTestCase(BasicTestCase): 1040 type = 'int32' 1041 shape = (1, 2, 3, 4, 5, 5, 4, 3, 2, 0) 1042 chunksize = 5 1043 nappends = 10 1044 1045 1046class ZlibComprTestCase(BasicTestCase): 1047 compress = 1 1048 complib = "zlib" 1049 start = 3 1050 # stop = 0 # means last row 1051 stop = None # means last row from 0.8 on 1052 step = 10 1053 1054 1055class ZlibShuffleTestCase(BasicTestCase): 1056 shuffle = 1 1057 compress = 1 1058 complib = "zlib" 1059 # case start < stop , i.e. no rows read 1060 start = 3 1061 stop = 1 1062 step = 10 1063 1064 1065@unittest.skipIf(not common.blosc_avail, 1066 'BLOSC compression library not available') 1067class BloscComprTestCase(BasicTestCase): 1068 compress = 1 # sss 1069 complib = "blosc" 1070 chunksize = 10 1071 nappends = 100 1072 start = 3 1073 stop = 10 1074 step = 3 1075 1076 1077@unittest.skipIf(not common.blosc_avail, 1078 'BLOSC compression library not available') 1079class BloscShuffleTestCase(BasicTestCase): 1080 compress = 1 1081 shuffle = 1 1082 complib = "blosc" 1083 chunksize = 100 1084 nappends = 10 1085 start = 3 1086 stop = 10 1087 step = 7 1088 1089 1090@unittest.skipIf(not common.lzo_avail, 'LZO compression library not available') 1091class LZOComprTestCase(BasicTestCase): 1092 compress = 1 # sss 1093 complib = "lzo" 1094 chunksize = 10 1095 nappends = 100 1096 start = 3 1097 stop = 10 1098 step = 3 1099 1100 1101@unittest.skipIf(not common.lzo_avail, 'LZO compression library not available') 1102class LZOShuffleTestCase(BasicTestCase): 1103 compress = 1 1104 shuffle = 1 1105 complib = "lzo" 1106 chunksize = 100 1107 nappends = 10 1108 start = 3 1109 stop = 10 1110 step = 7 1111 1112 1113@unittest.skipIf(not common.bzip2_avail, 1114 'BZIP2 compression library not available') 1115class Bzip2ComprTestCase(BasicTestCase): 1116 compress = 1 1117 complib = "bzip2" 1118 chunksize = 100 1119 nappends = 10 1120 start = 3 1121 stop = 10 1122 step = 8 1123 1124 1125@unittest.skipIf(not common.bzip2_avail, 1126 'BZIP2 compression library not available') 1127class Bzip2ShuffleTestCase(BasicTestCase): 1128 compress = 1 1129 shuffle = 1 1130 complib = "bzip2" 1131 chunksize = 100 1132 nappends = 10 1133 start = 3 1134 stop = 10 1135 step = 6 1136 1137 1138class Fletcher32TestCase(BasicTestCase): 1139 compress = 0 1140 fletcher32 = 1 1141 chunksize = 50 1142 nappends = 20 1143 start = 4 1144 stop = 20 1145 step = 7 1146 1147 1148class AllFiltersTestCase(BasicTestCase): 1149 compress = 1 1150 shuffle = 1 1151 fletcher32 = 1 1152 complib = "zlib" 1153 chunksize = 20 # sss 1154 nappends = 50 1155 start = 2 1156 stop = 99 1157 step = 6 1158# chunksize = 3 1159# nappends = 2 1160# start = 1 1161# stop = 10 1162# step = 2 1163 1164 1165class FloatTypeTestCase(BasicTestCase): 1166 type = 'float64' 1167 dtype = 'float64' 1168 shape = (2, 0) 1169 chunksize = 5 1170 nappends = 10 1171 start = 3 1172 stop = 10 1173 step = 20 1174 1175 1176class ComplexTypeTestCase(BasicTestCase): 1177 type = 'complex128' 1178 dtype = 'complex128' 1179 shape = (2, 0) 1180 chunksize = 5 1181 nappends = 10 1182 start = 3 1183 stop = 10 1184 step = 20 1185 1186 1187class StringTestCase(BasicTestCase): 1188 type = "string" 1189 length = 20 1190 shape = (2, 0) 1191 # shape = (2,0,20) 1192 chunksize = 5 1193 nappends = 10 1194 start = 3 1195 stop = 10 1196 step = 20 1197 slices = (slice(0, 1), slice(1, 2)) 1198 1199 1200class String2TestCase(BasicTestCase): 1201 type = "string" 1202 length = 20 1203 shape = (0,) 1204 # shape = (0, 20) 1205 chunksize = 5 1206 nappends = 10 1207 start = 1 1208 stop = 10 1209 step = 2 1210 1211 1212class StringComprTestCase(BasicTestCase): 1213 type = "string" 1214 length = 20 1215 shape = (20, 0, 10) 1216 # shape = (20,0,10,20) 1217 compr = 1 1218 # shuffle = 1 # this shouldn't do nothing on chars 1219 chunksize = 50 1220 nappends = 10 1221 start = -1 1222 stop = 100 1223 step = 20 1224 1225 1226class SizeOnDiskInMemoryPropertyTestCase(common.TempFileMixin, TestCase): 1227 1228 def setUp(self): 1229 super(SizeOnDiskInMemoryPropertyTestCase, self).setUp() 1230 1231 self.array_size = (0, 10) 1232 # set chunkshape so it divides evenly into array_size, to avoid 1233 # partially filled chunks 1234 self.chunkshape = (1000, 10) 1235 # approximate size (in bytes) of non-data portion of hdf5 file 1236 self.hdf_overhead = 6000 1237 1238 def create_array(self, complevel): 1239 filters = tables.Filters(complevel=complevel, complib='blosc') 1240 self.array = self.h5file.create_earray('/', 'earray', atom=Int32Atom(), 1241 shape=self.array_size, 1242 filters=filters, 1243 chunkshape=self.chunkshape) 1244 1245 def test_zero_length(self): 1246 complevel = 0 1247 self.create_array(complevel) 1248 self.assertEqual(self.array.size_on_disk, 0) 1249 self.assertEqual(self.array.size_in_memory, 0) 1250 1251 # add 10 chunks of data in one append 1252 def test_no_compression_one_append(self): 1253 complevel = 0 1254 self.create_array(complevel) 1255 self.array.append([tuple(range(10))] * self.chunkshape[0] * 10) 1256 self.assertEqual(self.array.size_on_disk, 10 * 1000 * 10 * 4) 1257 self.assertEqual(self.array.size_in_memory, 10 * 1000 * 10 * 4) 1258 1259 # add 10 chunks of data in two appends 1260 def test_no_compression_multiple_appends(self): 1261 complevel = 0 1262 self.create_array(complevel) 1263 self.array.append([tuple(range(10))] * self.chunkshape[0] * 5) 1264 self.array.append([tuple(range(10))] * self.chunkshape[0] * 5) 1265 self.assertEqual(self.array.size_on_disk, 10 * 1000 * 10 * 4) 1266 self.assertEqual(self.array.size_in_memory, 10 * 1000 * 10 * 4) 1267 1268 def test_with_compression(self): 1269 complevel = 1 1270 self.create_array(complevel) 1271 self.array.append([tuple(range(10))] * self.chunkshape[0] * 10) 1272 file_size = os.stat(self.h5fname).st_size 1273 self.assertTrue( 1274 abs(self.array.size_on_disk - file_size) <= self.hdf_overhead) 1275 self.assertEqual(self.array.size_in_memory, 10 * 1000 * 10 * 4) 1276 self.assertLess(self.array.size_on_disk, self.array.size_in_memory) 1277 1278 1279class OffsetStrideTestCase(common.TempFileMixin, TestCase): 1280 mode = "w" 1281 compress = 0 1282 complib = "zlib" # Default compression library 1283 1284 def setUp(self): 1285 super(OffsetStrideTestCase, self).setUp() 1286 self.rootgroup = self.h5file.root 1287 1288 def test01a_String(self): 1289 """Checking earray with offseted numpy strings appends.""" 1290 1291 root = self.rootgroup 1292 if common.verbose: 1293 print('\n', '-=' * 30) 1294 print("Running %s.test01a_StringAtom..." % self.__class__.__name__) 1295 1296 earray = self.h5file.create_earray(root, 'strings', 1297 atom=StringAtom(itemsize=3), 1298 shape=(0, 2, 2), 1299 title="Array of strings") 1300 a = numpy.array([[["a", "b"], [ 1301 "123", "45"], ["45", "123"]]], dtype="S3") 1302 earray.append(a[:, 1:]) 1303 a = numpy.array([[["s", "a"], [ 1304 "ab", "f"], ["s", "abc"], ["abc", "f"]]]) 1305 earray.append(a[:, 2:]) 1306 1307 # Read all the rows: 1308 row = earray.read() 1309 if common.verbose: 1310 print("Object read:", row) 1311 print("Nrows in", earray._v_pathname, ":", earray.nrows) 1312 print("Second row in earray ==>", row[1].tolist()) 1313 1314 self.assertEqual(earray.nrows, 2) 1315 self.assertEqual(row[0].tolist(), [[b"123", b"45"], [b"45", b"123"]]) 1316 self.assertEqual(row[1].tolist(), [[b"s", b"abc"], [b"abc", b"f"]]) 1317 self.assertEqual(len(row[0]), 2) 1318 self.assertEqual(len(row[1]), 2) 1319 1320 def test01b_String(self): 1321 """Checking earray with strided numpy strings appends.""" 1322 1323 root = self.rootgroup 1324 if common.verbose: 1325 print('\n', '-=' * 30) 1326 print("Running %s.test01b_StringAtom..." % self.__class__.__name__) 1327 1328 earray = self.h5file.create_earray(root, 'strings', 1329 atom=StringAtom(itemsize=3), 1330 shape=(0, 2, 2), 1331 title="Array of strings") 1332 a = numpy.array([[["a", "b"], [ 1333 "123", "45"], ["45", "123"]]], dtype="S3") 1334 earray.append(a[:, ::2]) 1335 a = numpy.array([[["s", "a"], [ 1336 "ab", "f"], ["s", "abc"], ["abc", "f"]]]) 1337 earray.append(a[:, ::2]) 1338 1339 # Read all the rows: 1340 row = earray.read() 1341 if common.verbose: 1342 print("Object read:", row) 1343 print("Nrows in", earray._v_pathname, ":", earray.nrows) 1344 print("Second row in earray ==>", row[1].tolist()) 1345 1346 self.assertEqual(earray.nrows, 2) 1347 self.assertEqual(row[0].tolist(), [[b"a", b"b"], [b"45", b"123"]]) 1348 self.assertEqual(row[1].tolist(), [[b"s", b"a"], [b"s", b"abc"]]) 1349 self.assertEqual(len(row[0]), 2) 1350 self.assertEqual(len(row[1]), 2) 1351 1352 def test02a_int(self): 1353 """Checking earray with offseted NumPy ints appends.""" 1354 1355 root = self.rootgroup 1356 if common.verbose: 1357 print('\n', '-=' * 30) 1358 print("Running %s.test02a_int..." % self.__class__.__name__) 1359 1360 # Create an string atom 1361 earray = self.h5file.create_earray(root, 'EAtom', 1362 atom=Int32Atom(), shape=(0, 3), 1363 title="array of ints") 1364 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1365 1, 1, 1), (0, 0, 0)], dtype='int32') 1366 earray.append(a[2:]) # Create an offset 1367 a = numpy.array([(1, 1, 1), (-1, 0, 0)], dtype='int32') 1368 earray.append(a[1:]) # Create an offset 1369 1370 # Read all the rows: 1371 row = earray.read() 1372 if common.verbose: 1373 print("Object read:", row) 1374 print("Nrows in", earray._v_pathname, ":", earray.nrows) 1375 print("Third row in vlarray ==>", row[2]) 1376 1377 self.assertEqual(earray.nrows, 3) 1378 self.assertTrue(allequal(row[ 1379 0], numpy.array([1, 1, 1], dtype='int32'))) 1380 self.assertTrue(allequal(row[ 1381 1], numpy.array([0, 0, 0], dtype='int32'))) 1382 self.assertTrue(allequal(row[ 1383 2], numpy.array([-1, 0, 0], dtype='int32'))) 1384 1385 def test02b_int(self): 1386 """Checking earray with strided NumPy ints appends.""" 1387 1388 root = self.rootgroup 1389 if common.verbose: 1390 print('\n', '-=' * 30) 1391 print("Running %s.test02b_int..." % self.__class__.__name__) 1392 1393 earray = self.h5file.create_earray(root, 'EAtom', 1394 atom=Int32Atom(), shape=(0, 3), 1395 title="array of ints") 1396 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1397 1, 1, 1), (3, 3, 3)], dtype='int32') 1398 earray.append(a[::3]) # Create an offset 1399 a = numpy.array([(1, 1, 1), (-1, 0, 0)], dtype='int32') 1400 earray.append(a[::2]) # Create an offset 1401 1402 # Read all the rows: 1403 row = earray.read() 1404 if common.verbose: 1405 print("Object read:", row) 1406 print("Nrows in", earray._v_pathname, ":", earray.nrows) 1407 print("Third row in vlarray ==>", row[2]) 1408 1409 self.assertEqual(earray.nrows, 3) 1410 self.assertTrue(allequal(row[ 1411 0], numpy.array([0, 0, 0], dtype='int32'))) 1412 self.assertTrue(allequal(row[ 1413 1], numpy.array([3, 3, 3], dtype='int32'))) 1414 self.assertTrue(allequal(row[ 1415 2], numpy.array([1, 1, 1], dtype='int32'))) 1416 1417 def test03a_int(self): 1418 """Checking earray with byteswapped appends (ints)""" 1419 1420 root = self.rootgroup 1421 if common.verbose: 1422 print('\n', '-=' * 30) 1423 print("Running %s.test03a_int..." % self.__class__.__name__) 1424 1425 earray = self.h5file.create_earray(root, 'EAtom', 1426 atom=Int32Atom(), shape=(0, 3), 1427 title="array of ints") 1428 # Add a native ordered array 1429 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1430 1, 1, 1), (3, 3, 3)], dtype='int32') 1431 earray.append(a) 1432 # Change the byteorder of the array 1433 a = a.byteswap() 1434 a = a.newbyteorder() 1435 # Add a byteswapped array 1436 earray.append(a) 1437 1438 # Read all the rows: 1439 native = earray[:4, :] 1440 swapped = earray[4:, :] 1441 if common.verbose: 1442 print("Native rows:", native) 1443 print("Byteorder native rows:", native.dtype.byteorder) 1444 print("Swapped rows:", swapped) 1445 print("Byteorder swapped rows:", swapped.dtype.byteorder) 1446 1447 self.assertTrue(allequal(native, swapped)) 1448 1449 def test03b_float(self): 1450 """Checking earray with byteswapped appends (floats)""" 1451 1452 root = self.rootgroup 1453 if common.verbose: 1454 print('\n', '-=' * 30) 1455 print("Running %s.test03b_float..." % self.__class__.__name__) 1456 1457 earray = self.h5file.create_earray(root, 'EAtom', 1458 atom=Float64Atom(), shape=(0, 3), 1459 title="array of floats") 1460 # Add a native ordered array 1461 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1462 1, 1, 1), (3, 3, 3)], dtype='float64') 1463 earray.append(a) 1464 # Change the byteorder of the array 1465 a = a.byteswap() 1466 a = a.newbyteorder() 1467 # Add a byteswapped array 1468 earray.append(a) 1469 1470 # Read all the rows: 1471 native = earray[:4, :] 1472 swapped = earray[4:, :] 1473 if common.verbose: 1474 print("Native rows:", native) 1475 print("Byteorder native rows:", native.dtype.byteorder) 1476 print("Swapped rows:", swapped) 1477 print("Byteorder swapped rows:", swapped.dtype.byteorder) 1478 1479 self.assertTrue(allequal(native, swapped)) 1480 1481 def test04a_int(self): 1482 """Checking earray with byteswapped appends (2, ints)""" 1483 1484 root = self.rootgroup 1485 if common.verbose: 1486 print('\n', '-=' * 30) 1487 print("Running %s.test04a_int..." % self.__class__.__name__) 1488 1489 byteorder = {'little': 'big', 'big': 'little'}[sys.byteorder] 1490 earray = self.h5file.create_earray(root, 'EAtom', 1491 atom=Int32Atom(), shape=(0, 3), 1492 title="array of ints", 1493 byteorder=byteorder) 1494 # Add a native ordered array 1495 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1496 1, 1, 1), (3, 3, 3)], dtype='int32') 1497 earray.append(a) 1498 # Change the byteorder of the array 1499 a = a.byteswap() 1500 a = a.newbyteorder() 1501 # Add a byteswapped array 1502 earray.append(a) 1503 1504 # Read all the rows: 1505 native = earray[:4, :] 1506 swapped = earray[4:, :] 1507 if common.verbose: 1508 print("Byteorder native rows:", byteorders[native.dtype.byteorder]) 1509 print("Byteorder earray on-disk:", earray.byteorder) 1510 1511 self.assertEqual(byteorders[native.dtype.byteorder], sys.byteorder) 1512 self.assertEqual(earray.byteorder, byteorder) 1513 self.assertTrue(allequal(native, swapped)) 1514 1515 def test04b_int(self): 1516 """Checking earray with byteswapped appends (2, ints, reopen)""" 1517 1518 root = self.rootgroup 1519 if common.verbose: 1520 print('\n', '-=' * 30) 1521 print("Running %s.test04b_int..." % self.__class__.__name__) 1522 1523 byteorder = {'little': 'big', 'big': 'little'}[sys.byteorder] 1524 earray = self.h5file.create_earray(root, 'EAtom', 1525 atom=Int32Atom(), shape=(0, 3), 1526 title="array of ints", 1527 byteorder=byteorder) 1528 self._reopen(mode="a") 1529 earray = self.h5file.get_node("/EAtom") 1530 # Add a native ordered array 1531 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1532 1, 1, 1), (3, 3, 3)], dtype='int32') 1533 earray.append(a) 1534 # Change the byteorder of the array 1535 a = a.byteswap() 1536 a = a.newbyteorder() 1537 # Add a byteswapped array 1538 earray.append(a) 1539 1540 # Read all the rows: 1541 native = earray[:4, :] 1542 swapped = earray[4:, :] 1543 if common.verbose: 1544 print("Byteorder native rows:", byteorders[native.dtype.byteorder]) 1545 print("Byteorder earray on-disk:", earray.byteorder) 1546 1547 self.assertEqual(byteorders[native.dtype.byteorder], sys.byteorder) 1548 self.assertEqual(earray.byteorder, byteorder) 1549 self.assertTrue(allequal(native, swapped)) 1550 1551 def test04c_float(self): 1552 """Checking earray with byteswapped appends (2, floats)""" 1553 1554 root = self.rootgroup 1555 if common.verbose: 1556 print('\n', '-=' * 30) 1557 print("Running %s.test04c_float..." % self.__class__.__name__) 1558 1559 byteorder = {'little': 'big', 'big': 'little'}[sys.byteorder] 1560 earray = self.h5file.create_earray(root, 'EAtom', 1561 atom=Float64Atom(), shape=(0, 3), 1562 title="array of floats", 1563 byteorder=byteorder) 1564 # Add a native ordered array 1565 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1566 1, 1, 1), (3, 3, 3)], dtype='float64') 1567 earray.append(a) 1568 # Change the byteorder of the array 1569 a = a.byteswap() 1570 a = a.newbyteorder() 1571 # Add a byteswapped array 1572 earray.append(a) 1573 1574 # Read all the rows: 1575 native = earray[:4, :] 1576 swapped = earray[4:, :] 1577 if common.verbose: 1578 print("Byteorder native rows:", byteorders[native.dtype.byteorder]) 1579 print("Byteorder earray on-disk:", earray.byteorder) 1580 1581 self.assertEqual(byteorders[native.dtype.byteorder], sys.byteorder) 1582 self.assertEqual(earray.byteorder, byteorder) 1583 self.assertTrue(allequal(native, swapped)) 1584 1585 def test04d_float(self): 1586 """Checking earray with byteswapped appends (2, floats, reopen)""" 1587 1588 root = self.rootgroup 1589 if common.verbose: 1590 print('\n', '-=' * 30) 1591 print("Running %s.test04d_float..." % self.__class__.__name__) 1592 1593 byteorder = {'little': 'big', 'big': 'little'}[sys.byteorder] 1594 earray = self.h5file.create_earray(root, 'EAtom', 1595 atom=Float64Atom(), shape=(0, 3), 1596 title="array of floats", 1597 byteorder=byteorder) 1598 self._reopen(mode='a') 1599 earray = self.h5file.get_node("/EAtom") 1600 # Add a native ordered array 1601 a = numpy.array([(0, 0, 0), (1, 0, 3), ( 1602 1, 1, 1), (3, 3, 3)], dtype='float64') 1603 earray.append(a) 1604 # Change the byteorder of the array 1605 a = a.byteswap() 1606 a = a.newbyteorder() 1607 # Add a byteswapped array 1608 earray.append(a) 1609 1610 # Read all the rows: 1611 native = earray[:4, :] 1612 swapped = earray[4:, :] 1613 if common.verbose: 1614 print("Byteorder native rows:", byteorders[native.dtype.byteorder]) 1615 print("Byteorder earray on-disk:", earray.byteorder) 1616 1617 self.assertEqual(byteorders[native.dtype.byteorder], sys.byteorder) 1618 self.assertEqual(earray.byteorder, byteorder) 1619 self.assertTrue(allequal(native, swapped)) 1620 1621 1622class CopyTestCase(common.TempFileMixin, TestCase): 1623 1624 def test01_copy(self): 1625 """Checking EArray.copy() method.""" 1626 1627 if common.verbose: 1628 print('\n', '-=' * 30) 1629 print("Running %s.test01_copy..." % self.__class__.__name__) 1630 1631 # Create an EArray 1632 atom = Int16Atom() 1633 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1634 atom=atom, shape=(0, 2), 1635 title="title array1") 1636 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 1637 1638 if self.close: 1639 if common.verbose: 1640 print("(closing file version)") 1641 self._reopen(mode='a') 1642 array1 = self.h5file.root.array1 1643 1644 # Copy it to another location 1645 array2 = array1.copy('/', 'array2') 1646 1647 if self.close: 1648 if common.verbose: 1649 print("(closing file version)") 1650 self._reopen() 1651 array1 = self.h5file.root.array1 1652 array2 = self.h5file.root.array2 1653 1654 if common.verbose: 1655 print("array1-->", array1.read()) 1656 print("array2-->", array2.read()) 1657 # print("dirs-->", dir(array1), dir(array2)) 1658 print("attrs array1-->", repr(array1.attrs)) 1659 print("attrs array2-->", repr(array2.attrs)) 1660 1661 # Check that all the elements are equal 1662 self.assertTrue(allequal(array1.read(), array2.read())) 1663 1664 # Assert other properties in array 1665 self.assertEqual(array1.nrows, array2.nrows) 1666 self.assertEqual(array1.shape, array2.shape) 1667 self.assertEqual(array1.extdim, array2.extdim) 1668 self.assertEqual(array1.flavor, array2.flavor) 1669 self.assertEqual(array1.atom.dtype, array2.atom.dtype) 1670 self.assertEqual(array1.atom.type, array2.atom.type) 1671 self.assertEqual(array1.atom.itemsize, array2.atom.itemsize) 1672 self.assertEqual(array1.title, array2.title) 1673 self.assertEqual(str(array1.atom), str(array2.atom)) 1674 1675 def test02_copy(self): 1676 """Checking EArray.copy() method (where specified)""" 1677 1678 if common.verbose: 1679 print('\n', '-=' * 30) 1680 print("Running %s.test02_copy..." % self.__class__.__name__) 1681 1682 # Create an EArray 1683 atom = Int16Atom() 1684 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1685 atom=atom, shape=(0, 2), 1686 title="title array1") 1687 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 1688 1689 if self.close: 1690 if common.verbose: 1691 print("(closing file version)") 1692 self._reopen(mode='a') 1693 array1 = self.h5file.root.array1 1694 1695 # Copy to another location 1696 group1 = self.h5file.create_group("/", "group1") 1697 array2 = array1.copy(group1, 'array2') 1698 1699 if self.close: 1700 if common.verbose: 1701 print("(closing file version)") 1702 self._reopen() 1703 array1 = self.h5file.root.array1 1704 array2 = self.h5file.root.group1.array2 1705 1706 if common.verbose: 1707 print("array1-->", array1.read()) 1708 print("array2-->", array2.read()) 1709 # print("dirs-->", dir(array1), dir(array2)) 1710 print("attrs array1-->", repr(array1.attrs)) 1711 print("attrs array2-->", repr(array2.attrs)) 1712 1713 # Check that all the elements are equal 1714 self.assertTrue(allequal(array1.read(), array2.read())) 1715 1716 # Assert other properties in array 1717 self.assertEqual(array1.nrows, array2.nrows) 1718 self.assertEqual(array1.shape, array2.shape) 1719 self.assertEqual(array1.extdim, array2.extdim) 1720 self.assertEqual(array1.flavor, array2.flavor) 1721 self.assertEqual(array1.atom.dtype, array2.atom.dtype) 1722 self.assertEqual(array1.atom.type, array2.atom.type) 1723 self.assertEqual(array1.atom.itemsize, array2.atom.itemsize) 1724 self.assertEqual(array1.title, array2.title) 1725 self.assertEqual(str(array1.atom), str(array2.atom)) 1726 1727 def test03a_copy(self): 1728 """Checking EArray.copy() method (python flavor)""" 1729 1730 if common.verbose: 1731 print('\n', '-=' * 30) 1732 print("Running %s.test03b_copy..." % self.__class__.__name__) 1733 1734 atom = Int16Atom() 1735 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1736 atom=atom, shape=(0, 2), 1737 title="title array1") 1738 array1.flavor = "python" 1739 array1.append(((456, 2), (3, 457))) 1740 1741 if self.close: 1742 if common.verbose: 1743 print("(closing file version)") 1744 self._reopen(mode='a') 1745 array1 = self.h5file.root.array1 1746 1747 # Copy to another location 1748 array2 = array1.copy('/', 'array2') 1749 1750 if self.close: 1751 if common.verbose: 1752 print("(closing file version)") 1753 self._reopen() 1754 array1 = self.h5file.root.array1 1755 array2 = self.h5file.root.array2 1756 1757 if common.verbose: 1758 print("attrs array1-->", repr(array1.attrs)) 1759 print("attrs array2-->", repr(array2.attrs)) 1760 1761 # Check that all elements are equal 1762 self.assertEqual(array1.read(), array2.read()) 1763 # Assert other properties in array 1764 self.assertEqual(array1.nrows, array2.nrows) 1765 self.assertEqual(array1.shape, array2.shape) 1766 self.assertEqual(array1.extdim, array2.extdim) 1767 self.assertEqual(array1.flavor, array2.flavor) # Very important here! 1768 self.assertEqual(array1.atom.dtype, array2.atom.dtype) 1769 self.assertEqual(array1.atom.type, array2.atom.type) 1770 self.assertEqual(array1.atom.itemsize, array2.atom.itemsize) 1771 self.assertEqual(array1.title, array2.title) 1772 self.assertEqual(str(array1.atom), str(array2.atom)) 1773 1774 def test03b_copy(self): 1775 """Checking EArray.copy() method (python string flavor)""" 1776 1777 if common.verbose: 1778 print('\n', '-=' * 30) 1779 print("Running %s.test03d_copy..." % self.__class__.__name__) 1780 1781 atom = StringAtom(itemsize=3) 1782 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1783 atom=atom, shape=(0, 2), 1784 title="title array1") 1785 array1.flavor = "python" 1786 array1.append([["456", "2"], ["3", "457"]]) 1787 1788 if self.close: 1789 if common.verbose: 1790 print("(closing file version)") 1791 self._reopen(mode='a') 1792 array1 = self.h5file.root.array1 1793 1794 # Copy to another location 1795 array2 = array1.copy('/', 'array2') 1796 1797 if self.close: 1798 if common.verbose: 1799 print("(closing file version)") 1800 self._reopen() 1801 array1 = self.h5file.root.array1 1802 array2 = self.h5file.root.array2 1803 1804 if common.verbose: 1805 print("attrs array1-->", repr(array1.attrs)) 1806 print("attrs array2-->", repr(array2.attrs)) 1807 1808 # Check that all elements are equal 1809 self.assertEqual(array1.read(), array2.read()) 1810 1811 # Assert other properties in array 1812 self.assertEqual(array1.nrows, array2.nrows) 1813 self.assertEqual(array1.shape, array2.shape) 1814 self.assertEqual(array1.extdim, array2.extdim) 1815 self.assertEqual(array1.flavor, array2.flavor) # Very important here! 1816 self.assertEqual(array1.atom.dtype, array2.atom.dtype) 1817 self.assertEqual(array1.atom.type, array2.atom.type) 1818 self.assertEqual(array1.atom.itemsize, array2.atom.itemsize) 1819 self.assertEqual(array1.title, array2.title) 1820 self.assertEqual(str(array1.atom), str(array2.atom)) 1821 1822 def test03c_copy(self): 1823 """Checking EArray.copy() method (String flavor)""" 1824 1825 if common.verbose: 1826 print('\n', '-=' * 30) 1827 print("Running %s.test03e_copy..." % self.__class__.__name__) 1828 1829 atom = StringAtom(itemsize=4) 1830 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1831 atom=atom, shape=(0, 2), 1832 title="title array1") 1833 array1.flavor = "numpy" 1834 array1.append(numpy.array([["456", "2"], ["3", "457"]], dtype="S4")) 1835 1836 if self.close: 1837 if common.verbose: 1838 print("(closing file version)") 1839 self._reopen(mode='a') 1840 array1 = self.h5file.root.array1 1841 1842 # Copy to another location 1843 array2 = array1.copy('/', 'array2') 1844 1845 if self.close: 1846 if common.verbose: 1847 print("(closing file version)") 1848 self._reopen() 1849 array1 = self.h5file.root.array1 1850 array2 = self.h5file.root.array2 1851 1852 if common.verbose: 1853 print("attrs array1-->", repr(array1.attrs)) 1854 print("attrs array2-->", repr(array2.attrs)) 1855 1856 # Check that all elements are equal 1857 self.assertTrue(allequal(array1.read(), array2.read())) 1858 # Assert other properties in array 1859 self.assertEqual(array1.nrows, array2.nrows) 1860 self.assertEqual(array1.shape, array2.shape) 1861 self.assertEqual(array1.extdim, array2.extdim) 1862 self.assertEqual(array1.flavor, array2.flavor) # Very important here! 1863 self.assertEqual(array1.atom.dtype, array2.atom.dtype) 1864 self.assertEqual(array1.atom.type, array2.atom.type) 1865 self.assertEqual(array1.atom.itemsize, array2.atom.itemsize) 1866 self.assertEqual(array1.title, array2.title) 1867 self.assertEqual(str(array1.atom), str(array2.atom)) 1868 1869 def test04_copy(self): 1870 """Checking EArray.copy() method (checking title copying)""" 1871 1872 if common.verbose: 1873 print('\n', '-=' * 30) 1874 print("Running %s.test04_copy..." % self.__class__.__name__) 1875 1876 # Create an EArray 1877 atom = Int16Atom() 1878 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1879 atom=atom, shape=(0, 2), 1880 title="title array1") 1881 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 1882 # Append some user attrs 1883 array1.attrs.attr1 = "attr1" 1884 array1.attrs.attr2 = 2 1885 1886 if self.close: 1887 if common.verbose: 1888 print("(closing file version)") 1889 self._reopen(mode='a') 1890 array1 = self.h5file.root.array1 1891 1892 # Copy it to another Array 1893 array2 = array1.copy('/', 'array2', title="title array2") 1894 1895 if self.close: 1896 if common.verbose: 1897 print("(closing file version)") 1898 self._reopen() 1899 array1 = self.h5file.root.array1 1900 array2 = self.h5file.root.array2 1901 1902 # Assert user attributes 1903 if common.verbose: 1904 print("title of destination array-->", array2.title) 1905 self.assertEqual(array2.title, "title array2") 1906 1907 def test05_copy(self): 1908 """Checking EArray.copy() method (user attributes copied)""" 1909 1910 if common.verbose: 1911 print('\n', '-=' * 30) 1912 print("Running %s.test05_copy..." % self.__class__.__name__) 1913 1914 # Create an EArray 1915 atom = Int16Atom() 1916 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1917 atom=atom, shape=(0, 2), 1918 title="title array1") 1919 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 1920 # Append some user attrs 1921 array1.attrs.attr1 = "attr1" 1922 array1.attrs.attr2 = 2 1923 1924 if self.close: 1925 if common.verbose: 1926 print("(closing file version)") 1927 self._reopen(mode='a') 1928 array1 = self.h5file.root.array1 1929 1930 # Copy it to another Array 1931 array2 = array1.copy('/', 'array2', copyuserattrs=1) 1932 1933 if self.close: 1934 if common.verbose: 1935 print("(closing file version)") 1936 self._reopen() 1937 array1 = self.h5file.root.array1 1938 array2 = self.h5file.root.array2 1939 1940 if common.verbose: 1941 print("attrs array1-->", repr(array1.attrs)) 1942 print("attrs array2-->", repr(array2.attrs)) 1943 1944 # Assert user attributes 1945 self.assertEqual(array2.attrs.attr1, "attr1") 1946 self.assertEqual(array2.attrs.attr2, 2) 1947 1948 def test05b_copy(self): 1949 """Checking EArray.copy() method (user attributes not copied)""" 1950 1951 if common.verbose: 1952 print('\n', '-=' * 30) 1953 print("Running %s.test05b_copy..." % self.__class__.__name__) 1954 1955 # Create an Array 1956 atom = Int16Atom() 1957 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 1958 atom=atom, shape=(0, 2), 1959 title="title array1") 1960 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 1961 # Append some user attrs 1962 array1.attrs.attr1 = "attr1" 1963 array1.attrs.attr2 = 2 1964 1965 if self.close: 1966 if common.verbose: 1967 print("(closing file version)") 1968 self._reopen(mode='a') 1969 array1 = self.h5file.root.array1 1970 1971 # Copy it to another Array 1972 array2 = array1.copy('/', 'array2', copyuserattrs=0) 1973 1974 if self.close: 1975 if common.verbose: 1976 print("(closing file version)") 1977 self._reopen() 1978 array1 = self.h5file.root.array1 1979 array2 = self.h5file.root.array2 1980 1981 if common.verbose: 1982 print("attrs array1-->", repr(array1.attrs)) 1983 print("attrs array2-->", repr(array2.attrs)) 1984 1985 # Assert user attributes 1986 self.assertEqual(hasattr(array2.attrs, "attr1"), 0) 1987 self.assertEqual(hasattr(array2.attrs, "attr2"), 0) 1988 1989 1990class CloseCopyTestCase(CopyTestCase): 1991 close = 1 1992 1993 1994class OpenCopyTestCase(CopyTestCase): 1995 close = 0 1996 1997 1998class CopyIndexTestCase(common.TempFileMixin, TestCase): 1999 nrowsinbuf = 2 2000 2001 def test01_index(self): 2002 """Checking EArray.copy() method with indexes.""" 2003 2004 if common.verbose: 2005 print('\n', '-=' * 30) 2006 print("Running %s.test01_index..." % self.__class__.__name__) 2007 2008 # Create an EArray 2009 atom = Int32Atom() 2010 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 2011 atom=atom, shape=(0, 2), 2012 title="title array1") 2013 r = numpy.arange(200, dtype='int32') 2014 r.shape = (100, 2) 2015 array1.append(r) 2016 2017 # Select a different buffer size: 2018 array1.nrowsinbuf = self.nrowsinbuf 2019 2020 # Copy to another array 2021 array2 = array1.copy("/", 'array2', 2022 start=self.start, 2023 stop=self.stop, 2024 step=self.step) 2025 if common.verbose: 2026 print("array1-->", array1.read()) 2027 print("array2-->", array2.read()) 2028 print("attrs array1-->", repr(array1.attrs)) 2029 print("attrs array2-->", repr(array2.attrs)) 2030 2031 # Check that all the elements are equal 2032 r2 = r[self.start:self.stop:self.step] 2033 self.assertTrue(allequal(r2, array2.read())) 2034 2035 # Assert the number of rows in array 2036 if common.verbose: 2037 print("nrows in array2-->", array2.nrows) 2038 print("and it should be-->", r2.shape[0]) 2039 self.assertEqual(r2.shape[0], array2.nrows) 2040 2041 def test02_indexclosef(self): 2042 """Checking EArray.copy() method with indexes (close file version)""" 2043 2044 if common.verbose: 2045 print('\n', '-=' * 30) 2046 print("Running %s.test02_indexclosef..." % self.__class__.__name__) 2047 2048 # Create an EArray 2049 atom = Int32Atom() 2050 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 2051 atom=atom, shape=(0, 2), 2052 title="title array1") 2053 r = numpy.arange(200, dtype='int32') 2054 r.shape = (100, 2) 2055 array1.append(r) 2056 2057 # Select a different buffer size: 2058 array1.nrowsinbuf = self.nrowsinbuf 2059 2060 # Copy to another array 2061 array2 = array1.copy("/", 'array2', 2062 start=self.start, 2063 stop=self.stop, 2064 step=self.step) 2065 # Close and reopen the file 2066 self._reopen() 2067 array1 = self.h5file.root.array1 2068 array2 = self.h5file.root.array2 2069 2070 if common.verbose: 2071 print("array1-->", array1.read()) 2072 print("array2-->", array2.read()) 2073 print("attrs array1-->", repr(array1.attrs)) 2074 print("attrs array2-->", repr(array2.attrs)) 2075 2076 # Check that all the elements are equal 2077 r2 = r[self.start:self.stop:self.step] 2078 self.assertTrue(allequal(r2, array2.read())) 2079 2080 # Assert the number of rows in array 2081 if common.verbose: 2082 print("nrows in array2-->", array2.nrows) 2083 print("and it should be-->", r2.shape[0]) 2084 self.assertEqual(r2.shape[0], array2.nrows) 2085 2086 2087class CopyIndex1TestCase(CopyIndexTestCase): 2088 nrowsinbuf = 1 2089 start = 0 2090 stop = 7 2091 step = 1 2092 2093 2094class CopyIndex2TestCase(CopyIndexTestCase): 2095 nrowsinbuf = 2 2096 start = 0 2097 stop = -1 2098 step = 1 2099 2100 2101class CopyIndex3TestCase(CopyIndexTestCase): 2102 nrowsinbuf = 3 2103 start = 1 2104 stop = 7 2105 step = 1 2106 2107 2108class CopyIndex4TestCase(CopyIndexTestCase): 2109 nrowsinbuf = 4 2110 start = 0 2111 stop = 6 2112 step = 1 2113 2114 2115class CopyIndex5TestCase(CopyIndexTestCase): 2116 nrowsinbuf = 2 2117 start = 3 2118 stop = 7 2119 step = 1 2120 2121 2122class CopyIndex6TestCase(CopyIndexTestCase): 2123 nrowsinbuf = 2 2124 start = 3 2125 stop = 6 2126 step = 2 2127 2128 2129class CopyIndex7TestCase(CopyIndexTestCase): 2130 start = 0 2131 stop = 7 2132 step = 10 2133 2134 2135class CopyIndex8TestCase(CopyIndexTestCase): 2136 start = 6 2137 stop = -1 # Negative values means starting from the end 2138 step = 1 2139 2140 2141class CopyIndex9TestCase(CopyIndexTestCase): 2142 start = 3 2143 stop = 4 2144 step = 1 2145 2146 2147class CopyIndex10TestCase(CopyIndexTestCase): 2148 nrowsinbuf = 1 2149 start = 3 2150 stop = 4 2151 step = 2 2152 2153 2154class CopyIndex11TestCase(CopyIndexTestCase): 2155 start = -3 2156 stop = -1 2157 step = 2 2158 2159 2160class CopyIndex12TestCase(CopyIndexTestCase): 2161 start = -1 # Should point to the last element 2162 stop = None # None should mean the last element (including it) 2163 step = 1 2164 2165 2166class TruncateTestCase(common.TempFileMixin, TestCase): 2167 2168 def setUp(self): 2169 super(TruncateTestCase, self).setUp() 2170 2171 # Create an EArray 2172 atom = Int16Atom(dflt=3) 2173 array1 = self.h5file.create_earray(self.h5file.root, 'array1', 2174 atom=atom, shape=(0, 2), 2175 title="title array1") 2176 # Add a couple of rows 2177 array1.append(numpy.array([[456, 2], [3, 457]], dtype='int16')) 2178 2179 def test00_truncate(self): 2180 """Checking EArray.truncate() method (truncating to 0 rows)""" 2181 2182 array1 = self.h5file.root.array1 2183 # Truncate to 0 elements 2184 array1.truncate(0) 2185 2186 if self.close: 2187 if common.verbose: 2188 print("(closing file version)") 2189 self._reopen() 2190 array1 = self.h5file.root.array1 2191 2192 if common.verbose: 2193 print("array1-->", array1.read()) 2194 2195 self.assertTrue(allequal( 2196 array1[:], numpy.array([], dtype='int16').reshape(0, 2))) 2197 2198 def test01_truncate(self): 2199 """Checking EArray.truncate() method (truncating to 1 rows)""" 2200 2201 array1 = self.h5file.root.array1 2202 # Truncate to 1 element 2203 array1.truncate(1) 2204 2205 if self.close: 2206 if common.verbose: 2207 print("(closing file version)") 2208 self._reopen() 2209 array1 = self.h5file.root.array1 2210 2211 if common.verbose: 2212 print("array1-->", array1.read()) 2213 2214 self.assertTrue(allequal( 2215 array1.read(), numpy.array([[456, 2]], dtype='int16'))) 2216 2217 def test02_truncate(self): 2218 """Checking EArray.truncate() method (truncating to == self.nrows)""" 2219 2220 array1 = self.h5file.root.array1 2221 # Truncate to 2 elements 2222 array1.truncate(2) 2223 2224 if self.close: 2225 if common.verbose: 2226 print("(closing file version)") 2227 self._reopen() 2228 array1 = self.h5file.root.array1 2229 2230 if common.verbose: 2231 print("array1-->", array1.read()) 2232 2233 self.assertTrue( 2234 allequal(array1.read(), 2235 numpy.array([[456, 2], [3, 457]], dtype='int16'))) 2236 2237 def test03_truncate(self): 2238 """Checking EArray.truncate() method (truncating to > self.nrows)""" 2239 2240 array1 = self.h5file.root.array1 2241 # Truncate to 4 elements 2242 array1.truncate(4) 2243 2244 if self.close: 2245 if common.verbose: 2246 print("(closing file version)") 2247 self._reopen() 2248 array1 = self.h5file.root.array1 2249 2250 if common.verbose: 2251 print("array1-->", array1.read()) 2252 2253 self.assertEqual(array1.nrows, 4) 2254 # Check the original values 2255 self.assertTrue(allequal(array1[:2], numpy.array([[456, 2], [3, 457]], 2256 dtype='int16'))) 2257 # Check that the added rows have the default values 2258 self.assertTrue(allequal(array1[2:], numpy.array([[3, 3], [3, 3]], 2259 dtype='int16'))) 2260 2261 2262class TruncateOpenTestCase(TruncateTestCase): 2263 close = 0 2264 2265 2266class TruncateCloseTestCase(TruncateTestCase): 2267 close = 1 2268 2269 2270# The next test should be run only in **common.heavy** mode 2271class Rows64bitsTestCase(common.TempFileMixin, TestCase): 2272 open_mode = 'a' 2273 narows = 1000 * 1000 # each numpy object will have 1 million entries 2274 # narows = 1000 # for testing only 2275 nanumber = 1000 * 3 # That should account for more than 2**31-1 2276 2277 def setUp(self): 2278 super(Rows64bitsTestCase, self).setUp() 2279 2280 # Create an EArray 2281 array = self.h5file.create_earray( 2282 self.h5file.root, 'array', 2283 atom=tables.Int8Atom(), shape=(0,), 2284 filters=tables.Filters(complib='lzo', complevel=1), 2285 # Specifying expectedrows takes more 2286 # CPU, but less disk 2287 expectedrows=self.narows * self.nanumber) 2288 2289 # Fill the array 2290 na = numpy.arange(self.narows, dtype='int8') 2291 for i in range(self.nanumber): 2292 array.append(na) 2293 2294 def test01_basiccheck(self): 2295 """Some basic checks for earrays exceeding 2**31 rows""" 2296 2297 array = self.h5file.root.array 2298 2299 if self.close: 2300 if common.verbose: 2301 # Check how many entries there are in the array 2302 print("Before closing") 2303 print("Entries:", array.nrows, type(array.nrows)) 2304 print("Entries:", array.nrows / (1000 * 1000), "Millions") 2305 print("Shape:", array.shape) 2306 2307 # Close the file 2308 self._reopen() 2309 2310 array = self.h5file.root.array 2311 if common.verbose: 2312 print("After re-open") 2313 2314 # Check how many entries there are in the array 2315 if common.verbose: 2316 print("Entries:", array.nrows, type(array.nrows)) 2317 print("Entries:", array.nrows / (1000 * 1000), "Millions") 2318 print("Shape:", array.shape) 2319 print("Last 10 elements-->", array[-10:]) 2320 stop = self.narows % 256 2321 if stop > 127: 2322 stop -= 256 2323 start = stop - 10 2324 print("Should look like-->", numpy.arange(start, stop, 2325 dtype='int8')) 2326 2327 nrows = self.narows * self.nanumber 2328 # check nrows 2329 self.assertEqual(array.nrows, nrows) 2330 # Check shape 2331 self.assertEqual(array.shape, (nrows,)) 2332 # check the 10 first elements 2333 self.assertTrue(allequal(array[:10], numpy.arange(10, dtype='int8'))) 2334 # check the 10 last elements 2335 stop = self.narows % 256 2336 if stop > 127: 2337 stop -= 256 2338 start = stop - 10 2339 self.assertTrue(allequal(array[-10:], 2340 numpy.arange(start, stop, dtype='int8'))) 2341 2342 2343class Rows64bitsTestCase1(Rows64bitsTestCase): 2344 close = 0 2345 2346 2347class Rows64bitsTestCase2(Rows64bitsTestCase): 2348 close = 1 2349 2350 2351# Test for appending zero-sized arrays 2352class ZeroSizedTestCase(common.TempFileMixin, TestCase): 2353 open_mode = 'a' 2354 2355 def setUp(self): 2356 super(ZeroSizedTestCase, self).setUp() 2357 2358 # Create an EArray 2359 ea = self.h5file.create_earray('/', 'test', 2360 atom=Int32Atom(), shape=(3, 0)) 2361 # Append a single row 2362 ea.append([[1], [2], [3]]) 2363 2364 def test01_canAppend(self): 2365 """Appending zero length array.""" 2366 2367 fileh = self.h5file 2368 ea = fileh.root.test 2369 np = numpy.empty(shape=(3, 0), dtype='int32') 2370 ea.append(np) 2371 self.assertEqual(ea.nrows, 1, "The number of rows should be 1.") 2372 2373 def test02_appendWithWrongShape(self): 2374 """Appending zero length array with wrong dimension.""" 2375 2376 fileh = self.h5file 2377 ea = fileh.root.test 2378 np = numpy.empty(shape=(3, 0, 3), dtype='int32') 2379 self.assertRaises(ValueError, ea.append, np) 2380 2381 2382# Test for dealing with multidimensional atoms 2383class MDAtomTestCase(common.TempFileMixin, TestCase): 2384 2385 def test01a_append(self): 2386 """Append a row to a (unidimensional) EArray with a MD tables.Atom.""" 2387 2388 # Create an EArray 2389 ea = self.h5file.create_earray('/', 'test', 2390 atom=Int32Atom((2, 2)), shape=(0,)) 2391 if self.reopen: 2392 self._reopen('a') 2393 ea = self.h5file.root.test 2394 # Append one row 2395 ea.append([[[1, 3], [4, 5]]]) 2396 self.assertEqual(ea.nrows, 1) 2397 if common.verbose: 2398 print("First row-->", ea[0]) 2399 self.assertTrue(allequal(ea[0], numpy.array([[1, 3], [4, 5]], 'i4'))) 2400 2401 def test01b_append(self): 2402 """Append several rows to a (unidimensional) EArray with a MD 2403 tables.Atom.""" 2404 2405 # Create an EArray 2406 ea = self.h5file.create_earray('/', 'test', 2407 atom=Int32Atom((2, 2)), shape=(0,)) 2408 if self.reopen: 2409 self._reopen('a') 2410 ea = self.h5file.root.test 2411 # Append three rows 2412 ea.append([[[1]], [[2]], [[3]]]) # Simple broadcast 2413 self.assertEqual(ea.nrows, 3) 2414 if common.verbose: 2415 print("Third row-->", ea[2]) 2416 self.assertTrue(allequal(ea[2], numpy.array([[3, 3], [3, 3]], 'i4'))) 2417 2418 def test02a_append(self): 2419 """Append a row to a (multidimensional) EArray with a 2420 MD tables.Atom.""" 2421 2422 # Create an EArray 2423 ea = self.h5file.create_earray('/', 'test', 2424 atom=Int32Atom((2,)), shape=(0, 3)) 2425 if self.reopen: 2426 self._reopen('a') 2427 ea = self.h5file.root.test 2428 # Append one row 2429 ea.append([[[1, 3], [4, 5], [7, 9]]]) 2430 self.assertEqual(ea.nrows, 1) 2431 if common.verbose: 2432 print("First row-->", ea[0]) 2433 self.assertTrue(allequal(ea[0], numpy.array( 2434 [[1, 3], [4, 5], [7, 9]], 'i4'))) 2435 2436 def test02b_append(self): 2437 """Append several rows to a (multidimensional) EArray with a MD 2438 tables.Atom.""" 2439 2440 # Create an EArray 2441 ea = self.h5file.create_earray('/', 'test', 2442 atom=Int32Atom((2,)), shape=(0, 3)) 2443 if self.reopen: 2444 self._reopen('a') 2445 ea = self.h5file.root.test 2446 # Append three rows 2447 ea.append([[[1, -3], [4, -5], [-7, 9]], 2448 [[-1, 3], [-4, 5], [7, -8]], 2449 [[-2, 3], [-5, 5], [7, -9]]]) 2450 self.assertEqual(ea.nrows, 3) 2451 if common.verbose: 2452 print("Third row-->", ea[2]) 2453 self.assertTrue(allequal( 2454 ea[2], numpy.array([[-2, 3], [-5, 5], [7, -9]], 'i4'))) 2455 2456 def test03a_MDMDMD(self): 2457 """Complex append of a MD array in a MD EArray with a 2458 MD tables.Atom.""" 2459 2460 # Create an EArray 2461 ea = self.h5file.create_earray('/', 'test', atom=Int32Atom((2, 4)), 2462 shape=(0, 2, 3)) 2463 if self.reopen: 2464 self._reopen('a') 2465 ea = self.h5file.root.test 2466 # Append three rows 2467 # The shape of the atom should be added at the end of the arrays 2468 a = numpy.arange(2 * 3*2*4, dtype='i4').reshape((2, 3, 2, 4)) 2469 ea.append([a * 1, a*2, a*3]) 2470 self.assertEqual(ea.nrows, 3) 2471 if common.verbose: 2472 print("Third row-->", ea[2]) 2473 self.assertTrue(allequal(ea[2], a * 3)) 2474 2475 def test03b_MDMDMD(self): 2476 "Complex append of a MD array in a MD EArray with a MD atom (II)." 2477 # Create an EArray 2478 ea = self.h5file.create_earray('/', 'test', atom=Int32Atom((2, 4)), 2479 shape=(2, 0, 3)) 2480 if self.reopen: 2481 self._reopen('a') 2482 ea = self.h5file.root.test 2483 # Append three rows 2484 # The shape of the atom should be added at the end of the arrays 2485 a = numpy.arange(2 * 3*2*4, dtype='i4').reshape((2, 1, 3, 2, 4)) 2486 ea.append(a * 1) 2487 ea.append(a * 2) 2488 ea.append(a * 3) 2489 self.assertEqual(ea.nrows, 3) 2490 if common.verbose: 2491 print("Third row-->", ea[:, 2, ...]) 2492 self.assertTrue(allequal(ea[:, 2, ...], a.reshape((2, 3, 2, 4))*3)) 2493 2494 def test03c_MDMDMD(self): 2495 "Complex append of a MD array in a MD EArray with a MD atom (III)." 2496 # Create an EArray 2497 ea = self.h5file.create_earray('/', 'test', atom=Int32Atom((2, 4)), 2498 shape=(2, 3, 0)) 2499 if self.reopen: 2500 self._reopen('a') 2501 ea = self.h5file.root.test 2502 # Append three rows 2503 # The shape of the atom should be added at the end of the arrays 2504 a = numpy.arange(2 * 3*2*4, dtype='i4').reshape((2, 3, 1, 2, 4)) 2505 ea.append(a * 1) 2506 ea.append(a * 2) 2507 ea.append(a * 3) 2508 self.assertEqual(ea.nrows, 3) 2509 if common.verbose: 2510 print("Third row-->", ea[:, :, 2, ...]) 2511 self.assertTrue(allequal(ea[:, :, 2, ...], a.reshape((2, 3, 2, 4))*3)) 2512 2513 2514class MDAtomNoReopen(MDAtomTestCase): 2515 reopen = False 2516 2517 2518class MDAtomReopen(MDAtomTestCase): 2519 reopen = True 2520 2521 2522class AccessClosedTestCase(common.TempFileMixin, TestCase): 2523 2524 def setUp(self): 2525 super(AccessClosedTestCase, self).setUp() 2526 self.array = self.h5file.create_earray(self.h5file.root, 'array', 2527 atom=Int32Atom(), shape=(0, 10)) 2528 self.array.append(numpy.zeros((10, 10))) 2529 2530 def test_read(self): 2531 self.h5file.close() 2532 self.assertRaises(tables.ClosedNodeError, self.array.read) 2533 2534 def test_getitem(self): 2535 self.h5file.close() 2536 self.assertRaises(tables.ClosedNodeError, self.array.__getitem__, 0) 2537 2538 def test_setitem(self): 2539 self.h5file.close() 2540 self.assertRaises(tables.ClosedNodeError, self.array.__setitem__, 0, 0) 2541 2542 def test_append(self): 2543 self.h5file.close() 2544 self.assertRaises(tables.ClosedNodeError, self.array.append, 2545 numpy.zeros((10, 10))) 2546 2547 2548class TestCreateEArrayArgs(common.TempFileMixin, TestCase): 2549 obj = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 2550 where = '/' 2551 name = 'earray' 2552 atom = tables.Atom.from_dtype(obj.dtype) 2553 shape = (0,) + obj.shape[1:] 2554 title = 'title' 2555 filters = None 2556 expectedrows = 1000 2557 chunkshape = (1, 2) 2558 byteorder = None 2559 createparents = False 2560 2561 def test_positional_args_01(self): 2562 self.h5file.create_earray(self.where, self.name, 2563 self.atom, self.shape, 2564 self.title, self.filters, 2565 self.expectedrows, self.chunkshape) 2566 2567 self._reopen() 2568 2569 ptarr = self.h5file.get_node(self.where, self.name) 2570 2571 self.assertEqual(ptarr.title, self.title) 2572 self.assertEqual(ptarr.shape, self.shape) 2573 self.assertEqual(ptarr.nrows, 0) 2574 self.assertEqual(ptarr.atom, self.atom) 2575 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2576 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2577 2578 def test_positional_args_02(self): 2579 ptarr = self.h5file.create_earray(self.where, self.name, 2580 self.atom, self.shape, 2581 self.title, 2582 self.filters, 2583 self.expectedrows, 2584 self.chunkshape) 2585 ptarr.append(self.obj) 2586 2587 self._reopen() 2588 2589 ptarr = self.h5file.get_node(self.where, self.name) 2590 nparr = ptarr.read() 2591 2592 self.assertEqual(ptarr.title, self.title) 2593 self.assertEqual(ptarr.shape, self.obj.shape) 2594 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2595 self.assertEqual(ptarr.atom, self.atom) 2596 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2597 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2598 self.assertTrue(allequal(self.obj, nparr)) 2599 2600 def test_positional_args_obj(self): 2601 self.h5file.create_earray(self.where, self.name, 2602 None, None, 2603 self.title, 2604 self.filters, 2605 self.expectedrows, 2606 self.chunkshape, 2607 self.byteorder, 2608 self.createparents, 2609 self.obj) 2610 2611 self._reopen() 2612 2613 ptarr = self.h5file.get_node(self.where, self.name) 2614 nparr = ptarr.read() 2615 2616 self.assertEqual(ptarr.title, self.title) 2617 self.assertEqual(ptarr.shape, self.obj.shape) 2618 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2619 self.assertEqual(ptarr.atom, self.atom) 2620 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2621 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2622 self.assertTrue(allequal(self.obj, nparr)) 2623 2624 def test_kwargs_obj(self): 2625 self.h5file.create_earray(self.where, self.name, title=self.title, 2626 chunkshape=self.chunkshape, 2627 obj=self.obj) 2628 2629 self._reopen() 2630 2631 ptarr = self.h5file.get_node(self.where, self.name) 2632 nparr = ptarr.read() 2633 2634 self.assertEqual(ptarr.title, self.title) 2635 self.assertEqual(ptarr.shape, self.obj.shape) 2636 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2637 self.assertEqual(ptarr.atom, self.atom) 2638 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2639 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2640 self.assertTrue(allequal(self.obj, nparr)) 2641 2642 def test_kwargs_atom_shape_01(self): 2643 ptarr = self.h5file.create_earray(self.where, self.name, 2644 title=self.title, 2645 chunkshape=self.chunkshape, 2646 atom=self.atom, shape=self.shape) 2647 ptarr.append(self.obj) 2648 2649 self._reopen() 2650 2651 ptarr = self.h5file.get_node(self.where, self.name) 2652 nparr = ptarr.read() 2653 2654 self.assertEqual(ptarr.title, self.title) 2655 self.assertEqual(ptarr.shape, self.obj.shape) 2656 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2657 self.assertEqual(ptarr.atom, self.atom) 2658 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2659 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2660 self.assertTrue(allequal(self.obj, nparr)) 2661 2662 def test_kwargs_atom_shape_02(self): 2663 ptarr = self.h5file.create_earray(self.where, self.name, 2664 title=self.title, 2665 chunkshape=self.chunkshape, 2666 atom=self.atom, shape=self.shape) 2667 #ptarr.append(self.obj) 2668 2669 self._reopen() 2670 2671 ptarr = self.h5file.get_node(self.where, self.name) 2672 2673 self.assertEqual(ptarr.title, self.title) 2674 self.assertEqual(ptarr.shape, self.shape) 2675 self.assertEqual(ptarr.nrows, 0) 2676 self.assertEqual(ptarr.atom, self.atom) 2677 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2678 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2679 2680 def test_kwargs_obj_atom(self): 2681 ptarr = self.h5file.create_earray(self.where, self.name, 2682 title=self.title, 2683 chunkshape=self.chunkshape, 2684 obj=self.obj, 2685 atom=self.atom) 2686 2687 self._reopen() 2688 2689 ptarr = self.h5file.get_node(self.where, self.name) 2690 nparr = ptarr.read() 2691 2692 self.assertEqual(ptarr.title, self.title) 2693 self.assertEqual(ptarr.shape, self.obj.shape) 2694 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2695 self.assertEqual(ptarr.atom, self.atom) 2696 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2697 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2698 self.assertTrue(allequal(self.obj, nparr)) 2699 2700 def test_kwargs_obj_shape(self): 2701 ptarr = self.h5file.create_earray(self.where, self.name, 2702 title=self.title, 2703 chunkshape=self.chunkshape, 2704 obj=self.obj, 2705 shape=self.shape) 2706 2707 self._reopen() 2708 2709 ptarr = self.h5file.get_node(self.where, self.name) 2710 nparr = ptarr.read() 2711 2712 self.assertEqual(ptarr.title, self.title) 2713 self.assertEqual(ptarr.shape, self.obj.shape) 2714 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2715 self.assertEqual(ptarr.atom, self.atom) 2716 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2717 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2718 self.assertTrue(allequal(self.obj, nparr)) 2719 2720 def test_kwargs_obj_atom_shape(self): 2721 ptarr = self.h5file.create_earray(self.where, self.name, 2722 title=self.title, 2723 chunkshape=self.chunkshape, 2724 obj=self.obj, 2725 atom=self.atom, 2726 shape=self.shape) 2727 2728 self._reopen() 2729 2730 ptarr = self.h5file.get_node(self.where, self.name) 2731 nparr = ptarr.read() 2732 2733 self.assertEqual(ptarr.title, self.title) 2734 self.assertEqual(ptarr.shape, self.obj.shape) 2735 self.assertEqual(ptarr.nrows, self.obj.shape[0]) 2736 self.assertEqual(ptarr.atom, self.atom) 2737 self.assertEqual(ptarr.atom.dtype, self.atom.dtype) 2738 self.assertEqual(ptarr.chunkshape, self.chunkshape) 2739 self.assertTrue(allequal(self.obj, nparr)) 2740 2741 def test_kwargs_obj_atom_error(self): 2742 atom = tables.Atom.from_dtype(numpy.dtype('complex')) 2743 #shape = self.shape + self.shape 2744 self.assertRaises(TypeError, 2745 self.h5file.create_earray, 2746 self.where, 2747 self.name, 2748 title=self.title, 2749 obj=self.obj, 2750 atom=atom) 2751 2752 def test_kwargs_obj_shape_error(self): 2753 #atom = tables.Atom.from_dtype(numpy.dtype('complex')) 2754 shape = self.shape + self.shape 2755 self.assertRaises(TypeError, 2756 self.h5file.create_earray, 2757 self.where, 2758 self.name, 2759 title=self.title, 2760 obj=self.obj, 2761 shape=shape) 2762 2763 def test_kwargs_obj_atom_shape_error_01(self): 2764 atom = tables.Atom.from_dtype(numpy.dtype('complex')) 2765 #shape = self.shape + self.shape 2766 self.assertRaises(TypeError, 2767 self.h5file.create_earray, 2768 self.where, 2769 self.name, 2770 title=self.title, 2771 obj=self.obj, 2772 atom=atom, 2773 shape=self.shape) 2774 2775 def test_kwargs_obj_atom_shape_error_02(self): 2776 #atom = tables.Atom.from_dtype(numpy.dtype('complex')) 2777 shape = self.shape + self.shape 2778 self.assertRaises(TypeError, 2779 self.h5file.create_earray, 2780 self.where, 2781 self.name, 2782 title=self.title, 2783 obj=self.obj, 2784 atom=self.atom, 2785 shape=shape) 2786 2787 def test_kwargs_obj_atom_shape_error_03(self): 2788 atom = tables.Atom.from_dtype(numpy.dtype('complex')) 2789 shape = self.shape + self.shape 2790 self.assertRaises(TypeError, 2791 self.h5file.create_earray, 2792 self.where, 2793 self.name, 2794 title=self.title, 2795 obj=self.obj, 2796 atom=atom, 2797 shape=shape) 2798 2799 2800def suite(): 2801 theSuite = unittest.TestSuite() 2802 niter = 1 2803 # common.heavy = 1 # uncomment this only for testing purposes 2804 2805 # theSuite.addTest(unittest.makeSuite(BasicWriteTestCase)) 2806 # theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase1)) 2807 # theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase2)) 2808 for n in range(niter): 2809 theSuite.addTest(unittest.makeSuite(BasicWriteTestCase)) 2810 theSuite.addTest(unittest.makeSuite(Basic2WriteTestCase)) 2811 theSuite.addTest(unittest.makeSuite(Basic3WriteTestCase)) 2812 theSuite.addTest(unittest.makeSuite(Basic4WriteTestCase)) 2813 theSuite.addTest(unittest.makeSuite(Basic5WriteTestCase)) 2814 theSuite.addTest(unittest.makeSuite(Basic6WriteTestCase)) 2815 theSuite.addTest(unittest.makeSuite(Basic7WriteTestCase)) 2816 theSuite.addTest(unittest.makeSuite(Basic8WriteTestCase)) 2817 theSuite.addTest(unittest.makeSuite(EmptyEArrayTestCase)) 2818 theSuite.addTest(unittest.makeSuite(Empty2EArrayTestCase)) 2819 theSuite.addTest(unittest.makeSuite(SlicesEArrayTestCase)) 2820 theSuite.addTest(unittest.makeSuite(Slices2EArrayTestCase)) 2821 theSuite.addTest(unittest.makeSuite(EllipsisEArrayTestCase)) 2822 theSuite.addTest(unittest.makeSuite(Ellipsis2EArrayTestCase)) 2823 theSuite.addTest(unittest.makeSuite(Ellipsis3EArrayTestCase)) 2824 theSuite.addTest(unittest.makeSuite(ZlibComprTestCase)) 2825 theSuite.addTest(unittest.makeSuite(ZlibShuffleTestCase)) 2826 theSuite.addTest(unittest.makeSuite(BloscComprTestCase)) 2827 theSuite.addTest(unittest.makeSuite(BloscShuffleTestCase)) 2828 theSuite.addTest(unittest.makeSuite(LZOComprTestCase)) 2829 theSuite.addTest(unittest.makeSuite(LZOShuffleTestCase)) 2830 theSuite.addTest(unittest.makeSuite(Bzip2ComprTestCase)) 2831 theSuite.addTest(unittest.makeSuite(Bzip2ShuffleTestCase)) 2832 theSuite.addTest(unittest.makeSuite(FloatTypeTestCase)) 2833 theSuite.addTest(unittest.makeSuite(ComplexTypeTestCase)) 2834 theSuite.addTest(unittest.makeSuite(StringTestCase)) 2835 theSuite.addTest(unittest.makeSuite(String2TestCase)) 2836 theSuite.addTest(unittest.makeSuite(StringComprTestCase)) 2837 theSuite.addTest(unittest.makeSuite( 2838 SizeOnDiskInMemoryPropertyTestCase)) 2839 theSuite.addTest(unittest.makeSuite(OffsetStrideTestCase)) 2840 theSuite.addTest(unittest.makeSuite(Fletcher32TestCase)) 2841 theSuite.addTest(unittest.makeSuite(AllFiltersTestCase)) 2842 theSuite.addTest(unittest.makeSuite(CloseCopyTestCase)) 2843 theSuite.addTest(unittest.makeSuite(OpenCopyTestCase)) 2844 theSuite.addTest(unittest.makeSuite(CopyIndex1TestCase)) 2845 theSuite.addTest(unittest.makeSuite(CopyIndex2TestCase)) 2846 theSuite.addTest(unittest.makeSuite(CopyIndex3TestCase)) 2847 theSuite.addTest(unittest.makeSuite(CopyIndex4TestCase)) 2848 theSuite.addTest(unittest.makeSuite(CopyIndex5TestCase)) 2849 theSuite.addTest(unittest.makeSuite(TruncateOpenTestCase)) 2850 theSuite.addTest(unittest.makeSuite(TruncateCloseTestCase)) 2851 theSuite.addTest(unittest.makeSuite(ZeroSizedTestCase)) 2852 theSuite.addTest(unittest.makeSuite(MDAtomNoReopen)) 2853 theSuite.addTest(unittest.makeSuite(MDAtomReopen)) 2854 theSuite.addTest(unittest.makeSuite(AccessClosedTestCase)) 2855 theSuite.addTest(unittest.makeSuite(TestCreateEArrayArgs)) 2856 if common.heavy: 2857 theSuite.addTest(unittest.makeSuite(Slices3EArrayTestCase)) 2858 theSuite.addTest(unittest.makeSuite(Slices4EArrayTestCase)) 2859 theSuite.addTest(unittest.makeSuite(Ellipsis4EArrayTestCase)) 2860 theSuite.addTest(unittest.makeSuite(Ellipsis5EArrayTestCase)) 2861 theSuite.addTest(unittest.makeSuite(Ellipsis6EArrayTestCase)) 2862 theSuite.addTest(unittest.makeSuite(Ellipsis7EArrayTestCase)) 2863 theSuite.addTest(unittest.makeSuite(MD3WriteTestCase)) 2864 theSuite.addTest(unittest.makeSuite(MD5WriteTestCase)) 2865 theSuite.addTest(unittest.makeSuite(MD6WriteTestCase)) 2866 theSuite.addTest(unittest.makeSuite(MD7WriteTestCase)) 2867 theSuite.addTest(unittest.makeSuite(MD10WriteTestCase)) 2868 theSuite.addTest(unittest.makeSuite(CopyIndex6TestCase)) 2869 theSuite.addTest(unittest.makeSuite(CopyIndex7TestCase)) 2870 theSuite.addTest(unittest.makeSuite(CopyIndex8TestCase)) 2871 theSuite.addTest(unittest.makeSuite(CopyIndex9TestCase)) 2872 theSuite.addTest(unittest.makeSuite(CopyIndex10TestCase)) 2873 theSuite.addTest(unittest.makeSuite(CopyIndex11TestCase)) 2874 theSuite.addTest(unittest.makeSuite(CopyIndex12TestCase)) 2875 theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase1)) 2876 theSuite.addTest(unittest.makeSuite(Rows64bitsTestCase2)) 2877 2878 return theSuite 2879 2880 2881if __name__ == '__main__': 2882 common.parse_argv(sys.argv) 2883 common.print_versions() 2884 unittest.main(defaultTest='suite') 2885 2886## Local Variables: 2887## mode: python 2888## py-indent-offset: 4 2889## tab-width: 4 2890## End: 2891