Skip to content

Commit

Permalink
Root level default group is a NXdata
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Apr 26, 2024
1 parent 6ac6855 commit 1563dbe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
14 changes: 10 additions & 4 deletions pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def set_default_group(template):
# defalut attribute key to the list of immediate child group
dflt_key_to_grp_li: Optional[dict[str, list]] = {}
# defalut attribute key to the group set by reader
dflt_key_to_exist_grp: dict[str, str] = {}
dflt_key_to_exist_grp: Optional[dict[str, str]] = {}

# "/abc[DATA]/XYe[anything]/mnf[MNYZ]/anything" -> ['DATA', 'anything', 'MNYZ']
pattern = r"\[(.*?)\]"
Expand All @@ -870,7 +870,7 @@ def set_default_group(template):
# Cancel out the attribuutes
if groups_list[-1].startswith("@"):
continue
# Cancel out the fields
# Cancel out the fields and end groups without fields
groups_list = groups_list[0:-1]
if not groups_list:
continue
Expand All @@ -893,6 +893,7 @@ def set_default_group(template):
dflt_key_to_grp_li[last_default_atttr] = {}
# Data groups
dflt_key_to_grp_li[last_default_atttr]["data"] = []
# Entry groups
dflt_key_to_grp_li[last_default_atttr]["entry"] = []
# Other groups
dflt_key_to_grp_li[last_default_atttr]["other"] = []
Expand All @@ -915,8 +916,8 @@ def set_default_group(template):

for deflt_key, value in dflt_key_to_grp_li.items():
pre_defalt_grp = dflt_key_to_exist_grp.get(deflt_key, None)
# Verify if user added the group here
if not pre_defalt_grp:
# Verify if user has added the group in default attribute
if pre_defalt_grp:
if (
pre_defalt_grp in value["entry"]
or pre_defalt_grp in value["data"]
Expand All @@ -929,6 +930,11 @@ def set_default_group(template):
if entry_default == deflt_key:
template[entry_default] = entry_data_rnd
continue
# Handle root level default
root_deflt = "/@default"
if deflt_key == root_deflt:
template[root_deflt] = entry_data_rnd
continue

if value["entry"]:
template[deflt_key] = value["entry"][0]
Expand Down
38 changes: 28 additions & 10 deletions tests/dataconverter/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,30 +463,48 @@ def test_path_in_data_dict(nxdl_path, expected, template):
assert helpers.path_in_data_dict(nxdl_path, tuple(template.keys())) == expected


def test_set_default_group(template):
@pytest.mark.parametrize(
"temp_dict",
[
{
"/ENTRY[entry1]/NXODD_name[nxodd_name]/float_value": 2.0,
"/ENTRY[entry1]/NXODD_name[nxodd_name_2]/float_value": 8.0,
"/ENTRY[entry1]/NXODD_name[nxodd_name_2]/DATA[data1]/data1": [3, 5, 6],
"/ENTRY[entry1]/NXODD_name[nxodd_name_2]/DATA[data2]/data2": [3, 5, 6],
}
],
)
def test_set_default_group(temp_dict):
"""_summary_
Parameters
----------
template : Template
"""
template = Template(temp_dict)
assert (
"/@default" not in template
), "To test the root level /@default should be empty."
assert (
"/ENTRY[entry]/@default" not in template
"/ENTRY[entry1]/@default" not in template
), "To test default attribute, entry attribute should be empty."

set_default_group(template)

assert (
template["/@default"] == "entry",
"To test the root level /@default should be empty.",
)
assert (
template["/ENTRY[entry]/@default"] == "nxodd_name",
"To test default attribute, entry attribute should be empty.",
)
assert template["/@default"] in [
"data1",
"data2",
], "To test the root level /@default should be empty."

assert template["/ENTRY[entry1]/@default"] in [
"nxodd_name",
"nxodd_name_2",
], "To test default attribute, entry attribute should be empty."

assert template["/ENTRY[entry1]/NXODD_name[nxodd_name_2]/@default"] in [
"data1",
"data2",
], "To test default attribute, entry attribute should be empty."


def test_atom_type_extractor_and_hill_conversion():
Expand Down

0 comments on commit 1563dbe

Please sign in to comment.