diff --git a/napytau/core/chi.py b/napytau/core/chi.py index d702339..3356b8a 100644 --- a/napytau/core/chi.py +++ b/napytau/core/chi.py @@ -2,22 +2,18 @@ from napytau.core.polynomials import ( evaluate_differentiated_polynomial_at_measuring_distances, ) # noqa E501 -from numpy import ndarray -from numpy import sum -from numpy import mean -from numpy import power -from scipy import optimize -from scipy.optimize import OptimizeResult +import numpy as np +import scipy as sp from typing import Tuple def chi_squared_fixed_t( - doppler_shifted_intensities: ndarray, - unshifted_intensities: ndarray, - delta_doppler_shifted_intensities: ndarray, - delta_unshifted_intensities: ndarray, - coefficients: ndarray, - distances: ndarray, + doppler_shifted_intensities: np.ndarray, + unshifted_intensities: np.ndarray, + delta_doppler_shifted_intensities: np.ndarray, + delta_unshifted_intensities: np.ndarray, + coefficients: np.ndarray, + distances: np.ndarray, t_hyp: float, weight_factor: float, ) -> float: @@ -47,14 +43,14 @@ def chi_squared_fixed_t( """ # Compute the difference between Doppler-shifted intensities and polynomial model - shifted_intensity_difference: ndarray = ( + shifted_intensity_difference: np.ndarray = ( doppler_shifted_intensities - evaluate_polynomial_at_measuring_distances(distances, coefficients) ) / delta_doppler_shifted_intensities # Compute the difference between unshifted intensities and # scaled derivative of the polynomial model - unshifted_intensity_difference: ndarray = ( + unshifted_intensity_difference: np.ndarray = ( unshifted_intensities - ( t_hyp @@ -65,24 +61,24 @@ def chi_squared_fixed_t( ) / delta_unshifted_intensities # combine the weighted sum of squared differences - result: float = sum( - (power(shifted_intensity_difference, 2)) - + (weight_factor * (power(unshifted_intensity_difference, 2))) + result: float = np.sum( + (np.power(shifted_intensity_difference, 2)) + + (weight_factor * (np.power(unshifted_intensity_difference, 2))) ) return result def optimize_coefficients( - doppler_shifted_intensities: ndarray, - unshifted_intensities: ndarray, - delta_doppler_shifted_intensities: ndarray, - delta_unshifted_intensities: ndarray, - initial_coefficients: ndarray, - distances: ndarray, + doppler_shifted_intensities: np.ndarray, + unshifted_intensities: np.ndarray, + delta_doppler_shifted_intensities: np.ndarray, + delta_unshifted_intensities: np.ndarray, + initial_coefficients: np.ndarray, + distances: np.ndarray, t_hyp: float, weight_factor: float, -) -> Tuple[ndarray, float]: +) -> Tuple[np.ndarray, float]: """ Optimizes the polynomial coefficients to minimize the chi-squared function. @@ -118,7 +114,7 @@ def optimize_coefficients( weight_factor, ) - result: OptimizeResult = optimize.minimize( + result: sp.optimize.OptimizeResult = sp.optimize.minimize( chi_squared, initial_coefficients, # Optimization method for bounded optimization. It minimizes a scalar function @@ -132,12 +128,12 @@ def optimize_coefficients( def optimize_t_hyp( - doppler_shifted_intensities: ndarray, - unshifted_intensities: ndarray, - delta_doppler_shifted_intensities: ndarray, - delta_unshifted_intensities: ndarray, - initial_coefficients: ndarray, - distances: ndarray, + doppler_shifted_intensities: np.ndarray, + unshifted_intensities: np.ndarray, + delta_doppler_shifted_intensities: np.ndarray, + delta_unshifted_intensities: np.ndarray, + initial_coefficients: np.ndarray, + distances: np.ndarray, t_hyp_range: Tuple[float, float], weight_factor: float, ) -> float: @@ -177,11 +173,11 @@ def optimize_t_hyp( weight_factor, )[1] - result: OptimizeResult = optimize.minimize( + result: sp.optimize.OptimizeResult = sp.optimize.minimize( chi_squared_t_hyp, # Initial guess for t_hyp. Startíng with the mean reduces likelihood of # biasing the optimization process toward one boundary. - x0=mean(t_hyp_range), + x0=np.mean(t_hyp_range), bounds=[(t_hyp_range[0], t_hyp_range[1])], ) diff --git a/napytau/core/core.py b/napytau/core/core.py index d21ef1f..d8c2a56 100644 --- a/napytau/core/core.py +++ b/napytau/core/core.py @@ -1,10 +1,10 @@ -import numpy as np from napytau.core.chi import optimize_t_hyp from napytau.core.chi import optimize_coefficients from napytau.core.tau import calculate_tau_i_values from napytau.core.delta_tau import calculate_error_propagation_terms from napytau.core.tau_final import calculate_tau_final from typing import Tuple, Optional +import numpy as np def calculate_lifetime( diff --git a/napytau/core/delta_tau.py b/napytau/core/delta_tau.py index 697b685..9e2343a 100644 --- a/napytau/core/delta_tau.py +++ b/napytau/core/delta_tau.py @@ -1,16 +1,13 @@ from napytau.core.polynomials import ( evaluate_differentiated_polynomial_at_measuring_distances, -) # noqa E501 +) from napytau.core.polynomials import evaluate_polynomial_at_measuring_distances -from numpy import array -from numpy import ndarray -from numpy import zeros -from numpy import diag -from numpy import power -from numpy import linalg +import numpy as np -def calculate_jacobian_matrix(distances: ndarray, coefficients: ndarray) -> ndarray: +def calculate_jacobian_matrix( + distances: np.ndarray, coefficients: np.ndarray +) -> np.ndarray: """ calculated the jacobian matrix for a set of polynomial coefficients taking different distances into account. @@ -26,20 +23,20 @@ def calculate_jacobian_matrix(distances: ndarray, coefficients: ndarray) -> ndar """ # initializes the jacobian matrix - jacobian_matrix: ndarray = zeros((len(distances), len(coefficients))) + jacobian_matrix: np.ndarray = np.zeros((len(distances), len(coefficients))) epsilon: float = 1e-8 # small disturbance value # Loop over each coefficient and calculate the partial derivative for i in range(len(coefficients)): - perturbed_coefficients: ndarray = array(coefficients, dtype=float) + perturbed_coefficients: np.ndarray = np.array(coefficients, dtype=float) perturbed_coefficients[i] += epsilon # slightly disturb the current coefficient # Compute the disturbed and original polynomial values at the given distances - perturbed_function: ndarray = evaluate_polynomial_at_measuring_distances( + perturbed_function: np.ndarray = evaluate_polynomial_at_measuring_distances( distances, perturbed_coefficients ) - original_function: ndarray = evaluate_polynomial_at_measuring_distances( + original_function: np.ndarray = evaluate_polynomial_at_measuring_distances( distances, coefficients ) @@ -53,8 +50,10 @@ def calculate_jacobian_matrix(distances: ndarray, coefficients: ndarray) -> ndar def calculate_covariance_matrix( - delta_shifted_intensities: ndarray, distances: ndarray, coefficients: ndarray -) -> ndarray: + delta_shifted_intensities: np.ndarray, + distances: np.ndarray, + coefficients: np.ndarray, +) -> np.ndarray: """ Computes the covariance matrix for the polynomial coefficients using the jacobian matrix and a weight matrix derived from the shifted intensities' errors. @@ -67,26 +66,26 @@ def calculate_covariance_matrix( ndarray: The computed covariance matrix for the polynomial coefficients. """ - jacobian_matrix: ndarray = calculate_jacobian_matrix(distances, coefficients) + jacobian_matrix: np.ndarray = calculate_jacobian_matrix(distances, coefficients) # Construct the weight matrix from the inverse squared errors - weight_matrix: ndarray = diag(1 / power(delta_shifted_intensities, 2)) + weight_matrix: np.ndarray = np.diag(1 / np.power(delta_shifted_intensities, 2)) - fit_matrix: ndarray = jacobian_matrix.T @ weight_matrix @ jacobian_matrix + fit_matrix: np.ndarray = jacobian_matrix.T @ weight_matrix @ jacobian_matrix - covariance_matrix: ndarray = linalg.inv(fit_matrix) + covariance_matrix: np.ndarray = np.linalg.inv(fit_matrix) return covariance_matrix def calculate_error_propagation_terms( - unshifted_intensities: ndarray, - delta_shifted_intensities: ndarray, - delta_unshifted_intensities: ndarray, - distances: ndarray, - coefficients: ndarray, + unshifted_intensities: np.ndarray, + delta_shifted_intensities: np.ndarray, + delta_unshifted_intensities: np.ndarray, + distances: np.ndarray, + coefficients: np.ndarray, taufactor: float, -) -> ndarray: +) -> np.ndarray: """ creates the error propagation term for the polynomial coefficients. combining direct errors, polynomial uncertainties, and mixed covariance terms. @@ -109,16 +108,16 @@ def calculate_error_propagation_terms( ) ) - gaussian_error_from_unshifted_intensity: ndarray = power( + gaussian_error_from_unshifted_intensity: np.ndarray = np.power( delta_unshifted_intensities, 2 - ) / power( + ) / np.power( calculated_differentiated_polynomial_sum_at_measuring_distances, 2, ) # Initialize the polynomial uncertainty term for second term - delta_p_j_i_squared: ndarray = zeros(len(distances)) - covariance_matrix: ndarray = calculate_covariance_matrix( + delta_p_j_i_squared: np.ndarray = np.zeros(len(distances)) + covariance_matrix: np.ndarray = calculate_covariance_matrix( delta_shifted_intensities, distances, coefficients ) @@ -127,25 +126,27 @@ def calculate_error_propagation_terms( for l in range(len(coefficients)): # noqa E741 delta_p_j_i_squared = ( delta_p_j_i_squared - + power(distances, k) * power(distances, l) * covariance_matrix[k, l] + + np.power(distances, k) + * np.power(distances, l) + * covariance_matrix[k, l] ) - gaussian_error_from_polynomial_uncertainties: ndarray = ( - power(unshifted_intensities, 2) - / power( + gaussian_error_from_polynomial_uncertainties: np.ndarray = ( + np.power(unshifted_intensities, 2) + / np.power( calculated_differentiated_polynomial_sum_at_measuring_distances, 4, ) - ) * power(delta_p_j_i_squared, 2) + ) * np.power(delta_p_j_i_squared, 2) - error_from_covariance: ndarray = ( + error_from_covariance: np.ndarray = ( unshifted_intensities * taufactor * delta_p_j_i_squared - ) / power(calculated_differentiated_polynomial_sum_at_measuring_distances, 3) + ) / np.power(calculated_differentiated_polynomial_sum_at_measuring_distances, 3) - interim_result: ndarray = ( + interim_result: np.ndarray = ( gaussian_error_from_unshifted_intensity + gaussian_error_from_polynomial_uncertainties ) - errors: ndarray = interim_result + error_from_covariance + errors: np.ndarray = interim_result + error_from_covariance # Return the sum of all three contributions return errors diff --git a/napytau/core/polynomials.py b/napytau/core/polynomials.py index 70bbecb..998bc2e 100644 --- a/napytau/core/polynomials.py +++ b/napytau/core/polynomials.py @@ -1,15 +1,12 @@ -from numpy import ndarray -from numpy import power -from numpy import zeros_like - from napytau.core.errors.polynomial_coefficient_error import ( PolynomialCoefficientError, ) +import numpy as np def evaluate_polynomial_at_measuring_distances( - distances: ndarray, coefficients: ndarray -) -> ndarray: + distances: np.ndarray, coefficients: np.ndarray +) -> np.ndarray: """ Computes the sum of a polynomial evaluated at given distance points. @@ -29,16 +26,16 @@ def evaluate_polynomial_at_measuring_distances( ) # Evaluate the polynomial sum at the given time points - sum_at_measuring_distances: ndarray = zeros_like(distances, dtype=float) + sum_at_measuring_distances: np.ndarray = np.zeros_like(distances, dtype=float) for exponent, coefficient in enumerate(coefficients): - sum_at_measuring_distances += coefficient * power(distances, exponent) + sum_at_measuring_distances += coefficient * np.power(distances, exponent) return sum_at_measuring_distances def evaluate_differentiated_polynomial_at_measuring_distances( - distances: ndarray, coefficients: ndarray -) -> ndarray: + distances: np.ndarray, coefficients: np.ndarray +) -> np.ndarray: """ Computes the sum of the derivative of a polynomial evaluated at given distance points. @@ -59,13 +56,13 @@ def evaluate_differentiated_polynomial_at_measuring_distances( "An empty array of coefficients can not be evaluated." ) - sum_of_derivative_at_measuring_distances: ndarray = zeros_like( + sum_of_derivative_at_measuring_distances: np.ndarray = np.zeros_like( distances, dtype=float ) for exponent, coefficient in enumerate(coefficients): if exponent > 0: sum_of_derivative_at_measuring_distances += ( - exponent * coefficient * power(distances, (exponent - 1)) + exponent * coefficient * np.power(distances, (exponent - 1)) ) return sum_of_derivative_at_measuring_distances diff --git a/napytau/core/tau.py b/napytau/core/tau.py index 80a54a6..6fef40d 100644 --- a/napytau/core/tau.py +++ b/napytau/core/tau.py @@ -3,21 +3,21 @@ from napytau.core.polynomials import ( evaluate_differentiated_polynomial_at_measuring_distances, ) # noqa E501 -from numpy import ndarray +import numpy as np from typing import Tuple, Optional def calculate_tau_i_values( - doppler_shifted_intensities: ndarray, - unshifted_intensities: ndarray, - delta_doppler_shifted_intensities: ndarray, - delta_unshifted_intensities: ndarray, - initial_coefficients: ndarray, - distances: ndarray, + doppler_shifted_intensities: np.ndarray, + unshifted_intensities: np.ndarray, + delta_doppler_shifted_intensities: np.ndarray, + delta_unshifted_intensities: np.ndarray, + initial_coefficients: np.ndarray, + distances: np.ndarray, t_hyp_range: Tuple[float, float], weight_factor: float, custom_t_hyp_estimate: Optional[float], -) -> ndarray: +) -> np.ndarray: """ Calculates the decay times (tau_i) based on the provided intensities and time points. @@ -61,7 +61,7 @@ def calculate_tau_i_values( ) # optimize the polynomial coefficients with the optimized t_hyp - optimized_coefficients: ndarray = ( + optimized_coefficients: np.ndarray = ( optimize_coefficients( doppler_shifted_intensities, unshifted_intensities, @@ -75,7 +75,7 @@ def calculate_tau_i_values( )[0] # calculate decay times using the optimized coefficients - tau_i_values: ndarray = ( + tau_i_values: np.ndarray = ( unshifted_intensities / evaluate_differentiated_polynomial_at_measuring_distances( distances, optimized_coefficients diff --git a/napytau/core/tau_final.py b/napytau/core/tau_final.py index 72a111b..53109c4 100644 --- a/napytau/core/tau_final.py +++ b/napytau/core/tau_final.py @@ -1,12 +1,9 @@ -from numpy import ndarray -from numpy import power -from numpy import sum -from numpy import sqrt +import numpy as np from typing import Tuple def calculate_tau_final( - tau_i_values: ndarray, delta_tau_i_values: ndarray + tau_i_values: np.ndarray, delta_tau_i_values: np.ndarray ) -> Tuple[float, float]: """ Computes the final decay time (tau_final) and its associated uncertainty @@ -24,12 +21,12 @@ def calculate_tau_final( if len(tau_i_values) == 0: return -1, -1 - weights: ndarray = 1 / power(delta_tau_i_values, 2) + weights: np.ndarray = 1 / np.power(delta_tau_i_values, 2) # Calculate the weighted mean of tau_i - weighted_mean: float = sum(weights * tau_i_values) / sum(weights) + weighted_mean: float = np.sum(weights * tau_i_values) / np.sum(weights) # Calculate the uncertainty of the weighted mean - uncertainty: float = sqrt(1 / sum(weights)) + uncertainty: float = np.sqrt(1 / np.sum(weights)) return weighted_mean, uncertainty diff --git a/tests/core/chi_test.py b/tests/core/chi_test.py index bd31d77..60b47b1 100644 --- a/tests/core/chi_test.py +++ b/tests/core/chi_test.py @@ -1,9 +1,8 @@ import unittest from unittest.mock import MagicMock, patch -from numpy import array -from numpy import ndarray -from numpy import testing -from scipy.optimize import OptimizeResult +import numpy as np +import numpy.testing as nptest +import scipy as sp from typing import Tuple @@ -29,16 +28,16 @@ def test_CanCalculateChiForValidData(self): polynomials_mock, numpy_module_mock, scipy_optimize_module_mock = set_up_mocks() polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([5, 15, 57]) + np.array([5, 15, 57]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [4, 20, 72] ) numpy_module_mock.sum.return_value = 628.3486168 numpy_module_mock.power.side_effect = [ - array([4, 18.77777778, 182.25]), - array([0.64, 34.02777778, 388.65306122]), + np.array([4, 18.77777778, 182.25]), + np.array([0.64, 34.02777778, 388.65306122]), ] with patch.dict( @@ -50,12 +49,12 @@ def test_CanCalculateChiForValidData(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([1, 2, 3]) - unshifted_intensities: ndarray = array([4, 5, 6]) - delta_doppler_shifted_intensities: ndarray = array([2, 3, 4]) - delta_unshifted_intensities: ndarray = array([5, 6, 7]) - coefficients: ndarray = array([5, 4, 3, 2, 1]) - distances: ndarray = array([0, 1, 2]) + doppler_shifted_intensities: np.ndarray = np.array([1, 2, 3]) + unshifted_intensities: np.ndarray = np.array([4, 5, 6]) + delta_doppler_shifted_intensities: np.ndarray = np.array([2, 3, 4]) + delta_unshifted_intensities: np.ndarray = np.array([5, 6, 7]) + coefficients: np.ndarray = np.array([5, 4, 3, 2, 1]) + distances: np.ndarray = np.array([0, 1, 2]) t_hyp: float = 2.0 weight_factor: float = 1.0 @@ -82,18 +81,18 @@ def test_CanCalculateChiForValidData(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1, 2])), + (np.array([0, 1, 2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -103,18 +102,18 @@ def test_CanCalculateChiForValidData(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1, 2])), + (np.array([0, 1, 2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -122,9 +121,9 @@ def test_CanCalculateChiForValidData(self): 1, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.sum.mock_calls[0].args[0], - (array([4.64, 52.80555556, 570.90306122])), + (np.array([4.64, 52.80555556, 570.90306122])), ) self.assertEqual( @@ -132,22 +131,22 @@ def test_CanCalculateChiForValidData(self): 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([-2, -4.33333333, -13.5])), + (np.array([-2, -4.33333333, -13.5])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([-0.8, -5.83333333, -19.71428571])), + (np.array([-0.8, -5.83333333, -19.71428571])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -158,14 +157,14 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): # Mocked return values of called functions polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([]) + np.array([]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [] ) numpy_module_mock.sum.return_value = 0 - numpy_module_mock.power.side_effect = [array([]), array([])] + numpy_module_mock.power.side_effect = [np.array([]), np.array([])] with patch.dict( "sys.modules", @@ -176,12 +175,12 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([]) - unshifted_intensities: ndarray = array([]) - delta_doppler_shifted_intensities: ndarray = array([]) - delta_unshifted_intensities: ndarray = array([]) - coefficients: ndarray = array([]) - distances: ndarray = array([]) + doppler_shifted_intensities: np.ndarray = np.array([]) + unshifted_intensities: np.ndarray = np.array([]) + delta_doppler_shifted_intensities: np.ndarray = np.array([]) + delta_unshifted_intensities: np.ndarray = np.array([]) + coefficients: np.ndarray = np.array([]) + distances: np.ndarray = np.array([]) t_hyp: float = 2.0 weight_factor: float = 1.0 @@ -208,18 +207,18 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([])), + (np.array([])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([])), + (np.array([])), ) self.assertEqual( @@ -229,18 +228,18 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([])), + (np.array([])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([])), + (np.array([])), ) self.assertEqual( @@ -248,9 +247,9 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.sum.mock_calls[0].args[0], - array([]), + np.array([]), ) self.assertEqual( @@ -258,22 +257,22 @@ def test_CanHandleEmptyDataArraysForChiCalculation(self): 2, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([])), + (np.array([])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([])), + (np.array([])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -284,14 +283,17 @@ def test_CanCalculateChiForASingleDatapoint(self): # Mocked return values of called functions polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([57]) + np.array([57]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [72] ) numpy_module_mock.sum.return_value = 1608.69444444 - numpy_module_mock.power.side_effect = [array([348.44444444]), array([1260.25])] + numpy_module_mock.power.side_effect = [ + np.array([348.44444444]), + np.array([1260.25]), + ] with patch.dict( "sys.modules", @@ -302,12 +304,12 @@ def test_CanCalculateChiForASingleDatapoint(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([1]) - unshifted_intensities: ndarray = array([2]) - delta_doppler_shifted_intensities: ndarray = array([3]) - delta_unshifted_intensities: ndarray = array([4]) - coefficients: ndarray = array([5, 4, 3, 2, 1]) - distances: ndarray = array([2]) + doppler_shifted_intensities: np.ndarray = np.array([1]) + unshifted_intensities: np.ndarray = np.array([2]) + delta_doppler_shifted_intensities: np.ndarray = np.array([3]) + delta_unshifted_intensities: np.ndarray = np.array([4]) + coefficients: np.ndarray = np.array([5, 4, 3, 2, 1]) + distances: np.ndarray = np.array([2]) t_hyp: float = 2.0 weight_factor: float = 1.0 @@ -334,18 +336,18 @@ def test_CanCalculateChiForASingleDatapoint(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([2])), + (np.array([2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -355,18 +357,18 @@ def test_CanCalculateChiForASingleDatapoint(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([2])), + (np.array([2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -374,9 +376,9 @@ def test_CanCalculateChiForASingleDatapoint(self): 1, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.sum.mock_calls[0].args[0], - (array([1608.69444444])), + (np.array([1608.69444444])), ) self.assertEqual( @@ -384,22 +386,22 @@ def test_CanCalculateChiForASingleDatapoint(self): 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([-18.66666667])), + (np.array([-18.66666667])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([-35.5])), + (np.array([-35.5])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -410,16 +412,16 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): # Mocked return values of called functions polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([5, 15]) + np.array([5, 15]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [4, 20] ) numpy_module_mock.sum.return_value = float("inf") numpy_module_mock.power.side_effect = [ - array([float("inf"), 169]), - array([float("inf"), 1296]), + np.array([float("inf"), 169]), + np.array([float("inf"), 1296]), ] with patch.dict( @@ -431,12 +433,12 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([1, 2]) - unshifted_intensities: ndarray = array([3, 4]) - delta_doppler_shifted_intensities: ndarray = array([0, 1]) - delta_unshifted_intensities: ndarray = array([0, 1]) - coefficients: ndarray = array([5, 4, 3, 2, 1]) - distances: ndarray = array([0, 1]) + doppler_shifted_intensities: np.ndarray = np.array([1, 2]) + unshifted_intensities: np.ndarray = np.array([3, 4]) + delta_doppler_shifted_intensities: np.ndarray = np.array([0, 1]) + delta_unshifted_intensities: np.ndarray = np.array([0, 1]) + coefficients: np.ndarray = np.array([5, 4, 3, 2, 1]) + distances: np.ndarray = np.array([0, 1]) t_hyp: float = 2.0 weight_factor: float = 1.0 @@ -463,18 +465,18 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1])), + (np.array([0, 1])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -484,18 +486,18 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1])), + (np.array([0, 1])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -503,9 +505,9 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.sum.mock_calls[0].args[0], - (array([float("inf"), 1465])), + (np.array([float("inf"), 1465])), ) self.assertEqual( @@ -513,22 +515,22 @@ def test_CanCalculateChiIfTheDenominatorIsZero(self): 2, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([-float("inf"), -13])), + (np.array([-float("inf"), -13])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([-float("inf"), -36])), + (np.array([-float("inf"), -36])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -539,16 +541,16 @@ def test_CanCalculateChiForNegativeValues(self): # Mocked return values of called functions polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([-5, -5]) + np.array([-5, -5]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [-4, -4] ) numpy_module_mock.sum.return_value = 22.02777778 numpy_module_mock.power.side_effect = [ - array([16, 2.25]), - array([2.77777778, 1]), + np.array([16, 2.25]), + np.array([2.77777778, 1]), ] with patch.dict( @@ -560,12 +562,12 @@ def test_CanCalculateChiForNegativeValues(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([-1, -2]) - unshifted_intensities: ndarray = array([-3, -4]) - delta_doppler_shifted_intensities: ndarray = array([1, 2]) - delta_unshifted_intensities: ndarray = array([3, 4]) - coefficients: ndarray = array([-5, -4, 3, 2, -1]) - distances: ndarray = array([0, 1]) + doppler_shifted_intensities: np.ndarray = np.array([-1, -2]) + unshifted_intensities: np.ndarray = np.array([-3, -4]) + delta_doppler_shifted_intensities: np.ndarray = np.array([1, 2]) + delta_unshifted_intensities: np.ndarray = np.array([3, 4]) + coefficients: np.ndarray = np.array([-5, -4, 3, 2, -1]) + distances: np.ndarray = np.array([0, 1]) t_hyp: float = 2.0 weight_factor: float = 1.0 @@ -592,18 +594,18 @@ def test_CanCalculateChiForNegativeValues(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1])), + (np.array([0, 1])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([-5, -4, 3, 2, -1])), + (np.array([-5, -4, 3, 2, -1])), ) self.assertEqual( @@ -613,18 +615,18 @@ def test_CanCalculateChiForNegativeValues(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1])), + (np.array([0, 1])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([-5, -4, 3, 2, -1])), + (np.array([-5, -4, 3, 2, -1])), ) self.assertEqual( @@ -632,9 +634,9 @@ def test_CanCalculateChiForNegativeValues(self): 1, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.sum.mock_calls[0].args[0], - (array([18.77777778, 3.25])), + (np.array([18.77777778, 3.25])), ) self.assertEqual( @@ -642,22 +644,22 @@ def test_CanCalculateChiForNegativeValues(self): 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([4, 1.5])), + (np.array([4, 1.5])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([1.66666667, 1])), + (np.array([1.66666667, 1])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -668,16 +670,16 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): # Mocked return values of called functions polynomials_mock.evaluate_polynomial_at_measuring_distances.return_value = ( - array([5, 15, 57]) + np.array([5, 15, 57]) ) - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value = np.array( [4, 20, 72] ) numpy_module_mock.sum.return_value = 205.02777778 numpy_module_mock.power.side_effect = [ - array([4, 18.77777778, 182.25]), - array([0.64, 34.02777778, 388.65306122]), + np.array([4, 18.77777778, 182.25]), + np.array([0.64, 34.02777778, 388.65306122]), ] with patch.dict( @@ -689,12 +691,12 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): ): from napytau.core.chi import chi_squared_fixed_t - doppler_shifted_intensities: ndarray = array([1, 2, 3]) - unshifted_intensities: ndarray = array([4, 5, 6]) - delta_doppler_shifted_intensities: ndarray = array([2, 3, 4]) - delta_unshifted_intensities: ndarray = array([5, 6, 7]) - coefficients: ndarray = array([5, 4, 3, 2, 1]) - distances: ndarray = array([0, 1, 2]) + doppler_shifted_intensities: np.ndarray = np.array([1, 2, 3]) + unshifted_intensities: np.ndarray = np.array([4, 5, 6]) + delta_doppler_shifted_intensities: np.ndarray = np.array([2, 3, 4]) + delta_unshifted_intensities: np.ndarray = np.array([5, 6, 7]) + coefficients: np.ndarray = np.array([5, 4, 3, 2, 1]) + distances: np.ndarray = np.array([0, 1, 2]) t_hyp: float = 2.0 weight_factor: float = 0.0 @@ -721,18 +723,18 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1, 2])), + (np.array([0, 1, 2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -742,18 +744,18 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1, 2])), + (np.array([0, 1, 2])), ) - testing.assert_array_equal( + nptest.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([5, 4, 3, 2, 1])), + (np.array([5, 4, 3, 2, 1])), ) self.assertEqual( @@ -761,9 +763,9 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): 1, ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.sum.mock_calls[0].args[0], - (array([4, 18.77777778, 182.25])), + (np.array([4, 18.77777778, 182.25])), ) self.assertEqual( @@ -771,22 +773,22 @@ def test_CanCalculateChiForAWeightFactorOfZero(self): 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[0].args[0], - (array([-2, -4.33333333, -13.5])), + (np.array([-2, -4.33333333, -13.5])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[0].args[1], 2, ) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( numpy_module_mock.power.mock_calls[1].args[0], - (array([-0.8, -5.83333333, -19.71428571])), + (np.array([-0.8, -5.83333333, -19.71428571])), ) - testing.assert_array_equal( + nptest.assert_array_equal( numpy_module_mock.power.mock_calls[1].args[1], 2, ) @@ -796,8 +798,8 @@ def test_CanOptimizePolynomialCoefficients(self): polynomials_mock, numpy_module_mock, scipy_optimize_module_mock = set_up_mocks() # Mocked return value of called function - scipy_optimize_module_mock.optimize.minimize.return_value = OptimizeResult( - x=[2, 3, 1], fun=0.0 + scipy_optimize_module_mock.optimize.minimize.return_value = ( + sp.optimize.OptimizeResult(x=[2, 3, 1], fun=0.0) ) with patch.dict( @@ -808,19 +810,19 @@ def test_CanOptimizePolynomialCoefficients(self): ): from napytau.core.chi import optimize_coefficients - doppler_shifted_intensities: ndarray = array([2, 6]) - unshifted_intensities: ndarray = array([6, 10]) - delta_doppler_shifted_intensities: ndarray = array([1, 1]) - delta_unshifted_intensities: ndarray = array([1, 1]) - initial_coefficients: ndarray = array([1, 1, 1]) - distances: ndarray = array([0, 1]) + doppler_shifted_intensities: np.ndarray = np.array([2, 6]) + unshifted_intensities: np.ndarray = np.array([6, 10]) + delta_doppler_shifted_intensities: np.ndarray = np.array([1, 1]) + delta_unshifted_intensities: np.ndarray = np.array([1, 1]) + initial_coefficients: np.ndarray = np.array([1, 1, 1]) + distances: np.ndarray = np.array([0, 1]) t_hyp: float = 2.0 weight_factor: float = 1.0 expected_chi: float = 0.0 - expected_coefficients: ndarray = array([2, 3, 1]) + expected_coefficients: np.ndarray = np.array([2, 3, 1]) - actual_coefficients: ndarray + actual_coefficients: np.ndarray actual_chi: float actual_coefficients, actual_chi = optimize_coefficients( doppler_shifted_intensities, @@ -834,7 +836,7 @@ def test_CanOptimizePolynomialCoefficients(self): ) self.assertAlmostEqual(actual_chi, expected_chi) - testing.assert_array_almost_equal( + nptest.assert_array_almost_equal( actual_coefficients, expected_coefficients ) @@ -842,9 +844,9 @@ def test_CanOptimizePolynomialCoefficients(self): len(scipy_optimize_module_mock.optimize.minimize.mock_calls), 1 ) - testing.assert_array_equal( + nptest.assert_array_equal( scipy_optimize_module_mock.optimize.minimize.mock_calls[0].args[1], - array([1, 1, 1]), + np.array([1, 1, 1]), ) self.assertEqual( @@ -859,8 +861,8 @@ def test_CanOptimizeTHypValue(self): polynomials_mock, numpy_module_mock, scipy_optimize_module_mock = set_up_mocks() # Mocked return values of called functions - scipy_optimize_module_mock.optimize.minimize.return_value = OptimizeResult( - x=2.0 + scipy_optimize_module_mock.optimize.minimize.return_value = ( + sp.optimize.OptimizeResult(x=2.0) ) numpy_module_mock.mean.return_value = 0 @@ -874,12 +876,12 @@ def test_CanOptimizeTHypValue(self): ): from napytau.core.chi import optimize_t_hyp - doppler_shifted_intensities: ndarray = array([2, 6]) - unshifted_intensities: ndarray = array([6, 10]) - delta_doppler_shifted_intensities: ndarray = array([1, 1]) - delta_unshifted_intensities: ndarray = array([1, 1]) - initial_coefficients: ndarray = array([1, 1, 1]) - distances: ndarray = array([0, 1]) + doppler_shifted_intensities: np.ndarray = np.array([2, 6]) + unshifted_intensities: np.ndarray = np.array([6, 10]) + delta_doppler_shifted_intensities: np.ndarray = np.array([1, 1]) + delta_unshifted_intensities: np.ndarray = np.array([1, 1]) + initial_coefficients: np.ndarray = np.array([1, 1, 1]) + distances: np.ndarray = np.array([0, 1]) t_hyp_range: Tuple[float, float] = (-5, 5) weight_factor: float = 1.0 diff --git a/tests/core/polynomials_test.py b/tests/core/polynomials_test.py index 0b8269b..db11d1f 100644 --- a/tests/core/polynomials_test.py +++ b/tests/core/polynomials_test.py @@ -1,13 +1,9 @@ import unittest from unittest.mock import MagicMock, patch - -from numpy import ndarray -from numpy import array -from numpy import testing - from napytau.core.errors.polynomial_coefficient_error import ( PolynomialCoefficientError, ) +import numpy as np def set_up_mocks() -> MagicMock: @@ -26,11 +22,11 @@ def test_CanEvaluateAValidPolynomialAtMeasuringDistances(): # Mocked return values of called functions numpy_module_mock.power.side_effect = [ - array([1, 1, 1]), - array([1, 2, 3]), - array([1, 4, 9]), + np.array([1, 1, 1]), + np.array([1, 2, 3]), + np.array([1, 4, 9]), ] - numpy_module_mock.zeros_like.return_value = array([0, 0, 0]) + numpy_module_mock.zeros_like.return_value = np.array([0, 0, 0]) with patch.dict( "sys.modules", @@ -43,13 +39,13 @@ def test_CanEvaluateAValidPolynomialAtMeasuringDistances(): ) # Test for a simple quadratic polynomial: 2 + 3x + 4x^2 - distances: ndarray = array([1, 2, 3]) - coefficients: ndarray = array([2, 3, 4]) + distances: np.ndarray = np.array([1, 2, 3]) + coefficients: np.ndarray = np.array([2, 3, 4]) # At x = 1: 2 + 3(1) + 4(1^2) = 9 # At x = 2: 2 + 3(2) + 4(2^2) = 2 + 6 + 16 = 24 # At x = 3: 2 + 3(3) + 4(3^2) = 2 + 9 + 36 = 47 - expected_result: ndarray = array([9, 24, 47]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([9, 24, 47]) + np.testing.assert_array_equal( evaluate_polynomial_at_measuring_distances(distances, coefficients), expected_result, ) @@ -60,8 +56,8 @@ def test_CanEvaluateAPolynomialAtMeasuringDistancesForEmptyDistanceInput(): numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([]), array([]), array([])] - numpy_module_mock.zeros_like.return_value = array([]) + numpy_module_mock.power.side_effect = [np.array([]), np.array([]), np.array([])] + numpy_module_mock.zeros_like.return_value = np.array([]) with patch.dict( "sys.modules", @@ -73,11 +69,11 @@ def test_CanEvaluateAPolynomialAtMeasuringDistancesForEmptyDistanceInput(): evaluate_polynomial_at_measuring_distances, ) - distances: ndarray = array([]) - coefficients: ndarray = array([2, 3, 4]) + distances: np.ndarray = np.array([]) + coefficients: np.ndarray = np.array([2, 3, 4]) # With an empty input array, the result should also be an empty array - expected_result: ndarray = array([]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([]) + np.testing.assert_array_equal( evaluate_polynomial_at_measuring_distances(distances, coefficients), expected_result, ) @@ -88,8 +84,12 @@ def test_CanEvaluateAPolynomialAtMeasuringDistancesForASingleDistance(): numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([1]), array([2]), array([4])] - numpy_module_mock.zeros_like.return_value = array([0]) + numpy_module_mock.power.side_effect = [ + np.array([1]), + np.array([2]), + np.array([4]), + ] + numpy_module_mock.zeros_like.return_value = np.array([0]) with patch.dict( "sys.modules", @@ -101,12 +101,12 @@ def test_CanEvaluateAPolynomialAtMeasuringDistancesForASingleDistance(): evaluate_polynomial_at_measuring_distances, ) - distances: ndarray = array([2]) - coefficients: ndarray = array([1, 2]) + distances: np.ndarray = np.array([2]) + coefficients: np.ndarray = np.array([1, 2]) # Polynomial: f(x) = 1 + 2x # At x = 2: 1 + 2(2) = 5 - expected_result: ndarray = array([5]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([5]) + np.testing.assert_array_equal( evaluate_polynomial_at_measuring_distances(distances, coefficients), expected_result, ) @@ -118,11 +118,11 @@ def test_CanEvaluateAPolynomialOfDegreeZeroAtMeasuringDistances(): # Mocked return values of called functions numpy_module_mock.power.side_effect = [ - array([1, 1, 1]), - array([1, 2, 3]), - array([1, 4, 9]), + np.array([1, 1, 1]), + np.array([1, 2, 3]), + np.array([1, 4, 9]), ] - numpy_module_mock.zeros_like.return_value = array([0, 0, 0]) + numpy_module_mock.zeros_like.return_value = np.array([0, 0, 0]) with patch.dict( "sys.modules", @@ -134,12 +134,12 @@ def test_CanEvaluateAPolynomialOfDegreeZeroAtMeasuringDistances(): evaluate_polynomial_at_measuring_distances, ) - distances: ndarray = array([1, 2, 3]) - coefficients: ndarray = array([5]) + distances: np.ndarray = np.array([1, 2, 3]) + coefficients: np.ndarray = np.array([5]) # Constant polynomial: f(x) = 5 # All values should be 5 - expected_result: ndarray = array([5, 5, 5]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([5, 5, 5]) + np.testing.assert_array_equal( evaluate_polynomial_at_measuring_distances(distances, coefficients), expected_result, ) @@ -151,8 +151,8 @@ def test_EvaluatePolynomialRaisesAPolynomialCoefficientErrorForAnEmptyCoefficien from napytau.core.polynomials import evaluate_polynomial_at_measuring_distances - distances: ndarray = array([1, 2]) - coefficients: ndarray = array([]) + distances: np.ndarray = np.array([1, 2]) + coefficients: np.ndarray = np.array([]) # With an empty coefficients array, the function should throw a polynomial # coefficient error. with self.assertRaises(PolynomialCoefficientError): @@ -164,8 +164,8 @@ def test_CanEvaluateAValidDifferentiatedPolynomialAtMeasuringDistances(): numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([1, 1, 1]), array([1, 2, 3])] - numpy_module_mock.zeros_like.return_value = array([0, 0, 0]) + numpy_module_mock.power.side_effect = [np.array([1, 1, 1]), np.array([1, 2, 3])] + numpy_module_mock.zeros_like.return_value = np.array([0, 0, 0]) with patch.dict( "sys.modules", @@ -178,14 +178,14 @@ def test_CanEvaluateAValidDifferentiatedPolynomialAtMeasuringDistances(): ) # Test for a simple quadratic polynomial: 2 + 3x + 4x^2 - distances: ndarray = array([1, 2, 3]) - coefficients: ndarray = array([2, 3, 4]) + distances: np.ndarray = np.array([1, 2, 3]) + coefficients: np.ndarray = np.array([2, 3, 4]) # The differentiated polynomial should be: 3 + 8x # At x = 1: 3 + 8(1) = 3 + 8 = 11 # At x = 2: 3 + 8(2) = 3 + 16 = 19 # At x = 3: 3 + 8(3) = 3 + 24 = 27 - expected_result: ndarray = array([11, 19, 27]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([11, 19, 27]) + np.testing.assert_array_equal( evaluate_differentiated_polynomial_at_measuring_distances( distances, coefficients ), @@ -198,8 +198,8 @@ def test_CanEvaluateADifferentiatedPolynomialAtMeasuringDistancesForEmptyDistanc numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([]), array([])] - numpy_module_mock.zeros_like.return_value = array([]) + numpy_module_mock.power.side_effect = [np.array([]), np.array([])] + numpy_module_mock.zeros_like.return_value = np.array([]) with patch.dict( "sys.modules", @@ -211,11 +211,11 @@ def test_CanEvaluateADifferentiatedPolynomialAtMeasuringDistancesForEmptyDistanc evaluate_differentiated_polynomial_at_measuring_distances, ) - distances: ndarray = array([]) - coefficients: ndarray = array([2, 3, 4]) + distances: np.ndarray = np.array([]) + coefficients: np.ndarray = np.array([2, 3, 4]) # With an empty input array, the result should also be an empty array - expected_result: ndarray = array([]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([]) + np.testing.assert_array_equal( evaluate_differentiated_polynomial_at_measuring_distances( distances, coefficients ), @@ -228,8 +228,8 @@ def test_CanEvaluateADifferentiatedPolynomialAtMeasuringDistancesForSingleDistan numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([1]), array([2])] - numpy_module_mock.zeros_like.return_value = array([0]) + numpy_module_mock.power.side_effect = [np.array([1]), np.array([2])] + numpy_module_mock.zeros_like.return_value = np.array([0]) with patch.dict( "sys.modules", @@ -241,12 +241,12 @@ def test_CanEvaluateADifferentiatedPolynomialAtMeasuringDistancesForSingleDistan evaluate_differentiated_polynomial_at_measuring_distances, ) - distances: ndarray = array([2]) - coefficients: ndarray = array([1, 2]) + distances: np.ndarray = np.array([2]) + coefficients: np.ndarray = np.array([1, 2]) # The differentiated polynomial should be: 2 # At x = 2: 2 - expected_result: ndarray = array([2]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([2]) + np.testing.assert_array_equal( evaluate_differentiated_polynomial_at_measuring_distances( distances, coefficients ), @@ -259,8 +259,8 @@ def test_CanEvaluateADifferentiatedPolynomialOfDegreeZeroAtMeasuringDistances(): numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.side_effect = [array([1, 1, 1]), array([1, 2, 3])] - numpy_module_mock.zeros_like.return_value = array([0, 0, 0]) + numpy_module_mock.power.side_effect = [np.array([1, 1, 1]), np.array([1, 2, 3])] + numpy_module_mock.zeros_like.return_value = np.array([0, 0, 0]) with patch.dict( "sys.modules", @@ -272,12 +272,12 @@ def test_CanEvaluateADifferentiatedPolynomialOfDegreeZeroAtMeasuringDistances(): evaluate_differentiated_polynomial_at_measuring_distances, ) - distances: ndarray = array([1, 2, 3]) - coefficients: ndarray = array([5]) + distances: np.ndarray = np.array([1, 2, 3]) + coefficients: np.ndarray = np.array([5]) # The differentiated polynomial should be: 0 # All values should therefore be 0 - expected_result: ndarray = array([0, 0, 0]) - testing.assert_array_equal( + expected_result: np.ndarray = np.array([0, 0, 0]) + np.testing.assert_array_equal( evaluate_differentiated_polynomial_at_measuring_distances( distances, coefficients ), @@ -292,8 +292,8 @@ def test_EvaluateDifferentiatedPolynomialRaisesAPolynomialCoefficientErrorForAnE evaluate_differentiated_polynomial_at_measuring_distances, ) - distances: ndarray = array([1, 2]) - coefficients: ndarray = array([]) + distances: np.ndarray = np.array([1, 2]) + coefficients: np.ndarray = np.array([]) # With an empty coefficients array, the function should throw a polynomial # coefficient error. with self.assertRaises(PolynomialCoefficientError): diff --git a/tests/core/tau_final_test.py b/tests/core/tau_final_test.py index 43cfc0d..98580d5 100644 --- a/tests/core/tau_final_test.py +++ b/tests/core/tau_final_test.py @@ -1,7 +1,6 @@ import unittest from unittest.mock import MagicMock, patch -from numpy import ndarray -from numpy import array +import numpy as np def set_up_mocks() -> MagicMock: @@ -18,7 +17,7 @@ def test_calculateTauFinalForValidData(self): numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.return_value: ndarray = array([1, 4]) + numpy_module_mock.power.return_value: np.ndarray = np.array([1, 4]) numpy_module_mock.sum.side_effect = [3, 1.25, 1.25] numpy_module_mock.sqrt.return_value: float = 0.894427191 @@ -30,8 +29,8 @@ def test_calculateTauFinalForValidData(self): ): from napytau.core.tau_final import calculate_tau_final - tau_i: ndarray = array([2, 4]) - delta_tau_i: ndarray = array([1, 2]) + tau_i: np.ndarray = np.array([2, 4]) + delta_tau_i: np.ndarray = np.array([1, 2]) tau_final: (float, float) = calculate_tau_final(tau_i, delta_tau_i) @@ -41,12 +40,38 @@ def test_calculateTauFinalForValidData(self): self.assertAlmostEqual(tau_final[0], expected_tau_final) self.assertAlmostEqual(tau_final[1], expected_uncertainty) + self.assertEqual(len(numpy_module_mock.power.mock_calls), 1) + + np.testing.assert_array_equal( + numpy_module_mock.power.mock_calls[0].args[0], np.array([1, 2]) + ) + + self.assertEqual(numpy_module_mock.power.mock_calls[0].args[1], 2) + + self.assertEqual(len(numpy_module_mock.sum.mock_calls), 3) + + np.testing.assert_array_equal( + numpy_module_mock.sum.mock_calls[0].args[0], np.array([2, 1]) + ) + + np.testing.assert_array_equal( + numpy_module_mock.sum.mock_calls[1].args[0], np.array([1, 0.25]) + ) + + np.testing.assert_array_equal( + numpy_module_mock.sum.mock_calls[2].args[0], np.array([1, 0.25]) + ) + + self.assertEqual(len(numpy_module_mock.sqrt.mock_calls), 1) + + self.assertEqual(numpy_module_mock.sqrt.mock_calls[0].args[0], 0.8) + def test_calculateTauFinalForEmptyInput(self): """Calculate tau_final for empty input.""" numpy_module_mock = set_up_mocks() # Mocked return values of called functions - numpy_module_mock.power.return_value: ndarray = array([]) + numpy_module_mock.power.return_value: np.ndarray = np.array([]) numpy_module_mock.sum.side_effect = [0, 0, 0, 0] numpy_module_mock.sqrt.return_value: float = 0 @@ -58,8 +83,8 @@ def test_calculateTauFinalForEmptyInput(self): ): from napytau.core.tau_final import calculate_tau_final - tau_i: ndarray = array([]) - delta_tau_i: ndarray = array([]) + tau_i: np.ndarray = np.array([]) + delta_tau_i: np.ndarray = np.array([]) tau_final: (float, float) = calculate_tau_final(tau_i, delta_tau_i) diff --git a/tests/core/tau_test.py b/tests/core/tau_test.py index f2a21df..d8e9145 100644 --- a/tests/core/tau_test.py +++ b/tests/core/tau_test.py @@ -1,8 +1,6 @@ import unittest from unittest.mock import MagicMock, patch -from numpy import ndarray -from numpy import array -from numpy import testing +import numpy as np from typing import Tuple @@ -23,12 +21,12 @@ def test_CanCalculateTau(self): chi_mock, polynomials_mock = set_up_mocks() # Mocked return values of called functions - chi_mock.optimize_coefficients.return_value: Tuple[ndarray, float] = ( - array([2, 3, 1]), + chi_mock.optimize_coefficients.return_value: Tuple[np.ndarray, float] = ( + np.array([2, 3, 1]), 0, ) chi_mock.optimize_t_hyp.return_value: float = 2.0 - polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value: ndarray = array( + polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.return_value: np.ndarray = np.array( [2, 6] ) @@ -41,19 +39,19 @@ def test_CanCalculateTau(self): ): from napytau.core.tau import calculate_tau_i_values - doppler_shifted_intensities: ndarray = array([2, 6]) - unshifted_intensities: ndarray = array([6, 10]) - delta_doppler_shifted_intensities: ndarray = array([1, 1]) - delta_unshifted_intensities: ndarray = array([1, 1]) - initial_coefficients: ndarray = array([1, 1, 1]) - distances: ndarray = array([0, 1]) + doppler_shifted_intensities: np.ndarray = np.array([2, 6]) + unshifted_intensities: np.ndarray = np.array([6, 10]) + delta_doppler_shifted_intensities: np.ndarray = np.array([1, 1]) + delta_unshifted_intensities: np.ndarray = np.array([1, 1]) + initial_coefficients: np.ndarray = np.array([1, 1, 1]) + distances: np.ndarray = np.array([0, 1]) t_hyp_range: (float, float) = (-5, 5) weight_factor: float = 1.0 # Expected result - expected_tau: ndarray = array([3, 1.6666667]) + expected_tau: np.ndarray = np.array([3, 1.6666667]) - testing.assert_array_almost_equal( + np.testing.assert_array_almost_equal( calculate_tau_i_values( doppler_shifted_intensities, unshifted_intensities, @@ -70,34 +68,34 @@ def test_CanCalculateTau(self): self.assertEqual(len(chi_mock.optimize_coefficients.mock_calls), 1) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[0], - (array([2, 6])), + (np.array([2, 6])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[1], - (array([6, 10])), + (np.array([6, 10])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[2], - (array([1, 1])), + (np.array([1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[3], - (array([1, 1])), + (np.array([1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[4], - (array([1, 1, 1])), + (np.array([1, 1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_coefficients.mock_calls[0].args[5], - (array([0, 1])), + (np.array([0, 1])), ) self.assertEqual( @@ -109,34 +107,34 @@ def test_CanCalculateTau(self): self.assertEqual(len(chi_mock.optimize_t_hyp.mock_calls), 1) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[0], - (array([2, 6])), + (np.array([2, 6])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[1], - (array([6, 10])), + (np.array([6, 10])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[2], - (array([1, 1])), + (np.array([1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[3], - (array([1, 1])), + (np.array([1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[4], - (array([1, 1, 1])), + (np.array([1, 1, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( chi_mock.optimize_t_hyp.mock_calls[0].args[5], - (array([0, 1])), + (np.array([0, 1])), ) self.assertEqual( @@ -153,16 +151,16 @@ def test_CanCalculateTau(self): 1, ) - testing.assert_array_equal( + np.testing.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[0], - (array([0, 1])), + (np.array([0, 1])), ) - testing.assert_array_equal( + np.testing.assert_array_equal( polynomials_mock.evaluate_differentiated_polynomial_at_measuring_distances.mock_calls[ 0 ].args[1], - (array([2, 3, 1])), + (np.array([2, 3, 1])), ) diff --git a/tools/prepare_release.py b/tools/prepare_release.py index aad3393..8efe067 100644 --- a/tools/prepare_release.py +++ b/tools/prepare_release.py @@ -61,7 +61,7 @@ def main() -> None: # os.system(f"git push origin {branch_name}") print( - f"Done! Branch {branch_name} created! Please create a pull request to merge the changes." # noqa E501 + f"Done! Branch {branch_name} created! Please create a pull request to merge the changes." # noqa E501 ) diff --git a/tools/release.py b/tools/release.py index fe72076..ce423d8 100644 --- a/tools/release.py +++ b/tools/release.py @@ -1,5 +1,6 @@ import os + def main() -> None: with open("pyproject.toml", "r") as config_file: lines = config_file.readlines() @@ -19,5 +20,6 @@ def main() -> None: print(f"Done! Tagged version {version}!") + if __name__ == "__main__": - main() \ No newline at end of file + main()