예제 #1
0
 def test_dx_histogram_ascending(self):
     dataX = [1., 2., 3., 4.]
     dataY = [1., 2., 3.]
     dx = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX, DataY=dataY, Dx=dx, UnitX='TOF', Distribution=False)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     sortedDx = sortedws.readDx(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual(dx, sortedDx.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #2
0
 def test_sort_descending(self):
     dataX = [1., 2., 3., 4.]
     dataY = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX, DataY=dataY, UnitX='TOF', Distribution=False)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws, Ordering="Descending")
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual([4., 3., 2., 1.], sortedX.tolist())
     self.assertEqual([3., 2., 1.], sortedY.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #3
0
 def test_on_multiple_spectrum(self):
     dataX = [3., 2., 1., 3., 2., 1.
              ]  # In descending order, so y and e will need to be reversed.
     dataY = [1., 2., 3., 1., 2., 3.]
     dataE = [1., 2., 3., 1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,
                                  DataY=dataY,
                                  DataE=dataE,
                                  UnitX='TOF',
                                  Distribution=True,
                                  NSpec=2)
     dataY.reverse()
     dataE.reverse()
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     # Check the resulting data values for 1st spectrum.
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     sortedE = sortedws.readE(0)
     self.assertEqual(sorted(dataX[0:3]), sortedX.tolist())
     self.assertEqual(dataY[0:3], sortedY.tolist())
     self.assertEqual(dataE[0:3], sortedE.tolist())
     # Check the resulting data values for 2nd spectrum.
     sortedX = sortedws.readX(1)
     sortedY = sortedws.readY(1)
     sortedE = sortedws.readE(1)
     self.assertEqual(sorted(dataX[3:]), sortedX.tolist())
     self.assertEqual(dataY[3:], sortedY.tolist())
     self.assertEqual(dataE[3:], sortedE.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
def rebin_reduction(workspace_name, rebin_string, multi_frame_rebin_string, num_bins):
    """
    @param workspace_name Name of workspace to rebin
    @param rebin_string Rebin parameters
    @param multi_frame_rebin_string Rebin string for multiple frame rebinning
    @param num_bins Max number of bins in input frames
    """
    from mantid.simpleapi import (Rebin, RebinToWorkspace, SortXAxis)

    if rebin_string is not None:
        if multi_frame_rebin_string is not None and num_bins is not None:
            # Multi frame data
            if mtd[workspace_name].blocksize() == num_bins:
                Rebin(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name,
                      Params=rebin_string)
            else:
                Rebin(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name,
                      Params=multi_frame_rebin_string)
        else:
            # Regular data
            SortXAxis(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name)
            Rebin(InputWorkspace=workspace_name,
                  OutputWorkspace=workspace_name,
                  Params=rebin_string)
    else:
        try:
            # If user does not want to rebin then just ensure uniform binning across spectra
            RebinToWorkspace(WorkspaceToRebin=workspace_name,
                             WorkspaceToMatch=workspace_name,
                             OutputWorkspace=workspace_name)
        except RuntimeError:
            logger.warning('Rebinning failed, will try to continue anyway.')
예제 #5
0
 def test_x_ascending(self):
     dataX = [1., 2., 3.] # In ascending order, so y and e will need to be reversed.
     dataY = [1., 2., 3.]
     dataE = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=True)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     sortedE = sortedws.readE(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual(dataX, sortedX.tolist())
     self.assertEqual(dataY, sortedY.tolist())
     self.assertEqual(dataE, sortedE.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #6
0
 def test_dx_multiple_spectrum(self):
     dataX = [3, 2, 1, 3, 2, 1]  # In descending order, so y and e will need to be reversed.
     dataY = [1, 2, 3, 1, 2, 3]
     dx = [1, 2, 3, 1, 2, 3]
     unsortedws = CreateWorkspace(DataX=dataX, DataY=dataY, Dx=dx, UnitX='TOF', Distribution=True, NSpec=2)
     dx.reverse()
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     # Check the resulting data values for 1st spectrum.
     sortedDx = sortedws.readDx(0)
     self.assertEqual(dx[0:3], sortedDx.tolist())
     # Check the resulting data values for 2nd spectrum.
     sortedDx = sortedws.readDx(1)
     self.assertEqual(dx[3:], sortedDx.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #7
0
 def test_sort_descending(self):
     dataX = [1., 2., 3., 4.]
     dataY = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,
                                  DataY=dataY,
                                  UnitX='TOF',
                                  Distribution=False)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws, Ordering="Descending")
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual([4., 3., 2., 1.], sortedX.tolist())
     self.assertEqual([3., 2., 1.], sortedY.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #8
0
 def test_dx_histogram_ascending(self):
     dataX = [1., 2., 3., 4.]
     dataY = [1., 2., 3.]
     dx = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,
                                  DataY=dataY,
                                  Dx=dx,
                                  UnitX='TOF',
                                  Distribution=False)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     sortedDx = sortedws.readDx(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual(dx, sortedDx.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #9
0
 def test_on_multiple_spectrum(self):
     dataX = [3., 2., 1., 3., 2., 1.] # In descending order, so y and e will need to be reversed.
     dataY = [1., 2., 3., 1., 2., 3.]
     dataE = [1., 2., 3., 1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=True, NSpec=2)
     dataY.reverse()
     dataE.reverse()
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     # Check the resulting data values for 1st spectrum.
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     sortedE = sortedws.readE(0)
     self.assertEqual(sorted(dataX[0:3]), sortedX.tolist())
     self.assertEqual(dataY[0:3], sortedY.tolist())
     self.assertEqual(dataE[0:3], sortedE.tolist())
     # Check the resulting data values for 2nd spectrum.
     sortedX = sortedws.readX(1)
     sortedY = sortedws.readY(1)
     sortedE = sortedws.readE(1)
     self.assertEqual(sorted(dataX[3:]), sortedX.tolist())
     self.assertEqual(dataY[3:], sortedY.tolist())
     self.assertEqual(dataE[3:], sortedE.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #10
0
    def test_sorts_x_histogram_descending(self):
        dataX = [4., 3., 2., 1.]
        dataY = [1., 2., 3.]
        dataE = [1., 2., 3.]
        unsortedws = CreateWorkspace(DataX=dataX,DataY=dataY,DataE=dataE,UnitX='TOF',Distribution=False)
        # Run the algorithm
        sortedws = SortXAxis(InputWorkspace=unsortedws)
        sortedX = sortedws.readX(0)
        sortedY = sortedws.readY(0)
        sortedE = sortedws.readE(0)
        # Check the resulting data values. Sorting operation should have resulted in no changes
        self.assertEqual(sorted(dataX), sortedX.tolist())
        dataY.reverse()
        dataE.reverse()
        self.assertEqual(dataY, sortedY.tolist())
        self.assertEqual(dataE, sortedE.tolist())

        DeleteWorkspace(unsortedws)
        DeleteWorkspace(sortedws)
예제 #11
0
 def test_x_ascending(self):
     dataX = [1., 2., 3.
              ]  # In ascending order, so y and e will need to be reversed.
     dataY = [1., 2., 3.]
     dataE = [1., 2., 3.]
     unsortedws = CreateWorkspace(DataX=dataX,
                                  DataY=dataY,
                                  DataE=dataE,
                                  UnitX='TOF',
                                  Distribution=True)
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     sortedX = sortedws.readX(0)
     sortedY = sortedws.readY(0)
     sortedE = sortedws.readE(0)
     # Check the resulting data values. Sorting operation should have resulted in no changes
     self.assertEqual(dataX, sortedX.tolist())
     self.assertEqual(dataY, sortedY.tolist())
     self.assertEqual(dataE, sortedE.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #12
0
 def test_dx_multiple_spectrum(self):
     dataX = [3, 2, 1, 3, 2, 1
              ]  # In descending order, so y and e will need to be reversed.
     dataY = [1, 2, 3, 1, 2, 3]
     dx = [1, 2, 3, 1, 2, 3]
     unsortedws = CreateWorkspace(DataX=dataX,
                                  DataY=dataY,
                                  Dx=dx,
                                  UnitX='TOF',
                                  Distribution=True,
                                  NSpec=2)
     dx.reverse()
     # Run the algorithm
     sortedws = SortXAxis(InputWorkspace=unsortedws)
     # Check the resulting data values for 1st spectrum.
     sortedDx = sortedws.readDx(0)
     self.assertEqual(dx[0:3], sortedDx.tolist())
     # Check the resulting data values for 2nd spectrum.
     sortedDx = sortedws.readDx(1)
     self.assertEqual(dx[3:], sortedDx.tolist())
     DeleteWorkspace(unsortedws)
     DeleteWorkspace(sortedws)
예제 #13
0
    def test_sorts_x_histogram_descending(self):
        dataX = [4., 3., 2., 1.]
        dataY = [1., 2., 3.]
        dataE = [1., 2., 3.]
        unsortedws = CreateWorkspace(DataX=dataX,
                                     DataY=dataY,
                                     DataE=dataE,
                                     UnitX='TOF',
                                     Distribution=False)
        # Run the algorithm
        sortedws = SortXAxis(InputWorkspace=unsortedws)
        sortedX = sortedws.readX(0)
        sortedY = sortedws.readY(0)
        sortedE = sortedws.readE(0)
        # Check the resulting data values. Sorting operation should have resulted in no changes
        self.assertEqual(sorted(dataX), sortedX.tolist())
        dataY.reverse()
        dataE.reverse()
        self.assertEqual(dataY, sortedY.tolist())
        self.assertEqual(dataE, sortedE.tolist())

        DeleteWorkspace(unsortedws)
        DeleteWorkspace(sortedws)
예제 #14
0
def rebin_reduction(workspace_name, rebin_string, multi_frame_rebin_string,
                    num_bins):
    """
    @param workspace_name Name of workspace to rebin
    @param rebin_string Rebin parameters
    @param multi_frame_rebin_string Rebin string for multiple frame rebinning
    @param num_bins Max number of bins in input frames
    """
    from mantid.simpleapi import (Rebin, SortXAxis, RemoveSpectra)

    if rebin_string is not None:
        if multi_frame_rebin_string is not None and num_bins is not None:
            # Multi frame data
            if mtd[workspace_name].blocksize() == num_bins:
                Rebin(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name,
                      Params=rebin_string)
            else:
                Rebin(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name,
                      Params=multi_frame_rebin_string)
        else:
            # Regular data
            RemoveSpectra(InputWorkspace=workspace_name,
                          OutputWorkspace=workspace_name,
                          RemoveSpectraWithNoDetector=True)
            SortXAxis(InputWorkspace=workspace_name,
                      OutputWorkspace=workspace_name)
            Rebin(InputWorkspace=workspace_name,
                  OutputWorkspace=workspace_name,
                  Params=rebin_string)
    else:
        try:
            # If user does not want to rebin then just ensure uniform binning across spectra
            # extract the binning parameters from the first spectrum.
            # there is probably a better way to calculate the binning parameters, but this
            # gets the right answer.
            xaxis = mtd[workspace_name].readX(0)
            params = []
            for i, x in enumerate(xaxis):
                params.append(x)
                if i < len(xaxis) - 1:
                    params.append(xaxis[i + 1] - x)  # delta
            Rebin(InputWorkspace=workspace_name,
                  OutputWorkspace=workspace_name,
                  Params=params)
        except RuntimeError:
            logger.warning('Rebinning failed, will try to continue anyway.')
예제 #15
0
def _sort_x_axis(workspace):
    return SortXAxis(InputWorkspace=workspace,
                     OutputWorkspace="__sorted",
                     StoreInADS=False,
                     EnableLogging=False)
예제 #16
0
    def PyExec(self):
        self._setup()

        # Fit line to each of the spectra
        function = 'name=LinearBackground, A0=0, A1=0'
        input_params = [
            self._input_ws + ',i%d' % i
            for i in range(self._spec_range[0], self._spec_range[1] + 1)
        ]
        input_params = ';'.join(input_params)
        PlotPeakByLogValue(Input=input_params,
                           OutputWorkspace=self._output_msd_ws,
                           Function=function,
                           StartX=self._x_range[0],
                           EndX=self._x_range[1],
                           FitType='Sequential',
                           CreateOutput=True)

        delete_alg = self.createChildAlgorithm("DeleteWorkspace",
                                               enableLogging=False)
        delete_alg.setProperty(
            "Workspace", self._output_msd_ws + '_NormalisedCovarianceMatrices')
        delete_alg.execute()
        delete_alg.setProperty("Workspace",
                               self._output_msd_ws + '_Parameters')
        delete_alg.execute()
        rename_alg = self.createChildAlgorithm("RenameWorkspace",
                                               enableLogging=False)
        rename_alg.setProperty("InputWorkspace", self._output_msd_ws)
        rename_alg.setProperty("OutputWorkspace", self._output_param_ws)
        rename_alg.execute()

        params_table = mtd[self._output_param_ws]

        # MSD value should be positive, but the fit output is negative
        msd = params_table.column('A1')
        for i, value in enumerate(msd):
            params_table.setCell('A1', i, value * -1)

        # Create workspaces for each of the parameters
        parameter_ws_group = []

        # A0 workspace
        ws_name = self._output_msd_ws + '_A0'
        parameter_ws_group.append(ws_name)
        ConvertTableToMatrixWorkspace(self._output_param_ws,
                                      OutputWorkspace=ws_name,
                                      ColumnX='axis-1',
                                      ColumnY='A0',
                                      ColumnE='A0_Err',
                                      EnableLogging=False)
        xunit = mtd[ws_name].getAxis(0).setUnit('Label')
        xunit.setLabel('Temperature', 'K')
        SortXAxis(InputWorkspace=ws_name,
                  OutputWorkspace=ws_name,
                  EnableLogging=False)

        # A1 workspace
        ws_name = self._output_msd_ws + '_A1'
        parameter_ws_group.append(ws_name)
        ConvertTableToMatrixWorkspace(self._output_param_ws,
                                      OutputWorkspace=ws_name,
                                      ColumnX='axis-1',
                                      ColumnY='A1',
                                      ColumnE='A1_Err',
                                      EnableLogging=False)
        xunit = mtd[ws_name].getAxis(0).setUnit('Label')
        xunit.setLabel('Temperature', 'K')
        SortXAxis(InputWorkspace=ws_name,
                  OutputWorkspace=ws_name,
                  EnableLogging=False)

        AppendSpectra(InputWorkspace1=self._output_msd_ws + '_A0',
                      InputWorkspace2=self._output_msd_ws + '_A1',
                      ValidateInputs=False,
                      OutputWorkspace=self._output_msd_ws,
                      EnableLogging=False)
        delete_alg.setProperty("Workspace", self._output_msd_ws + '_A0')
        delete_alg.execute()
        delete_alg.setProperty("Workspace", self._output_msd_ws + '_A1')
        delete_alg.execute()
        # Create a new vertical axis for the Q and Q**2 workspaces
        y_axis = NumericAxis.create(2)
        for idx in range(1):
            y_axis.setValue(idx, idx)
        mtd[self._output_msd_ws].replaceAxis(1, y_axis)

        # Rename fit workspace group
        original_fit_ws_name = self._output_msd_ws + '_Workspaces'
        if original_fit_ws_name != self._output_fit_ws:
            rename_alg.setProperty("InputWorkspace",
                                   self._output_msd_ws + '_Workspaces')
            rename_alg.setProperty("OutputWorkspace", self._output_fit_ws)
            rename_alg.execute()

        # Add sample logs to output workspace
        copy_alg = self.createChildAlgorithm("CopyLogs", enableLogging=False)
        copy_alg.setProperty("InputWorkspace", self._input_ws)
        copy_alg.setProperty("OutputWorkspace", self._output_msd_ws)
        copy_alg.execute()
        copy_alg.setProperty("InputWorkspace", self._input_ws)
        copy_alg.setProperty("OutputWorkspace", self._output_fit_ws)
        copy_alg.execute()

        self.setProperty('OutputWorkspace', self._output_msd_ws)
        self.setProperty('ParameterWorkspace', self._output_param_ws)
        self.setProperty('FitWorkspaces', self._output_fit_ws)