BornAgain  1.18.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 scattering at grazing incidence
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_CORE_INTENSITY_HISTOGRAM2D_H
16 #define BORNAGAIN_CORE_INTENSITY_HISTOGRAM2D_H
17 
20 
21 //! Two dimensional histogram.
22 //! @ingroup tools
23 
24 class Histogram2D : public IHistogram
25 {
26 public:
27  //! @brief Constructor for fix bin size histograms.
28  //! @param nbinsx number of bins on X-axis
29  //! @param xlow low edge of the first bin of X-axis
30  //! @param xup upper edge of the last bin of X-axis
31  //! @param nbinsy number of bins on Y axis
32  //! @param ylow low edge of the first bin of Y-axis
33  //! @param yup upper edge of the last bin of Y-axis
34  Histogram2D(int nbinsx, double xlow, double xup, int nbinsy, double ylow, double yup);
35 
36  //! @brief Constructor for variable bin size histograms.
37  //! @param nbinsx number of bins on X-axis
38  //! @param xbins Array of size nbins+1 containing low-edges for each
39  //! bin and upper edge of last bin.
40  //! @param nbinsy number of bins on Y-axis
41  //! @param ybins Array of size nbins+1 containing low-edges for each
42  //! bin and upper edge of last bin.
43  Histogram2D(int nbinsx, const std::vector<double>& xbins, int nbinsy,
44  const std::vector<double>& ybins);
45 
46  //! Constructor for 2D histogram with custom axes
47  Histogram2D(const IAxis& axis_x, const IAxis& axis_y);
48 
49  //! Constructor for 2D histograms from basic OutputData object
50  Histogram2D(const OutputData<double>& data);
51 
52  //! Constructor for 2D histograms from numpy array (thanks to swig)
53  Histogram2D(std::vector<std::vector<double>> data);
54 
55  //! Returns clone of other histogram
56  Histogram2D* clone() const;
57 
58  //! Returns the number of histogram dimensions
59  size_t getRank() const { return 2; }
60 
61  //! Increment bin with abscissa x and ordinate y with a weight.
62  int fill(double x, double y, double weight = 1.0);
63 
64  //! Project a 2D histogram into 1D histogram along X. The projection is made
65  //! from all bins along y-axis.
67 
68  //! @brief Project a 2D histogram into 1D histogram along X. The projection is made
69  //! from the y-bin closest to given ordinate yvalue.
70  //! @param yvalue the value on y-axis at which projection is taken
71  Histogram1D* projectionX(double yvalue);
72 
73  //! @brief Project a 2D histogram into 1D histogram along X. The projection is made from
74  //! all y-bins corresponding to ordinate between ylow and yup.
75  //! @param ylow lower edje on y-axis
76  //! @param yup upper edje on y-axis
77  Histogram1D* projectionX(double ylow, double yup);
78 
79  //! Project a 2D histogram into 1D histogram along Y. The projection is made
80  //! from all bins along x-axis.
82 
83  //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made
84  //! from the x-bin closest to given abscissa xvalue.
85  //! @param xvalue the value on x-axis at which projection is taken
86  Histogram1D* projectionY(double xvalue);
87 
88  //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made from
89  //! all x-bins corresponding to abscissa between xlow and xup.
90  //! @param xlow lower edje on x-axis
91  //! @param xup upper edje on x-axis
92  Histogram1D* projectionY(double xlow, double xup);
93 
94  //! Creates new histogram by applying rectangular clip.
95  Histogram2D* crop(double xmin, double ymin, double xmax, double ymax);
96 
97  //! Sets the values in histograms channels from numpy array,
98  void setContent(const std::vector<std::vector<double>>& data);
99 
100  //! Add to values in histograms channels from numpy array,
101  void addContent(const std::vector<std::vector<double>>& data);
102 
103 protected:
104  template <typename T> void initFromShape(const T& data);
105 
106  //! Creates projection along X. The projections is made by collecting the data in the range
107  //! between [ybinlow, ybinup].
108  Histogram1D* create_projectionX(int ybinlow, int ybinup);
109 
110  //! Creates projection along Y. The projections is made by collecting the data in the range
111  //! between [xbinlow, xbinup].
112  Histogram1D* create_projectionY(int xbinlow, int xbinup);
113 };
114 
115 template <typename T> void Histogram2D::initFromShape(const T& data)
116 {
117  auto shape = ArrayUtils::getShape(data);
118  const size_t nrows = shape.first;
119  const size_t ncols = shape.second;
120 
121  if (nrows == 0 || ncols == 0)
122  throw Exceptions::LogicErrorException("Histogram2D::Histogram2D() -> Error. "
123  "Not a two-dimensional numpy array");
124 
125  m_data.addAxis(FixedBinAxis("x-axis", ncols, 0.0, static_cast<double>(ncols)));
126  m_data.addAxis(FixedBinAxis("y-axis", nrows, 0.0, static_cast<double>(nrows)));
127 }
128 
129 #endif // BORNAGAIN_CORE_INTENSITY_HISTOGRAM2D_H
Defines various functions to interact from numpy on Python side.
Defines class IHistogram.
Axis with fixed bin size.
Definition: FixedBinAxis.h:24
One dimensional histogram.
Definition: Histogram1D.h:24
Two dimensional histogram.
Definition: Histogram2D.h:25
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,.
Histogram1D * projectionY()
Project a 2D histogram into 1D histogram along Y.
Definition: Histogram2D.cpp:81
void initFromShape(const T &data)
Definition: Histogram2D.h:115
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
size_t getRank() const
Returns the number of histogram dimensions.
Definition: Histogram2D.h:59
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:289
std::pair< size_t, size_t > getShape(const T &data)
Returns shape nrows, ncols of 2D array.
Definition: ArrayUtils.h:123