File: Dataset.cpp

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (278 lines) | stat: -rw-r--r-- 8,625 bytes parent folder | download
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#include <dataset/Dataset.h>
#include <math/MovingAverager.h>
#include <math/CubicSpline.h>
#include <math/PeakFinder.h>
#include <utility/Exceptions.h>
#include <utility/StringUtils.h>
#include <utility/Console.h>
#include <dataset/DatasetFactory.h>
#include <settings/GeneralSettings.h>

#include <vector>
#include <string>
#include <fstream>
#include <sstream>

using namespace ausaxs;

Dataset::Dataset() = default;
Dataset::Dataset(const Dataset& d) = default;
Dataset::Dataset(Dataset&& d) = default;
Dataset& Dataset::operator=(const Dataset& other) = default;
Dataset& Dataset::operator=(Dataset&& other) = default;

Dataset::Dataset(const io::ExistingFile& path) : Dataset() {
    *this = std::move(*factory::DatasetFactory::construct(path));
}

Dataset::~Dataset() = default;

void Dataset::assign_matrix(Matrix<double>&& m) {
    if (m.M != data.M) {
        throw except::invalid_operation("Dataset::operator=: Matrix has wrong number of columns. "
        "Expected " + std::to_string(data.M) + ", but got " + std::to_string(m.M));
    }
    force_assign_matrix(std::move(m));
}

void Dataset::force_assign_matrix(Matrix<double>&& m) {
    data.data = std::move(m.data);
    data.N = m.N;
    data.M = m.M;
}

bool Dataset::empty() const noexcept {
    return data.data.empty();
}

void Dataset::limit_x(const Limit& limits) {
    if (size() == 0) {return;}
    if (limits.min < x(0) && x(size()-1) < limits.max) {return;}

    Matrix<double> limited(0, data.M); 
    for (unsigned int i = 0; i < size(); i++) {
        double val = x(i);
        if (val < limits.min) {continue;}
        else if (limits.max < val) {break;}
        limited.push_back(row(i));
    }
    assign_matrix(std::move(limited));
}

void Dataset::limit_y(const Limit& limits) {
    if (size() == 0) {return;}

    Matrix<double> limited(0, data.M);
    for (unsigned int i = 0; i < size(); i++) {
        double val = y(i);
        if (val < limits.min || limits.max < val) {continue;}
        limited.push_back(row(i));
    }
    assign_matrix(std::move(limited));
}

void Dataset::limit_x(double min, double max) {limit_x({min, max});}
void Dataset::limit_y(double min, double max) {limit_y({min, max});}

MutableColumn<double> Dataset::col(unsigned int index) {
    return data.col(index);
}

const ConstColumn<double> Dataset::col(unsigned int index) const {
    return data.col(index);
}

MutableRow<double> Dataset::row(unsigned int index) {
    return data.row(index);
}

const ConstRow<double> Dataset::row(unsigned int index) const {
    return data.row(index);
}

Dataset Dataset::select_columns(const std::vector<unsigned int>& cols) const {
    if (cols.size() == 0) {throw except::invalid_argument("Dataset::select_columns: No columns selected.");}
    Dataset data(this->data.N, cols.size());
    for (unsigned int i = 0; i < cols.size(); i++) {
        data.col(i) = col(cols[i]);
    }
    return data;
}

void Dataset::save(const io::File& path, const std::string& header) const {
    path.directory().create();

    // check if file was succesfully opened
    std::ofstream output(path);
    if (!output.is_open()) {throw std::ios_base::failure("IntensityFitter::save: Could not open file \"" + path.str() + "\"");}

    // write header
    if (!header.empty()) {
        output << header << std::endl;
    }
    output << std::endl;

    // write data
    for (unsigned int i = 0; i < data.N; i++) {
        for (unsigned int j = 0; j < data.M-1; j++) {
            output << std::left << std::setw(16) << std::setprecision(8) << std::scientific << index(i, j) << "\t";
        }
        output << index(i, data.M-1) << "\n";
    }
    output.close();
}

void Dataset::load(const io::ExistingFile& path) {
    auto dataset = factory::DatasetFactory::construct(path, data.M);
    if (dataset->data.M != data.M) {
        throw except::invalid_operation(
            "Dataset::load: Number of columns does not match. "
            "(" + std::to_string(dataset->data.M) + " != " + std::to_string(data.M) + ")"
        );
    }
    *this = std::move(*dataset);
}

Dataset Dataset::rolling_average(unsigned int window_size) const {
    Dataset result(*this);
    result.y() = MovingAverage::average_half(y(), window_size);
    return result;
}

