def setUp(self):
     input_ws = CreateSampleWorkspace(
         Function="User Defined",
         UserDefinedFunction="name=LinearBackground, " +
         "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
         NumBanks=2,
         BankPixelWidth=1,
         XMin=0,
         XMax=10,
         BinWidth=0.1,
         BankDistanceFromSample=4.0)
     self._input_ws = input_ws
     self._table = FindEPP(input_ws, OutputWorkspace="table")
     AddSampleLog(self._input_ws,
                  LogName='wavelength',
                  LogText='4.0',
                  LogType='Number',
                  LogUnit='Angstrom')
     for i in range(input_ws.getNumberHistograms()):
         y = input_ws.dataY(i)
         y.fill(0.)
         y[51] = 100.
         e = input_ws.dataE(i)
         e.fill(0.)
         e[51] = 10.
 def setUp(self):
     input_ws = CreateSampleWorkspace(Function="User Defined",
                                      UserDefinedFunction="name=LinearBackground, A0=0.3;name=Gaussian, \
                                      PeakCentre=5, Height=10, Sigma=0.3", NumBanks=2, BankPixelWidth=1,
                                      XMin=0, XMax=10, BinWidth=0.1, BankDistanceFromSample=4.0)
     self._input_ws = input_ws
     self._table = FindEPP(input_ws, OutputWorkspace="table")
     AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0', LogType='Number', LogUnit='Angstrom')
 def setUp(self):
     input_ws = CreateSampleWorkspace(
         Function="User Defined",
         UserDefinedFunction="name=LinearBackground, " +
         "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
         NumBanks=2, BankPixelWidth=1, XMin=0, XMax=10, BinWidth=0.1,
         BankDistanceFromSample=4.0)
     self._input_ws = input_ws
     self._table = FindEPP(input_ws, OutputWorkspace="table")
     AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0',
                  LogType='Number', LogUnit='Angstrom')
     # These ranges correspond to 6*FWHM of the gaussian above,
     # the integration ranges of ComputeCalibrationCoefVan.
     self._lowerBoundRange = slice(28, 73)
     self._upperBoundRange = slice(27, 74)
Example #4
0
def _fitElasticChannel(ys, wsNames, wsCleanup, algorithmLogging):
    """Return index to the peak position of ys."""
    xs = np.array([i for i in range(len(ys))])
    l2SumWSName = wsNames.withSuffix('summed_detectors_at_l2')
    l2SumWS = CreateWorkspace(OutputWorkspace=l2SumWSName,
                              DataX=xs,
                              DataY=ys,
                              EnableLogging=algorithmLogging)
    fitWSName = wsNames.withSuffix('summed_detectors_at_l2_fit_results')
    fitWS = FindEPP(InputWorkspace=l2SumWS,
                    OutputWorkspace=fitWSName,
                    EnableLogging=algorithmLogging)
    peakCentre = float(fitWS.cell('PeakCentre', 0))
    wsCleanup.cleanup(l2SumWS)
    wsCleanup.cleanup(fitWS)
    return peakCentre
def _fitElasticChannel(ys, wsNames, wsCleanup, algorithmLogging):
    """Return index to the peak position of ys."""
    xs = numpy.array([i for i in range(len(ys))])
    l2SumWSName = wsNames.withSuffix('summed_detectors_at_l2')
    l2SumWS = CreateWorkspace(OutputWorkspace=l2SumWSName,
                              DataX=xs,
                              DataY=ys,
                              EnableLogging=algorithmLogging)
    fitWSName = wsNames.withSuffix('summed_detectors_at_l2_fit_results')
    fitWS = FindEPP(InputWorkspace=l2SumWS,
                    OutputWorkspace=fitWSName,
                    EnableLogging=algorithmLogging)
    peakCentre = float(fitWS.cell('PeakCentre', 0))
    wsCleanup.cleanup(l2SumWS)
    wsCleanup.cleanup(fitWS)
    return int(round(peakCentre))
