Skip to content

Commit

Permalink
Refactoring pynxtools/dataconverter/convert.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Oct 26, 2023
1 parent 76677f6 commit 3248c47
Showing 1 changed file with 103 additions and 19 deletions.
122 changes: 103 additions & 19 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@ def get_names_of_all_readers() -> List[str]:
return all_readers


# pylint: disable=too-many-arguments,too-many-locals
def convert(input_file: Tuple[str],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
**kwargs):
"""The conversion routine that takes the input parameters and calls the necessary functions."""
def get_nxdl_root_and_path(nxdl: str):
"""Get xml root element from nxdl name e.g. NXapm.
Parameters
----------
nxdl: str
Name of nxdl file e.g. NXapm from NXapm.nxdl.xml.
Returns
-------
ET.root
Root element of nxdl file.
Raises
------
FileNotFoundError
Error if no file with the given nxdl name is found.
"""
# Reading in the NXDL and generating a template
definitions_path = nexus.get_nexus_definitions_path()
if nxdl == "NXtest":
Expand All @@ -89,27 +96,56 @@ def convert(input_file: Tuple[str],
if not os.path.exists(nxdl_path):
raise FileNotFoundError(f"The nxdl file, {nxdl}, was not found.")

nxdl_root = ET.parse(nxdl_path).getroot()
return ET.parse(nxdl_path).getroot(), nxdl_path


def transfer_data_into_template(input_file,
reader, nxdl_name,
undocumented=False, nxdl_root=None,
generate_template=False, **kwargs):
"""Transfer parse and merged data from input experimental file, config file and eln.
Experimental and eln files will be parsed and finally will be merged into template.
Before returning the template validate the template data.
Parameters
----------
input_file : Union[tuple[str], str]
Tuple of files or file
reader: str
Name of reader such as xps
nxdl_root : ET.element
Root element of nxdl file, otherwise provide nxdl_name
nxdl_name : str
Root name of nxdl file, e.g. NXmpes from NXmpes.nxdl.xml
generate_template : bool
To print template in log.
Returns
-------
template: Template
Template filled with data from raw file and eln file.
"""
if nxdl_root is None:
nxdl_root, _ = get_nxdl_root_and_path(nxdl=nxdl_name)
if undocumented:
logger.setLevel(UNDOCUMENTED)

if isinstance(input_file, str):
input_file = (input_file,)
template = Template()
helpers.generate_template_from_nxdl(nxdl_root, template)
if generate_template:
logger.info(template)
return

# Setting up all the input data
if isinstance(input_file, str):
input_file = (input_file,)
bulletpoint = "\n\u2022 "
logger.info("Using %s reader to convert the given files: %s ",
reader,
bulletpoint.join((" ", *input_file)))

data_reader = get_reader(reader)
if not (nxdl in data_reader.supported_nxdls or "*" in data_reader.supported_nxdls):
if not (nxdl_name in data_reader.supported_nxdls or "*" in data_reader.supported_nxdls):
raise NotImplementedError("The chosen NXDL isn't supported by the selected reader.")

data = data_reader().read( # type: ignore[operator]
Expand All @@ -118,11 +154,37 @@ def convert(input_file: Tuple[str],
**kwargs,
)
helpers.validate_data_dict(template, data, nxdl_root)
if fair and data.undocumented.keys():
return data


# pylint: disable=too-many-arguments,too-many-locals
def convert(input_file: Tuple[str],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
temp_data_dict=None, # pre generated empty template from nxdl name.
**kwargs):
"""The conversion routine that takes the input parameters and calls the necessary functions."""

nxdl_root, nxdl_path = get_nxdl_root_and_path(nxdl)

# Setting up all the input data
if isinstance(input_file, str):
input_file = (input_file,)
temp_data = transfer_data_into_template(input_file=input_file, reader=reader,
nxdl_name=nxdl, undocumented=undocumented,
nxdl_root=nxdl_root, generate_template=generate_template,
**kwargs)
# This is mainly if the filled template data is needed out of this function
temp_data_dict['data'] = temp_data
if fair and temp_data.undocumented.keys():
logger.warning("There are undocumented paths in the template. This is not acceptable!")
return

for path in data.undocumented.keys():
for path in temp_data.undocumented.keys():
if "/@default" in path:
continue
logger.log(
Expand All @@ -131,11 +193,33 @@ def convert(input_file: Tuple[str],
path
)

Writer(data=data, nxdl_path=nxdl_path, output_path=output).write()
Writer(data=temp_data, nxdl_path=nxdl_path, output_path=output).write()

logger.info("The output file generated: %s", output)


def convert_and_return_template(input_file: Tuple[str],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
**kwargs):

"""Convert input files into structure data according template and return template.
This function only is special than convert function by return value which is filled data
template.
"""
temp_data_dict = {'data': None}
convert(input_file=input_file, reader=reader,
nxdl=nxdl, output=output, generate_template=generate_template,
fair=fair, undocumented=undocumented, temp_data_dict=temp_data_dict, **kwargs)

return temp_data_dict['data']


def parse_params_file(params_file):
"""Parses the parameters from a given dictionary and returns them"""
params = yaml.load(params_file, Loader=yaml.Loader)['dataconverter']
Expand Down

0 comments on commit 3248c47

Please sign in to comment.