Commit 71e07b36 authored by payno's avatar payno
Browse files

Merge branch 'add_set_param' into '0.3'

Add set param

See merge request !24
parents 9d9b4eff 8f1c88ed
Pipeline #31868 passed with stage
in 19 seconds
......@@ -38,6 +38,10 @@ tmp*
*.xml
*.db
*.tar.bz2
*.nx
*.hdf5
*.h5
*.edf
dataset/
datasets/
......
Change Log
==========
0.3.2: XXX
----------
* h5_to_nx:
* add set-param option to let the user define some parameters values like energy if he knows it is missing (and avoid asking him n times).
0.3.1: 2020/08/19
-----------------
......
......@@ -49,6 +49,54 @@ _logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
_SETTABLE_PARAMETERS_UNITS = \
{
'energy': 'kev',
}
_SETTABLE_PARAMETERS = _SETTABLE_PARAMETERS_UNITS.keys()
def _getPossibleInputParams():
"""
:return: string with param1 (expected unit) ...
"""
res = []
for key, value in _SETTABLE_PARAMETERS_UNITS.items():
res.append('{} ({})'.format(key, value))
return ', '.join(res)
def _extract_param_value(key_values):
'''extract all the key / values elements from the str_list. Expected
format is `param_1_name param_1_value param_2_name param_2_value ...`
:param str str_list: raw input string as `param_1_name param_1_value
param_2_name param_2_value ...`
:return: dict of tuple (param_name, param_value)
:rtype: dict
'''
if len(key_values) % 2 != 0:
raise ValueError('Expect a pair `param_name, param_value` for each '
'parameters')
def pairwise(it):
it = iter(it)
while True:
try:
yield next(it), next(it)
except StopIteration:
# no more elements in the iterator
return
res = {}
for name, value in pairwise(key_values):
if name not in _SETTABLE_PARAMETERS:
raise ValueError('parameters {} is not managed'.format(name))
res[name] = value
return res
def main(argv):
"""
"""
......@@ -105,7 +153,11 @@ def main(argv):
help='Titles corresponding to projection scans')
parser.add_argument('--align_titles', default=','.join(H5_ALIGNMENT_TITLES),
help='Titles corresponding to alignment scans')
parser.add_argument('--set-params', default='',
nargs='*',
help='Allow manual definition of some parameters. ' \
'Valid parameters (and expected input unit) '
'are: {}.'.format(_getPossibleInputParams()))
options = parser.parse_args(argv[1:])
conv = utils.get_tuple_of_keys_from_cmd
file_keys = H5FileKeys(x_trans_keys=conv(options.x_trans_keys),
......@@ -122,12 +174,14 @@ def main(argv):
ref_titles=conv(options.ref_titles),
proj_titles=conv(options.proj_titles),
align_titles=conv(options.align_titles))
options.set_params = _extract_param_value(options.set_params)
h5_to_nx(input_file_path=options.input_file_path,
output_file=options.output_file, single_file=options.single_file,
file_extension=options.file_extension,
request_input=options.request_input, file_keys=file_keys,
scan_titles=scan_titles)
scan_titles=scan_titles,
param_already_defined=options.set_params)
exit(0)
......
......@@ -37,7 +37,7 @@ from tomoscan.esrf.edfscan import EDFTomoScan
from nxtomomill.utils import Progress
from nxtomomill.plugins import (get_plugins_instances_frm_env_var,
get_plugins_positioners_resources)
from nxtomomill.settings import (H5_ACQ_EXPO_TIME_KEYS, H5_ROT_ANGLE_KEYS,
from nxtomomill.settings import (H5_ROT_ANGLE_KEYS,
H5_VALID_CAMERA_NAMES, H5_X_TRANS_KEYS,
H5_Y_TRANS_KEYS, H5_Z_TRANS_KEYS,
H5_ALIGNMENT_TITLES, H5_ACQ_EXPO_TIME_KEYS,
......@@ -487,7 +487,8 @@ def h5_to_nx(input_file_path: str, output_file: str, single_file:bool,
request_input=False,
entries: typing.Union[typing.Iterable, None] = None,
input_callback=None, file_keys=DEFAULT_H5_KEYS,
scan_titles=DEFAULT_SCAN_TITLES):
scan_titles=DEFAULT_SCAN_TITLES,
param_already_defined=None):
"""
:param str input_file_path: file to be converted from .h5 to tomo .nx
......@@ -504,10 +505,14 @@ def h5_to_nx(input_file_path: str, output_file: str, single_file:bool,
parameters and return a text (that might be casted
according to the expected input type).
:param H5FileKeys file_keys: name of cameras, translation keys ...
:param Union[None, dict]: parameters for which the value has been defined
manually by the user. Like 'energy'...
:return: tuple of tuples (file_name, entry_name)
:rtype: tuple
"""
print('******set up***********')
if param_already_defined is None:
param_already_defined = {}
if not os.path.isfile(input_file_path):
raise ValueError('Given input file does not exists: %s'
......@@ -554,7 +559,8 @@ def h5_to_nx(input_file_path: str, output_file: str, single_file:bool,
if entry_type is AcquisitionStep.INITIALIZATION:
current_acquisition = _Acquisition(entry, file_keys,
scan_titles=scan_titles)
scan_titles=scan_titles,
param_already_defined=param_already_defined)
acquisitions.append(current_acquisition)
elif current_acquisition is not None:
current_acquisition.register_step(entry)
......@@ -674,13 +680,17 @@ class _Acquisition:
_FOV_PATH = 'technique/scan/field_of_view'
def __init__(self, entry: h5py.Group, file_keys: H5FileKeys, scan_titles):
def __init__(self, entry: h5py.Group, file_keys: H5FileKeys, scan_titles,
param_already_defined):
self._initialization_entry = entry
self._indexes = entry[_Acquisition._SCAN_NUMBER_PATH]
self._indexes_str = tuple([str(index) for index in entry[_Acquisition._SCAN_NUMBER_PATH]])
self._registered_entries = []
self._file_keys = file_keys
self._scan_titles = scan_titles
self._param_already_defined = param_already_defined
"""user can have defined already some parameter values as energy.
The idea is to avoid asking him if """
# variables set by the `_preprocess_frames` function
self._data = None
......@@ -1044,8 +1054,12 @@ class _Acquisition:
def _write_beam(self, root_node, request_input, input_callback):
beam_node = root_node.create_group('beam')
energy, unit = self._get_energy(ask_if_0=request_input,
input_callback=input_callback)
if 'energy' in self._param_already_defined:
energy = self._param_already_defined['energy']
unit = 'kev'
else:
energy, unit = self._get_energy(ask_if_0=request_input,
input_callback=input_callback)
if energy is not None:
beam_node["incident_energy"] = energy
beam_node["incident_energy"].attrs["unit"] = unit
......
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