Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
XRD
darfix
Commits
9a0ccab7
Commit
9a0ccab7
authored
Feb 07, 2020
by
Julia Garriga Ferrer
Browse files
[io][utils] Adds function for writing comp info to h5 file
parent
3cb3cbfc
Changes
1
Hide whitespace changes
Inline
Side-by-side
darfix/io/utils.py
View file @
9a0ccab7
...
...
@@ -26,9 +26,12 @@
__authors__
=
[
"J. Garriga"
]
__license__
=
"MIT"
__date__
=
"
23
/0
7
/20
19
"
__date__
=
"
06
/0
2
/20
20
"
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
:
print
()
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'
)
else
:
with
h5py
.
File
(
h5_file
,
'a'
)
as
h5f
:
nx
=
h5f
.
require_group
(
path_name
)
try
:
nx
[
key_path
]
=
value
except
TypeError
as
e
:
_logger
.
error
(
'Unable to write'
,
str
(
key_path
),
'reason is'
,
str
(
e
))
else
:
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'
]
=
datetime
.
now
().
replace
(
microsecond
=
0
).
isoformat
()
nx_process
[
'processing_order'
]
=
numpy
.
int32
(
processing_order
)
nx_parameters
=
nx_process
.
require_group
(
"input"
)
nx_parameters
.
attrs
[
'NX_class'
]
=
"NXparameters"
nx_parameters_path
=
nx_parameters
.
name
results
=
nx_process
.
require_group
(
"results"
)
results
.
attrs
[
"NX_class"
]
=
"NXcollection"
results_path
=
results
.
name
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment