-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgen_cube.cc
More file actions
129 lines (114 loc) · 4.36 KB
/
gen_cube.cc
File metadata and controls
129 lines (114 loc) · 4.36 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
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2015 Maxim Zhurovich <zhurovich@gmail.com>
Copyright (c) 2015 Dmitry "Dima" Korolev <dmitry.korolev@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#include "../Current/Bricks/cerealize/cerealize.h"
#include "../Current/Bricks/dflags/dflags.h"
#include "../Current/Bricks/file/file.h"
#include "cubes.h"
DEFINE_string(input, "data/cube_input.json", "");
DEFINE_string(output, "data/cube.tsv", "");
using bricks::FileSystem;
struct Cell {
// Coordinates are [Dimension name, Bin name].
typedef std::pair<std::string, std::string> Coordinate;
std::vector<Coordinate> coordinates;
Cell() = delete;
Cell(const Space& space) {
for (const auto& dim : space.dimensions) {
coordinates.emplace_back(
dim.name,
std::string(dim.name) == DEVICE_DIMENSION_NAME ? DEVICE_UNSPECIFIED_BIN_NAME : NONE_BIN_NAME);
}
}
bool SetCoordinate(const std::string& dimension, const std::string& value) {
for (auto& it : coordinates) {
if (it.first == dimension) {
it.second = value;
return true;
}
}
return false;
}
};
int main(int argc, char** argv) {
ParseDFlags(&argc, &argv);
fprintf(stderr, "Reading '%s' ...\n", FLAGS_input.c_str());
fflush(stderr);
auto input = ParseJSON<CubeGeneratorInput>(FileSystem::ReadFileAsString(FLAGS_input));
fprintf(stderr,
"Done reading file. Got %lu dimensions with %lu sessions.\n",
input.space.dimensions.size(),
input.sessions.size());
const auto& dimensions = input.space.dimensions;
const auto& sessions = input.sessions;
std::ofstream fo(FLAGS_output);
std::string header;
for (const Dimension& dim : dimensions) {
header += "FEATURE|" + dim.name;
for (const Bin& bin : dim.bins) {
header += "|" + bin.name;
}
header += "\t";
}
header += "TOTAL|" + std::to_string(sessions.size()) + "\n";
fo << header;
for (const auto& session : input.sessions) {
Cell cell(input.space);
for (const auto& feature_counter : session.feature_count) {
const std::string feature = feature_counter.first;
std::string dimension_name;
std::string bin_name;
if (feature != TIME_DIMENSION_NAME) {
const auto dim_bin = input.space.SplitFeatureIntoDimensionAndBinNames(feature);
if (dim_bin.first.empty()) {
continue;
}
dimension_name = dim_bin.first;
if (!dim_bin.second.empty()) {
bin_name = dim_bin.second;
}
} else { // Time dimension.
dimension_name = feature;
}
if (bin_name.empty()) {
Dimension* dim = input.space.DimensionByName(dimension_name);
assert(dim);
bin_name = dim->BinNameByValue(feature_counter.second);
if (bin_name.empty()) {
std::cerr << "FATAL ERROR: No bin assignment for " << feature_counter.second << " within "
<< dim->name << std::endl;
std::cerr << JSON(*dim) << std::endl;
std::exit(-1);
}
}
cell.SetCoordinate(dimension_name, bin_name);
}
bool first = true;
for (const auto& cit : cell.coordinates) {
if (first) {
first = false;
} else {
fo << "\t";
}
fo << cit.second;
}
fo << "\t1\n";
}
}