Loading...
Searching...
No Matches
datatable.C
Go to the documentation of this file.
1/*
2 * This file is part of the SPLINTER library.
3 * Copyright (C) 2012 Bjarne Grimstad (bjarne.grimstad@gmail.com).
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10#include "datatable.h"
11#include <string>
12#include <fstream>
13#include <iomanip>
14#include <stdexcept>
15#include <limits>
16#include <serializer.h>
17#include <initializer_list>
18
19namespace SPLINTER
20{
21
22DataTable::DataTable()
23 : DataTable(false, false)
24{
25}
26
27DataTable::DataTable(bool allowDuplicates)
28 : DataTable(allowDuplicates, false)
29{
30}
31
32DataTable::DataTable(bool allowDuplicates, bool allowIncompleteGrid)
33 : allowDuplicates(allowDuplicates),
34 allowIncompleteGrid(allowIncompleteGrid),
35 numDuplicates(0),
36 numVariables(0)
37{
38}
39
40DataTable::DataTable(const char* fileName)
41 : DataTable(std::string(fileName))
42{
43}
44
45DataTable::DataTable(const std::string& fileName)
46{
47 load(fileName);
48}
49
50void DataTable::addSample(double x, double y)
51{
52 addSample(DataPoint(x, y));
53}
54
55void DataTable::addSample(std::vector<double> x, double y)
56{
57 addSample(DataPoint(x, y));
58}
59
60void DataTable::addSample(DenseVector x, double y)
61{
62 addSample(DataPoint(x, y));
63}
64
65void DataTable::addSample(const DataPoint& sample)
66{
67 if (getNumSamples() == 0)
68 {
69 numVariables = sample.getDimX();
70 initDataStructures();
71 }
72
73 if (sample.getDimX() != numVariables)
74 {
75 throw Exception("Datatable::addSample: Dimension of new sample is inconsistent with previous samples!");
76 }
77
78 // Check if the sample has been added already
79 if (samples.count(sample) > 0)
80 {
81 if (!allowDuplicates)
82 {
83#ifndef NDEBUG
84 std::cout << "Discarding duplicate sample because allowDuplicates is false!" <<
85 std::endl;
86 std::cout << "Initialise with DataTable(true) to set it to true." << std::endl;
87#endif // NDEBUG
88 return;
89 }
90
91 numDuplicates++;
92 }
93
94 samples.insert(sample);
95 recordGridPoint(sample);
96}
97
98void DataTable::addSample(std::initializer_list<DataPoint> samples)
99{
100 for (auto& sample : samples)
101 {
102 addSample(sample);
103 }
104}
105
106void DataTable::recordGridPoint(const DataPoint& sample)
107{
108 for (unsigned int i = 0; i < getNumVariables(); i++)
109 {
110 grid.at(i).insert(sample.getX().at(i));
111 }
112}
113
114unsigned int DataTable::getNumSamplesRequired() const
115{
116 unsigned long samplesRequired = 1;
117 unsigned int i = 0;
118
119 for (auto& variable : grid)
120 {
121 samplesRequired *= (unsigned long) variable.size();
122 i++;
123 }
124
125 return (i > 0 ? samplesRequired : (unsigned long) 0);
126}
127
128bool DataTable::isGridComplete() const
129{
130 return samples.size() > 0
131 && samples.size() - numDuplicates == getNumSamplesRequired();
132}
133
134void DataTable::initDataStructures()
135{
136 for (unsigned int i = 0; i < getNumVariables(); i++)
137 {
138 grid.push_back(std::set<double>());
139 }
140}
141
142void DataTable::gridCompleteGuard() const
143{
144 if (!(isGridComplete() || allowIncompleteGrid))
145 {
146 throw Exception("DataTable::gridCompleteGuard: The grid is not complete yet!");
147 }
148}
149
150void DataTable::save(const std::string& fileName) const
151{
152 Serializer s;
153 s.serialize(*this);
154 s.saveToFile(fileName);
155}
156
157void DataTable::load(const std::string& fileName)
158{
159 Serializer s(fileName);
160 s.deserialize(*this);
161}
162
163/*
164 * Getters for iterators
165 */
166std::multiset<DataPoint>::const_iterator DataTable::cbegin() const
167{
168 return samples.cbegin();
169}
170
171std::multiset<DataPoint>::const_iterator DataTable::cend() const
172{
173 return samples.cend();
174}
175
176/*
177 * Get table of samples x-values,
178 * i.e. table[i][j] is the value of variable i at sample j
179 */
180std::vector< std::vector<double>> DataTable::getTableX() const
181{
182 gridCompleteGuard();
183 // Initialize table
184 std::vector<std::vector<double>> table;
185
186 for (unsigned int i = 0; i < numVariables; i++)
187 {
188 std::vector<double> xi(getNumSamples(), 0.0);
189 table.push_back(xi);
190 }
191
192 // Fill table with values
193 int i = 0;
194
195 for (auto& sample : samples)
196 {
197 std::vector<double> x = sample.getX();
198
199 for (unsigned int j = 0; j < numVariables; j++)
200 {
201 table.at(j).at(i) = x.at(j);
202 }
203
204 i++;
205 }
206
207 return table;
208}
209
210// Get vector of y-values
211std::vector<double> DataTable::getVectorY() const
212{
213 std::vector<double> y;
214
215 for (std::multiset<DataPoint>::const_iterator it = cbegin(); it != cend(); ++it)
216 {
217 y.push_back(it->getY());
218 }
219
220 return y;
221}
222
223DataTable operator+(const DataTable& lhs, const DataTable& rhs)
224{
225 if (lhs.getNumVariables() != rhs.getNumVariables())
226 {
227 throw Exception("operator+(DataTable, DataTable): trying to add two DataTable's of different dimensions!");
228 }
229
230 DataTable result;
231
232 for (auto it = lhs.cbegin(); it != lhs.cend(); it++)
233 {
234 result.addSample(*it);
235 }
236
237 for (auto it = rhs.cbegin(); it != rhs.cend(); it++)
238 {
239 result.addSample(*it);
240 }
241
242 return result;
243}
244
245DataTable operator-(const DataTable& lhs, const DataTable& rhs)
246{
247 if (lhs.getNumVariables() != rhs.getNumVariables())
248 {
249 throw Exception("operator-(DataTable, DataTable): trying to subtract two DataTable's of different dimensions!");
250 }
251
252 DataTable result;
253 auto rhsSamples = rhs.getSamples();
254
255 // Add all samples from lhs that are not in rhs
256 for (auto it = lhs.cbegin(); it != lhs.cend(); it++)
257 {
258 if (rhsSamples.count(*it) == 0)
259 {
260 result.addSample(*it);
261 }
262 }
263
264 return result;
265}
266
267} // namespace SPLINTER
DataTable operator-(const DataTable &lhs, const DataTable &rhs)
Definition datatable.C:245
DataTable operator+(const DataTable &lhs, const DataTable &rhs)
Definition datatable.C:223
Eigen::Matrix< T, -1, dim > load(Eigen::Matrix< T, -1, dim > &mat, const std::string fname, std::string order="rowMajor")
label i
Definition pEqn.H:46