forked from Yiqing-Gu/Pidentify
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNOTAPoints.cpp
More file actions
470 lines (390 loc) · 16.7 KB
/
NOTAPoints.cpp
File metadata and controls
470 lines (390 loc) · 16.7 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
#include <random>
#include <cmath>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "nanoflann/KDTreeVectorOfVectorsAdaptor.h"
#include "NOTAPoints.h"
#include "modelState.h"
#include "testResults.h"
#include "process.h"
#include "cachePaths.h"
void updateFeatureBB(const std::vector<double>& features) {
size_t dim = features.size();
for (size_t i = 0; i < dim; ++i) {
if (features[i] < MODEL_STATE.featureMins[i]) {
MODEL_STATE.featureMins[i] = features[i];
}
if (features[i] > MODEL_STATE.featureMaxs[i]) {
MODEL_STATE.featureMaxs[i] = features[i];
}
}
}
void findFeatureBB(const std::unordered_map<std::string, std::vector<ClassMember> >& dataset, double extension) {
MODEL_STATE.featureMins = MODEL_STATE.featureMaxs = dataset.begin()->second[0].features;
size_t dim = dataset.begin()->second[0].features.size();
for (const auto& pair : dataset) {
for (const auto& obj : pair.second) {
updateFeatureBB(obj.features);
}
}
for (size_t i = 0; i < dim; ++i) {
double extensionVal = (MODEL_STATE.featureMaxs[i] - MODEL_STATE.featureMins[i]) * extension;
MODEL_STATE.featureMins[i] -= extensionVal;
MODEL_STATE.featureMaxs[i] += extensionVal;
}
}
// Convert integer index into the multiplier for void NOTA points
double NNStepMultiplier(size_t idx) {
return NN_STEPS_START + (NN_STEP_SIZE * idx);
}
// Convert multiplier into integer index for void NOTA points or -1 for hyperspace NOTA points
int NNUnitsFromClass(double multiplier) {
if (multiplier < NN_STEPS_START || multiplier > NN_STEPS_START + NN_STEP_SIZE * (NUM_NN_STEPS - 1)) {
return -1;
}
else {
return std::round((multiplier - NN_STEPS_START) / NN_STEP_SIZE);
}
}
/*
void insertVoidNOTAPoints(const std::vector<double>& featureMins, const std::vector<double>& featureMaxs, size_t idx,
std::mt19937& gen,
const std::unordered_map<std::string, KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<double> >, double>* >& kdTrees,
const std::vector<std::unordered_map<std::string, double> >& minNNDistances, std::vector<ClassMember>& NOTAPoints) {
size_t total = std::max(MODEL_STATE.datasetSize, MIN_NOTA_POINTS);
//size_t total = 10;
size_t dim = featureMins.size();
size_t neighborIndices[1];
double squaredDistances[1];
// Create random number generators
std::vector<std::uniform_real_distribution<double> > featureGenerators(dim);
for (size_t i = 0; i < dim; ++i) {
featureGenerators[i] = std::move(std::uniform_real_distribution<double>(featureMins[i], featureMaxs[i]));
}
size_t created = 0;
bool validNOTAPoint;
for (size_t attempts = 0, maxAttempts = total * 10; created < total && attempts < maxAttempts; ++attempts) {
// Create a new datapoint
ClassMember NOTAPoint(std::vector<double>(dim), "NOTA", 0, NOTACategory::VOID, idx);
for (size_t i = 0; i < dim; ++i) {
NOTAPoint.features[i] = featureGenerators[i](gen);
}
// Check if datapoint satisfies the minimum nearest neighbor distance from classes requirement
validNOTAPoint = true;
for (const auto& kdTree : kdTrees) {
kdTree.second->query(&NOTAPoint.features[0], 1, &neighborIndices[0], &squaredDistances[0]);
if (std::sqrt(squaredDistances[0]) < minNNDistances[idx].at(kdTree.first)) {
validNOTAPoint = false;
break;
}
}
// Add datapoint to NOTA points if it satisfies minimum nearest neighbor distance requirement
if (validNOTAPoint) {
NOTAPoints.push_back(std::move(NOTAPoint));
++created;
}
}
// Update the state to reflect the total void NOTA points that will be tested on across all folds
TEST_RESULTS.voidRandomPoints[idx][0] = created * K_FOLDS;
}
*/
void insertVoidNOTAPoints(const std::vector<double>& featureMins, const std::vector<double>& featureMaxs, size_t idx,
std::mt19937& gen,
const std::unordered_map<std::string, KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<double> >, double>* >& kdTrees,
const std::vector<std::unordered_map<std::string, double> >& minNNDistances, std::vector<ClassMember>& NOTAPoints) {
size_t total = std::max(MODEL_STATE.datasetSize, MIN_NOTA_POINTS);
//size_t total = 10;
size_t dim = featureMins.size();
size_t neighborIndices[1];
double squaredDistances[1];
// Create random number generators
std::vector<std::uniform_real_distribution<double> > featureGenerators(dim);
for (size_t i = 0; i < dim; ++i) {
featureGenerators[i] = std::move(std::uniform_real_distribution<double>(featureMins[i], featureMaxs[i]));
}
size_t created = 0;
bool validNOTAPoint;
for (size_t attempts = 0, maxAttempts = total * 10; created < total && attempts < maxAttempts; ++attempts) {
// Create a new datapoint
ClassMember NOTAPoint(std::vector<double>(dim), "NOTA", 0, NOTACategory::VOID, idx);
for (size_t i = 0; i < dim; ++i) {
NOTAPoint.features[i] = featureGenerators[i](gen);
}
// Check if datapoint satisfies the minimum nearest neighbor distance from classes requirement
validNOTAPoint = true;
for (const auto& kdTree : kdTrees) {
kdTree.second->query(&NOTAPoint.features[0], 1, &neighborIndices[0], &squaredDistances[0]);
if (std::sqrt(squaredDistances[0]) < minNNDistances[idx].at(kdTree.first)) {
validNOTAPoint = false;
break;
}
}
// Add datapoint to NOTA points if it satisfies minimum nearest neighbor distance requirement
if (validNOTAPoint) {
NOTAPoints.push_back(std::move(NOTAPoint));
++created;
}
}
// Update the state to reflect the total void NOTA points that will be tested on across all folds
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
TEST_RESULTS.voidRandomPoints[pvalCat][idx][0] = created * K_FOLDS;
}
}
/*
void insertHyperspaceNOTAPoints(const std::vector<std::pair<double, double> >& outerBbox,
const std::vector<std::pair<double, double> >& innerBbox, NOTACategory NOTALoc, std::mt19937& gen,
std::vector<ClassMember>& NOTAPoints) {
size_t total = std::max(MODEL_STATE.datasetSize, MIN_NOTA_POINTS);
//size_t total = 10;
size_t dim = outerBbox.size();
// Create random number generators
std::vector<std::uniform_real_distribution<double> > featureGenerators(dim);
for (size_t i = 0; i < dim; ++i) {
featureGenerators[i] = std::move(std::uniform_real_distribution<double>(outerBbox[i].first, outerBbox[i].second));
}
int genAttempts = 0, maxFeatureGenAttempts = 100;
for (size_t numCreatedPoints = 0; numCreatedPoints < total; ++numCreatedPoints) {
ClassMember NOTAPoint(std::vector<double>(dim), "NOTA", 0, NOTALoc, -1);
// Generate features between an inner and outer bounding box
for (size_t i = 0; i < dim; ++i) {
for (genAttempts = 0; genAttempts < maxFeatureGenAttempts; ++genAttempts) {
NOTAPoint.features[i] = featureGenerators[i](gen);
if (NOTAPoint.features[i] < innerBbox[i].first || NOTAPoint.features[i] > innerBbox[i].second ||
std::abs(outerBbox[i].first - innerBbox[i].first) < 1e-6) {
break;
}
}
// If no longer able to create points between the inner and outer bounding box, abort
if (genAttempts >= maxFeatureGenAttempts) {
TEST_RESULTS.hyperspaceRandomPoints[NOTALoc][0] = numCreatedPoints * K_FOLDS;
return;
}
}
NOTAPoints.push_back(std::move(NOTAPoint));
}
// Update the state to reflect the total hyperspace NOTA points that will be tested on across all folds
TEST_RESULTS.hyperspaceRandomPoints[NOTALoc][0] = total * K_FOLDS;
}
*/
void insertHyperspaceNOTAPoints(const std::vector<std::pair<double, double> >& outerBbox,
const std::vector<std::pair<double, double> >& innerBbox, NOTACategory NOTALoc, std::mt19937& gen,
std::vector<ClassMember>& NOTAPoints) {
size_t total = std::max(MODEL_STATE.datasetSize, MIN_NOTA_POINTS);
//size_t total = 10;
size_t dim = outerBbox.size();
// Create random number generators
std::vector<std::uniform_real_distribution<double> > featureGenerators(dim);
for (size_t i = 0; i < dim; ++i) {
featureGenerators[i] = std::move(std::uniform_real_distribution<double>(outerBbox[i].first, outerBbox[i].second));
}
int genAttempts = 0, maxFeatureGenAttempts = 100;
for (size_t numCreatedPoints = 0; numCreatedPoints < total; ++numCreatedPoints) {
ClassMember NOTAPoint(std::vector<double>(dim), "NOTA", 0, NOTALoc, -1);
// Generate features between an inner and outer bounding box
for (size_t i = 0; i < dim; ++i) {
for (genAttempts = 0; genAttempts < maxFeatureGenAttempts; ++genAttempts) {
NOTAPoint.features[i] = featureGenerators[i](gen);
if (NOTAPoint.features[i] < innerBbox[i].first || NOTAPoint.features[i] > innerBbox[i].second ||
std::abs(outerBbox[i].first - innerBbox[i].first) < 1e-6) {
break;
}
}
// If no longer able to create points between the inner and outer bounding box, abort
if (genAttempts >= maxFeatureGenAttempts) {
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
TEST_RESULTS.hyperspaceRandomPoints[pvalCat][NOTALoc][0] = numCreatedPoints * K_FOLDS;
}
return;
}
}
NOTAPoints.push_back(std::move(NOTAPoint));
}
// Update the state to reflect the total hyperspace NOTA points that will be tested on across all folds
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
TEST_RESULTS.hyperspaceRandomPoints[pvalCat][NOTALoc][0] = total * K_FOLDS;
}
}
// Returns a map from the class name to the largest nearest neighbor distance internal to the class
std::unordered_map<std::string, double> findLargestNNDistancesPerClass(
const std::unordered_map<std::string, std::vector<ClassMember> >& dataset) {
// Copy datapoints to map from class name to matrix containing feature vectors for members of the class
std::unordered_map<std::string, std::vector<std::vector<double> > > classMap;
for (const auto& pair : dataset) {
classMap[pair.first].reserve(MODEL_STATE.numInstancesPerClass.at(pair.first));
for (const ClassMember& obj : pair.second) {
classMap[pair.first].push_back(obj.features);
}
}
// Calculate nearest neighbor distances within each class
std::unordered_map<std::string, std::vector<double> > nnDistances = computeNearestNeighborDistances(classMap);
// Find the largest nearest neighbor distance internal to each class
std::unordered_map<std::string, double> largestNNDistances;
for (const auto& pair : nnDistances) {
largestNNDistances[pair.first] = *std::max_element(pair.second.begin(), pair.second.end());
}
return largestNNDistances;
}
std::vector<ClassMember> insertRandomPoints(const std::unordered_map<std::string, std::vector<ClassMember> >& dataset) {
size_t dim = MODEL_STATE.featureMins.size();
std::unordered_map<std::string, std::vector<std::vector<double> > > matrixDataset;
for (const auto& pair : dataset) {
matrixDataset[pair.first].reserve(pair.second.size());
for (const ClassMember& obj : pair.second) {
matrixDataset[pair.first].insert(matrixDataset[pair.first].end(), obj.features);
}
}
// Create KD Trees for finding nearest neighbor distances
std::unordered_map<std::string, KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<double> >, double>* > kdTrees;
for (const auto& pair : matrixDataset) {
kdTrees[pair.first] = new KDTreeVectorOfVectorsAdaptor<std::vector<std::vector<double> >, double>(dim, pair.second, 10, 0);
}
std::random_device rd;
std::mt19937 gen(rd());
std::vector<ClassMember> NOTAPoints;
// Find the largest nearest neighbor distances for each class for different multipliers
std::unordered_map<std::string, double> largestClassNNDistances = findLargestNNDistancesPerClass(dataset);
std::vector<std::unordered_map<std::string, double> > minNNDistances(NUM_NN_STEPS);
for (size_t i = 0; i < NUM_NN_STEPS; ++i) {
for (const auto& pair : largestClassNNDistances) {
minNNDistances[i][pair.first] = pair.second * NNStepMultiplier(i);
}
}
// Create void NOTA points for different multipliers
for (size_t i = 0; i < NUM_NN_STEPS; ++i) {
insertVoidNOTAPoints(MODEL_STATE.featureMins, MODEL_STATE.featureMaxs, i, gen, kdTrees, minNNDistances, NOTAPoints);
}
std::vector<double> featureDifs(dim);
std::vector<std::pair<double, double> > innerBbox(dim);
std::vector<std::pair<double, double> > outerBbox(dim);
// Find the outer bounding box for hyperspace NOTA points
for (size_t i = 0; i < dim; ++i) {
featureDifs[i] = MODEL_STATE.featureMaxs[i] - MODEL_STATE.featureMins[i];
outerBbox[i].first = MODEL_STATE.featureMins[i] - (featureDifs[i] * HYPERSPACE_MAX_BBOX_EXTENSION);
outerBbox[i].second = MODEL_STATE.featureMaxs[i] + (featureDifs[i] * HYPERSPACE_MAX_BBOX_EXTENSION);
}
// Create hyperspace NOTA points for different inner bounding boxes
for (size_t lowerBoundIdx = 0; lowerBoundIdx < HYPERSPACE_LOWER_BOUNDS; ++lowerBoundIdx) {
for (size_t i = 0; i < dim; ++i) {
innerBbox[i].first = MODEL_STATE.featureMins[i] - (featureDifs[i] * HYPERSPACE_BBOX_LOWER_BOUNDS[lowerBoundIdx]);
innerBbox[i].second = MODEL_STATE.featureMaxs[i] + (featureDifs[i] * HYPERSPACE_BBOX_LOWER_BOUNDS[lowerBoundIdx]);
}
insertHyperspaceNOTAPoints(outerBbox, innerBbox, static_cast<NOTACategory>(lowerBoundIdx), gen, NOTAPoints);
}
for (const auto& kdTree : kdTrees) {
delete kdTree.second;
}
// Beginning of saving NOTA points
char delim;
double minNNUnits;
FILE* fp = fopen(CACHE_PATHS.NOTAPointsFilepath.c_str(), "w");
fprintf(fp, "category,minNNUnitsFromClass,");
for (size_t i = 0; i < dim; ++i) {
delim = (i == dim - 1) ? '\n' : ',';
fprintf(fp, "col%lu%c", i, delim);
}
for (size_t i = 0; i < NOTAPoints.size(); ++i) {
minNNUnits = (NOTAPoints[i].NOTALocation == NOTACategory::VOID) ?
NNStepMultiplier(NOTAPoints[i].NNUnitsFromClass) : NOTAPoints[i].NNUnitsFromClass;
fprintf(fp, "%i,%g,", NOTAPoints[i].NOTALocation, minNNUnits);
for (size_t j = 0; j < dim; ++j) {
delim = (j == dim - 1) ? '\n' : ',';
fprintf(fp, "%g%c", NOTAPoints[i].features[j], delim);
}
}
fclose(fp);
//fp = fopen(CACHE_PATHS.finishedFilepath.c_str(), "w");
//fclose(fp);
// Ending of saving NOTA points
return NOTAPoints;
}
std::vector<ClassMember> createNOTAPoints(const std::unordered_map<std::string, std::vector<ClassMember> >& dataset) {
findFeatureBB(dataset, 0.0);
return insertRandomPoints(dataset);
}
/*
std::vector<ClassMember> readNOTAPointsFromFile(const std::string& NOTAPointsFilename) {
std::ifstream NOTAPointsFile(NOTAPointsFilename);
std::stringstream ss;
std::string line, field;
int NOTALoc;
std::vector<ClassMember> NOTAPoints;
std::getline(NOTAPointsFile, line);
while (std::getline(NOTAPointsFile, line)) {
ss.str("");
ss.clear();
ss << line;
ClassMember NOTAPoint;
std::getline(ss, field, ',');
NOTALoc = std::stoi(field);
NOTAPoint.NOTALocation = static_cast<NOTACategory>(NOTALoc);
std::getline(ss, field, ',');
NOTAPoint.NNUnitsFromClass = NNUnitsFromClass(std::stod(field));
while (std::getline(ss, field, ',')) {
NOTAPoint.features.emplace_back(std::stod(field));
}
NOTAPoint.name = "NOTA";
NOTAPoint.lineNumber = 0;
if (NOTAPoint.NOTALocation == NOTACategory::VOID) {
++TEST_RESULTS.voidRandomPoints[NOTAPoint.NNUnitsFromClass][0];
}
else {
++TEST_RESULTS.hyperspaceRandomPoints[NOTAPoint.NOTALocation][0];
}
NOTAPoints.push_back(std::move(NOTAPoint));
}
NOTAPointsFile.close();
for (size_t i = 0; i < NUM_NN_STEPS; ++i) {
TEST_RESULTS.voidRandomPoints[i][0] *= K_FOLDS;
}
for (size_t i = 0; i < HYPERSPACE_LOWER_BOUNDS; ++i) {
TEST_RESULTS.hyperspaceRandomPoints[static_cast<NOTACategory>(i)][0] *= K_FOLDS;
}
return NOTAPoints;
}
*/
std::vector<ClassMember> readNOTAPointsFromFile(const std::string& NOTAPointsFilename) {
std::ifstream NOTAPointsFile(NOTAPointsFilename);
std::stringstream ss;
std::string line, field;
int NOTALoc;
std::vector<ClassMember> NOTAPoints;
std::getline(NOTAPointsFile, line);
while (std::getline(NOTAPointsFile, line)) {
ss.str("");
ss.clear();
ss << line;
ClassMember NOTAPoint;
std::getline(ss, field, ',');
NOTALoc = std::stoi(field);
NOTAPoint.NOTALocation = static_cast<NOTACategory>(NOTALoc);
std::getline(ss, field, ',');
NOTAPoint.NNUnitsFromClass = NNUnitsFromClass(std::stod(field));
while (std::getline(ss, field, ',')) {
NOTAPoint.features.emplace_back(std::stod(field));
}
NOTAPoint.name = "NOTA";
NOTAPoint.lineNumber = 0;
if (NOTAPoint.NOTALocation == NOTACategory::VOID) {
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
++TEST_RESULTS.voidRandomPoints[pvalCat][NOTAPoint.NNUnitsFromClass][0];
}
}
else {
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
++TEST_RESULTS.hyperspaceRandomPoints[pvalCat][NOTAPoint.NOTALocation][0];
}
}
NOTAPoints.push_back(std::move(NOTAPoint));
}
NOTAPointsFile.close();
for (int pvalCat = 0; pvalCat < PVALUE_NUMERATOR_MAX; ++pvalCat) {
for (size_t i = 0; i < NUM_NN_STEPS; ++i) {
TEST_RESULTS.voidRandomPoints[pvalCat][i][0] *= K_FOLDS;
}
for (size_t i = 0; i < HYPERSPACE_LOWER_BOUNDS; ++i) {
TEST_RESULTS.hyperspaceRandomPoints[pvalCat][static_cast<NOTACategory>(i)][0] *= K_FOLDS;
}
}
return NOTAPoints;
}