BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Datafield Class Reference

Description

Stores radiation power per bin.

Definition at line 30 of file Datafield.h.

Collaboration diagram for Datafield:
[legend]

Public Member Functions

 Datafield (const Datafield &)=delete
 
 Datafield (const std::vector< IAxis * > &axes)
 Constructor that takes ownership of supplied axes. More...
 
 Datafield (const std::vector< IAxis * > &axes, const std::vector< double > &vector)
 Constructor that takes ownership of supplied axes. More...
 
 Datafield (Datafield &&)=default
 
 Datafield (Frame *frame)
 Constructor that takes ownership of supplied frame. More...
 
 Datafield (Frame *frame, const std::vector< double > &vector)
 Constructor that takes ownership of supplied frame. More...
 
 ~Datafield ()
 
const IAxisaxis (size_t k) const
 
Datafieldclone () const
 
Datafieldcrop (double xmin, double xmax) const
 
Datafieldcrop (double xmin, double ymin, double xmax, double ymax) const
 
std::vector< double > flatVector () const
 Returns copy of raw data vector. More...
 
const Frameframe () const
 
bool hasSameShape (const Datafield &other) const
 Returns true if objects a) have same dimensions b) bin boundaries of axes coincide. More...
 
bool hasSameSizes (const Datafield &other) const
 Returns true if object have same dimensions and number of axes bins. More...
 
double maxVal () const
 
double minVal () const
 
DatafieldnormalizedToMaximum () const
 
PyObjectnpArray () const
 Returns data as Python numpy array. More...
 
double & operator[] (size_t i)
 indexed accessor More...
 
const double & operator[] (size_t i) const
 ied accessor (const) More...
 
size_t rank () const
 
void setAllTo (const double &value)
 Sets content of output data to specific value. More...
 
void setAt (size_t i, double val)
 
void setVector (const std::vector< double > &data_vector)
 Sets new values to raw data vector. More...
 
size_t size () const
 Returns total size of data buffer (product of bin number in every dimension). More...
 
double valAt (size_t i) const
 
const IAxisxAxis () const
 
DatafieldxProjection ()
 Project a 2D histogram into 1D histogram along X. The projection is made from all bins along y-axis. More...
 
DatafieldxProjection (double ylow, double yup)
 Project a 2D histogram into 1D histogram along X. The projection is made from all y-bins corresponding to ordinate between ylow and yup. More...
 
DatafieldxProjection (double yvalue)
 Project a 2D histogram into 1D histogram along X. The projection is made from the y-bin closest to given ordinate yvalue. More...
 
const IAxisyAxis () const
 
DatafieldyProjection ()
 Project a 2D histogram into 1D histogram along Y. The projection is made from all bins along x-axis. More...
 
DatafieldyProjection (double xlow, double xup)
 Project a 2D histogram into 1D histogram along Y. The projection is made from all x-bins corresponding to abscissa between xlow and xup. More...
 
DatafieldyProjection (double xvalue)
 Project a 2D histogram into 1D histogram along Y. The projection is made from the x-bin closest to given abscissa xvalue. More...
 

Private Member Functions

Datafieldcreate_xProjection (int ybinlow, int ybinup)
 Creates projection along X. The projections is made by collecting the data in the range between [ybinlow, ybinup]. More...
 
Datafieldcreate_yProjection (int xbinlow, int xbinup)
 Creates projection along Y. The projections is made by collecting the data in the range between [xbinlow, xbinup]. More...
 

Private Attributes

std::unique_ptr< Framem_frame
 
std::vector< double > m_vec
 

Constructor & Destructor Documentation

◆ Datafield() [1/6]

Datafield::Datafield ( Frame frame)

Constructor that takes ownership of supplied frame.

Definition at line 24 of file Datafield.cpp.

25  : m_frame(frame)
26  , m_vec(m_frame->size())
27 {
28 }
std::vector< double > m_vec
Definition: Datafield.h:141
std::unique_ptr< Frame > m_frame
Definition: Datafield.h:140
const Frame & frame() const
Definition: Datafield.cpp:86

Referenced by clone(), create_xProjection(), create_yProjection(), crop(), and normalizedToMaximum().

◆ Datafield() [2/6]

Datafield::Datafield ( Frame frame,
const std::vector< double > &  vector 
)

Constructor that takes ownership of supplied frame.

Definition at line 30 of file Datafield.cpp.

31  : m_frame(frame)
32  , m_vec(vector)
33 {
34 }

◆ Datafield() [3/6]

Datafield::Datafield ( const std::vector< IAxis * > &  axes)

Constructor that takes ownership of supplied axes.

Definition at line 41 of file Datafield.cpp.

42  : Datafield(new Frame(axes))
43 {
44 }
Datafield(Frame *frame)
Constructor that takes ownership of supplied frame.
Definition: Datafield.cpp:24
Holds one or two axes.
Definition: Frame.h:27

◆ Datafield() [4/6]

Datafield::Datafield ( const std::vector< IAxis * > &  axes,
const std::vector< double > &  vector 
)

Constructor that takes ownership of supplied axes.

Definition at line 36 of file Datafield.cpp.

37  : Datafield(new Frame(axes), vector)
38 {
39 }

◆ Datafield() [5/6]

Datafield::Datafield ( const Datafield )
delete

◆ Datafield() [6/6]

Datafield::Datafield ( Datafield &&  )
default

◆ ~Datafield()

Datafield::~Datafield ( )

Definition at line 46 of file Datafield.cpp.

46 {}

Member Function Documentation

◆ axis()

◆ clone()

Datafield * Datafield::clone ( ) const

Definition at line 48 of file Datafield.cpp.

49 {
50  return new Datafield(m_frame->cloned_axes(), m_vec);
51 }

References Datafield(), m_frame, and m_vec.

Referenced by DataUtils::Data::createRearrangedDataSet().

Here is the call graph for this function:

◆ create_xProjection()

Datafield * Datafield::create_xProjection ( int  ybinlow,
int  ybinup 
)
private

Creates projection along X. The projections is made by collecting the data in the range between [ybinlow, ybinup].

Definition at line 260 of file Datafield.cpp.

261 {
262  std::vector<double> out(xAxis().size());
263  for (size_t i = 0; i < size(); ++i) {
264  int ybin = static_cast<int>(m_frame->projectedIndex(i, 1));
265  if (ybin >= ybinlow && ybin <= ybinup) {
266  double x = m_frame->projectedCoord(i, 0);
267  ASSERT(xAxis().contains(x));
268  size_t iout = xAxis().findClosestIndex(x);
269  out[iout] += valAt(i);
270  }
271  }
272  return new Datafield({xAxis().clone()}, out);
273 }
#define ASSERT(condition)
Definition: Assert.h:45
double valAt(size_t i) const
Definition: Datafield.cpp:58
size_t size() const
Returns total size of data buffer (product of bin number in every dimension).
Definition: Datafield.cpp:80
const IAxis & xAxis() const
Definition: Datafield.cpp:96
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
virtual IAxis * clone() const =0

References Datafield(), ASSERT, IAxis::clone(), IAxis::findClosestIndex(), m_frame, size(), valAt(), and xAxis().

Referenced by xProjection().

Here is the call graph for this function:

◆ create_yProjection()

Datafield * Datafield::create_yProjection ( int  xbinlow,
int  xbinup 
)
private

Creates projection along Y. The projections is made by collecting the data in the range between [xbinlow, xbinup].

Definition at line 275 of file Datafield.cpp.

276 {
277  std::vector<double> out(yAxis().size());
278  for (size_t i = 0; i < size(); ++i) {
279  int xbin = static_cast<int>(m_frame->projectedIndex(i, 0));
280  if (xbin >= xbinlow && xbin <= xbinup) {
281 
282  // TODO: duplicates code from create_xProjection, move to Frame ?
283 
284  double y = m_frame->projectedCoord(i, 1);
285  ASSERT(yAxis().contains(y));
286  size_t iout = yAxis().findClosestIndex(y);
287  out[iout] += valAt(i);
288  }
289  }
290  return new Datafield({yAxis().clone()}, out);
291 }
const IAxis & yAxis() const
Definition: Datafield.cpp:101

References Datafield(), ASSERT, IAxis::clone(), IAxis::findClosestIndex(), m_frame, size(), valAt(), and yAxis().

Referenced by yProjection().

Here is the call graph for this function:

◆ crop() [1/2]

Datafield * Datafield::crop ( double  xmin,
double  xmax 
) const

Definition at line 165 of file Datafield.cpp.

166 {
167  const std::unique_ptr<IAxis> xaxis{xAxis().clone()};
168  xaxis->clip(xmin, xmax);
169  std::vector<double> out(size());
170  size_t iout = 0;
171  for (size_t i = 0; i < size(); ++i) {
172  const double x = m_frame->projectedCoord(i, 0);
173  if (xaxis->contains(x))
174  out[iout++] = m_vec[i];
175  }
176  return new Datafield(m_frame->cloned_axes(), out);
177 }
virtual void clip(double lower, double upper)
Clips this axis to the given values.
Definition: IAxis.cpp:35

