Skip to content

Commit

Permalink
Adding functionality to add default plot.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubelMozumder committed Feb 22, 2024
1 parent 63e3fc6 commit f96cf3b
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions pynxtools_stm/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit f96cf3b

Please sign in to comment.