comparison common.py @ 1:6c28339b73f7 draft

planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_lcf commit 1cf6d7160497ba58fe16a51f00d088a20934eba6
author muon-spectroscopy-computational-project
date Wed, 06 Dec 2023 13:04:01 +0000
parents f59731986b61
children 85284c3e1354
comparison
equal deleted inserted replaced
0:f59731986b61 1:6c28339b73f7
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 if key is None: 11 if key is None:
11 group_keys = list(athena_group._athena_groups.keys())
12 key = group_keys[0] 12 key = group_keys[0]
13 return extract_athenagroup(athena_group._athena_groups[key]) 13 else:
14 key = key.replace("-", "_")
15
16 try:
17 return extract_athenagroup(athena_group._athena_groups[key])
18 except KeyError as e:
19 raise KeyError(f"{key} not in {group_keys}") from e
14 20
15 21
16 def read_group(dat_file: str, key: str = None, xftf_params: dict = None): 22 def read_all_groups(dat_file: str, key: str = None) -> "dict[str, Group]":
17 athena_group = read_athena(dat_file) 23 # Cannot rely on do_ABC as _larch is None
24 athena_group = read_athena(
25 dat_file,
26 do_preedge=False,
27 do_bkg=False,
28 do_fft=False,
29 )
30 all_groups = {}
31 for key in athena_group._athena_groups.keys():
32 group = get_group(athena_group, key)
33 pre_edge_with_defaults(group=group)
34 xftf_with_defaults(group=group)
35 all_groups[key] = group
36
37 return all_groups
38
39
40 def read_group(dat_file: str, key: str = None):
41 # Cannot rely on do_ABC as _larch is None
42 athena_group = read_athena(
43 dat_file,
44 do_preedge=False,
45 do_bkg=False,
46 do_fft=False,
47 )
18 group = get_group(athena_group, key) 48 group = get_group(athena_group, key)
19 bkg_parameters = group.athena_params.bkg 49 pre_edge_with_defaults(group=group)
20 print(group.athena_params.fft) 50 xftf_with_defaults(group=group)
21 print(group.athena_params.fft.__dict__) 51 return group
22 pre_edge( 52
23 group, 53
24 e0=bkg_parameters.e0, 54 def pre_edge_with_defaults(group: Group, settings: dict = None):
25 pre1=bkg_parameters.pre1, 55 merged_settings = {}
26 pre2=bkg_parameters.pre2, 56 try:
27 norm1=bkg_parameters.nor1, 57 bkg_parameters = group.athena_params.bkg
28 norm2=bkg_parameters.nor2, 58 except AttributeError as e:
29 nnorm=bkg_parameters.nnorm, 59 print(f"Cannot load group.athena_params.bkg from group:\n{e}")
30 make_flat=bkg_parameters.flatten, 60 bkg_parameters = None
61
62 keys = (
63 ("e0", "e0", None),
64 ("pre1", "pre1", None),
65 ("pre2", "pre2", None),
66 ("norm1", "nor1", None),
67 ("norm2", "nor2", None),
68 ("nnorm", "nnorm", None),
69 ("make_flat", "flatten", None),
70 ("step", "step", None),
71 # This cannot be read from file as it is not stored by Larch (0.9.71)
72 # ("nvict", "nvict", None),
31 ) 73 )
32 autobk(group) 74 for key, parameters_key, default in keys:
33 if xftf_params is None: 75 extract_attribute(
34 xftf(group) 76 merged_settings, key, bkg_parameters, parameters_key, default
35 else: 77 )
36 print(xftf_params) 78
37 xftf(group, **xftf_params) 79 if settings:
38 xftf_details = Group() 80 for k, v in settings.items():
39 setattr(xftf_details, "call_args", xftf_params) 81 if k == "nvict":
40 group.xftf_details = xftf_details 82 print(
41 return group 83 "WARNING: `nvict` can be used for pre-edge but is not "
84 "saved to file, so value used will not be accessible in "
85 "future operations using this Athena .prj"
86 )
87 merged_settings[k] = v
88
89 print(f"Pre-edge normalization with {merged_settings}")
90 try:
91 pre_edge(group, **merged_settings)
92 except Warning as e:
93 raise Warning(
94 "Unable to perform pre-edge fitting with:\n\n"
95 f"energy:\n{group.energy}\n\nmu:{group.mu}\n\n"
96 "Consider checking the correct columns have been extracted"
97 ) from e
98 autobk(group, pre_edge_kws=merged_settings)
99
100
101 def xftf_with_defaults(group: Group, settings: dict = None):
102 merged_settings = {}
103 try:
104 fft_parameters = group.athena_params.fft
105 except AttributeError as e:
106 print(f"Cannot load group.athena_params.fft from group:\n{e}")
107 fft_parameters = None
108
109 keys = (
110 ("kmin", "kmin", 0),
111 ("kmax", "kmax", 20),
112 ("dk", "dk", 1),
113 ("kweight", "kw", 2),
114 ("kweight", "kweight", 2),
115 ("window", "kwindow", "kaiser"),
116 )
117 for key, parameters_key, default in keys:
118 extract_attribute(
119 merged_settings, key, fft_parameters, parameters_key, default
120 )
121
122 if settings:
123 for k, v in settings.items():
124 merged_settings[k] = v
125
126 print(f"XFTF with {merged_settings}")
127 xftf(group, **merged_settings)
128 xftf_details = Group()
129 setattr(xftf_details, "call_args", merged_settings)
130 group.xftf_details = xftf_details
131
132
133 def extract_attribute(
134 merged_settings: dict,
135 key: str,
136 parameters_group: Group,
137 parameters_key: str,
138 default: "str|int" = None,
139 ):
140 if parameters_group is not None:
141 try:
142 merged_settings[key] = getattr(parameters_group, parameters_key)
143 return
144 except AttributeError:
145 pass
146
147 if default is not None:
148 merged_settings[key] = default
42 149
43 150
44 def read_groups(dat_files: "list[str]", key: str = None) -> Iterable[Group]: 151 def read_groups(dat_files: "list[str]", key: str = None) -> Iterable[Group]:
45 for dat_file in dat_files: 152 for dat_file in dat_files:
46 yield read_group(dat_file=dat_file, key=key) 153 yield read_group(dat_file=dat_file, key=key)