From f96cf3b9c5b516baf201e24773c56b32538580f0 Mon Sep 17 00:00:00 2001 From: Rubel Date: Thu, 22 Feb 2024 15:22:29 +0100 Subject: [PATCH] Adding functionality to add default plot. --- pynxtools_stm/reader.py | 72 +++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/pynxtools_stm/reader.py b/pynxtools_stm/reader.py index 45ff2b7..096d753 100644 --- a/pynxtools_stm/reader.py +++ b/pynxtools_stm/reader.py @@ -184,15 +184,15 @@ def get_appropriate_parser(self, eln_dict: Dict) -> Callable: return parser def set_default_group_for_each_group(template): - """Set default group for each group of data. + """Set default group for each group of Nexus file. Each group will have a \@default attrubute refering the immediate child group in a NeXus chain. - + e.g. /@default = "entry1" + /entry1/@default = "data1" + Parameters ---------- template : Template Template from filled from datafile and eln. - eln_dict : _type_ - _description_ Returns ------- @@ -212,10 +212,11 @@ def set_default_group_for_each_group(template): # "/abc[DATA]/XYe[anything]/mnf[MNYZ]/anything" -> ['DATA', 'anything', 'MNYZ'] pattern = r'\[(.*?)\]' + entry_data_rnd = "" for template_concept, val in template.items(): - groups_list = template_concept.split('/') + groups_list = template_concept.split("/") last_default_key = "" - if template_concept.endswith('/@default') and val: + if template_concept.endswith("/@default") and val: dflt_key_to_exist_grp[template_concept] = val for group in groups_list: @@ -225,21 +226,58 @@ def set_default_group_for_each_group(template): if modified_name: modified_name = modified_name[0] else: - modified_name = "" - # take only nxdata group - if group.startswith("DATA") or group.startswith("ENTRY"): - if not dflt_key_to_grp_li.get(f"{last_default_key}/@default", None): - dflt_key_to_grp_li[f"{last_default_key}/@default"] = [modified_name] - else: - dflt_key_to_grp_li[f"{last_default_key}/@default"].append(modified_name) + modified_name = group + last_default_atttr = f"{last_default_key}/@default" + if not dflt_key_to_grp_li.get(last_default_atttr, None): + dflt_key_to_grp_li[last_default_atttr] = {} + # Data groups + dflt_key_to_grp_li[last_default_atttr]['data'] = [] + dflt_key_to_grp_li[last_default_atttr]['entry'] = [] + # Other groupa + dflt_key_to_grp_li[last_default_atttr]['other'] = [] + + if template_concept.endswith("/@default"): + dflt_key_to_exist_grp[template_concept] = val + + # Entry + if group.startswith("ENTRY"): + dflt_key_to_grp_li[last_default_atttr]["entry"].append(modified_name) + # Data + elif group.startswith("DATA"): + dflt_key_to_grp_li[last_default_atttr]["data"].append(modified_name) + if not entry_data_rnd: + entry_data_rnd = modified_name + else: + dflt_key_to_grp_li[last_default_atttr]["other"].append(modified_name) + last_default_key = last_default_key + "/" + group + 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: + if (pre_defalt_grp in value['entry'] + or pre_defalt_grp in value["data"] + or pre_defalt_grp in value['other']): + continue - if not pre_defalt_grp or pre_defalt_grp not in value: - # Choose the first data or entry group for defalult view if not set by reader - # or if the group does not exist in the list of immediate child group - template[deflt_key] = value[0] + # Entry default group always a NXdata + entry_default = "/entry/@default" + if entry_default == deflt_key: + template[entry_default] = entry_data_rnd + continue + + if value['entry']: + template[deflt_key] = value['entry'][0] + # Prioritize data group on other groups + elif value['data']: + template[deflt_key] = value['data'][0] + # Randomly choose a NXdata group for entry + elif value['other']: + template[deflt_key] = value['other'][0] + else: + template[deflt_key] = "" + # pylint: disable=invalid-name, too-few-public-methods