Example #6
0
    def test_calculation_fitsample(self):
        OutputWorkspaceName = "outputws"
        # generate EPP table
        table = FindEPP(self._input_ws)

        alg_test = run_algorithm("TOFTOFConvertTofToDeltaE",
                                 InputWorkspace=self._input_ws,
                                 EPPTable=table,
                                 OutputWorkspace=OutputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(OutputWorkspaceName)

        # create reference data for X axis
        dataX = np.linspace(4005.75, 7995.75, 381)
        tel = 6005.25
        factor = m_n * 1e+15 / eV
        newX = 0.5 * factor * 16.0 * (1 / tel**2 - 1 / dataX**2)
        # compare
        self.assertTrue(np.allclose(newX, wsoutput.readX(0)))  # sdd = 4.0
        self.assertTrue(np.allclose(4.0 * newX,
                                    wsoutput.readX(1)))  # sdd = 8.0

        # create reference data for Y axis and compare to the output
        tof = dataX[:-1] + 5.25
        newY = self._input_ws.readY(0) * tof**3 / (factor * 10.5 * 16.0)
        self.assertTrue(np.allclose(newY, wsoutput.readY(0)))  # sdd = 4.0
        self.assertTrue(np.allclose(newY / 4.0,
                                    wsoutput.readY(1)))  # sdd = 8.0

        DeleteWorkspace(wsoutput)
        DeleteWorkspace(table)
 def setUp(self):
     input_ws = CreateSampleWorkspace(Function="User Defined",
                                      UserDefinedFunction="name=LinearBackground, A0=0.3;name=Gaussian, \
                                      PeakCentre=5, Height=10, Sigma=0.3", NumBanks=2, BankPixelWidth=1,
                                      XMin=0, XMax=10, BinWidth=0.1, BankDistanceFromSample=4.0)
     self._input_ws = input_ws
     self._table = FindEPP(input_ws, OutputWorkspace="table")
     AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0', LogType='Number', LogUnit='Angstrom')
Example #8
0
def _fitEPP(ws, wsType, wsNames, algorithmLogging):
    """Return a fitted EPP table for a workspace."""
    if wsType == common.WS_CONTENT_DETS:
        eppWSName = wsNames.withSuffix('epp_detectors')
    else:
        eppWSName = wsNames.withSuffix('epp_monitors')
    eppWS = FindEPP(InputWorkspace=ws,
                    OutputWorkspace=eppWSName,
                    EnableLogging=algorithmLogging)
    return eppWS
 def setUp(self):
     input_ws = CreateSampleWorkspace(
         Function="User Defined",
         UserDefinedFunction="name=LinearBackground, " +
         "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
         NumBanks=2, BankPixelWidth=1, XMin=0, XMax=10, BinWidth=0.1,
         BankDistanceFromSample=4.0)
     self._input_ws = input_ws
     self._table = FindEPP(input_ws, OutputWorkspace="table")
     AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0',
                  LogType='Number', LogUnit='Angstrom')
     for i in range(input_ws.getNumberHistograms()):
         y = input_ws.dataY(i)
         y.fill(0.)
         y[51] = 100.
         e = input_ws.dataE(i)
         e.fill(0.)
         e[51] = 10.
 def _EPPTable(self, ws, eppWSName):
     eppWS = FindEPP(InputWorkspace=ws,
                     OutputWorkspace=eppWSName,
                     EnableLogging=False)
     return eppWS
