42 }
catch (std::invalid_argument &) {
59 for (
size_t i = 0; i < shapeWS.
getNumDims(); ++i) {
62 const auto dataDim = findMatchingDimension(dataWS, *shapeDim);
63 if (!dataDim || dataDim->getIsIntegrated()) {
79 std::vector<int> axes;
80 for (
size_t i = 0; i < dataWS.
getNumDims(); ++i) {
82 if (!dataDim->getIsIntegrated()) {
84 axes.emplace_back(
index);
109size_t linearIndexToLinearIndex(
const size_t &nDimsShape,
const size_t &shapeReplicationIndex,
110 const std::vector<size_t> &indexMaxShape,
const std::vector<size_t> &indexMakerShape,
111 const size_t &sourceIndex, std::vector<size_t> &indexMakerData,
112 const size_t &nDimsData) {
113 std::vector<size_t> vecShapeIndexes(nDimsShape);
115 &vecShapeIndexes[0]);
117 vecShapeIndexes.erase(vecShapeIndexes.begin() + shapeReplicationIndex);
141 return "This is an algorithm to create a higher dimensional dataset by "
142 "replicating along an additional axis";
154 transposeMD->setProperty(
"InputWorkspace", toTranspose);
158 return std::dynamic_pointer_cast<const MDHistoWorkspace>(outputWS);
167 std::map<std::string, std::string> errorMap;
170 if (shapeWS->getNumNonIntegratedDims() != dataWS->getNumNonIntegratedDims() + 1) {
171 errorMap.emplace(
"DataWorkspace",
"Expect to have n-1 non-interated dimensions of ShapeWorkspace");
174 size_t nonMatchingCount = 0;
175 bool haveMatchingIntegratedDims =
false;
176 for (
size_t i = 0; i < shapeWS->getNumDims(); ++i) {
177 const auto shapeDim = shapeWS->getDimension(i);
180 const auto dataDim = findMatchingDimension(*dataWS, *shapeDim);
182 if (dataDim->getIsIntegrated()) {
183 if (!shapeDim->getIsIntegrated()) {
187 haveMatchingIntegratedDims =
true;
191 if (shapeDim->getNBins() != dataDim->getNBins()) {
192 std::stringstream stream;
193 stream <<
"Dimension with id " << shapeDim->getDimensionId()
194 <<
" in ShapeWorkspace has a different number of bins as the "
195 "same id dimension in the DataWorkspace";
196 errorMap.emplace(
"DataWorkspace", stream.str());
197 }
else if (haveMatchingIntegratedDims) {
198 errorMap.emplace(
"ShapeWorkspace",
"Extra integrated dimensions must be only "
199 "the last dimensions, e.g.:\n\nThis is allowed:\n "
200 "Shape: {10, 5, 1, 1}\n Data: { 1, 5, 1, 1}\n\nBut "
201 "this is not:\n Shape: {10, 1, 5, 1}\n Data: { 1, 1, "
202 "5, 1}\n\nUse TransposeMD to re-arrange dimensions.");
212 if (nonMatchingCount != 1) {
213 errorMap.emplace(
"DataWorkspace",
"There should be ONLY 1 dimension present "
214 "in the ShapeWorkspace that is not present "
215 "(or integrated out) in the DataWorkspace");
226 "An input workspace defining the shape of the output.");
228 "An input workspace containing the data to replicate.");
230 "An output workspace with replicated data.");
241 shapeWS = std::dynamic_pointer_cast<MDHistoWorkspace>(temp);
255 dataWS = std::dynamic_pointer_cast<MDHistoWorkspace>(temp);
269 const size_t nDimsShape = shapeWS->getNumDims();
270 size_t nDimsData = dataWS->getNumDims();
279 if (nDimsData <= nDimsShape) {
280 auto axes = findAxes(*shapeWS, *dataWS);
283 const auto numberOfDimensionsOfDataWorkspace =
static_cast<int>(nDimsData);
284 const auto found = std::find_if(axes.cbegin(), axes.cend(), [numberOfDimensionsOfDataWorkspace](
const auto &axis) {
285 return axis >= numberOfDimensionsOfDataWorkspace;
287 if (found != axes.cend()) {
288 std::string message =
"ReplicateMD: Cannot transpose the data workspace. Attempting to "
289 "swap dimension index " +
290 std::to_string(std::distance(
static_cast<const int *
>(&axes[0]), &(*found))) +
292 ", but the dimensionality of the data workspace is " +
std::to_string(nDimsData);
293 throw std::runtime_error(message);
296 nDimsData = transposedDataWS->getNumDims();
300 std::vector<size_t> indexMakerShape(nDimsShape);
301 std::vector<size_t> indexMaxShape(nDimsShape);
302 for (
size_t i = 0; i < nDimsShape; ++i) {
303 indexMaxShape[i] = shapeWS->getDimension(i)->getNBins();
308 std::vector<size_t> indexMakerData(nDimsData);
309 std::vector<size_t> indexMaxData(nDimsData);
310 for (
size_t i = 0; i < nDimsData; ++i) {
311 indexMaxData[i] = transposedDataWS->getDimension(i)->getNBins();
316 const size_t shapeReplicationIndex = findReplicationDimension(*shapeWS, *transposedDataWS);
323 auto iterators = outputWS->createIterators(nThreads,
nullptr);
326 for (
int it = 0; it < int(iterators.size()); ++it) {
337 const size_t targetIndex = linearIndexToLinearIndex(nDimsShape, shapeReplicationIndex, indexMaxShape,
338 indexMakerShape, sourceIndex, indexMakerData, nDimsData);
341 outputWS->setSignalAt(sourceIndex, transposedDataWS->getSignalAt(targetIndex));
342 outputWS->setErrorSquaredAt(sourceIndex, transposedDataWS->getErrorAt(targetIndex) *
343 transposedDataWS->getErrorAt(targetIndex));
344 outputWS->setNumEventsAt(sourceIndex, transposedDataWS->getNumEventsAt(targetIndex));
345 outputWS->setMDMaskAt(sourceIndex, transposedDataWS->getIsMaskedAt(targetIndex));
348 }
while (outIt->next());
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_FOR_NO_WSP_CHECK()
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Abstract interface to MDHistoWorkspace, for use in exposing to Python.
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimension(size_t index) const
Get a dimension.
size_t getDimensionIndexById(const std::string &id) const
Get the index of the dimension that matches the ID given.
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimensionWithId(std::string id) const
Get a dimension.
virtual size_t getNumDims() const
Helper class for reporting progress from algorithms.
A property class for workspaces.
An implementation of IMDIterator that iterates through a MDHistoWorkspace.
size_t getLinearIndex() const override
Getter for the linear index.
The class describes one dimension of multidimensional dataset representing an orthogonal dimension an...
virtual const std::string & getDimensionId() const =0
short name which identify the dimension among other dimension.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
ReplicateMD : Algorithm header for ReplicateMD.
std::shared_ptr< Mantid::DataObjects::MDHistoWorkspace > getDataWorkspace() const
fetches and down casts
void exec() override
Execute the algorithm.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
std::map< std::string, std::string > validateInputs() override
Valdiate the algorithm inputs.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
std::shared_ptr< const Mantid::DataObjects::MDHistoWorkspace > transposeMD(std::shared_ptr< Mantid::DataObjects::MDHistoWorkspace > &toTranspose, const std::vector< int > &axes)
Transpose the input data workspace according to the axis provided.
std::shared_ptr< Mantid::DataObjects::MDHistoWorkspace > getShapeWorkspace() const
fetches and down casts
int version() const override
Algorithm's version for identification.
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
std::shared_ptr< const MDHistoWorkspace > MDHistoWorkspace_const_sptr
A shared pointer to a const MDHistoWorkspace.
std::shared_ptr< const IMDDimension > IMDDimension_const_sptr
Shared Pointer to const IMDDimension.
void GetIndicesFromLinearIndex(const size_t numDims, const size_t linear_index, const size_t *index_maker, const size_t *index_max, size_t *out_indices)
Set up a nested for loop by creating an array of counters.
size_t GetLinearIndex(const size_t numDims, size_t *index, size_t *index_maker)
Return a linear index from dimensional indices of a nested for loop.
void SetUpIndexMaker(const size_t numDims, size_t *out, const size_t *index_max)
Set up an "index maker" for a nested for loop.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
@ Output
An output workspace.