12#include "MantidHistogramData/LinearGenerator.h"
31 data.resize(other.data.size());
32 for (
size_t i = 0; i <
data.size(); ++i) {
33 data[i] = std::make_unique<Histogram1D>(*(other.data[i]));
52void Workspace2D::init(
const std::size_t &NVectors,
const std::size_t &XLength,
const std::size_t &YLength) {
53 data.resize(NVectors);
55 auto x = Kernel::make_cow<HistogramData::HistogramX>(XLength, HistogramData::LinearGenerator(1.0, 1.0));
56 HistogramData::Counts
y(YLength);
57 HistogramData::CountStandardDeviations
e(YLength);
58 Histogram1D spec(HistogramData::getHistogramXMode(XLength, YLength), HistogramData::Histogram::YMode::Counts);
62 for (
size_t i = 0; i <
data.size(); i++) {
63 data[i] = std::make_unique<Histogram1D>(spec);
70 m_axes[0] = std::make_unique<API::RefAxis>(
this);
71 m_axes[1] = std::make_unique<API::SpectraAxis>(
this);
77 HistogramData::Histogram initializedHistogram(
histogram);
79 if (
histogram.yMode() == HistogramData::Histogram::YMode::Frequencies) {
80 initializedHistogram.setFrequencies(
histogram.size(), 0.0);
81 initializedHistogram.setFrequencyStandardDeviations(
histogram.size(), 0.0);
83 initializedHistogram.setCounts(
histogram.size(), 0.0);
84 initializedHistogram.setCountStandardDeviations(
histogram.size(), 0.0);
88 Histogram1D spec(initializedHistogram.xMode(), initializedHistogram.yMode());
91 [&spec](
const auto &) { return std::move(std::make_unique<Histogram1D>(spec)); });
95 m_axes[0] = std::make_unique<API::RefAxis>(
this);
96 m_axes[1] = std::make_unique<API::SpectraAxis>(
this);
103 throw std::runtime_error(
"There is no data in the Workspace2D, "
104 "therefore cannot determine if it is ragged.");
106 const auto numberOfBins =
data[0]->size();
107 return std::any_of(
data.cbegin(),
data.cend(),
108 [&numberOfBins](
const auto &
histogram) { return numberOfBins != histogram->size(); });
119 return std::accumulate(
120 data.begin(),
data.end(),
static_cast<size_t>(0),
121 [](
const size_t value,
const std::unique_ptr<Histogram1D> &histo) { return value + histo->size(); });
129 size_t numBins =
data[0]->size();
131 std::find_if_not(
data.cbegin(),
data.cend(), [numBins](
const auto &iter) { return numBins == iter->size(); });
132 if (it !=
data.cend())
133 throw std::length_error(
"blocksize undefined because size of histograms is not equal");
146 throw std::invalid_argument(
"Could not find number of bins in a histogram at index " +
std::to_string(
index) +
147 ": index is too large.");
157 auto maxNumberOfBins =
data[0]->size();
158 for (
const auto &iter :
data) {
159 const auto numberOfBins = iter->size();
160 if (numberOfBins > maxNumberOfBins)
161 maxNumberOfBins = numberOfBins;
163 return maxNumberOfBins;
206 bool loadAsRectImg,
double scale_1, [[maybe_unused]]
bool parallelExecution) {
207 if (imageY.empty() && imageE.empty())
209 if (imageY.empty() && imageE[0].empty())
211 if (imageE.empty() && imageY[0].empty())
215 if (!loadAsRectImg && numBins != 1) {
216 throw std::runtime_error(
"Cannot set image in workspace: a single bin workspace is "
217 "required when initializing a workspace from an "
218 "image using a histogram per pixel.");
223 if (!imageY.empty()) {
225 width = imageY.front().size();
228 width = imageE.front().size();
230 size_t dataSize = width *
height;
233 throw std::runtime_error(
"Cannot set image: image is bigger than workspace.");
236 if (!loadAsRectImg) {
239 for (
int i = 0; i < static_cast<int>(
height); ++i) {
241 const auto &rowY = imageY[i];
242 const auto &rowE = imageE[i];
243 size_t spec = start +
static_cast<size_t>(i) * width;
244 auto pE = rowE.begin();
245 for (
auto pY = rowY.begin(); pY != rowY.end() && pE != rowE.end(); ++pY, ++pE, ++spec) {
246 data[spec]->dataY()[0] = *pY;
247 data[spec]->dataE()[0] = *pE;
253 throw std::runtime_error(std::string(
"To load an image into a workspace with one spectrum per "
254 "row, then number of spectra (") +
259 if (width != numBins)
260 throw std::runtime_error(std::string(
"To load an image into a workspace with one spectrum per "
261 "row, then number of bins (") +
262 std::to_string(numBins) +
") needs to be equal to the width (columns) of the image (" +
267 for (
int i = 0; i < static_cast<int>(
height); ++i) {
269 const auto &rowY = imageY[i];
270 const auto &rowE = imageE[i];
271 data[i]->dataY() = rowY;
272 data[i]->dataE() = rowE;
277 for (
int i = 0; i < static_cast<int>(width) + 1; ++i) {
278 data[0]->dataX()[i] = i * scale_1;
281 for (
int i = 1; i < static_cast<int>(
height); ++i) {
297 std::ostringstream ss;
298 ss <<
"Workspace2D::getSpectrum, histogram number " <<
index <<
" out of range " <<
data.size();
299 throw std::range_error(ss.str());
325 bool skipError)
const {
328 throw std::range_error(
"Workspace2D::generateHistogram, histogram number out of range");
331 const MantidVec ¤tX = spec.readX();
332 const MantidVec ¤tY = spec.readY();
333 const MantidVec ¤tE = spec.readE();
335 throw std::runtime_error(
"Workspace2D::generateHistogram(): X vector must be at least length 2");
336 Y.resize(
X.size() - 1, 0);
337 E.resize(
X.size() - 1, 0);
340 if (currentX.size() == currentY.size())
342 std::vector<double> histX;
343 histX.resize(currentX.size() + 1);
359IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_sptr>(
const std::string &name)
const {
364 std::string message =
365 "Attempt to assign property " + name +
" to incorrect type. Expected shared_ptr<Workspace2D>.";
366 throw std::runtime_error(message);
372IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_const_sptr>(
const std::string &name)
const {
375 return prop->operator()();
377 std::string message =
378 "Attempt to assign property " + name +
" to incorrect type. Expected const shared_ptr<Workspace2D>.";
379 throw std::runtime_error(message);
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
#define DECLARE_WORKSPACE(classname)
size_t numberOfDetectorGroups() const
Returns the number of detector groups.
HistoWorkspace is an abstract base class for MatrixWorkspace types that are NOT event workspaces.
void setHistogram(T &&...data)
Sets the Histogram associated with this spectrum.
void setMatrixWorkspace(MatrixWorkspace *matrixWorkspace, const size_t index)
Sets the MatrixWorkspace pointer (pointer to the owning workspace).
void setCountStandardDeviations(T &&...data) &
void setCounts(T &&...data) &
std::vector< std::unique_ptr< Axis > > m_axes
A vector of pointers to the axes for this workspace.
const HistogramData::HistogramE & e(const size_t index) const
bool isDistribution() const
Are the Y-values dimensioned?
HistogramData::Histogram histogram(const size_t index) const
Returns the Histogram at the given workspace index.
Parallel::StorageMode storageMode() const
Returns the storage mode (used for MPI runs)
1D histogram implementation.
void setX(const Kernel::cow_ptr< HistogramData::HistogramX > &X) override
Deprecated, use setSharedX() instead.
Concrete workspace implementation.
std::size_t getNumberHistograms() const override
Returns the histogram number.
std::size_t size() const override
get pseudo size
bool isRaggedWorkspace() const override
Returns true if the workspace is ragged (has differently sized spectra).
std::vector< std::unique_ptr< Histogram1D > > data
A vector that holds the 1D histograms.
std::size_t getMaxNumberBins() const override
Returns the maximum number of bins in a workspace (works on ragged data).
void setImageYAndE(const API::MantidImage &imageY, const API::MantidImage &imageE, size_t start=0, bool loadAsRectImg=false, double scale_1=1.0, bool parallelExecution=true)
Copy the data from an image to this workspace's (Y's) and errors.
void init(const std::size_t &NVectors, const std::size_t &XLength, const std::size_t &YLength) override
Called by initialize()
Workspace2D * doClone() const override
Virtual clone method. Not implemented to force implementation in children.
void generateHistogram(const std::size_t index, const MantidVec &X, MantidVec &Y, MantidVec &E, bool skipError=false) const override
Generate a new histogram by rebinning the existing histogram.
~Workspace2D() override
Destructor.
virtual std::size_t getHistogramNumberHelper() const
Returns the number of histograms.
Histogram1D & getSpectrumWithoutInvalidation(const size_t index) override
Return reference to Histogram1D at the given workspace index.
std::size_t getNumberBins(const std::size_t &index) const override
Returns the number of bins for a given histogram index.
void setImageY(const API::MantidImage &image, size_t start=0, bool parallelExecution=true) override
Copy the data (Y's) from an image to this workspace.
void setImageE(const API::MantidImage &image, size_t start=0, bool parallelExecution=true) override
Copy the data from an image to this workspace's errors.
Workspace2D(const Parallel::StorageMode storageMode=Parallel::StorageMode::Cloned)
Constructor.
Histogram1D & getSpectrum(const size_t index) override
Return the underlying ISpectrum ptr at the given workspace index.
std::size_t blocksize() const override
get the size of each vector
Workspace2D * doCloneEmpty() const override
Virtual cloneEmpty method.
The concrete, templated class for properties.
std::vector< std::vector< double > > MantidImage
typedef for the image type
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
std::shared_ptr< const Workspace2D > Workspace2D_const_sptr
shared pointer to Mantid::DataObjects::Workspace2D (const version)
void MANTID_KERNEL_DLL rebin(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool distribution, bool addition=false)
Rebins data according to a new output X array.
void MANTID_KERNEL_DLL convertToBinBoundary(const std::vector< double > &bin_centers, std::vector< double > &bin_edges)
Convert an array of bin centers to bin boundary values.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
int32_t specnum_t
Typedef for a spectrum Number.
std::string to_string(const wide_integer< Bits, Signed > &n)