File: Datafield.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 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 (374 lines) | stat: -rw-r--r-- 10,354 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Device/Data/Datafield.cpp
//! @brief     Implements class Datafield.cpp.
//!
//! @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)
//
//  ************************************************************************************************

#include "Device/Data/Datafield.h"
#include "Base/Axis/Frame.h"
#include "Base/Axis/MakeScale.h"
#include "Base/Axis/Scale.h"
#include "Base/Util/Assert.h"
#include <algorithm>
#include <random>

#ifdef BORNAGAIN_PYTHON
namespace {

Arrayf64Wrapper arrayExport(const Frame& frame, const std::vector<double>& flatData,
                            const bool owndata)
{
    ASSERT(frame.rank() <= 2);

    const std::size_t n_dims = frame.rank();
    std::vector<std::size_t> dimensions(n_dims);

    for (std::size_t i = 0; i < n_dims; i++) {
        dimensions[i] = frame.axis(i).size();
    }

    // for rot90 of 2-dim arrays to conform with numpy
    // TODO: why this rotation is required?
    if (n_dims == 2)
        std::swap(dimensions[0], dimensions[1]);

    return {flatData.size(), n_dims, dimensions.data(), flatData.data(), owndata};
}

} // namespace

#endif // BORNAGAIN_PYTHON


Datafield::Datafield(const std::string& title, const Frame& frame,
                     const std::vector<double>& values, const std::vector<double>& errSigmas)
    : m_title(title)
    , m_frame(frame.clone())
    , m_values(values)
    , m_err_sigmas(errSigmas)
{
    ASSERT(m_frame);
    ASSERT(m_values.size() == m_frame->size());
    ASSERT(m_err_sigmas.empty() || m_err_sigmas.size() == m_values.size());
}

Datafield::Datafield(const std::string& title, const Frame& frame)
    : Datafield(title, frame, std::vector<double>(frame.size(), 0.))
{
}

Datafield::Datafield(const Frame& frame, const std::vector<double>& values,
                     const std::vector<double>& errSigmas)
    : Datafield("", frame, values, errSigmas)
{
}

Datafield::Datafield(const Frame& frame)
    : Datafield("", frame)
{
}

Datafield::Datafield(const std::vector<const Scale*>& axes, const std::vector<double>& values,
                     const std::vector<double>& errSigmas)
    : Datafield(Frame(axes), values, errSigmas)
{
}

Datafield::Datafield(const std::vector<const Scale*>& axes)
    : Datafield(Frame(axes))
{
}

Datafield::Datafield(const std::string& xlabel, const std::string& ylabel, const double2d_t& vec)
{
    const size_t nrows = vec.size();
    const size_t ncols = vec[0].size();
    ASSERT(nrows > 0);
    ASSERT(ncols > 0);

    std::vector<const Scale*> axes;
    if (nrows < 2)
        axes = {newEquiDivision(xlabel, ncols, 0.0, (double)ncols)};
    else if (ncols < 2)
        axes = {newEquiDivision(ylabel, nrows, 0.0, (double)nrows)};
    else
        axes = {newEquiDivision(xlabel, ncols, 0.0, (double)ncols),
                newEquiDivision(ylabel, nrows, 0.0, (double)nrows)};
    m_frame = std::make_unique<Frame>(axes);

    setVector2D(vec);
}

Datafield::Datafield(Datafield&&) noexcept = default;

Datafield::Datafield(const Datafield& other)
    : Datafield(other.title(), *other.m_frame, other.m_values, other.m_err_sigmas)
{
}

Datafield::~Datafield() = default;

Datafield* Datafield::clone() const
{
    return new Datafield(title(), frame(), m_values, m_err_sigmas);
}

void Datafield::setTitle(const std::string& title)
{
    m_title = title;
}

void Datafield::setAt(size_t i, double val)
{
    m_values[i] = val;
}

bool Datafield::hasErrorSigmas() const
{
    return !m_err_sigmas.empty();
}

void Datafield::setAllTo(const double& value)
{
    for (double& v : m_values)
        v = value;
}

void Datafield::setVector(const std::vector<double>& vector)
{
    ASSERT(vector.size() == frame().size());
    m_values = vector;
}

void Datafield::setVector2D(const double2d_t& in)
{
    m_values = FieldUtil::flatten(in);
}

size_t Datafield::rank() const
{
    return frame().rank();
}

size_t Datafield::size() const
{
    ASSERT(frame().size() == m_values.size());
    return frame().size();
}

const Frame& Datafield::frame() const
{
    ASSERT(m_frame);
    return *m_frame;
}

const Scale& Datafield::axis(size_t k) const
{
    return frame().axis(k);
}

const Scale& Datafield::xAxis() const
{
    return frame().axis(0);
}

const Scale& Datafield::yAxis() const
{
    return frame().axis(1);
}

double Datafield::maxVal() const
{
    return *std::max_element(m_values.begin(), m_values.end());
}

double Datafield::minVal() const
{
    return *std::min_element(m_values.begin(), m_values.end());
}

Datafield* Datafield::crop(double xmin, double ymin, double xmax, double ymax) const
{
    ASSERT(rank() == 2);

    const size_t N = size();
    std::vector<double> out;
    for (size_t i = 0; i < N; ++i) {
        const Bin1D& x_bin = frame().projectedBin(i, 0);
        const Bin1D& y_bin = frame().projectedBin(i, 1);
        if (xmin <= x_bin.max() && x_bin.min() <= xmax && ymin <= y_bin.max()
            && y_bin.min() <= ymax)
            out.push_back(m_values[i]);
    }

    const Scale* xclipped = xAxis().clipped(xmin, xmax).clone();
    const Scale* yclipped = yAxis().clipped(ymin, ymax).clone();
    const Frame outframe = Frame(xclipped, yclipped);

    ASSERT(outframe.size() == out.size());
    return new Datafield(outframe, out);
}