class ComputeCalibrationCoefVanTest(unittest.TestCase):
    def setUp(self):
        input_ws = CreateSampleWorkspace(
            Function="User Defined",
            UserDefinedFunction="name=LinearBackground, " +
            "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
            NumBanks=2, BankPixelWidth=1, XMin=0, XMax=10, BinWidth=0.1,
            BankDistanceFromSample=4.0)
        self._input_ws = input_ws
        self._table = FindEPP(input_ws, OutputWorkspace="table")
        AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0',
                     LogType='Number', LogUnit='Angstrom')
        # These ranges correspond to 6*FWHM of the gaussian above,
        # the integration ranges of ComputeCalibrationCoefVan.
        self._lowerBoundRange = slice(28, 73)
        self._upperBoundRange = slice(27, 74)

    def test_output(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Output = Vanadium ws
        self.assertEqual(wsoutput.getRun().getLogData('run_title').value,
                         self._input_ws.getRun().getLogData('run_title').value)

        # Size of output workspace
        self.assertEqual(wsoutput.getNumberHistograms(),
                         self._input_ws.getNumberHistograms())

        DeleteWorkspace(wsoutput)
        return

    def test_sum(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Check whether the sum is calculated correctly, for theta=0, dwf=1
        # The result should be somewhere between the full bin sums.
        y_sumMin = np.sum(self._input_ws.readY(0)[self._lowerBoundRange])
        y_sumMax = np.sum(self._input_ws.readY(0)[self._upperBoundRange])
        e_sumMin = np.sqrt(np.sum(np.square(self._input_ws.readE(0)[self._lowerBoundRange])))
        e_sumMax = np.sqrt(np.sum(np.square(self._input_ws.readE(0)[self._upperBoundRange])))
        self.assertLess(y_sumMin, wsoutput.readY(0)[0])
        self.assertGreater(y_sumMax, wsoutput.readY(0)[0])
        self.assertLess(e_sumMin, wsoutput.readE(0)[0])
        self.assertGreater(e_sumMax, wsoutput.readE(0)[0])

        DeleteWorkspace(wsoutput)

    def test_dwf_using_default_temperature(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 293.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_from_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 0.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_input_overrides_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 567.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName,
                                 Temperature=0.0)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_input_not_modified(self):
        backup = CloneWorkspace(self._input_ws)
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        self.assertTrue(CompareWorkspaces(backup, self._input_ws)[0])
        DeleteWorkspace(backup)

    def tearDown(self):
        if AnalysisDataService.doesExist(self._input_ws.name()):
            DeleteWorkspace(self._input_ws)

        if AnalysisDataService.doesExist(self._table.name()):
            DeleteWorkspace(self._table)

    def _checkDWF(self, wsoutput, temperature):
        if temperature == 0.0:
            integral = 0.5
        elif temperature == 293.0:
            integral = 4.736767162094296 / 3.0
        else:
            raise RuntimeError("Unsupported temperature supplied to " +
                               "_checkDWF(). Use 0K or 293K only.")
        y_sumMin = np.sum(self._input_ws.readY(1)[self._lowerBoundRange])
        y_sumMax = np.sum(self._input_ws.readY(1)[self._upperBoundRange])
        e_sumMin = np.sqrt(np.sum(np.square(self._input_ws.readE(1)[self._lowerBoundRange])))
        e_sumMax = np.sqrt(np.sum(np.square(self._input_ws.readE(1)[self._upperBoundRange])))
        mvan = 0.001*50.942/N_A
        Bcoef = 3.0*integral*1e+20*hbar*hbar/(2.0*mvan*k*389.0)
        dwf = np.exp(
            -1.0*Bcoef*(4.0*np.pi*np.sin(0.5*np.radians(15.0))/4.0)**2)
        self.assertLess(y_sumMin/dwf, wsoutput.readY(1)[0])
        self.assertGreater(y_sumMax/dwf, wsoutput.readY(1)[0])
        self.assertLess(e_sumMin/dwf, wsoutput.readE(1)[0])
        self.assertGreater(e_sumMax/dwf, wsoutput.readE(1)[0])
class ComputeCalibrationCoefVanTest(unittest.TestCase):
    def setUp(self):
        input_ws = CreateSampleWorkspace(
            Function="User Defined",
            UserDefinedFunction="name=LinearBackground, " +
            "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
            NumBanks=2,
            BankPixelWidth=1,
            XMin=0,
            XMax=10,
            BinWidth=0.1,
            BankDistanceFromSample=4.0)
        self._input_ws = input_ws
        self._table = FindEPP(input_ws, OutputWorkspace="table")
        AddSampleLog(self._input_ws,
                     LogName='wavelength',
                     LogText='4.0',
                     LogType='Number',
                     LogUnit='Angstrom')
        for i in range(input_ws.getNumberHistograms()):
            y = input_ws.dataY(i)
            y.fill(0.)
            y[51] = 100.
            e = input_ws.dataE(i)
            e.fill(0.)
            e[51] = 10.

    def test_output(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Output = Vanadium ws
        self.assertEqual(wsoutput.getRun().getLogData('run_title').value,
                         self._input_ws.getRun().getLogData('run_title').value)

        # Size of output workspace
        self.assertEqual(wsoutput.getNumberHistograms(),
                         self._input_ws.getNumberHistograms())

        DeleteWorkspace(wsoutput)
        return

    def test_sum(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        for i in range(wsoutput.getNumberHistograms()):
            self.assertEqual(100., wsoutput.readY(i)[0])
            self.assertEqual(10., wsoutput.readE(i)[0])

        DeleteWorkspace(wsoutput)

    def test_dwf_using_default_temperature(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 293.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_from_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 0.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_log_is_time_series(self):
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        AddTimeSeriesLog(self._input_ws,
                         'temperature',
                         '2010-09-14T04:20:12',
                         Value='0.0')
        AddTimeSeriesLog(self._input_ws,
                         'temperature',
                         '2010-09-14T04:20:13',
                         Value='0.0')
        AddTimeSeriesLog(self._input_ws,
                         'temperature',
                         '2010-09-14T04:20:14',
                         Value='0.0')
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

    def test_temperature_log_name_from_IPF(self):
        self._input_ws.mutableRun().addProperty('sample.temperature', 0.0,
                                                True)
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        SetInstrumentParameter(Workspace=self._input_ws,
                               ParameterName="temperature_log_entry",
                               ParameterType="String",
                               Value="sample.temperature")
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.)

    def test_temperature_input_overrides_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 567.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName,
                                 Temperature=0.0)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_input_not_modified(self):
        backup = CloneWorkspace(self._input_ws)
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        self.assertTrue(CompareWorkspaces(backup, self._input_ws)[0])
        DeleteWorkspace(backup)

    def test_disabled_debye_waller_correction(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName,
                                 EnableDWF=False)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)
        for i in range(wsoutput.getNumberHistograms()):
            self.assertEqual(100., wsoutput.readY(i)[0])
            self.assertEqual(10., wsoutput.readE(i)[0])

        DeleteWorkspace(wsoutput)

    def tearDown(self):
        if AnalysisDataService.doesExist(self._input_ws.name()):
            DeleteWorkspace(self._input_ws)

        if AnalysisDataService.doesExist(self._table.name()):
            DeleteWorkspace(self._table)

    def _checkDWF(self, wsoutput, temperature):
        self.assertEqual(100., wsoutput.readY(0)[0])
        self.assertEqual(10., wsoutput.readE(0)[0])
        if temperature == 0.0:
            integral = 0.5
        elif temperature == 293.0:
            integral = 4.736767162094296 / 3.0
        else:
            raise RuntimeError("Unsupported temperature supplied to " +
                               "_checkDWF(). Use 0K or 293K only.")
        mvan = 0.001 * 50.942 / N_A
        Bcoef = 3.0 * integral * 1e+20 * hbar * hbar / (2.0 * mvan * k * 389.0)
        dwf = np.exp(-1.0 * Bcoef *
                     (4.0 * np.pi * np.sin(0.5 * np.radians(15.0)) / 4.0)**2)
        self.assertEqual(100. / dwf, wsoutput.readY(1)[0])
        self.assertEqual(10. / dwf, wsoutput.readE(1)[0])
class ComputeCalibrationCoefVanTest(unittest.TestCase):
    def setUp(self):
        input_ws = CreateSampleWorkspace(
            Function="User Defined",
            UserDefinedFunction="name=LinearBackground, A0=0.3;name=Gaussian, \
                                         PeakCentre=5, Height=10, Sigma=0.3",
            NumBanks=2,
            BankPixelWidth=1,
            XMin=0,
            XMax=10,
            BinWidth=0.1,
            BankDistanceFromSample=4.0)
        self._input_ws = input_ws
        self._table = FindEPP(input_ws, OutputWorkspace="table")
        AddSampleLog(self._input_ws,
                     LogName='wavelength',
                     LogText='4.0',
                     LogType='Number',
                     LogUnit='Angstrom')

    def test_output(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Output = Vanadium ws
        self.assertEqual(wsoutput.getRun().getLogData('run_title').value,
                         self._input_ws.getRun().getLogData('run_title').value)

        # Size of output workspace
        self.assertEqual(wsoutput.getNumberHistograms(),
                         self._input_ws.getNumberHistograms())

        DeleteWorkspace(wsoutput)
        return

    def test_sum(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # check whether sum is calculated correctly, for theta=0, dwf=1
        y_sum = sum(self._input_ws.readY(0)[27:75])
        self.assertAlmostEqual(y_sum, wsoutput.readY(0)[0])

        DeleteWorkspace(wsoutput)

    def test_dwf(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws,
                               L2="4,8",
                               Polar="0,15",
                               Azimuthal="0,0",
                               DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # check dwf calculation
        y_sum = sum(self._input_ws.readY(1)[27:75])
        mvan = 0.001 * 50.942 / N_A
        Bcoef = 4.736767162094296 * 1e+20 * hbar * hbar / (2.0 * mvan * k *
                                                           389.0)
        dwf = np.exp(-1.0 * Bcoef *
                     (4.0 * np.pi * np.sin(0.5 * np.radians(15.0)) / 4.0)**2)
        self.assertAlmostEqual(y_sum * dwf, wsoutput.readY(1)[0])

        DeleteWorkspace(wsoutput)

    def test_input_not_modified(self):
        backup = CloneWorkspace(self._input_ws)
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        self.assertEqual("Success!",
                         CheckWorkspacesMatch(backup, self._input_ws))
        DeleteWorkspace(backup)

    def tearDown(self):
        if AnalysisDataService.doesExist(self._input_ws.getName()):
            DeleteWorkspace(self._input_ws)

        if AnalysisDataService.doesExist(self._table.getName()):
            DeleteWorkspace(self._table)
class ComputeCalibrationCoefVanTest(unittest.TestCase):
    def setUp(self):
        input_ws = CreateSampleWorkspace(Function="User Defined",
                                         UserDefinedFunction="name=LinearBackground, A0=0.3;name=Gaussian, \
                                         PeakCentre=5, Height=10, Sigma=0.3", NumBanks=2, BankPixelWidth=1,
                                         XMin=0, XMax=10, BinWidth=0.1, BankDistanceFromSample=4.0)
        self._input_ws = input_ws
        self._table = FindEPP(input_ws, OutputWorkspace="table")
        AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0', LogType='Number', LogUnit='Angstrom')

    def test_output(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan", VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table, OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Output = Vanadium ws
        self.assertEqual(wsoutput.getRun().getLogData('run_title').value,
                         self._input_ws.getRun().getLogData('run_title').value)

        # Size of output workspace
        self.assertEqual(wsoutput.getNumberHistograms(), self._input_ws.getNumberHistograms())

        DeleteWorkspace(wsoutput)
        return

    def test_sum(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan", VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table, OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # check whether sum is calculated correctly, for theta=0, dwf=1
        y_sum = sum(self._input_ws.readY(0)[27:75])
        self.assertAlmostEqual(y_sum, wsoutput.readY(0)[0])

        DeleteWorkspace(wsoutput)

    def test_dwf(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15", Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan", VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table, OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # check dwf calculation
        y_sum = sum(self._input_ws.readY(1)[27:75])
        mvan = 0.001*50.942/N_A
        Bcoef = 4.736767162094296*1e+20*hbar*hbar/(2.0*mvan*k*389.0)
        dwf = np.exp(-1.0*Bcoef*(4.0*np.pi*np.sin(0.5*np.radians(15.0))/4.0)**2)
        self.assertAlmostEqual(y_sum*dwf, wsoutput.readY(1)[0])

        DeleteWorkspace(wsoutput)

    def test_input_not_modified(self):
        backup = CloneWorkspace(self._input_ws)
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan", VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table, OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        self.assertEqual("Success!", CheckWorkspacesMatch(backup, self._input_ws))
        DeleteWorkspace(backup)

    def tearDown(self):
        if AnalysisDataService.doesExist(self._input_ws.getName()):
            DeleteWorkspace(self._input_ws)

        if AnalysisDataService.doesExist(self._table.getName()):
            DeleteWorkspace(self._table)
class ComputeCalibrationCoefVanTest(unittest.TestCase):
    def setUp(self):
        input_ws = CreateSampleWorkspace(
            Function="User Defined",
            UserDefinedFunction="name=LinearBackground, " +
            "A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.3",
            NumBanks=2, BankPixelWidth=1, XMin=0, XMax=10, BinWidth=0.1,
            BankDistanceFromSample=4.0)
        self._input_ws = input_ws
        self._table = FindEPP(input_ws, OutputWorkspace="table")
        AddSampleLog(self._input_ws, LogName='wavelength', LogText='4.0',
                     LogType='Number', LogUnit='Angstrom')
        for i in range(input_ws.getNumberHistograms()):
            y = input_ws.dataY(i)
            y.fill(0.)
            y[51] = 100.
            e = input_ws.dataE(i)
            e.fill(0.)
            e[51] = 10.

    def test_output(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        # Output = Vanadium ws
        self.assertEqual(wsoutput.getRun().getLogData('run_title').value,
                         self._input_ws.getRun().getLogData('run_title').value)

        # Size of output workspace
        self.assertEqual(wsoutput.getNumberHistograms(),
                         self._input_ws.getNumberHistograms())

        DeleteWorkspace(wsoutput)
        return

    def test_sum(self):
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        for i in range(wsoutput.getNumberHistograms()):
            self.assertEqual(100., wsoutput.readY(i)[0])
            self.assertEqual(10., wsoutput.readE(i)[0])

        DeleteWorkspace(wsoutput)

    def test_dwf_using_default_temperature(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 293.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_from_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 0.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_temperature_log_is_time_series(self):
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        AddTimeSeriesLog(
            self._input_ws,
            'temperature',
            '2010-09-14T04:20:12',
            Value='0.0')
        AddTimeSeriesLog(
            self._input_ws,
            'temperature',
            '2010-09-14T04:20:13',
            Value='0.0')
        AddTimeSeriesLog(
            self._input_ws,
            'temperature',
            '2010-09-14T04:20:14',
            Value='0.0')
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

    def test_temperature_log_name_from_IPF(self):
        self._input_ws.mutableRun().addProperty('sample.temperature', 0.0, True)
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        SetInstrumentParameter(
            Workspace=self._input_ws,
            ParameterName="temperature_log_entry",
            ParameterType="String",
            Value="sample.temperature")
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.)

    def test_temperature_input_overrides_sample_log(self):
        self._input_ws.mutableRun().addProperty('temperature', 567.0, True)
        outputWorkspaceName = "output_ws"
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName,
                                 Temperature=0.0)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)

        self._checkDWF(wsoutput, 0.0)

        DeleteWorkspace(wsoutput)

    def test_input_not_modified(self):
        backup = CloneWorkspace(self._input_ws)
        outputWorkspaceName = "output_ws"
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName)
        self.assertTrue(alg_test.isExecuted())
        self.assertTrue(CompareWorkspaces(backup, self._input_ws)[0])
        DeleteWorkspace(backup)

    def test_disabled_debye_waller_correction(self):
        outputWorkspaceName = "output_ws"

        # change theta to make dwf != 1
        EditInstrumentGeometry(self._input_ws, L2="4,8", Polar="0,15",
                               Azimuthal="0,0", DetectorIDs="1,2")
        alg_test = run_algorithm("ComputeCalibrationCoefVan",
                                 VanadiumWorkspace=self._input_ws,
                                 EPPTable=self._table,
                                 OutputWorkspace=outputWorkspaceName,
                                 EnableDWF=False)
        self.assertTrue(alg_test.isExecuted())
        wsoutput = AnalysisDataService.retrieve(outputWorkspaceName)
        for i in range(wsoutput.getNumberHistograms()):
            self.assertEqual(100., wsoutput.readY(i)[0])
            self.assertEqual(10., wsoutput.readE(i)[0])

        DeleteWorkspace(wsoutput)

    def tearDown(self):
        if AnalysisDataService.doesExist(self._input_ws.name()):
            DeleteWorkspace(self._input_ws)

        if AnalysisDataService.doesExist(self._table.name()):
            DeleteWorkspace(self._table)

    def _checkDWF(self, wsoutput, temperature):
        self.assertEqual(100., wsoutput.readY(0)[0])
        self.assertEqual(10., wsoutput.readE(0)[0])
        if temperature == 0.0:
            integral = 0.5
        elif temperature == 293.0:
            integral = 4.736767162094296 / 3.0
        else:
            raise RuntimeError("Unsupported temperature supplied to " +
                               "_checkDWF(). Use 0K or 293K only.")
        mvan = 0.001*50.942/N_A
        Bcoef = 3.0*integral*1e+20*hbar*hbar/(2.0*mvan*k*389.0)
        dwf = np.exp(
            -1.0*Bcoef*(4.0*np.pi*np.sin(0.5*np.radians(15.0))/4.0)**2)
        self.assertEqual(100./dwf, wsoutput.readY(1)[0])
        self.assertEqual(10./dwf, wsoutput.readE(1)[0])