Dataset Dataset::interpolate(unsigned int n) const {
    Matrix<double> interpolated(size()*(n+1)-n-1, data.M);

    std::vector<math::CubicSpline> splines;
    for (unsigned int col_index = 1; col_index < data.M; ++col_index) {
        splines.push_back(math::CubicSpline(x(), col(col_index)));
    }

    for (unsigned int i = 0; i < size()-1; i++) {
        double x = this->x(i);
        interpolated[i*(n+1)] = row(i);

        double x_next = this->x(i+1);
        double step = (x_next - x)/(n+1);
        for (unsigned int j = 0; j < n; j++) {
            std::vector<double> row_new(data.M);
            row_new[0] = x + (j+1)*step;;
            for (unsigned int k = 1; k < data.M; k++) {
                row_new[k] = splines[k-1].spline(row_new[0]);
            }
            interpolated[i*(n+1) + j + 1] = row_new;
        }
    }
    return interpolated;
}

std::vector<double> Dataset::find_minimum(unsigned int col_i) const {
    if (size() == 0) {
        if (settings::general::verbose) {
            console::print_warning("Warning in Dataset::find_minimum: Dataset is empty.");
        }
        return std::vector<double>(data.M, 0);
    }
    
    unsigned int min_index = 0;
    double min_value = y(0);
    for (unsigned int i = 1; i < size(); i++) {
        if (col(col_i)[i] < min_value) {
            min_index = i;
            min_value = col(col_i)[i];
        }
    }
    return row(min_index);
}

Dataset Dataset::interpolate(const std::vector<double>& newx) const {
    Matrix<double> interpolated(newx.size(), data.M);

    std::vector<math::CubicSpline> splines;
    for (unsigned int col_index = 1; col_index < data.M; ++col_index) {
        splines.push_back(math::CubicSpline(x(), col(col_index)));
    }

    for (unsigned int i = 0; i < newx.size(); i++) {
        std::vector<double> row_new(data.M);
        row_new[0] = newx[i];
        for (unsigned int j = 0; j < splines.size(); j++) {
            row_new[1+j] = splines[j].spline(newx[i]);
        }
        interpolated[i] = row_new;
    }
    return interpolated;
}

double Dataset::interpolate_x(double x, unsigned int col_index) const {
    math::CubicSpline spline(this->x(), col(col_index));
    return spline.spline(x);
}

void Dataset::append(const Dataset& other) {
    if (this == &other) {throw except::invalid_argument("Dataset::append: Cannot append to itself.");}
    if (data.M != other.data.M) {throw except::invalid_argument("Dataset::append: Number of columns does not match.");}
    unsigned int n = size();
    data.extend(other.size());
    for (unsigned int i = 0; i < other.size(); i++) {
        row(n+i) = other.row(i);
    }
}

void Dataset::sort_x() {
    Matrix<double> newdata(data.N, data.M);
    std::vector<unsigned int> indices(data.N);
    std::iota(indices.begin(), indices.end(), 0);
    std::sort(indices.begin(), indices.end(), [this] (unsigned int i, unsigned int j) {return x(i) < x(j);});
    for (unsigned int i = 0; i < data.N; i++) {
        newdata.row(i) = this->row(indices[i]);
    }
    this->assign_matrix(std::move(newdata));
}

std::string Dataset::to_string() const {
    std::stringstream ss;
    for (unsigned int i = 0; i < size(); i++) {
        for (unsigned int j = 0; j < data.M; j++) {
            ss << std::setw(16) << std::setprecision(8) << std::scientific << index(i, j) << " ";
        }
        ss << "\n";
    }
    return ss.str();
}

double Dataset::index(unsigned int i, unsigned int j) const {
    return data.index(i, j);
}

double& Dataset::index(unsigned int i, unsigned int j) {
    return data.index(i, j);
}

void Dataset::push_back(const std::vector<double>& row) {
    data.push_back(row);
}

std::vector<unsigned int> Dataset::find_minima(unsigned int min_spacing, double min_prominence) const {
    return math::find_minima(x(), y(), min_spacing, min_prominence);
}

std::vector<unsigned int> Dataset::find_maxima(unsigned int min_spacing, double min_prominence) const {
    return math::find_minima(x(), -y(), min_spacing, min_prominence);
}

unsigned int Dataset::size() const noexcept {
    return size_rows();
}

unsigned int Dataset::size_rows() const noexcept {
    return data.N;
}

unsigned int Dataset::size_cols() const noexcept {
    return data.M;
}

bool Dataset::operator==(const Dataset& other) const = default;