def test_copy_stream(self, stream_name, tmpdir): stream = getattr(self, stream_name) filename = str(tmpdir.join('copy.hdf5')) with hdf5.open(filename, 'w', template=stream) as f5w: if stream_name == 'fh': assert f5w.bps == 2 else: assert not hasattr(f5w, 'bps') self.check(stream, f5w) header0 = f5w.header0 self.check(stream, header0) f5w.write(self.data) # Check repr works, though ignore the contents for now. repr(f5w) with h5py.File(filename, 'r') as h5: assert set(h5.keys()) == {'header', 'payload'} header = hdf5.HDF5Header.fromfile(h5) self.check(stream, header) assert header == header0 payload = hdf5.HDF5Payload.fromfile(h5, header) assert_array_equal(payload.data, self.data) assert_array_equal(payload[:], self.data) with hdf5.open(filename, 'r') as f5r: self.check(stream, f5r) assert f5r.header0 == header0 data = f5r.read() assert_array_equal(data, self.data) # Check repr works, though ignore the contents for now. repr(f5r) # Should also work when closed. repr(f5w) repr(f5r)
def test_stream_as_output(self, stream_name, tmpdir): stream = getattr(self, stream_name) filename = str(tmpdir.join('copy.hdf5')) with hdf5.open(filename, 'w', template=stream) as f5w: stream.seek(0) stream.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(stream, f5r) data = f5r.read() assert_array_equal(data, self.data)
def test_complex_baseband(self, tmpdir): filename = str(tmpdir.join('copy.hdf5')) with baseband.vdif.open(baseband.data.SAMPLE_AROCHIME_VDIF, 'rs', sample_rate=800 * u.MHz / 2048) as fh: data = fh.read() fh.seek(0) with hdf5.open(filename, 'w', template=fh) as f5w: assert f5w.complex_data fh.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(fh, f5r) recovered = f5r.read() assert_array_equal(recovered, data)
def test_copy_stream_copy(self, stream_name, tmpdir): # Check that we can copy ourselves and not mess up depending # on raw vs encoded data. stream = getattr(self, stream_name) filename = str(tmpdir.join('copy.hdf5')) with hdf5.open(filename, 'w', template=stream) as f5w: f5w.write(self.data) copyname = str(tmpdir.join('copycopy.hdf5')) with hdf5.open(filename, 'r') as f5r: with hdf5.open(copyname, 'w', template=f5r) as f5w: if stream_name == 'fh': assert f5w.bps == 2 else: assert not hasattr(f5w, 'bps') self.check(stream, f5w) header0 = f5w.header0 self.check(stream, header0) f5w.write(self.data)
def test_complex_stream_as_i1(self, tmpdir): # Not a particularly good idea, but just to show it is possible. filename = str(tmpdir.join('copy.hdf5')) with hdf5.open(filename, 'w', template=self.wrapped, encoded_dtype='i1') as f5w: assert not f5w.complex_data assert f5w.header0.encoded_dtype == 'i1' self.wrapped.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(self.wrapped, f5r, ('sample_shape', 'dtype', 'sample_rate', 'time')) assert f5r.header0.encoded_dtype == 'i1' recovered = f5r.read() # Will not recover correctly, given the use of int, but should be # within tolerance. assert np.allclose(recovered, self.data, atol=0.5)
def test_stream_as_f2(self, tmpdir): stream = self.wrapped filename = str(tmpdir.join('copy.hdf5')) with hdf5.open(filename, 'w', template=stream, encoded_dtype='<f2') as f5w: assert f5w.header0.encoded_dtype == '<f2' assert f5w.header0.dtype == '=f4' assert not f5w.complex_data stream.seek(0) stream.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(f5w, f5r) assert f5r.header0.encoded_dtype == '<f2' assert f5r.dtype == '=f4' data = f5r.read() assert np.allclose(data, self.data, atol=0, rtol=np.finfo('f2').eps)
def test_complex_stream(self, tmpdir): filename = str(tmpdir.join('copy.hdf5')) with baseband.vdif.open(baseband.data.SAMPLE_AROCHIME_VDIF, 'rs', sample_rate=800 * u.MHz / 2048) as fh: wrapped = SetAttribute(fh) data = wrapped.read() wrapped.seek(0) with hdf5.open(filename, 'w', template=wrapped) as f5w: assert f5w.complex_data assert f5w.header0.encoded_dtype == 'c8' wrapped.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(wrapped, f5r, ('sample_shape', 'dtype', 'sample_rate', 'time')) assert f5r.header0.encoded_dtype == 'c8' recovered = f5r.read() # Cannot recover exactly, given scaling, but should be within # tolerance for float16. assert_array_equal(recovered, data)
def test_complex_stream_as_c4(self, tmpdir): filename = str(tmpdir.join('copy.hdf5')) with baseband.vdif.open(baseband.data.SAMPLE_AROCHIME_VDIF, 'rs', sample_rate=800 * u.MHz / 2048) as fh: wrapped = SetAttribute(fh) data = wrapped.read() wrapped.seek(0) with hdf5.open(filename, 'w', template=wrapped, encoded_dtype='c4') as f5w: assert f5w.complex_data assert f5w.header0.encoded_dtype == hdf5.payload.DTYPE_C4 wrapped.read(out=f5w) with hdf5.open(filename, 'r') as f5r: self.check(wrapped, f5r, ('sample_shape', 'dtype', 'sample_rate', 'time')) assert f5r.header0.encoded_dtype == hdf5.payload.DTYPE_C4 recovered = f5r.read() # Cannot recover exactly, given scaling, but should be within # tolerance for float16. assert np.allclose(recovered, data, atol=0, rtol=np.finfo('f2').eps)