-
Notifications
You must be signed in to change notification settings - Fork 33
/
prepare-counts.cpp
148 lines (107 loc) · 5.58 KB
/
prepare-counts.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
#include "util/io.h"
#include "util/data.h"
std::vector<std::pair<std::string, std::vector<std::string>>> filesets {
{ "cv1", { "cache/clicks_cv1_train.csv.gz", "cache/clicks_cv1_test.csv.gz" } },
{ "cv2", { "cache/clicks_cv2_train.csv.gz", "cache/clicks_cv2_test.csv.gz" } },
{ "full", { "../input/clicks_train.csv.gz", "../input/clicks_test.csv.gz" } }
};
struct cnt {
uint32_t train_count;
uint32_t test_count;
uint32_t & operator[](uint i) {
switch (i) {
case 0: return train_count;
case 1: return test_count;
default: throw std::logic_error("Invalid field index");
}
}
};
template <typename T>
void write_counts(const std::unordered_map<T, cnt> & map, const std::string & file_name) {
using namespace std;
cout << "Writing " << file_name << "... " << endl;
ofstream outfile(file_name, std::ios_base::out | std::ios_base::binary);
streamsize buffer_size = 1024*1024;
boost::iostreams::filtering_streambuf<boost::iostreams::output> buf;
buf.push(boost::iostreams::gzip_compressor(), buffer_size, buffer_size);
buf.push(outfile, buffer_size, buffer_size);
std::ostream out(&buf);
out << "id,train_count,test_count" << endl;
for (auto it = map.begin(); it != map.end(); ++ it)
out << it->first << "," << it->second.train_count << "," << it->second.test_count << endl;
}
int main() {
using namespace std;
cout << "Loading reference data..." << endl;
auto data = load_reference_data();
for (auto it = filesets.begin(); it != filesets.end(); ++ it) {
unordered_map<int, cnt> ad_counts;
unordered_map<int, cnt> ad_campaign_counts;
unordered_map<int, cnt> ad_advertiser_counts;
unordered_map<int, cnt> ad_doc_counts;
unordered_map<int, cnt> ad_doc_source_counts;
unordered_map<int, cnt> ad_doc_publisher_counts;
unordered_map<int, cnt> ev_doc_counts;
unordered_map<int, cnt> ev_doc_source_counts;
unordered_map<int, cnt> ev_doc_publisher_counts;
unordered_map<int, cnt> uid_counts;
unordered_map<std::string, cnt> ev_country_counts;
unordered_map<std::string, cnt> ev_state_counts;
unordered_map<std::string, cnt> ev_region_counts;
cout << "Processing " << it->first << "... " << endl;
for (uint fi = 0; fi < it->second.size(); ++ fi) {
auto file_name = it->second[fi];
clock_t begin = clock();
cout << " Loading " << file_name << "... ";
cout.flush();
compressed_csv_file file(file_name);
for (int ri = 0;; ++ri) {
auto row = file.getrow();
if (row.empty())
break;
if (ri > 0 && ri % 5000000 == 0) {
cout << (ri / 1000000) << "M... ";
cout.flush();
}
// Extract fields
int ev_id = stoi(row[0]);
int ad_id = stoi(row[1]);
auto ad = data.ads[ad_id];
auto ev = data.events[ev_id];
auto ad_doc = data.documents.at(ad.document_id);
auto ev_doc = data.documents.at(ev.document_id);
// Increment counters
++ ad_counts[ad_id][fi];
++ ad_campaign_counts[ad.campaign_id][fi];
++ ad_advertiser_counts[ad.advertiser_id][fi];
++ ad_doc_counts[ad.document_id][fi];
++ ad_doc_source_counts[ad_doc.source_id][fi];
++ ad_doc_publisher_counts[ad_doc.publisher_id][fi];
++ ev_doc_counts[ev.document_id][fi];
++ ev_doc_source_counts[ev_doc.source_id][fi];
++ ev_doc_publisher_counts[ev_doc.publisher_id][fi];
++ ev_country_counts[ev.country][fi];
++ ev_state_counts[ev.state][fi];
++ ev_region_counts[ev.region][fi];
++ uid_counts[ev.uid][fi];
}
clock_t end = clock();
double elapsed = double(end - begin) / CLOCKS_PER_SEC;
cout << "done in " << elapsed << " seconds" << endl;
}
write_counts(ad_counts, string("cache/counts/ads_") + it->first + string(".csv.gz"));
write_counts(ad_campaign_counts, string("cache/counts/ad_campaigns_") + it->first + string(".csv.gz"));
write_counts(ad_advertiser_counts, string("cache/counts/ad_advertisers_") + it->first + string(".csv.gz"));
write_counts(ad_doc_counts, string("cache/counts/ad_docs_") + it->first + string(".csv.gz"));
write_counts(ad_doc_source_counts, string("cache/counts/ad_doc_sources_") + it->first + string(".csv.gz"));
write_counts(ad_doc_publisher_counts, string("cache/counts/ad_doc_publishers_") + it->first + string(".csv.gz"));
write_counts(ev_doc_counts, string("cache/counts/ev_docs_") + it->first + string(".csv.gz"));
write_counts(ev_doc_source_counts, string("cache/counts/ev_doc_sources_") + it->first + string(".csv.gz"));
write_counts(ev_doc_publisher_counts, string("cache/counts/ev_doc_publishers_") + it->first + string(".csv.gz"));
write_counts(ev_country_counts, string("cache/counts/ev_countries_") + it->first + string(".csv.gz"));
write_counts(ev_state_counts, string("cache/counts/ev_states_") + it->first + string(".csv.gz"));
write_counts(ev_region_counts, string("cache/counts/ev_regions_") + it->first + string(".csv.gz"));
write_counts(uid_counts, string("cache/counts/uids_") + it->first + string(".csv.gz"));
}
cout << "Done." << endl;
}