Datafield* Datafield::crop(double xmin, double xmax) const
{
    ASSERT(rank() == 1);

    const size_t N = size();
    std::vector<double> out;
    std::vector<double> errout;
    for (size_t i = 0; i < N; ++i) {
        const Bin1D& x_bin = frame().projectedBin(i, 0);
        if (xmin <= x_bin.max() && x_bin.min() <= xmax) {
            out.push_back(m_values[i]);
            if (hasErrorSigmas())
                errout.push_back(m_err_sigmas[i]);
        }
    }
    const Scale* xclipped = xAxis().clipped(xmin, xmax).clone();
    const Frame outframe = Frame(xclipped);

    ASSERT(outframe.xAxis().size() == out.size());
    return new Datafield(outframe, out, errout);
}

#ifdef BORNAGAIN_PYTHON

Arrayf64Wrapper Datafield::xCenters() const
{
    // NOTE: the array data is produced on the fly;
    // therefore, the ArrayDescriptor must store the data.
    return ::arrayExport(Frame(xAxis().clone()), xAxis().binCenters(),
                         /* owndata= */ true);
}

Arrayf64Wrapper Datafield::dataArray() const
{
    return ::arrayExport(frame(), flatVector(), /* owndata= */ false);
}

Arrayf64Wrapper Datafield::errors() const
{
    return ::arrayExport(frame(), errorSigmas(), /* owndata= */ false);
}

#endif // BORNAGAIN_PYTHON

Datafield* Datafield::xProjection() const
{
    return create_xProjection(0, static_cast<int>(xAxis().size()) - 1);
}

Datafield* Datafield::xProjection(double yvalue) const
{
    int ybin_selected = static_cast<int>(yAxis().closestIndex(yvalue));
    return create_xProjection(ybin_selected, ybin_selected);
}

Datafield* Datafield::xProjection(double ylow, double yup) const
{
    int ybinlow = static_cast<int>(yAxis().closestIndex(ylow));
    int ybinup = static_cast<int>(yAxis().closestIndex(yup));
    return create_xProjection(ybinlow, ybinup);
}

Datafield* Datafield::yProjection() const
{
    return create_yProjection(0, static_cast<int>(xAxis().size()) - 1);
}

Datafield* Datafield::yProjection(double xvalue) const
{
    int xbin_selected = static_cast<int>(xAxis().closestIndex(xvalue));
    return create_yProjection(xbin_selected, xbin_selected);
}

Datafield* Datafield::yProjection(double xlow, double xup) const
{
    int xbinlow = static_cast<int>(xAxis().closestIndex(xlow));
    int xbinup = static_cast<int>(xAxis().closestIndex(xup));
    return create_yProjection(xbinlow, xbinup);
}

Datafield* Datafield::create_xProjection(int ybinlow, int ybinup) const
{
    std::vector<double> out(xAxis().size());
    for (size_t i = 0; i < size(); ++i) {
        int ybin = static_cast<int>(frame().projectedIndex(i, 1));
        if (ybin >= ybinlow && ybin <= ybinup) {
            double x = frame().projectedCoord(i, 0);
            size_t iout = xAxis().closestIndex(x);
            out[iout] += valAt(i);
        }
    }
    return new Datafield(std::vector<const Scale*>{xAxis().clone()}, out);
}

Datafield* Datafield::create_yProjection(int xbinlow, int xbinup) const
{
    std::vector<double> out(yAxis().size());
    for (size_t i = 0; i < size(); ++i) {
        int xbin = static_cast<int>(frame().projectedIndex(i, 0));
        if (xbin >= xbinlow && xbin <= xbinup) {

            // TODO: duplicates code from create_xProjection, move to Frame ?

            double y = frame().projectedCoord(i, 1);
            size_t iout = yAxis().closestIndex(y);
            out[iout] += valAt(i);
        }
    }
    return new Datafield(std::vector<const Scale*>{yAxis().clone()}, out);
}

Datafield Datafield::plottableField() const
{
    return {title(), frame().plottableFrame(), m_values, m_err_sigmas};
}

Datafield Datafield::flat() const
{
    return {title(), frame().flat(), m_values, m_err_sigmas};
}

Datafield Datafield::noisy(double prefactor, double minimum) const
{
    auto const seed = 123;
    static auto urbg = std::mt19937(seed);

    std::vector<double> outval(size());
    std::vector<double> errval(size());
    for (size_t i = 0; i < size(); ++i) {
        double mean = valAt(i);
        double stdv = prefactor * sqrt(mean);
        auto norm = std::normal_distribution<double>(mean, stdv);
        outval[i] = std::max(norm(urbg), minimum);
        errval[i] = stdv;
    }
    return {title(), frame(), outval, errval};
}

Datafield Datafield::normalizedToMax() const
{
    const double maxval = *std::max_element(m_values.begin(), m_values.end());
    std::vector<double> outval(size());
    std::vector<double> errval(size());
    for (size_t i = 0; i < size(); ++i) {
        outval[i] = m_values[i] / maxval;
        if (hasErrorSigmas())
            errval[i] = m_err_sigmas[i] / maxval;
    }
    return {title(), frame(), outval, errval};
}

double2d_t Datafield::values2D() const
{
    return FieldUtil::reshapeTo2D(m_values, axis(1).size());
}