Commit c9be9599 authored by payno's avatar payno
Browse files

rever 94a73f5d

parent 94a73f5d
Pipeline #45876 failed with stages
in 19 seconds
......@@ -40,7 +40,6 @@ from nxtomomill.io.confighandler import XRD3DHDF5ConfigHandler
from nxtomomill.io.confighandler import SETTABLE_PARAMETERS_UNITS
from collections.abc import Iterable
from nxtomomill.utils import Format
from nxtomomill.io.config import XRD3DHDF5Config
import argparse
......@@ -70,6 +69,7 @@ def _ask_for_selecting_detector(det_grps: Iterable):
elif res in det_grps:
return res
else:
print("detector name not recognized.")
return _ask_for_selecting_detector(det_grps)
......@@ -86,7 +86,6 @@ def main(argv):
parser.add_argument(
"output_file", help="output .nx or .h5 file", default=None, nargs="?"
)
parser.add_argument(
"--file-extension",
"--file_extension",
......@@ -147,21 +146,6 @@ def main(argv):
action="store_false",
default=None,
)
parser.add_argument(
"--standard-format",
help="Format of the input file is the 'standard' tomography format",
dest="is_xrd_ct_format",
action="store_false",
default=None,
)
parser.add_argument(
"--xrd-ct-format",
help="Format of the input file is the 'XRD-CT' tomography format",
dest="is_xrd_ct_format",
action="store_true",
default=None,
)
parser.add_argument(
"--x_trans_keys",
"--x-trans-keys",
......@@ -282,9 +266,8 @@ def main(argv):
_logger.error(e)
return
else:
configuration = configuration_handler.configuration
assert isinstance(configuration, XRD3DHDF5Config)
for title in configuration.init_titles:
configuration = configuration_handler.configuration.init_titles
for title in configuration:
assert title != ""
configuration.format = Format.XRD_3D
from_h5_to_nx(
......
......@@ -107,7 +107,6 @@ class StandardAcquisition(BaseAcquisition):
self._acq_expo_time = None
self._copied_dataset = {}
"register dataset copied. Key if the original location as" "DataUrl.path. Value is the DataUrl it has been moved to"
self._current_scan_n_frame = None
@property
def image_key(self):
......@@ -285,7 +284,7 @@ class StandardAcquisition(BaseAcquisition):
self._virtual_sources_len.append(n_frame)
return n_frame
def _treate_valid_camera(
def __treate_valid_camera(
self,
detector_node,
entry,
......@@ -389,7 +388,6 @@ class StandardAcquisition(BaseAcquisition):
n_frame=n_frame,
)
)
self._current_scan_n_frame = n_frame
def camera_is_valid(self, det_name):
assert isinstance(det_name, str)
......@@ -451,7 +449,7 @@ class StandardAcquisition(BaseAcquisition):
continue
else:
detector_node = instrument_grp[key]
self._treate_valid_camera(
self.__treate_valid_camera(
detector_node,
entry=entry,
frame_type=type_,
......@@ -505,7 +503,7 @@ class StandardAcquisition(BaseAcquisition):
return values, unit
def _write_beam(self, root_node, request_input, input_callback):
beam_node = root_node.require_group("beam")
beam_node = root_node.create_group("beam")
if not self.has_diode:
if "energy" in self.configuration.param_already_defined:
energy = float(self.configuration.param_already_defined["energy"])
......@@ -519,11 +517,11 @@ class StandardAcquisition(BaseAcquisition):
beam_node["incident_energy"].attrs["unit"] = unit
def _write_instrument(self, root_node):
instrument_node = root_node.require_group("instrument")
instrument_node = root_node.create_group("instrument")
instrument_node.attrs["NX_class"] = "NXinstrument"
instrument_node.attrs["default"] = "detector"
detector_node = instrument_node.require_group("detector")
detector_node = instrument_node.create_group("detector")
detector_node.attrs["NX_class"] = "NXdetector"
# write data
if self._virtual_sources is not None:
......@@ -577,7 +575,6 @@ class StandardAcquisition(BaseAcquisition):
detector_node["estimated_cor_from_motor"].attrs["unit"] = unit
def _create_data_virtual_dataset(self, detector_node):
print("_create_data_virtual_dataset")
if (
self.n_frames is None
or self.dim_1 is None
......
......@@ -38,9 +38,6 @@ __date__ = "19/04/2021"
from nxtomomill.converter.hdf5.acquisition.standardacquisition import (
StandardAcquisition,
)
import logging
_logger = logging.getLogger(__name__)
class XRD3DAcquisition(StandardAcquisition):
......@@ -66,52 +63,12 @@ class XRD3DAcquisition(StandardAcquisition):
self._rocking = rocking
def _get_rocking_dataset(self, entry, n_frames):
for grp in self._get_positioners_node(entry), entry:
try:
rocking, unit = self._get_node_values_for_frame_array(
node=grp,
n_frame=n_frames,
keys=self.configuration.rocking_keys,
info_retrieve="rocking",
expected_unit=None,
)
except (ValueError, KeyError):
pass
else:
return rocking, None
mess = "Unable to find rocking for {}" "".format(self.root_url.path())
if self.raise_error_if_issue:
raise ValueError(mess)
else:
mess += "default value will be set. (0)"
_logger.warning(mess)
return 0, None
raise NotImplementedError()
def _get_base_tilt_dataset(self, entry, n_frames):
for grp in self._get_positioners_node(entry), entry:
try:
base_tilt, unit = self._get_node_values_for_frame_array(
node=grp,
n_frame=n_frames,
keys=self.configuration.base_tilt_keys,
info_retrieve="base tilt",
expected_unit=None,
)
except (ValueError, KeyError):
pass
else:
return base_tilt, None
raise NotImplementedError()
mess = "Unable to find base tilt for {}" "".format(self.root_url.path())
if self.raise_error_if_issue:
raise ValueError(mess)
else:
mess += "default value will be set. (0)"
_logger.warning(mess)
return 0, None
def _treate_valid_camera(
def __treate_valid_camera(
self,
detector_node,
entry,
......@@ -121,7 +78,7 @@ class XRD3DAcquisition(StandardAcquisition):
entry_path,
entry_url,
):
super()._treate_valid_camera(
super().__treate_valid_camera(
detector_node=detector_node,
entry=entry,
frame_type=frame_type,
......@@ -132,30 +89,17 @@ class XRD3DAcquisition(StandardAcquisition):
)
# store base tilt information
if not self._ignore_sample_output("base_tilt"):
base_tilt, _ = self._get_base_tilt_dataset(
entry=entry, n_frames=self._current_scan_n_frame
)
self._base_tilt.extend(base_tilt)
rots = self._get_base_tilt_dataset(entry=entry, n_frames=self._n_frames)[0]
self._base_tilt.extend(rots)
else:
self._base_tilt = None
# store rocking information
if not self._ignore_sample_output("rocking"):
rocking, _ = self._get_rocking_dataset(
entry=entry, n_frames=self._current_scan_n_frame
)
self._rocking.extend(rocking)
else:
self._rocking = None
def _preprocess_registered_entries(self, output_file):
super()._preprocess_register_entries(output_file=output_file)
self._base_tilt = []
self._rocking = []
super()._preprocess_registered_entries(output_file=output_file)
def _write_sample(self, root_node):
super()._write_sample(root_node)
sample_node = root_node.require_group("sample")
if self._rocking is not None:
sample_node["rocking"] = self._rocking
if self._base_tilt is not None:
sample_node["base_tilt"] = self._base_tilt
def _write_instrument(self, root_node):
super()._write_instrument(root_node)
instrument_node = root_node.create_group("instrument")
raise NotImplementedError()
......@@ -293,10 +293,16 @@ class _H5ToNxConverter:
detector_sel_callback=self.detector_sel_callback,
)
elif current_format is Format.XRD_3D:
root_acquisition = XRDCTAcquisition(
configuration=self.configuration,
detector_sel_callback=self.detector_sel_callback,
copy_frames=frame_grp.copy,
)
elif current_format is Format.XRD_CT:
root_acquisition = XRD3DAcquisition(
root_url=frame_grp.url,
configuration=self.configuration,
detector_sel_callback=self.detector_sel_callback,
copy_frames=frame_grp.copy,
)
else:
raise ValueError("Format {} is not handled".format(current_format))
......
......@@ -1090,7 +1090,6 @@ class TomoHDF5Config:
@staticmethod
def from_cfg_file(file_path: str, encoding=None):
assert file_path is not None, "file_path should not be None"
config_parser = configparser.ConfigParser(allow_no_value=True)
config_parser.read(file_path, encoding=encoding)
return TomoHDF5Config.from_dict(config_parser)
......@@ -1104,51 +1103,7 @@ class TomoHDF5Config:
class XRD3DHDF5Config(TomoHDF5Config):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._rocking_keys = settings.XRD3D.H5.ROCKING_KEYS
self._base_tilt_keys = settings.XRD3D.H5.BASE_TILT_KEYS
@property
def rocking_keys(self) -> Iterable:
return self._rocking_keys
@rocking_keys.setter
def rocking_keys(self, keys) -> None:
if not isinstance(keys, Iterable):
raise TypeError("'keys' should be an Iterable")
else:
self._rocking_keys = keys
@staticmethod
def from_cfg_file(file_path: str, encoding=None):
assert file_path is not None, "file_path should not be None"
config_parser = configparser.ConfigParser(allow_no_value=True)
config_parser.read(file_path, encoding=encoding)
return XRD3DHDF5Config.from_dict(config_parser)
@staticmethod
def from_dict(dict_: dict):
"""
Create a HDF5Config object and set it from values contained in the
dictionary
:param dict dict_: settings dictionary
:return: HDF5Config
"""
config = XRD3DHDF5Config()
config.load_from_dict(dict_)
return config
@property
def base_tilt_keys(self) -> Iterable:
return self._base_tilt_keys
@base_tilt_keys.setter
def base_tilt_keys(self, keys) -> None:
if not isinstance(keys, Iterable):
raise TypeError("'keys' should be an Iterable")
else:
self._base_tilt_keys = keys
pass
def generate_default_h5_config() -> dict:
......
......@@ -91,28 +91,6 @@ class _BaseHDF5ConfigHandler:
And insure there is no opposite Information
"""
@staticmethod
def get_tuple_of_keys_from_cmd(cmd_value):
return utils.get_tuple_of_keys_from_cmd(cmd_value)
@staticmethod
def conv_str_to_bool(bstr):
return bstr in ("True", True)
@staticmethod
def conv_log_level(bool_debug):
if bool_debug is True:
return "debug"
else:
return "warning"
@staticmethod
def conv_xrd_ct_to_format(str_bool):
if str_bool in ("True", True):
return Format.XRD_CT
else:
return None
def __init__(self, argparse_options, raise_error=True):
self._argparse_options = argparse_options
self._config = None
......@@ -126,123 +104,6 @@ class _BaseHDF5ConfigHandler:
def argparse_options(self):
return self._argparse_options
def _check_argparse_options(self, raise_error):
raise NotImplementedError("BaseClass")
def _create_HDF5_config(self):
raise NotImplementedError("BAse class")
def build_configuration(self, raise_error) -> bool:
"""
:param bool raise_error: raise error if encounter some errors. Else
display a log message
:return: True if the settings are valid
"""
self._check_argparse_options(raise_error=raise_error)
options = self.argparse_options
config = self._create_HDF5_config()
# check input and output file
if config.input_file is None:
config.input_file = options.input_file
elif options.input_file is not None and config.input_file != options.input_file:
raise ValueError(
"Two different input files provided from "
"command line and from the configuration file"
)
if config.input_file is None:
err = "No input file provided"
if raise_error:
raise ValueError(err)
else:
_logger.error(err)
if config.output_file is None:
config.output_file = options.output_file
elif (
options.output_file is not None
and config.output_file != options.output_file
):
raise ValueError(
"Two different output files provided from "
"command line and from the configuration file"
)
if config.output_file is None:
input_file, input_file_ext = os.path.splitext(config.input_file)
if config.file_extension is None:
err = "If no outputfile provided you should provide the " "extension"
if raise_error:
raise ValueError(err)
else:
_logger.error(err)
config.output_file = input_file + config.file_extension.value
# set parameter from the arg parse options
# key is the name of the argparse option.
# value is a tuple: (name of the setter in the HDF5Config,
# function to format the input)
self._config = self._map_option_to_config_param(config, options)
def _map_option_to_config_param(self, config, options):
raise NotImplementedError("Base class")
def __str__(self):
raise NotImplementedError("")
class TomoHDF5ConfigHandler(_BaseHDF5ConfigHandler):
def _create_HDF5_config(self):
if self.argparse_options.config:
return TomoHDF5Config.from_cfg_file(self.argparse_options.config)
else:
return TomoHDF5Config()
def _map_option_to_config_param(self, config, options):
mapping = {
"valid_camera_names": (
"valid_camera_names",
self.get_tuple_of_keys_from_cmd,
),
"overwrite": ("overwrite", self.conv_str_to_bool),
"file_extension": ("file_extension", filter_str_def),
"single_file": ("single_file", self.conv_str_to_bool),
"debug": ("log_level", self.conv_log_level),
"entries": ("entries", self.get_tuple_of_keys_from_cmd),
"ignore_sub_entries": (
"sub_entries_to_ignore",
self.get_tuple_of_keys_from_cmd,
),
"raises_error": ("raises_error", self.conv_str_to_bool),
"field_of_view": ("field_of_view", filter_str_def),
"request_input": ("request_input", self.conv_str_to_bool),
"is_xrd_ct_format": ("format", self.conv_xrd_ct_to_format),
"x_trans_keys": ("x_trans_keys", self.get_tuple_of_keys_from_cmd),
"y_trans_keys": ("y_trans_keys", self.get_tuple_of_keys_from_cmd),
"z_trans_keys": ("z_trans_keys", self.get_tuple_of_keys_from_cmd),
"rot_angle_keys": ("rotation_angle_keys", self.get_tuple_of_keys_from_cmd),
"acq_expo_time_keys": (
"exposition_time_keys",
self.get_tuple_of_keys_from_cmd,
),
"x_pixel_size_key": ("x_pixel_size_paths", self.get_tuple_of_keys_from_cmd),
"y_pixel_size_key": ("y_pixel_size_paths", self.get_tuple_of_keys_from_cmd),
"init_titles": ("init_titles", self.get_tuple_of_keys_from_cmd),
"init_zserie_titles": (
"zserie_init_titles",
self.get_tuple_of_keys_from_cmd,
),
"dark_titles": ("dark_titles", self.get_tuple_of_keys_from_cmd),
"ref_titles": ("flat_titles", self.get_tuple_of_keys_from_cmd),
"proj_titles": ("projections_titles", self.get_tuple_of_keys_from_cmd),
"align_titles": ("alignment_titles", self.get_tuple_of_keys_from_cmd),
"set_params": ("param_already_defined", _extract_param_value),
}
for argparse_name, (config_name, format_fct) in mapping.items():
argparse_value = getattr(options, argparse_name)
if argparse_value is not None:
value = format_fct(argparse_value)
setattr(config, config_name, value)
return config
def _check_argparse_options(self, raise_error):
if self.argparse_options is None:
err = "No argparse options provided"
......@@ -324,51 +185,72 @@ class TomoHDF5ConfigHandler(_BaseHDF5ConfigHandler):
else:
_logger.error(err)
class XRD3DHDF5ConfigHandler(_BaseHDF5ConfigHandler):
def _create_HDF5_config(self):
print("B")
if self.argparse_options.config:
return XRD3DHDF5Config.from_cfg_file(self.argparse_options.config)
else:
return XRD3DHDF5Config()
if config.output_file is None:
config.output_file = options.output_file
elif (
options.output_file is not None
and config.output_file != options.output_file
):
raise ValueError(
"Two different output files provided from "
"command line and from the configuration file"
)
if config.output_file is None:
input_file, input_file_ext = os.path.splitext(config.input_file)
if config.file_extension is None:
err = "If no outputfile provided you should provide the " "extension"
if raise_error:
raise ValueError(err)
else:
_logger.error(err)
config.output_file = input_file + config.file_extension.value
# set parameter from the arg parse options
# key is the name of the argparse option.
# value is a tuple: (name of the setter in the HDF5Config,
# function to format the input)
# TODO: map all values
conv = utils.get_tuple_of_keys_from_cmd
def conv_str_to_bool(bstr):
return bstr in ("True", True)
def conv_log_level(bool_debug):
if bool_debug is True:
return "debug"
else:
return "warning"
def conv_xrd_ct_to_format(str_bool):
if str_bool in ("True", True):
return Format.XRD_CT
else:
return None
def _map_option_to_config_param(self, config, options):
mapping = {
"valid_camera_names": (
"valid_camera_names",
self.get_tuple_of_keys_from_cmd,
),
"overwrite": ("overwrite", self.conv_str_to_bool),
"valid_camera_names": ("valid_camera_names", conv),
"overwrite": ("overwrite", conv_str_to_bool),
"file_extension": ("file_extension", filter_str_def),
"single_file": ("single_file", self.conv_str_to_bool),
"debug": ("log_level", self.conv_log_level),
"entries": ("entries", self.get_tuple_of_keys_from_cmd),
"ignore_sub_entries": (
"sub_entries_to_ignore",
self.get_tuple_of_keys_from_cmd,
),
"raises_error": ("raises_error", self.conv_str_to_bool),
"single_file": ("single_file", conv_str_to_bool),
"debug": ("log_level", conv_log_level),
"entries": ("entries", conv),
"ignore_sub_entries": ("sub_entries_to_ignore", conv),
"raises_error": ("raises_error", conv_str_to_bool),
"field_of_view": ("field_of_view", filter_str_def),
"request_input": ("request_input", self.conv_str_to_bool),
"x_trans_keys": ("x_trans_keys", self.get_tuple_of_keys_from_cmd),
"y_trans_keys": ("y_trans_keys", self.get_tuple_of_keys_from_cmd),
"z_trans_keys": ("z_trans_keys", self.get_tuple_of_keys_from_cmd),
"rot_angle_keys": ("rotation_angle_keys", self.get_tuple_of_keys_from_cmd),
"acq_expo_time_keys": (
"exposition_time_keys",
self.get_tuple_of_keys_from_cmd,
),
"x_pixel_size_key": ("x_pixel_size_paths", self.get_tuple_of_keys_from_cmd),
"y_pixel_size_key": ("y_pixel_size_paths", self.get_tuple_of_keys_from_cmd),
"init_titles": ("init_titles", self.get_tuple_of_keys_from_cmd),
"init_zserie_titles": (
"zserie_init_titles",
self.get_tuple_of_keys_from_cmd,
),
"dark_titles": ("dark_titles", self.get_tuple_of_keys_from_cmd),
"ref_titles": ("flat_titles", self.get_tuple_of_keys_from_cmd),
"proj_titles": ("projections_titles", self.get_tuple_of_keys_from_cmd),
"align_titles": ("alignment_titles", self.get_tuple_of_keys_from_cmd),
"request_input": ("request_input", conv_str_to_bool),
"is_xrd_ct_format": ("format", conv_xrd_ct_to_format),
"x_trans_keys": ("x_trans_keys", conv),
"y_trans_keys": ("y_trans_keys", conv),
"z_trans_keys": ("z_trans_keys", conv),
"rot_angle_keys": ("rotation_angle_keys", conv),
"acq_expo_time_keys": ("exposition_time_keys", conv),
"x_pixel_size_key": ("x_pixel_size_paths", conv),
"y_pixel_size_key": ("y_pixel_size_paths", conv),
"init_titles": ("init_titles", conv),
"init_zserie_titles": ("zserie_init_titles", conv),
"dark_titles": ("dark_titles", conv),
"ref_titles": ("flat_titles", conv),
"proj_titles": ("projections_titles", conv),
"align_titles": ("alignment_titles", conv),
"set_params": ("param_already_defined", _extract_param_value),
}
for argparse_name, (config_name, format_fct) in mapping.items():
......@@ -376,7 +258,7 @@ class XRD3DHDF5ConfigHandler(_BaseHDF5ConfigHandler):
if argparse_value is not None:
value = format_fct(argparse_value)
setattr(config, config_name, value)
return config
self._config = config
def __str__(self):
raise NotImplementedError("")
......@@ -396,55 +278,3 @@ class XRD3DHDF5ConfigHandler(_BaseHDF5ConfigHandler):
return XRD3DHDF5Config.from_cfg_file(self.argparse_options.config)
else:
return XRD3DHDF5Config()
def _check_argparse_options(self, raise_error):
if self.argparse_options is None:
err = "No argparse options provided"
if raise_error:
raise ValueError(err)
else:
_logger.error(err)
return False