Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
from datetime import datetime, timezone
from enum import Enum
from functools import lru_cache
from typing import Any, Callable, List, Optional, Tuple, Union, Sequence
from typing import Any, List, Optional, Sequence, Tuple, Union

import h5py
import lxml.etree as ET
import numpy as np
from ase.data import chemical_symbols

from pynxtools import get_nexus_version, get_nexus_version_hash
from pynxtools.dataconverter.template import Template
from pynxtools.definitions.dev_tools.utils.nxdl_utils import (
get_enums,
get_inherited_nodes,
Expand Down Expand Up @@ -400,6 +401,45 @@ def add_inherited_children(list_of_children_to_add, path, nxdl_root, template):
return template


def add_template_key_from(nx_node, template: Template, parent_path=""):
key = None
unit = None
if nx_node.type == "attribute":
leaf_part = (
f"{nx_node.name}[{nx_node.name}]" if nx_node.variadic else nx_node.name
)
key = f"{parent_path}/@{leaf_part}"
elif nx_node.type == "field":
leaf_part = (
f"{nx_node.name}[{nx_node.name}]" if nx_node.variadic else nx_node.name
)
key = f"{parent_path}/{leaf_part}"
if hasattr(nx_node, "unit") and nx_node.unit:
unit = f"{key}/@units"
elif nx_node.type == "group":
leaf_part = f"{nx_node.name}[{nx_node.name}]"
key = f"{parent_path}/{leaf_part}"

if key:
template[nx_node.optionality][key] = None
if unit:
template[nx_node.optionality][unit] = None
return key


def build_template_from_nexus_tree(appdef_root, template, parent_path=""):
"""
Build a template from the nexus tree.
"""

for child in appdef_root.children:
key = add_template_key_from(child, parent_path)
if not key:
continue

build_template_from_nexus_tree(child, template, key)


def generate_template_from_nxdl(
root, template, path="", nxdl_root=None, nxdl_name=None
):
Expand Down
7 changes: 3 additions & 4 deletions src/pynxtools/dataconverter/nexus_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,23 @@
"""

from functools import lru_cache, reduce
from typing import Any, List, Dict, Literal, Optional, Set, Tuple, Union
from typing import Any, Dict, List, Literal, Optional, Set, Tuple

import lxml.etree as ET
from anytree.node.nodemixin import NodeMixin

from pynxtools import get_definitions_url
from pynxtools import NX_DOC_BASES, get_definitions_url
from pynxtools.dataconverter.helpers import (
get_all_parents_for,
get_nxdl_root_and_path,
is_variadic,
is_appdef,
is_variadic,
remove_namespace_from_tag,
)
from pynxtools.definitions.dev_tools.utils.nxdl_utils import (
get_nx_namefit,
is_name_type,
)
from pynxtools import NX_DOC_BASES

NexusType = Literal[
"NX_BINARY",
Expand Down
59 changes: 59 additions & 0 deletions src/pynxtools/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from pynxtools.dataconverter.helpers import generate_template_from_nxdl
from pynxtools.dataconverter.nexus_tree import *
from pynxtools.dataconverter.template import Template

spm_appdef = "NXspm"
spmT = generate_tree_from(spm_appdef)


def add_template_key_from(nx_node,
parent_path = ""):
key = None
unit = None
if nx_node.type == "attribute":
leaf_part = (f"{nx_node.name}[{nx_node.name}]"
Comment on lines +1 to +14
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file.

if nx_node.variadic else nx_node.name)
key = f"{parent_path}/@{leaf_part}"
elif nx_node.type == "field":
leaf_part = (f"{nx_node.name}[{nx_node.name}]"
if nx_node.variadic else nx_node.name)
key = f"{parent_path}/{leaf_part}"
if hasattr(nx_node, "unit") and nx_node.unit:
unit = f"{key}/@units"
elif nx_node.type == "group":
leaf_part = f"{nx_node.name}[{nx_node.name}]"
key = f"{parent_path}/{leaf_part}"
if key:
template[nx_node.optionality][key] = None
if unit:
template[nx_node.optionality][unit] = None
return key
def build_template_from_nexus_tree(appdef_root, template, parent_path = ""):
"""
Build a template from the nexus tree.
"""
for child in appdef_root.children:
key = add_template_key_from(child, parent_path)
if not key:
continue
build_template_from_nexus_tree(child, template, key)
template = Template()
build_template_from_nexus_tree(spmT, template)
template_old = Template()

root, _ = get_nxdl_root_and_path(spm_appdef)
generate_template_from_nxdl(
root=root,
template=template_old,
)
print(' #### check optional missing keys')
template_ = template
template_old_ = template_old
def check_missing():
for key, _ in template_['optional'].items():
if key not in template_old_['optional']:
print(f"OPTIONAL Missing key in template_old: {key}")
print(' #### check required missing keys')
for key, _ in template_['required'].items():
if key not in template_old_['required']:
print(f"REQUIRED: Missing key in template_old: {key}")
Loading