Repository 'larch_athena'
hg clone https://toolshed.g2.bx.psu.edu/repos/muon-spectroscopy-computational-project/larch_athena

Changeset 3:82e9dd980916 (2024-03-22)
Previous changeset 2:a1e26990131c (2024-03-04) Next changeset 4:a0d3b0fe0fa3 (2024-04-11)
Commit message:
planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_athena commit d4c7e090dc5c94395d7e1574845ac2c76f2e4f5f
modified:
common.py
larch_athena.py
larch_athena.xml
b
diff -r a1e26990131c -r 82e9dd980916 common.py
--- a/common.py Mon Mar 04 11:43:19 2024 +0000
+++ b/common.py Fri Mar 22 14:23:27 2024 +0000
b
@@ -73,7 +73,11 @@
     )
     for key, parameters_key, default in keys:
         extract_attribute(
-            merged_settings, key, bkg_parameters, parameters_key, default
+            merged_settings=merged_settings,
+            key=key,
+            parameters_group=bkg_parameters,
+            parameters_key=parameters_key,
+            default=default,
         )
 
     if settings:
@@ -116,7 +120,11 @@
     )
     for key, parameters_key, default in keys:
         extract_attribute(
-            merged_settings, key, fft_parameters, parameters_key, default
+            merged_settings=merged_settings,
+            key=key,
+            parameters_group=fft_parameters,
+            parameters_key=parameters_key,
+            default=default,
         )
 
     if settings:
