-
Notifications
You must be signed in to change notification settings - Fork 4
/
ax3.cpp
544 lines (470 loc) · 20.2 KB
/
ax3.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <ctime>
#include <cmath>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <map>
#include <numeric>
using namespace std;
struct cwa_timestamp {
uint16_t year;
uint16_t month;
uint16_t day;
uint16_t hours;
uint16_t mins;
uint16_t seconds;
};
struct OM_READER_HEADER_PACKET {
uint16_t packetHeader; // @ 0 +2 ASCII "MD", little-endian (0x444D)
uint16_t packetLength; // @ 2 +2 Packet length (1020 bytes, with header (4) = 1024 bytes total)
uint8_t reserved1; // @ 4 +1 (1 byte reserved)
uint16_t deviceId; // @ 5 +2 Device identifier
uint32_t sessionId; // @ 7 +4 Unique session identifier
uint16_t reserved2; // @11 +2 (2 bytes reserved)
uint32_t loggingStartTime; // @13 +4 Start time for delayed logging
uint32_t loggingEndTime; // @17 +4 Stop time for delayed logging
uint32_t loggingCapacity; // @21 +4 Preset maximum number of samples to collect, 0 = unlimited
uint8_t reserved3[11]; // @25 +11 (11 bytes reserved)
uint8_t samplingRate; // @36 +1 Sampling rate
uint32_t lastChangeTime; // @37 +4 Last change metadata time
uint8_t firmwareRevision; // @41 +1 Firmware revision number
int16_t timeZone; // @42 +2 Time Zone offset from UTC (in minutes), 0xffff = -1 = unknown
uint8_t reserved4[20]; // @44 +20 (20 bytes reserved)
uint8_t annotation[448]; // @64 +448 Scratch buffer / meta-data (448 characters, space padded, generally URL-encoded key/value pairs)
uint8_t reserved[512]; // @512 +512 Reserved for post-collection scratch buffer / meta-data (512 characters)
};
struct OM_READER_DATA_PACKET {
uint16_t packetHeader; // @ 0 +2 ASCII "AX", little-endian (0x5841)
uint16_t packetLength; // @ 2 +2 Packet length (508 bytes, with header (4) = 512 bytes total)
uint16_t deviceFractional; // @ 4 +2 Top bit set: 15-bit fraction of a second for the time stamp, the timestampOffset was already adjusted to minimize this assuming ideal sample rate; Top bit clear: 15-bit device identifier, 0 = unknown;
uint32_t sessionId; // @ 6 +4 Unique session identifier, 0 = unknown
uint32_t sequenceId; // @10 +4 Sequence counter, each packet has a new number (reset if restarted)
uint32_t timestamp; // @14 +4 Last reported RTC value, 0 = unknown
uint16_t light; // @18 +2 Last recorded light sensor value in raw units, 0 = none
uint16_t temperature; // @20 +2 Last recorded temperature sensor value in raw units, 0 = none
uint8_t events; // @22 +1 Event flags since last packet, b0 = resume logging, b1 = single-tap event, b2 = double-tap event, b3-b7 = reserved for diagnostic use)
uint8_t battery; // @23 +1 Last recorded battery level in raw units, 0 = unknown
uint8_t sampleRate; // @24 +1 Sample rate code, (3200/(1<<(15-(rate & 0x0f)))) Hz
uint8_t numAxesBPS; // @25 +1 0x32 (top nibble: number of axes = 3; bottom nibble: packing format - 2 = 3x 16-bit signed, 0 = 3x 10-bit signed + 2-bit exponent)
int16_t timestampOffset; // @26 +2 Relative sample index from the start of the buffer where the whole-second timestamp is valid
uint16_t sampleCount; // @28 +2 Number of accelerometer samples (80 or 120)
uint8_t rawSampleData[480]; // @30 +480 Raw sample data. Each sample is either 3x 16-bit signed values (x, y, z) or one 32-bit packed value (The bits in bytes [3][2][1][0]: eezzzzzz zzzzyyyy yyyyyyxx xxxxxxxx, e = binary exponent, lsb on right)
uint16_t checksum; // @510 +2 Checksum of packet (16-bit word-wise sum of the whole packet, should be zero)
};
template <typename T>
float percentile(vector<T> vec, float p) {
vector<T> copy = vec;
sort(copy.begin(), copy.end());
size_t n = copy.size();
size_t index = (size_t) float(n)*p;
return copy[index];
}
template <typename T>
float mean(vector<T> vec) {
float sum = accumulate(begin(vec), end(vec), 0.0);
float mean = sum / vec.size();
return mean;
}
template <typename T>
float stdDev(vector<T> vec) {
float sum = accumulate(begin(vec), end(vec), 0.0);
float mean = sum / vec.size();
float accum = 0.0;
for (int i = 0; i < vec.size(); ++i) {
accum += pow((float) vec[i] - mean, 2);
}
float stdev = sqrt(accum / (vec.size()-1));
return stdev;
}
/*
* The vector temperatures here can be light or temperature, just called temperature as that's
* what it was initally for
* */
template <typename T>
void summarise_individual(string biobank_id, vector<cwa_timestamp> &cwa_timestamps,
vector<T> &temperatures, string summary_filename) {
float total_mean = 0.0;
float sd = 0.0;
float iqr = 0.0;
map<int, vector<double>> hourly_temps;
total_mean = mean(temperatures);
sd = stdDev(temperatures);
iqr = percentile(temperatures, 0.75) - percentile(temperatures, 0.25);
for (int i = 0; i < temperatures.size(); ++i) {
cwa_timestamp ts = cwa_timestamps[i];
int hour = ts.hours;
hourly_temps[hour].push_back(temperatures[i]);
}
ofstream summary_file;
summary_file.open(summary_filename, fstream::app);
summary_file << biobank_id << "," << to_string(total_mean) << "," << to_string(sd) << "," << to_string(iqr) << ",";
for (int i = 0; i < 24; ++i) {
try {
float temp_mean = mean(hourly_temps[i]);
float temp_sd = stdDev(hourly_temps[i]);
summary_file << temp_mean << ",";
summary_file << temp_sd;
if (i == 23) {
summary_file << "\n";
} else {
summary_file << ",";
}
} catch (...) {
cerr << "Error occured" << endl;
}
}
summary_file.close();
}
void process_header(char header_buffer[], OM_READER_HEADER_PACKET &header) {
memcpy(&header.packetHeader, header_buffer, 2);
memcpy(&header.packetLength, header_buffer + 2, 2);
memcpy(&header.reserved1, header_buffer + 4, 1);
memcpy(&header.deviceId, header_buffer + 5, 2);
memcpy(&header.sessionId, header_buffer + 7, 4);
memcpy(&header.reserved2, header_buffer + 11, 2);
memcpy(&header.loggingStartTime, header_buffer + 13, 4);
memcpy(&header.loggingEndTime, header_buffer + 17, 4);
memcpy(&header.loggingCapacity, header_buffer + 21, 4);
memcpy(&header.reserved3, header_buffer + 25, 11);
memcpy(&header.samplingRate, header_buffer + 36, 1);
memcpy(&header.lastChangeTime, header_buffer + 37, 4);
memcpy(&header.firmwareRevision, header_buffer + 41, 1);
memcpy(&header.timeZone, header_buffer + 42, 2);
memcpy(&header.reserved4, header_buffer + 44, 20);
memcpy(&header.annotation, header_buffer + 64, 448);
memcpy(&header.reserved, header_buffer + 512, 512);
}
void process_data_block(char data_buffer[], OM_READER_DATA_PACKET &data) {
memcpy(&data.packetHeader, data_buffer, 2);
memcpy(&data.packetLength, data_buffer + 2, 2);
memcpy(&data.deviceFractional, data_buffer + 4, 2);
memcpy(&data.sessionId, data_buffer + 6, 4);
memcpy(&data.sequenceId, data_buffer + 10, 4);
memcpy(&data.timestamp, data_buffer + 14, 4);
memcpy(&data.light, data_buffer + 18, 2);
memcpy(&data.temperature, data_buffer + 20, 2);
memcpy(&data.events, data_buffer + 22, 1);
memcpy(&data.battery, data_buffer + 23, 1);
memcpy(&data.sampleRate, data_buffer + 24, 1);
memcpy(&data.numAxesBPS, data_buffer + 25, 1);
memcpy(&data.timestampOffset, data_buffer + 26, 2);
memcpy(&data.sampleCount, data_buffer + 28, 2);
memcpy(&data.rawSampleData, data_buffer + 30, 480);
memcpy(&data.checksum, data_buffer + 510, 2);
}
void process_cwa_timestamp(uint32_t raw, cwa_timestamp ×tamp) {
timestamp.year = ((raw >> 26) & 0x3f) + 2000;
timestamp.month = ((raw >> 22) & 0x0f);
timestamp.day = ((raw >> 17) & 0x1f);
timestamp.hours = ((raw >> 12) & 0x1f);
timestamp.mins = ((raw >> 6) & 0x3f);
timestamp.seconds = raw & 0x3f;
}
void show_header_details(OM_READER_HEADER_PACKET &header) {
cwa_timestamp ts_start;
cwa_timestamp ts_end;
cwa_timestamp ts_change;
process_cwa_timestamp(header.loggingStartTime, ts_start);
process_cwa_timestamp(header.loggingEndTime, ts_end);
process_cwa_timestamp(header.lastChangeTime, ts_change);
cout << "------------Header Details------------" << endl;
cout << setw(20) << left << "Packet Header: " << (char*) &header.packetHeader << endl;
cout << setw(20) << "Packet Length: " << header.packetLength << endl;
cout << setw(20) << "Device ID: " << header.deviceId << endl;
cout << setw(20) << "Session ID: " << header.sessionId << endl;
cout << setw(20) << "Logging Start Time: " << ts_start.day << " " << ts_start.month << " " << ts_start.year << " " << ts_start.hours << ":" << ts_start.mins << ":" << ts_start.seconds << endl;
cout << setw(20) << "Logging End Time: " << ts_end.day << " " << ts_end.month << " " << ts_end.year << " " << ts_end.hours << ":" << ts_end.mins << ":" << ts_end.seconds << endl;
cout << setw(20) << "Logging Capacity: " << header.loggingCapacity << " (0 = unlimited)" << endl;
cout << setw(20) << "Sampling Rate: " << (int) header.samplingRate << " Hz" << endl;
cout << setw(20) << "Last Change Time: " << ts_change.day << " " << ts_change.month << " " << ts_change.year << " " << ts_change.hours << ":" << ts_change.mins << ":" << ts_change.seconds << endl;
cout << setw(20) << "Firmware Revision: " << (int) header.firmwareRevision << endl;
cout << setw(20) << "Time Zone: " << header.timeZone << " (-1 = unknown)" << endl;
}
/*
* This and read_light_data are essentially the same, should refactor them together in to a more generic function
*/
void read_temp_data(ifstream &file, vector<uint16_t> &temps, vector<string> ×tamps, vector<cwa_timestamp> &cwa_timestamps) {
// Read data blocks until nothing is left
while (file.peek() != EOF) {
char data_buffer[512];
file.read(data_buffer, 512);
// Could use this to be more generic and to extract more data in future
// OM_READER_DATA_PACKET data;
// process_data_block();
int16_t temp;
uint32_t raw_timestamp;
cwa_timestamp timestamp;
memcpy(&temp, data_buffer + 20, 2); // Extract Temperature from data block
memcpy(&raw_timestamp, data_buffer + 14, 4); // Extract timestamp from data block
temp = (temp * 150.0 - 20500) / 1000.0; // Convert to degrees C
temps.push_back(temp);
process_cwa_timestamp(raw_timestamp, timestamp);
char timestamp_string[19];
sprintf(timestamp_string, "%4d-%02d-%02d %02d:%02d:%02d", timestamp.year,
timestamp.month,
timestamp.day,
timestamp.hours,
timestamp.mins,
timestamp.seconds);
timestamps.push_back(timestamp_string);
cwa_timestamps.push_back(timestamp);
}
}
void read_light_data(ifstream &file, vector<uint16_t> &light, vector<string> ×tamps, vector<cwa_timestamp> &cwa_timestamps) {
while (file.peek() != EOF) {
char data_buffer[512];
file.read(data_buffer, 512);
uint16_t lux;
uint32_t raw_timestamp;
cwa_timestamp timestamp;
memcpy(&lux, data_buffer + 18, 2); // Extract Light value from data block
memcpy(&raw_timestamp, data_buffer + 14, 4); // Extract timestamp
// No point trying to convert to lux (for UKBiobank Data), light sensor was partially covered
//double log10LuxTimes10Power3 = ((lux + 512.0) * 6000 / 1024);
//lux = pow(10.0, log10LuxTimes10Power3 / 1000.0); // In lux, supposedly
light.push_back(lux);
process_cwa_timestamp(raw_timestamp, timestamp);
char timestamp_string[19];
sprintf(timestamp_string, "%04d-%02d-%02d %02d:%02d:%02d", timestamp.year,
timestamp.month,
timestamp.day,
timestamp.hours,
timestamp.mins,
timestamp.seconds);
cwa_timestamps.push_back(timestamp);
timestamps.push_back(timestamp_string);
}
}
template <typename T>
void save_temps_to_file(vector<T> &temps, vector<string> ×tamps, string filename) {
uint16_t difference = 0;
if (temps.size() != timestamps.size()) {
// Temperatures have been averaged
difference = timestamps.size() - temps.size();
}
// Save temperatures to file
ofstream temp_file;
temp_file.open(filename, ios::out);
temp_file << "timestamp, temp\n";
for (int i = difference; i < temps.size(); ++i) {
temp_file << timestamps[i] << ", " << temps[i] << "\n";
}
temp_file.close();
}
template <typename T>
void save_light_to_file(vector<T> &light, vector<string> ×tamps, string filename) {
uint16_t difference = 0;
if (light.size() != timestamps.size()) {
// Temperatures have been averaged
difference = timestamps.size() - light.size();
}
// Save temperatures to file
ofstream light_file;
light_file.open(filename, ios::out);
light_file << "timestamp, light\n";
for (int i = difference; i < light.size(); ++i) {
light_file << timestamps[i] << ", " << light[i] << "\n";
}
light_file.close();
}
template <typename T>
void save_temps_and_sigs(vector<T> &temps, vector<int> &signals, string filename) {
ofstream temp_file;
temp_file.open(filename, ios::out);
for (int i = 0; i < temps.size(); ++i) {
temp_file << i << " " << temps[i] << " " << signals[i] << "\n";
}
temp_file.close();
}
void central_moving_average(vector<uint16_t> &temps, vector<float> &av_temps, uint16_t window_size) {
uint32_t n = temps.size();
if (n <= window_size) {
cerr << "Window size too large (" << window_size << ") to compute central MA for data of length " << n << endl;
exit(1);
}
uint32_t start, end;
start = window_size / 2;
end = n - (window_size / 2);
for (int i = start; i < end; ++i) {
float sum = 0.0;
int evals = 0;
// Left of central point
for (int l = i - (window_size/2); l < i; ++l) {
sum += (float) temps[l];
}
// Right of central point
for (int r = i; r < i + (window_size/2); ++r) {
sum += (float) temps[r];
}
av_temps.push_back(sum/window_size);
}
}
// NOT USED, kept for potential future use
//
// Based on "Smoothed z-score algo" found on SO, here:
// https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data
// Needs optimizing
void peak_detect(vector<uint16_t> &temps, vector<int> &sigs,
uint16_t lag, float threshold, float influence) {
for (int i = 0; i < temps.size(); ++i) {
sigs.push_back(0);
}
vector<float> filteredInput(temps.size(), 0.0);
vector<float> avgFilter(temps.size(), 0.0);
vector<float> stdFilter(temps.size(), 0.0);
vector<float> subVecStart(temps.begin(), temps.begin() + lag);
avgFilter[lag] = mean(subVecStart);
stdFilter[lag] = stdDev(subVecStart);
for (int i = lag + 1; i < temps.size(); ++i) {
if (abs(temps[i] - avgFilter[i-1]) > threshold * stdFilter[i-1]) {
if (temps[i] > avgFilter[i-1]) {
sigs[i] = 1;
} else {
// If we wanted negative signals for troughs too
sigs[i] = -1;
}
// Makes the influence lower
filteredInput[i] = influence*temps[i] + (1 - influence) * filteredInput[i-1];
} else {
sigs[i] = 0;
filteredInput[i] = temps[i];
}
//Adjust the filters
vector<float> subVec(filteredInput.begin() + i - lag, filteredInput.begin() + i);
avgFilter[i] = mean(subVec);
stdFilter[i] = stdDev(subVec);
}
}
void print_usage() {
cout << "----------Axivity AX3 Temperature Extractor----------" << endl;
cout << "Author: James Christie (jlc_christie@live.com)" << endl;
cout << "Date: June 2018" << endl;
cout << "Usage: ./ax3 -t|-l -i [input file] -o [output file]" << endl;
cout << "\t-t -- Temperature Mode" << endl;
cout << "\t-l -- Light Mode" << endl;
cout << "\t-i -- Path to .cwa input file" << endl;
cout << "\t-o -- Path to output file to be generated" << endl;
cout << "\t-s -- Path to summary statistics file (appends if exists)" << endl;
cout << "\t-a -- Use a central moving average on the read data (Window Size" << endl;
cout << "\t must be passed in to args after -a, e.g. -a 50" << endl;
}
int main(int argc, char* argv[]) {
string in_filename;
string out_filename;
string sum_filename;
string biobank_id;
bool temp_mode = false;
bool mode_set = false;
bool in_set = false;
bool out_set = false;
bool use_sma = false;
bool sum_stats = false;
uint16_t win_size = 50;
for (int i = 1; i < argc; ++i) {
switch (string(argv[i])[1]) {
case 'l':
if (mode_set) {
cerr << "error: cannot use -t and -l flags together" << endl;
exit(1);
}
mode_set = true;
break;
case 't':
if (mode_set) {
cerr << "error: cannot use -t and -l flags together" << endl;
exit(1);
}
temp_mode = true;
mode_set = true;
break;
case 'i':
in_filename = string(argv[i+1]);
in_set = true;
biobank_id = in_filename.substr(in_filename.find_last_of("/")+1, string::npos);
biobank_id = biobank_id.substr(0, biobank_id.find("_"));
++i;
break;
case 'o':
out_filename = string(argv[i+1]);
out_set = true;
++i;
break;
case 's':
sum_stats = true;
sum_filename = string(argv[i+1]);
++i;
break;
case 'a':
use_sma = true;
win_size = stoi(string(argv[i+1]));
++i;
break;
default:
cerr << "error: argument not recognised: " << string(argv[i]) << endl;
cerr << "see usage: \n" << endl;
print_usage();
exit(1);
}
}
if (!mode_set) {
cerr << "error: mode not set, use -t for temperature, -l for light" << endl << endl;
print_usage();
exit(1);
} else if (!in_set) {
cerr << "error: no input file given" << endl << endl;
print_usage();
exit(1);
} else if (!out_set) {
cerr << "error: no output file given" << endl << endl;
print_usage();
exit(1);
}
ifstream aws_file (in_filename, ios::in | ios::binary);
// Read header
char header_buffer[1024];
if (!aws_file.read(header_buffer, 1024)) {
cerr << "Error occured reading header of file, exiting." << endl;
exit(1);
}
OM_READER_HEADER_PACKET header;
process_header(header_buffer, header);
//show_header_details(header);
vector<uint16_t> temperatures;
vector<uint16_t> light;
vector<string> timestamps;
vector<cwa_timestamp> cwa_timestamps;
if (temp_mode) {
read_temp_data(aws_file, temperatures, timestamps, cwa_timestamps);
} else {
read_light_data(aws_file, light, timestamps, cwa_timestamps);
}
vector<float> av;
if (use_sma) {
if (temp_mode) {
central_moving_average(temperatures, av, win_size);
save_temps_to_file(av, timestamps, out_filename);
} else {
central_moving_average(light, av, win_size);
save_light_to_file(av, timestamps, out_filename);
}
} else {
if (temp_mode) {
save_temps_to_file(temperatures, timestamps, out_filename);
} else {
save_light_to_file(light, timestamps, out_filename);
}
}
if (sum_stats) {
if (temp_mode) {
summarise_individual(biobank_id, cwa_timestamps, temperatures, sum_filename);
} else {
summarise_individual(biobank_id, cwa_timestamps, light, sum_filename);
}
}
aws_file.close();
return 0;
}