Mantid
Loading...
Searching...
No Matches
IPeaksWorkspace.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 +
8#include "MantidAPI/Run.h"
15#include <boost/none.hpp>
16#include <boost/python/class.hpp>
17#include <boost/python/iterator.hpp>
18#include <boost/python/manage_new_object.hpp>
19#include <boost/python/return_internal_reference.hpp>
20#include <optional>
21#include <utility>
22
23using namespace boost::python;
24using namespace Mantid::Geometry;
25using namespace Mantid::API;
32
34
35namespace {
36
38IPeak *createPeakHKL(const IPeaksWorkspace &self, const object &data) {
40 // Python will manage it
41 return peak.release();
42}
43
45IPeak *createPeakQLab(const IPeaksWorkspace &self, const object &data) {
46 auto peak = self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), std::nullopt);
47 // Python will manage it
48 return peak.release();
49}
50
52IPeak *createPeakQLabWithDistance(const IPeaksWorkspace &self, const object &data, double detectorDistance) {
53 auto peak = self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), detectorDistance);
54 // Python will manage the object
55 return peak.release();
56}
57
59IPeak *createPeakQSample(const IPeaksWorkspace &self, const object &data) {
61 // Python will manage it
62 return peak.release();
63}
64
66void addPeak(IPeaksWorkspace &self, const IPeak &peak) {
67 self.addPeak(peak);
68 self.modified();
69}
70
72void addPeak2(IPeaksWorkspace &self, const object &data, const SpecialCoordinateSystem &frame) {
73 self.addPeak(PyObjectToV3D(data)(), frame);
74 self.modified();
75}
76
78void removePeak(IPeaksWorkspace &self, int peak_num) {
79 self.removePeak(peak_num);
80 self.modified();
81}
82
84void removePeaks(IPeaksWorkspace &self, const boost::python::object &peak_nums) {
86 self.modified();
87}
88
96class PeakWorkspaceTableAdaptor {
97public:
103 explicit PeakWorkspaceTableAdaptor(IPeaksWorkspace &peaksWorkspace) : m_peaksWorkspace(peaksWorkspace) {
104 // Create a map of string -> setter functions
105 // Each function will extract the given value from the passed python type.
106 m_setterMap = {{"RunNumber", setterFunction(&IPeak::setRunNumber)},
107 {"h", setterFunction(&IPeak::setH)},
108 {"k", setterFunction(&IPeak::setK)},
109 {"l", setterFunction(&IPeak::setL)},
110 {"Wavelength", setterFunction(&IPeak::setWavelength)},
111 {"Intens", setterFunction(&IPeak::setIntensity)},
112 {"SigInt", setterFunction(&IPeak::setSigmaIntensity)},
113 {"BinCount", setterFunction(&IPeak::setBinCount)},
114 {"PeakNumber", setterFunction(&IPeak::setPeakNumber)},
115 {"QLab", setterFunction(&IPeak::setQLabFrame)},
116 {"QSample", setterFunction(&IPeak::setQSampleFrame)}};
117 }
118
126 void setProperty(const std::string &columnName, const int rowIndex, object value) {
127 auto &peak = m_peaksWorkspace.getPeak(rowIndex);
128 if (m_setterMap.find(columnName) == m_setterMap.end()) {
129 throw std::runtime_error(columnName + " is a read only column of a peaks workspace");
130 }
131 m_setterMap[columnName](peak, std::move(value));
132 }
133
134private:
135 // type alias for the member function to wrap
136 template <typename T> using MemberFunc = void (IPeak::*)(T value);
137 // special type alias for V3D functions that take an addtional parameter
138 using MemberFuncV3D = void (IPeak::*)(const V3D &value, std::optional<double>);
139 // type alias for the setter function
140 using SetterType = std::function<void(IPeak &peak, const object)>;
141
149 template <typename T> SetterType setterFunction(const MemberFunc<T> func) {
150 return [func](IPeak &peak, const object &value) {
151 extract<T> extractor{value};
152 if (!extractor.check()) {
153 throw std::runtime_error("Cannot set value. Value was not of the expected type!");
154 }
155 (peak.*func)(extractor());
156 };
157 }
158
169 SetterType setterFunction(const MemberFuncV3D func) {
170 return [func](IPeak &peak, const object &value) {
171 extract<const V3D &> extractor{value};
172 if (!extractor.check()) {
173 throw std::runtime_error("Cannot set value. Value was not of the expected type!");
174 }
175 (peak.*func)(extractor(), std::nullopt);
176 };
177 }
178
179 // The PeaksWorkspace we need to map value to.
180 IPeaksWorkspace &m_peaksWorkspace;
181 // Map of string value to setter functions.
182 std::unordered_map<std::string, SetterType> m_setterMap;
183};
184
185GNU_DIAG_OFF("maybe-uninitialized")
186
187
197std::pair<int, std::string> getRowAndColumnName(const IPeaksWorkspace &self, const object &col_or_row,
198 const int row_or_col) {
199 extract<std::string> columnNameExtractor{col_or_row};
200 std::string columnName;
201 int rowIndex;
202
203 if (columnNameExtractor.check()) {
204 columnName = columnNameExtractor();
205 rowIndex = row_or_col;
206 } else {
207 rowIndex = extract<int>(col_or_row)();
208 const auto colIndex = row_or_col;
209 const auto columnNames = self.getColumnNames();
210 columnName = columnNames.at(colIndex);
211 }
212
213 return std::make_pair(rowIndex, columnName);
214}
215
216GNU_DIAG_ON("maybe-uninitialized")
217
218
226void setCell(IPeaksWorkspace &self, const object &col_or_row, const int row_or_col, const object &value) {
227 std::string columnName;
228 int rowIndex;
229 std::tie(rowIndex, columnName) = getRowAndColumnName(self, col_or_row, row_or_col);
230
231 PeakWorkspaceTableAdaptor tableMap{self};
232 tableMap.setProperty(columnName, rowIndex, value);
233}
234
236struct IPeaksWorkspaceIterator {
237 explicit IPeaksWorkspaceIterator(IPeaksWorkspace *const workspace)
238 : m_workspace{workspace}, m_numPeaks{workspace->getNumberPeaks()}, m_rowIndex{-1} {
239 assert(workspace);
240 }
241 IPeak *next() {
242 ++m_rowIndex;
243 if (m_rowIndex >= m_numPeaks) {
244 objects::stop_iteration_error();
245 }
246 return m_workspace->getPeakPtr(m_rowIndex);
247 }
248
249private:
250 IPeaksWorkspace *const m_workspace;
251 const int m_numPeaks;
252 int m_rowIndex;
253};
254
255// Create an iterator from the given workspace
256IPeaksWorkspaceIterator makePyIterator(IPeaksWorkspace &self) { return IPeaksWorkspaceIterator(&self); }
257
258} // namespace
259
261 class_<IPeaksWorkspaceIterator>("IPeaksWorkspaceIterator", no_init)
262 .def("__next__", &IPeaksWorkspaceIterator::next, return_value_policy<reference_existing_object>())
263 .def("__iter__", objects::identity_function());
264}
265
267 // IPeaksWorkspace class
268 class_<IPeaksWorkspace, bases<ITableWorkspace, ExperimentInfo>, boost::noncopyable>("IPeaksWorkspace", no_init)
269 .def("getNumberPeaks", &IPeaksWorkspace::getNumberPeaks, arg("self"),
270 "Returns the number of peaks within the workspace")
271 .def("addPeak", addPeak, (arg("self"), arg("peak")), "Add a peak to the workspace")
272 .def("addPeak", addPeak2, (arg("self"), arg("data"), arg("coord_system")), "Add a peak to the workspace")
273 .def("removePeak", removePeak, (arg("self"), arg("peak_num")), "Remove a peak from the workspace")
274 .def("removePeaks", removePeaks, (arg("self"), arg("peak_num")), "Remove specified peaks from the workspace")
275 .def("getPeak", &IPeaksWorkspace::getPeakPtr, (arg("self"), arg("peak_num")), return_internal_reference<>(),
276 "Returns a peak at the given index")
277 .def("createPeak", createPeakQLab, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
278 "Create a Peak and return it from its coordinates in the QLab frame")
279 .def("createPeak", createPeakQLabWithDistance, (arg("self"), arg("data"), arg("detector_distance")),
280 return_value_policy<manage_new_object>(),
281 "Create a Peak and return it from its coordinates in the QLab "
282 "frame, detector-sample distance explicitly provided")
283 .def("createPeakQSample", createPeakQSample, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
284 "Create a Peak and return it from its coordinates in the QSample "
285 "frame")
286 .def("createPeakHKL", createPeakHKL, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
287 "Create a Peak and return it from its coordinates in the HKL frame")
288 .def("hasIntegratedPeaks", &IPeaksWorkspace::hasIntegratedPeaks, arg("self"),
289 "Determine if the peaks have been integrated")
290 .def("getRun", &IPeaksWorkspace::mutableRun, arg("self"), return_internal_reference<>(),
291 "Return the Run object for this workspace")
292 .def("peakInfoNumber", &IPeaksWorkspace::peakInfoNumber, (arg("self"), arg("qlab_frame"), arg("lab_coordinate")),
293 "Peak info number at Q vector for this workspace")
294 .def("setCell", &setCell, (arg("self"), arg("row_or_column"), arg("column_or_row"), arg("value")),
295 "Sets the value of a given cell. If the row_or_column argument is a "
296 "number then it is interpreted as a row otherwise it "
297 "is interpreted as a column name.")
298 .def("__iter__", makePyIterator);
299 //-------------------------------------------------------------------------------------------------
300
302}
double value
The value of the point.
Definition FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
IPeaksWorkspace_sptr workspace
void export_IPeaksWorkspaceIterator()
void export_IPeaksWorkspace()
#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.
Interface to the class Mantid::DataObjects::PeaksWorkspace.
virtual bool hasIntegratedPeaks() const =0
Determine if the workspace has been integrated using a peaks integration algorithm.
virtual void removePeak(int peakNum)=0
Removes the indicated peak.
virtual void removePeaks(std::vector< int > badPeaks)=0
virtual void addPeak(const Mantid::Geometry::IPeak &ipeak)=0
Add a peak to the list.
virtual int peakInfoNumber(const Kernel::V3D &qLabFrame, bool labCoords) const =0
virtual std::unique_ptr< Geometry::IPeak > createPeakHKL(const Mantid::Kernel::V3D &HKL) const =0
Create an instance of a peak using a V3D.
virtual std::unique_ptr< Geometry::IPeak > createPeak(const Mantid::Kernel::V3D &QLabFrame, std::optional< double > detectorDistance=std::nullopt) const =0
Create an instance of a Peak.
Mantid::Geometry::IPeak * getPeakPtr(size_t const peakNum)
Return a pointer to the Peak.
virtual int getNumberPeaks() const =0
virtual std::unique_ptr< Mantid::Geometry::IPeak > createPeakQSample(const Mantid::Kernel::V3D &position) const =0
Create an instance of a Peak.
void modified()
If the workspace is the AnalysisDataService sends AfterReplaceNotification.
Structure describing a single-crystal peak.
Definition IPeak.h:26
virtual void setIntensity(double m_Intensity)=0
virtual void setK(double m_K)=0
virtual void setBinCount(double m_BinCount)=0
virtual void setQLabFrame(const Mantid::Kernel::V3D &QLabFrame, std::optional< double > detectorDistance)=0
virtual void setH(double m_H)=0
virtual void setPeakNumber(int m_PeakNumber)=0
virtual void setSigmaIntensity(double m_SigmaIntensity)=0
virtual void setL(double m_L)=0
virtual void setRunNumber(int m_RunNumber)=0
virtual void setQSampleFrame(const Mantid::Kernel::V3D &QSampleFrame, std::optional< double > detectorDistance)=0
virtual void setWavelength(double wavelength)=0
Class for 3D vectors.
Definition V3D.h:34
std::shared_ptr< Column > Column_sptr
Definition Column.h:232
SpecialCoordinateSystem
Special coordinate systems for Q3D.
STL namespace.
Takes a Python object and if it supports indexing and is of length 3 then it will attempt to convert ...
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...