Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def PyExec(self):
if not self.no_x0_needed:
x0s = np.asarray(peak_param_ws.column("X0"))
# if no hkl provided, peak will be set to mean x0
peak = np.mean(x0s)
peak = np.mean(x0s[np.isfinite(x0s)]) # only use non nan x0s
if not self.no_chi2_needed:
chi2 = np.asarray(peak_param_ws.column("chi2"))
else:
Expand Down
80 changes: 40 additions & 40 deletions Testing/SystemTests/tests/framework/TextureAnalysisScriptTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import systemtesting
from mantid import config
from mantid.api import AnalysisDataService as ADS
from Engineering.texture.TextureUtils import run_abs_corr, fit_all_peaks, is_macOS
from Engineering.texture.TextureUtils import run_abs_corr, fit_all_peaks
from mantid.simpleapi import LoadEmptyInstrument, CreateSampleShape, SetSampleMaterial, Load, ExtractSingleSpectrum, ConvertUnits
from Engineering.common.xml_shapes import get_cube_xml
import numpy as np
Expand Down Expand Up @@ -57,7 +57,7 @@ def validate_expected_files(self):
[self.assertTrue(os.path.exists(ef)) for ef in self.expected_files]


class RunAStandardAbsorptionCorrectionWithAttenuationTable(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrectionWithAttenuationTable(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
kwargs = self.default_kwargs
Expand All @@ -82,7 +82,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["AbsorptionCorrection", "AttenuationTables", "User"])


class RunAStandardAbsorptionCorrection(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrection(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
kwargs = self.default_kwargs
Expand All @@ -101,7 +101,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["AbsorptionCorrection"])


class RunAStandardAbsorptionCorrectionEulerGoniometer(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrectionEulerGoniometer(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
orientation_file = os.path.join(CWDIR, "rotation_as_euler.txt")
Expand All @@ -124,7 +124,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["AbsorptionCorrection"])


class RunAStandardAbsorptionCorrectionProvideGoniometerMatrix(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrectionProvideGoniometerMatrix(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
orientation_file = os.path.join(CWDIR, "rotation_as_matrix.txt")
Expand All @@ -145,7 +145,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["AbsorptionCorrection"])


class RunAStandardAbsorptionCorrectionWithCustomGaugeVolume(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrectionWithCustomGaugeVolume(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
gv_file = os.path.join(CWDIR, "custom_gauge_volume.xml")
Expand All @@ -166,7 +166,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["AbsorptionCorrection"])


class RunAStandardAbsorptionCorrectionWithDivergenceCorrection(systemtesting.MantidSystemTest, AbsCorrMixin):
class RunAStandardAbsorptionCorrectionWithDivergenceCorrection(AbsCorrMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_absorption_correction_inputs()
kwargs = self.default_kwargs
Expand Down Expand Up @@ -196,57 +196,57 @@ def setup_fit_peaks_inputs(self):
self.fit_dir = os.path.join(CWDIR, "FitParameters")
self.peaks = (1.8, 1.44)
self.reference_columns = ["wsindex", "I_est", "I", "I_err", "A", "A_err", "B", "B_err", "X0", "X0_err", "S", "S_err"]
self.cols_to_check_vals = ["wsindex", "I_est", "I", "X0", "S"]
self.default_kwargs = {
"wss": ["ENGINX_280625_focused_bank_1_dSpacing"],
"peaks": self.peaks,
"peak_window": 0.03,
"peak_window": 0.05,
"save_dir": self.fit_dir,
}
self.peak_1_vals = [
0,
54.72063098592834,
219.83253111955707,
60.0549787,
58.67645081,
0.85816193,
8.69792,
0.0,
8.11518344152499,
0.0,
-0.7790091073987409,
0.0,
1.7477947721710467,
0.0,
0.00042411,
0.29023,
0.0,
1.80098428,
9.53361135e-05,
126.32162042,
1.8714507,
]
self.peak_2_vals = [
0,
45.836957142608,
60.962977355483645,
0.0,
212.37513269155008,
0.0,
155.94311474389426,
0.0,
1.4379627981678513,
53.11713333,
58.45633143,
1.11592828,
1497790.0,
0.0,
0.00696813,
0.0165231,
0.0,
1.43592701,
0.00016835,
163.86109962,
3.08200681,
]

def validate_table(self, out_table, expected_dict):
def validate_table(self, out_table, expected_dict, rtol=5e-3):
expected_cols = list(expected_dict.keys())
for c in out_table.getColumnNames():
print(c, ": ", np.nan_to_num(out_table.column(c)), ", validation value: ", expected_dict[c])

for c in out_table.getColumnNames():
self.assertIn(c, expected_cols)
if not is_macOS():
# fitting results currently flaky on mac os
np.testing.assert_allclose(np.nan_to_num(out_table.column(c)), expected_dict[c], rtol=1e-3)
if c in self.cols_to_check_vals:
np.testing.assert_allclose(np.nan_to_num(out_table.column(c)), expected_dict[c], rtol=rtol)

def validate_missing_peaks_vals(self, peak_1_vals, peak_2_vals):
param_table1 = ADS.retrieve("ENGINX_280625_2.3_GROUP_Fit_Parameters")
param_table1 = ADS.retrieve("ENGINX_280625_2.2_GROUP_Fit_Parameters")
param_table2 = ADS.retrieve("ENGINX_280625_2.5_GROUP_Fit_Parameters")
expected_files = [
os.path.join(CWDIR, "FitParameters", "GROUP", "2.3", "ENGINX_280625_2.3_GROUP_Fit_Parameters.nxs"),
os.path.join(CWDIR, "FitParameters", "GROUP", "2.2", "ENGINX_280625_2.2_GROUP_Fit_Parameters.nxs"),
os.path.join(CWDIR, "FitParameters", "GROUP", "2.5", "ENGINX_280625_2.5_GROUP_Fit_Parameters.nxs"),
]

Expand All @@ -255,7 +255,7 @@ def validate_missing_peaks_vals(self, peak_1_vals, peak_2_vals):
[self.assertTrue(os.path.exists(ef)) for ef in expected_files]


class TestFittingPeaksOfFocusedData(systemtesting.MantidSystemTest, PeakFitMixin):
class TestFittingPeaksOfFocusedDataNoGroup(PeakFitMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_fit_peaks_inputs()
fit_all_peaks(**self.default_kwargs)
Expand All @@ -269,19 +269,19 @@ def validate(self):
]

self.validate_table(param_table1, dict(zip(self.reference_columns, self.peak_1_vals)))
self.validate_table(param_table2, dict(zip(self.reference_columns, self.peak_2_vals)))
self.validate_table(param_table2, dict(zip(self.reference_columns, self.peak_2_vals)), rtol=8e-3)
[self.assertTrue(os.path.exists(ef)) for ef in expected_files]

def cleanup(self):
ADS.clear()
_try_delete_dirs(CWDIR, ["FitParameters"])


class TestFittingPeaksOfMissingPeakDataWithFillZero(systemtesting.MantidSystemTest, PeakFitMixin):
class TestFittingPeaksOfMissingPeakDataWithFillZero(PeakFitMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_fit_peaks_inputs()
kwargs = self.default_kwargs
kwargs["peaks"] = (2.3, 2.5)
kwargs["peaks"] = (2.2, 2.5)
# expect no peaks here, set the i over sigma threshold large as well as sigma is ill-defined
fit_all_peaks(**kwargs, i_over_sigma_thresh=10.0, nan_replacement="zeros")

Expand All @@ -296,11 +296,11 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["FitParameters"])


class TestFittingPeaksOfMissingPeakDataWithSpecifiedValue(systemtesting.MantidSystemTest, PeakFitMixin):
class TestFittingPeaksOfMissingPeakDataWithSpecifiedValue(PeakFitMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_fit_peaks_inputs()
kwargs = self.default_kwargs
kwargs["peaks"] = (2.3, 2.5)
kwargs["peaks"] = (2.2, 2.5)
# expect no peaks here, set the i over sigma threshold large as well as sigma is ill-defined
fit_all_peaks(**kwargs, i_over_sigma_thresh=10.0, nan_replacement="zeros", no_fit_value_dict={"I_est": 1.0, "I": 0.01})

Expand All @@ -316,7 +316,7 @@ def cleanup(self):
_try_delete_dirs(CWDIR, ["FitParameters"])


class TestFittingPeaksOfFocusedDataWithGroup(systemtesting.MantidSystemTest, PeakFitMixin):
class TestFittingPeaksOfFocusedDataWithGroup(PeakFitMixin, systemtesting.MantidSystemTest):
def runTest(self):
self.setup_fit_peaks_inputs()
self.input_ws.getRun().addProperty("Grouping", "TEST", False)
Expand All @@ -332,7 +332,7 @@ def validate(self):
]

self.validate_table(param_table1, dict(zip(self.reference_columns, self.peak_1_vals)))
self.validate_table(param_table2, dict(zip(self.reference_columns, self.peak_2_vals)))
self.validate_table(param_table2, dict(zip(self.reference_columns, self.peak_2_vals)), rtol=8e-3)
[self.assertTrue(os.path.exists(ef)) for ef in expected_files]

def cleanup(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The automated peak fitting routine ``fit_all_peaks`` in ``Engineering.texture.TextureUtils`` has been updated to improve robustness, particularly in terms of fitting peak positions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

from mantid import FunctionFactory
from mantid.api import TextAxis
from mantid.kernel import UnitConversion, DeltaEModeType, UnitParams
from mantid.kernel import UnitConversion, DeltaEModeType
from mantid.simpleapi import logger, CreateEmptyTableWorkspace, GroupWorkspaces, CreateWorkspace, FindPeaksConvolve, SaveNexus
from mantid.api import AnalysisDataService as ADS
from mantidqtinterfaces.Engineering.gui.engineering_diffraction.tabs.common.output_sample_logs import write_table_row
from mantid.api import CompositeFunction
from mantid.fitfunctions import FunctionWrapper
from mantidqtinterfaces.Engineering.gui.engineering_diffraction.tabs.common import output_settings
from mantidqtinterfaces.Engineering.gui.engineering_diffraction.tabs.common import wsname_in_instr_run_ceria_group_ispec_unit_format
from Engineering.EnggUtils import convert_TOFerror_to_derror
from os import path, makedirs


Expand Down Expand Up @@ -122,9 +123,7 @@ def _convert_TOF_to_d(self, tof, ws_name):

def _convert_TOFerror_to_derror(self, tof_error, d, ws_name):
diff_consts = self._get_diff_constants(ws_name)
difc = diff_consts[UnitParams.difc]
difa = diff_consts[UnitParams.difa] if UnitParams.difa in diff_consts else 0
return tof_error / (2 * difa * d + difc)
return convert_TOFerror_to_derror(diff_consts, tof_error, d)

def _get_diff_constants(self, ws_name):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,19 +669,6 @@ def test_save_files_with_no_RB_creates_dirs_and_saves(self, mock_output_path, mo

self.run_the_save_tests(expected_dirs, mock_makedirs, mock_save)

@patch(plot_model_path + ".FittingPlotModel._get_diff_constants")
def test_convert_centres_and_error_from_TOF_to_d(self, mock_get_diffs):
params = UnitParametersMap()
params[UnitParams.difc] = 18000
mock_get_diffs.return_value = params
tof = 40000
tof_error = 5
d = self.model._convert_TOF_to_d(tof, "ws_name")
d_error = self.model._convert_TOFerror_to_derror(tof_error, d, "ws_name")

self.assertAlmostEqual(tof / d, 18000, delta=1e-10)
self.assertAlmostEqual(d_error / d, tof_error / tof, delta=1e-10)

def _get_sample_findpeaksconvolve_group_ws(self):
peak_centers = CreateEmptyTableWorkspace(OutputWorkspace="PeakCentre")
peak_centers.addColumn("int", "SpecIndex")
Expand Down
6 changes: 6 additions & 0 deletions scripts/Engineering/EnggUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,12 @@ def convert_to_TOF(parent, ws):
return alg.getProperty("OutputWorkspace").value


def convert_TOFerror_to_derror(diff_consts, tof_error, d):
difc = diff_consts[UnitParams.difc]
difa = diff_consts[UnitParams.difa] if UnitParams.difa in diff_consts else 0
return tof_error / (2 * difa * d + difc)


def crop_data(parent, ws, indices):
"""
DEPRECATED: not used in UI, only in deprecated functions (EnggVanadiumCorrections and EnggFocus)
Expand Down
Loading