Mercurial > repos > muon-spectroscopy-computational-project > larch_lcf
comparison common.py @ 3:6bb927e155fc draft
planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_lcf commit 3fe6078868efd0fcea0fb5eea8dcd4b152d9c0a8
author | muon-spectroscopy-computational-project |
---|---|
date | Thu, 11 Apr 2024 09:02:19 +0000 |
parents | 85284c3e1354 |
children | c2d5bfef5b63 |
comparison
equal
deleted
inserted
replaced
2:85284c3e1354 | 3:6bb927e155fc |
---|---|
5 from larch.symboltable import Group | 5 from larch.symboltable import Group |
6 from larch.xafs import autobk, pre_edge, xftf | 6 from larch.xafs import autobk, pre_edge, xftf |
7 | 7 |
8 | 8 |
9 def get_group(athena_group: AthenaGroup, key: str = None) -> Group: | 9 def get_group(athena_group: AthenaGroup, key: str = None) -> Group: |
10 group_keys = list(athena_group._athena_groups.keys()) | 10 group_keys = list(athena_group.keys()) |
11 if key is None: | 11 if key is None: |
12 key = group_keys[0] | 12 key = group_keys[0] |
13 else: | 13 else: |
14 key = key.replace("-", "_") | 14 key = key.replace("-", "_") |
15 | 15 |
16 try: | 16 try: |
17 return extract_athenagroup(athena_group._athena_groups[key]) | 17 return extract_athenagroup(athena_group.groups[key]) |
18 except KeyError as e: | 18 except KeyError as e: |
19 raise KeyError(f"{key} not in {group_keys}") from e | 19 raise KeyError(f"{key} not in {group_keys}") from e |
20 | 20 |
21 | 21 |
22 def read_all_groups(dat_file: str, key: str = None) -> "dict[str, Group]": | 22 def read_all_groups(dat_file: str, key: str = None) -> "dict[str, Group]": |
26 do_preedge=False, | 26 do_preedge=False, |
27 do_bkg=False, | 27 do_bkg=False, |
28 do_fft=False, | 28 do_fft=False, |
29 ) | 29 ) |
30 all_groups = {} | 30 all_groups = {} |
31 for key in athena_group._athena_groups.keys(): | 31 for key in athena_group.keys(): |
32 print(f"\nExtracting group {key}") | 32 print(f"\nExtracting group {key}") |
33 group = get_group(athena_group, key) | 33 group = get_group(athena_group, key) |
34 pre_edge_with_defaults(group=group) | 34 pre_edge_with_defaults(group=group) |
35 xftf_with_defaults(group=group) | 35 xftf_with_defaults(group=group) |
36 all_groups[key] = group | 36 all_groups[key] = group |
50 pre_edge_with_defaults(group=group) | 50 pre_edge_with_defaults(group=group) |
51 xftf_with_defaults(group=group) | 51 xftf_with_defaults(group=group) |
52 return group | 52 return group |
53 | 53 |
54 | 54 |
55 def pre_edge_with_defaults(group: Group, settings: dict = None): | 55 def pre_edge_with_defaults( |
56 group: Group, settings: dict = None, ref_channel: str = None | |
57 ): | |
56 merged_settings = {} | 58 merged_settings = {} |
57 try: | 59 if ref_channel is not None: |
58 bkg_parameters = group.athena_params.bkg | 60 print(f"Performing pre-edge with reference channel {ref_channel}") |
59 except AttributeError as e: | 61 ref = getattr(group, ref_channel.lower()) |
60 print(f"Cannot load group.athena_params.bkg from group:\n{e}") | 62 group.e0 = None |
61 bkg_parameters = None | 63 pre_edge(energy=group.energy, mu=ref, group=group) |
64 bkg_parameters = group.pre_edge_details | |
65 else: | |
66 try: | |
67 bkg_parameters = group.athena_params.bkg | |
68 except AttributeError as e: | |
69 print(f"Cannot load group.athena_params.bkg from group:\n{e}") | |
70 bkg_parameters = None | |
62 | 71 |
63 keys = ( | 72 keys = ( |
64 ("e0", "e0", None), | 73 ("e0", "e0", None), |
65 ("pre1", "pre1", None), | 74 ("pre1", "pre1", None), |
66 ("pre2", "pre2", None), | 75 ("pre2", "pre2", None), |
71 ("step", "step", None), | 80 ("step", "step", None), |
72 ("nvict", "nvict", None), | 81 ("nvict", "nvict", None), |
73 ) | 82 ) |
74 for key, parameters_key, default in keys: | 83 for key, parameters_key, default in keys: |
75 extract_attribute( | 84 extract_attribute( |
76 merged_settings, key, bkg_parameters, parameters_key, default | 85 merged_settings=merged_settings, |
86 key=key, | |
87 parameters_group=bkg_parameters, | |
88 parameters_key=parameters_key, | |
89 default=default, | |
77 ) | 90 ) |
78 | 91 |
79 if settings: | 92 if settings: |
80 for k, v in settings.items(): | 93 for k, v in settings.items(): |
81 if k == "nvict": | 94 if k == "nvict": |
114 ("kweight", "kweight", 2), | 127 ("kweight", "kweight", 2), |
115 ("window", "kwindow", "kaiser"), | 128 ("window", "kwindow", "kaiser"), |
116 ) | 129 ) |
117 for key, parameters_key, default in keys: | 130 for key, parameters_key, default in keys: |
118 extract_attribute( | 131 extract_attribute( |
119 merged_settings, key, fft_parameters, parameters_key, default | 132 merged_settings=merged_settings, |
133 key=key, | |
134 parameters_group=fft_parameters, | |
135 parameters_key=parameters_key, | |
136 default=default, | |
120 ) | 137 ) |
121 | 138 |
122 if settings: | 139 if settings: |
123 for k, v in settings.items(): | 140 for k, v in settings.items(): |
124 merged_settings[k] = v | 141 merged_settings[k] = v |