Skip to content

Commit f0b1ba4

Browse files
Add unit tests for numerical third derivative
Also renamed function to more pythonic style. And added tests for function to extend range.
1 parent 11f4bb6 commit f0b1ba4

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/mvesuvio/analysis_reduction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
VesuvioThickness, Integration, Divide, Multiply, DeleteWorkspaces, \
1313
CreateWorkspace
1414

15-
from mvesuvio.util.analysis_helpers import numericalThirdDerivative, load_resolution, load_instrument_params, \
15+
from mvesuvio.util.analysis_helpers import numerical_third_derivative, load_resolution, load_instrument_params, \
1616
extend_range_of_array
1717

1818
np.set_printoptions(suppress=True, precision=4, linewidth=200)
@@ -559,7 +559,7 @@ def _neutron_compton_profiles(self, pars):
559559
JOfY = scipy.special.voigt_profile(self._y_space_arrays[self._row_being_fit] - centers, total_gaussian_width, lorentzian_width)
560560

561561
# Third derivative cuts edges of array by 6 indices
562-
JOfY_third_derivative = numericalThirdDerivative(self._y_space_arrays[self._row_being_fit], JOfY)
562+
JOfY_third_derivative = numerical_third_derivative(self._y_space_arrays[self._row_being_fit], JOfY)
563563

564564
deltaQ = self._deltaQ[self._row_being_fit, 6: -6]
565565
E0 = self._E0[self._row_being_fit, 6: -6]

src/mvesuvio/util/analysis_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def extractWS(ws):
237237
return ws.extractX(), ws.extractY(), ws.extractE()
238238

239239

240-
def numericalThirdDerivative(x, y):
240+
def numerical_third_derivative(x, y):
241241
k6 = (- y[:, 12:] + y[:, :-12]) * 1
242242
k5 = (+ y[:, 11:-1] - y[:, 1:-11]) * 24
243243
k4 = (- y[:, 10:-2] + y[:, 2:-10]) * 192

tests/unit/analysis/test_analysis_helpers.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import unittest
22
import numpy as np
3+
import scipy
34
import dill
45
import numpy.testing as nptest
56
from mock import MagicMock
67
from mvesuvio.util.analysis_helpers import extractWS, _convert_dict_to_table, \
7-
fix_profile_parameters, calculate_h_ratio
8+
fix_profile_parameters, calculate_h_ratio, extend_range_of_array, numerical_third_derivative
89
from mantid.simpleapi import CreateWorkspace, DeleteWorkspace
910

1011

@@ -126,5 +127,27 @@ def test_conversion_of_constraints(self):
126127
self.assertEqual(converted_constraints[1]['fun']([0, 0, 0, 2, 0, 0, 1]), 2-0.7234)
127128

128129

130+
def test_extend_range_of_array_for_increasing_range(self):
131+
x = np.arange(10)
132+
x = np.vstack([x, 2*x])
133+
x_extended = extend_range_of_array(x, 5)
134+
np.testing.assert_array_equal(x_extended, np.vstack([np.arange(-5, 15, 1), np.arange(-10, 30, 2)]))
135+
136+
137+
def test_extend_range_of_array_for_decreasing_range(self):
138+
x = np.linspace(-5, 5, 21)
139+
x = np.vstack([x, 2*x])
140+
x_extended = extend_range_of_array(x, 5)
141+
np.testing.assert_array_equal(x_extended, np.vstack([np.linspace(-7.5, 7.5, 31), np.linspace(-15, 15, 31)]))
142+
143+
144+
def test_numerical_third_derivative(self):
145+
x= np.linspace(-20, 20, 300) # Workspaces are about 300 points of range
146+
x = np.vstack([x, 2*x])
147+
y = scipy.special.voigt_profile(x, 5, 5)
148+
numerical_derivative = numerical_third_derivative(x, y)
149+
expected_derivative = np.array([np.gradient(np.gradient(np.gradient(y_i, x_i), x_i), x_i)[6: -6] for y_i, x_i in zip(y, x) ])
150+
np.testing.assert_allclose(numerical_derivative, expected_derivative, atol=1e-6)
151+
129152
if __name__ == "__main__":
130153
unittest.main()

0 commit comments

Comments
 (0)