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

https://github.com/wdzhou/mantid · Python · 206 lines · 144 code · 50 blank · 12 comment · 1 complexity · 27caea78dca4683a38eafaaa4dc9a8dd MD5 · raw file

  1. from __future__ import (absolute_import, division, print_function)
  2. from mantid.simpleapi import (SimpleShapeMonteCarloAbsorption, Load, ConvertUnits,
  3. CompareWorkspaces, SetSampleMaterial, CreateSampleWorkspace,
  4. DeleteWorkspace)
  5. from mantid.kernel import *
  6. from mantid.api import *
  7. import unittest
  8. class SimpleShapeMonteCarloAbsorptionTest(unittest.TestCase):
  9. def setUp(self):
  10. red_ws = Load('irs26176_graphite002_red.nxs')
  11. red_ws = ConvertUnits(InputWorkspace=red_ws, Target='Wavelength', EMode='Indirect', EFixed=1.845)
  12. self._arguments = {'ChemicalFormula': 'H2-O',
  13. 'DensityType': 'Mass Density',
  14. 'Density': 1.0,
  15. 'EventsPerPoint': 200,
  16. 'BeamHeight': 3.5,
  17. 'BeamWidth': 4.0,
  18. 'Height': 2.0}
  19. self._red_ws = red_ws
  20. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  21. Shape='FlatPlate',
  22. Width=2.0,
  23. Thickness=2.0,
  24. **self._arguments)
  25. # store the basic flat plate workspace so it can be compared with others
  26. self._corrected_flat_plate = corrected
  27. def _test_corrections_workspace(self, corr_ws):
  28. x_unit = corr_ws.getAxis(0).getUnit().unitID()
  29. self.assertEquals(x_unit, 'Wavelength')
  30. y_unit = corr_ws.YUnitLabel()
  31. self.assertEquals(y_unit, 'Attenuation factor')
  32. num_hists = corr_ws.getNumberHistograms()
  33. self.assertEquals(num_hists, 10)
  34. blocksize = corr_ws.blocksize()
  35. self.assertEquals(blocksize, 1905)
  36. def tearDown(self):
  37. DeleteWorkspace(self._red_ws)
  38. DeleteWorkspace(self._corrected_flat_plate)
  39. def test_flat_plate(self):
  40. # Test flat plate shape
  41. kwargs = self._arguments
  42. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  43. Shape='FlatPlate',
  44. Width=2.0,
  45. Thickness=2.0,
  46. **kwargs)
  47. self._test_corrections_workspace(corrected)
  48. def test_cylinder(self):
  49. # Test cylinder shape
  50. kwargs = self._arguments
  51. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  52. Shape='Cylinder',
  53. Radius=2.0,
  54. **kwargs)
  55. self._test_corrections_workspace(corrected)
  56. def test_annulus(self):
  57. # Test annulus shape
  58. kwargs = self._arguments
  59. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  60. Shape='Annulus',
  61. InnerRadius=1.0,
  62. OuterRadius=2.0,
  63. **kwargs)
  64. self._test_corrections_workspace(corrected)
  65. def test_number_density(self):
  66. # Mass Density for water is 1.0
  67. # Number Density for water is 0.033428
  68. # These should give similar results
  69. kwargs = self._arguments
  70. kwargs['DensityType'] = 'Number Density'
  71. kwargs['Density'] = 0.033428
  72. corrected_num = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  73. Shape='FlatPlate',
  74. Width=2.0,
  75. Thickness=2.0,
  76. **kwargs)
  77. # _corrected_flat_plate is with mass density 1.0
  78. CompareWorkspaces(self._corrected_flat_plate, corrected_num, Tolerance=1e-6)
  79. DeleteWorkspace(corrected_num)
  80. def test_material_already_defined(self):
  81. SetSampleMaterial(InputWorkspace=self._red_ws,
  82. ChemicalFormula='H2-O',
  83. SampleMassDensity=1.0)
  84. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=self._red_ws,
  85. MaterialAlreadyDefined=True,
  86. EventsPerPoint=200,
  87. BeamHeight=3.5,
  88. BeamWidth=4.0,
  89. Height=2.0,
  90. Shape='FlatPlate',
  91. Width=2.0,
  92. Thickness=2.0)
  93. self._test_corrections_workspace(corrected)
  94. CompareWorkspaces(self._corrected_flat_plate, corrected, Tolerance=1e-6)
  95. def test_ILL_reduced(self):
  96. ill_red_ws = Load('ILL/IN16B/091515_red.nxs')
  97. ill_red_ws = ConvertUnits(ill_red_ws, Target='Wavelength', EMode='Indirect', EFixed=1.845)
  98. kwargs = self._arguments
  99. corrected = SimpleShapeMonteCarloAbsorption(InputWorkspace=ill_red_ws,
  100. Shape='FlatPlate',
  101. Width=2.0,
  102. Thickness=2.0,
  103. **kwargs)
  104. x_unit = corrected.getAxis(0).getUnit().unitID()
  105. self.assertEquals(x_unit, 'Wavelength')
  106. y_unit = corrected.YUnitLabel()
  107. self.assertEquals(y_unit, 'Attenuation factor')
  108. num_hists = corrected.getNumberHistograms()
  109. self.assertEquals(num_hists, 18)
  110. blocksize = corrected.blocksize()
  111. self.assertEquals(blocksize, 1024)
  112. DeleteWorkspace(ill_red_ws)
  113. # TODO: add test for powder diffraction data
  114. # ------------------------------------- Failure Cases ----------------------------------------
  115. def test_no_chemical_formula(self):
  116. kwargs = {'InputWorkspace': self._red_ws,
  117. 'MaterialAlreadyDefined': False,
  118. 'DensityType': 'Mass Density',
  119. 'Density': 1.0,
  120. 'EventsPerPoint': 200,
  121. 'BeamHeight': 3.5,
  122. 'BeamWidth': 4.0,
  123. 'Height': 2.0,
  124. 'Shape': 'FlatPlate',
  125. 'Width': 1.4,
  126. 'Thickness': 2.1}
  127. self.assertRaises(RuntimeError, SimpleShapeMonteCarloAbsorption, **kwargs)
  128. def test_flat_plate_no_params(self):
  129. # If the shape is flat plate but the relevant parameters haven't been entered this should throw
  130. # relevant params are Height, Width, Thickness
  131. kwargs = {'InputWorkspace': self._red_ws,
  132. 'ChemicalFormula': 'H2-O',
  133. 'DensityType': 'Mass Density',
  134. 'Density': 1.0,
  135. 'EventsPerPoint': 200,
  136. 'BeamHeight': 3.5,
  137. 'BeamWidth': 4.0,
  138. 'Shape': 'FlatPlate'}
  139. self.assertRaises(RuntimeError, SimpleShapeMonteCarloAbsorption, **kwargs)
  140. def test_not_in_wavelength(self):
  141. red_ws_not_wavelength = Load('irs26176_graphite002_red.nxs')
  142. kwargs = {'InputWorkspace': red_ws_not_wavelength,
  143. 'ChemicalFormula': 'H2-O',
  144. 'DensityType': 'Mass Density',
  145. 'Density': 1.0,
  146. 'EventsPerPoint': 200,
  147. 'BeamHeight': 3.5,
  148. 'BeamWidth': 4.0,
  149. 'Height': 2.0,
  150. 'Shape': 'FlatPlate',
  151. 'Width': 1.4,
  152. 'Thickness': 2.1}
  153. self.assertRaises(RuntimeError, SimpleShapeMonteCarloAbsorption, **kwargs)
  154. DeleteWorkspace(red_ws_not_wavelength)
  155. if __name__ == "__main__":
  156. unittest.main()