def test_algorithm_with_invalid_spectrum_number(self): #tests that a float spectrum number throws an error with self.assertRaises(TypeError): FindPeaksAutomatic(InputWorkspace=self.data_ws, PlotPeaks=False, SpectrumNumber=3.4) #tests that a negative integer throws an error with self.assertRaises(ValueError): FindPeaksAutomatic(InputWorkspace=self.data_ws, PlotPeaks=False, SpectrumNumber=-1)
def test_algorithm_creates_all_output_workspaces(self): ws_name = self.raw_ws.getName() FindPeaksAutomatic(self.raw_ws) self.assertIn('{}_with_errors'.format(ws_name), mtd) self.assertIn('{}_{}'.format(self.raw_ws.getName(), 'properties'), mtd) self.assertIn('{}_{}'.format(self.raw_ws.getName(), 'refit_properties'), mtd)
def test_output_tables_are_correctly_formatted(self): FindPeaksAutomatic(self.raw_ws, FitToBaseline=True) peak_table = mtd['{}_{}'.format(self.raw_ws.getName(), 'properties')] refit_peak_table = mtd['{}_{}'.format(self.raw_ws.getName(), 'refit_properties')] self.assertEqual(self.peak_table_header, peak_table.getColumnNames()) self.assertEqual(self.peak_table_header, refit_peak_table.getColumnNames()) self.assertEqual(2, peak_table.rowCount()) self.assertEqual(0, refit_peak_table.rowCount())
def test_algorithm_does_not_create_temporary_workspaces(self): FindPeaksAutomatic(self.raw_ws) self.assertNotIn('ret', mtd) self.assertNotIn('raw_data_ws', mtd) self.assertNotIn('flat_ws', mtd) self.assertNotIn('fit_result_NormalisedCovarianceMatrix', mtd) self.assertNotIn('fit_result_Parameters', mtd) self.assertNotIn('fit_result_Workspace', mtd) self.assertNotIn('fit_cost', mtd)
def find_peak_algorithm(workspace, spectrum_number, min_energy, max_energy, threshold, min_width, estimate_width, max_width): FindPeaksAutomatic(InputWorkspace=retrieve_ws(workspace), SpectrumNumber=spectrum_number, StartXValue=min_energy, EndXValue=max_energy, AcceptanceThreshold=threshold, PeakPropertiesTableName=workspace + PEAKS_WS_SUFFIX, RefitPeakPropertiesTableName=workspace + REFITTED_PEAKS_WS_SUFFIX, MinPeakSigma=min_width, EstimatePeakSigma=estimate_width, MaxPeakSigma=max_width)
def test_that_algorithm_finds_peaks_correctly(self): FindPeaksAutomatic( InputWorkspace=self.raw_ws, SmoothWindow=500, EstimatePeakSigma=5, MinPeakSigma=3, MaxPeakSigma=15, ) peak_table = mtd['{}_{}'.format(self.raw_ws.getName(), 'properties')] refit_peak_table = mtd['{}_{}'.format(self.raw_ws.getName(), 'refit_properties')] self.assertEqual(2, peak_table.rowCount()) self.assertEqual(0, refit_peak_table.rowCount()) self.assertPeakFound(peak_table.row(0), self.centre[0], self.height[0], self.width[0], 0.05) self.assertPeakFound(peak_table.row(1), self.centre[1], self.height[1], self.width[1], 0.05)
def test_algorithm_works_on_specified_spectrum(self): x_values = np.array( [np.linspace(0, 100, 1001), np.linspace(0, 100, 1001)], dtype=float) centre = np.array([[25, 75], [10, 60]], dtype=float) height = np.array([[35, 20], [40, 50]], dtype=float) width = np.array([[10, 5], [8, 6]], dtype=float) y_values = np.array([ self.gaussian(x_values[0], centre[0, 0], height[0, 0], width[0, 0]), self.gaussian(x_values[1], centre[1, 0], height[1, 0], width[1, 0]) ]) y_values += np.array([ self.gaussian(x_values[0], centre[0, 1], height[0, 1], width[0, 1]), self.gaussian(x_values[1], centre[1, 1], height[1, 1], width[1, 1]) ]) background = 10 * np.ones(x_values.shape) y_values += background raw_ws = CreateWorkspace(DataX=x_values, DataY=y_values, OutputWorkspace='raw_ws', NSpec=2) FindPeaksAutomatic( InputWorkspace=raw_ws, SpectrumNumber=2, SmoothWindow=500, EstimatePeakSigma=6, MinPeakSigma=3, MaxPeakSigma=15, ) peak_table = mtd['{}_{}'.format(raw_ws.getName(), 'properties')] print(peak_table.row(1)) self.assertPeakFound(peak_table.row(0), 10, 40, 8) self.assertPeakFound(peak_table.row(1), 60, 50, 6)
def test_algorithm_throws_RuntimeError_when_called_with_invalid_spectrum_number( self): x_values = np.array( [np.linspace(0, 100, 1001), np.linspace(0, 100, 1001)], dtype=float) centre = np.array([[25, 75], [10, 60]], dtype=float) height = np.array([[35, 20], [40, 50]], dtype=float) width = np.array([[10, 5], [8, 6]], dtype=float) y_values = np.array([ self.gaussian(x_values[0], centre[0, 0], height[0, 0], width[0, 0]), self.gaussian(x_values[1], centre[1, 0], height[1, 0], width[1, 0]) ]) y_values += np.array([ self.gaussian(x_values[0], centre[0, 1], height[0, 1], width[0, 1]), self.gaussian(x_values[1], centre[1, 1], height[1, 1], width[1, 1]) ]) background = 10 * np.ones(x_values.shape) y_values += background raw_ws = CreateWorkspace(DataX=x_values, DataY=y_values, OutputWorkspace='raw_ws', NSpec=2) with self.assertRaises(RuntimeError): FindPeaksAutomatic( InputWorkspace=raw_ws, SpectrumNumber=3, SmoothWindow=500, EstimatePeakSigma=6, MinPeakSigma=3, MaxPeakSigma=15, )
def test_algorithm_with_negative_max_peak_sigma_throws(self): with self.assertRaises(ValueError): FindPeaksAutomatic(InputWorkspace=self.data_ws, MaxPeakSigma=-0.1, PlotPeaks=False)
def test_algorithm_with_negative_num_bad_peaks_to_consider_throws(self): with self.assertRaises(ValueError): FindPeaksAutomatic(InputWorkspace=self.data_ws, BadPeaksToConsider=-3, PlotPeaks=False)
def test_algorithm_with_negative_smooth_window_throws(self): with self.assertRaises(ValueError): FindPeaksAutomatic(InputWorkspace=self.data_ws, SmoothWindow=-5, PlotPeaks=False)
def test_algorithm_with_negative_acceptance_threshold_throws(self): with self.assertRaises(ValueError): FindPeaksAutomatic(InputWorkspace=self.data_ws, AcceptanceThreshold=-0.1, PlotPeaks=False)
def test_algorithm_with_no_input_workspace_raises_exception(self): with self.assertRaises(RuntimeError): FindPeaksAutomatic()