From 035691cd0b81baf24bce343d37851fb6f67dc669 Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Wed, 25 Oct 2023 14:05:29 +0200 Subject: [PATCH 1/3] Enables accessing NXroot in nexus.py --- pynxtools/nexus/nxdl_utils.py | 3 +++ tests/dataconverter/test_helpers.py | 1 + tests/dataconverter/test_readers.py | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pynxtools/nexus/nxdl_utils.py b/pynxtools/nexus/nxdl_utils.py index 706390a7c..aa64d5caa 100644 --- a/pynxtools/nexus/nxdl_utils.py +++ b/pynxtools/nexus/nxdl_utils.py @@ -701,6 +701,9 @@ def get_node_at_nxdl_path(nxdl_path: str = None, we are looking for or the root elem from a previously loaded NXDL file and finds the corresponding XML element with the needed attributes.""" try: + if nxdl_path.count("/") == 1 and nxdl_path not in ("/ENTRY", "/entry"): + elem = None + nx_name = "NXroot" (class_path, nxdlpath, elist) = get_inherited_nodes(nxdl_path, nx_name, elem) except ValueError as value_error: if exc: diff --git a/tests/dataconverter/test_helpers.py b/tests/dataconverter/test_helpers.py index a018b92bd..9cfdf60c6 100644 --- a/tests/dataconverter/test_helpers.py +++ b/tests/dataconverter/test_helpers.py @@ -162,6 +162,7 @@ def fixture_filled_test_data(template, tmp_path): TEMPLATE["lone_groups"] = ['/ENTRY[entry]/required_group', '/ENTRY[entry]/required_group2', '/ENTRY[entry]/optional_parent/req_group_in_opt_group'] +TEMPLATE["optional"]["/@default"] = "Some NXroot attribute" @pytest.mark.parametrize("data_dict,error_message", [ diff --git a/tests/dataconverter/test_readers.py b/tests/dataconverter/test_readers.py index 79ec89bf4..d75344541 100644 --- a/tests/dataconverter/test_readers.py +++ b/tests/dataconverter/test_readers.py @@ -105,7 +105,7 @@ def test_has_correct_read_func(reader): @pytest.mark.parametrize("reader_name,nxdl,undocumented_keys", [ - ('mpes', 'NXmpes', ['/@default']) + ('mpes', 'NXmpes', []) ]) def test_shows_correct_warnings(reader_name, nxdl, undocumented_keys): """ From 1fdd9c15d6e9781c635cdd5fabe161b8d913b4df Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Thu, 26 Oct 2023 11:42:26 +0200 Subject: [PATCH 2/3] Adds most default NXroot attributes automatically --- pynxtools/dataconverter/convert.py | 2 ++ pynxtools/dataconverter/helpers.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pynxtools/dataconverter/convert.py b/pynxtools/dataconverter/convert.py index 29ca21b6e..a30c4bee1 100644 --- a/pynxtools/dataconverter/convert.py +++ b/pynxtools/dataconverter/convert.py @@ -122,6 +122,8 @@ def convert(input_file: Tuple[str], logger.warning("There are undocumented paths in the template. This is not acceptable!") return + helpers.add_default_root_attributes(data=data, filename=os.path.basename(output)) + for path in data.undocumented.keys(): if "/@default" in path: continue diff --git a/pynxtools/dataconverter/helpers.py b/pynxtools/dataconverter/helpers.py index f6b6fa8c5..199dec181 100644 --- a/pynxtools/dataconverter/helpers.py +++ b/pynxtools/dataconverter/helpers.py @@ -21,9 +21,11 @@ from typing import Tuple, Callable, Union import re import xml.etree.ElementTree as ET +from datetime import datetime, timezone import numpy as np from ase.data import chemical_symbols +import h5py from pynxtools.nexus import nexus from pynxtools.nexus.nexus import NxdlAttributeError @@ -583,12 +585,22 @@ def convert_to_hill(atoms_typ): return atom_list + list(atoms_typ) +def add_default_root_attributes(data, filename): + """Takes a dict/Template and adds NXroot fields/attributes that are inherently available""" + data["/@NX_class"] = "NXroot" + data["/@file_name"] = filename + data["/@file_time"] = str(datetime.now(timezone.utc).astimezone()) + data["/@file_update_time"] = data["/@file_time"] + data["/@NeXus_version"] = "NOT_IMPLEMENTED" + data["/@HDF5_version"] = '.'.join(map(str, h5py.h5.get_libversion())) # pylint: disable=c-extension-no-member + data["/@h5py_version"] = h5py.__version__ + + def extract_atom_types(formula, mode='hill'): """Extract atom types form chemical formula.""" - atom_types: set = set() element: str = "" - # tested with "(C38H54S4)n(NaO2)5(CH4)NH3B" + for char in formula: if char.isalpha(): if char.isupper() and element == "": From 58d258fa69905b3bd42ec22d812c404c672c3672 Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Fri, 27 Oct 2023 11:26:31 +0200 Subject: [PATCH 3/3] Fixes undocumented warnings for auto NXroot attributes --- pynxtools/dataconverter/convert.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pynxtools/dataconverter/convert.py b/pynxtools/dataconverter/convert.py index a30c4bee1..dfef05f0a 100644 --- a/pynxtools/dataconverter/convert.py +++ b/pynxtools/dataconverter/convert.py @@ -122,8 +122,6 @@ def convert(input_file: Tuple[str], logger.warning("There are undocumented paths in the template. This is not acceptable!") return - helpers.add_default_root_attributes(data=data, filename=os.path.basename(output)) - for path in data.undocumented.keys(): if "/@default" in path: continue @@ -133,6 +131,8 @@ def convert(input_file: Tuple[str], path ) + helpers.add_default_root_attributes(data=data, filename=os.path.basename(output)) + Writer(data=data, nxdl_path=nxdl_path, output_path=output).write() logger.info("The output file generated: %s", output)