File: Datafield.h

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (172 lines) | stat: -rw-r--r-- 6,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Device/Data/Datafield.h
//! @brief     Defines class Datafield.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_DEVICE_DATA_DATAFIELD_H
#define BORNAGAIN_DEVICE_DATA_DATAFIELD_H

#include "Base/Py/ArrayWrapper.h" // Arrayf64Wrapper
#include "Base/Type/Field2D.h"
#include <memory>
#include <string>
#include <vector>

using std::size_t;

class Frame;
class Scale;

//! Stores one- or two-dimensional data with associated scales.

class Datafield {
public:
    Datafield(const std::string& title, const Frame& frame, const std::vector<double>& values,
              const std::vector<double>& errSigmas = {});
    Datafield(const std::string& title, const Frame& frame);

    //! Constructor that takes ownership of supplied frame and initializes values and errorbars.
    Datafield(const Frame& frame, const std::vector<double>& values,
              const std::vector<double>& errSigmas = {});

    //! Constructor that takes ownership of supplied frame.
    Datafield(const Frame& frame);

    //! Constructor that takes ownership of supplied axes and initializes values and errorbars.
    Datafield(const std::vector<const Scale*>& axes, const std::vector<double>& values,
              const std::vector<double>& errSigmas = {});

    //! Constructor that takes ownership of supplied axes.
    Datafield(const std::vector<const Scale*>& axes);

    //! Constructor that creates equidistant grid from 2D array.
    Datafield(const std::string& xlabel, const std::string& ylabel, const double2d_t& vec);

    Datafield(const Datafield&);

#ifndef SWIG
    Datafield(Datafield&&) noexcept;

    Datafield* clone() const;
#endif

    virtual ~Datafield();

    //... modifiers

    void setAt(size_t i, double val);
    void setTitle(const std::string& title);

    //... retrieve contents

    const Frame& frame() const;
    size_t rank() const;
    const Scale& axis(size_t k) const;
    const Scale& xAxis() const;
    const Scale& yAxis() const;
    std::string title() const { return m_title; }

    //! Returns total size of data buffer (product of bin number in every dimension).
    size_t size() const;
    bool empty() const { return size() == 0; }

    //! Returns reference to the flat data.
    const std::vector<double>& flatVector() const { return m_values; }

    double maxVal() const;
    double minVal() const;

    double valAt(size_t i) const { return m_values[i]; }

#ifdef BORNAGAIN_PYTHON
    //! Returns data to construct a Python Numpy array.
    Arrayf64Wrapper xCenters() const;
    Arrayf64Wrapper dataArray() const;
    Arrayf64Wrapper errors() const;
#endif

    //... helpers that return modified a Datafield

    Datafield plottableField() const;
    Datafield flat() const;
    Datafield noisy(double prefactor, double minimum) const;
    Datafield normalizedToMax() const;

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

    //! Project a 2D histogram into 1D histogram along X. The projection is made
    //! from all bins along y-axis.
    Datafield* xProjection() const;

    //! @brief Project a 2D histogram into 1D histogram along X. The projection is made
    //! from the y-bin closest to given ordinate yvalue.
    //! @param yvalue the value on y-axis at which projection is taken
    Datafield* xProjection(double yvalue) const;

    //! @brief Project a 2D histogram into 1D histogram along X. The projection is made from
    //! all y-bins corresponding to ordinate between ylow and yup.
    //! @param ylow lower edje on y-axis
    //! @param yup upper edje on y-axis
    Datafield* xProjection(double ylow, double yup) const;

    //! Project a 2D histogram into 1D histogram along Y. The projection is made
    //! from all bins along x-axis.
    Datafield* yProjection() const;

    //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made
    //! from the x-bin closest to given abscissa xvalue.
    //! @param xvalue the value on x-axis at which projection is taken
    Datafield* yProjection(double xvalue) const;

    //! @brief Project a 2D histogram into 1D histogram along Y. The projection is made from
    //! all x-bins corresponding to abscissa between xlow and xup.
    //! @param xlow lower edje on x-axis
    //! @param xup upper edje on x-axis
    Datafield* yProjection(double xlow, double xup) const;

    //! Sets content of output data to specific value.
    void setAllTo(const double& value); // Py: test only; rm when possible

    const std::vector<double>& errorSigmas() const { return m_err_sigmas; }

#ifndef SWIG
    double& operator[](size_t i) { return m_values[i]; }
    const double& operator[](size_t i) const { return m_values[i]; }

    //! Sets new values to raw data vector.
    void setVector(const std::vector<double>& data_vector);
    void setVector2D(const double2d_t& in);

    bool hasErrorSigmas() const;
    std::vector<double>& errorSigmas() { return m_err_sigmas; }

    double2d_t values2D() const;

private:
    std::string m_title;
    std::unique_ptr<const Frame> m_frame;
    std::vector<double> m_values;
    std::vector<double> m_err_sigmas;

    //! Creates projection along X. The projections is made by collecting the data in the range
    //! between [ybinlow, ybinup].
    Datafield* create_xProjection(int ybinlow, int ybinup) const;

    //! Creates projection along Y. The projections is made by collecting the data in the range
    //! between [xbinlow, xbinup].
    Datafield* create_yProjection(int xbinlow, int xbinup) const;

#endif // SWIG
};

#endif // BORNAGAIN_DEVICE_DATA_DATAFIELD_H