From 579154e919f06a8ef9261b810034f562e1eed7c0 Mon Sep 17 00:00:00 2001 From: Bhuvan Bezawada <bhuvan_777@outlook.com> Date: Mon, 25 Jun 2018 13:04:08 +0100 Subject: [PATCH] dtype() function works and tests pass re #22553 --- Framework/Kernel/CMakeLists.txt | 5 +-- .../Kernel/inc/MantidKernel/ContainerDtype.h | 30 +++++++++-------- .../kernel/PropertyWithValueExporter.h | 12 ++++++- .../kernel/src/Exports/ArrayProperty.cpp | 32 +++++-------------- .../kernel/src/Exports/TimeSeriesProperty.cpp | 26 ++++----------- .../mantid/kernel/PropertyWithValueTest.py | 3 ++ .../mantid/kernel/TimeSeriesPropertyTest.py | 2 +- 7 files changed, 49 insertions(+), 61 deletions(-) diff --git a/Framework/Kernel/CMakeLists.txt b/Framework/Kernel/CMakeLists.txt index 938ea19d10f..5435b145466 100644 --- a/Framework/Kernel/CMakeLists.txt +++ b/Framework/Kernel/CMakeLists.txt @@ -166,7 +166,7 @@ set ( INC_FILES inc/MantidKernel/ConfigService.h inc/MantidKernel/ConfigObserver.h inc/MantidKernel/ConfigPropertyObserver.h - inc/MantidKernel/DateAndTime.h + inc/MantidKernel/DateAndTime.h inc/MantidKernel/DataItem.h inc/MantidKernel/DataService.h inc/MantidKernel/DateAndTimeHelpers.h @@ -247,7 +247,7 @@ set ( INC_FILES inc/MantidKernel/NetworkProxy.h inc/MantidKernel/NeutronAtom.h inc/MantidKernel/NexusDescriptor.h - inc/MantidKernel/normal_distribution.h + inc/MantidKernel/normal_distribution.h inc/MantidKernel/NullValidator.h inc/MantidKernel/OptionalBool.h inc/MantidKernel/ParaViewVersion.h @@ -316,6 +316,7 @@ set ( INC_FILES inc/MantidKernel/cow_ptr.h inc/MantidKernel/make_cow.h inc/MantidKernel/make_unique.h + inc/MantidKernel/ContainerDType.h ) set ( TEST_FILES diff --git a/Framework/Kernel/inc/MantidKernel/ContainerDtype.h b/Framework/Kernel/inc/MantidKernel/ContainerDtype.h index 7ab86706152..f32eb2f6a40 100644 --- a/Framework/Kernel/inc/MantidKernel/ContainerDtype.h +++ b/Framework/Kernel/inc/MantidKernel/ContainerDtype.h @@ -2,8 +2,8 @@ #define MANTID_KERNEL_CONTAINERDTYPE_H_ #include <string> -#include <vector> #include <type_traits> +#include <vector> #include <boost/python/class.hpp> #include <boost/python/implicit.hpp> @@ -15,8 +15,8 @@ /** ContainerDtype Header File - A helper free function to allow identification of data type being used by providing a - numpy friendly string. + A helper free function to allow identification of data type being used by + providing a numpy friendly string. @author Lamar Moore STFC, Bhuvan Bezawada STFC @date 21/06/2018 @@ -42,27 +42,31 @@ File change history is stored at: <https://github.com/mantidproject/mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ + namespace dtypeHelper { -template <template<class> typename Container, typename HeldType> +// Free function to determine data type being stored in container +template <template <class> typename Container, typename HeldType> std::string dtype(Container<HeldType> &self) { if (std::is_same<HeldType, bool>::value) { return "b"; - } - else if (std::is_integral<HeldType>::value) { + } else if (std::is_same<HeldType, std::int32_t>::value) { + return "int32"; + } else if (std::is_same<HeldType, std::int64_t>::value) { + return "int64"; + } else if (std::is_integral<HeldType>::value) { return "i"; - } - else if (std::is_floating_point<HeldType>::value) { + } else if (std::is_same<HeldType, std::float_t>::value) { + return "f"; + } else if (std::is_same<HeldType, std::double_t>::value) { return "d"; - } - else if (std::is_same<HeldType, std::string>::value) { + } else if (std::is_same<HeldType, std::string>::value) { return "s"; - } - else { + } else { return "obj"; } } -} +} // namespace dtypeHelper #endif /*MANTID_KERNEL_CONTAINERDTYPE_H_*/ diff --git a/Framework/PythonInterface/inc/MantidPythonInterface/kernel/PropertyWithValueExporter.h b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/PropertyWithValueExporter.h index 8cc6f84d4fe..f1ebd9b71b7 100644 --- a/Framework/PythonInterface/inc/MantidPythonInterface/kernel/PropertyWithValueExporter.h +++ b/Framework/PythonInterface/inc/MantidPythonInterface/kernel/PropertyWithValueExporter.h @@ -25,6 +25,7 @@ */ #include "MantidKernel/PropertyWithValue.h" +#include "MantidKernel/ContainerDtype.h" #ifndef Q_MOC_RUN #include <boost/python/class.hpp> @@ -33,8 +34,15 @@ #include <boost/python/return_by_value.hpp> #endif +// Call the dtype helper function +template <typename HeldType> +std::string dtype(Mantid::Kernel::PropertyWithValue<HeldType> &self) { + return dtypeHelper::dtype(self); +} + namespace Mantid { namespace PythonInterface { + /** * A helper struct to export PropertyWithValue<> types to Python. */ @@ -49,9 +57,11 @@ struct PropertyWithValueExporter { pythonClassName, no_init) .add_property("value", make_function(&PropertyWithValue<HeldType>::operator(), - return_value_policy<ValueReturnPolicy>())); + return_value_policy<ValueReturnPolicy>())) + .def("dtype", &dtype<HeldType>, arg("self"), "returns :`std::string`"); } }; + } } diff --git a/Framework/PythonInterface/mantid/kernel/src/Exports/ArrayProperty.cpp b/Framework/PythonInterface/mantid/kernel/src/Exports/ArrayProperty.cpp index 8f650b64e21..e1e33e2e756 100644 --- a/Framework/PythonInterface/mantid/kernel/src/Exports/ArrayProperty.cpp +++ b/Framework/PythonInterface/mantid/kernel/src/Exports/ArrayProperty.cpp @@ -1,5 +1,6 @@ #include "MantidKernel/ArrayProperty.h" #include "MantidKernel/NullValidator.h" +#include "MantidKernel/ContainerDtype.h" #include "MantidPythonInterface/kernel/Converters/NDArrayToVector.h" #include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h" @@ -24,32 +25,15 @@ using namespace boost::python; namespace { /// return_value_policy for cloned numpy array -using return_cloned_numpy = + using return_cloned_numpy = return_value_policy<Policies::VectorRefToNumpy<Converters::Clone>>; -// Default return if no matches occur -template <typename type> std::string dtype(ArrayProperty<type> &self) { - return "obj"; +// Call the dtype helper function +template <typename type> +std::string dtype(ArrayProperty<type> &self) { + return dtypeHelper::dtype(self); } -// String return -template <> std::string dtype(ArrayProperty<std::string> &self) { return "s"; } - -// Integer (32-bit) return -template <> std::string dtype(ArrayProperty<int32_t> &self) { return "i"; } - -// Integer (64-bit) return -template <> std::string dtype(ArrayProperty<int64_t> &self) { return "i"; } - -// Long return -template <> std::string dtype(ArrayProperty<long> &self) { return "i"; } - -// Boolean return -template <> std::string dtype(ArrayProperty<bool> &self) { return "b"; } - -// Double return -template <> std::string dtype(ArrayProperty<double> &self) { return "d"; } - #define EXPORT_ARRAY_PROP(type, prefix) \ class_<ArrayProperty<type>, bases<PropertyWithValue<std::vector<type>>>, \ boost::noncopyable>(#prefix "ArrayProperty", no_init) \ @@ -123,7 +107,7 @@ ArrayProperty<T> *createArrayPropertyFromNDArray(const std::string &name, void export_ArrayProperty() { // Match the python names to their C types - EXPORT_ARRAY_PROP(double, Float); + EXPORT_ARRAY_PROP(double, Float); EXPORT_ARRAY_PROP(long, Int); EXPORT_ARRAY_PROP(std::string, String); @@ -131,4 +115,4 @@ void export_ArrayProperty() { // Python can be seen also. Users shouldn't need this EXPORT_ARRAY_PROP(int, CInt); EXPORT_ARRAY_PROP(size_t, UnsignedInt); -} +} \ No newline at end of file diff --git a/Framework/PythonInterface/mantid/kernel/src/Exports/TimeSeriesProperty.cpp b/Framework/PythonInterface/mantid/kernel/src/Exports/TimeSeriesProperty.cpp index fd66a145845..0abdccb66f9 100644 --- a/Framework/PythonInterface/mantid/kernel/src/Exports/TimeSeriesProperty.cpp +++ b/Framework/PythonInterface/mantid/kernel/src/Exports/TimeSeriesProperty.cpp @@ -2,6 +2,7 @@ #include "MantidPythonInterface/kernel/Converters/DateAndTime.h" #include "MantidPythonInterface/kernel/GetPointer.h" #include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h" +#include "MantidKernel/ContainerDtype.h" #include <boost/python/class.hpp> #include <boost/python/implicit.hpp> @@ -35,28 +36,12 @@ void addPyTimeValue(TimeSeriesProperty<TYPE> &self, self.addValue(*dateandtime, value); } -// Default return if no matches occur -template <typename TYPE> std::string dtype(TimeSeriesProperty<TYPE> &self) { - return "obj"; -} - -// String return -template <> std::string dtype(TimeSeriesProperty<std::string> &self) { - return "s"; +// Call the dtype helper function +template <typename TYPE> +std::string dtype(TimeSeriesProperty<TYPE> &self) { + return dtypeHelper::dtype(self); } -// Integer (32-bit) return -template <> std::string dtype(TimeSeriesProperty<int32_t> &self) { return "i"; } - -// Integer (64-bit) return -template <> std::string dtype(TimeSeriesProperty<int64_t> &self) { return "i"; } - -// Boolean return -template <> std::string dtype(TimeSeriesProperty<bool> &self) { return "b"; } - -// Double return -template <> std::string dtype(TimeSeriesProperty<double> &self) { return "d"; } - // Macro to reduce copy-and-paste #define EXPORT_TIMESERIES_PROP(TYPE, Prefix) \ register_ptr_to_python<TimeSeriesProperty<TYPE> *>(); \ @@ -109,6 +94,7 @@ template <> std::string dtype(TimeSeriesProperty<double> &self) { return "d"; } } // namespace + void export_TimeSeriesProperty_Double() { EXPORT_TIMESERIES_PROP(double, Float); } diff --git a/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py b/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py index 773ec7871a5..b0d2411eef3 100644 --- a/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py +++ b/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py @@ -126,6 +126,9 @@ class PropertyWithValueTest(unittest.TestCase): result = det_list_prop.valueAsPrettyStr(40,False) self.assertEquals(result.startswith("1,2,3,"), True) self.assertEquals(result.endswith("98,99"), True) + + # Check the dtype return value + self.assertEquals(det_list_prop.dtype(), "int32") def _do_vector_double_numpy_test(self, int_type=False): create_ws = AlgorithmManager.createUnmanaged('CreateWorkspace') diff --git a/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py b/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py index 5017ae8da86..6da8d40c794 100644 --- a/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py +++ b/Framework/PythonInterface/test/python/mantid/kernel/TimeSeriesPropertyTest.py @@ -69,7 +69,7 @@ class TimeSeriesPropertyTest(unittest.TestCase): self.assertEquals(log_series.size(), self._nframes) self.assertEquals(log_series.nthValue(1), 1436) # Check the dtype return value - self.assertEquals(log_series.dtype(), "i") + self.assertEquals(log_series.dtype(), "int64") def test_time_series_string_can_be_extracted(self): log_series = self._test_ws.getRun()["icp_event"] -- GitLab