From fc67796445cfa18cc636139627239b58d1585334 Mon Sep 17 00:00:00 2001
From: Bhuvan Bezawada <bhuvan_777@outlook.com>
Date: Wed, 27 Jun 2018 15:48:41 +0100
Subject: [PATCH] Altered return strings for compatibility with numpy

re #22553
Added extra tests to create numpy arrays from return values
---
 .../Kernel/inc/MantidKernel/ContainerDtype.h  | 16 +++---
 .../python/mantid/kernel/ArrayPropertyTest.py | 52 +++++++++++++++++--
 .../mantid/kernel/TimeSeriesPropertyTest.py   |  6 +--
 3 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/Framework/Kernel/inc/MantidKernel/ContainerDtype.h b/Framework/Kernel/inc/MantidKernel/ContainerDtype.h
index 3ecdf326db3..aff76ce1835 100644
--- a/Framework/Kernel/inc/MantidKernel/ContainerDtype.h
+++ b/Framework/Kernel/inc/MantidKernel/ContainerDtype.h
@@ -49,9 +49,9 @@ namespace dtypeHelper {
 template <template <class> class Container, typename HeldType>
 std::string dtype(const Container<HeldType>) {
   if (std::is_same<HeldType, bool>::value) {
-    return "boolean";
+    return "bool_";
   } else if (std::is_same<HeldType, short>::value) {
-    return "short";
+    return "int16";
   } else if (std::is_same<HeldType, std::int8_t>::value) {
     return "int8";
   } else if (std::is_same<HeldType, std::int16_t>::value) {
@@ -61,17 +61,17 @@ std::string dtype(const Container<HeldType>) {
   } else if (std::is_same<HeldType, std::int64_t>::value) {
     return "int64";
   } else if (std::is_same<HeldType, long>::value) {
-    return "long";
+    return "int_";
   } else if (std::is_same<HeldType, long long>::value) {
-    return "longlong";
+    return "int64";
   } else if (std::is_same<HeldType, std::float_t>::value) {
-    return "float";
+    return "float32";
   } else if (std::is_same<HeldType, std::double_t>::value) {
-    return "double";
+    return "float64";
   } else if (std::is_same<HeldType, std::string>::value) {
-    return "string";
+    return "string_";
   } else {
-    return "obj";
+    return "object_";
   }
 }
 
diff --git a/Framework/PythonInterface/test/python/mantid/kernel/ArrayPropertyTest.py b/Framework/PythonInterface/test/python/mantid/kernel/ArrayPropertyTest.py
index ba08b5b3e97..db5f08d0f27 100644
--- a/Framework/PythonInterface/test/python/mantid/kernel/ArrayPropertyTest.py
+++ b/Framework/PythonInterface/test/python/mantid/kernel/ArrayPropertyTest.py
@@ -150,22 +150,64 @@ class ArrayPropertyTest(unittest.TestCase):
         int_arr = IntArrayProperty("integers", int_input_values, validator, direc)
 
         # Create string array
-        str_input_values =["a", "b", "c", "d", "e"]
+        str_input_values = ["a", "b", "c", "d", "e"]
         str_arr = StringArrayProperty("letters", str_input_values, validator, direc)
 
         # Test
-        self.assertEquals(float_arr.dtype(), "double")
-        self.assertEquals(str_arr.dtype(), "string")
+        self.assertEquals(float_arr.dtype(), "float64")
+        self.assertEquals(str_arr.dtype(), "string_")
         
         # Implementation of IntArrayProperty is based on long 
         # (which is itself implementation defined, so a special case is needed here)
         if sys.platform == 'win32' or sys.platform == 'darwin':
-          # Windows and macOS should return "long"
-          self.assertEquals(int_arr.dtype(), "long")
+          # Windows and macOS should return "int_"
+          self.assertEquals(int_arr.dtype(), "int_")
         else:
           # Linux based systems should return "int64"
           self.assertEquals(int_arr.dtype(), "int64")
 
+    def test_construct_numpy_array_with_given_dtype_float(self):
+        # Set up
+        direc = Direction.Output
+        validator = NullValidator()
+
+        # Create float array
+        float_input_values = [1.1, 2.5, 5.6, 4.6, 9.0, 6.0]
+        float_arr = FloatArrayProperty("floats", float_input_values, validator, direc)
+        
+        # Use the returned dtype() to check it works with numpy arrays
+        x = np.arange(1, 10, dtype=float_arr.dtype())
+        self.assertIsInstance(x, np.ndarray)
+        self.assertEquals(x.dtype, float_arr.dtype()) 
+
+    def test_construct_numpy_array_with_given_dtype_int(self):
+        # Set up
+        direc = Direction.Output
+        validator = NullValidator()
+
+        # Create int array
+        int_input_values = [1, 2, 5, 4, 9, 6]
+        int_arr = IntArrayProperty("integers", int_input_values, validator, direc)
+        
+        # Use the returned dtype() to check it works with numpy arrays
+        x = np.arange(1, 10, dtype=int_arr.dtype())
+        self.assertIsInstance(x, np.ndarray)
+        self.assertEquals(x.dtype, int_arr.dtype())
+
+    def test_construct_numpy_array_with_given_dtype_string(self):
+        # Set up
+        direc = Direction.Output
+        validator = NullValidator()
+
+        # Create string array
+        str_input_values = ["hello", "testing", "word", "another word", "word"]
+        str_arr = StringArrayProperty("letters", str_input_values, validator, direc)
+        
+        # Use the returned dtype() to check it works with numpy arrays
+        x = np.array(str_input_values, dtype=str_arr.dtype())
+        self.assertIsInstance(x, np.ndarray)
+        # Expect longest string to be returned
+        self.assertEquals(x.dtype, "S12")     
 
     def test_PythonAlgorithm_setProperty_With_Ranges_String(self):
         """
diff --git a/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py b/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py
index ca07e433b75..d162fc3f5b4 100644
--- a/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py
+++ b/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py
@@ -61,7 +61,7 @@ class TimeSeriesPropertyTest(unittest.TestCase):
         self.assertEquals(log_series.size(), self._ntemp)
         self.assertAlmostEqual(log_series.nthValue(0), -0.00161)
         # Check the dtype return value
-        self.assertEquals(log_series.dtype(), "double")
+        self.assertEquals(log_series.dtype(), "float64")
 
     def test_time_series_int_can_be_extracted(self):
         log_series = self._test_ws.getRun()["raw_frames"]
@@ -77,14 +77,14 @@ class TimeSeriesPropertyTest(unittest.TestCase):
         self.assertEquals(log_series.size(), 4)
         self.assertEquals(log_series.nthValue(0).strip(), 'CHANGE_PERIOD 1')
         # Check the dtype return value
-        self.assertEquals(log_series.dtype(), "string")
+        self.assertEquals(log_series.dtype(), "string_")
 
     def test_time_series_bool_can_be_extracted(self):
         log_series = self._test_ws.getRun()["period 1"]
         self._check_has_time_series_attributes(log_series)
         self.assertEquals(log_series.size(), 1)
         # Check the dtype return value
-        self.assertEquals(log_series.dtype(), "boolean")
+        self.assertEquals(log_series.dtype(), "bool_")
 
     def _check_has_time_series_attributes(self, log, values_type=np.ndarray):
         self.assertTrue(hasattr(log, "value"))
-- 
GitLab