# HG changeset patch
# User muon-spectroscopy-computational-project
# Date 1699976122 0
# Node ID f59731986b616b0853ae156be19b2638c598734b
planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_lcf commit 5be486890442dedfb327289d597e1c8110240735
diff -r 000000000000 -r f59731986b61 common.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common.py	Tue Nov 14 15:35:22 2023 +0000
@@ -0,0 +1,46 @@
+from typing import Iterable
+
+from larch.io import extract_athenagroup, read_athena
+from larch.io.athena_project import AthenaGroup
+from larch.symboltable import Group
+from larch.xafs import autobk, pre_edge, xftf
+
+
+def get_group(athena_group: AthenaGroup, key: str = None) -> Group:
+    if key is None:
+        group_keys = list(athena_group._athena_groups.keys())
+        key = group_keys[0]
+    return extract_athenagroup(athena_group._athena_groups[key])
+
+
+def read_group(dat_file: str, key: str = None, xftf_params: dict = None):
+    athena_group = read_athena(dat_file)
+    group = get_group(athena_group, key)
+    bkg_parameters = group.athena_params.bkg
+    print(group.athena_params.fft)
+    print(group.athena_params.fft.__dict__)
+    pre_edge(
+        group,
+        e0=bkg_parameters.e0,
+        pre1=bkg_parameters.pre1,
+        pre2=bkg_parameters.pre2,
+        norm1=bkg_parameters.nor1,
+        norm2=bkg_parameters.nor2,
+        nnorm=bkg_parameters.nnorm,
+        make_flat=bkg_parameters.flatten,
+    )
+    autobk(group)
+    if xftf_params is None:
+        xftf(group)
+    else:
+        print(xftf_params)
+        xftf(group, **xftf_params)
+        xftf_details = Group()
+        setattr(xftf_details, "call_args", xftf_params)
+        group.xftf_details = xftf_details
+    return group
+
+
+def read_groups(dat_files: "list[str]", key: str = None) -> Iterable[Group]:
+    for dat_file in dat_files:
+        yield read_group(dat_file=dat_file, key=key)
diff -r 000000000000 -r f59731986b61 larch_lcf.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/larch_lcf.py	Tue Nov 14 15:35:22 2023 +0000
@@ -0,0 +1,82 @@
+import json
+import sys
+
+from common import read_group
+
+from larch.math.lincombo_fitting import get_label, lincombo_fit
+from larch.symboltable import Group
+
+import matplotlib
+import matplotlib.pyplot as plt
+
+
+def plot(
+    group_to_fit: Group,
+    fit_group: Group,
+    energy_min: float,
+    energy_max: float,
+):
+    formatted_label = ""
+    for label, weight in fit_group.weights.items():
+        formatted_label += f"{label}: {weight:.3%}\n"
+
+    plt.figure()
+    plt.plot(
+        group_to_fit.energy,
+        group_to_fit.norm,
+        label=group_to_fit.filename,
+        linewidth=4,
+        color="blue",
+    )
+    plt.plot(
+        fit_group.xdata,
+        fit_group.ydata,
+        label=formatted_label[:-1],
+        linewidth=2,
+        color="orange",
+        linestyle="--",
+    )
+    plt.grid(color="black", linestyle=":", linewidth=1)  # show and format grid
+    plt.xlim(energy_min, energy_max)
+    plt.xlabel("Energy (eV)")
+    plt.ylabel("normalised x$\mu$(E)")  # noqa: W605
+    plt.legend()
+    plt.savefig("plot.png", format="png")
+    plt.close("all")
+
+
+def set_label(component_group, label):
+    if label is not None:
+        component_group.filename = label
+    else:
+        component_group.filename = get_label(component_group)
+
+
+if __name__ == "__main__":
+    # larch imports set this to an interactive backend, so need to change it
+    matplotlib.use("Agg")
+    prj_file = sys.argv[1]
+    input_values = json.load(open(sys.argv[2], "r", encoding="utf-8"))
+
+    group_to_fit = read_group(prj_file)
+    set_label(group_to_fit, input_values["label"])
+
+    component_groups = []
+    for component in input_values["components"]:
+        component_group = read_group(component["component_file"])
+        set_label(component_group, component["label"])
+        component_groups.append(component_group)
+
+    fit_group = lincombo_fit(group_to_fit, component_groups)
+    print(f"Goodness of fit (rfactor): {fit_group.rfactor:.6%}")
+
+    energy_min = input_values["energy_min"]
+    energy_max = input_values["energy_max"]
+    if input_values["energy_format"] == "relative":
+        e0 = group_to_fit.e0
+        if energy_min is not None:
+            energy_min += e0
+        if energy_max is not None:
+            energy_max += e0
+
+    plot(group_to_fit, fit_group, energy_min, energy_max)
diff -r 000000000000 -r f59731986b61 larch_lcf.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/larch_lcf.xml	Tue Nov 14 15:35:22 2023 +0000
@@ -0,0 +1,63 @@
+
+    perform linear combination fit on XAS data
+    
+        
+        0.9.71
+        
+        0
+        
+        
+        10.1088/1742-6596/430/1/012007
+        macros.xml
+    
+    
+        
+    
+    
+        xraylarch
+        matplotlib
+    
+    
+        
+    
+    
+    
+        
+    
+    
+        
+        
+        
+            
+            
+        
+        
+    
+    
+        
+    
+    
+        
+            
+            
+            
+            
+            
+            
+        
+    
+    
+    
+        @TOOL_CITATION@
+        10.1107/S0909049505012719
+    
+
\ No newline at end of file
diff -r 000000000000 -r f59731986b61 macros.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/macros.xml	Tue Nov 14 15:35:22 2023 +0000
@@ -0,0 +1,26 @@
+
+    
+        
+            
+            
+        
+        
+        
+    
+    
+        
+        
+        
+        
+        
+            
+            
+            
+            
+            
+            
+        
+        
+        
+    
+
\ No newline at end of file
diff -r 000000000000 -r f59731986b61 test-data/PtSn_OCO_Abu_1.prj
Binary file test-data/PtSn_OCO_Abu_1.prj has changed
diff -r 000000000000 -r f59731986b61 test-data/SnO2_extracted.prj
Binary file test-data/SnO2_extracted.prj has changed
diff -r 000000000000 -r f59731986b61 test-data/Sn_foil_extracted.prj
Binary file test-data/Sn_foil_extracted.prj has changed