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"
14
15#include <boost/python/class.hpp>
16#include <boost/python/copy_const_reference.hpp>
17#include <boost/python/list.hpp>
18#include <boost/python/overloads.hpp>
19#include <boost/python/register_ptr_to_python.hpp>
20#include <utility>
21
26using namespace boost::python;
27
30
31namespace {
32namespace bpl = boost::python;
33
42double getPropertyAsSingleValueWithDefaultStatistic(Run &self, const std::string &name) {
43 // Before calling the function we need to release the GIL,
44 // drop the Python threadstate and reset anything installed
45 // via PyEval_SetTrace while we execute the C++ code -
46 // ReleaseGlobalInterpreter does this for us
48 return self.getPropertyAsSingleValue(name);
49}
50
58double getPropertyAsSingleValueWithTimeAveragedMean(Run &self, const std::string &name) {
60}
61
71void addPropertyWithUnit(Run &self, const std::string &name, const bpl::object &value, const std::string &units,
72 bool replace) {
73 extract<Property *> extractor(value);
74 if (extractor.check()) {
75 Property *prop = extractor();
76 self.addProperty(prop->clone(),
77 replace); // Clone the property as Python owns the one that is passed in
78 return;
79 }
80
81 // Use the factory
84 property->setUnits(units);
85 self.addProperty(std::move(property), replace);
86}
87
96void addProperty(Run &self, const std::string &name, const bpl::object &value, bool replace) {
97 addPropertyWithUnit(self, name, value, "", replace);
98}
99
107void addOrReplaceProperty(Run &self, const std::string &name, const bpl::object &value) {
108 addProperty(self, name, value, true);
109}
110
118bpl::object getWithDefault(bpl::object self, const bpl::object &key, bpl::object default_) {
119 bpl::object exists(self.attr("__contains__"));
120 if (extract<bool>(exists(key))()) {
121 return self.attr("__getitem__")(key);
122 } else {
123 return default_;
124 }
125}
126
133bpl::object get(bpl::object self, const bpl::object &key) {
134 return getWithDefault(std::move(self), key, bpl::object());
135}
136
141bpl::list keys(Run &self) {
142 const std::vector<Mantid::Kernel::Property *> &logs = self.getProperties();
143 bpl::list names;
144 for (auto log : logs) {
145 names.append(log->name());
146 }
147 return names;
148}
149} // namespace
150GNU_DIAG_OFF("unused-local-typedef")
151// Ignore -Wconversion warnings coming from boost::python
152// Seen with GCC 7.1.1 and Boost 1.63.0
153GNU_DIAG_OFF("conversion")
154
155BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(integrateProtonCharge_Overload, integrateProtonCharge, 0, 1)
156BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getPropertyAsSingleValue_Overload, getPropertyAsSingleValue, 1, 1)
157GNU_DIAG_ON("conversion")
158GNU_DIAG_ON("unused-local-typedef")
159
161 // Pointer
162 register_ptr_to_python<Run *>();
163
164 // Run class
165 class_<Run, boost::noncopyable>("Run")
166 .def("getProtonCharge", &Run::getProtonCharge, arg("self"), "Return the total good proton charge for the run")
167
168 .def("integrateProtonCharge", &Run::integrateProtonCharge,
169 integrateProtonCharge_Overload("Set the total good proton charge for the run, from the proton "
170 "charge log",
171 (arg("self"), arg("logname") = "proton_charge")))
172
173 .def("hasProperty", &Run::hasProperty, (arg("self"), arg("name")),
174 "Returns True if the given log value is contained within the run")
175
176 .def("getProperty", &Run::getProperty, (arg("self"), arg("name")), return_value_policy<return_by_value>(),
177 "Returns the named property "
178 "(log value). Use '.value' "
179 "to return the value.")
180
181 .def("getPropertyAsSingleValue", getPropertyAsSingleValueWithDefaultStatistic, (arg("self"), arg("name")),
182 "Return a log as a single float value. Time series values are "
183 "averaged.")
184
185 .def("getPropertyAsSingleValueWithTimeAveragedMean", getPropertyAsSingleValueWithTimeAveragedMean,
186 (arg("self"), arg("name")),
187 "Returns a log as a single float value. Calculated using "
188 "time-averaged mean.")
189
190 .def("getTimeAveragedStd", &Run::getTimeAveragedStd, (arg("self"), arg("name")),
191 "Returns the time averaged standard deviation")
192
193 .def("getProperties", &Run::getProperties, arg("self"), return_internal_reference<>(),
194 "Return the list of run properties managed by this object.")
195
196 .def("getLogData", (Property * (Run::*)(const std::string &) const) & Run::getLogData, (arg("self"), arg("name")),
197 return_value_policy<return_by_value>(),
198 "Returns the named log. Use '.value' to return the value. The "
199 "same "
200 "as getProperty.")
201
202 .def("getLogData", (const std::vector<Property *> &(Run::*)() const) & Run::getLogData, arg("self"),
203 return_internal_reference<>(),
204 "Return the list of logs for this run. The same as "
205 "getProperties.")
206
207 .def("getGoniometer", (const Mantid::Geometry::Goniometer &(Run::*)(const size_t) const) & Run::getGoniometer,
208 (arg("self"), arg("index") = 0), return_value_policy<reference_existing_object>(),
209 "Return Goniometer object associated with this run by index, "
210 "default first goniometer.")
211
212 .def("getNumGoniometers", &Run::getNumGoniometers, arg("self"),
213 "Return the number of goniometer objects associated with this run.")
214
215 .def("addProperty", &addProperty, (arg("self"), arg("name"), arg("value"), arg("replace")),
216 "Adds a property with the given name "
217 "and value. If replace=True then an "
218 "existing property is overwritten")
219
220 .def("addProperty", &addPropertyWithUnit, (arg("self"), arg("name"), arg("value"), arg("units"), arg("replace")),
221 "Adds a property with the given name, value and unit. If "
222 "replace=True then an existing property is overwritten")
223
224 .def("setStartAndEndTime", &Run::setStartAndEndTime, (arg("self"), arg("start"), arg("end")),
225 "Set the start and end time of the run")
226
227 .def("startTime", &Run::startTime, arg("self"), "Return the total starting time of the run.")
228
229 .def("endTime", &Run::endTime, arg("self"), "Return the total ending time of the run.")
230
231 //-------------------- Dictionary access----------------------------
232 .def("get", &getWithDefault, (arg("self"), arg("key"), arg("default")),
233 "Returns the value pointed to by the key or the default value "
234 "given.")
235 .def("get", &get, (arg("self"), arg("key")),
236 "Returns the value pointed to by the key or "
237 "None if it does not exist.")
238 .def("keys", &keys, arg("self"), "Returns the names of the properties as list")
239 .def("__contains__", &Run::hasProperty, (arg("self"), arg("name")))
240 .def("__getitem__", &Run::getProperty, (arg("self"), arg("name")), return_value_policy<return_by_value>())
241 .def("__setitem__", &addOrReplaceProperty, (arg("self"), arg("name"), arg("value")))
242 .def("__copy__", &Mantid::PythonInterface::generic__copy__<Run>)
243 .def("__deepcopy__", &Mantid::PythonInterface::generic__deepcopy__<Run>)
244 .def("__eq__", &Run::operator==, arg("self"), arg("other"));
245}
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:160
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
Definition: Property.cpp:102
#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.
Definition: LogManager.cpp:322
const Types::Core::DateAndTime endTime() const
Return the run end time.
Definition: LogManager.cpp:165
bool hasProperty(const std::string &name) const
Does the property exist on the object.
Definition: LogManager.cpp:265
const std::vector< Kernel::Property * > & getLogData() const
Access all log entries.
Definition: LogManager.h:134
const Types::Core::DateAndTime startTime() const
Return the run start time.
Definition: LogManager.cpp:133
Kernel::Property * getProperty(const std::string &name) const
Returns the named property as a pointer.
Definition: LogManager.cpp:404
const std::vector< Kernel::Property * > & getProperties() const
Return all of the current properties.
Definition: LogManager.cpp:287
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
Definition: LogManager.h:79
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.
Definition: LogManager.cpp:349
void setStartAndEndTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &end)
Set the run start and end.
Definition: LogManager.cpp:121
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
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:208
size_t getNumGoniometers() const
Get the number of goniometers in the Run.
Definition: Run.cpp:373
const Geometry::Goniometer & getGoniometer() const
Return reference to the first const Goniometer object for this run.
Definition: Run.cpp:324
double getProtonCharge() const
Get the proton charge.
Definition: Run.cpp:182
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
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)
Based on the current group in the file, does the named sub-entry exist?
@ Input
An input workspace.
Definition: Property.h:53