changeset 3:82e9dd980916 draft

planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_athena commit d4c7e090dc5c94395d7e1574845ac2c76f2e4f5f
author muon-spectroscopy-computational-project
date Fri, 22 Mar 2024 14:23:27 +0000
parents a1e26990131c
children a0d3b0fe0fa3
files common.py larch_athena.py larch_athena.xml
diffstat 3 files changed, 86 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/common.py	Mon Mar 04 11:43:19 2024 +0000
+++ b/common.py	Fri Mar 22 14:23:27 2024 +0000
@@ -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:
--- 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")
 
--- a/larch_athena.xml	Mon Mar 04 11:43:19 2024 +0000
+++ b/larch_athena.xml	Fri Mar 22 14:23:27 2024 +0000
@@ -4,7 +4,7 @@
         <!-- version of underlying tool (PEP 440) -->
         <token name="@TOOL_VERSION@">0.9.74</token>
         <!-- version of this tool wrapper (integer) -->
-        <token name="@WRAPPER_VERSION@">0</token>
+        <token name="@WRAPPER_VERSION@">1</token>
         <!-- citation should be updated with every underlying tool version -->
         <!-- typical fields to update are version, month, year, and doi -->
         <token name="@TOOL_CITATION@">10.1088/1742-6596/430/1/012007</token>
