diff larch_lcf.py @ 4:c2d5bfef5b63 draft

planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_lcf commit 4814f53888643f1d3667789050914675fffb7d59
author muon-spectroscopy-computational-project
date Fri, 23 Aug 2024 14:10:44 +0000 (9 months ago)
parents 6c28339b73f7
children
line wrap: on
line diff
--- a/larch_lcf.py	Thu Apr 11 09:02:19 2024 +0000
+++ b/larch_lcf.py	Fri Aug 23 14:10:44 2024 +0000
@@ -1,7 +1,8 @@
 import json
+import os
 import sys
 
-from common import read_group
+from common import read_group, sorting_key
 
 from larch.math.lincombo_fitting import get_label, lincombo_fit
 from larch.symboltable import Group
@@ -15,6 +16,7 @@
     fit_group: Group,
     x_limit_min: float,
     x_limit_max: float,
+    prj_id: str,
 ):
     formatted_label = ""
     for label, weight in fit_group.weights.items():
@@ -41,7 +43,7 @@
     plt.xlabel("Energy (eV)")
     plt.ylabel("normalised x$\mu$(E)")  # noqa: W605
     plt.legend()
-    plt.savefig("plot.png", format="png")
+    plt.savefig(f"plot/{prj_id}.png", format="png")
     plt.close("all")
 
 
@@ -52,12 +54,7 @@
         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"))
-
+def main(prj_file: str, input_values: dict, prj_id: str = "plot"):
     group_to_fit = read_group(prj_file)
     set_label(group_to_fit, input_values["label"])
 
@@ -79,4 +76,29 @@
 
     x_limit_min = input_values["x_limit_min"]
     x_limit_max = input_values["x_limit_max"]
-    plot(group_to_fit, fit_group, x_limit_min, x_limit_max)
+    plot(group_to_fit, fit_group, x_limit_min, x_limit_max, prj_id)
+
+
+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"))
+
+    if input_values["execution"]["execution"] == "parallel":
+        main(prj_file, input_values)
+
+    else:
+        if os.path.isdir(prj_file):
+            # Sort the unzipped directory, all filenames should be zero-padded
+            paths = os.listdir(prj_file)
+            filepaths = [os.path.join(prj_file, p) for p in paths]
+            filepaths.sort(key=sorting_key)
+        else:
+            # DO NOT sort if we have multiple Galaxy datasets - the filenames
+            # are arbitrary but should be in order
+            filepaths = prj_file.split(",")
+
+        id_length = len(str(len(filepaths)))
+        for i, prj_file in enumerate(filepaths):
+            main(prj_file, input_values, str(i).zfill(id_length))