# HG changeset patch # User muon-spectroscopy-computational-project # Date 1699976136 0 # Node ID 886949a033777207c93fd1ae7e13f374370af8dd planemo upload for repository https://github.com/MaterialsGalaxy/larch-tools/tree/main/larch_plot commit 5be486890442dedfb327289d597e1c8110240735 diff -r 000000000000 -r 886949a03377 common.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common.py Tue Nov 14 15:35:36 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 886949a03377 larch_plot.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/larch_plot.py Tue Nov 14 15:35:36 2023 +0000 @@ -0,0 +1,101 @@ +import json +import sys + +from common import read_groups + +import matplotlib +import matplotlib.pyplot as plt + +import numpy as np + + +Y_LABELS = { + "norm": r"x$\mu$(E), normalised", + "dmude": r"d(x$\mu$(E))/dE, normalised", + "chir_mag": r"|$\chi$(r)|", +} + + +def main(dat_files: "list[str]", plot_settings: "list[dict]"): + groups = list(read_groups(dat_files)) + + for i, settings in enumerate(plot_settings): + data_list = [] + e0_min = None + e0_max = None + variable = settings["variable"]["variable"] + x_min = settings["variable"]["energy_min"] + x_max = settings["variable"]["energy_max"] + plot_path = f"plots/{i}_{variable}.png" + plt.figure() + + for group in groups: + label = group.athena_params.annotation or group.athena_params.id + if variable == "chir_mag": + x = group.r + energy_format = None + else: + x = group.energy + energy_format = settings["variable"]["energy_format"] + if energy_format == "relative": + e0 = group.athena_params.bkg.e0 + e0_min = find_relative_limit(e0_min, e0, min) + e0_max = find_relative_limit(e0_max, e0, max) + + y = getattr(group, variable) + if x_min is None and x_max is None: + plt.plot(x, y, label=label) + else: + data_list.append({"x": x, "y": y, "label": label}) + + if variable != "chir_mag" and energy_format == "relative": + if x_min is not None: + x_min += e0_min + if x_max is not None: + x_max += e0_max + + if x_min is not None or x_max is not None: + for data in data_list: + index_min = None + index_max = None + x = data["x"] + if x_min is not None: + index_min = max(np.searchsorted(x, x_min) - 1, 0) + if x_max is not None: + index_max = min(np.searchsorted(x, x_max) + 1, len(x)) + plt.plot( + x[index_min:index_max], + data["y"][index_min:index_max], + label=data["label"], + ) + + plt.xlim(x_min, x_max) + + save_plot(variable, plot_path) + + +def find_relative_limit(e0_min: "float|None", e0: float, function: callable): + if e0_min is None: + e0_min = e0 + else: + e0_min = function(e0_min, e0) + return e0_min + + +def save_plot(y_type: str, plot_path: str): + plt.grid(color="r", linestyle=":", linewidth=1) + plt.xlabel("Energy (eV)") + plt.ylabel(Y_LABELS[y_type]) + plt.legend() + plt.savefig(plot_path, format="png") + plt.close("all") + + +if __name__ == "__main__": + # larch imports set this to an interactive backend, so need to change it + matplotlib.use("Agg") + + dat_files = sys.argv[1] + input_values = json.load(open(sys.argv[2], "r", encoding="utf-8")) + + main(dat_files.split(","), input_values["plots"]) diff -r 000000000000 -r 886949a03377 larch_plot.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/larch_plot.xml Tue Nov 14 15:35:36 2023 +0000 @@ -0,0 +1,83 @@ + + plot Athena projects + + + 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 886949a03377 macros.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macros.xml Tue Nov 14 15:35:36 2023 +0000 @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 000000000000 -r 886949a03377 test-data/test.prj Binary file test-data/test.prj has changed