def test03_read_float64(self): dtype = "float64" ds = getattr(self.fileh.root, dtype) self.assertFalse(isinstance(ds, UnImplemented)) self.assertEqual(ds.shape, (self.ncols, self.nrows)) self.assertEqual(ds.dtype, dtype) data = ds.read() common.allequal(data, self.values)
def test01_read_float16(self): dtype = "float16" if hasattr(numpy, dtype): ds = getattr(self.fileh.root, dtype) self.assertFalse(isinstance(ds, UnImplemented)) self.assertEqual(ds.shape, (self.ncols, self.nrows)) self.assertEqual(ds.dtype, dtype) data = ds.read() common.allequal(data, self.values) else: ds = self.assertWarns(UserWarning, getattr, self.fileh.root, dtype) self.assertTrue(isinstance(ds, UnImplemented))
def test(self): self.assertIn('/test_var/structure variable', self.h5file) tbl = self.h5file.get_node('/test_var/structure variable') self.assertIsInstance(tbl, tb.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( common.allequal(row['c'], np.array([2.0, 3.0], dtype="float64"))) self.assertEqual(row['d'], b"d") self.h5file.close()
def test08a_modifyingRows(self): """Checking modifying just one row at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk.field('y')[:] = -1 table.modifyRows(6, 7, 1, chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2,2), 'Float64')-1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal assert isinstance(data, NumArray) # Check the type assert data.type() == ycol.type() assert allequal(ycol, data, "numarray")
def test07b_modifyingRows(self): """Checking modifying several rows at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk.field('y')[:] = -1 table.cols[3:6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((3,2,2), 'Float64')-1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal assert isinstance(data, NumArray) # Check the type assert data.type() == ycol.type() assert allequal(ycol, data, "numarray")
def test01a_basicTableRead(self): """Checking the return of a numarray in read().""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[:] if common.verbose: print "Type of read:", type(data) print "Formats of the record:", data._formats print "First 3 elements of read:", data[:3] # Check the type of the recarray assert isinstance(data, records.RecArray) # Check the value of some columns # A flat column col = table.cols.x[:3] assert isinstance(col, NumArray) npcol = zeros((3,2), type="Int32") if common.verbose: print "Plain column:" print "read column-->", col print "should look like-->", npcol assert allequal(col, npcol, "numarray") # A nested column col = table.cols.Info[:3] assert isinstance(col, records.RecArray) npcol = self._infozeros if common.verbose: print "Nested column:" print "read column-->", col print "should look like-->", npcol assert col.descr == npcol.descr assert str(col) == str(npcol)
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 test07a_modifyingRows(self): """Checking modifying several rows at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk.field('y')[:] = -1 table.modifyRows(3, 6, 1, rows=chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = zeros((3, 2, 2), 'Float64') - 1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray"))
def test01b_basicTableRead(self): """Checking the return of a numarray in read() (strided version).""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[::3] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.descr print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, records.RecArray)) # Check the value of some columns # A flat column col = table.cols.x[:9:3] self.assertTrue(isinstance(col, NumArray)) npcol = zeros((3, 2), dtype="Int32") if common.verbose: print "Plain column:" print "read column-->", col print "should look like-->", npcol self.assertTrue(allequal(col, npcol, "numarray")) # A nested column col = table.cols.Info[:9:3] self.assertTrue(isinstance(col, records.RecArray)) npcol = self._infozeros if common.verbose: print "Nested column:" print "read column-->", col print "should look like-->", npcol self.assertEqual(col.descr, npcol.descr) self.assertEqual(str(col), str(npcol))
def test05b_modifyingColumns(self): """Checking modifying several columns at once.""" table = self.fileh.root.table xcol = ones((3, 2), 'Int32') ycol = ones((3, 2, 2), 'Float64') zcol = zeros((3, ), 'UInt8') table.modifyColumns(3, 6, 1, [xcol, ycol, zcol], ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(data, ycol, "numarray"))
def test01_readTableChar(self): """Checking column conversion into NumPy in read(). Char flavor """ table = self.h5file.root.table table.flavor = "numpy" for colname in table.colnames: numcol = table.read(field=colname) typecol = table.coltypes[colname] itemsizecol = table.description._v_dtypes[colname].base.itemsize nctypecode = numcol.dtype.char if typecol == "string": if itemsizecol > 1: orignumcol = np.array(['abcd'] * self.nrows, dtype='S4') else: orignumcol = np.array(['a'] * self.nrows, dtype='S1') if common.verbose: print("Typecode of NumPy column read:", nctypecode) print("Should look like:", 'c') print("Itemsize of column:", itemsizecol) print("Shape of NumPy column read:", numcol.shape) print("Should look like:", orignumcol.shape) print("First 3 elements of read col:", numcol[:3]) # Check that both NumPy objects are equal self.assertTrue(allequal(numcol, orignumcol, "numpy"))
def test07a_modifyingRows(self): """Checking modifying several rows at once (using modify_rows).""" table = self.h5file.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk['y'][:] = -1 table.modify_rows(3, 6, 1, rows=chunk) if self.close: self._reopen(mode='a') table = self.h5file.root.table ycol = np.zeros((3, 2, 2), 'float64') - 1 data = table.cols.y[3:6] if common.verbose: print("Type of read:", type(data)) print("Description of the record:", data.dtype.descr) print("First 3 elements of read:", data[:3]) print("Length of the data read:", len(data)) # Check that both NumPy objects are equal self.assertIsInstance(data, np.ndarray) # Check the type self.assertEqual(data.dtype.descr, ycol.dtype.descr) if common.verbose: print("ycol-->", ycol) print("data-->", data) self.assertTrue(allequal(ycol, data, "numpy"))
def WriteRead(self, testArray): if common.verbose: print('\n', '-=' * 30) print("Running test for array with typecode '%s'" % testArray.dtype.char, end=' ') print("for class check:", self.title) # Create an instance of HDF5 Table self.h5fname = tempfile.mktemp(".h5") try: with tables.open_file(self.h5fname, mode="w") as self.h5file: self.root = self.h5file.root # Create the array under root and name 'somearray' a = testArray self.h5file.create_array(self.root, 'somearray', a, "Some array") # Re-open the file in read-only mode with tables.open_file(self.h5fname, mode="r") as self.h5file: self.root = self.h5file.root # Read the saved array b = self.root.somearray.read() # For cases that read returns a python type instead of a # numpy type if not hasattr(b, "shape"): b = np.np.array(b, dtype=a.dtype.str) # Compare them. They should be equal. # if not allequal(a,b, "numpy") and common.verbose: if common.verbose: print("Array written:", a) print("Array written shape:", a.shape) print("Array written itemsize:", a.itemsize) print("Array written type:", a.dtype.char) print("Array read:", b) print("Array read shape:", b.shape) print("Array read itemsize:", b.itemsize) print("Array read type:", b.dtype.char) type_ = self.root.somearray.atom.type # Check strictly the array equality self.assertEqual(type(a), type(b)) self.assertEqual(a.shape, b.shape) self.assertEqual(a.shape, self.root.somearray.shape) self.assertEqual(a.dtype, b.dtype) if a.dtype.char[0] == "S": self.assertEqual(type_, "string") else: self.assertEqual(a.dtype.base.name, type_) self.assertTrue(allequal(a, b, "numpy")) finally: # Then, delete the file if os.path.exists(self.h5fname): os.remove(self.h5fname)
def test02_updateAttribute(self): """Checking the modification of a numpy attribute.""" group = self.h5file.root.group g_attrs = group._v_attrs g_attrs.numpy1 = np.zeros((1, 2), dtype='int16') if self.close: self._reopen(mode='a') group = self.h5file.root.group g_attrs = group._v_attrs # Update this attribute g_attrs.numpy1 = np.ones((1, 2), dtype='int16') # Check that we can retrieve a numpy object data = g_attrs.numpy1 npcomp = np.ones((1, 2), dtype='int16') # Check that both NumPy objects are equal self.assertIsInstance(data, np.ndarray) # Check the type self.assertEqual(data.dtype.descr, npcomp.dtype.descr) if common.verbose: print("npcomp-->", npcomp) print("data-->", data) self.assertTrue(allequal(npcomp, data, "numpy"))
def test08b_modifyingRows(self): """Checking modifying just one row at once (using cols accessor).""" table = self.h5file.root.table # Read a chunk of the table chunk = table[3:4] # Modify it somewhat chunk['y'][:] = -1 table.cols[6] = chunk if self.close: self._reopen(mode='a') table = self.h5file.root.table # Check that some column has been actually modified ycol = np.zeros((2, 2), 'float64') - 1 data = table.cols.y[6] if common.verbose: print("Type of read:", type(data)) print("Description of the record:", data.dtype.descr) print("First 3 elements of read:", data[:3]) print("Length of the data read:", len(data)) # Check that both NumPy objects are equal self.assertIsInstance(data, np.ndarray) # Check the type self.assertEqual(data.dtype.descr, ycol.dtype.descr) if common.verbose: print("ycol-->", ycol) print("data-->", data) self.assertTrue(allequal(ycol, data, "numpy"))
def test08b_modifyingRows(self): """Checking modifying just one row at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk['y'][:] = -1 table.cols[6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2, 2), 'Float64') - 1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray"))
def test08a_modifyingRows(self): """Checking modifying just one row at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk['y'][:] = -1 table.modifyRows(6, 7, 1, chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2, 2), 'float64') - 1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.dtype.descr print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) # Check that both NumPy objects are equal assert isinstance(data, ndarray) # Check the type assert data.dtype.descr == ycol.dtype.descr if common.verbose: print "ycol-->", ycol print "data-->", data assert allequal(ycol, data, "numpy")
def _test(self): self.assert_("/test_var/structure variable" in self.h5file) tbl = self.h5file.getNode("/test_var/structure variable") self.assert_(isinstance(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.assert_(allequal(row["c"], numpy.array([2.0, 3.0], dtype="float64"))) self.assertEqual(row["d"], "d") self.h5file.close()
def test01_readTableChar(self): """Checking column conversion into Numeric in read(). Char flavor""" table = self.fileh.root.table table.flavor = "numeric" for colname in table.colnames: numcol = table.read(field=colname) typecol = table.coltypes[colname] itemsizecol = table.description._v_dtypes[colname].base.itemsize nctypecode = numcol.typecode() if typecol == "string": if itemsizecol > 1: orignumcol = array(['abcd'] * self.nrows, typecode='c') else: orignumcol = array(['a'] * self.nrows, typecode='c') orignumcol.shape = (self.nrows, ) if common.verbose: print "Typecode of Numeric column read:", nctypecode print "Should look like:", 'c' print "Itemsize of column:", itemsizecol print "Shape of Numeric column read:", numcol.shape print "Should look like:", orignumcol.shape print "First 3 elements of read col:", numcol[:3] # Check that both Numeric objects are equal self.assertTrue(allequal(numcol, orignumcol, "numeric"))
def test05b_modifyingColumns(self): """Checking modifying several columns at once.""" table = self.fileh.root.table xcol = ones((3, 2), 'Int32') ycol = ones((3, 2, 2), 'Float64') zcol = zeros((3,), 'UInt8') table.modifyColumns(3, 6, 1, [xcol, ycol, zcol], ['x', 'y', 'z']) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(data, ycol, "numarray"))
def test08b_modifyingRows(self): """Checking modifying just one row at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[3] # Modify it somewhat chunk['y'][:] = -1 table.cols[6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((2, 2), 'Float64')-1 data = table.cols.y[6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray"))
def test03b_Compare64EArray(self): """Comparing several written and read 64-bit time values in an EArray.""" # Create test EArray with data. ea = self.h5file.create_earray('/', 'test', tb.Time64Atom(), shape=(0, 2)) # Size of the test. nrows = ea.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. # nrows = 10 for i in range(nrows): j = i * 2 ea.append(((j + 0.012, j + 1 + 0.012), )) self._reopen() # Check the written data. arr = self.h5file.root.test.read() self.h5file.close() orig_val = np.arange(0, nrows * 2, dtype=np.int32) + 0.012 orig_val.shape = (nrows, 2) if common.verbose: print("Original values:", orig_val) print("Retrieved values:", arr) self.assertTrue(common.allequal(arr, orig_val), "Stored and retrieved values do not match.")
def test02_readCoordsChar(self): """Column conversion into Numeric in readCoords(). Chars""" table = self.fileh.root.table table.flavor = "numeric" coords = (1, 2, 3) self.nrows = len(coords) for colname in table.colnames: numcol = table.readCoordinates(coords, field=colname) typecol = table.coltypes[colname] itemsizecol = table.description._v_dtypes[colname].base.itemsize nctypecode = numcol.typecode() if typecol == "string": if itemsizecol > 1: orignumcol = array(['abcd']*self.nrows, typecode='c') else: orignumcol = array(['a']*self.nrows, typecode='c') orignumcol.shape=(self.nrows,) if common.verbose: print "Typecode of Numeric column read:", nctypecode print "Should look like:", 'c' print "Itemsize of column:", itemsizecol print "Shape of Numeric column read:", numcol.shape print "Should look like:", orignumcol.shape print "First 3 elements of read col:", numcol[:3] # Check that both Numeric objects are equal self.assertTrue(allequal(numcol, orignumcol, "numeric"))
def _test(self): self.assert_('/test_var/structure variable' in self.h5file) tbl = self.h5file.getNode('/test_var/structure variable') self.assert_(isinstance(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.assert_( allequal(row['c'], numpy.array([2.0, 3.0], dtype="float64"))) self.assertEqual(row['d'], "d") self.h5file.close()
def test02_readCoordsChar(self): """Column conversion into NumPy in readCoords(). Chars""" table = self.fileh.root.table table.flavor = "numpy" coords = (1, 2, 3) self.nrows = len(coords) for colname in table.colnames: numcol = table.readCoordinates(coords, field=colname) typecol = table.coltypes[colname] itemsizecol = table.description._v_dtypes[colname].base.itemsize nctypecode = numcol.dtype.char if typecol == "string": if itemsizecol > 1: orignumcol = array(['abcd'] * self.nrows, dtype='S4') else: orignumcol = array(['a'] * self.nrows, dtype='S1') if common.verbose: print "Typecode of NumPy column read:", nctypecode print "Should look like:", 'c' print "Itemsize of column:", itemsizecol print "Shape of NumPy column read:", numcol.shape print "Should look like:", orignumcol.shape print "First 3 elements of read col:", numcol[:3] # Check that both NumPy objects are equal assert allequal(numcol, orignumcol, "numpy")
def test02_read_float32(self): dtype = "float32" ds = getattr(self.h5file.root, dtype) self.assertNotIsInstance(ds, tb.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 test07a_modifyingRows(self): """Checking modifying several rows at once (using modifyRows).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk.field('y')[:] = -1 table.modifyRows(3, 6, 1, rows=chunk) if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table ycol = zeros((3, 2, 2), 'Float64')-1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) if common.verbose: print "ycol-->", ycol print "data-->", data # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check the type self.assertEqual(data.type(), ycol.type()) self.assertTrue(allequal(ycol, data, "numarray"))
def test03b_Compare64EArray(self): "Comparing several written and read 64-bit time values in an EArray." # Create test EArray with data. h5file = tables.openFile( self.h5fname, 'w', title = "Test for comparing Time64 E arrays") ea = h5file.createEArray( '/', 'test', tables.Time64Atom(), shape=(0, 2)) # Size of the test. nrows = ea.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. ##nrows = 10 for i in xrange(nrows): j = i * 2 ea.append(((j + 0.012, j + 1 + 0.012),)) h5file.close() # Check the written data. h5file = tables.openFile(self.h5fname) arr = h5file.root.test.read() h5file.close() orig_val = numpy.arange(0, nrows*2, dtype=numpy.int32) + 0.012 orig_val.shape = (nrows, 2) if common.verbose: print "Original values:", orig_val print "Retrieved values:", arr self.assertTrue(allequal(arr, orig_val), "Stored and retrieved values do not match.")
def test01b_Compare64VLArray(self): "Comparing several written and read 64-bit time values in a VLArray." # Create test VLArray with data. h5file = tables.open_file(self.h5fname, "w", title="Test for comparing Time64 VL arrays") vla = h5file.create_vlarray("/", "test", self.myTime64Atom) # Size of the test. nrows = vla.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. # nrows = 10 for i in range(nrows): j = i * 2 vla.append((j + 0.012, j + 1 + 0.012)) h5file.close() # Check the written data. h5file = tables.open_file(self.h5fname) arr = h5file.root.test.read() h5file.close() arr = numpy.array(arr) orig_val = numpy.arange(0, nrows * 2, dtype=numpy.int32) + 0.012 orig_val.shape = (nrows, 1, 2) if common.verbose: print("Original values:", orig_val) print("Retrieved values:", arr) self.assertTrue(allequal(arr, orig_val), "Stored and retrieved values do not match.")
def test07b_modifyingRows(self): """Checking modifying several rows at once (using cols accessor).""" table = self.fileh.root.table # Read a chunk of the table chunk = table[0:3] # Modify it somewhat chunk['y'][:] = -1 table.cols[3:6] = chunk if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table # Check that some column has been actually modified ycol = zeros((3,2,2), 'float64')-1 data = table.cols.y[3:6] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.dtype.descr print "First 3 elements of read:", data[:3] print "Length of the data read:", len(data) # Check that both NumPy objects are equal assert isinstance(data, ndarray) # Check the type assert data.dtype.descr == ycol.dtype.descr if common.verbose: print "ycol-->", ycol print "data-->", data assert allequal(ycol, data, "numpy")
def test04_read_longdouble(self): dtype = "longdouble" if "Float96Atom" in globals() or "Float128Atom" in globals(): ds = getattr(self.fileh.root, dtype) self.assertFalse(isinstance(ds, 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 "Float96Atom" in globals(): self.assertEqual(ds.dtype, "float96") elif "Float128Atom" in globals(): self.assertEqual(ds.dtype, "float128") else: # XXX: check # the behavior depends on the HDF5 lib configuration try: ds = self.assertWarns(UserWarning, getattr, self.fileh.root, dtype) self.assertTrue(isinstance(ds, UnImplemented)) except AssertionError: from tables.utilsextension import _broken_hdf5_long_double if not _broken_hdf5_long_double(): ds = getattr(self.fileh.root, dtype) self.assertEqual(ds.dtype, "float64")
def test01b_basicTableRead(self): """Checking the return of a NumPy in read() (strided version).""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table[::3] if common.verbose: print "Type of read:", type(data) print "Description of the record:", data.dtype.descr print "First 3 elements of read:", data[:3] # Check that both NumPy objects are equal assert isinstance(data, ndarray) # Check the value of some columns # A flat column col = table.cols.x[:9:3] assert isinstance(col, ndarray) npcol = zeros((3, 2), dtype="int32") assert allequal(col, npcol, "numpy") # A nested column col = table.cols.Info[:9:3] assert isinstance(col, ndarray) dtype = [('value', '%sc16' % byteorder), ('y2', '%sf8' % byteorder), ('Info2', [('name', '|S2'), ('value', '%sc16' % byteorder, (2, )), ('y3', '%sf8' % byteorder, (2, ))]), ('name', '|S2'), ('z2', '|u1')] npcol = zeros((3, ), dtype=dtype) assert col.dtype.descr == npcol.dtype.descr if common.verbose: print "col-->", col print "npcol-->", npcol # A copy() is needed in case the buffer can be in different segments assert col.copy().data == npcol.data
def _test(self): self.assertTrue('/test_var/structure variable' in self.h5file) tbl = self.h5file.get_node('/test_var/structure variable') self.assertTrue(isinstance(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()
def test03b_Compare64EArray(self): "Comparing several written and read 64-bit time values in an EArray." # Create test EArray with data. h5file = tables.openFile(self.h5fname, 'w', title="Test for comparing Time64 E arrays") ea = h5file.createEArray('/', 'test', tables.Time64Atom(), shape=(0, 2)) # Size of the test. nrows = ea.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. ##nrows = 10 for i in xrange(nrows): j = i * 2 ea.append(((j + 0.012, j + 1 + 0.012), )) h5file.close() # Check the written data. h5file = tables.openFile(self.h5fname) arr = h5file.root.test.read() h5file.close() orig_val = numpy.arange(0, nrows * 2, dtype=numpy.int32) + 0.012 orig_val.shape = (nrows, 2) if common.verbose: print "Original values:", orig_val print "Retrieved values:", arr self.assertTrue(allequal(arr, orig_val), "Stored and retrieved values do not match.")
def WriteRead(self, testArray): if common.verbose: print '\n', '-=' * 30 print "Running test for array with typecode '%s'" % \ testArray.dtype.char, print "for class check:", self.title # Create an instance of HDF5 Table self.file = tempfile.mktemp(".h5") self.fileh = openFile(self.file, mode="w") self.root = self.fileh.root # Create the array under root and name 'somearray' a = testArray self.fileh.createArray(self.root, 'somearray', a, "Some array") # Close the file self.fileh.close() # Re-open the file in read-only mode self.fileh = openFile(self.file, mode="r") self.root = self.fileh.root # Read the saved array b = self.root.somearray.read() # For cases that read returns a python type instead of a numpy type if not hasattr(b, "shape"): b = array(b, dtype=a.dtype.str) # Compare them. They should be equal. #if not allequal(a,b, "numpy") and common.verbose: if common.verbose: print "Array written:", a print "Array written shape:", a.shape print "Array written itemsize:", a.itemsize print "Array written type:", a.dtype.char print "Array read:", b print "Array read shape:", b.shape print "Array read itemsize:", b.itemsize print "Array read type:", b.dtype.char type_ = self.root.somearray.atom.type # Check strictly the array equality assert type(a) == type(b) assert a.shape == b.shape assert a.shape == self.root.somearray.shape assert a.dtype == b.dtype if a.dtype.char[0] == "S": assert type_ == "string" else: assert a.dtype.base.name == type_ assert allequal(a, b, "numpy") self.fileh.close() # Then, delete the file os.remove(self.file) return
def WriteRead(self, testArray): if common.verbose: print '\n', '-=' * 30 print "Running test for array with typecode '%s'" % \ testArray.dtype.char, print "for class check:", self.title # Create an instance of HDF5 Table self.file = tempfile.mktemp(".h5") self.fileh = openFile(self.file, mode = "w") self.root = self.fileh.root # Create the array under root and name 'somearray' a = testArray self.fileh.createArray(self.root, 'somearray', a, "Some array") # Close the file self.fileh.close() # Re-open the file in read-only mode self.fileh = openFile(self.file, mode = "r") self.root = self.fileh.root # Read the saved array b = self.root.somearray.read() # For cases that read returns a python type instead of a numpy type if not hasattr(b, "shape"): b = array(b, dtype=a.dtype.str) # Compare them. They should be equal. #if not allequal(a,b, "numpy") and common.verbose: if common.verbose: print "Array written:", a print "Array written shape:", a.shape print "Array written itemsize:", a.itemsize print "Array written type:", a.dtype.char print "Array read:", b print "Array read shape:", b.shape print "Array read itemsize:", b.itemsize print "Array read type:", b.dtype.char type_ = self.root.somearray.atom.type # Check strictly the array equality assert type(a) == type(b) assert a.shape == b.shape assert a.shape == self.root.somearray.shape assert a.dtype == b.dtype if a.dtype.char[0] == "S": assert type_ == "string" else: assert a.dtype.base.name == type_ assert allequal(a,b, "numpy") self.fileh.close() # Then, delete the file os.remove(self.file) return
def test01_readAttr(self): """Checking backward compatibility of old formats for attributes.""" if common.verbose: print('\n', '-=' * 30) print("Running %s.test01_readAttr..." % self.__class__.__name__) # Read old formats a = self.h5file.get_node("/a") scalar = numpy.array(1, dtype="int32") vector = numpy.array([1], dtype="int32") if self.format == "1.3": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertEqual(a.attrs.pythonscalar, 1) elif self.format == "1.4": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertTrue(allequal(a.attrs.pythonscalar, scalar))
def test00_CompareTable(self): "Comparing written unaligned time data with read data in a Table." # Create test Table with data. h5file = tables.openFile( self.h5fname, 'w', title = "Test for comparing Time tables") tbl = h5file.createTable('/', 'test', self.MyTimeRow) # Size of the test. nrows = tbl.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. ##nrows = 10 row = tbl.row for i in xrange(nrows): row['i8col'] = i row['t32col'] = i j = i * 2 row['t64col'] = (j+0.012, j+1+0.012) row.append() h5file.close() # Check the written data. h5file = tables.openFile(self.h5fname) recarr = h5file.root.test.read() h5file.close() # Int8 column. orig_val = numpy.arange(nrows, dtype=numpy.int8) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['i8col'][:] self.assert_( numpy.alltrue(recarr['i8col'][:] == orig_val), "Stored and retrieved values do not match.") # Time32 column. orig_val = numpy.arange(nrows, dtype=numpy.int32) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['t32col'][:] self.assert_( numpy.alltrue(recarr['t32col'][:] == orig_val), "Stored and retrieved values do not match.") # Time64 column. orig_val = numpy.arange(0, nrows*2, dtype=numpy.int32) + 0.012 orig_val.shape = (nrows, 2) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['t64col'][:] self.assert_( allequal(recarr['t64col'][:], orig_val, numpy.float64), "Stored and retrieved values do not match.")
def test00_CompareTable(self): "Comparing written unaligned time data with read data in a Table." # Create test Table with data. h5file = tables.openFile(self.h5fname, 'w', title="Test for comparing Time tables") tbl = h5file.createTable('/', 'test', self.MyTimeRow) # Size of the test. nrows = tbl.nrowsinbuf + 34 # Add some more rows than buffer. # Only for home checks; the value above should check better # the I/O with multiple buffers. ##nrows = 10 row = tbl.row for i in xrange(nrows): row['i8col'] = i row['t32col'] = i j = i * 2 row['t64col'] = (j + 0.012, j + 1 + 0.012) row.append() h5file.close() # Check the written data. h5file = tables.openFile(self.h5fname) recarr = h5file.root.test.read() h5file.close() # Int8 column. orig_val = numpy.arange(nrows, dtype=numpy.int8) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['i8col'][:] self.assertTrue(numpy.alltrue(recarr['i8col'][:] == orig_val), "Stored and retrieved values do not match.") # Time32 column. orig_val = numpy.arange(nrows, dtype=numpy.int32) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['t32col'][:] self.assertTrue(numpy.alltrue(recarr['t32col'][:] == orig_val), "Stored and retrieved values do not match.") # Time64 column. orig_val = numpy.arange(0, nrows * 2, dtype=numpy.int32) + 0.012 orig_val.shape = (nrows, 2) if common.verbose: print "Original values:", orig_val print "Retrieved values:", recarr['t64col'][:] self.assertTrue(allequal(recarr['t64col'][:], orig_val, numpy.float64), "Stored and retrieved values do not match.")
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 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 test01_readAttr(self): """Checking backward compatibility of old formats for attributes""" if common.verbose: print '\n', '-=' * 30 print "Running %s.test01_readAttr..." % self.__class__.__name__ # Read old formats filename = self._testFilename(self.file) self.fileh = open_file(filename % self.format, "r") a = self.fileh.get_node("/a") scalar = numpy.array(1, dtype="int32") vector = numpy.array([1], dtype="int32") if self.format == "1.3": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertEqual(a.attrs.pythonscalar, 1) elif self.format == "1.4": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertTrue(allequal(a.attrs.pythonscalar, scalar)) self.fileh.close()
def test01_readAttr(self): """Checking backward compatibility of old formats for attributes""" if common.verbose: print '\n', '-=' * 30 print "Running %s.test01_readAttr..." % self.__class__.__name__ # Read old formats filename = self._testFilename(self.file) self.fileh = openFile(filename % self.format, "r") a = self.fileh.getNode("/a") scalar = numpy.array(1, dtype="int32") vector = numpy.array([1], dtype="int32") if self.format == "1.3": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertEqual(a.attrs.pythonscalar, 1) elif self.format == "1.4": self.assertTrue(allequal(a.attrs.arrdim1, vector)) self.assertTrue(allequal(a.attrs.arrscalar, scalar)) self.assertTrue(allequal(a.attrs.pythonscalar, scalar)) self.fileh.close()
def test01_Compare64VLArray(self): """Comparing written 64-bit time data with read data in a VLArray.""" wtime = numpy.array((1234567890.123456,) * 2, numpy.float64) # Create test VLArray with data. vla = self.h5file.create_vlarray('/', 'test', self.myTime64Atom) vla.append(wtime) self._reopen() # Check the written data. rtime = self.h5file.root.test.read()[0][0] self.h5file.close() self.assertTrue(allequal(rtime, wtime), "Stored and retrieved values do not match.")
def test01_Compare64VLArray(self): """Comparing written 64-bit time data with read data in a VLArray.""" wtime = np.array((1_234_567_890.123456, ) * 2, np.float64) # Create test VLArray with data. vla = self.h5file.create_vlarray('/', 'test', self.myTime64Atom) vla.append(wtime) self._reopen() # Check the written data. rtime = self.h5file.root.test.read()[0][0] self.h5file.close() self.assertTrue(common.allequal(rtime, wtime), "Stored and retrieved values do not match.")
def test03_Compare64EArray(self): """Comparing written 64-bit time data with read data in an EArray.""" wtime = 1234567890.123456 # Create test EArray with data. ea = self.h5file.create_earray( '/', 'test', tables.Time64Atom(), shape=(0,)) ea.append((wtime,)) self._reopen() # Check the written data. rtime = self.h5file.root.test[0] self.h5file.close() self.assertTrue(allequal(rtime, wtime), "Stored and retrieved values do not match.")
def test01_readTableNum(self): """Checking column conversion into numarray in read(). Numerical.""" table = self.fileh.root.table table.flavor="numarray" for colname in table.colnames: numcol = table.read(field=colname) typecol = table.coltypes[colname] if typecol != "string": type_ = numcol.type() if common.verbose: print "Type of numarray column read:", type_ print "Should look like:", typecol orignumcol = ones(shape=self.nrows, dtype=numcol.type()) # Check that both numarray objects are equal self.assertTrue(allequal(numcol, orignumcol, "numarray"))
def test03_Compare64EArray(self): "Comparing written 64-bit time data with read data in an EArray." wtime = 1234567890.123456 # Create test EArray with data. h5file = tables.open_file(self.h5fname, "w", title="Test for comparing Time64 EArrays") ea = h5file.create_earray("/", "test", tables.Time64Atom(), shape=(0,)) ea.append((wtime,)) h5file.close() # Check the written data. h5file = tables.open_file(self.h5fname) rtime = h5file.root.test[0] h5file.close() self.assertTrue(allequal(rtime, wtime), "Stored and retrieved values do not match.")
def test01_Compare64VLArray(self): "Comparing written 64-bit time data with read data in a VLArray." wtime = numpy.array((1234567890.123456,) * 2, numpy.float64) # Create test VLArray with data. h5file = tables.open_file(self.h5fname, "w", title="Test for comparing Time64 VL arrays") vla = h5file.create_vlarray("/", "test", self.myTime64Atom) vla.append(wtime) h5file.close() # Check the written data. h5file = tables.open_file(self.h5fname) rtime = h5file.root.test.read()[0][0] h5file.close() self.assertTrue(allequal(rtime, wtime), "Stored and retrieved values do not match.")
def test01_readTableNum(self): """Checking column conversion into NumPy in read(). NumPy flavor""" table = self.fileh.root.table table.flavor = "numpy" for colname in table.colnames: numcol = table.read(field=colname) typecol = table.coltypes[colname] nctypecode = typeNA[numcol.dtype.char[0]] if typecol != "string": if common.verbose: print "Typecode of NumPy column read:", nctypecode print "Should look like:", typecol orignumcol = ones(shape=self.nrows, dtype=numcol.dtype.char) # Check that both NumPy objects are equal assert allequal(numcol, orignumcol, "numpy")
def test02_getWhereList(self): """Checking the return of numarray in getWhereList method.""" if self.close: self.fileh.close() self.fileh = openFile(self.file, "a") table = self.fileh.root.table data = table.getWhereList('z == 1') if common.verbose: print "Type of read:", type(data) print "First 3 elements of read:", data[:3] # Check that both numarray objects are equal self.assertTrue(isinstance(data, NumArray)) # Check that all columns have been selected self.assertEqual(len(data), 100) # Finally, check that the contents are ok self.assertTrue(allequal(data, arange(100, type="Int64"), "numarray"))
def test01_backCompat(self): """Checking backward compatibility with old flavors of VLArray""" # Open a PYTABLES_FORMAT_VERSION=1.6 file filename = self._testFilename("flavored_vlarrays-format1.6.h5") fileh = openFile(filename, "r") # Check that we can read the contents without problems (nor warnings!) vlarray1 = fileh.root.vlarray1 assert vlarray1.flavor == "numeric" if numeric_imported: assert allequal(vlarray1[1], Numeric.array([5, 6, 7], typecode='i'), "numeric") vlarray2 = fileh.root.vlarray2 assert vlarray2.flavor == "python" assert vlarray2[1] == ['5', '6', '77'] fileh.close()
def test00_Compare32VLArray(self): "Comparing written 32-bit time data with read data in a VLArray." wtime = numpy.array((1234567890,) * 2, numpy.int32) # Create test VLArray with data. h5file = tables.openFile( self.h5fname, 'w', title = "Test for comparing Time32 VL arrays") vla = h5file.createVLArray('/', 'test', self.myTime32Atom) vla.append(wtime) h5file.close() # Check the written data. h5file = tables.openFile(self.h5fname) rtime = h5file.root.test.read()[0][0] h5file.close() self.assertTrue(allequal(rtime, wtime), "Stored and retrieved values do not match.")