@@ -58,7 +58,7 @@
         <include type="literal" path="common.py"/>
     </required_files>
     <command detect_errors="exit_code"><![CDATA[
-        mkdir prj edge flat derivative
+        mkdir prj plot
         #if $merge_inputs.format.format=="plaintext":
             #if $merge_inputs.format.is_zipped.is_zipped=="true":
                 && echo Unzipping '$merge_inputs.format.is_zipped.dat_file.name'
@@ -90,7 +90,7 @@
                         <conditional name="is_zipped" >
                             <expand macro="is_zipped"/>
                             <when value="">
-                                <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."/>
+                                <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."/>
                             </when>
                             <when value="true">
                                 <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."/>
@@ -180,7 +180,11 @@
                 </when>
             </conditional>
         </section>
-        <param name="plot_graph" type="boolean" label="Plot graph" help="Whether to plot the pre/post edge fitting and the normalised xμ data."/>
+        <param name="plot_graph" type="select" multiple="true" display="checkboxes" label="Plot graphs">
+            <option value="edge">Pre/post edge fitting</option>
+            <option value="flat">Flattened xμ</option>
+            <option value="dmude">Derivative of xμ</option>
+        </param>
         <param name="zip_outputs" type="boolean" label="Zip outputs" help="Whether to zip all outputs into one dataset."/>
     </inputs>
     <outputs>
@@ -193,17 +197,7 @@
             <filter>not zip_outputs</filter>
             <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>
         </data>
-        <data name="edge_plot" format="png" from_work_dir="edge/out.png" label="Edge fitting ${annotation} ${on_string}">
-            <filter>plot_graph</filter>
-            <filter>not zip_outputs</filter>
-            <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>
-        </data>
-        <data name="flat_plot" format="png" from_work_dir="flat/out.png" label="Flattened plot ${annotation} ${on_string}">
-            <filter>plot_graph</filter>
-            <filter>not zip_outputs</filter>
-            <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>
-        </data>
-        <data name="derivative_plot" format="png" from_work_dir="derivative/out.png" label="Derivative plot ${annotation} ${on_string}">
+        <data name="plot" format="png" from_work_dir="plot/out.png" label="Plot of ${annotation} ${on_string}">
             <filter>plot_graph</filter>
             <filter>not zip_outputs</filter>
             <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>
@@ -214,20 +208,8 @@
             <filter>not zip_outputs</filter>
             <filter>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>
         </collection>
-        <collection name="edge_plot_collection" format="png" type="list" label="Edge fittings ${annotation} ${on_string}">
-            <discover_datasets pattern="__name_and_ext__" directory="edge"/>
-            <filter>plot_graph</filter>
-            <filter>not zip_outputs</filter>
-            <filter>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>
-        </collection>
-        <collection name="flat_plot_collection" format="png" type="list" label="Flattened plots ${annotation} ${on_string}">
-            <discover_datasets pattern="__name_and_ext__" directory="flat"/>
-            <filter>plot_graph</filter>
-            <filter>not zip_outputs</filter>
-            <filter>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>
-        </collection>
-        <collection name="derivative_plot_collection" format="png" type="list" label="Derivative plots ${annotation} ${on_string}">
-            <discover_datasets pattern="__name_and_ext__" directory="derivative"/>
+        <collection name="plot_collection" format="png" type="list" label="Plots of ${annotation} ${on_string}">
+            <discover_datasets pattern="__name_and_ext__" directory="plot"/>
             <filter>plot_graph</filter>
             <filter>not zip_outputs</filter>
             <filter>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>
@@ -262,49 +244,37 @@
             </output>
         </test>
         <!-- 4 -->
-        <test expect_num_outputs="4">
+        <test expect_num_outputs="2">
             <param name="dat_file" value="test.xmu"/>
-            <param name="plot_graph" value="true"/>
+            <param name="plot_graph" value="edge,flat,dmude"/>
             <output name="athena_project_file">
                 <assert_contents>
                     <has_size value="5400" delta="100"/>
                 </assert_contents>
             </output>
-            <output name="edge_plot">
-                <assert_contents>
-                    <has_size value="54036" delta="20"/>
-                </assert_contents>
-            </output>
-            <output name="flat_plot">
+            <output name="plot">
                 <assert_contents>
-                    <has_size value="39266" delta="20"/>
-                </assert_contents>
-            </output>
-            <output name="derivative_plot">
-                <assert_contents>
-                    <has_size value="42435" delta="20"/>
+                    <has_size value="134972" delta="20"/>
                 </assert_contents>
             </output>
         </test>
         <!-- 5 -->
-        <test expect_num_outputs="4">
+        <test expect_num_outputs="2">
             <param name="is_zipped" value="true"/>
             <param name="dat_file" value="test.zip"/>
-            <param name="plot_graph" value="true"/>
+            <param name="plot_graph" value="edge,flat,dmude"/>
             <output_collection name="athena_project_file_collection" type="list" count="2"/>
-            <output_collection name="edge_plot_collection" type="list" count="2"/>
-            <output_collection name="flat_plot_collection" type="list" count="2"/>
-            <output_collection name="derivative_plot_collection" type="list" count="2"/>
+            <output_collection name="plot_collection" type="list" count="2"/>
         </test>
         <!-- 6 -->
         <test expect_num_outputs="1">
             <param name="is_zipped" value="true"/>
             <param name="dat_file" value="h5.zip"/>
-            <param name="plot_graph" value="true"/>
+            <param name="plot_graph" value="edge,flat,dmude"/>
             <param name="zip_outputs" value="true"/>
             <output name="out_zip">
                 <assert_contents>
-                    <has_size value="312000" delta="500"/>
+                    <has_size value="75500" delta="500"/>
                 </assert_contents>
             </output>
         </test>
@@ -320,30 +290,20 @@
             </output>
         </test>
         <!-- 8 -->
-        <test expect_num_outputs="4">
+        <test expect_num_outputs="2">
             <param name="dat_file" value="test.xmu"/>
             <param name="calibrate" value="true"/>
             <param name="energy_min" value="7000"/>
             <param name="energy_max" value="7200"/>
-            <param name="plot_graph" value="true"/>
+            <param name="plot_graph" value="edge,flat,dmude"/>
             <output name="athena_project_file">
                 <assert_contents>
                     <has_size value="3300" delta="50"/>
                 </assert_contents>
             </output>
-            <output name="edge_plot">
-                <assert_contents>
-                    <has_size value="44900" delta="100"/>
-                </assert_contents>
-            </output>
-            <output name="flat_plot">
+            <output name="plot">
                 <assert_contents>
-                    <has_size value="39400" delta="100"/>
-                </assert_contents>
-            </output>
-            <output name="derivative_plot">
-                <assert_contents>
-                    <has_size value="45900" delta="100"/>
+                    <has_size value="134700" delta="100"/>
                 </assert_contents>
             </output>
         </test>
@@ -392,29 +352,19 @@
             </output>
         </test>
         <!-- 13: Test merging and plotting multiple prj inputs -->
-        <test expect_num_outputs="4">
+        <test expect_num_outputs="2">
             <param name="merge_inputs" value="true"/>
             <param name="format" value="athena"/>
             <param name="dat_file" value="test.prj,test.prj"/>
-            <param name="plot_graph" value="true"/>
+            <param name="plot_graph" value="edge,flat,dmude"/>
             <output name="athena_project_file">
                 <assert_contents>
                     <has_size value="4500" delta="100"/>
                 </assert_contents>
             </output>
-            <output name="edge_plot">
-                <assert_contents>
-                    <has_size value="54200" delta="100"/>
-                </assert_contents>
-            </output>
-            <output name="flat_plot">
+            <output name="plot">
                 <assert_contents>
-                    <has_size value="39400" delta="100"/>
-                </assert_contents>
-            </output>
-            <output name="derivative_plot">
-                <assert_contents>
-                    <has_size value="41800" delta="100"/>
+                    <has_size value="135000" delta="100"/>
                 </assert_contents>
             </output>
         </test>