References Datafield(), IAxis::clip(), IAxis::clone(), m_frame, m_vec, size(), and xAxis().

Here is the call graph for this function:

◆ crop() [2/2]

Datafield * Datafield::crop ( double  xmin,
double  ymin,
double  xmax,
double  ymax 
) const

Definition at line 147 of file Datafield.cpp.

148 {
149  const std::unique_ptr<IAxis> xaxis{xAxis().clone()};
150  const std::unique_ptr<IAxis> yaxis{yAxis().clone()};
151  xaxis->clip(xmin, xmax);
152  yaxis->clip(ymin, ymax);
153 
154  std::vector<double> out(size());
155  size_t iout = 0;
156  for (size_t i = 0; i < size(); ++i) {
157  double x = m_frame->projectedCoord(i, 0);
158  double y = m_frame->projectedCoord(i, 1);
159  if (xaxis->contains(x) && yaxis->contains(y))
160  out[iout++] = m_vec[i];
161  }
162  return new Datafield(m_frame->cloned_axes(), out);
163 }

References Datafield(), IAxis::clip(), IAxis::clone(), m_frame, m_vec, size(), xAxis(), and yAxis().

Here is the call graph for this function:

◆ flatVector()

std::vector< double > Datafield::flatVector ( ) const

Returns copy of raw data vector.

Definition at line 119 of file Datafield.cpp.

120 {
121  std::vector<double> ret(size());
122  for (size_t i = 0; i < size(); ++i)
123  ret[i] = (*this)[i];
124  return ret;
125 }

References size().

Referenced by ConvolutionDetectorResolution::apply1dConvolution(), ConvolutionDetectorResolution::apply2dConvolution(), ICoordSystem::createConvertedData(), CoordSystem1D::createConvertedData(), DataUtils::Array::createVector1D(), SimDataPair::experimental_array(), SimDataPair::simulation_array(), SimDataPair::uncertainties_array(), and SimDataPair::user_weights_array().

Here is the call graph for this function:

◆ frame()

◆ hasSameShape()

bool Datafield::hasSameShape ( const Datafield other) const

Returns true if objects a) have same dimensions b) bin boundaries of axes coincide.

Returns true if object have same dimensions and shape of axis.

Definition at line 114 of file Datafield.cpp.

115 {
116  return frame() == other.frame();
117 }

References frame().

Referenced by DiffUtil::relativeDifferenceField().

Here is the call graph for this function:

◆ hasSameSizes()

bool Datafield::hasSameSizes ( const Datafield other) const

Returns true if object have same dimensions and number of axes bins.

Returns true if object have same dimensions.

Definition at line 108 of file Datafield.cpp.

109 {
110  return frame().hasSameSizes(other.frame());
111 }
bool hasSameSizes(const Frame &) const
Returns true if both Frames have same rank, and all axes have same sizes.
Definition: Frame.cpp:102

References frame(), and Frame::hasSameSizes().

Here is the call graph for this function:

◆ maxVal()

double Datafield::maxVal ( ) const

Definition at line 137 of file Datafield.cpp.

138 {
139  return *std::max_element(m_vec.begin(), m_vec.end());
140 }

References m_vec.

Referenced by normalizedToMaximum().

◆ minVal()

double Datafield::minVal ( ) const

Definition at line 142 of file Datafield.cpp.

143 {
144  return *std::min_element(m_vec.begin(), m_vec.end());
145 }

References m_vec.

◆ normalizedToMaximum()

Datafield * Datafield::normalizedToMaximum ( ) const

Definition at line 127 of file Datafield.cpp.

128 {
129  double maxval = maxVal();
130  ASSERT(maxval > 0);
131  std::vector<double> out(frame().size());
132  for (size_t i = 0; i < frame().size(); ++i)
133  out[i] = m_vec[i] / maxval;
134  return new Datafield(m_frame->cloned_axes(), out);
135 }
double maxVal() const
Definition: Datafield.cpp:137
size_t size() const
Returns total number of bins.
Definition: Frame.h:37

References Datafield(), ASSERT, frame(), m_frame, m_vec, maxVal(), Frame::size(), and size().

Here is the call graph for this function:

◆ npArray()

PyObject * Datafield::npArray ( ) const

Returns data as Python numpy array.

Definition at line 181 of file Datafield.cpp.

