Mantid
Loading...
Searching...
No Matches
CombineTableWorkspaces.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2025 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
11
12namespace Mantid::Algorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(CombineTableWorkspaces)
21
22//----------------------------------------------------------------------------------------------
23
24
25const std::string CombineTableWorkspaces::name() const { return "CombineTableWorkspaces"; }
26
28int CombineTableWorkspaces::version() const { return 1; }
29
31const std::string CombineTableWorkspaces::category() const { return "Utility\\Workspaces"; }
32
34const std::string CombineTableWorkspaces::summary() const {
35 return "Algorithm takes two table workspaces and, if they have the same column titles and data types, combines them "
36 "into a single table. Currently supports data types of double, int, float, string, bool, size_t and V3D.";
37}
38
39//----------------------------------------------------------------------------------------------
44 std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("LHSWorkspace", "", Direction::Input),
45 "The first table workspace.");
47 std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("RHSWorkspace", "", Direction::Input),
48 "The second table workspace.");
50 std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("OutputWorkspace", "", Direction::Output),
51 "The combined table workspace.");
52}
53
54const std::map<std::string, int> &CombineTableWorkspaces::allowedTypes() {
55 static const std::map<std::string, int> types = {{"double", 0}, {"int", 1}, {"str", 2}, {"bool", 3},
56 {"size_t", 4}, {"float", 5}, {"V3D", 6}};
57 return types;
58}
59
60std::map<std::string, std::string> CombineTableWorkspaces::validateInputs() {
61 std::map<std::string, std::string> results;
62
63 const DataObjects::TableWorkspace_sptr LHSWorkspace = getProperty("LHSWorkspace");
64 const DataObjects::TableWorkspace_sptr RHSWorkspace = getProperty("RHSWorkspace");
65
66 const std::size_t expectedCols = LHSWorkspace->columnCount();
67
68 // check correct number of columns
69 if (RHSWorkspace->columnCount() != expectedCols) {
70 results.emplace("LHSWorkspace", "Both Table Workspaces must have the same number of columns");
71 results.emplace("RHSWorkspace", "Both Table Workspaces must have the same number of columns");
72 return results;
73 }
74
75 // get column titles
76 const auto lColNames = LHSWorkspace->getColumnNames();
77 const auto rColNames = RHSWorkspace->getColumnNames();
78
79 const std::map<std::string, int> &allowedColumnTypes = allowedTypes();
80
81 bool matchingColumnNames = true;
82 bool matchingColumnTypes = true;
83 bool allColumnTypesAllowed = true;
84
85 for (std::size_t i = 0; i < expectedCols; i++) {
86 if (lColNames[i] != rColNames[i]) {
87 matchingColumnNames = false;
88 break;
89 }
90 auto LHType = LHSWorkspace->getColumn(i)->type();
91 if (LHType != RHSWorkspace->getColumn(i)->type()) {
92 matchingColumnTypes = false;
93 break;
94 }
95 auto it = allowedColumnTypes.find(LHType);
96 if (it == allowedColumnTypes.end()) {
97 allColumnTypesAllowed = false;
98 break;
99 }
100 }
101
102 if (!matchingColumnNames) {
103 results.emplace("LHSWorkspace", "Both Table Workspaces must have the same column titles");
104 results.emplace("RHSWorkspace", "Both Table Workspaces must have the same column titles");
105 }
106
107 if (!matchingColumnTypes) {
108 results.emplace("LHSWorkspace", "Both Table Workspaces must have the same data types for corresponding columns");
109 results.emplace("RHSWorkspace", "Both Table Workspaces must have the same data types for corresponding columns");
110 }
111
112 if (!allColumnTypesAllowed) {
113 results.emplace("LHSWorkspace", "Only supported data types are: double, int, string, bool, size_t, float, and V3D");
114 results.emplace("RHSWorkspace", "Only supported data types are: double, int, string, bool, size_t, float, and V3D");
115 }
116
117 return results;
118}
119
120//----------------------------------------------------------------------------------------------
124 const DataObjects::TableWorkspace_sptr LHSWorkspace = getProperty("LHSWorkspace");
125 const DataObjects::TableWorkspace_sptr RHSWorkspace = getProperty("RHSWorkspace");
126
127 const std::map<std::string, int> &allowedColumnTypes = allowedTypes();
128
129 // Copy the first workspace to our output workspace
130 DataObjects::TableWorkspace_sptr outputWS = LHSWorkspace->clone();
131 // Get hold of the peaks in the second workspace
132 const auto nRows = RHSWorkspace->rowCount();
133 const auto nCols = RHSWorkspace->columnCount();
134
135 std::vector<std::string> colTypes = {};
136 for (std::size_t i = 0; i < nCols; i++) {
137 colTypes.emplace_back(RHSWorkspace->getColumn(i)->type());
138 }
139
140 Progress progress(this, 0.0, 1.0, nRows);
141
142 for (std::size_t r = 0; r < nRows; r++) {
143 TableRow newRow = outputWS->appendRow();
144 TableRow currentRow = RHSWorkspace->getRow(r);
145 for (std::size_t c = 0; c < nCols; c++) {
146 const auto dType = colTypes[c];
147 const int dTypeVal = allowedColumnTypes.at(dType);
148 switch (dTypeVal) {
149 case 0:
150 newRow << currentRow.Double(c);
151 break;
152 case 1:
153 newRow << currentRow.Int(c);
154 break;
155 case 2:
156 newRow << currentRow.cell<std::string>(c);
157 break;
158 case 3:
159 newRow << currentRow.cell<Mantid::API::Boolean>(c);
160 break;
161 case 4:
162 newRow << currentRow.cell<std::size_t>(c);
163 break;
164 case 5:
165 newRow << currentRow.cell<float>(c);
166 break;
167 case 6:
168 newRow << currentRow.cell<V3D>(c);
169 break;
170 default:
171 // this should be caught by the input validation anyway
172 throw std::runtime_error("Unsupported Data Type");
173 }
174 }
175 progress.report();
176 }
177
178 setProperty("OutputWorkspace", outputWS);
179}
180
181} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
TableRow represents a row in a TableWorkspace.
Definition TableRow.h:39
double & Double(size_t col)
Returns a reference to the element in position col if its type is double.
Definition TableRow.h:137
int & Int(size_t col)
Returns a reference to the element in position col if its type is int.
Definition TableRow.h:131
T & cell(size_t col)
Templated method to access the element col in the row.
Definition TableRow.h:115
A property class for workspaces.
CombineTableWorkspaces : Take a pair of table workspaces and combine them into a single table by appe...
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
static const std::map< std::string, int > & allowedTypes()
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void exec() override
Execute the algorithm.
const std::string category() const override
Algorithm's category for identification.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Class for 3D vectors.
Definition V3D.h:34
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
STL namespace.
As TableColumn stores its data in a std::vector bool type cannot be used in the same way as the other...
Definition Column.h:213
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54