-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_average.cpp
More file actions
115 lines (95 loc) · 3.22 KB
/
calculate_average.cpp
File metadata and controls
115 lines (95 loc) · 3.22 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
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const string PARSED_DATA_PATH = "/home/kadyrbek/Desktop/WiFiLocalization/parsed_data/";
const string DATA_PATH = "/home/kadyrbek/Desktop/WiFiLocalization/data/lg";
bool calculateAveragesForPoint (string file_name, int class_id, int point_id, bool is_test = 0) {
map<string, int> counter;
map<string, int> signals;
string line;
ifstream file(file_name);
if (file.fail()) {
cout << "Can't find the file " << file_name << "\n";
return false;
}
string temp_folder_name = (is_test? "lg/tests" : "lg");
string parsed_data_file_path = PARSED_DATA_PATH + "/" + temp_folder_name + "/"
+ to_string(class_id)
+ "."
+ to_string(point_id);
ofstream parsed_file;
parsed_file.open(parsed_data_file_path);
if(!parsed_file.is_open()) {
// cout << "Can't open parsed_data_file " << parsed_data_file_path << "\n";
return false;
}
while (getline(file, line)) {
string mac_address = "";
string signal = "";
bool is_mac = true;
for (size_t i = 0 ; i < line.length() ; ++i) {
if(line[i] == ' ') continue;
if (line[i] == '-') {
is_mac = false;
continue;
}
if (is_mac) {
mac_address += line[i];
} else {
signal += line[i];
}
}
//cout << "mac_address = " << mac_address << "\n";
//cout << "signal = " << signal << "\n";
counter[mac_address] += 1;
signals[mac_address] += stoi(signal);
}
int real_cnt = 0;
for (pair<string, int> cnt : counter) {
if (cnt.second <= 1) // biased
continue;
int averaged = signals[cnt.first] / cnt.second;
parsed_file << cnt.first << "," << averaged << "\n";
real_cnt ++;
}
parsed_file.close();
return true;
}
void calculateAveragesForClass(string file_class_name, int class_id, int num_files = 10) {
// train data
/*
vector<int> test_points = {rand() % 10 + 1};
int rand_num;
while (test_points.size() < 3){
while (test_points.back() == (rand_num = (rand() % 10 + 1))); // 30% of data
test_points.push_back(rand_num);
}
for (int point_id : test_points) { // tests
string file_name = file_class_name + "." + to_string(point_id) + ".txt";
calculateAveragesForPoint(file_name, class_id, point_id, 1);
}
*/
for (int point_id = 1 ; point_id <= num_files ; ++point_id) {
// if (find(test_points.begin(), test_points.end(), point_id) != test_points.end())
//continue;
string file_name = file_class_name + "." + to_string(point_id) + ".txt";
calculateAveragesForPoint(file_name, class_id, point_id);
}
}
void startAveraging(int num_classes, string data_folder, string file_name_preffix) {
for (int class_id = 1 ; class_id <= num_classes; ++class_id) {
string file_class_name = data_folder + "/" + file_name_preffix + "." + to_string(class_id);
calculateAveragesForClass(file_class_name, class_id);
}
}
int main(int argc, char const *argv[]) {
const string preffix = "lg";
const int num_classes = 16;
srand(time(NULL));
startAveraging(num_classes, DATA_PATH, preffix);
return 0;
}