b
diff -r a1e26990131c -r 82e9dd980916 larch_athena.py
--- a/larch_athena.py Mon Mar 04 11:43:19 2024 +0000
+++ b/larch_athena.py Fri Mar 22 14:23:27 2024 +0000
[
@@ -5,7 +5,10 @@
 import sys
 
 from common import (
-    pre_edge_with_defaults, read_all_groups, read_group, xftf_with_defaults
+    pre_edge_with_defaults,
+    read_all_groups,
+    read_group,
+    xftf_with_defaults,
 )
 
 from larch.io import (
@@ -45,12 +48,14 @@
     ) -> "dict[str, Group]":
         if merge_inputs:
             out_group = self.merge_files(
-                dat_files=dat_file, is_zipped=is_zipped
+                dat_files=dat_file,
+                is_zipped=is_zipped,
             )
             return {"out": out_group}
         else:
             return self.load_single_file(
-                filepath=dat_file, is_zipped=is_zipped
+                filepath=dat_file,
+                is_zipped=is_zipped,
             )
 
     def merge_files(
@@ -258,7 +263,7 @@
     pre_edge_settings: dict,
     do_xftf: bool,
     xftf_settings: dict,
-    plot_graph: bool,
+    plot_graph: list,
     annotation: str,
     path_key: str = "out",
 ):
@@ -287,9 +292,11 @@
         xftf_with_defaults(xas_data, xftf_settings)
 
     if plot_graph:
-        plot_edge_fits(f"edge/{path_key}.png", xas_data)
-        plot_flattened(f"flat/{path_key}.png", xas_data)
-        plot_derivative(f"derivative/{path_key}.png", xas_data)
+        plot_graphs(
+            plot_path=f"plot/{path_key}.png",
+            xas_data=xas_data,
+            plot_keys=plot_graph,
+        )
 
     xas_project = create_athena(f"prj/{path_key}.prj")
     xas_project.add_group(xas_data)
@@ -302,36 +309,43 @@
     gc.collect()
 
 
-def plot_derivative(plot_path: str, xafs_group: Group):
-    plt.figure()
-    plt.plot(xafs_group.energy, xafs_group.dmude)
-    plt.grid(color="r", linestyle=":", linewidth=1)
-    plt.xlabel("Energy (eV)")
-    plt.ylabel("Derivative normalised to x$\mu$(E)")  # noqa: W605
-    plt.savefig(plot_path, format="png")
-    plt.close("all")
-
+def plot_graphs(
+    plot_path: str,
+    xas_data: Group,
+    plot_keys: list,
+) -> None:
+    nrows = len(plot_keys)
+    index = 1
+    plt.figure(figsize=(6.4, nrows * 4.8))
+    if "edge" in plot_keys:
+        plt.subplot(nrows, 1, index)
+        plt.plot(xas_data.energy, xas_data.pre_edge, "g", label="pre-edge")
+        plt.plot(xas_data.energy, xas_data.post_edge, "r", label="post-edge")
+        plt.plot(xas_data.energy, xas_data.mu, "b", label="fit data")
+        plt.grid(color="r", linestyle=":", linewidth=1)
+        plt.xlabel("Energy (eV)")
+        plt.ylabel("x$\mu$(E)")  # noqa: W605
+        plt.title("Pre-edge and post_edge fitting to $\mu$")  # noqa: W605
+        plt.legend()
+        index += 1
 
-def plot_edge_fits(plot_path: str, xafs_group: Group):
-    plt.figure()
-    plt.plot(xafs_group.energy, xafs_group.pre_edge, "g", label="pre-edge")
-    plt.plot(xafs_group.energy, xafs_group.post_edge, "r", label="post-edge")
-    plt.plot(xafs_group.energy, xafs_group.mu, "b", label="fit data")
-    plt.grid(color="r", linestyle=":", linewidth=1)
-    plt.xlabel("Energy (eV)")
-    plt.ylabel("x$\mu$(E)")  # noqa: W605
-    plt.title("pre-edge and post_edge fitting to $\mu$")  # noqa: W605
-    plt.legend()
-    plt.savefig(plot_path, format="png")
-    plt.close("all")
+    if "flat" in plot_keys:
+        plt.subplot(nrows, 1, index)
+        plt.plot(xas_data.energy, xas_data.flat)
+        plt.grid(color="r", linestyle=":", linewidth=1)
+        plt.xlabel("Energy (eV)")
+        plt.ylabel("Flattened x$\mu$(E)")  # noqa: W605
+        index += 1
 
+    if "dmude" in plot_keys:
+        plt.subplot(nrows, 1, index)
+        plt.plot(xas_data.energy, xas_data.dmude)
+        plt.grid(color="r", linestyle=":", linewidth=1)
+        plt.xlabel("Energy (eV)")
+        plt.ylabel("Derivative normalised to x$\mu$(E)")  # noqa: W605
+        index += 1
 
-def plot_flattened(plot_path: str, xafs_group: Group):
-    plt.figure()
-    plt.plot(xafs_group.energy, xafs_group.flat)
-    plt.grid(color="r", linestyle=":", linewidth=1)
-    plt.xlabel("Energy (eV)")
-    plt.ylabel("normalised x$\mu$(E)")  # noqa: W605
+    plt.tight_layout(rect=(0, 0, 0.88, 1))
     plt.savefig(plot_path, format="png")
     plt.close("all")
 
b
diff -r a1e26990131c -r 82e9dd980916 larch_athena.xml
--- a/larch_athena.xml Mon Mar 04 11:43:19 2024 +0000
+++ b/larch_athena.xml Fri Mar 22 14:23:27 2024 +0000
[
b'@@ -4,7 +4,7 @@\n         <!-- version of underlying tool (PEP 440) -->\n         <token name="@TOOL_VERSION@">0.9.74</token>\n         <!-- version of this tool wrapper (integer) -->\n-        <token name="@WRAPPER_VERSION@">0</token>\n+        <token name="@WRAPPER_VERSION@">1</token>\n         <!-- citation should be updated with every underlying tool version -->\n         <!-- typical fields to update are version, month, year, and doi -->\n         <token name="@TOOL_CITATION@">10.1088/1742-6596/430/1/012007</token>\n@@ -58,7 +58,7 @@\n         <include type="literal" path="common.py"/>\n     </required_files>\n     <command detect_errors="exit_code"><![CDATA[\n-        mkdir prj edge flat derivative\n+        mkdir prj plot\n         #if $merge_inputs.format.format=="plaintext":\n             #if $merge_inputs.format.is_zipped.is_zipped=="true":\n                 && echo Unzipping \'$merge_inputs.format.is_zipped.dat_file.name\'\n@@ -90,7 +90,7 @@\n                         <conditional name="is_zipped" >\n                             <expand macro="is_zipped"/>\n                             <when value="">\n-                                <param name="dat_file" type="data" format="h5,txt" label="XAFS data file" help="X-ray Absorption Fine Structure (XAFS) data, either in h5 or plaintext."/>\n+                                <param name="dat_file" type="data" format="h5,tabular" label="XAFS data file" help="X-ray Absorption Fine Structure (XAFS) data, either in h5 or plaintext."/>\n                             </when>\n                             <when value="true">\n                                 <param name="dat_file" type="data" format="zip" label="Zipped XAFS data files" help="Zipped X-ray Absorption Fine Structure (XAFS) data, either in h5 or plaintext."/>\n@@ -180,7 +180,11 @@\n                 </when>\n             </conditional>\n         </section>\n-        <param name="plot_graph" type="boolean" label="Plot graph" help="Whether to plot the pre/post edge fitting and the normalised x\xce\xbc data."/>\n+        <param name="plot_graph" type="select" multiple="true" display="checkboxes" label="Plot graphs">\n+            <option value="edge">Pre/post edge fitting</option>\n+            <option value="flat">Flattened x\xce\xbc</option>\n+            <option value="dmude">Derivative of x\xce\xbc</option>\n+        </param>\n         <param name="zip_outputs" type="boolean" label="Zip outputs" help="Whether to zip all outputs into one dataset."/>\n     </inputs>\n     <outputs>\n@@ -193,17 +197,7 @@\n             <filter>not zip_outputs</filter>\n             <filter>not (merge_inputs["merge_inputs"] == "" and ((merge_inputs["format"]["format"] == "plaintext" and merge_inputs["format"]["is_zipped"]["is_zipped"]) or (merge_inputs["format"]["format"] == "athena" and merge_inputs["format"]["extract_group"]["extract_group"] != "single")))</filter>\n         </data>\n-        <data name="edge_plot" format="png" from_work_dir="edge/out.png" label="Edge fitting ${annotation} ${on_string}">\n-            <filter>plot_graph</filter>\n-            <filter>not zip_outputs</filter>\n-            <filter>not (merge_inputs["merge_inputs"] == "" and ((merge_inputs["format"]["format"] == "plaintext" and merge_inputs["format"]["is_zipped"]["is_zipped"]) or (merge_inputs["format"]["format"] == "athena" and merge_inputs["format"]["extract_group"]["extract_group"] != "single")))</filter>\n-        </data>\n-        <data name="flat_plot" format="png" from_work_dir="flat/out.png" label="Flattened plot ${annotation} ${on_string}">\n-            <filter>plot_graph</filter>\n-            <filter>not zip_outputs</filter>\n-            <filter>not (merge_inputs["merge_inputs"] == "" and ((merge_inputs["format"]["format"] == "plaintext" and merge_inputs["format"]["is_zipped"]["is_zipped"]) or (merge_inputs["format"]["format"] == "athena" and merge_inputs["format"]["extract_group"]["extract_group"] != "single")))</filter>\n-        </data>\n-        <data name="derivative_plot" format="png" from_work_dir="derivati'..b'e"/>\n             <param name="dat_file" value="test.zip"/>\n-            <param name="plot_graph" value="true"/>\n+            <param name="plot_graph" value="edge,flat,dmude"/>\n             <output_collection name="athena_project_file_collection" type="list" count="2"/>\n-            <output_collection name="edge_plot_collection" type="list" count="2"/>\n-            <output_collection name="flat_plot_collection" type="list" count="2"/>\n-            <output_collection name="derivative_plot_collection" type="list" count="2"/>\n+            <output_collection name="plot_collection" type="list" count="2"/>\n         </test>\n         <!-- 6 -->\n         <test expect_num_outputs="1">\n             <param name="is_zipped" value="true"/>\n             <param name="dat_file" value="h5.zip"/>\n-            <param name="plot_graph" value="true"/>\n+            <param name="plot_graph" value="edge,flat,dmude"/>\n             <param name="zip_outputs" value="true"/>\n             <output name="out_zip">\n                 <assert_contents>\n-                    <has_size value="312000" delta="500"/>\n+                    <has_size value="75500" delta="500"/>\n                 </assert_contents>\n             </output>\n         </test>\n@@ -320,30 +290,20 @@\n             </output>\n         </test>\n         <!-- 8 -->\n-        <test expect_num_outputs="4">\n+        <test expect_num_outputs="2">\n             <param name="dat_file" value="test.xmu"/>\n             <param name="calibrate" value="true"/>\n             <param name="energy_min" value="7000"/>\n             <param name="energy_max" value="7200"/>\n-            <param name="plot_graph" value="true"/>\n+            <param name="plot_graph" value="edge,flat,dmude"/>\n             <output name="athena_project_file">\n                 <assert_contents>\n                     <has_size value="3300" delta="50"/>\n                 </assert_contents>\n             </output>\n-            <output name="edge_plot">\n-                <assert_contents>\n-                    <has_size value="44900" delta="100"/>\n-                </assert_contents>\n-            </output>\n-            <output name="flat_plot">\n+            <output name="plot">\n                 <assert_contents>\n-                    <has_size value="39400" delta="100"/>\n-                </assert_contents>\n-            </output>\n-            <output name="derivative_plot">\n-                <assert_contents>\n-                    <has_size value="45900" delta="100"/>\n+                    <has_size value="134700" delta="100"/>\n                 </assert_contents>\n             </output>\n         </test>\n@@ -392,29 +352,19 @@\n             </output>\n         </test>\n         <!-- 13: Test merging and plotting multiple prj inputs -->\n-        <test expect_num_outputs="4">\n+        <test expect_num_outputs="2">\n             <param name="merge_inputs" value="true"/>\n             <param name="format" value="athena"/>\n             <param name="dat_file" value="test.prj,test.prj"/>\n-            <param name="plot_graph" value="true"/>\n+            <param name="plot_graph" value="edge,flat,dmude"/>\n             <output name="athena_project_file">\n                 <assert_contents>\n                     <has_size value="4500" delta="100"/>\n                 </assert_contents>\n             </output>\n-            <output name="edge_plot">\n-                <assert_contents>\n-                    <has_size value="54200" delta="100"/>\n-                </assert_contents>\n-            </output>\n-            <output name="flat_plot">\n+            <output name="plot">\n                 <assert_contents>\n-                    <has_size value="39400" delta="100"/>\n-                </assert_contents>\n-            </output>\n-            <output name="derivative_plot">\n-                <assert_contents>\n-                    <has_size value="41800" delta="100"/>\n+                    <has_size value="135000" delta="100"/>\n                 </assert_contents>\n             </output>\n         </test>\n'