-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAfterImage.cpp
More file actions
528 lines (456 loc) · 16.4 KB
/
AfterImage.cpp
File metadata and controls
528 lines (456 loc) · 16.4 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
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
#include "AfterImage.h"
#include <algorithm>
#include <sstream>
// incStat 实现
incStat::incStat(double Lambda, const std::string &ID, double init_time,
bool isTypeDiff)
: ID(ID), CF1(0), CF2(0), w(1e-20), isTypeDiff(isTypeDiff), Lambda(Lambda),
lastTimestamp(init_time), cur_mean(NAN), cur_var(NAN), cur_std(NAN) {}
void incStat::insert(double v, double t) {
if (isTypeDiff) {
double dif = t - lastTimestamp;
v = dif > 0 ? dif : 0;
}
processDecay(t);
CF1 += v;
CF2 += v * v;
w += 1;
cur_mean = NAN;
cur_var = NAN;
cur_std = NAN;
for (auto *cov : covs) {
cov->update_cov(ID, v, t);
}
}
double incStat::processDecay(double timestamp) {
// 计算时间衰减因子
double factor = 1.0;
double timeDiff = timestamp - lastTimestamp;
if (timeDiff > 0) {
factor = std::pow(2, (-Lambda * timeDiff));
CF1 *= factor;
CF2 *= factor;
w *= factor;
lastTimestamp = timestamp;
}
return factor;
}
double incStat::mean() const {
if (std::isnan(cur_mean)) {
cur_mean = CF1 / w;
}
return cur_mean;
}
double incStat::var() const {
if (std::isnan(cur_var)) {
cur_var = std::abs(CF2 / w - std::pow(mean(), 2));
}
return cur_var;
}
double incStat::std() const {
if (std::isnan(cur_std)) {
cur_std = std::sqrt(var());
}
return cur_std;
}
double incStat::cov(const std::string &ID2) const {
for (auto *cov : covs) {
if (cov->getIncStats()[0]->getID() == ID2 ||
cov->getIncStats()[1]->getID() == ID2) {
return cov->cov();
}
}
return NAN;
}
double incStat::pcc(const std::string &ID2) const {
for (auto *cov : covs) {
if (cov->getIncStats()[0]->getID() == ID2 ||
cov->getIncStats()[1]->getID() == ID2) {
return cov->pcc();
}
}
return NAN;
}
std::vector<double> incStat::cov_pcc(const std::string &ID2) const {
for (auto *cov : covs) {
if (cov->getIncStats()[0]->getID() == ID2 ||
cov->getIncStats()[1]->getID() == ID2) {
return cov->get_stats1();
}
}
return {NAN, NAN};
}
double incStat::radius(const std::vector<incStat *> &other_incStats) const {
double sum = var() * var();
for (auto *incS : other_incStats) {
sum += incS->var() * incS->var();
}
return std::sqrt(sum);
}
double incStat::magnitude(const std::vector<incStat *> &other_incStats) const {
double sum = std::pow(mean(), 2);
for (auto *incS : other_incStats) {
sum += std::pow(incS->mean(), 2);
}
return std::sqrt(sum);
}
std::vector<double> incStat::allstats_1D() const {
cur_mean = CF1 / w;
cur_var = std::abs(CF2 / w - std::pow(cur_mean, 2));
return {w, cur_mean, cur_var};
}
std::vector<double> incStat::allstats_2D(const std::string &ID2) const {
std::vector<double> stats1D = allstats_1D();
std::vector<double> stats2D(4, NAN);
for (auto *cov : covs) {
if (cov->getIncStats()[0]->getID() == ID2 ||
cov->getIncStats()[1]->getID() == ID2) {
stats2D = cov->get_stats2();
break;
}
}
stats1D.insert(stats1D.end(), stats2D.begin(), stats2D.end());
return stats1D;
}
std::vector<std::string> incStat::getHeaders_1D(bool suffix) const {
std::string s0;
ID.empty() ? s0 = "" : s0 = "_0";
if (suffix) {
s0 = "_" + ID;
}
return {"weight" + s0, "mean" + s0, "std" + s0};
}
std::vector<std::string> incStat::getHeaders_2D(const std::string &ID2,
bool suffix) const {
auto hdrs1D = getHeaders_1D(suffix);
std::string s0 = suffix ? "_" + ID : "_0";
std::string s1 = suffix ? "_" + ID2 : "_1";
std::vector<std::string> hdrs2D = {
"radius_" + s0 + "_" + s1, "magnitude_" + s0 + "_" + s1,
"covariance_" + s0 + "_" + s1, "pcc_" + s0 + "_" + s1};
hdrs1D.insert(hdrs1D.end(), hdrs2D.begin(), hdrs2D.end());
return hdrs1D;
}
/*-------------------------------------------------checkpoint
* 1-------------------------------------------------*/
// incStat_cov 实现
incStat_cov::incStat_cov(incStat *incS1, incStat *incS2, double init_time)
: incStats{incS1, incS2}, lastRes{0, 0}, CF3(0), w3(1e-20),
lastTimestamp_cf3(init_time) {}
void incStat_cov::update_cov(const std::string &ID, double v, double t) {
int inc = 0;
if (ID == incStats[0]->getID()) {
inc = 0;
} else if (ID == incStats[1]->getID()) {
inc = 1;
} else {
std::cout << "update_cov ID error" << std::endl;
return;
}
incStats[1 - inc]->processDecay(t);
processDecay(t, inc);
double res = (v - incStats[inc]->mean());
double resid = (v - incStats[inc]->mean()) * lastRes[1 - inc];
CF3 += resid;
w3 += 1;
lastRes[inc] = res;
}
double incStat_cov::processDecay(double t, int micro_inc_indx) {
double factor = 1;
double timeDiffs_cf3 = t - lastTimestamp_cf3;
if (timeDiffs_cf3 > 0) {
factor =
std::pow(2, -incStats[micro_inc_indx]->getLambda() * timeDiffs_cf3);
CF3 *= factor;
w3 *= factor;
lastTimestamp_cf3 = t;
lastRes[micro_inc_indx] *= factor;
}
return factor;
}
double incStat_cov::cov() const { return CF3 / w3; }
double incStat_cov::pcc() const {
double ss = incStats[0]->std() * incStats[1]->std();
return ss != 0 ? cov() / ss : 0;
}
std::vector<double> incStat_cov::get_stats1() const { return {cov(), pcc()}; }
std::vector<double> incStat_cov::get_stats2() const {
return {incStats[0]->radius({incStats[1]}),
incStats[0]->magnitude({incStats[1]}), cov(), pcc()};
}
std::vector<double> incStat_cov::get_stats3() const {
return {incStats[0]->weight(),
incStats[0]->mean(),
incStats[0]->std(),
incStats[1]->weight(),
incStats[1]->mean(),
incStats[1]->std(),
cov(),
pcc()};
}
std::vector<double> incStat_cov::get_stats4() const {
return {incStats[0]->weight(),
incStats[0]->mean(),
incStats[0]->std(),
incStats[1]->weight(),
incStats[1]->mean(),
incStats[1]->std(),
incStats[0]->radius({incStats[1]}),
incStats[0]->magnitude({incStats[1]}),
cov(),
pcc()};
}
std::vector<std::string> incStat_cov::getHeaders(int ver, bool suffix) const {
std::string s0 = suffix ? incStats[0]->getID() : "0";
std::string s1 = suffix ? incStats[1]->getID() : "1";
std::vector<std::string> headers;
switch (ver) {
case 1:
headers = {"covariance_" + s0 + "_" + s1, "pcc_" + s0 + "_" + s1};
break;
case 2:
headers = {"radius_" + s0 + "_" + s1, "magnitude_" + s0 + "_" + s1,
"covariance_" + s0 + "_" + s1, "pcc_" + s0 + "_" + s1};
break;
case 3:
headers = {"weight_" + s0,
"mean_" + s0,
"std_" + s0,
"weight_" + s1,
"mean_" + s1,
"std_" + s1,
"covariance_" + s0 + "_" + s1,
"pcc_" + s0 + "_" + s1};
break;
case 4:
headers = {"weight_" + s0, "mean_" + s0, "std_" + s0,
"covariance_" + s0 + "_" + s1, "pcc_" + s0 + "_" + s1};
break;
case 5:
headers = {"weight_" + s0,
"mean_" + s0,
"std_" + s0,
"weight_" + s1,
"mean_" + s1,
"std_" + s1,
"radius_" + s0 + "_" + s1,
"magnitude_" + s0 + "_" + s1,
"covariance_" + s0 + "_" + s1,
"pcc_" + s0 + "_" + s1};
break;
}
return headers;
}
// incStatDB 实现
incStatDB::incStatDB(double limit, double default_lambda)
: limit(limit), df_lambda(default_lambda) {}
double incStatDB::get_lambda(double Lambda) const {
return std::isnan(df_lambda) ? Lambda : df_lambda;
}
incStat *incStatDB::register_stream(const std::string &ID, double Lambda,
double init_time, bool isTypeDiff) {
Lambda = get_lambda(Lambda);
std::string key = ID + "_" + std::to_string(Lambda);
auto it = HT.find(key);
if (it == HT.end()) {
if (HT.size() + 1 > limit) {
throw std::runtime_error("Adding Entry: " + key +
" would exceed incStatHT 1D limit of " +
std::to_string(limit) +
". Observation Rejected.");
}
std::unique_ptr<incStat> new_stat(
new incStat(Lambda, ID, init_time, isTypeDiff));
std::pair<std::string, std::unique_ptr<incStat>> pair(
key, std::move(new_stat));
auto insert_result = HT.insert(std::move(pair));
return insert_result.first->second.get();
}
return it->second.get();
}
incStat_cov *incStatDB::register_cov(const std::string &ID1,
const std::string &ID2, double Lambda,
double init_time, bool isTypeDiff) {
Lambda = get_lambda(Lambda);
incStat *incS1 = register_stream(ID1, Lambda, init_time, isTypeDiff);
incStat *incS2 = register_stream(ID2, Lambda, init_time, isTypeDiff);
for (auto *cov : incS1->covs) {
if (cov->getIncStats()[0]->getID() == ID2 ||
cov->getIncStats()[1]->getID() == ID2) {
return cov;
}
}
auto *inc_cov = new incStat_cov(incS1, incS2, init_time);
incS1->addCov(inc_cov);
incS2->addCov(inc_cov);
return inc_cov;
}
incStat *incStatDB::update(const std::string &ID, double t, double v,
double Lambda, bool isTypeDiff) {
incStat *incS = register_stream(ID, Lambda, t, isTypeDiff);
incS->insert(v, t);
return incS;
}
std::vector<double> incStatDB::get_1D_Stats(const std::string &ID,
double Lambda) const {
Lambda = get_lambda(Lambda);
std::string key = ID + "_" + std::to_string(Lambda);
auto it = HT.find(key);
if (it == HT.end()) {
return {std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()};
}
return it->second->allstats_1D();
}
std::vector<double> incStatDB::get_2D_Stats(const std::string &ID1,
const std::string &ID2,
double Lambda) const {
Lambda = get_lambda(Lambda);
std::string key = ID1 + "_" + std::to_string(Lambda);
auto it = HT.find(key);
if (it == HT.end()) {
return {std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()};
}
return it->second->cov_pcc(ID2);
}
std::pair<std::vector<std::vector<double>>,
std::vector<std::vector<std::string>>>
incStatDB::get_all_2D_Stats(const std::string &ID, double Lambda) const {
Lambda = get_lambda(Lambda);
std::string key = ID + "_" + std::to_string(Lambda);
auto it = HT.find(key);
if (it == HT.end()) {
return {{}, {}};
}
std::vector<std::vector<double>> stats;
std::vector<std::vector<std::string>> IDs;
for (auto *cov : it->second->covs) {
stats.push_back(cov->get_stats1());
IDs.push_back(
{cov->getIncStats()[0]->getID(), cov->getIncStats()[1]->getID()});
}
return {stats, IDs};
}
std::vector<double> incStatDB::get_nD_Stats(const std::vector<std::string> &IDs,
double Lambda) const {
Lambda = get_lambda(Lambda);
std::vector<incStat *> incStats;
for (const auto &ID : IDs) {
std::string key = ID + "_" + std::to_string(Lambda);
auto it = HT.find(key);
if (it != HT.end()) {
incStats.push_back(it->second.get());
}
}
double rad = 0, mag = 0;
for (auto *incS : incStats) {
rad += incS->var();
mag += incS->mean() * incS->mean();
}
return {std::sqrt(rad), std::sqrt(mag)};
}
std::vector<double> incStatDB::update_get_1D_Stats(const std::string &ID,
double t, double v,
double Lambda,
bool isTypeDiff) {
incStat *incS = update(ID, t, v, Lambda, isTypeDiff);
return incS->allstats_1D();
}
std::vector<double> incStatDB::update_get_2D_Stats(const std::string &ID1,
const std::string &ID2,
double t1, double v1,
double Lambda, int level) {
incStat_cov *inc_cov = register_cov(ID1, ID2, Lambda, t1);
inc_cov->update_cov(ID1, v1, t1);
return level == 1 ? inc_cov->get_stats1() : inc_cov->get_stats2();
}
std::vector<double> incStatDB::update_get_1D2D_Stats(const std::string &ID1,
const std::string &ID2,
double t1, double v1,
double Lambda) {
auto stats1D = update_get_1D_Stats(ID1, t1, v1, Lambda);
auto stats2D = update_get_2D_Stats(ID1, ID2, t1, v1, Lambda, 2);
stats1D.insert(stats1D.end(), stats2D.begin(), stats2D.end());
return stats1D;
}
std::vector<std::string> incStatDB::getHeaders_1D(double Lambda,
const std::string &ID) const {
Lambda = get_lambda(Lambda);
auto hdrs = incStat(Lambda, ID).getHeaders_1D(false);
std::vector<std::string> result;
for (const auto &hdr : hdrs) {
result.push_back(std::to_string(Lambda) + "_" + hdr);
}
return result;
}
std::vector<std::string>
incStatDB::getHeaders_2D(double Lambda, const std::vector<std::string> *IDs,
int ver) const {
Lambda = get_lambda(Lambda);
std::vector<std::string> defaultIDs = {"0", "1"};
const auto &idList = IDs ? *IDs : defaultIDs;
auto hdrs = incStat_cov(new incStat(Lambda, idList[0]),
new incStat(Lambda, idList[0]), Lambda)
.getHeaders(ver, false);
std::vector<std::string> result;
for (const auto &hdr : hdrs) {
result.push_back(std::to_string(Lambda) + "_" + hdr);
}
return result;
}
std::vector<std::string>
incStatDB::getHeaders_1D2D(double Lambda, const std::vector<std::string> *IDs,
int ver) const {
Lambda = get_lambda(Lambda);
std::vector<std::string> defaultIDs = {"0", "1"};
const auto &idList = IDs ? *IDs : defaultIDs;
auto hdrs1D = getHeaders_1D(Lambda, idList[0]);
auto hdrs2D = getHeaders_2D(Lambda, IDs, ver);
hdrs1D.insert(hdrs1D.end(), hdrs2D.begin(), hdrs2D.end());
return hdrs1D;
}
std::vector<std::string>
incStatDB::getHeaders_nD(double Lambda,
const std::vector<std::string> &IDs) const {
Lambda = get_lambda(Lambda);
std::string ID = ":";
for (const auto &s : IDs) {
ID += "_" + s;
}
std::vector<std::string> hdrs = {"radius" + ID, "magnitude" + ID};
std::vector<std::string> result;
for (const auto &hdr : hdrs) {
result.push_back(std::to_string(Lambda) + "_" + hdr);
}
return result;
}
int incStatDB::cleanOutOldRecords(double cutoffWeight, double curTime) {
int n = 0;
std::vector<std::pair<std::string, std::unique_ptr<incStat>>>
sorted_entries;
sorted_entries.reserve(HT.size());
for (const auto &entry : HT) {
std::pair<std::string, std::unique_ptr<incStat>> pair(
entry.first,
std::move(const_cast<std::unique_ptr<incStat> &>(entry.second)));
sorted_entries.push_back(std::move(pair));
}
std::sort(sorted_entries.begin(), sorted_entries.end(),
[](const std::pair<std::string, std::unique_ptr<incStat>> &a,
const std::pair<std::string, std::unique_ptr<incStat>> &b) {
return a.second->weight() < b.second->weight();
});
for (const auto &entry : sorted_entries) {
entry.second->processDecay(curTime);
double W = entry.second->weight();
if (W <= cutoffWeight) {
HT.erase(entry.first);
n++;
} else {
break;
}
}
return n;
}