From bb8c158591f8c1abfe74474f74567763f7477576 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Wed, 8 Feb 2023 10:00:14 +0100 Subject: [PATCH] Add a possibility to plot (m)hkl with measured data --- pyzebra/app/panel_plot_data.py | 46 +++++++++++++++++++++++++++++++++- pyzebra/app/plot_hkl.py | 45 ++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/pyzebra/app/panel_plot_data.py b/pyzebra/app/panel_plot_data.py index 9182276..030a55b 100644 --- a/pyzebra/app/panel_plot_data.py +++ b/pyzebra/app/panel_plot_data.py @@ -1,5 +1,6 @@ import base64 import io +import os import numpy as np from bokeh.layouts import column, row @@ -31,6 +32,9 @@ def create(): measured_data_div = Div(text="Measured data:") measured_data = FileInput(accept=".hdf", multiple=True, width=200) + upload_hkl_div = Div(text="Open hkl/mhkl data:") + upload_hkl_fi = FileInput(accept=".hkl,.mhkl", multiple=True, width=200) + def _prepare_plotting(): flag_ub = bool(redef_ub_cb.active) flag_lattice = bool(redef_lattice_cb.active) @@ -129,6 +133,24 @@ def create(): y_c = np.cross(x_c, o_c) hkl_in_plane_y.value = " ".join([f"{val:.1f}" for val in y_c]) + # Prepare hkl/mhkl data + hkl_coord = [] + for j, fname in enumerate(upload_hkl_fi.filename): + with io.StringIO(base64.b64decode(upload_hkl_fi.value[j]).decode()) as file: + _, ext = os.path.splitext(fname) + try: + fdata = pyzebra.parse_hkl(file, ext) + except: + print(f"Error loading {fname}") + return + + for ind in range(len(fdata["counts"])): + # Recognize k_flag_vec + hkl = np.array([fdata["h"][ind], fdata["k"][ind], fdata["k"][ind]]) + + # Save data + hkl_coord.append(hkl) + def _update_slice(): # Where should cut be along orthogonal direction (Mutliplication factor onto orth_dir) orth_cut = hkl_cut.value @@ -196,6 +218,22 @@ def create(): image=[I.T], x=[min_x], dw=[max_x - min_x], y=[min_y], dh=[max_y - min_y] ) + scan_x, scan_y = [], [] + for j in range(len(hkl_coord)): + # Get middle hkl from list + hklm = M @ hkl_coord[j] + + # Decide if point is in the cut + proj = np.dot(hklm, o_c) + if abs(proj - orth_cut) >= delta: + continue + + # Plot middle point of scan + scan_x.append(hklm[0]) + scan_y.append(hklm[1]) + + scatter_source.data.update(x=scan_x, y=scan_y) + return _update_slice def plot_file_callback(): @@ -219,6 +257,9 @@ def create(): image_source = ColumnDataSource(dict(image=[np.zeros((1, 1))], x=[0], y=[0], dw=[1], dh=[1])) plot.image(source=image_source, color_mapper=color_mapper) + scatter_source = ColumnDataSource(dict(x=[], y=[])) + plot.scatter(source=scatter_source, size=4, fill_color="green", line_color="green") + hkl_div = Div(text="HKL:", margin=(5, 5, 0, 5)) hkl_normal = TextInput(title="normal", value="0 0 1", width=70) @@ -312,7 +353,10 @@ def create(): ) cm_layout = row(colormap_select, display_min_ni, display_max_ni) column1_layout = column( - row(measured_data_div, measured_data, plot_file), + row( + column(row(measured_data_div, measured_data), row(upload_hkl_div, upload_hkl_fi)), + plot_file, + ), plot, column( hkl_div, diff --git a/pyzebra/app/plot_hkl.py b/pyzebra/app/plot_hkl.py index 93f8f0f..b85c4d9 100644 --- a/pyzebra/app/plot_hkl.py +++ b/pyzebra/app/plot_hkl.py @@ -35,6 +35,9 @@ class PlotHKL: measured_data_div = Div(text="Measured data:") measured_data = FileInput(accept=".ccl", multiple=True, width=200) + upload_hkl_div = Div(text="Open hkl/mhkl data:") + upload_hkl_fi = FileInput(accept=".hkl,.mhkl", multiple=True, width=200) + min_grid_x = -10 max_grid_x = 10 min_grid_y = -5 @@ -218,6 +221,24 @@ class PlotHKL: x=[x_c[0] / 2, y_c[0] / 2 - 0.1], y=[x_c[1] - 0.1, y_c[1] / 2], text=["h", "k"] ) + # Prepare hkl/mhkl data + hkl_coord2 = [] + for j, fname in enumerate(upload_hkl_fi.filename): + with io.StringIO(base64.b64decode(upload_hkl_fi.value[j]).decode()) as file: + _, ext = os.path.splitext(fname) + try: + fdata = pyzebra.parse_hkl(file, ext) + except: + print(f"Error loading {fname}") + return + + for ind in range(len(fdata["counts"])): + # Recognize k_flag_vec + hkl = np.array([fdata["h"][ind], fdata["k"][ind], fdata["k"][ind]]) + + # Save data + hkl_coord2.append(hkl) + def _update_slice(): cut_tol = hkl_delta.value cut_or = hkl_cut.value @@ -316,6 +337,22 @@ class PlotHKL: plot.legend.items = legend_items + scan_x2, scan_y2 = [], [] + for j in range(len(hkl_coord2)): + # Get middle hkl from list + hklm = M @ hkl_coord2[j] + + # Decide if point is in the cut + proj = np.dot(hklm, o_c) + if abs(proj - cut_or) >= cut_tol: + continue + + # Plot middle point of scan + scan_x2.append(hklm[0]) + scan_y2.append(hklm[1]) + + scatter_source2.data.update(x=scan_x2, y=scan_y2) + return _update_slice def plot_file_callback(): @@ -362,6 +399,9 @@ class PlotHKL: source=scan_source, marker="m", size="s", fill_color="c", line_color="c" ) + scatter_source2 = ColumnDataSource(dict(x=[], y=[])) + plot.scatter(source=scatter_source2, size=4, fill_color="green", line_color="green") + plot.add_layout(Legend(items=[], location="top_left", click_policy="hide")) hkl_div = Div(text="HKL:", margin=(5, 5, 0, 5)) @@ -401,7 +441,10 @@ class PlotHKL: disting_layout = column(disting_opt_div, row(disting_opt_cb, disting_opt_rb)) layout = column( - row(measured_data_div, measured_data, plot_file), + row( + column(row(measured_data_div, measured_data), row(upload_hkl_div, upload_hkl_fi)), + plot_file, + ), plot, row(hkl_layout, k_vectors), row(disting_layout, column(tol_k_ni, res_mult_ni)),