182 {
183  std::vector<size_t> dimensions;
184  for (size_t i = 0; i < rank(); i++)
185  dimensions.push_back(axis(i).size());
186 
187  // for rot90 of 2-dim arrays to conform with numpy
188  if (dimensions.size() == 2)
189  std::swap(dimensions[0], dimensions[1]);
190 
191  // creating ndarray objects describing size of dimensions
192  npy_int ndim_numpy = (int)dimensions.size();
193  auto* ndimsizes_numpy = new npy_intp[dimensions.size()];
194  for (size_t i = 0; i < dimensions.size(); i++)
195  ndimsizes_numpy[i] = dimensions[i];
196 
197  // creating standalone numpy array
198  PyObject* pyarray = PyArray_SimpleNew(ndim_numpy, ndimsizes_numpy, NPY_DOUBLE);
199  delete[] ndimsizes_numpy;
200  ASSERT(pyarray);
201 
202  // getting pointer to data buffer of numpy array
203  double* array_buffer = (double*)PyArray_DATA((PyArrayObject*)pyarray);
204 
205  // filling numpy array with output_data
206  if (rank() == 2) {
207  for (size_t i = 0; i < size(); ++i) {
208  std::vector<int> axes_indices = m_frame->allIndices(i);
209  size_t offset =
210  axes_indices[0] + axis(0).size() * (axis(1).size() - 1 - axes_indices[1]);
211  array_buffer[offset] = (*this)[i];
212  }
213  } else {
214  for (size_t i = 0; i < size(); ++i)
215  *array_buffer++ = (*this)[i];
216  }
217 
218  return pyarray;
219 }
_object PyObject
Definition: PyObject.h:25
const IAxis & axis(size_t k) const
Definition: Datafield.cpp:91
size_t rank() const
Definition: Datafield.cpp:75
virtual size_t size() const =0
Returns the number of bins.

References ASSERT, axis(), m_frame, rank(), size(), and IAxis::size().

Here is the call graph for this function:

◆ operator[]() [1/2]

double& Datafield::operator[] ( size_t  i)
inline

indexed accessor

Definition at line 78 of file Datafield.h.

78 { return m_vec[i]; }

References m_vec.

◆ operator[]() [2/2]

const double& Datafield::operator[] ( size_t  i) const
inline

ied accessor (const)

Definition at line 81 of file Datafield.h.

81 { return m_vec[i]; }

References m_vec.

◆ rank()

◆ setAllTo()

void Datafield::setAllTo ( const double &  value)

Sets content of output data to specific value.

Definition at line 63 of file Datafield.cpp.

64 {
65  for (double& e : m_vec)
66  e = value;
67 }

References m_vec.

◆ setAt()

void Datafield::setAt ( size_t  i,
double  val 
)

Definition at line 53 of file Datafield.cpp.

54 {
55  m_vec[i] = val;
56 }

References m_vec.

◆ setVector()

void Datafield::setVector ( const std::vector< double > &  data_vector)

Sets new values to raw data vector.

Definition at line 69 of file Datafield.cpp.

70 {
71  ASSERT(vector.size() == frame().size());
72  m_vec = vector;
73 }

References ASSERT, frame(), m_vec, and size().

Referenced by ConvolutionDetectorResolution::apply1dConvolution(), IDetector::applyDetectorResolution(), ICoordSystem::createConvertedData(), and CoordSystem1D::createConvertedData().

Here is the call graph for this function:

◆ size()

size_t Datafield::size ( ) const

Returns total size of data buffer (product of bin number in every dimension).

Definition at line 80 of file Datafield.cpp.

81 {
82  ASSERT(frame().size() == m_vec.size());
83  return frame().size();
84 }

References ASSERT, frame(), m_vec, and Frame::size().

Referenced by ConvolutionDetectorResolution::apply2dConvolution(), create_xProjection(), create_yProjection(), CoordSystem1D::createConvertedData(), DataUtils::Data::createRearrangedDataSet(), crop(), DataUtils::Format::fillDatafield(), flatVector(), normalizedToMaximum(), npArray(), DiffUtil::relativeDifferenceField(), setVector(), ReadWriteINT::writeDatafieldDoubles(), xProjection(), and yProjection().

Here is the call graph for this function:

◆ valAt()

double Datafield::valAt ( size_t  i) const

Definition at line 58 of file Datafield.cpp.

59 {
60  return m_vec[i];
61 }

References m_vec.

Referenced by create_xProjection(), create_yProjection(), and DiffUtil::relativeDifferenceField().

◆ xAxis()

const IAxis & Datafield::xAxis ( ) const

