Commit 221d24e2 authored by Julia Garriga Ferrer's avatar Julia Garriga Ferrer
Browse files

[io][utils] Adds function for writing comp info to h5 file

parent edf9704a
......@@ -26,9 +26,12 @@
__authors__ = ["J. Garriga"]
__license__ = "MIT"
__date__ = "23/07/2019"
__date__ = "06/02/2020"
import logging
import h5py
from datetime import datetime
import numpy
_logger = logging.getLogger(__file__)
......@@ -65,3 +68,90 @@ def advancement_display(iteration, total, prefix='', suffix='', decimals=1, leng
# Print New Line on Complete
if iteration == total:
def write_process(h5_file, entry, dimensions, W, data, processing_order,
data_path='/', overwrite=True):
Write a stack of components and its parameters into .h5
:param str h5_file: path to the hdf5 file
:param str entry: entry name
:param dict dimensions: Dictionary with the dimensions names and values
:param W: Matrix with the rocking curves values
:type: numpy.ndarray
:param data: Stack with the components
:type: numpy.ndarray
:param processing_order: processing order of treatment
:type: int
:param data_path: path to store the data
:type: str
process_name = 'process_' + str(processing_order)
def get_interpretation(my_data):
"""Return hdf5 attribute for this type of data"""
if isinstance(my_data, numpy.ndarray):
if my_data.ndim is 1:
return 'spectrum'
elif my_data.ndim in (2, 3):
return 'image'
return None
def save_key(path_name, key_path, value):
"""Save the given value to the associated path. Manage numpy arrays
and dictionaries"""
key_path = key_path.replace('.', '/')
# save if is dict
if isinstance(value, dict):
h5_path = '/'.join((path_name, key_path))
dicttoh5(value, h5file=h5_file, h5path=h5_path,
overwrite_data=True, mode='a')
with h5py.File(h5_file, 'a') as h5f:
nx = h5f.require_group(path_name)
nx[key_path] = value
except TypeError as e:
_logger.error('Unable to write', str(key_path), 'reason is', str(e))
interpretation = get_interpretation(value)
if interpretation:
nx[key_path].attrs['interpretation'] = interpretation
with h5py.File(h5_file, 'a') as h5f:
h5f.attrs["default"] = "entry"
nx_entry = h5f.require_group('/'.join((data_path, entry)))
nx_entry.attrs["NX_class"] = "NXentry"
nx_entry.attrs["default"] = "data"
nx_process = nx_entry.require_group(process_name)
nx_process.attrs['NX_class'] = "NXprocess"
if overwrite:
for key in ('program', 'version', 'date', 'processing_order',
'class_instance', 'ft'):
if key in nx_process:
del nx_process[key]
nx_process['program'] = 'darfix'
nx_process['version'] = '0.2'
nx_process['date'] =
nx_process['processing_order'] = numpy.int32(processing_order)
nx_parameters = nx_process.require_group("input")
nx_parameters.attrs['NX_class'] = "NXparameters"
nx_parameters_path =
results = nx_process.require_group("results")
results.attrs["NX_class"] = "NXcollection"
results_path =
nx_data = nx_entry.require_group("data")
nx_data.attrs["NX_class"] = "NXdata"
nx_data.attrs["signal"] = "components"
source_addr = "entry/" + process_name + "/results/components"
results.attrs["target"] = "components"
save_key(results_path, "W", W)
save_key(results_path, "components", data)
nx_data["components"] = h5f[source_addr]
for key, value in dimensions.items():
save_key(nx_parameters_path, key_path=key, value=value)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment