Store arrays in HDF5 file This saves several dask arrays into several datapaths in an HDF5 file. It creates the necessary datasets and handles clean file opening/closing. Parameters ---------- chunks: tuple or ``True`` Chunk shape, or ``True`` to pass the chunks from th
(filename, *args, chunks=True, **kwargs)
| 5704 | |
| 5705 | |
| 5706 | def to_hdf5(filename, *args, chunks=True, **kwargs): |
| 5707 | """Store arrays in HDF5 file |
| 5708 | |
| 5709 | This saves several dask arrays into several datapaths in an HDF5 file. |
| 5710 | It creates the necessary datasets and handles clean file opening/closing. |
| 5711 | |
| 5712 | Parameters |
| 5713 | ---------- |
| 5714 | chunks: tuple or ``True`` |
| 5715 | Chunk shape, or ``True`` to pass the chunks from the dask array. |
| 5716 | Defaults to ``True``. |
| 5717 | |
| 5718 | Examples |
| 5719 | -------- |
| 5720 | |
| 5721 | >>> da.to_hdf5('myfile.hdf5', '/x', x) # doctest: +SKIP |
| 5722 | |
| 5723 | or |
| 5724 | |
| 5725 | >>> da.to_hdf5('myfile.hdf5', {'/x': x, '/y': y}) # doctest: +SKIP |
| 5726 | |
| 5727 | Optionally provide arguments as though to ``h5py.File.create_dataset`` |
| 5728 | |
| 5729 | >>> da.to_hdf5('myfile.hdf5', '/x', x, compression='lzf', shuffle=True) # doctest: +SKIP |
| 5730 | |
| 5731 | >>> da.to_hdf5('myfile.hdf5', '/x', x, chunks=(10,20,30)) # doctest: +SKIP |
| 5732 | |
| 5733 | This can also be used as a method on a single Array |
| 5734 | |
| 5735 | >>> x.to_hdf5('myfile.hdf5', '/x') # doctest: +SKIP |
| 5736 | |
| 5737 | See Also |
| 5738 | -------- |
| 5739 | da.store |
| 5740 | h5py.File.create_dataset |
| 5741 | """ |
| 5742 | if len(args) == 1 and isinstance(args[0], dict): |
| 5743 | data = args[0] |
| 5744 | elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], Array): |
| 5745 | data = {args[0]: args[1]} |
| 5746 | else: |
| 5747 | raise ValueError("Please provide {'/data/path': array} dictionary") |
| 5748 | |
| 5749 | import h5py |
| 5750 | |
| 5751 | with h5py.File(filename, mode="a") as f: |
| 5752 | dsets = [ |
| 5753 | f.require_dataset( |
| 5754 | dp, |
| 5755 | shape=x.shape, |
| 5756 | dtype=x.dtype, |
| 5757 | chunks=tuple(c[0] for c in x.chunks) if chunks is True else chunks, |
| 5758 | **kwargs, |
| 5759 | ) |
| 5760 | for dp, x in data.items() |
| 5761 | ] |
| 5762 | store(list(data.values()), dsets) |
| 5763 |