def runTest(self): ws = DirectILLCollectData('ILL/IN6/164192.nxs', ElasticChannel='Default Elastic Channel', FlatBkg='Flat Bkg OFF', Normalisation='Normalisation OFF') for i in range(ws.getNumberHistograms()): ws.dataY(i).fill(1) dE = ConvertUnits(ws, 'DeltaE', 'Direct') corr = DetectorEfficiencyCorUser(dE) Ei = corr.run().get('Ei').value def det_corr(x): high = x > 5.113 low = x <= 5.113 c = numpy.empty_like(x) c[high] = 0.94 * (1. - numpy.exp(-3.284 / numpy.sqrt(x[high]))) c[low] = numpy.exp(-0.0565 / numpy.sqrt(x[low])) * ( 1. - numpy.exp(-3.284 / numpy.sqrt(x[low]))) return c corr_at_Ei = det_corr(numpy.array([Ei]))[0] for i in range(corr.getNumberHistograms()): x = (corr.readX(i)[:-1] + corr.readX(i)[1:]) / 2. e = Ei - x assert_almost_equal(corr.readY(i), corr_at_Ei / det_corr(e))
def test_power_transform_all_negative(self): gamma = 3 scale = PowerScale(None, gamma=gamma) x = np.linspace(-10, 0) transform = scale.get_transform() testhelpers.assert_almost_equal(np.power(x, gamma), transform.transform_non_affine(x))
def testNormalisationToTimeWhenMonitorCountsAreTooLow(self): outWSName = 'outWS' duration = 3612.3 logs = mtd[self._TEST_WS_NAME].mutableRun() logs.addProperty('duration', duration, True) monsum = 10 logs.addProperty('monitor.monsum', monsum, True) algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'FlatBkg': 'Flat Bkg OFF', 'IncidentEnergyCalibration': 'Energy Calibration OFF', 'Normalisation': 'Normalisation Monitor', 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] inWS = mtd[self._TEST_WS_NAME] ys = outWS.extractY() originalYs = inWS.extractY() assert_almost_equal(ys, originalYs[:-1, :] / duration) es = outWS.extractE() originalEs = inWS.extractE() assert_almost_equal(es, originalEs[:-1, :] / duration)
def _backgroundSubtraction(self, subtractionType): inWSName = 'ReflectometryILLPreprocess_test_ws' self.create_sample_workspace(inWSName) # Add a peak to the sample workspace. ws = mtd[inWSName] ys = ws.dataY(49) ys += 10.0 args = { 'InputWorkspace': inWSName, 'OutputWorkspace': 'unused_for_child', 'LinePosition': 49, 'FluxNormalisation': 'Normalisation OFF', 'FlatBackground': subtractionType, 'rethrow': True, 'child': True } alg = create_algorithm('ReflectometryILLPreprocess', **args) assertRaisesNothing(self, alg.execute) outWS = alg.getProperty('OutputWorkspace').value self.assertEqual(outWS.getNumberHistograms(), 100) ysSize = outWS.blocksize() for i in range(outWS.getNumberHistograms()): ys = outWS.readY(i) if i != 49: assert_almost_equal(ys, [0.0] * ysSize) else: assert_almost_equal(ys, [10.0] * ysSize) self.assertEqual(mtd.getObjectNames(), ['ReflectometryILLPreprocess_test_ws'])
def runTest(self): ws = DirectILLCollectData('ILL/IN4/084446.nxs', ElasticChannel='Default Elastic Channel', FlatBkg='Flat Bkg OFF', Normalisation='Normalisation OFF') for i in range(ws.getNumberHistograms()): ws.dataY(i).fill(1) dE = ConvertUnits(ws, 'DeltaE', 'Direct') corr = DetectorEfficiencyCorUser(dE) Ei = corr.run().get('Ei').value def rosace_corr(x): return 1.0 - numpy.exp(-6.1343 / numpy.sqrt(x)) def wide_angle_corr(x): return 0.951 * numpy.exp(-0.0887 / numpy.sqrt(x)) * (1 - numpy.exp(-5.597 / numpy.sqrt(x))) def eff_factor(x, corr_func): return corr_func(Ei) / corr_func(x) # Wide-angle detectors are at ws indices 0-299 for i in range(0, 300): x = (corr.readX(i)[:-1] + corr.readX(i)[1:]) / 2. e = Ei - x assert_almost_equal(corr.readY(i), eff_factor(e, wide_angle_corr)) # Rosace detectors are at ws indices 300-395 for i in range(300, 396): x = (corr.readX(i)[:-1] + corr.readX(i)[1:]) / 2. e = Ei - x assert_almost_equal(corr.readY(i), eff_factor(e, rosace_corr))
def testNoOperationClonesInputWorkspace(self): ws = self._cloneTestWorkspace() outWSName = 'outWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'rethrow': True } run_algorithm('DirectILLApplySelfShielding', **algProperties) # If the previous run didn't clone the input workspace, the two later # calls will be triggered to use 'outWS' as the input. self.assertTrue(mtd.doesExist(outWSName)) corrFactor = 0.43 corrWS = self._cloneTestWorkspace('correctionWS') for i in range(corrWS.getNumberHistograms()): ys = corrWS.dataY(i) ys.fill(corrFactor) es = corrWS.dataE(i) es.fill(0) algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'SelfShieldingCorrectionWorkspace': corrWS, 'rethrow': True } run_algorithm('DirectILLApplySelfShielding', **algProperties) run_algorithm('DirectILLApplySelfShielding', **algProperties) outWS = mtd[outWSName] self.assertEqual(outWS.getNumberHistograms(), ws.getNumberHistograms()) ys = outWS.extractY() originalYs = ws.extractY() assert_almost_equal(ys, originalYs / corrFactor) es = outWS.extractE() originalEs = ws.extractE() assert_almost_equal(es, originalEs / corrFactor)
def testSelfShieldingCorrections(self): ws = self._cloneTestWorkspace() corrFactor = 0.789 corrWS = self._cloneTestWorkspace('correctionWS') for i in range(corrWS.getNumberHistograms()): ys = corrWS.dataY(i) ys.fill(corrFactor) es = corrWS.dataE(i) es.fill(0) outWSName = 'outWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'SelfShieldingCorrectionWorkspace': corrWS, 'rethrow': True } run_algorithm('DirectILLApplySelfShielding', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] self.assertEqual(outWS.getNumberHistograms(), ws.getNumberHistograms()) ys = outWS.extractY() originalYs = ws.extractY() assert_almost_equal(ys, originalYs / corrFactor) es = outWS.extractE() originalEs = ws.extractE() assert_almost_equal(es, originalEs / corrFactor)
def test_get_spectrum_list_multi_ion_and_spectra(self): params = { 'ion0.B20': 0.37737, 'ion0.B22': 3.9770, 'ion0.B40': -0.031787, 'ion0.B42': -0.11611, 'ion0.B44': -0.12544, 'ion1.B20': 0.37737, 'ion1.B22': 3.9770, 'ion1.B40': -0.031787, 'ion1.B42': -0.11611, 'ion1.B44': -0.12544 } cfms = CrystalFieldMultiSite(Ions=['Ce', 'Pr'], Symmetries=['C2v', 'C2v'], Temperatures=[44.0, 50.0], FWHM=[1.1, 1.2], parameters=params) r = [0.0, 1.45, 2.4, 3.0, 3.85] x, y = cfms.getSpectrum(0, r) y = y / c_mbsr expected_y = [3.904037, 0.744519, 0.274897, 0.175713, 0.106540] np.testing.assert_equal(x, r) testhelpers.assert_almost_equal(y, expected_y, 6) x, y = cfms.getSpectrum(1, r) y = y / c_mbsr expected_y = [3.704726, 0.785600, 0.296255, 0.190176, 0.115650] np.testing.assert_equal(x, r) testhelpers.assert_almost_equal(y, expected_y, 6)
def test_power_inverse_transform_all_positive(self): gamma = 3 scale = PowerScale(None, gamma=gamma) x = np.linspace(0, 10) inv_transform = scale.get_transform().inverted() testhelpers.assert_almost_equal(np.power(x, 1. / gamma), inv_transform.transform_non_affine(x))
def test_power_inverse_transform_all_negative(self): gamma = 3 scale = PowerScale(None, gamma=gamma) x = np.linspace(-10, 0) expected = np.negative(np.power(np.negative(x), 1. / gamma)) inv_transform = scale.get_transform().inverted() testhelpers.assert_almost_equal(expected, inv_transform.transform_non_affine(x))
def test_power_inverse_transform_mix_positive_negative(self): gamma = 3 scale = PowerScale(None, gamma=gamma) x = np.linspace(-5, 5) negative_pos = (x < 0.0) expected = np.copy(x) np.negative(x, where=negative_pos, out=expected) expected = np.power(expected, 1. / gamma) np.negative(expected, where=negative_pos, out=expected) inv_transform = scale.get_transform().inverted() testhelpers.assert_almost_equal(expected, inv_transform.transform_non_affine(x))
def _test_toggle_normalization(self, errorbars_on, plot_kwargs): fig = plot([self.ws], spectrum_nums=[1], errors=errorbars_on, plot_kwargs=plot_kwargs) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) # Earlier versions of matplotlib do not store the data assciated with a # line with high precision and hence we need to set a lower tolerance # when making comparisons of this data if matplotlib.__version__ < "2": decimal_tol = 1 else: decimal_tol = 7 ax = fig.axes[0] fig_interactor._toggle_normalization(ax) assert_almost_equal(ax.lines[0].get_xdata(), [15, 25]) assert_almost_equal(ax.lines[0].get_ydata(), [0.2, 0.3], decimal=decimal_tol) self.assertEqual("Counts ($\\AA$)$^{-1}$", ax.get_ylabel()) fig_interactor._toggle_normalization(ax) assert_almost_equal(ax.lines[0].get_xdata(), [15, 25]) assert_almost_equal(ax.lines[0].get_ydata(), [2, 3], decimal=decimal_tol) self.assertEqual("Counts", ax.get_ylabel())
def testTOF(self): CalculateFlux(InputWorkspace="ws", BeamRadius=0.05, OutputWorkspace="flux") self.assertTrue(mtd["flux"]) self.assertEqual(mtd["flux"].getNumberHistograms(), 1) self.assertEqual(mtd["flux"].blocksize(), mtd["ws"].blocksize()) self.assertEqual(mtd["flux"].getAxis(0).getUnit().unitID(), "Wavelength") expectation = np.empty(100) expectation.fill(self.pixels_in_shape * 0.3) expectation[50] = self.pixels_in_shape * 10.3 reality = mtd["flux"].readY(0) testhelpers.assert_almost_equal(reality, expectation, decimal=6)
def testOutputHasCommonBinningWithInput(self): self._setDefaultSample(self._TEST_WS_NAME) outWSName = 'correctionWS' kwargs = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'rethrow': True } run_algorithm('DirectILLSelfShielding', **kwargs) self.assertTrue(mtd.doesExist(outWSName)) inWS = mtd[self._TEST_WS_NAME] outWS = mtd[outWSName] self.assertEqual(outWS.getNumberHistograms(), inWS.getNumberHistograms()) xs = outWS.extractX() originalXs = inWS.extractX() assert_almost_equal(xs, originalXs[:, :])
def testIncidentEnergyPanther(self): outWSName = 'outWS' eiWSName = 'Ei' algProperties = { 'Run': 'ILL/PANTHER/002687.nxs', 'OutputWorkspace': outWSName, 'IncidentEnergyCalibration': 'Energy Calibration ON', 'OutputIncidentEnergyWorkspace': eiWSName, 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(eiWSName)) eiWS = mtd[eiWSName] outWS = mtd[outWSName] E_i = outWS.run().getProperty('Ei').value assert_almost_equal(eiWS.readY(0)[0], E_i, 2) assert_almost_equal(E_i, 75.37, 2)
def runTest(self): ws = DirectILLCollectData('ILL/IN5/104007.nxs', ElasticChannel='Default Elastic Channel', FlatBkg='Flat Bkg OFF', Normalisation='Normalisation OFF') for i in range(ws.getNumberHistograms()): ws.dataY(i).fill(1) dE = ConvertUnits(ws, 'DeltaE', 'Direct') corr = DetectorEfficiencyCorUser(dE) Ei = corr.run().get('Ei').value def tube_corr(x): return 1. - numpy.exp(-5.6 / numpy.sqrt(x)) corr_at_Ei = tube_corr(Ei) for i in range(corr.getNumberHistograms()): x = (corr.readX(i)[:-1] + corr.readX(i)[1:]) / 2. e = Ei - x assert_almost_equal(corr.readY(i), corr_at_Ei / tube_corr(e))
def testBackgroundOutput(self): outWSName = 'outWS' outBkgWSName = 'outBkg' bkgScaling = 0.33 # Output should not be scaled, actually. algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'FlatBkg': 'Flat Bkg ON', 'FlatBkgScaling': bkgScaling, 'IncidentEnergyCalibration': 'Energy Calibration OFF', 'Normalisation': 'Normalisation OFF', 'OutputFlatBkgWorkspace': outBkgWSName, 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(outBkgWSName)) outBkgWS = mtd[outBkgWSName] assert_almost_equal(outBkgWS.extractY(), self._BKG_LEVEL)
def testQRebinning(self): _add_natural_angle_step_parameter(self._TEST_WS_NAME) outWSName = 'outWS' Q0 = 2.3 dQ = 0.1 Q1 = 2.7 algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'QBinningParams': [Q0, dQ, Q1], 'rethrow': True } run_algorithm('DirectILLReduction', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) ws = mtd[outWSName] self.assertEqual(ws.getAxis(0).getUnit().unitID(), 'MomentumTransfer') xs = ws.readX(0) assert_almost_equal(xs, numpy.arange(Q0, Q1, dQ))
def testHybridERebinningSingleUserRange(self): outWSName = 'outWS' E0 = -2. dE = 0.13 E1 = E0 + 40 * dE algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'EnergyRebinning': '{},{},{}'.format(E0, dE, E1), 'Transposing': 'Transposing OFF', 'rethrow': True } run_algorithm('DirectILLReduction', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) ws = mtd[outWSName] self.assertEqual(ws.getAxis(0).getUnit().unitID(), 'DeltaE') xs = ws.readX(0) assert_almost_equal(xs, numpy.arange(E0, E1 + 0.01, dE))
def test_get_spectrum_from_list(self): cfms = CrystalFieldMultiSite(Ions=['Ce'], Symmetries=['C2v'], Temperatures=[4.0], FWHM=[0.1], B20=0.035, B40=-0.012, B43=-0.027, B60=-0.00012, B63=0.0025, B66=0.0068, ToleranceIntensity=0.001 * c_mbsr) r = [0.0, 1.45, 2.4, 3.0, 3.85] x, y = cfms.getSpectrum(r) y = y / c_mbsr expected_y = [12.474955, 1.190169, 0.122781, 0.042940, 10.837438] np.testing.assert_equal(x, r) testhelpers.assert_almost_equal(y, expected_y, 6)
def testQRebinningBinWidthOnly(self): _add_natural_angle_step_parameter(self._TEST_WS_NAME) outWSName = 'outWS' dQ = 0.1 algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'QBinningParams': [dQ], 'rethrow': True } run_algorithm('DirectILLReduction', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) ws = mtd[outWSName] self.assertEqual(ws.getAxis(0).getUnit().unitID(), 'MomentumTransfer') xs = ws.readX(0) self.assertGreater(len(xs), 3) dx = xs[1:] - xs[:-1] # Bin widths may differ at the edges. assert_almost_equal(dx[1:-1], 0.1)
def testERebinning(self): _add_natural_angle_step_parameter(self._TEST_WS_NAME) outWSName = 'outWS' E0 = -2. dE = 0.13 E1 = E0 + 40 * dE algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'EnergyRebinningParams': [E0, dE, E1], 'Transposing': 'Transposing OFF', 'rethrow': True } run_algorithm('DirectILLReduction', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) ws = mtd[outWSName] self.assertEqual(ws.getAxis(0).getUnit().unitID(), 'DeltaE') xs = ws.readX(0) assert_almost_equal(xs, numpy.arange(E0, E1 + 0.01, dE))
def testBackgroundSubtraction(self): outWSName = 'outWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'FlatBkg': 'Flat Bkg ON', 'FlatBkgScaling': 1.0, 'IncidentEnergyCalibration': 'Energy Calibration OFF', 'Normalisation': 'Normalisation OFF', 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] inWS = mtd[self._TEST_WS_NAME] self.assertEqual(outWS.getNumberHistograms(), inWS.getNumberHistograms() - 1) ys = outWS.extractY() originalYs = inWS.extractY() assert_almost_equal(ys, originalYs[:-1, :] - self._BKG_LEVEL)
def testExecSparseInstrument(self): self._setDefaultSample(self._TEST_WS_NAME) outWSName = 'correctionWS' kwargs = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'SimulationInstrument': 'Sparse Instrument', 'SparseInstrumentRows': 3, 'SparseInstrumentColumns': 2, 'rethrow': True } run_algorithm('DirectILLSelfShielding', **kwargs) self.assertTrue(mtd.doesExist(outWSName)) inWS = mtd[self._TEST_WS_NAME] outWS = mtd[outWSName] self.assertEqual(outWS.getNumberHistograms(), inWS.getNumberHistograms()) xs = outWS.extractX() originalXs = inWS.extractX() assert_almost_equal(xs, originalXs[:, :])
def testEmptyContainerSubtraction(self): ws = self._cloneTestWorkspace() ecWSName = 'testECWS_' ecWS = self._cloneTestWorkspace(ecWSName) ecFactor = 0.13 ecWS *= ecFactor outWSName = 'outWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'EmptyContainerWorkspace': ecWSName, 'rethrow': True } run_algorithm('DirectILLApplySelfShielding', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] self.assertEqual(outWS.getNumberHistograms(), ws.getNumberHistograms()) ys = outWS.extractY() originalYs = ws.extractY() assert_almost_equal(ys, (1.0 - ecFactor) * originalYs)
def testSuccessWhenEverythingDisabled(self): outWSName = 'outWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'FlatBkg': 'Flat Bkg OFF', 'IncidentEnergyCalibration': 'Energy Calibration OFF', 'Normalisation': 'Normalisation OFF', 'ElasticChannel': 'Default Elastic Channel', 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] inWS = mtd[self._TEST_WS_NAME] self.assertEqual(outWS.getNumberHistograms(), inWS.getNumberHistograms() - 1) xs = outWS.extractX() originalXs = inWS.extractX() assert_almost_equal(xs, originalXs[:-1, :]) ys = outWS.extractY() originalYs = inWS.extractY() assert_almost_equal(ys, originalYs[:-1, :]) es = outWS.extractE() originalEs = inWS.extractE() assert_almost_equal(es, originalEs[:-1, :])
def testRawWorkspaceOutput(self): outWSName = 'outWS' rawWSName = 'rawWS' algProperties = { 'InputWorkspace': self._TEST_WS_NAME, 'OutputWorkspace': outWSName, 'OutputRawWorkspace': rawWSName, 'rethrow': True } run_algorithm('DirectILLCollectData', **algProperties) self.assertTrue(mtd.doesExist(outWSName)) outWS = mtd[outWSName] inWS = mtd[self._TEST_WS_NAME] self.assertTrue(mtd.doesExist(rawWSName)) rawWS = mtd[rawWSName] ys = rawWS.extractY() originalYS = inWS.extractY() assert_almost_equal(ys, originalYS[:-1, :]) es = rawWS.extractE() originalES = inWS.extractE() assert_almost_equal(es, originalES[:-1, :]) xs = rawWS.extractX() outXS = outWS.extractX() assert_almost_equal(xs, outXS) Ei = rawWS.getRun().getProperty('Ei').value outEi = outWS.getRun().getProperty('Ei').value self.assertEqual(Ei, outEi) wavelength = outWS.getRun().getProperty('wavelength').value outWavelength = outWS.getRun().getProperty('wavelength').value self.assertEqual(wavelength, outWavelength)
def test_get_spectrum_from_list_multi_spectra(self): cfms = CrystalFieldMultiSite(Ions=['Ce'], Symmetries=['C2v'], Temperatures=[4.0, 50.0], FWHM=[0.1, 0.2], B20=0.035, B40=-0.012, B43=-0.027, B60=-0.00012, B63=0.0025, B66=0.0068) r = [0.0, 1.45, 2.4, 3.0, 3.85] x, y = cfms.getSpectrum(0, r) y = y / c_mbsr expected_y = [12.474946, 1.190160, 0.122785, 0.042940, 10.837170] np.testing.assert_equal(x, r) testhelpers.assert_almost_equal(y, expected_y, 6) x, y = cfms.getSpectrum(1, r) y = y / c_mbsr expected_y = [6.304662, 0.331218, 1.224681, 0.078540, 2.638049] np.testing.assert_equal(x, r) testhelpers.assert_almost_equal(y, expected_y, 6)
def test_square_inverse_transform(self): scale = SquareScale(None) x = np.linspace(0, 10, 1) inv_transform = scale.get_transform().inverted() testhelpers.assert_almost_equal(np.sqrt(x), inv_transform.transform_non_affine(x))
def test_square_transform(self): scale = SquareScale(None) x = np.linspace(0, 10, 1) transform = scale.get_transform() testhelpers.assert_almost_equal(np.power(x, 2), transform.transform_non_affine(x))