def test02_copy(self): """Checking (X)Array.copy() method ('numetic' flavor)""" srcfile = test_filename("oldflavor_numeric.h5") tmpfile = tempfile.mktemp(".h5") shutil.copy(srcfile, tmpfile) try: # Open the HDF5 with old numeric flavor with tables.open_file(tmpfile, "r+") as h5file: # Copy to another location self.assertWarns(FlavorWarning, h5file.root.array1.copy, '/', 'array1copy') h5file.root.array2.copy('/', 'array2copy') h5file.root.carray1.copy('/', 'carray1copy') h5file.root.carray2.copy('/', 'carray2copy') h5file.root.vlarray1.copy('/', 'vlarray1copy') h5file.root.vlarray2.copy('/', 'vlarray2copy') if self.close: h5file.close() h5file = tables.open_file(tmpfile) else: h5file.flush() # Assert other properties in array self.assertEqual(h5file.root.array1copy.flavor, 'numeric') self.assertEqual(h5file.root.array2copy.flavor, 'python') self.assertEqual(h5file.root.carray1copy.flavor, 'numeric') self.assertEqual(h5file.root.carray2copy.flavor, 'python') self.assertEqual(h5file.root.vlarray1copy.flavor, 'numeric') self.assertEqual(h5file.root.vlarray2copy.flavor, 'python') finally: os.remove(tmpfile)
def test01_readTable(self): """Checking backward compatibility of old formats of tables.""" if common.verbose: print('\n', '-=' * 30) print("Running %s.test01_readTable..." % self.__class__.__name__) # Create an instance of an HDF5 Table with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) h5file = tables.open_file(test_filename(self.h5fname), "r") try: table = h5file.get_node("/tuple0") # Read the 100 records result = [rec['var2'] for rec in table] if common.verbose: print("Nrows in", table._v_pathname, ":", table.nrows) print("Last record in table ==>", rec) print("Total selected records in table ==> ", len(result)) self.assertEqual(len(result), 100) finally: h5file.close()
class ContiguousCompoundAppendTestCase(common.TestFileMixin, TestCase): """Test for appending data to native contiguous compound datasets.""" h5fname = test_filename('non-chunked-table.h5') def test(self): self.assertIn('/test_var/structure variable', self.h5file) self.h5file.close() # Do a copy to a temporary to avoid modifying the original file h5fname_copy = tempfile.mktemp(".h5") shutil.copy(self.h5fname, h5fname_copy) # Reopen in 'a'ppend mode try: self.h5file = tables.open_file(h5fname_copy, 'a') except IOError: # Problems for opening (probably not permisions to write the file) return tbl = self.h5file.get_node('/test_var/structure variable') # Try to add rows to a non-chunked table (this should raise an error) self.assertRaises(tables.HDF5ExtError, tbl.append, [(4.0, 5.0, [2.0, 3.0], 'd')]) # Appending using the Row interface self.assertRaises(tables.HDF5ExtError, tbl.row.append) # Remove the file copy self.h5file.close() # Close the handler first os.remove(h5fname_copy)
class EnumTestCase(common.TestFileMixin, TestCase): """Test for enumerated datatype. See ftp://ftp.hdfgroup.org/HDF5/current/src/unpacked/test/enum.c. """ h5fname = test_filename('smpl_enum.h5') def test(self): self.assertIn('/EnumTest', self.h5file) arr = self.h5file.get_node('/EnumTest') self.assertIsInstance(arr, tables.Array) enum = arr.get_enum() expectedEnum = tables.Enum(['RED', 'GREEN', 'BLUE', 'WHITE', 'BLACK']) self.assertEqual(enum, expectedEnum) data = list(arr.read()) expectedData = [ enum[name] for name in [ 'RED', 'GREEN', 'BLUE', 'WHITE', 'BLACK', 'RED', 'GREEN', 'BLUE', 'WHITE', 'BLACK' ] ] self.assertEqual(data, expectedData)
class ExtendibleTestCase(common.TestFileMixin, TestCase): """Test for extendible datasets. See the example programs in the Introduction to HDF5. """ h5fname = test_filename('smpl_SDSextendible.h5') def test(self): self.assertIn('/ExtendibleArray', self.h5file) arr = self.h5file.get_node('/ExtendibleArray') self.assertIsInstance(arr, tables.EArray) self.assertEqual(arr.byteorder, 'big') self.assertEqual(arr.atom.type, 'int32') self.assertEqual(arr.shape, (10, 5)) self.assertEqual(arr.extdim, 0) self.assertEqual(len(arr), 10) data = arr.read() expectedData = numpy.array( [[1, 1, 1, 3, 3], [1, 1, 1, 3, 3], [1, 1, 1, 0, 0], [ 2, 0, 0, 0, 0 ], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0], [2, 0, 0, 0, 0]], dtype=arr.atom.type) self.assertTrue(common.areArraysEqual(data, expectedData))
class PaddedArrayTestCase(common.TestFileMixin, TestCase): """Test for H5T_COMPOUND (Table) datatype with padding. Regression test for issue gh-734 itemsize.h5 was created with h5py with the array `expectedData` (see below) in the table `/Test`: 'A' and 'B' are 4 + 4 bytes, with 8 bytes padding. $ h5ls -v itemsize.h5 Test Dataset {3/3} Location: 1:800 Links: 1 Storage: 48 logical bytes, 48 allocated bytes, 100.00% utilization Type: struct { "A" +0 native unsigned int "B" +4 native unsigned int } 16 bytes """ h5fname = test_filename('itemsize.h5') def test(self): arr = self.h5file.get_node('/Test') data = arr.read() expectedData = numpy.array( [(1, 11), (2, 12), (3, 13)], dtype={ 'names': ['A', 'B'], 'formats': ['<u4', '<u4'], 'offsets': [0, 4], 'itemsize': 16 }) self.assertTrue(common.areArraysEqual(data, expectedData))
class SzipTestCase(common.TestFileMixin, TestCase): """Test for native HDF5 files with datasets compressed with szip.""" h5fname = test_filename('test_szip.h5') def test(self): self.assertIn('/dset_szip', self.h5file) arr = self.h5file.get_node('/dset_szip') filters = ("Filters(complib='szip', shuffle=False, bitshuffle=False, " "fletcher32=False, least_significant_digit=None)") self.assertEqual(repr(arr.filters), filters)
class VLArrayTestCase(common.TestFileMixin, TestCase): h5fname = test_filename("flavored_vlarrays-format1.6.h5") def test01_backCompat(self): """Checking backward compatibility with old flavors of VLArray.""" # Check that we can read the contents without problems (nor warnings!) vlarray1 = self.h5file.root.vlarray1 self.assertEqual(vlarray1.flavor, "numeric") vlarray2 = self.h5file.root.vlarray2 self.assertEqual(vlarray2.flavor, "python") self.assertEqual(vlarray2[1], [b'5', b'6', b'77'])
class ChunkedCompoundTestCase(common.TestFileMixin, TestCase): """Test for a more complex and chunked compound structure. This is generated by a chunked version of the example in ftp://ftp.ncsa.uiuc.edu/HDF/files/hdf5/samples/compound2.c. """ h5fname = test_filename('smpl_compound_chunked.h5') def test(self): self.assertIn('/CompoundChunked', self.h5file) tbl = self.h5file.get_node('/CompoundChunked') self.assertIsInstance(tbl, tables.Table) self.assertEqual( tbl.colnames, ['a_name', 'c_name', 'd_name', 'e_name', 'f_name', 'g_name']) self.assertEqual(tbl.coltypes['a_name'], 'int32') self.assertEqual(tbl.coldtypes['a_name'].shape, ()) self.assertEqual(tbl.coltypes['c_name'], 'string') self.assertEqual(tbl.coldtypes['c_name'].shape, ()) self.assertEqual(tbl.coltypes['d_name'], 'int16') self.assertEqual(tbl.coldtypes['d_name'].shape, (5, 10)) self.assertEqual(tbl.coltypes['e_name'], 'float32') self.assertEqual(tbl.coldtypes['e_name'].shape, ()) self.assertEqual(tbl.coltypes['f_name'], 'float64') self.assertEqual(tbl.coldtypes['f_name'].shape, (10, )) self.assertEqual(tbl.coltypes['g_name'], 'uint8') self.assertEqual(tbl.coldtypes['g_name'].shape, ()) for m in range(len(tbl)): row = tbl[m] # This version of the loop seems to fail because of ``iterrows()``. # for (m, row) in enumerate(tbl): self.assertEqual(row['a_name'], m) self.assertEqual(row['c_name'], b"Hello!") dRow = row['d_name'] for n in range(5): for o in range(10): self.assertEqual(dRow[n][o], m + n + o) self.assertAlmostEqual(row['e_name'], m * 0.96, places=6) fRow = row['f_name'] for n in range(10): self.assertAlmostEqual(fRow[n], m * 1024.9637) self.assertEqual(row['g_name'], ord('m'))
class ObjectReferenceTestCase(common.TestFileMixin, TestCase): h5fname = test_filename('test_ref_array1.mat') def test_node_var(self): array = self.h5file.get_node('/ANN/my_arr') self.assertEqual(array.shape, (1, 3)) def test_ref_utf_str(self): array = self.h5file.get_node('/ANN/my_arr') self.assertTrue( common.areArraysEqual(array[0][0][0], numpy.array([0, 0], dtype=numpy.uint64)))
def test01_open(self): """Checking opening of (X)Array (old 'numeric' flavor)""" # Open the HDF5 with old numeric flavor h5fname = test_filename("oldflavor_numeric.h5") with tables.open_file(h5fname) as h5file: # Assert other properties in array self.assertEqual(h5file.root.array1.flavor, 'numeric') self.assertEqual(h5file.root.array2.flavor, 'python') self.assertEqual(h5file.root.carray1.flavor, 'numeric') self.assertEqual(h5file.root.carray2.flavor, 'python') self.assertEqual(h5file.root.vlarray1.flavor, 'numeric') self.assertEqual(h5file.root.vlarray2.flavor, 'python')
class MatlabFileTestCase(common.TestFileMixin, TestCase): h5fname = test_filename('matlab_file.mat') def test_unicode(self): array = self.h5file.get_node('/', 'a') self.assertEqual(array.shape, (3, 1)) # in Python 3 this will be the same as the test above def test_string(self): array = self.h5file.get_node('/', 'a') self.assertEqual(array.shape, (3, 1)) def test_numpy_str(self): array = self.h5file.get_node(numpy.str_('/'), numpy.str_('a')) self.assertEqual(array.shape, (3, 1))
class TimeTestCase(common.TestFileMixin, TestCase): # Open a PYTABLES_FORMAT_VERSION=1.x file h5fname = test_filename("time-table-vlarray-1_x.h5") def test00_table(self): """Checking backward compatibility with old TimeXX types (tables).""" # Check that we can read the contents without problems (nor warnings!) table = self.h5file.root.table self.assertEqual(table.byteorder, "little") def test01_vlarray(self): """Checking backward compatibility with old TimeXX types (vlarrays).""" # Check that we can read the contents without problems (nor warnings!) vlarray4 = self.h5file.root.vlarray4 self.assertEqual(vlarray4.byteorder, "little") vlarray8 = self.h5file.root.vlarray4 self.assertEqual(vlarray8.byteorder, "little")
class ObjectReferenceRecursiveTestCase(common.TestFileMixin, TestCase): h5fname = test_filename('test_ref_array2.mat') def test_var(self): array = self.h5file.get_node('/var') self.assertEqual(array.shape, (3, 1)) def test_ref_str(self): array = self.h5file.get_node('/var') self.assertTrue(common.areArraysEqual( array[1][0][0], numpy.array([[116], [101], [115], [116]], dtype=numpy.uint16))) def test_double_ref(self): array = self.h5file.get_node('/var') self.assertTrue(common.areArraysEqual( array[2][0][0][1][0], numpy.array([[105], [110], [115], [105], [100], [101]], dtype=numpy.uint16)))
class ContiguousCompoundTestCase(common.TestFileMixin, TestCase): """Test for support of native contiguous compound datasets. This example has been provided by Dav Clark. """ h5fname = test_filename('non-chunked-table.h5') def test(self): self.assertIn('/test_var/structure variable', self.h5file) tbl = self.h5file.get_node('/test_var/structure variable') self.assertIsInstance(tbl, tables.Table) self.assertEqual( tbl.colnames, ['a', 'b', 'c', 'd']) self.assertEqual(tbl.coltypes['a'], 'float64') self.assertEqual(tbl.coldtypes['a'].shape, ()) self.assertEqual(tbl.coltypes['b'], 'float64') self.assertEqual(tbl.coldtypes['b'].shape, ()) self.assertEqual(tbl.coltypes['c'], 'float64') self.assertEqual(tbl.coldtypes['c'].shape, (2,)) self.assertEqual(tbl.coltypes['d'], 'string') self.assertEqual(tbl.coldtypes['d'].shape, ()) for row in tbl.iterrows(): self.assertEqual(row['a'], 3.0) self.assertEqual(row['b'], 4.0) self.assertTrue(allequal(row['c'], numpy.array([2.0, 3.0], dtype="float64"))) self.assertEqual(row['d'], b"d") self.h5file.close()
class F64LETestCase(NumericTestCase): h5fname = test_filename('smpl_f64le.h5') type = 'float64' byteorder = 'little'
def setUp(self): self.h5fname = test_filename(self.FILENAME % self.format) super(BackCompatAttrsTestCase, self).setUp()
def setUp(self): self.h5fname = test_filename(self.FILENAME % self.format) super().setUp()
class I64LETestCase(NumericTestCase): h5fname = test_filename('smpl_i64le.h5') type = 'int64' byteorder = 'little'
def setUp(self): super().setUp() filename = common.test_filename('times-nested-be.h5') self.h5file = tb.open_file(filename, 'r')
class I32LETestCase(NumericTestCase): h5fname = test_filename('smpl_i32le.h5') type = 'int32' byteorder = 'little'
class F64BETestCase(NumericTestCase): h5fname = test_filename('smpl_f64be.h5') type = 'float64' byteorder = 'big'
class I64BETestCase(NumericTestCase): h5fname = test_filename('smpl_i64be.h5') type = 'int64' byteorder = 'big'
def setUp(self): super(BigEndianTestCase, self).setUp() filename = test_filename('times-nested-be.h5') self.h5file = tables.open_file(filename, 'r')
class Indexes2_1TestCase(IndexesTestCase): h5fname = test_filename("indexes_2_1.h5")
class I32BETestCase(NumericTestCase): h5fname = test_filename('smpl_i32be.h5') type = 'int32' byteorder = 'big'
class ReadFloatTestCase(common.TestFileMixin, TestCase): h5fname = test_filename("float.h5") nrows = 5 ncols = 6 def setUp(self): super(ReadFloatTestCase, self).setUp() x = numpy.arange(self.ncols) y = numpy.arange(self.nrows) y.shape = (self.nrows, 1) self.values = x + y def test01_read_float16(self): dtype = "float16" if hasattr(numpy, dtype): ds = getattr(self.h5file.root, dtype) self.assertFalse(isinstance(ds, tables.UnImplemented)) self.assertEqual(ds.shape, (self.nrows, self.ncols)) self.assertEqual(ds.dtype, dtype) self.assertTrue( common.allequal(ds.read(), self.values.astype(dtype))) else: with self.assertWarns(UserWarning): ds = getattr(self.h5file.root, dtype) self.assertTrue(isinstance(ds, tables.UnImplemented)) def test02_read_float32(self): dtype = "float32" ds = getattr(self.h5file.root, dtype) self.assertFalse(isinstance(ds, tables.UnImplemented)) self.assertEqual(ds.shape, (self.nrows, self.ncols)) self.assertEqual(ds.dtype, dtype) self.assertTrue(common.allequal(ds.read(), self.values.astype(dtype))) def test03_read_float64(self): dtype = "float64" ds = getattr(self.h5file.root, dtype) self.assertFalse(isinstance(ds, tables.UnImplemented)) self.assertEqual(ds.shape, (self.nrows, self.ncols)) self.assertEqual(ds.dtype, dtype) self.assertTrue(common.allequal(ds.read(), self.values.astype(dtype))) def test04_read_longdouble(self): dtype = "longdouble" if hasattr(tables, "Float96Atom") or hasattr(tables, "Float128Atom"): ds = getattr(self.h5file.root, dtype) self.assertFalse(isinstance(ds, tables.UnImplemented)) self.assertEqual(ds.shape, (self.nrows, self.ncols)) self.assertEqual(ds.dtype, dtype) self.assertTrue( common.allequal(ds.read(), self.values.astype(dtype))) if hasattr(tables, "Float96Atom"): self.assertEqual(ds.dtype, "float96") elif hasattr(tables, "Float128Atom"): self.assertEqual(ds.dtype, "float128") else: # XXX: check # the behavior depends on the HDF5 lib configuration try: with self.assertWarns(UserWarning): ds = getattr(self.h5file.root, dtype) self.assertTrue(isinstance(ds, tables.UnImplemented)) except AssertionError: from tables.utilsextension import _broken_hdf5_long_double if not _broken_hdf5_long_double(): ds = getattr(self.h5file.root, dtype) self.assertEqual(ds.dtype, "float64") def test05_read_quadprecision_float(self): # XXX: check try: with self.assertWarns(UserWarning): ds = self.h5file.root.quadprecision self.assertTrue(isinstance(ds, tables.UnImplemented)) except AssertionError: # NOTE: it would be nice to have some sort of message that warns # against the potential precision loss: the quad-precision # dataset actually uses 128 bits for each element, not just # 80 bits (longdouble) ds = self.h5file.root.quadprecision self.assertEqual(ds.dtype, "longdouble")
class Indexes2_0TestCase(IndexesTestCase): h5fname = common.test_filename("indexes_2_0.h5")