13#include "MantidHistogramData/LinearGenerator.h"
32 data.resize(other.data.size());
33 for (
size_t i = 0; i <
data.size(); ++i) {
34 data[i] = std::make_unique<Histogram1D>(*(other.data[i]));
53void Workspace2D::init(
const std::size_t &NVectors,
const std::size_t &XLength,
const std::size_t &YLength) {
54 data.resize(NVectors);
56 auto x = Kernel::make_cow<HistogramData::HistogramX>(XLength, HistogramData::LinearGenerator(1.0, 1.0));
57 HistogramData::Counts
y(YLength);
58 HistogramData::CountStandardDeviations
e(YLength);
59 Histogram1D spec(HistogramData::getHistogramXMode(XLength, YLength), HistogramData::Histogram::YMode::Counts);
63 for (
size_t i = 0; i <
data.size(); i++) {
64 data[i] = std::make_unique<Histogram1D>(spec);
71 m_axes[0] = std::make_unique<API::RefAxis>(
this);
72 m_axes[1] = std::make_unique<API::SpectraAxis>(
this);
78 HistogramData::Histogram initializedHistogram(
histogram);
80 if (
histogram.yMode() == HistogramData::Histogram::YMode::Frequencies) {
81 initializedHistogram.setFrequencies(
histogram.size(), 0.0);
82 initializedHistogram.setFrequencyStandardDeviations(
histogram.size(), 0.0);
84 initializedHistogram.setCounts(
histogram.size(), 0.0);
85 initializedHistogram.setCountStandardDeviations(
histogram.size(), 0.0);
89 Histogram1D spec(initializedHistogram.xMode(), initializedHistogram.yMode());
92 [&spec](
const auto &) { return std::make_unique<Histogram1D>(spec); });
96 m_axes[0] = std::make_unique<API::RefAxis>(
this);
97 m_axes[1] = std::make_unique<API::SpectraAxis>(
this);
104 throw std::runtime_error(
"There is no data in the Workspace2D, "
105 "therefore cannot determine if it is ragged.");
107 const auto numberOfBins =
data[0]->size();
108 return std::any_of(
data.cbegin(),
data.cend(),
109 [&numberOfBins](
const auto &
histogram) { return numberOfBins != histogram->size(); });
120 return std::accumulate(
121 data.begin(),
data.end(),
static_cast<size_t>(0),
122 [](
const size_t value,
const std::unique_ptr<Histogram1D> &histo) { return value + histo->size(); });
130 size_t numBins =
data[0]->size();
132 std::find_if_not(
data.cbegin(),
data.cend(), [numBins](
const auto &iter) { return numBins == iter->size(); });
133 if (it !=
data.cend())
134 throw std::length_error(
"blocksize undefined because size of histograms is not equal");
147 throw std::invalid_argument(
"Could not find number of bins in a histogram at index " +
std::to_string(
index) +
148 ": index is too large.");
158 auto maxNumberOfBins =
data[0]->size();
159 for (
const auto &iter :
data) {
160 const auto numberOfBins = iter->size();
161 if (numberOfBins > maxNumberOfBins)
162 maxNumberOfBins = numberOfBins;
164 return maxNumberOfBins;
207 bool loadAsRectImg,
double scale_1, [[maybe_unused]]
bool parallelExecution) {
208 if (imageY.empty() && imageE.empty())
210 if (imageY.empty() && imageE[0].empty())
212 if (imageE.empty() && imageY[0].empty())
216 if (!loadAsRectImg && numBins != 1) {
217 throw std::runtime_error(
"Cannot set image in workspace: a single bin workspace is "
218 "required when initializing a workspace from an "
219 "image using a histogram per pixel.");
224 if (!imageY.empty()) {
226 width = imageY.front().size();
229 width = imageE.front().size();
231 size_t dataSize = width *
height;
234 throw std::runtime_error(
"Cannot set image: image is bigger than workspace.");
237 if (!loadAsRectImg) {
240 for (
int i = 0; i < static_cast<int>(
height); ++i) {
242 const auto &rowY = imageY[i];
243 const auto &rowE = imageE[i];
244 size_t spec = start +
static_cast<size_t>(i) * width;
245 auto pE = rowE.begin();
246 for (
auto pY = rowY.begin(); pY != rowY.end() && pE != rowE.end(); ++pY, ++pE, ++spec) {
247 data[spec]->dataY()[0] = *pY;
248 data[spec]->dataE()[0] = *pE;
254 throw std::runtime_error(std::string(
"To load an image into a workspace with one spectrum per "
255 "row, then number of spectra (") +
260 if (width != numBins)
261 throw std::runtime_error(std::string(
"To load an image into a workspace with one spectrum per "
262 "row, then number of bins (") +
263 std::to_string(numBins) +
") needs to be equal to the width (columns) of the image (" +
268 for (
int i = 0; i < static_cast<int>(
height); ++i) {
270 const auto &rowY = imageY[i];
271 const auto &rowE = imageE[i];
272 data[i]->dataY() = rowY;
273 data[i]->dataE() = rowE;
278 for (
int i = 0; i < static_cast<int>(width) + 1; ++i) {
279 data[0]->dataX()[i] = i * scale_1;
282 for (
int i = 1; i < static_cast<int>(
height); ++i) {
298 std::ostringstream ss;
299 ss <<
"Workspace2D::getSpectrum, histogram number " <<
index <<
" out of range " <<
data.
size();
300 throw std::range_error(ss.str());
326 bool skipError)
const {
329 throw std::range_error(
"Workspace2D::generateHistogram, histogram number out of range");
332 const MantidVec ¤tX = spec.readX();
333 const MantidVec ¤tY = spec.readY();
334 const MantidVec ¤tE = spec.readE();
336 throw std::runtime_error(
"Workspace2D::generateHistogram(): X vector must be at least length 2");
337 Y.resize(
X.size() - 1, 0);
338 E.resize(
X.size() - 1, 0);
341 if (currentX.size() == currentY.size())
343 std::vector<double> histX;
344 histX.resize(currentX.size() + 1);
360IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_sptr>(
const std::string &
name)
const {
365 std::string message =
366 "Attempt to assign property " +
name +
" to incorrect type. Expected shared_ptr<Workspace2D>.";
367 throw std::runtime_error(message);
373IPropertyManager::getValue<Mantid::DataObjects::Workspace2D_const_sptr>(
const std::string &
name)
const {
377 return prop->operator()();
379 std::string message =
380 "Attempt to assign property " +
name +
" to incorrect type. Expected const shared_ptr<Workspace2D>.";
381 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.
1D histogram implementation.
virtual std::size_t size() const
get pseudo size
void setX(const Kernel::cow_ptr< HistogramData::HistogramX > &X) override
Deprecated, use setSharedX() instead.
Concrete workspace implementation.
Workspace2D()
Constructor.
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.
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)