Mantid
Loading...
Searching...
No Matches
Run.cpp
Go to the documentation of this 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#include "MantidAPI/Run.h"
15
16#include <boost/python/class.hpp>
17#include <boost/python/copy_const_reference.hpp>
18#include <boost/python/list.hpp>
19#include <boost/python/overloads.hpp>
20#include <boost/python/register_ptr_to_python.hpp>
21#include <utility>
22
27using namespace boost::python;
28
31
32namespace {
33namespace bpl = boost::python;
34
43double getPropertyAsSingleValueWithDefaultStatistic(Run &self, const std::string &name) {
44 // Before calling the function we need to release the GIL,
45 // drop the Python threadstate and reset anything installed
46 // via PyEval_SetTrace while we execute the C++ code -
47 // ReleaseGlobalInterpreter does this for us
49 return self.getPropertyAsSingleValue(name);
50}
51
59double getPropertyAsSingleValueWithTimeAveragedMean(Run &self, const std::string &name) {
61}
62
72void addPropertyWithUnit(Run &self, const std::string &name, const bpl::object &value, const std::string &units,
73 bool replace) {
74 extract<Property *> extractor(value);
75 if (extractor.check()) {
76 Property *prop = extractor();
77 // Clone the property as Python owns the one that is passed in
78 auto new_prop = prop->clone();
79 // set name and units if provided
80 if (!name.empty())
81 new_prop->setName(name);
82
83 if (!units.empty())
84 new_prop->setUnits(units);
85 self.addProperty(new_prop, replace);
86 return;
87 }
88
89 // Use the factory
92 property->setUnits(units);
93 self.addProperty(std::move(property), replace);
94}
95
104void addProperty(Run &self, const std::string &name, const bpl::object &value, bool replace) {
105 addPropertyWithUnit(self, name, value, "", replace);
106}
107
115void addOrReplaceProperty(Run &self, const std::string &name, const bpl::object &value) {
116 addProperty(self, name, value, true);
117}
118
126bpl::object getWithDefault(bpl::object self, const bpl::object &key, bpl::object default_) {
127 bpl::object exists(self.attr("__contains__"));
128 if (extract<bool>(exists(key))()) {
129 return self.attr("__getitem__")(key);
130 } else {
131 return default_;
132 }
133}
134
141bpl::object get(bpl::object self, const bpl::object &key) {
142 return getWithDefault(std::move(self), key, bpl::object());
143}
144
149bpl::list keys(Run &self) {
150 const std::vector<Mantid::Kernel::Property *> &logs = self.getProperties();
151 bpl::list names;
152 for (auto log : logs) {
153 names.append(log->name());
154 }
155 return names;
156}
157} // namespace
158GNU_DIAG_OFF("unused-local-typedef")
159// Ignore -Wconversion warnings coming from boost::python
160// Seen with GCC 7.1.1 and Boost 1.63.0
161GNU_DIAG_OFF("conversion")
162
163BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(integrateProtonCharge_Overload, integrateProtonCharge, 0, 1)
164BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getPropertyAsSingleValue_Overload, getPropertyAsSingleValue, 1, 1)
165GNU_DIAG_ON("conversion")
166GNU_DIAG_ON("unused-local-typedef")
167
169 // Pointer
170 register_ptr_to_python<Run *>();
171
172 // Run class
173 class_<Run, boost::noncopyable>("Run")
174 .def("getProtonCharge", &Run::getProtonCharge, arg("self"), "Return the total good proton charge for the run")
175
176 .def("integrateProtonCharge", &Run::integrateProtonCharge,
177 integrateProtonCharge_Overload("Set the total good proton charge for the run, from the proton "
178 "charge log",
179 (arg("self"), arg("logname") = "proton_charge")))
180
181 .def("hasProperty", &Run::hasProperty, (arg("self"), arg("name")),
182 "Returns True if the given log value is contained within the run")
183
184 .def("getProperty", &Run::getProperty, (arg("self"), arg("name")), return_value_policy<return_by_value>(),
185 "Returns the named property "
186 "(log value). Use '.value' "
187 "to return the value.")
188
189 .def("getPropertyAsSingleValue", getPropertyAsSingleValueWithDefaultStatistic, (arg("self"), arg("name")),
190 "Return a log as a single float value. Time series values are "
191 "averaged.")
192
193 .def("getPropertyAsSingleValueWithTimeAveragedMean", getPropertyAsSingleValueWithTimeAveragedMean,
194 (arg("self"), arg("name")),
195 "Returns a log as a single float value. Calculated using "
196 "time-averaged mean.")
197
198 .def("getTimeAveragedStd", &Run::getTimeAveragedStd, (arg("self"), arg("name")),
199 "Returns the time averaged standard deviation")
200 .def("getTimeAveragedValue", &Run::getTimeAveragedValue, (arg("self"), arg("name")),
201 "Returns the time averaged value of a log")
202 .def("getStatistics", &Run::getStatistics, (arg("self"), arg("name")),
203 "Returns the all of the statistics about the obect")
204 .def("getProperties", &Run::getProperties, arg("self"), return_internal_reference<>(),
205 "Return the list of run properties managed by this object.")
206
207 .def("getLogData", (Property * (Run::*)(const std::string &) const) & Run::getLogData, (arg("self"), arg("name")),
208 return_value_policy<return_by_value>(),
209 "Returns the named log. Use '.value' to return the value. The "
210 "same "
211 "as getProperty.")
212
213 .def("getLogData", (const std::vector<Property *> &(Run::*)() const) & Run::getLogData, arg("self"),
214 return_internal_reference<>(),
215 "Return the list of logs for this run. The same as "
216 "getProperties.")
217
218 .def("getGoniometer", (const Mantid::Geometry::Goniometer &(Run::*)(const size_t) const) & Run::getGoniometer,
219 (arg("self"), arg("index") = 0), return_value_policy<reference_existing_object>(),
220 "Return Goniometer object associated with this run by index, "
221 "default first goniometer.")
222
223 .def("getNumGoniometers", &Run::getNumGoniometers, arg("self"),
224 "Return the number of goniometer objects associated with this run.")
225
226 .def("addProperty", &addProperty, (arg("self"), arg("name"), arg("value"), arg("replace")),
227 "Adds a property with the given name "
228 "and value. If replace=True then an "
229 "existing property is overwritten")
230
231 .def("addProperty", &addPropertyWithUnit, (arg("self"), arg("name"), arg("value"), arg("units"), arg("replace")),
232 "Adds a property with the given name, value and unit. If "
233 "replace=True then an existing property is overwritten")
234
235 .def("setStartAndEndTime", &Run::setStartAndEndTime, (arg("self"), arg("start"), arg("end")),
236 "Set the start and end time of the run")
237
238 .def("startTime", &Run::startTime, arg("self"), "Return the total starting time of the run.")
239
240 .def("endTime", &Run::endTime, arg("self"), "Return the total ending time of the run.")
241
242 .def("getTimeROI", &Run::getTimeROI, arg("self"), return_value_policy<reference_existing_object>(),
243 "Return the time regoin of interest")
244
245 //-------------------- Dictionary access----------------------------
246 .def("get", &getWithDefault, (arg("self"), arg("key"), arg("default")),
247 "Returns the value pointed to by the key or the default value "
248 "given.")
249 .def("get", &get, (arg("self"), arg("key")),
250 "Returns the value pointed to by the key or "
251 "None if it does not exist.")
252 .def("keys", &keys, arg("self"), "Returns the names of the properties as list")
253 .def("__contains__", &Run::hasProperty, (arg("self"), arg("name")))
254 .def("__getitem__", &Run::getProperty, (arg("self"), arg("name")), return_value_policy<return_by_value>())
255 .def("__setitem__", &addOrReplaceProperty, (arg("self"), arg("name"), arg("value")))
256 .def("__copy__", &Mantid::PythonInterface::generic__copy__<Run>)
257 .def("__deepcopy__", &Mantid::PythonInterface::generic__deepcopy__<Run>)
258 .def("__eq__", &Run::operator==, arg("self"), arg("other"));
259}
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_Run()
Definition Run.cpp:168
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
This class contains the information about the log entries.
Definition LogManager.h:44
double getTimeAveragedStd(const std::string &name) const
Get the time averaged standard deviation for a log.
const Types::Core::DateAndTime endTime() const
Return the run end time.
bool hasProperty(const std::string &name) const
Does the property exist on the object.
const Kernel::TimeROI & getTimeROI() const
const std::vector< Kernel::Property * > & getLogData() const
Access all log entries.
Definition LogManager.h:146
double getTimeAveragedValue(const std::string &name) const
Get the time averaged value for a log.
Kernel::TimeSeriesPropertyStatistics getStatistics(const std::string &name) const
Returns various statistics computations for a given property.
const Types::Core::DateAndTime startTime() const
Return the run start time.
Kernel::Property * getProperty(const std::string &name) const
Returns the named property as a pointer.
const std::vector< Kernel::Property * > & getProperties() const
Return all of the current properties.
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
Definition LogManager.h:91
double getPropertyAsSingleValue(const std::string &name, Kernel::Math::StatisticType statistic=Kernel::Math::Mean) const
Returns a property as a single double value from its name.
void setStartAndEndTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &end)
Set the run start and end.
This class stores information regarding an experimental run as a series of log entries.
Definition Run.h:35
void integrateProtonCharge(const std::string &logname="proton_charge") const
Integrate the proton charge over the whole run time - default log proton_charge.
Definition Run.cpp:333
size_t getNumGoniometers() const
Get the number of goniometers in the Run.
Definition Run.cpp:574
const Geometry::Goniometer & getGoniometer() const
Return reference to the first const Goniometer object for this run.
Definition Run.cpp:525
double getProtonCharge() const
Get the proton charge.
Definition Run.cpp:300
Class to represent a particular goniometer setting, which is described by the rotation matrix.
Definition Goniometer.h:55
Base class for properties.
Definition Property.h:94
void setName(const std::string &name)
Set the property's name.
Definition Property.cpp:66
virtual Property * clone() const =0
'Virtual copy constructor'
static std::unique_ptr< Kernel::Property > create(const std::string &name, const boost::python::object &defaultValue, const boost::python::object &validator, const unsigned int direction)
Creates a PropertyWithValue<Type> instance from the given information.
Defines a structure for releasing the Python GIL using the RAII pattern.
bool exists(Nexus::File &file, const std::string &name)
@ Input
An input workspace.
Definition Property.h:53