# Access to 'miscellaneous dataset information'/get size of compressed dataset

**URL:** <https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582>\
**Category:** h5py\
**Created:** [June 10, 2021, 2:19pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582 "2021-06-10T14:19:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://forum.hdfgroup.org/u/jan)\
**Post date:** [June 10, 2021, 2:19pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/1 "2021-06-10T14:19:21Z")

</div>

Hi all,  
is there a way to get access to the information of a dataset which is displayed in the ‘General Object Info’ tab?  
Especially I need information about the compression of a compressed dataset, e.g. with GZIP. In ‘Miscellaneous Dataset Information’ I can see the compression ratio and the storage size.  
But if I try to get the size of the dataset with `<dataset>.size`, I only get the uncompressed value. Is this behavior correct?  
I’m using h5py=3.2.1, hdf5=1.12.0 and Python 3.9.5.

Thank you for your support!  
Jan

---

<div class="post-metadata">

**Author:** ![steven](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/steven/32/117_2.png) [@steven](https://forum.hdfgroup.org/u/steven)\
**Post date:** [June 10, 2021, 3:12pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/2 "2021-06-10T15:12:43Z")

</div>

Hi,  
I dont work with python, but the filter call has this information, and should pass it to chunk write. I am typing from a phone, so lets see what others have to say. Or you can dig around in the capi filters, see the function prototype and then see how it is used in python.

Steve

---

<div class="post-metadata">

**Author:** ![jan](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://forum.hdfgroup.org/u/jan)\
**Post date:** [June 11, 2021, 1:15pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/3 "2021-06-11T13:15:13Z")

</div>

Hi Steven,  
thank you for your suggestions. I have looked where the filters in Python are called, but even there the only available size is the one of the uncompressed data. I’m sorry but I’m not familiar in C, I think this is the reason why I didn’t find anything in the C API.  
Do you have any idea how I can get the information I want?

Jan

---

<div class="post-metadata">

**Author:** ![steven](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/steven/32/117_2.png) [@steven](https://forum.hdfgroup.org/u/steven)\
**Post date:** [June 11, 2021, 1:39pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/4 "2021-06-11T13:39:21Z")

</div>

Hi Jan,  
I am not so certain if I pointed you at the right direction, in the case my apologies. This is how the callback should look like, and you are interested in the `nbytes` passed to the function and the returned value. What makes it complex is the filters are usually implemented as shared objects – loaded by the library on demand. So you may not have direct access to it.

`typedef size_t (*H5Z_func_t) (unsigned int flags, size_t cd_nelmts, const unsigned int cd_values[], size_t nbytes, size_t *buf_size, void **buf)`

_If successful, the filter operation callback function returns the number of valid bytes of data contained in buf. In the case of failure, the return value is 0 (zero) and all pointer arguments are left unchanged._

`ratio = nbytes / ret_val`

my apologies if it lead you to the wrong direction. I am only familiar with the C API calls.

best: steve

---

<div class="post-metadata">

**Author:** ![jan](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://forum.hdfgroup.org/u/jan)\
**Post date:** [June 11, 2021, 2:22pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/5 "2021-06-11T14:22:20Z")

</div>

Hi Steve,  
thanks again for your suggestions and no worry, it was a great help for me. Meanwhile I’ve tried again to use the C API and I can get the correct storage value with `H5Dget_storage_size(dset)`, the type is `hsize_t` - that’s perfect, now I’ll have a look if this function is also available in Python.  
In C I don’t know how to get the size of the uncompressed dataset (so exactly the other way around like in Python).

Jan

---

<div class="post-metadata">

**Author:** ![rosborn](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/rosborn/32/118_2.png) [@rosborn](https://forum.hdfgroup.org/u/rosborn)\
**Post date:** [June 11, 2021, 3:18pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/6 "2021-06-11T15:18:35Z")

</div>

I would recommend looking at the h5py documentation ([https://docs.h5py.org/en/stable/high/dataset.html](https://docs.h5py.org/en/stable/high/dataset.html) for datasets). Most of the information about HDF5 datasets is available as an attribute, e.g., `h5py.File('filename.h5')['path/to/field'].compression` should return the compression filter or `None` if none were applied.

---

<div class="post-metadata">

**Author:** ![jan](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://forum.hdfgroup.org/u/jan)\
**Post date:** [June 11, 2021, 3:42pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/7 "2021-06-11T15:42:34Z")

</div>

Hi Steve,

I’ve found the solution in the low-level API of h5py.  
The dataset’s `id` object (class `h5py.h5d.DatasetID`) owns a method called `get_storage_size` which returns the storage size used by a dataset.  
Thank you for your help!

All best  
Jan

---

<div class="post-metadata">

**Author:** ![rosborn](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/rosborn/32/118_2.png) [@rosborn](https://forum.hdfgroup.org/u/rosborn)\
**Post date:** [June 11, 2021, 4:05pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/8 "2021-06-11T16:05:43Z")

</div>

I apologize for skimming the question too quickly. I thought you were asking for more basic information about datasets, which you obviously already knew.

---

<div class="post-metadata">

**Author:** ![ajelenak](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@ajelenak](https://forum.hdfgroup.org/u/ajelenak)\
**Post date:** [June 11, 2021, 4:39pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/9 "2021-06-11T16:39:26Z")

</div>

Hi @jan!

Yes, you are on the right track. Below is a Python example that prints compression ratio for every HDF5 dataset in a file:

```python
import h5py

def comp_ratio(name, obj):
    if isinstance(obj, h5py.Dataset) and obj.chunks is not None:
        dcpl = obj.id.get_create_plist()
        if dcpl.get_nfilters():
            stor_size = obj.id.get_storage_size()
            if stor_size != 0:
                ratio = float(obj.nbytes) / float(stor_size)
                print(f'Compression ratio for "{obj.name}": {ratio}')

fname = 'example.h5'
with h5py.File(fname, mode='r') as h5f:
    h5f.visititems(comp_ratio)

```

-Aleksandar

---

<div class="post-metadata">

**Author:** ![jan](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://forum.hdfgroup.org/u/jan)\
**Post date:** [June 11, 2021, 4:54pm UTC](https://forum.hdfgroup.org/t/access-to-miscellaneous-dataset-information-get-size-of-compressed-dataset/8582/10 "2021-06-11T16:54:22Z")

</div>

Hi @ajelenak!

Thank you very much for your code example, it works pretty fine and that’s exactly what I was looking for!

All best  
Jan
