From 3117d9666ba7863123b065ab3e8ed4033327c9ab Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Mon, 19 Aug 2024 12:54:52 +0200 Subject: [PATCH] Fix pre-commit --- cleedpy/preprocessing.py | 32 ++++++++--------- cleedpy/rfactor.py | 72 ++++++++++++++++++------------------- tests/curves_helper.py | 8 ++--- tests/test_preprocessing.py | 14 ++++---- tests/test_rfactor.py | 10 +++--- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/cleedpy/preprocessing.py b/cleedpy/preprocessing.py index efc1777..b80660b 100644 --- a/cleedpy/preprocessing.py +++ b/cleedpy/preprocessing.py @@ -1,8 +1,6 @@ import numpy as np from scipy.interpolate import CubicHermiteSpline -from cleedpy.rfactor import r2_factor, rp_factor - def lorentzian_smoothing(curve, vi): """ @@ -18,19 +16,21 @@ def lorentzian_smoothing(curve, vi): The result of the convolution: a new array of [e,i] arrays """ - E = curve[:, 0] - I = curve[:, 1] - L = [] + energies = curve[:, 0] + intensities = curve[:, 1] + result = [] - for j in range(E.size): + for j in range(energies.size): c = 0 - L_value = 0 - for i in range(E.size): - c += vi**2 / ((E[i] - E[j]) ** 2 + vi**2) - L_value += I[i] * vi**2 / ((E[i] - E[j]) ** 2 + vi**2) - L.append(L_value / c) + l_value = 0 + for i in range(energies.size): + c += vi**2 / ((energies[i] - energies[j]) ** 2 + vi**2) + l_value += ( + intensities[i] * vi**2 / ((energies[i] - energies[j]) ** 2 + vi**2) + ) + result.append(l_value / c) - return np.array(list(zip(E, L))) + return np.array(list(zip(energies, result))) def preprocessing_loop(theoretical_curves, experimental_curves, shift, r_factor, vi): @@ -97,10 +97,10 @@ def preprocessing_loop(theoretical_curves, experimental_curves, shift, r_factor, # Spline for i in range(len(filtered_experimental_curves)): - E_values = filtered_experimental_curves[i][:, 0] - I_values = filtered_experimental_curves[i][:, 1] - theoretical_E_grid = filtered_theoretical_curves[i][:, 0] - CubicHermiteSpline(E_values, I_values, theoretical_E_grid) + e_values = filtered_experimental_curves[i][:, 0] + i_values = filtered_experimental_curves[i][:, 1] + theoretical_e_grid = filtered_theoretical_curves[i][:, 0] + CubicHermiteSpline(e_values, i_values, theoretical_e_grid) print(globals().get(r_factor)) return diff --git a/cleedpy/rfactor.py b/cleedpy/rfactor.py index 0caa424..8517602 100644 --- a/cleedpy/rfactor.py +++ b/cleedpy/rfactor.py @@ -11,11 +11,11 @@ def r2_factor(theoretical_curve, experimental_curve): - experimental curve: numpy array of [e,i] arrays Design: - R2 = sqrt{ S(It - c*Ie)^2 / S(It - It_avg)^2 } + R2 = sqrt{ S(it - c*ie)^2 / S(it - it_avg)^2 } where: - c = sqrt( S|It|^2 / S|Ie|^ 2) - It_avg = (S It)/ dE + c = sqrt( S|it|^2 / S|ie|^ 2) + it_avg = (S it)/ dE """ # [TODO] (not sure) Use the length of the shortest curve @@ -23,23 +23,23 @@ def r2_factor(theoretical_curve, experimental_curve): theoretical_curve = theoretical_curve[:min_length] experimental_curve = experimental_curve[:min_length] - # Extract the It, Ie values for theoretical and experimental intensity - It = theoretical_curve[:, 1] - Ie = experimental_curve[:, 1] + # Extract the it, ie values for theoretical and experimental intensity + it = theoretical_curve[:, 1] + ie = experimental_curve[:, 1] - # Calculate normalization factor c and It_avg - c = np.sqrt(np.sum(It**2) / np.sum(Ie**2)) - It_avg = np.sum(It) / It.size # dE = number of energy steps + # Calculate normalization factor c and it_avg + c = np.sqrt(np.sum(it**2) / np.sum(ie**2)) + it_avg = np.sum(it) / it.size # dE = number of energy steps # Calculate the numerator and denominator of R2 - numerator = np.sum((It - c * Ie) ** 2) - denominator = np.sum((It - It_avg) ** 2) + numerator = np.sum((it - c * ie) ** 2) + denominator = np.sum((it - it_avg) ** 2) # Calculate R2 - R2 = np.sqrt(numerator / denominator) + r2 = np.sqrt(numerator / denominator) # [TODO] error handling: may return NaN - return R2 + return r2 def rp_factor(theoretical_curve, experimental_curve): @@ -52,39 +52,39 @@ def rp_factor(theoretical_curve, experimental_curve): - experimental curve: numpy array of [e,i] arrays Design: - Rp = S(Ye - Yt)^2 / S(Ye^2 + Yt^2) + Rp = S(ye - yt)^2 / S(ye^2 + yt^2) """ - # Extract the It, Ie values for theoretical and experimental intensity - It = theoretical_curve[:, 1] - Ie = experimental_curve[:, 1] + # Extract the it, ie values for theoretical and experimental intensity + it = theoretical_curve[:, 1] + ie = experimental_curve[:, 1] - # Extract the Et, Ee values for theoretical and experimental energy - Et = theoretical_curve[:, 0] - Ee = experimental_curve[:, 0] + # Extract the et, ee values for theoretical and experimental energy + et = theoretical_curve[:, 0] + ee = experimental_curve[:, 0] # Calculate theoretical and experimental energy steps - step_Et = energy_step(Et) - step_Ee = energy_step(Ee) + step_et = energy_step(et) + step_ee = energy_step(ee) # Calculate Y for theoretical and experimental intensity - Yt = Y_function(It, step_Et) - Ye = Y_function(Ie, step_Ee) + yt = y_function(it, step_et) + ye = y_function(ie, step_ee) # Calculate the numerator and denominator of Rp - numerator = np.sum((Yt - Ye) ** 2 * step_Et) - denominator = np.sum(Ye**2 * step_Ee) + np.sum(Yt**2 * step_Et) + numerator = np.sum((yt - ye) ** 2 * step_et) + denominator = np.sum(ye**2 * step_ee) + np.sum(yt**2 * step_et) # Calculate Rp - Rp = numerator / denominator + rp = numerator / denominator # [TODO] error handling: may return NaN - return Rp + return rp -def Y_function(I, energy_step): +def y_function(intensities, energy_step): """ - Calculates Y for a given curve. + Calculates y for a given curve. Inputs: - I: array of intensity values of the curve @@ -100,13 +100,13 @@ def Y_function(I, energy_step): # [TODO] constants should be defined elsewhere vi = 4 - L = (I[1:] - I[:-1]) / (energy_step * 0.5 * (I[1:] + I[:-1])) - Y = L / (1 + L**2 * vi**2) + y_values = (intensities[1:] - intensities[:-1]) / ( + energy_step * 0.5 * (intensities[1:] + intensities[:-1]) + ) + return y_values / (1 + y_values**2 * vi**2) - return Y - -def energy_step(E): +def energy_step(energies): """ Calculates the pairwise energy step. Returns an array. @@ -117,4 +117,4 @@ def energy_step(E): step = E[i] - E[i-1] """ - return E[1:] - E[:-1] + return energies[1:] - energies[:-1] diff --git a/tests/curves_helper.py b/tests/curves_helper.py index 5ded4f6..6ccf2ca 100644 --- a/tests/curves_helper.py +++ b/tests/curves_helper.py @@ -1,4 +1,4 @@ -def curve_A(): +def curve_a(): return [ [70, 0.0037557780], [74, 0.0013476560], @@ -18,7 +18,7 @@ def curve_A(): ] -def curve_B(): +def curve_b(): return [ [70, 0.0101207100], [74, 0.0100794400], @@ -38,7 +38,7 @@ def curve_B(): ] -def curve_C(): +def curve_c(): return [ [70, 0.0101222800], [74, 0.0100787500], @@ -58,7 +58,7 @@ def curve_C(): ] -def curve_A_smoothed(): +def curve_a_smoothed(): return [ [70, 0.00258495], [74, 0.00182937], diff --git a/tests/test_preprocessing.py b/tests/test_preprocessing.py index ad58b42..402afb2 100644 --- a/tests/test_preprocessing.py +++ b/tests/test_preprocessing.py @@ -5,24 +5,24 @@ import pytest from cleedpy.preprocessing import lorentzian_smoothing, preprocessing_loop -from tests.curves_helper import curve_A, curve_A_smoothed, curve_B +from tests.curves_helper import curve_a, curve_a_smoothed, curve_b @pytest.mark.parametrize( "curve, vi, expected", [ - (np.array(curve_A()), 4, np.array(curve_A_smoothed())), + (np.array(curve_a()), 4, np.array(curve_a_smoothed())), ], ) def test_lorentzian_smoothing(curve, vi, expected): - L_curve = lorentzian_smoothing(curve, vi) + l_curve = lorentzian_smoothing(curve, vi) x_values = curve[:, 0] y_values = curve[:, 1] plt.plot(x_values, y_values, marker="o", color="g", label="Input: Initial curve") - x_values = L_curve[:, 0] - y_values = L_curve[:, 1] + x_values = l_curve[:, 0] + y_values = l_curve[:, 1] plt.plot(x_values, y_values, marker="x", color="r", label="Output: Smoothed curve") y_min = 0 @@ -34,13 +34,13 @@ def test_lorentzian_smoothing(curve, vi, expected): plt.legend() plt.savefig("test_lorentzian_smoothing_output.png") - assert np.allclose(expected, L_curve) + assert np.allclose(expected, l_curve) @pytest.mark.parametrize( "the_curve, exp_curve, shift, r_factor, vi, expected", [ - ([curve_A(), curve_B()], [curve_A(), curve_B()], 1, "r2_factor", 4, 0), + ([curve_a(), curve_b()], [curve_a(), curve_b()], 1, "r2_factor", 4, 0), ], ) def test_preprocessing_loop(the_curve, exp_curve, shift, r_factor, vi, expected): diff --git a/tests/test_rfactor.py b/tests/test_rfactor.py index 4eb005f..e23295d 100644 --- a/tests/test_rfactor.py +++ b/tests/test_rfactor.py @@ -4,14 +4,14 @@ import pytest from cleedpy.rfactor import r2_factor, rp_factor -from tests.curves_helper import curve_A, curve_B, curve_C +from tests.curves_helper import curve_a, curve_b, curve_c @pytest.mark.parametrize( "the_curve, exp_curve, expected_r", [ - (curve_A(), curve_B(), 1.8688830931), - (curve_B(), curve_C(), 0.0001429368), + (curve_a(), curve_b(), 1.8688830931), + (curve_b(), curve_c(), 0.0001429368), ], ) def test_r2_factor(the_curve, exp_curve, expected_r): @@ -23,8 +23,8 @@ def test_r2_factor(the_curve, exp_curve, expected_r): @pytest.mark.parametrize( "the_curve, exp_curve, expected_r", [ - (curve_A(), curve_B(), 1.4898282448), - (curve_B(), curve_C(), 0), + (curve_a(), curve_b(), 1.4898282448), + (curve_b(), curve_c(), 0), ], ) def test_rp_factor(the_curve, exp_curve, expected_r):