/Framework/PythonInterface/test/python/mantid/kernel/ArrayPropertyTest.py

https://github.com/mantidproject/mantid · Python · 262 lines · 145 code · 46 blank · 71 comment · 2 complexity · a3ff165cc5b5401d39dc1e514e1653d3 MD5 · raw file

  1. # Mantid Repository : https://github.com/mantidproject/mantid
  2. #
  3. # Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
  4. # NScD Oak Ridge National Laboratory, European Spallation Source,
  5. # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. # SPDX - License - Identifier: GPL - 3.0 +
  7. """Test the exposed ArrayProperty
  8. """
  9. import unittest
  10. from mantid.kernel import (FloatArrayProperty, StringArrayProperty, IntArrayProperty, Direction,
  11. NullValidator)
  12. from mantid.api import PythonAlgorithm
  13. import numpy as np
  14. import sys
  15. class ArrayPropertyTest(unittest.TestCase):
  16. def test_default_constructor_raises_an_exception(self):
  17. """
  18. Test that the class cannot be default constructed
  19. """
  20. self.assertRaises(Exception, FloatArrayProperty)
  21. def test_name_only_constructor_gives_correct_object(self):
  22. """
  23. Tests the simplest constructor that takes
  24. only a name
  25. """
  26. name = "numbers"
  27. arr = FloatArrayProperty(name)
  28. self.assertTrue(isinstance(arr, FloatArrayProperty))
  29. self._check_object_attributes(arr, name, Direction.Input)
  30. def test_name_direction_constructor_gives_correct_object(self):
  31. """
  32. Tests the constructor that takes
  33. only a name & direction
  34. """
  35. name = "numbers"
  36. direc = Direction.Output
  37. arr = FloatArrayProperty(name, direc)
  38. self._check_object_attributes(arr, name, direc)
  39. def test_name_validator_direction_constructor_gives_correct_object(self):
  40. """
  41. Test the constructor that takes a name, validator & direction
  42. """
  43. name = "numbers"
  44. direc = Direction.Output
  45. validator = NullValidator()
  46. arr = FloatArrayProperty(name, validator, direc)
  47. self._check_object_attributes(arr, name, direc)
  48. self.assertEqual(arr.isValid, "")
  49. def test_name_string_values_validator_direction_constructor_gives_correct_object(self):
  50. """
  51. Test the constructor that takes a name, values as string, validator & direction
  52. """
  53. name = "numbers"
  54. direc = Direction.Output
  55. validator = NullValidator()
  56. values_str = "1.345,34.2,5345.3,4,5.3948"
  57. arr = FloatArrayProperty(name, values_str, validator, direc)
  58. self._check_object_attributes(arr, name, direc, length = 5)
  59. self.assertEqual(arr.isValid, "")
  60. values = arr.value
  61. self.assertTrue(isinstance(values, np.ndarray))
  62. def test_name_values_from_list_validator_direction_constructor_gives_correct_object(self):
  63. """
  64. Test the constructor that takes a name, values from python object,
  65. validator & direction
  66. """
  67. name = "numbers"
  68. direc = Direction.Output
  69. validator = NullValidator()
  70. input_values =[1.1,2.5,5.6,4.6,9.0, 6.0]
  71. arr = FloatArrayProperty(name, input_values, validator, direc)
  72. self._check_object_attributes(arr, name, direc, length = len(input_values))
  73. def test_name_values_from_array_validator_direction_constructor_gives_correct_object(self):
  74. """
  75. Test the constructor that takes a name, values from python object,
  76. validator & direction
  77. """
  78. name = "numbers"
  79. direc = Direction.Output
  80. validator = NullValidator()
  81. input_values = np.array([1.1,2.5,5.6,4.6,9.0, 6.0])
  82. arr = FloatArrayProperty(name, input_values, validator, direc)
  83. self._check_object_attributes(arr, name, direc, length = 6)
  84. def _check_object_attributes(self, arrprop, name, direction, length = 0):
  85. """
  86. Do attribute tests
  87. """
  88. self.assertEqual(arrprop.name, name)
  89. self.assertEqual(arrprop.direction, direction)
  90. self.assertEqual(len(arrprop.value), length)
  91. def test_setProperty_with_FloatArrayProperty(self):
  92. """
  93. Test ArrayProperty within a python algorithm
  94. """
  95. class AlgWithFloatArrayProperty(PythonAlgorithm):
  96. _input_values = None
  97. def PyInit(self):
  98. name = "numbers"
  99. self.declareProperty(
  100. FloatArrayProperty("Input", Direction.Input), "Float array")
  101. def PyExec(self):
  102. self._input_values = self.getProperty("Input").value
  103. input_values = [1.1,2.5,5.6,4.6,9.0,6.0]
  104. self._do_algorithm_test(AlgWithFloatArrayProperty, input_values)
  105. def test_setProperty_With_FloatArrayProperty_And_Py3_Range_Object(self):
  106. """
  107. Python 3 range() returns a range object that behaves like a sequence
  108. whereas it just returned a list in Python 2
  109. """
  110. class AlgWithFloatArrayProperty(PythonAlgorithm):
  111. _input_values = None
  112. def PyInit(self):
  113. name = "numbers"
  114. self.declareProperty(
  115. FloatArrayProperty("Input", Direction.Input), "Float array")
  116. def PyExec(self):
  117. self._input_values = self.getProperty("Input").value
  118. input_values = range(1, 5)
  119. self._do_algorithm_test(AlgWithFloatArrayProperty, input_values)
  120. def test_dtype_function_calls(self):
  121. """
  122. Tests the dtype() function call for the data types stored in the array.
  123. """
  124. # Set up
  125. direc = Direction.Output
  126. validator = NullValidator()
  127. # Create float array
  128. float_input_values = [1.1, 2.5, 5.6, 4.6, 9.0, 6.0]
  129. float_arr = FloatArrayProperty("floats", float_input_values, validator, direc)
  130. # Create int array
  131. int_input_values = [1, 2, 5, 4, 9, 6]
  132. int_arr = IntArrayProperty("integers", int_input_values, validator, direc)
  133. # Create string array
  134. str_input_values = ["a", "b", "c", "d", "e"]
  135. str_arr = StringArrayProperty("letters", str_input_values, validator, direc)
  136. # Test
  137. self.assertEqual(float_arr.dtype(), "f")
  138. self.assertEqual(int_arr.dtype(), "i")
  139. self.assertEqual(str_arr.dtype(), "S1")
  140. def test_construct_numpy_array_with_given_dtype_float(self):
  141. # Set up
  142. direc = Direction.Output
  143. validator = NullValidator()
  144. # Create float array
  145. float_input_values = [1.1, 2.5, 5.6, 4.6, 9.0, 6.0]
  146. float_arr = FloatArrayProperty("floats", float_input_values, validator, direc)
  147. # Use the returned dtype() to check it works with numpy arrays
  148. x = np.arange(1, 10, dtype=float_arr.dtype())
  149. self.assertIsInstance(x, np.ndarray)
  150. self.assertEqual(x.dtype, float_arr.dtype())
  151. def test_construct_numpy_array_with_given_dtype_int(self):
  152. # Set up
  153. direc = Direction.Output
  154. validator = NullValidator()
  155. # Create int array
  156. int_input_values = [1, 2, 5, 4, 9, 6]
  157. int_arr = IntArrayProperty("integers", int_input_values, validator, direc)
  158. # Use the returned dtype() to check it works with numpy arrays
  159. x = np.arange(1, 10, dtype=int_arr.dtype())
  160. self.assertIsInstance(x, np.ndarray)
  161. self.assertEqual(x.dtype, int_arr.dtype())
  162. def test_construct_numpy_array_with_given_dtype_string(self):
  163. # Set up
  164. direc = Direction.Output
  165. validator = NullValidator()
  166. # Create string array
  167. str_input_values = ["hello", "testing", "word", "another word", "word"]
  168. str_arr = StringArrayProperty("letters", str_input_values, validator, direc)
  169. # Use the returned dtype() to check it works with numpy arrays
  170. x = np.array(str_input_values, dtype=str_arr.dtype())
  171. self.assertIsInstance(x, np.ndarray)
  172. # Expect longest string to be returned
  173. self.assertEqual(x.dtype, "S12")
  174. def test_PythonAlgorithm_setProperty_With_Ranges_String(self):
  175. """
  176. Test ArrayProperty within a python algorithm can
  177. be set with a string range
  178. """
  179. class AlgWithIntArrayProperty(PythonAlgorithm):
  180. _input_values = None
  181. def PyInit(self):
  182. self.declareProperty(IntArrayProperty("Input", Direction.Input), "Run numbers")
  183. def PyExec(self):
  184. self._input_values = self.getProperty("Input").value
  185. alg = AlgWithIntArrayProperty()
  186. alg.initialize()
  187. alg.setProperty("Input", "10:15")
  188. alg.execute()
  189. self.assertEqual(6, len(alg._input_values))
  190. def test_PythonAlgorithm_setProperty_with_StringArrayProperty(self):
  191. """
  192. Test StringArrayProperty within a python algorithm
  193. """
  194. class AlgWithStringArrayProperty(PythonAlgorithm):
  195. _input_values = None
  196. def PyInit(self):
  197. self.declareProperty(
  198. StringArrayProperty("Input", Direction.Input), "string array")
  199. def PyExec(self):
  200. self._input_values = self.getProperty("Input").value
  201. input_values = ["val1","val2","val3"]
  202. self._do_algorithm_test(AlgWithStringArrayProperty, input_values)
  203. def _do_algorithm_test(self, class_, input_values):
  204. """
  205. Run the algorithm and test the values are passed correctly
  206. """
  207. alg = class_()
  208. alg.initialize()
  209. alg.setProperty("Input", input_values)
  210. alg.execute()
  211. self.assertTrue(alg._input_values is not None)
  212. self.assertEqual(len(alg._input_values), len(input_values))
  213. for index, val in enumerate(input_values):
  214. self.assertEqual(val,input_values[index])
  215. if __name__ == "__main__":
  216. unittest.main()