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"
13#include <boost/none.hpp>
14#include <boost/optional.hpp>
15#include <boost/python/class.hpp>
16#include <boost/python/iterator.hpp>
17#include <boost/python/manage_new_object.hpp>
18#include <boost/python/return_internal_reference.hpp>
19#include <utility>
20
21using namespace boost::python;
22using namespace Mantid::Geometry;
23using namespace Mantid::API;
29
31
32namespace {
33
35IPeak *createPeakHKL(const IPeaksWorkspace &self, const object &data) {
37 // Python will manage it
38 return peak.release();
39}
40
42IPeak *createPeakQLab(const IPeaksWorkspace &self, const object &data) {
43 auto peak = self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), boost::none);
44 // Python will manage it
45 return peak.release();
46}
47
49IPeak *createPeakQLabWithDistance(const IPeaksWorkspace &self, const object &data, double detectorDistance) {
50 auto peak = self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), detectorDistance);
51 // Python will manage the object
52 return peak.release();
53}
54
56IPeak *createPeakQSample(const IPeaksWorkspace &self, const object &data) {
58 // Python will manage it
59 return peak.release();
60}
61
63void addPeak(IPeaksWorkspace &self, const IPeak &peak) {
64 self.addPeak(peak);
65 self.modified();
66}
67
69void addPeak2(IPeaksWorkspace &self, const object &data, const SpecialCoordinateSystem &frame) {
70 self.addPeak(PyObjectToV3D(data)(), frame);
71 self.modified();
72}
73
75void removePeak(IPeaksWorkspace &self, int peak_num) {
76 self.removePeak(peak_num);
77 self.modified();
78}
79
87class PeakWorkspaceTableAdaptor {
88public:
94 explicit PeakWorkspaceTableAdaptor(IPeaksWorkspace &peaksWorkspace) : m_peaksWorkspace(peaksWorkspace) {
95 // Create a map of string -> setter functions
96 // Each function will extract the given value from the passed python type.
97 m_setterMap = {{"RunNumber", setterFunction(&IPeak::setRunNumber)},
98 {"h", setterFunction(&IPeak::setH)},
99 {"k", setterFunction(&IPeak::setK)},
100 {"l", setterFunction(&IPeak::setL)},
101 {"Wavelength", setterFunction(&IPeak::setWavelength)},
102 {"Intens", setterFunction(&IPeak::setIntensity)},
103 {"SigInt", setterFunction(&IPeak::setSigmaIntensity)},
104 {"BinCount", setterFunction(&IPeak::setBinCount)},
105 {"PeakNumber", setterFunction(&IPeak::setPeakNumber)},
106 {"QLab", setterFunction(&IPeak::setQLabFrame)},
107 {"QSample", setterFunction(&IPeak::setQSampleFrame)}};
108 }
109
117 void setProperty(const std::string &columnName, const int rowIndex, object value) {
118 auto &peak = m_peaksWorkspace.getPeak(rowIndex);
119 if (m_setterMap.find(columnName) == m_setterMap.end()) {
120 throw std::runtime_error(columnName + " is a read only column of a peaks workspace");
121 }
122 m_setterMap[columnName](peak, std::move(value));
123 }
124
125private:
126 // type alias for the member function to wrap
127 template <typename T> using MemberFunc = void (IPeak::*)(T value);
128 // special type alias for V3D functions that take an addtional parameter
129 using MemberFuncV3D = void (IPeak::*)(const V3D &value, boost::optional<double>);
130 // type alias for the setter function
131 using SetterType = std::function<void(IPeak &peak, const object)>;
132
140 template <typename T> SetterType setterFunction(const MemberFunc<T> func) {
141 return [func](IPeak &peak, const object &value) {
142 extract<T> extractor{value};
143 if (!extractor.check()) {
144 throw std::runtime_error("Cannot set value. Value was not of the expected type!");
145 }
146 (peak.*func)(extractor());
147 };
148 }
149
160 SetterType setterFunction(const MemberFuncV3D func) {
161 return [func](IPeak &peak, const object &value) {
162 extract<const V3D &> extractor{value};
163 if (!extractor.check()) {
164 throw std::runtime_error("Cannot set value. Value was not of the expected type!");
165 }
166 (peak.*func)(extractor(), boost::none);
167 };
168 }
169
170 // The PeaksWorkspace we need to map value to.
171 IPeaksWorkspace &m_peaksWorkspace;
172 // Map of string value to setter functions.
173 std::unordered_map<std::string, SetterType> m_setterMap;
174};
175
186std::pair<int, std::string> getRowAndColumnName(const IPeaksWorkspace &self, const object &col_or_row,
187 const int row_or_col) {
188 extract<std::string> columnNameExtractor{col_or_row};
189 std::string columnName;
190 int rowIndex;
191
192 if (columnNameExtractor.check()) {
193 columnName = columnNameExtractor();
194 rowIndex = row_or_col;
195 } else {
196 rowIndex = extract<int>(col_or_row)();
197 const auto colIndex = row_or_col;
198 const auto columnNames = self.getColumnNames();
199 columnName = columnNames.at(colIndex);
200 }
201
202 return std::make_pair(rowIndex, columnName);
203}
204
213void setCell(IPeaksWorkspace &self, const object &col_or_row, const int row_or_col, const object &value) {
214 std::string columnName;
215 int rowIndex;
216 std::tie(rowIndex, columnName) = getRowAndColumnName(self, col_or_row, row_or_col);
217
218 PeakWorkspaceTableAdaptor tableMap{self};
219 tableMap.setProperty(columnName, rowIndex, value);
220}
221
223struct IPeaksWorkspaceIterator {
224 explicit IPeaksWorkspaceIterator(IPeaksWorkspace *const workspace)
225 : m_workspace{workspace}, m_numPeaks{workspace->getNumberPeaks()}, m_rowIndex{-1} {
226 assert(workspace);
227 }
228 IPeak *next() {
229 ++m_rowIndex;
230 if (m_rowIndex >= m_numPeaks) {
231 objects::stop_iteration_error();
232 }
233 return m_workspace->getPeakPtr(m_rowIndex);
234 }
235
236private:
237 IPeaksWorkspace *const m_workspace;
238 const int m_numPeaks;
239 int m_rowIndex;
240};
241
242// Create an iterator from the given workspace
243IPeaksWorkspaceIterator makePyIterator(IPeaksWorkspace &self) { return IPeaksWorkspaceIterator(&self); }
244
245} // namespace
246
248 class_<IPeaksWorkspaceIterator>("IPeaksWorkspaceIterator", no_init)
249 .def("__next__", &IPeaksWorkspaceIterator::next, return_value_policy<reference_existing_object>())
250 .def("__iter__", objects::identity_function());
251}
252
254 // IPeaksWorkspace class
255 class_<IPeaksWorkspace, bases<ITableWorkspace, ExperimentInfo>, boost::noncopyable>("IPeaksWorkspace", no_init)
256 .def("getNumberPeaks", &IPeaksWorkspace::getNumberPeaks, arg("self"),
257 "Returns the number of peaks within the workspace")
258 .def("addPeak", addPeak, (arg("self"), arg("peak")), "Add a peak to the workspace")
259 .def("addPeak", addPeak2, (arg("self"), arg("data"), arg("coord_system")), "Add a peak to the workspace")
260 .def("removePeak", removePeak, (arg("self"), arg("peak_num")), "Remove a peak from the workspace")
261 .def("getPeak", &IPeaksWorkspace::getPeakPtr, (arg("self"), arg("peak_num")), return_internal_reference<>(),
262 "Returns a peak at the given index")
263 .def("createPeak", createPeakQLab, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
264 "Create a Peak and return it from its coordinates in the QLab frame")
265 .def("createPeak", createPeakQLabWithDistance, (arg("self"), arg("data"), arg("detector_distance")),
266 return_value_policy<manage_new_object>(),
267 "Create a Peak and return it from its coordinates in the QLab "
268 "frame, detector-sample distance explicitly provided")
269 .def("createPeakQSample", createPeakQSample, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
270 "Create a Peak and return it from its coordinates in the QSample "
271 "frame")
272 .def("createPeakHKL", createPeakHKL, (arg("self"), arg("data")), return_value_policy<manage_new_object>(),
273 "Create a Peak and return it from its coordinates in the HKL frame")
274 .def("hasIntegratedPeaks", &IPeaksWorkspace::hasIntegratedPeaks, arg("self"),
275 "Determine if the peaks have been integrated")
276 .def("getRun", &IPeaksWorkspace::mutableRun, arg("self"), return_internal_reference<>(),
277 "Return the Run object for this workspace")
278 .def("peakInfoNumber", &IPeaksWorkspace::peakInfoNumber, (arg("self"), arg("qlab_frame"), arg("lab_coordinate")),
279 "Peak info number at Q vector for this workspace")
280 .def("setCell", &setCell, (arg("self"), arg("row_or_column"), arg("column_or_row"), arg("value")),
281 "Sets the value of a given cell. If the row_or_column argument is a "
282 "number then it is interpreted as a row otherwise it "
283 "is interpreted as a column name.")
284 .def("__iter__", makePyIterator);
285 //-------------------------------------------------------------------------------------------------
286
288}
double value
The value of the point.
Definition: FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
void export_IPeaksWorkspaceIterator()
void export_IPeaksWorkspace()
Interface to the class Mantid::DataObjects::PeaksWorkspace.
Mantid::Geometry::IPeak * getPeakPtr(const int peakNum)
Return a pointer to the Peak.
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 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 > createPeak(const Mantid::Kernel::V3D &QLabFrame, boost::optional< double > detectorDistance=boost::none) const =0
Create an instance of a Peak.
virtual std::unique_ptr< Geometry::IPeak > createPeakHKL(const Mantid::Kernel::V3D &HKL) const =0
Create an instance of a peak using a V3D.
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.
virtual std::vector< std::string > getColumnNames() const =0
Returns a vector of all column names.
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, boost::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 setQSampleFrame(const Mantid::Kernel::V3D &QSampleFrame, boost::optional< double > detectorDistance)=0
virtual void setRunNumber(int m_RunNumber)=0
virtual void setWavelength(double wavelength)=0
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< Column > Column_sptr
Definition: Column.h:228
SpecialCoordinateSystem
Special coordinate systems for Q3D.
Takes a Python object and if it supports indexing and is of length 3 then it will attempt to convert ...
Definition: PyObjectToV3D.h:22
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...