BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Histogram2D.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Histo/Histogram2D.h
6 //! @brief Defines class Histogram2D.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_DEVICE_HISTO_HISTOGRAM2D_H
16 #define BORNAGAIN_DEVICE_HISTO_HISTOGRAM2D_H
17 
18 #include "Device/Data/ArrayUtils.h"
20 
21 //! Two dimensional histogram.
22 //! @ingroup tools
23 
24 class Histogram2D : public IHistogram {
25 public:
26  //! @brief Constructor for fix bin size histograms.
27  //! @param nbinsx number of bins on X-axis
28  //! @param xlow low edge of the first bin of X-axis
29  //! @param xup upper edge of the last bin of X-axis
30  //! @param nbinsy number of bins on Y axis
31  //! @param ylow low edge of the first bin of Y-axis
32  //! @param yup upper edge of the last bin of Y-axis
33  Histogram2D(int nbinsx, double xlow, double xup, int nbinsy, double ylow, double yup);
34 
35  //! @brief Constructor for variable bin size histograms.
36  //! @param nbinsx number of bins on X-axis
37  //! @param xbins Array of size nbins+1 containing low-edges for each
38  //! bin and upper edge of last bin.
39  //! @param nbinsy number of bins on Y-axis
40  //! @param ybins Array of size nbins+1 containing low-edges for each
41  //! bin and upper edge of last bin.
42  Histogram2D(int nbinsx, const std::vector<double>& xbins, int nbinsy,
43  const std::vector<double>& ybins);
44 
45  //! Constructor for 2D histogram with custom axes
46  Histogram2D(const IAxis& axis_x, const IAxis& axis_y);
47 
48  //! Constructor for 2D histograms from basic OutputData object
49  Histogram2D(const OutputData<double>& data);
50 
51  //! Constructor for 2D histograms from numpy array (thanks to swig)
52  Histogram2D(std::vector<std::vector<double>> data);
53 
54  //! Returns clone of other histogram
55  Histogram2D* clone() const;
56 
57  //! Returns the number of histogram dimensions
58  size_t rank() const { return 2; }
59 
60  //! Increment bin with abscissa x and ordinate y with a weight.
61  int fill(double x, double y, double weight = 1.0);
62 
63  //! Project a 2D histogram into 1D histogram along X. The projection is made
64  //! from all bins along y-axis.
66 
67  //! @brief Project a 2D histogram into 1D histogram along X. The projection is made
68  //! from the y-bin closest to given ordinate yvalue.
69  //! @param yvalue the value on y-axis at which projection is taken
70  Histogram1D* projectionX(double yvalue);
71 
72  //! @brief Project a 2D histogram into 1D histogram along X. The projection is made from
73  //! all y-bins corresponding to ordinate between ylow and yup.
74  //! @param ylow lower edje on y-axis
75  //! @param yup upper edje on y-axis
76  Histogram1D* projectionX(double ylow, double yup);
77 
78  //! Project a 2D histogram into 1D histogram along Y. The projection is made
79  //! from all bins along x-axis.
81 
82  //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made
83  //! from the x-bin closest to given abscissa xvalue.
84  //! @param xvalue the value on x-axis at which projection is taken
85  Histogram1D* projectionY(double xvalue);
86 
87  //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made from
88  //! all x-bins corresponding to abscissa between xlow and xup.
89  //! @param xlow lower edje on x-axis
90  //! @param xup upper edje on x-axis
91  Histogram1D* projectionY(double xlow, double xup);
92 
93  //! Creates new histogram by applying rectangular clip.
94  Histogram2D* crop(double xmin, double ymin, double xmax, double ymax);
95 
96  //! Sets the values in histograms channels from numpy array,
97  void setContent(const std::vector<std::vector<double>>& data);
98 
99  //! Add to values in histograms channels from numpy array,
100  void addContent(const std::vector<std::vector<double>>& data);
101 
102 protected:
103  template <typename T> void initFromShape(const T& data);
104 
105  //! Creates projection along X. The projections is made by collecting the data in the range
106  //! between [ybinlow, ybinup].
107  Histogram1D* create_projectionX(int ybinlow, int ybinup);
108 
109  //! Creates projection along Y. The projections is made by collecting the data in the range
110  //! between [xbinlow, xbinup].
111  Histogram1D* create_projectionY(int xbinlow, int xbinup);
112 };
113 
114 template <typename T> void Histogram2D::initFromShape(const T& data)
115 {
116  auto shape = ArrayUtils::getShape(data);
117  const size_t nrows = shape.first;
118  const size_t ncols = shape.second;
119 
120  if (nrows == 0 || ncols == 0)
121  throw std::runtime_error("Histogram2D::Histogram2D() -> Error. "
122  "Not a two-dimensional numpy array");
123 
124  m_data.addAxis(FixedBinAxis("x-axis", ncols, 0.0, static_cast<double>(ncols)));
125  m_data.addAxis(FixedBinAxis("y-axis", nrows, 0.0, static_cast<double>(nrows)));
126 }
127 
128 #endif // BORNAGAIN_DEVICE_HISTO_HISTOGRAM2D_H
Defines various functions to interact from numpy on Python side.
Defines interface IHistogram.
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
One dimensional histogram.
Definition: Histogram1D.h:23
Two dimensional histogram.
Definition: Histogram2D.h:24
Histogram1D * create_projectionY(int xbinlow, int xbinup)
Creates projection along Y.
void setContent(const std::vector< std::vector< double >> &data)
Sets the values in histograms channels from numpy array,.
int fill(double x, double y, double weight=1.0)
Increment bin with abscissa x and ordinate y with a weight.
Definition: Histogram2D.cpp:52
void addContent(const std::vector< std::vector< double >> &data)
Add to values in histograms channels from numpy array,.
size_t rank() const
Returns the number of histogram dimensions.
Definition: Histogram2D.h:58
Histogram1D * projectionY()
Project a 2D histogram into 1D histogram along Y.
Definition: Histogram2D.cpp:81
void initFromShape(const T &data)
Definition: Histogram2D.h:114
Histogram2D * crop(double xmin, double ymin, double xmax, double ymax)
Creates new histogram by applying rectangular clip.
Definition: Histogram2D.cpp:99
Histogram1D * projectionX()
Project a 2D histogram into 1D histogram along X.
Definition: Histogram2D.cpp:63
Histogram2D(int nbinsx, double xlow, double xup, int nbinsy, double ylow, double yup)
Constructor for fix bin size histograms.
Definition: Histogram2D.cpp:20
Histogram1D * create_projectionX(int ybinlow, int ybinup)
Creates projection along X.
Histogram2D * clone() const
Returns clone of other histogram.
Definition: Histogram2D.cpp:47
Interface for one-dimensional axes.
Definition: IAxis.h:25
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
OutputData< CumulativeValue > m_data
Definition: IHistogram.h:193
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:295
std::pair< size_t, size_t > getShape(const T &data)
Returns shape nrows, ncols of 2D array.
Definition: ArrayUtils.h:127