/
biosignalmldatastoreexporter.cpp
199 lines (136 loc) · 7.15 KB
/
biosignalmldatastoreexporter.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
/*******************************************************************************
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 <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// BioSignalML data store exporter
//==============================================================================
#include "biosignalmldatastoredata.h"
#include "biosignalmldatastoreexporter.h"
//==============================================================================
#include <QFile>
#include <QUrl>
//==============================================================================
#include "libbiosignalmlbegin.h"
#include "biosignalml/data/hdf5.h"
#include "libbiosignalmlend.h"
//==============================================================================
namespace OpenCOR {
namespace BioSignalMLDataStore {
//==============================================================================
BiosignalmlDataStoreExporterWorker::BiosignalmlDataStoreExporterWorker(DataStore::DataStoreExportData *pDataStoreData) :
DataStore::DataStoreExporterWorker(pDataStoreData)
{
}
//==============================================================================
void BiosignalmlDataStoreExporterWorker::run()
{
// Determine the number of steps to export everything
auto dataStoreData = static_cast<BiosignalmlDataStoreData *>(mDataStoreData);
DataStore::DataStore *dataStore = dataStoreData->dataStore();
int nbOfRuns = dataStore->runsCount();
int nbOfSteps = 0;
for (int i = 0; i < nbOfRuns; ++i) {
nbOfSteps += dataStore->size(i);
}
double oneOverNbOfSteps = 1.0/nbOfSteps;
int stepNb = 0;
// Export our data store to a BioSignalML file
bsml::HDF5::Recording *recording = nullptr;
QString errorMessage = QString();
try {
// Create and populate a recording
// Note: although a VOI may not officially be visible to a user, we
// still need to export it since it's needed by BioSignalML...
DataStore::DataStoreVariable *voi = dataStore->voi();
std::string recordingUri = QUrl::fromLocalFile(dataStoreData->fileName()).toEncoded().toStdString();
std::string baseUnits = dataStore->uri().toStdString()+"/units#";
recording = new bsml::HDF5::Recording(recordingUri, dataStoreData->fileName().toStdString(), true);
recording->add_prefix(rdf::Namespace("units", baseUnits));
recording->set_label(dataStoreData->name().toStdString());
recording->set_investigator(rdf::Literal(dataStoreData->author().toStdString()));
recording->set_description(dataStoreData->description().toStdString());
recording->set_comment(dataStoreData->comment().toStdString());
for (int i = 0; i < nbOfRuns; ++i) {
// Create and populate a clock
std::string runNb = (nbOfRuns == 1)?"":"/"+QString::number(i+1).toStdString();
bsml::HDF5::Clock::Ptr clock = recording->new_clock(std::string().append(recordingUri).append("/clock/").append(voi->uri().toStdString()).append(runNb),
rdf::URI(baseUnits+voi->unit().toStdString()),
voi->values(i),
voi->size(i));
clock->set_label(voi->label().toStdString());
// Determine what should be exported (minus the VOI, which should
// always be exported in the case of a BioSignalML file)
DataStore::DataStoreVariables variables = mDataStoreData->variables();
variables.removeOne(dataStore->voi());
// Retrieve some information about the different variables that are
// to be exported
std::vector<std::string> uris;
std::vector<rdf::URI> units;
for (auto variable : variables) {
uris.emplace_back(std::string().append(recordingUri).append("/signal/").append(variable->uri().toStdString()).append(runNb));
units.emplace_back(rdf::URI(baseUnits+variable->unit().toStdString()));
}
// Create and populate a signal array
enum {
BufferRows = 50000
};
bsml::HDF5::SignalArray::Ptr signalArray = recording->new_signalarray(uris, units, clock);
bsml::HDF5::SignalArray::size_type n = 0;
for (auto variable : variables) {
(*signalArray)[n]->set_label(variable->label().toStdString());
++n;
}
auto data = new double[quint64(variables.count()*BufferRows)] {};
double *dataPointer = data;
size_t rowCount = 0;
for (quint64 j = 0, jMax = dataStore->size(i); j < jMax; ++j) {
for (auto variable : variables) {
*dataPointer++ = variable->value(j, i);
}
++rowCount;
if (rowCount >= BufferRows) {
signalArray->extend(data, size_t(variables.count())*BufferRows);
dataPointer = data;
rowCount = 0;
}
emit progress(mDataStoreData, ++stepNb*oneOverNbOfSteps);
}
signalArray->extend(data, size_t(variables.count())*rowCount);
delete[] data;
}
} catch (bsml::data::Exception &exception) {
// Something went wrong, so retrieve the error message and delete our
// BioSignalML file
errorMessage = tr("The data could not be exported to BioSignalML (%1).").arg(exception.what());
QFile::remove(dataStoreData->fileName());
}
// Close and delete our recording, if any
if (recording != nullptr) {
recording->close();
delete recording;
}
// Let people know that our export is done
emit done(mDataStoreData, errorMessage);
}
//==============================================================================
DataStore::DataStoreExporterWorker * BiosignalmlDataStoreExporter::workerInstance(DataStore::DataStoreExportData *pDataStoreData)
{
// Return an instance of our worker
return new BiosignalmlDataStoreExporterWorker(pDataStoreData);
}
//==============================================================================
} // namespace BioSignalMLDataStore
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================