def test_hdf(tempdir): """Test saving/loading with HDF5.""" data = np.random.random((10, 10, 10, 10)) dims = ('time', 'x', 'y', 'z') coords = {label: np.linspace(0, 1, 10) for label in dims} rate = 1 ts = TimeSeriesX.create(data, rate, coords=coords, dims=dims, name="test") filename = osp.join(tempdir, "timeseries.h5") ts.to_hdf(filename) with h5py.File(filename, 'r') as hfile: assert "data" in hfile assert "dims" in hfile assert "coords" in hfile assert "name" in list(hfile['/'].attrs.keys()) assert "ptsa_version" in hfile.attrs assert "created" in hfile.attrs loaded = TimeSeriesX.from_hdf(filename) assert (loaded.data == data).all() for coord in loaded.coords: assert (loaded.coords[coord] == ts.coords[coord]).all() for n, dim in enumerate(dims): assert loaded.dims[n] == dim assert loaded.name == "test" ts_with_attrs = TimeSeriesX.create(data, rate, coords=coords, dims=dims, name="test", attrs=dict(a=1, b=[1, 2])) ts_with_attrs.to_hdf(filename) loaded = TimeSeriesX.from_hdf(filename) for key in ts_with_attrs.attrs: assert ts_with_attrs.attrs[key] == loaded.attrs[key]
def test_hdf(tempdir): """Test saving/loading with HDF5.""" data = np.random.random((10, 10, 10, 10)) dims = ('time', 'x', 'y', 'z') coords = {label: np.linspace(0, 1, 10) for label in dims} rate = 1 ts = TimeSeriesX.create(data, rate, coords=coords, dims=dims, name="test") filename = osp.join(tempdir, "timeseries.h5") ts.to_hdf(filename) with h5py.File(filename, 'r') as hfile: assert "data" in hfile assert "dims" in hfile assert "coords" in hfile assert "name" in list(hfile['/'].attrs.keys()) loaded = TimeSeriesX.from_hdf(filename) assert (loaded.data == data).all() for coord in loaded.coords: assert (loaded.coords[coord] == ts.coords[coord]).all() for n, dim in enumerate(dims): assert loaded.dims[n] == dim assert loaded.name == "test" ts_with_attrs = TimeSeriesX.create(data, rate, coords=coords, dims=dims, name="test", attrs=dict(a=1, b=[1, 2])) ts_with_attrs.to_hdf(filename) loaded = TimeSeriesX.from_hdf(filename) for key in ts_with_attrs.attrs: assert ts_with_attrs.attrs[key] == loaded.attrs[key]