-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKistlerFile.cpp
More file actions
261 lines (219 loc) · 7.73 KB
/
Copy pathKistlerFile.cpp
File metadata and controls
261 lines (219 loc) · 7.73 KB
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
// Copyright 2024
// Author: Paul Soelder <p.soelder@mailbox.org>
#include "./KistlerFile.h"
// ____________________________________________________________________________
KistlerCSVFile::KistlerCSVFile(const std::string &fileName)
: KistlerFile(fileName) {
// Sanity checks on the provided file.
validateFile();
// Read column headers so we know which data we have available.
if (isValid_)
parseMetaData();
// Now we're ready for getting data
}
// ____________________________________________________________________________
void KistlerCSVFile::validateFile() {
std::ifstream file(fileName_);
isValid_ = true;
// (1) Check if file exists.
if (!file) {
isValid_ = false;
std::cerr << "Error in KistlerCSVFile::validateFile(): No such file or "
"directory: "
<< fileName_ << std::endl;
return;
}
// (1) Check if file is non-empty.
// Thanks https://stackoverflow.com/a/2390938
if (file.peek() == std::ifstream::traits_type::eof()) {
isValid_ = false;
std::cerr << "Error in KistlerCSVFile::validateFile(): File is empty: "
<< fileName_ << std::endl;
return;
}
// (2) Check if there is "BioWare" in the first line.
std::string line;
std::getline(file, line);
// Thanks https://stackoverflow.com/a/2340309
if (line.find("BioWare") == std::string::npos) {
isValid_ = false;
std::cerr << "Error in KistlerCSVFile::validateFile(): File does not "
"appear to be a valid BioWare file: "
<< fileName_ << std::endl;
return;
}
// (3) Check if there are sensible column headers in line 18 (the variable
// names, like "Fx").
for (int i = 0; i < 17; i++) {
std::getline(file, line);
}
// For now this is hard-coded, but this might change (e.g. depending on
// BioWare settings).
if (line.find("abs time") == std::string::npos) {
isValid_ = false;
std::cerr << "Error in KistlerCSVFile::validateFile(): File does not "
"appear to be a valid BioWare file: "
<< fileName_ << std::endl;
return;
}
}
// ____________________________________________________________________________
void KistlerCSVFile::parseMetaData() {
// Get the sample rate.
// Get the column headers.
std::ifstream file(fileName_);
if (!isValid_) {
std::cerr << "Error in KistlerCSVFile::parseMetaData(): File does not "
"appear to be a valid BioWare file: "
<< fileName_ << std::endl;
return;
}
std::string line;
std::getline(file, line);
// Sampling rates are in line 4.
for (int i = 0; i < 3; i++) {
std::getline(file, line);
}
std::vector<std::string> samplingRates_ =
sliceRow(line, '\t'); // hard-coded delimiter ...
if (!(samplingRates_[0] == "Rate (Hz):")) {
std::cerr << "Error in KistlerCSVFile::parseMetaData(): Could not "
"determine the sampling rate of the file, it does not appear "
"to be a valid BioWare file: "
<< fileName_ << std::endl;
isValid_ = false;
return;
} else {
// Assuming that the sampling rate is the same for each channel.
if (samplingRates_.size() > 1) {
float samplingRate;
try {
samplingRate = std::stof(samplingRates_[1]);
} catch (std::exception &e) {
// failed to parse sampling rate -> invalid file
isValid_ = false;
return;
}
if (samplingRate <= 0) {
std::cerr << "Error in KistlerCSVFile::parseMetaData(): Determined an "
"invalid sampling rate of the file, it does not appear to "
"be a valid BioWare file: "
<< fileName_ << std::endl;
isValid_ = false;
return;
}
samplingRate_ = samplingRate;
qDebug() << "Detected sampling rate of" << samplingRate_ << "Hz.";
} else {
std::cerr
<< "Error in KistlerCSVFile::parseMetaData(): Could not "
"determine the sampling rate of the file, it does not appear "
"to be a valid BioWare file: "
<< fileName_ << std::endl;
isValid_ = false;
return;
}
}
// Column headers are in line 18 (4 already read for the sampling rate).
for (int i = 0; i < 14; i++) {
std::getline(file, line);
}
columnNames_ = sliceRow(line, '\t'); // hard-coded delimiter ...
numCols_ = columnNames_.size();
}
// ____________________________________________________________________________
std::vector<std::string> KistlerCSVFile::sliceRow(std::string line,
const char delimiter) {
// Remove newline and carriage return.
line.erase(std::remove(line.begin(), line.end(), '\n'), line.cend());
line.erase(std::remove(line.begin(), line.end(), '\r'), line.cend());
// Thanks https://stackoverflow.com/a/55263720
std::string tmp;
std::stringstream ss(line);
std::vector<std::string> elements;
while (getline(ss, tmp, delimiter)) {
elements.push_back(tmp);
}
// If the line ends with a delimiter it should append an empty element.
if (!line.empty() && line.back() == delimiter) {
elements.push_back("");
}
return elements;
}
// ____________________________________________________________________________
const std::shared_ptr<std::unordered_map<std::string, std::vector<float>>>
KistlerCSVFile::getData(int startRow, int stopRow) const {
// Check for invalid row indices.
if (stopRow < startRow && stopRow != -1) {
std::cerr << "Error in KistlerCSVFile::getData(): Invalid row indices "
"startRow and/or stopRow. This can happen e.g. if startRow > "
"stopRow."
<< std::endl;
exit(EXIT_FAILURE); // replace with exception handling
}
auto data =
std::make_shared<std::unordered_map<std::string, std::vector<float>>>();
for (const auto &column : columnNames_) {
(*data)[column] = std::vector<float>();
}
std::ifstream file(fileName_);
std::string line;
std::getline(file, line);
// Data starts at line 20.
for (int i = 0; i < 18; i++) {
std::getline(file, line);
}
// Take care of startRow.
if (startRow != -1) {
for (int i = 0; i < startRow; i++) {
std::getline(file, line);
}
}
// Note: This is a big performance limitation, as the number of I/O operations
// increase linearly with application runtime. We will have to think about
// buffering or using byte-wise access with istream::seekg().
// Take care of stopRow.
int nRows;
if (stopRow != -1 && startRow != -1) {
nRows = stopRow - startRow + 1;
} else if (stopRow != -1 && startRow == -1) {
nRows = stopRow + 1;
} else {
nRows = -1; // indicator to read until EOF in the loop below.
}
// We can reserve some memory in advance to avoid multiple allocations.
if (nRows != -1) {
for (auto &column : *data) {
column.second.reserve(nRows);
}
}
// Read nRows lines.
bool done = false;
int i = 0;
do {
// Continue if we have not reached nRows yet OR we should read until EOF
// (nRows == -1).
if ((nRows != -1 && i < nRows) || nRows == -1) {
if (!std::getline(file, line)) {
qDebug() << "KistlerCSVFile::getData(int, int): reached EOF";
// reached EOF.
done = true;
break;
}
auto row = sliceRow(line, '\t');
for (size_t j = 0; j < columnNames_.size(); j++) {
try {
float value = std::stof(row[j]);
data->at(columnNames_[j]).push_back(value);
} catch (std::exception &e) {
throw CorruptKistlerFileException(
"Error in KistlerCSVFile::getData(): Cannot convert string to "
"float. Seems like the data is corrupt.");
}
}
i++;
} else
done = true;
} while (!done);
return data;
}