/Framework/PythonInterface/test/python/plugins/algorithms/WorkflowAlgorithms/ApplyPaalmanPingsCorrectionTest.py

https://github.com/wdzhou/mantid · Python · 195 lines · 131 code · 41 blank · 23 comment · 5 complexity · c35bcacb48cef2ab89b2f0fb5a918af7 MD5 · raw file

  1. from __future__ import (absolute_import, division, print_function)
  2. import unittest
  3. from mantid.kernel import *
  4. from mantid.api import *
  5. from mantid.simpleapi import (CreateWorkspace, Load, ConvertUnits,
  6. SplineInterpolation, ApplyPaalmanPingsCorrection,
  7. DeleteWorkspace)
  8. import numpy
  9. class ApplyPaalmanPingsCorrectionTest(unittest.TestCase):
  10. def setUp(self):
  11. """
  12. Create sample workspaces.
  13. """
  14. # Load the sample and can
  15. sample_ws = Load('irs26176_graphite002_red.nxs')
  16. can_ws = Load('irs26173_graphite002_red.nxs')
  17. # Convert sample and can to wavelength
  18. sample_ws = ConvertUnits(InputWorkspace=sample_ws,
  19. Target='Wavelength',
  20. EMode='Indirect',
  21. EFixed=1.845)
  22. can_ws = ConvertUnits(InputWorkspace=can_ws,
  23. Target='Wavelength',
  24. EMode='Indirect',
  25. EFixed=1.845)
  26. self._sample_ws = sample_ws
  27. self._can_ws = can_ws
  28. # Load the corrections workspace
  29. corrections = Load('irs26176_graphite002_cyl_Abs.nxs')
  30. # Interpolate each of the correction factor workspaces
  31. # Required to use corrections from the old indirect calculate
  32. # corrections routines
  33. for factor_ws in corrections:
  34. SplineInterpolation(WorkspaceToMatch=sample_ws,
  35. WorkspaceToInterpolate=factor_ws,
  36. OutputWorkspace=factor_ws,
  37. OutputWorkspaceDeriv='')
  38. self._corrections_ws = corrections
  39. def tearDown(self):
  40. """
  41. Remove workspaces from ADS.
  42. """
  43. DeleteWorkspace(self._sample_ws)
  44. DeleteWorkspace(mtd['can_ws'])
  45. DeleteWorkspace(self._corrections_ws)
  46. def _verify_workspace(self, ws, correction_type):
  47. """
  48. Do validation on a correction workspace.
  49. @param ws Workspace to validate
  50. @param correction_type Type of correction that should have been applied
  51. """
  52. # X axis should be in wavelength
  53. x_unit = ws.getAxis(0).getUnit().unitID()
  54. self.assertEquals(x_unit, 'Wavelength')
  55. # Sample logs should contain correction type
  56. logs = ws.getSampleDetails()
  57. self.assertTrue('corrections_type' in logs)
  58. # Ensure value from sample log is correct
  59. if 'corrections_type' in logs:
  60. log_correction_type = logs['corrections_type'].value
  61. self.assertEqual(log_correction_type, correction_type)
  62. def test_can_subtraction(self):
  63. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  64. CanWorkspace=self._can_ws)
  65. self._verify_workspace(corr, 'can_subtraction')
  66. def test_can_subtraction_with_can_scale(self):
  67. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  68. CanWorkspace=self._can_ws,
  69. CanScaleFactor=0.9)
  70. self._verify_workspace(corr, 'can_subtraction')
  71. def test_can_subtraction_with_can_shift(self):
  72. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  73. CanWorkspace=self._can_ws,
  74. CanShiftFactor=0.03)
  75. self._verify_workspace(corr, 'can_subtraction')
  76. def test_sample_corrections_only(self):
  77. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  78. CorrectionsWorkspace=self._corrections_ws)
  79. self._verify_workspace(corr, 'sample_corrections_only')
  80. def test_sample_and_can_corrections(self):
  81. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  82. CorrectionsWorkspace=self._corrections_ws,
  83. CanWorkspace=self._can_ws)
  84. self._verify_workspace(corr, 'sample_and_can_corrections')
  85. def test_sample_and_can_corrections_with_can_scale(self):
  86. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  87. CorrectionsWorkspace=self._corrections_ws,
  88. CanWorkspace=self._can_ws,
  89. CanScaleFactor=0.9)
  90. self._verify_workspace(corr, 'sample_and_can_corrections')
  91. def test_sample_and_can_corrections_with_can_shift(self):
  92. corr = ApplyPaalmanPingsCorrection(SampleWorkspace=self._sample_ws,
  93. CorrectionsWorkspace=self._corrections_ws,
  94. CanWorkspace=self._can_ws,
  95. CanShiftFactor = 0.03)
  96. self._verify_workspace(corr, 'sample_and_can_corrections')
  97. def test_container_input_workspace_not_unintentionally_rebinned(self):
  98. xs = numpy.array([0.0, 1.0, 0.0, 1.1])
  99. ys = numpy.array([2.2, 3.3])
  100. sample_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  101. UnitX='Wavelength')
  102. ys = numpy.array([0.11, 0.22])
  103. container_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  104. UnitX='Wavelength')
  105. corrected = ApplyPaalmanPingsCorrection(SampleWorkspace=sample_1,
  106. CanWorkspace=container_1)
  107. numHisto = container_1.getNumberHistograms()
  108. for i in range(numHisto):
  109. container_xs = container_1.readX(i)
  110. for j in range(len(container_xs)):
  111. self.assertEqual(container_xs[j], xs[i * numHisto + j])
  112. DeleteWorkspace(sample_1)
  113. DeleteWorkspace(container_1)
  114. DeleteWorkspace(corrected)
  115. def test_container_rebinning_enabled(self):
  116. xs = numpy.array([0.0, 1.0, 0.0, 1.1])
  117. ys = numpy.array([2.2, 3.3])
  118. sample_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  119. UnitX='Wavelength')
  120. xs = numpy.array([-1.0, 0.0, 1.0, 2.0, -1.0, 0.0, 1.0, 2.0])
  121. ys = numpy.array([0.101, 0.102, 0.103, 0.104, 0.105, 0.106])
  122. container_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  123. UnitX='Wavelength')
  124. corrected = ApplyPaalmanPingsCorrection(SampleWorkspace=sample_1,
  125. CanWorkspace=container_1,
  126. RebinCanToSample=True)
  127. self.assertTrue(numpy.all(sample_1.extractY() > corrected.extractY()))
  128. DeleteWorkspace(sample_1)
  129. DeleteWorkspace(container_1)
  130. DeleteWorkspace(corrected)
  131. def test_container_rebinning_disabled(self):
  132. xs = numpy.array([0.0, 1.0, 0.0, 1.1])
  133. ys = numpy.array([2.2, 3.3])
  134. sample_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  135. UnitX='Wavelength')
  136. xs = numpy.array([-1.0, 0.0, 1.0, 2.0, -1.0, 0.0, 1.0, 2.0])
  137. ys = numpy.array([0.101, 0.102, 0.103, 0.104, 0.105, 0.106])
  138. container_1 = CreateWorkspace(DataX=xs, DataY=ys, NSpec=2,
  139. UnitX='Wavelength')
  140. corrected_ws_name = 'corrected_workspace'
  141. kwargs = {
  142. 'SampleWorkspace': sample_1,
  143. 'CanWorkspace': container_1,
  144. 'OutputWorkspaced': corrected_ws_name,
  145. 'RebinCanToSample': False
  146. }
  147. # The Minus algorithm will fail due to different bins in sample and
  148. # container.
  149. self.assertRaises(RuntimeError, ApplyPaalmanPingsCorrection, **kwargs)
  150. DeleteWorkspace(sample_1)
  151. DeleteWorkspace(container_1)
  152. if __name__=="__main__":
  153. unittest.main()