Exemple #1
0
    def create_merged_workspace(self, workspace_list):
        if workspace_list:
            # get max number of bins and max X range
            max_num_bins = 0
            for ws_name in workspace_list:
                if ws_name:
                    ws = AnalysisDataService.retrieve(ws_name)
                    max_num_bins = max(ws.blocksize(), max_num_bins)

            # create single ws for the merged data, use original ws as a template
            template_ws = next(ws for ws in workspace_list if ws is not None)
            merged_ws = WorkspaceFactory.create(
                AnalysisDataService.retrieve(template_ws),
                NVectors=NUM_FILES_PER_DETECTOR,
                XLength=max_num_bins,
                YLength=max_num_bins)

            # create a merged workspace based on every entry from workspace list
            for i in range(0, NUM_FILES_PER_DETECTOR):
                # load in ws - first check workspace exists
                if workspace_list[i]:
                    ws = AnalysisDataService.retrieve(workspace_list[i])
                    # check if histogram data, and convert if necessary
                    if ws.isHistogramData():
                        ws = ConvertToPointData(InputWorkspace=ws.name(),
                                                OutputWorkspace=ws.name())
                    # find max x val
                    max_x = np.max(ws.readX(0))
                    # get current number of bins
                    num_bins = ws.blocksize()
                    # pad bins
                    X_padded = np.empty(max_num_bins)
                    X_padded.fill(max_x)
                    X_padded[:num_bins] = ws.readX(0)
                    Y_padded = np.zeros(max_num_bins)
                    Y_padded[:num_bins] = ws.readY(0)
                    E_padded = np.zeros(max_num_bins)
                    E_padded[:num_bins] = ws.readE(0)

                    # set row of merged workspace
                    merged_ws.setX(i, X_padded)
                    merged_ws.setY(i, Y_padded)
                    merged_ws.setE(i, E_padded)

                    # set y axis labels
                    self.set_y_axis_labels(merged_ws, SPECTRUM_INDEX)

                    # remove workspace from ADS
                    DeleteWorkspace(ws)

            return merged_ws
    def PyExec(self):
        # Progress reporter for algorithm initialization
        prog_reporter = Progress(self, start=0.0, end=0.1, nreports=4)

        raw_xvals, raw_yvals, raw_error, error_ws = self.load_data(
            prog_reporter)

        # Convert the data to point data
        prog_reporter.report('Converting to point data')
        raw_data_ws = ConvertToPointData(error_ws, StoreInADS=False)
        raw_xvals = raw_data_ws.readX(0).copy()
        raw_yvals = raw_data_ws.readY(0).copy()

        raw_xvals, raw_yvals, raw_error = self.crop_data(
            raw_xvals, raw_yvals, raw_error, prog_reporter)

        # Find the best peaks
        (peakids, peak_table, refit_peak_table), baseline = self.process(
            raw_xvals,
            raw_yvals,
            raw_error,
            acceptance=self._acceptance,
            average_window=self._smooth_window,
            bad_peak_to_consider=self._bad_peak_to_consider,
            use_poisson=self._use_poisson_cost,
            peak_width_estimate=self._estimate_peak_sigma,
            fit_to_baseline=self._fit_to_baseline,
            prog_reporter=prog_reporter)

        if self._plot_peaks:
            self.plot_peaks(raw_xvals, raw_yvals, baseline, peakids)

        self.set_output_properties(peak_table, refit_peak_table)
class FitIncidentSpectrumTest(unittest.TestCase):

    incident_wksp_name = 'incident_spectrum_wksp'
    phiMax = 6324
    phiEpi = 786
    alpha = 0.099
    lambda1 = 0.67143
    lambda2 = 0.06075
    lambdaT = 1.58
    binning_default = "0.2,0.01,4.0"

    def setUp(self):
        # Create the workspace to hold the already corrected incident spectrum
        self.incident_wksp = CreateWorkspace(
            OutputWorkspace=self.incident_wksp_name,
            NSpec=1,
            DataX=[0],
            DataY=[0],
            UnitX='Wavelength',
            VerticalAxisUnit='Text',
            VerticalAxisValues='IncidentSpectrum')
        self.incident_wksp = Rebin(InputWorkspace=self.incident_wksp,
                                   OutputWorkspace="foobar",
                                   Params=self.binning_default)
        self.incident_wksp = ConvertToPointData(
            InputWorkspace=self.incident_wksp, OutputWorkspace="foobar")
        # Add the incident spectrum to the workspace
        corrected_spectrum = self.generate_incident_spectrum(
            self.incident_wksp.readX(0), self.phiMax, self.phiEpi, self.alpha,
            self.lambda1, self.lambda2, self.lambdaT)
        self.incident_wksp.setY(0, corrected_spectrum)
        self.agl_instance = FitIncidentSpectrum

    def generate_incident_spectrum(self, wavelengths, phi_max, phi_epi, alpha,
                                   lambda_1, lambda_2, lambda_T):
        delta_term = 1. / (1. + np.exp((wavelengths - lambda_1) / lambda_2))
        term1 = phi_max * (lambda_T**4. / wavelengths**
                           5.) * np.exp(-(lambda_T / wavelengths)**2.)
        term2 = phi_epi * delta_term / (wavelengths**(1 + 2 * alpha))
        return term1 + term2

    def test_fit_cubic_spline_with_gauss_conv_produces_fit_with_same_range_as_binning_for_calc(
            self):
        binning_for_calc = "0.2,0.1,3.0"
        binning_for_fit = "0.2,0.1,4.0"
        alg_test = run_algorithm("FitIncidentSpectrum",
                                 InputWorkspace=self.incident_wksp,
                                 OutputWorkspace="fit_wksp",
                                 BinningForCalc=binning_for_calc,
                                 BinningForFit=binning_for_fit,
                                 FitSpectrumWith="GaussConvCubicSpline")
        self.assertTrue(alg_test.isExecuted())
        fit_wksp = AnalysisDataService.retrieve("fit_wksp")
        self.assertEqual(
            fit_wksp.readX(0).all(),
            np.arange(0.2, 3, 0.01).all())

    def test_fit_cubic_spline_produces_fit_with_same_range_as_binning_for_calc(
            self):
        binning_for_calc = "0.2,0.1,3.0"
        binning_for_fit = "0.2,0.1,4.0"
        alg_test = run_algorithm("FitIncidentSpectrum",
                                 InputWorkspace=self.incident_wksp,
                                 OutputWorkspace="fit_wksp",
                                 BinningForCalc=binning_for_calc,
                                 BinningForFit=binning_for_fit,
                                 FitSpectrumWith="CubicSpline")
        self.assertTrue(alg_test.isExecuted())
        fit_wksp = AnalysisDataService.retrieve("fit_wksp")
        self.assertEqual(fit_wksp.readX(0).all(), np.arange(0.2, 3, 0.1).all())

    def test_fit_cubic_spline_via_mantid_produces_fit_with_same_range_as_binning_for_calc(
            self):
        binning_for_calc = "0.2,0.1,3.0"
        binning_for_fit = "0.2,0.1,4.0"
        alg_test = run_algorithm("FitIncidentSpectrum",
                                 InputWorkspace=self.incident_wksp,
                                 OutputWorkspace="fit_wksp",
                                 BinningForCalc=binning_for_calc,
                                 BinningForFit=binning_for_fit,
                                 FitSpectrumWith="CubicSplineViaMantid")
        self.assertTrue(alg_test.isExecuted())
        fit_wksp = AnalysisDataService.retrieve("fit_wksp")
        self.assertEqual(fit_wksp.readX(0).all(), np.arange(0.2, 3, 0.1).all())