Skip to content

Commit

Permalink
Added list_attributes method to Pypeline for printing and returning a…
Browse files Browse the repository at this point in the history
…ll attributes of a dataset
  • Loading branch information
tomasstolker committed Sep 28, 2021
1 parent bb21ddd commit 9c7b228
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/running_pynpoint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,16 @@ There are several ways to access the datasets in the HDF5 database that is used
* The `h5py <http://www.h5py.org/>`_ Python package can be used to access the HDF5 file directly.

* There are external tools available such as `HDFCompass <https://support.hdfgroup.org/projects/compass/download.html>`_ or `HDFView <https://support.hdfgroup.org/downloads/index.html>`_ to read, inspect, and visualize data and attributes. HDFCompass is easy to use and has a basic plotting functionality. In HDFCompass, the static PynPoint attributes can be opened with the *Reopen as HDF5 Attributes* option.

.. _data_attributes:

Dataset attributes
------------------

In addition to the :meth:`~pynpoint.core.pypeline.Pypeline.get_attribute` method, it is also possible to print and return all attributes of a dataset by using the :meth:`~pynpoint.core.pypeline.Pypeline.list_attributes` method of :class:`~pynpoint.core.pypeline.Pypeline`:

.. code-block:: python
attr_dict = pipeline.list_attributes('tag_name')
The method returns a dictionary with both the static and non-static attributes.
48 changes: 47 additions & 1 deletion pynpoint/core/pypeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import urllib.request
import warnings

from typing import Any, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.error import URLError

import h5py
Expand Down Expand Up @@ -754,3 +754,49 @@ def get_shape(self,
self.m_data_storage.close_connection()

return data_shape

@typechecked
def list_attributes(self,
data_tag: str) -> Dict[str, Union[str, np.float64, np.ndarray]]:
"""
Method for printing and returning an overview of all attributes of a dataset.
Parameters
----------
data_tag : str
Database tag of which the attributes will be extracted.
Returns
-------
dict(str, bool)
Dictionary with all attributes, both static and non-static.
"""

data_text = f'Attribute overview of {data_tag}'

print('\n' + len(data_text) * '-')
print(data_text)
print(len(data_text) * '-' + '\n')

self.m_data_storage.open_connection()

attributes = {}

print('Static attributes:')

for key in self.m_data_storage.m_data_bank[data_tag].attrs:
value = self.m_data_storage.m_data_bank[data_tag].attrs[key]
attributes[key] = value
print(f'\n - {key} = {value}')

print('\nNon-static attributes:')

for key in self.m_data_storage.m_data_bank[f'header_{data_tag}']:
value = self.m_data_storage.m_data_bank[f'header_{data_tag}/{key}']
value = list(value)
attributes[key] = value
print(f'\n - {key} = {value}')

self.m_data_storage.close_connection()

return attributes

0 comments on commit 9c7b228

Please sign in to comment.