From e72f7f70e209c0bed92e69b77a1bf7ef5d328e96 Mon Sep 17 00:00:00 2001 From: orioncohen Date: Fri, 5 May 2023 11:03:17 -0700 Subject: [PATCH 1/4] small change to structure to also support MoleculeGraphs --- crystal_toolkit/components/structure.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crystal_toolkit/components/structure.py b/crystal_toolkit/components/structure.py index 78812e81..3e350e51 100644 --- a/crystal_toolkit/components/structure.py +++ b/crystal_toolkit/components/structure.py @@ -335,9 +335,11 @@ def update_graph(graph_generation_options, struct_or_mol, current_graph): bonding_strategy_kwargs=bonding_strategy_kwargs, ) + struct_name = "structure" if isinstance(struct_or_mol, StructureGraph) else "molecule" + if ( current_graph - and graph.structure == current_graph.structure + and getattr(graph, struct_name) == getattr(current_graph, struct_name) and graph == current_graph ): raise PreventUpdate From 7d3ba05871a8aa73799d5af5153b1af853d876dc Mon Sep 17 00:00:00 2001 From: orioncohen Date: Fri, 5 May 2023 11:12:58 -0700 Subject: [PATCH 2/4] add vectorized inner loop for get_figure --- crystal_toolkit/components/diffraction.py | 35 ++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/crystal_toolkit/components/diffraction.py b/crystal_toolkit/components/diffraction.py index e86c880c..d1642c4a 100644 --- a/crystal_toolkit/components/diffraction.py +++ b/crystal_toolkit/components/diffraction.py @@ -354,12 +354,39 @@ def get_figure( # optimal number of points per degree determined through usage experiments # scaled to log size to the 4th power - N_density = 150 * (math.log10(grain_size) ** 4) if grain_size > 10 else 150 + n_density = 150 * (math.log10(grain_size) ** 4) if grain_size > 10 else 150 - N = int(N_density * domain) # num total points + n_points = int(n_density * domain) # num total points - x = np.linspace(first, last, N).tolist() - y = np.zeros(len(x)).tolist() + x = np.linspace(first, last + 300, n_points) + y = np.zeros(len(x)) + + if broadening: + x_arr = np.array(x_peak) + y_arr = np.array(y_peak) + + peak_function = getattr(XRayDiffractionComponent, peak_profile) + + for xp, yp, in zip(x_arr, y_arr): + + hwhm = XRayDiffractionComponent.grain_to_hwhm( + grain_size, math.radians(xp / 2), K=float(K), wavelength=rad_source + ) + sigma = hwhm / np.sqrt(2 * np.log(2)) + + center_idx = ((x_arr - first) * n_density).round().astype(int) + half_window = (num_sigma * sigma * n_density).round().astype(int) + + lb = np.maximum(0, center_idx - half_window) + ub = np.minimum(n_points, center_idx + half_window) + + xi = x[lb:ub] + Gi = peak_function(xi, xp, hwhm) + G0 = peak_function(0, 0, hwhm) + y[lb:ub] += yp * Gi / G0 + + x = x.tolist() + y = y.tolist() if broadening: for xp, yp in zip(x_peak, y_peak): From 588bb3fcfa2ced7022a2266dc24b5bf88a11eb39 Mon Sep 17 00:00:00 2001 From: orioncohen Date: Fri, 5 May 2023 11:20:24 -0700 Subject: [PATCH 3/4] remove old functionality --- crystal_toolkit/components/diffraction.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/crystal_toolkit/components/diffraction.py b/crystal_toolkit/components/diffraction.py index b8967c5f..8aaa98f0 100644 --- a/crystal_toolkit/components/diffraction.py +++ b/crystal_toolkit/components/diffraction.py @@ -388,27 +388,6 @@ def get_figure( x = x.tolist() y = y.tolist() - if broadening: - for xp, yp in zip(x_peak, y_peak): - alpha = XRayDiffractionComponent.grain_to_hwhm( - grain_size, math.radians(xp / 2), K=float(K), wavelength=rad_source - ) - sigma = (alpha / np.sqrt(2 * np.log(2))).item() - - center_idx = int(round((xp - first) * N_density)) - # total broadening window of 2 * num_sigma - half_window = int(round(num_sigma * sigma * N_density)) - - lb = max(0, (center_idx - half_window)) - ub = min(N, (center_idx + half_window)) - - G0 = getattr(XRayDiffractionComponent, peak_profile)(0, 0, alpha) - for ii, jj in zip(range(lb, ub), range(lb, ub)): - Gi = getattr(XRayDiffractionComponent, peak_profile)( - x[ii], xp, alpha - ) - y[jj] += yp * Gi / G0 - layout = XRayDiffractionComponent.default_xrd_plot_style if x_axis == "Q": From 85fca9aec692c83e06609cd3d53c384aac4722a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 5 May 2023 18:25:50 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- crystal_toolkit/components/diffraction.py | 6 ++++-- crystal_toolkit/components/structure.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crystal_toolkit/components/diffraction.py b/crystal_toolkit/components/diffraction.py index 8aaa98f0..1c1068d9 100644 --- a/crystal_toolkit/components/diffraction.py +++ b/crystal_toolkit/components/diffraction.py @@ -367,8 +367,10 @@ def get_figure( peak_function = getattr(XRayDiffractionComponent, peak_profile) - for xp, yp, in zip(x_arr, y_arr): - + for ( + xp, + yp, + ) in zip(x_arr, y_arr): hwhm = XRayDiffractionComponent.grain_to_hwhm( grain_size, math.radians(xp / 2), K=float(K), wavelength=rad_source ) diff --git a/crystal_toolkit/components/structure.py b/crystal_toolkit/components/structure.py index 7ff6b9a0..0f795eb8 100644 --- a/crystal_toolkit/components/structure.py +++ b/crystal_toolkit/components/structure.py @@ -336,7 +336,9 @@ def update_graph(graph_generation_options, struct_or_mol, current_graph): bonding_strategy_kwargs=bonding_strategy_kwargs, ) - struct_name = "structure" if isinstance(struct_or_mol, StructureGraph) else "molecule" + struct_name = ( + "structure" if isinstance(struct_or_mol, StructureGraph) else "molecule" + ) if ( current_graph