Definition at line 96 of file Datafield.cpp.

97 {
98  return m_frame->axis(0);
99 }

References m_frame.

Referenced by create_xProjection(), crop(), DataUtils::Data::FindPeaks(), xProjection(), and yProjection().

◆ xProjection() [1/3]

Datafield * Datafield::xProjection ( )

Project a 2D histogram into 1D histogram along X. The projection is made from all bins along y-axis.

Definition at line 224 of file Datafield.cpp.

225 {
226  return create_xProjection(0, static_cast<int>(xAxis().size()) - 1);
227 }
Datafield * create_xProjection(int ybinlow, int ybinup)
Creates projection along X. The projections is made by collecting the data in the range between [ybin...
Definition: Datafield.cpp:260

References create_xProjection(), size(), and xAxis().

Here is the call graph for this function:

◆ xProjection() [2/3]

Datafield * Datafield::xProjection ( double  ylow,
double  yup 
)

Project a 2D histogram into 1D histogram along X. The projection is made from all y-bins corresponding to ordinate between ylow and yup.

Parameters
ylowlower edje on y-axis
yupupper edje on y-axis

Definition at line 235 of file Datafield.cpp.

236 {
237  int ybinlow = static_cast<int>(yAxis().findClosestIndex(ylow));
238  int ybinup = static_cast<int>(yAxis().findClosestIndex(yup));
239  return create_xProjection(ybinlow, ybinup);
240 }

References create_xProjection(), IAxis::findClosestIndex(), and yAxis().

Here is the call graph for this function:

◆ xProjection() [3/3]

Datafield * Datafield::xProjection ( double  yvalue)

Project a 2D histogram into 1D histogram along X. The projection is made from the y-bin closest to given ordinate yvalue.

Parameters
yvaluethe value on y-axis at which projection is taken

Definition at line 229 of file Datafield.cpp.

230 {
231  int ybin_selected = static_cast<int>(yAxis().findClosestIndex(yvalue));
232  return create_xProjection(ybin_selected, ybin_selected);
233 }

References create_xProjection(), IAxis::findClosestIndex(), and yAxis().

Here is the call graph for this function:

◆ yAxis()

const IAxis & Datafield::yAxis ( ) const

Definition at line 101 of file Datafield.cpp.

102 {
103  return m_frame->axis(1);
104 }

References m_frame.

Referenced by create_yProjection(), crop(), DataUtils::Data::FindPeaks(), and xProjection().

◆ yProjection() [1/3]

Datafield * Datafield::yProjection ( )

Project a 2D histogram into 1D histogram along Y. The projection is made from all bins along x-axis.

Definition at line 242 of file Datafield.cpp.

243 {
244  return create_yProjection(0, static_cast<int>(xAxis().size()) - 1);
245 }
Datafield * create_yProjection(int xbinlow, int xbinup)
Creates projection along Y. The projections is made by collecting the data in the range between [xbin...
Definition: Datafield.cpp:275

References create_yProjection(), size(), and xAxis().

Here is the call graph for this function:

◆ yProjection() [2/3]

Datafield * Datafield::yProjection ( double  xlow,
double  xup 
)

Project a 2D histogram into 1D histogram along Y. The projection is made from all x-bins corresponding to abscissa between xlow and xup.

Parameters
xlowlower edje on x-axis
xupupper edje on x-axis

Definition at line 253 of file Datafield.cpp.

254 {
255  int xbinlow = static_cast<int>(xAxis().findClosestIndex(xlow));
256  int xbinup = static_cast<int>(xAxis().findClosestIndex(xup));
257  return create_yProjection(xbinlow, xbinup);
258 }

References create_yProjection(), IAxis::findClosestIndex(), and xAxis().

Here is the call graph for this function:

◆ yProjection() [3/3]

Datafield * Datafield::yProjection ( double  xvalue)

Project a 2D histogram into 1D histogram along Y. The projection is made from the x-bin closest to given abscissa xvalue.

Parameters
xvaluethe value on x-axis at which projection is taken

Definition at line 247 of file Datafield.cpp.

248 {
249  int xbin_selected = static_cast<int>(xAxis().findClosestIndex(xvalue));
250  return create_yProjection(xbin_selected, xbin_selected);
251 }

References create_yProjection(), IAxis::findClosestIndex(), and xAxis().

Here is the call graph for this function:

Member Data Documentation

◆ m_frame

std::unique_ptr<Frame> Datafield::m_frame
private

◆ m_vec

std::vector<double> Datafield::m_vec
private

The documentation for this class was generated from the following files: