|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +import numpy.testing as nptest |
| 4 | +from mock import MagicMock |
| 5 | +from mvesuvio.util.analysis_helpers import extractWS, _convert_dict_to_table, fix_profile_parameters |
| 6 | +from mantid.simpleapi import CreateWorkspace, DeleteWorkspace |
| 7 | + |
| 8 | + |
| 9 | +class TestAnalysisHelpers(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + pass |
| 12 | + |
| 13 | + def test_extract_ws(self): |
| 14 | + data = [1, 2, 3] |
| 15 | + ws = CreateWorkspace(DataX=data, DataY=data, DataE=data, NSpec=1, UnitX="some_unit") |
| 16 | + |
| 17 | + dataX, dataY, dataE = extractWS(ws) |
| 18 | + nptest.assert_array_equal([data], dataX) |
| 19 | + nptest.assert_array_equal([data], dataY) |
| 20 | + nptest.assert_array_equal([data], dataE) |
| 21 | + |
| 22 | + DeleteWorkspace(ws) |
| 23 | + |
| 24 | + |
| 25 | + def test_convert_dict_to_table(self): |
| 26 | + d = {'H': {'label': 'H', 'mass': 1, 'intensity': 1}} |
| 27 | + table = _convert_dict_to_table(d) |
| 28 | + self.assertEqual(['label', 'mass', 'intensity'], table.getColumnNames()) |
| 29 | + self.assertEqual({'label': 'H', 'mass': 1, 'intensity': 1}, table.row(0)) |
| 30 | + |
| 31 | + |
| 32 | + def test_fix_profile_parameters_with_H(self): |
| 33 | + means_table_mock = MagicMock() |
| 34 | + means_table_mock.rowCount.return_value = 3 |
| 35 | + means_table_mock.row.side_effect = [ |
| 36 | + {'label': '16.0', 'mass': 16.0, 'mean_width': 8.974, 'std_width': 1.401, 'mean_intensity': 0.176, 'std_intensity': 0.08722}, |
| 37 | + {'label': '27.0', 'mass': 27.0, 'mean_width': 15.397, 'std_width': 1.131, 'mean_intensity': 0.305, 'std_intensity': 0.04895}, |
| 38 | + {'label': '12.0', 'mass': 12.0, 'mean_width': 13.932, 'std_width': 0.314, 'mean_intensity': 0.517, 'std_intensity': 0.09531} |
| 39 | + ] |
| 40 | + profiles_table_mock = MagicMock() |
| 41 | + profiles_table_mock.rowCount.return_value = 4 |
| 42 | + profiles_table_mock.row.side_effect = [ |
| 43 | + {'label': '1.0079', 'mass': 1.0078, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 4.699, 'width_bounds': '[3, 6]', 'center': 0.0, 'center_bounds': '[-3, 1]'}, |
| 44 | + {'label': '16.0', 'mass': 16.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 12, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'}, |
| 45 | + {'label': '12.0', 'mass': 12.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 8, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'}, |
| 46 | + {'label': '27.0', 'mass': 27.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 13, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 47 | + ] |
| 48 | + |
| 49 | + result_table = fix_profile_parameters(means_table_mock, profiles_table_mock, h_ratio=14.7) |
| 50 | + |
| 51 | + # For some reason, reading floats from table introduces small variations in stored values |
| 52 | + # TODO: Fix floating positions eg. 8.973999977111816 -> 8.974 |
| 53 | + self.assertEqual( |
| 54 | + result_table.row(0), |
| 55 | + {'label': '1.0079', 'mass': 1.0077999830245972, 'intensity': 0.8839251399040222, 'intensity_bounds': '[0, None]', 'width': 4.698999881744385, 'width_bounds': '[3, 6]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 56 | + ) |
| 57 | + self.assertEqual( |
| 58 | + result_table.row(1), |
| 59 | + {'label': '16.0', 'mass': 16.0, 'intensity': 0.020470114424824715, 'intensity_bounds': '[0, None]', 'width': 8.973999977111816, 'width_bounds': '[8.974, 8.974]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 60 | + ) |
| 61 | + self.assertEqual( |
| 62 | + result_table.row(2), |
| 63 | + {'label': '12.0', 'mass': 12.0, 'intensity': 0.06013096123933792, 'intensity_bounds': '[0, None]', 'width': 13.932000160217285, 'width_bounds': '[13.932, 13.932]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 64 | + ) |
| 65 | + self.assertEqual( |
| 66 | + result_table.row(3), |
| 67 | + {'label': '27.0', 'mass': 27.0, 'intensity': 0.0354737788438797, 'intensity_bounds': '[0, None]', 'width': 15.397000312805176, 'width_bounds': '[15.397, 15.397]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | + def test_fix_profile_parameters_without_H(self): |
| 72 | + # TODO: Use a more physical example containing Deuterium |
| 73 | + # Same profiles as before, except when H is not present, |
| 74 | + # the first mass will be made free to vary and the intensities don't change |
| 75 | + |
| 76 | + means_table_mock = MagicMock() |
| 77 | + means_table_mock.rowCount.return_value = 3 |
| 78 | + means_table_mock.row.side_effect = [ |
| 79 | + {'label': '16.0', 'mass': 16.0, 'mean_width': 8.974, 'std_width': 1.401, 'mean_intensity': 0.176, 'std_intensity': 0.08722}, |
| 80 | + {'label': '27.0', 'mass': 27.0, 'mean_width': 15.397, 'std_width': 1.131, 'mean_intensity': 0.305, 'std_intensity': 0.04895}, |
| 81 | + {'label': '12.0', 'mass': 12.0, 'mean_width': 13.932, 'std_width': 0.314, 'mean_intensity': 0.517, 'std_intensity': 0.09531} |
| 82 | + ] |
| 83 | + profiles_table_mock = MagicMock() |
| 84 | + profiles_table_mock.rowCount.return_value = 3 |
| 85 | + profiles_table_mock.row.side_effect = [ |
| 86 | + {'label': '16.0', 'mass': 16.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 12, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'}, |
| 87 | + {'label': '12.0', 'mass': 12.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 8, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'}, |
| 88 | + {'label': '27.0', 'mass': 27.0, 'intensity': 1.0, 'intensity_bounds': '[0, None]', 'width': 13, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 89 | + ] |
| 90 | + |
| 91 | + result_table = fix_profile_parameters(means_table_mock, profiles_table_mock, h_ratio=14.7) |
| 92 | + |
| 93 | + # For some reason, reading floats from table introduces small variations in stored values |
| 94 | + # TODO: Fix floating positions eg. 8.973999977111816 -> 8.974 |
| 95 | + self.assertEqual( |
| 96 | + result_table.row(0), |
| 97 | + {'label': '16.0', 'mass': 16.0, 'intensity': 0.17635270953178406, 'intensity_bounds': '[0, None]', 'width': 8.973999977111816, 'width_bounds': '[8.974, 8.974]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 98 | + ) |
| 99 | + self.assertEqual( |
| 100 | + result_table.row(1), |
| 101 | + {'label': '12.0', 'mass': 12.0, 'intensity': 0.5180360674858093, 'intensity_bounds': '[0, None]', 'width': 13.932000160217285, 'width_bounds': '[0, None]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 102 | + ) |
| 103 | + self.assertEqual( |
| 104 | + result_table.row(2), |
| 105 | + {'label': '27.0', 'mass': 27.0, 'intensity': 0.3056112229824066, 'intensity_bounds': '[0, None]', 'width': 15.397000312805176, 'width_bounds': '[15.397, 15.397]', 'center': 0.0, 'center_bounds': '[-3, 1]'} |
| 106 | + ) |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + unittest.main() |
0 commit comments