-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEVFile.cpp
329 lines (238 loc) · 10.1 KB
/
NEVFile.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "NEVFile.h"
#include "datapacket.h"
#include <iostream>
#include <cstring>
NEVFile::NEVFile(std::string filename, size_t buffersize) :
BUFFERSIZE(buffersize)
{
this->file.open(filename, std::ios_base::binary);
// this->file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
if(!this->file) {
std::cerr << "Failed to open file " << filename << "|\n" << std::endl;
throw(std::runtime_error("Cannot open file for reading"));
}
auto nHeaders = readBasicHeader();
readExtendedHeaders(nHeaders);
buffer = new uint8_t[BUFFERSIZE*packetSize];
file.read(reinterpret_cast<char*>(buffer), BUFFERSIZE*packetSize);
buffer_capacity = file.gcount();
buffer_pos = 0;
}
NEVFile::~NEVFile() {
this->file.close();
delete [] this->buffer;
}
std::uint32_t NEVFile::readBasicHeader() {
char* buffer = new char[200]; //largest field is 200 bytes (we'll reuse this)
/* Read the magic word to ensure this is the right kind of file*/
file.read(buffer, sizeof(char)*8);
if(!std::equal(buffer, buffer+8, "NEURALEV")) {
std::cerr << buffer << std::endl << "vs" << std::endl << "NEURALEV" << std::endl;
throw(std::runtime_error("Not a Ripple NEV (neural event) file"));
}
file.read(reinterpret_cast<char*>(&majorVersion), sizeof(majorVersion));
file.read(reinterpret_cast<char*>(&minorVersion), sizeof(minorVersion));
file.read(reinterpret_cast<char*>(&flags), sizeof(flags));
file.read(reinterpret_cast<char*>(&headerSize), sizeof(headerSize));
file.read(reinterpret_cast<char*>(&packetSize), sizeof(packetSize));
file.read(reinterpret_cast<char*>(&fsTimestamp), sizeof(fsTimestamp));
file.read(reinterpret_cast<char*>(&fsWaveforms), sizeof(fsWaveforms));
file.read(reinterpret_cast<char*>(&origin), sizeof(origin));
// The next two reads only work because each is bigger than the last (so no need to clear the buffer)
file.read(buffer, sizeof(char)*32);
creator = std::string(buffer);
file.read(buffer, sizeof(char)*200);
comment = std::string(buffer);
file.ignore(52); // Per spec, reserved for future use
file.read(reinterpret_cast<char*>(&processorTime), sizeof(processorTime));
std::uint32_t nHeaders;
file.read(reinterpret_cast<char*>(&nHeaders), sizeof(nHeaders));
delete [] buffer;
return nHeaders;
}
void NEVFile::readExtendedHeaders(const std::uint32_t nHeaders) {
const size_t EXTENDED_HEADER_SIZE = 32;
char buffer[EXTENDED_HEADER_SIZE];
for(auto i=0; i < nHeaders; i++) {
file.read(buffer, EXTENDED_HEADER_SIZE);
if(std::equal(buffer, buffer+7, "NEUEVWAV")) {
/* NEUEVWAV events contain metadata about the stimulating and recording amplifiers. This can be
distinguished by looking at the neural scale factor, which is zero for stimulation. Thus,
we extract everything into an NEUEVWAV object, and then move it into the "right" kind. */
NEUEVWAV tmpHeader;
std::copy(buffer+8, buffer+8+sizeof(tmpHeader), reinterpret_cast<char*>(&tmpHeader));
if(tmpHeader.electrodeID > 5120)
stimHeaders.emplace(tmpHeader.electrodeID - 5120, StimHeader(tmpHeader));
else
spikeHeaders.emplace(tmpHeader.electrodeID, SpikeHeader(tmpHeader));
}
else if(std::equal(buffer, buffer+7, "NEUEVFLT")) {
/* Spike filters are easy--just copy the data over
(depends on getting the alignment right!) */
SpikeFilter sf;
std::copy(buffer+8, buffer+8+sizeof(SpikeFilter), reinterpret_cast<char*>(&sf));
this->spikeFilters.emplace(sf.electrodeID, sf);
}
else if(std::equal(buffer, buffer+7, "NEUEVLBL")) {
/*Label markers are so trivial we can just unpack it right here*/
std::uint16_t electrodeID;
char label[16];
std::copy(buffer+8, buffer+10, reinterpret_cast<char*>(&electrodeID));
std::copy(buffer+9, buffer+9+16, label);
this->labels.emplace(electrodeID, std::string(label));
}
else if(std::equal(buffer, buffer+7, "DIGLABEL")) {
this->hasDigitalEvents = true;
this->digitalMode = static_cast<DigitalMode>(buffer[24]);
}
else {
std::cerr << "Unrecognized extended header detected" << std::endl;
}
}
return;
}
std::ostream& operator<<(std::ostream& out, const NEVFile& f) {
out << "NEV File (version " << int(f.majorVersion) << '.' << int(f.minorVersion) << ")\n";
out << "Created by " << f.creator << "\n";
out << "Data collection began at " << f.origin << std::endl;
out << "Comments: " << f.comment << std::endl;
out << "Packet Size: " << f.packetSize << std::endl;
return out;
}
bool NEVFile::eof() const {
return file.eof() && (this->buffer_pos == this->buffer_capacity);
}
void NEVFile::refillBuffer() {
if(!file.eof()) {
try {
file.read(reinterpret_cast<char*>(buffer), BUFFERSIZE*packetSize);
} catch (std::ifstream::failure &e) {
if(!file.eof())
throw(e);
}
buffer_capacity = file.gcount();
buffer_pos = 0;
}
return;
}
std::shared_ptr<Packet> NEVFile::readPacket(bool digital, bool stim, bool spike) {
/* Read the next packet of the specifed type(s) and returns a shared_ptr to it. */
std::shared_ptr<Packet> packet;
while(!packet && !this->eof()) {
packet = readPacketOrNull(digital, stim, spike);
}
return packet;
}
std::shared_ptr<Packet> NEVFile::readPacketOrNull(bool keep_digital, bool keep_stim, bool keep_spike) {
/* Read the next packet. If the corresponding type (digital, stim,
or spike) is true, parse it and return a shared_ptr. Otherwise,
return nullptr.
This avoids pointlessly allocating and then deallocating structures, particularly
SpikePackets. There are a ton of them, we're not particularly interested in them
and the alloc/dealloc consumes a massive amount of runtime (>30% in the destructors alone).
*/
if(this->eof())
throw(std::runtime_error("Read past end of time"));
if(this->buffer_capacity == this->buffer_pos) {
refillBuffer();
}
std::uint32_t timestamp;
std::uint16_t packetID;
auto start = buffer + buffer_pos + sizeof(timestamp);
std::copy(start, start+sizeof(packetID), reinterpret_cast<char*>(&packetID));
std::shared_ptr<Packet> p = nullptr;
if(packetID == 0 && keep_digital) {
p = parseCurrentAsDigital();
} else if(packetID <= 512 && keep_spike) {
p = parseCurrentAsSpike();
} else if (packetID > 512 && keep_stim) {
p = parseCurrentAsStim();
}
//Peek at the next packet to see if it is a continuation packet
this->buffer_pos+=this->packetSize;
while(true) {
if(this->buffer_pos == this->buffer_capacity) {
if(this->eof())
return p;
else
refillBuffer();
}
start = buffer + buffer_pos;
std::copy(start, start+sizeof(timestamp),
reinterpret_cast<char*>(×tamp));
if(timestamp != 0xFFFFFFU) //not a continuation packet
break;
if(!p) { // a continuation packet, but we're ignoring it (wrong type)
this->buffer_pos += this->packetSize;
continue;
} else {
auto wavep = std::dynamic_pointer_cast<WavePacket>(p);
auto newlen = wavep->len + this->packetSize - sizeof(timestamp);
char* newdata = new char[newlen];
std::copy(wavep->waveform, wavep->waveform + wavep->len, newdata);
std::copy(start, start + this->packetSize - sizeof(timestamp),
newdata + wavep->len);
delete [] wavep->waveform;
wavep->waveform = newdata;
wavep->len = newlen;
buffer_pos += this->packetSize;
}
}
return p;
}
std::shared_ptr<DigitalPacket> NEVFile::parseCurrentAsDigital() {
auto start = buffer + buffer_pos;
std::shared_ptr<DigitalPacket> p(new DigitalPacket);
std::copy(start, start+sizeof(p->timestamp),
reinterpret_cast<char*>(&(p->timestamp)));
start+=sizeof(p->timestamp) + 2; //+ 2 to skip the packetID
DigitalReason reason;
std::copy(start, start+sizeof(p->reason), reinterpret_cast<char*>(&reason));
p->reason = reason;
start+=sizeof(p->reason) + 1; //Skipping a byte reserved for future use
std::copy(start, start+sizeof(p->parallel), reinterpret_cast<char*>(&(p->parallel)));
start+=sizeof(p->parallel);
std::copy(start, start+sizeof(p->SMA1), reinterpret_cast<char*>(&(p->SMA1)));
start+=sizeof(p->SMA1);
std::copy(start, start+sizeof(p->SMA2), reinterpret_cast<char*>(&(p->SMA2)));
start+=sizeof(p->SMA2);
std::copy(start, start+sizeof(p->SMA3), reinterpret_cast<char*>(&(p->SMA3)));
start+=sizeof(p->SMA3);
std::copy(start, start+sizeof(p->SMA4), reinterpret_cast<char*>(&(p->SMA4)));
start+=sizeof(p->SMA4);
return p;
}
std::shared_ptr<SpikePacket> NEVFile::parseCurrentAsSpike() {
auto start = buffer + buffer_pos;
std::shared_ptr<SpikePacket> p(new SpikePacket);
std::copy(start, start+sizeof(p->timestamp), reinterpret_cast<char*>(&(p->timestamp)));
start+=sizeof(p->timestamp);
std::copy(start, start+sizeof(p->electrodeID), reinterpret_cast<char*>(&(p->electrodeID)));
start+=sizeof(p->electrodeID);
std::copy(start, start+sizeof(p->unit), reinterpret_cast<char*>(&(p->unit)));
start+=sizeof(p->unit) + 1; // Skip reserved byte
p->waveform = new char[this->packetSize - 8]; //Now managed by SpikePacket
p->len = this->packetSize - 8;
std::copy(start, start+this->packetSize-8, p->waveform);
return p;
}
std::shared_ptr<StimPacket> NEVFile::parseCurrentAsStim() {
auto start = buffer + buffer_pos;
std::shared_ptr<StimPacket> p(new StimPacket);
std::copy(start, start+sizeof(p->timestamp), reinterpret_cast<char*>(&(p->timestamp)));
start+=sizeof(p->timestamp);
std::copy(start, start+sizeof(p->electrodeID), reinterpret_cast<char*>(&(p->electrodeID)));
p->electrodeID -= 5120; //Remove offset
start+=sizeof(p->electrodeID) + 2;
p->waveform = new char[this->packetSize - 8]; //Now managed by StimPacket
p->len = this->packetSize - 8;
std::copy(start, start+this->packetSize-8, p->waveform);
return p;
}
std::ostream& operator<<(std::ostream& out, const DigitalMode& m) {
switch(m) {
case SERIAL_MODE: out << "Serial"; break;
case PARALLEL: out << "Parallel"; break;
}
return out;
}