-
Notifications
You must be signed in to change notification settings - Fork 26
/
csvdatastoreexporter.cpp
244 lines (173 loc) · 8.44 KB
/
csvdatastoreexporter.cpp
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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// CSV data store exporter
//==============================================================================
#include "corecliutils.h"
#include "csvdatastoreexporter.h"
//==============================================================================
#include <QDir>
#include <QSet>
//==============================================================================
namespace OpenCOR {
namespace CSVDataStore {
//==============================================================================
CsvDataStoreExporter::CsvDataStoreExporter(DataStore::DataStoreData *pDataStoreData) :
DataStore::DataStoreExporter(pDataStoreData)
{
}
//==============================================================================
void CsvDataStoreExporter::execute(QString &pErrorMessage) const
{
// Do the export itself
// Note: we would normally rely on a string to which we would append our
// header and then data, and then use that string as a parameter to
// Core::writeFileContentsToFile(). However, although this works fine
// with 'small' amounts of data to export, this can crash OpenCOR if
// we really have a lot of data to write. So, instead, we do what
// Core::writeFileContentsToFile() does, but rather than writing one
// potentially humongous string, we first write our header and then
// our data, one row at a time...
QFile file(Core::temporaryFileName());
if (file.open(QIODevice::WriteOnly)) {
// Determine whether we need to export the VOI and, if so, remove it
// from our variables since it gets exported separately
DataStore::DataStore *dataStore = mDataStoreData->dataStore();
DataStore::DataStoreVariables variables = mDataStoreData->variables();
DataStore::DataStoreVariable *voi = variables.contains(dataStore->voi())?dataStore->voi():0;
variables.removeOne(voi);
// Retrieve the different values for our VOI and sets of variables
// Note #1: this is needed when we have two runs with different
// starting/ending points and/or point intervals...
// Note #2: after our for loop, our list may contain duplicates and may
// not be sorted. When it comes to removing duplicates, we do
// this by converting our list to a set and back. Indeed, this
// is much faster than preventing ourselves from adding
// duplicates in the first place...
QStringList variablesUri = QStringList();
QList<DataStore::DataStoreVariables> variablesRuns = QList<DataStore::DataStoreVariables>();
foreach (DataStore::DataStoreVariable *variable, variables) {
variablesUri << variable->uri();
variablesRuns << DataStore::DataStoreVariables();
}
int nbOfRuns = dataStore->runsCount();
QList<quint64> runsIndex = QList<quint64>();
QDoubleList voiValues = QDoubleList();
for (int i = 0; i < nbOfRuns; ++i) {
// Original index for the current run
runsIndex << 0;
// VOI values
for (quint64 j = 0, jMax = dataStore->size(i); j < jMax; ++j)
voiValues << dataStore->voi()->value(j, i);
// Variables
int j = 0;
foreach (DataStore::DataStoreVariable *variable, dataStore->variables()) {
if (variablesUri.contains(variable->uri())) {
variablesRuns[j] << variable;
++j;
}
}
}
voiValues = voiValues.toSet().toList();
std::sort(voiValues.begin(), voiValues.end());
// Determine the number of steps to export everything, i.e. one for our
// header and then some for our number of VOI values
double oneOverNbOfSteps = 1.0/(1+voiValues.count());
int stepNb = 0;
// Output our header
static const QString Header = "%1 (%2)%3";
static const QString RunNb = " | Run #%1";
QString header = QString();
if (voi) {
header += Header.arg(voi->uri().replace("/prime", "'").replace('/', " | "))
.arg(voi->unit())
.arg(QString());
}
foreach (DataStore::DataStoreVariable *variable, variables) {
for (int i = 0; i < nbOfRuns; ++i) {
if (!header.isEmpty())
header += ',';
header += Header.arg(variable->uri().replace("/prime", "'").replace('/', " | "))
.arg(variable->unit())
.arg((nbOfRuns == 1)?
QString():
RunNb.arg(i+1));
}
}
header += '\n';
bool res = file.write(header.toUtf8()) != -1;
// Output our different sets of data, one row at a time, if we were able
// to output our header
if (res) {
emit progress(++stepNb*oneOverNbOfSteps);
for (quint64 i = 0, iMax = voiValues.count(); i < iMax; ++i) {
QString rowData = QString();
double voiValue = voiValues[i];
if (voi)
rowData += QString::number(voiValue);
bool firstRowData = true;
QBoolList updateRunsIndex = QBoolList();
for (int j = 0; j < nbOfRuns; ++j)
updateRunsIndex << false;
foreach (const DataStore::DataStoreVariables &variableRuns, variablesRuns) {
int j = 0;
foreach (DataStore::DataStoreVariable *variableRun, variableRuns) {
if (firstRowData && rowData.isEmpty())
firstRowData = false;
else
rowData += ',';
if (dataStore->voi()->value(runsIndex[j], j) == voiValue) {
rowData += QString::number(variableRun->value(runsIndex[j], j));
updateRunsIndex[j] = true;
}
++j;
}
}
for (int j = 0; j < nbOfRuns; ++j) {
if (updateRunsIndex[j])
++runsIndex[j];
}
rowData += "\n";
res = file.write(rowData.toUtf8()) != -1;
if (!res)
break;
emit progress(++stepNb*oneOverNbOfSteps);
}
}
// Close our temporary file and rename it to our final file, if we were
// able to output all of our data
file.close();
if (res) {
QDir dir(QFileInfo(mDataStoreData->fileName()).path());
res = dir.exists() || dir.mkpath(dir.dirName());
if (res) {
if (QFile::exists(mDataStoreData->fileName()))
QFile::remove(mDataStoreData->fileName());
res = file.rename(mDataStoreData->fileName());
}
}
if (!res) {
file.remove();
pErrorMessage = tr("The data could not be exported to CSV.");
}
} else {
pErrorMessage = tr("The CSV file could not be created.");
}
}
//==============================================================================
} // namespace CSVDataStore
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================