-
Notifications
You must be signed in to change notification settings - Fork 536
/
format_converter.cc
328 lines (267 loc) · 8.91 KB
/
format_converter.cc
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
/*
* Transform a TSV format factor graph file and output corresponding binary format used in DeepDive
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <stdint.h>
#include <vector>
using namespace std;
// 64-bit endian conversion, note input must be unsigned long
// for double, cast its underlying bytes to unsigned long, see examples in load_var
# define bswap_64(x) \
((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
| (((x) & 0x000000ff00000000ull) >> 8) \
| (((x) & 0x00000000ff000000ull) << 8) \
| (((x) & 0x0000000000ff0000ull) << 24) \
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56))
// 16-bit endian conversion
#define bswap_16(x) \
((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
// read variables and convert to binary format
void load_var(std::string input_filename, std::string output_filename) {
std::ifstream fin(input_filename.c_str());
std::ofstream fout(output_filename.c_str(), std::ios::binary | std::ios::out);
long count = 0;
long vid;
int is_evidence;
double initial_value;
short type;
long edge_count = -1;
long cardinality;
edge_count = bswap_64(edge_count);
while (fin >> vid >> is_evidence >> initial_value >> type >> cardinality) {
// endianess
vid = bswap_64(vid);
uint64_t initval = bswap_64(*(uint64_t *)&initial_value);
type = bswap_16(type);
cardinality = bswap_64(cardinality);
fout.write((char*)&vid, 8);
fout.write((char*)&is_evidence, 1);
fout.write((char*)&initval, 8);
fout.write((char*)&type, 2);
fout.write((char *)&edge_count, 8);
fout.write((char*)&cardinality, 8);
++count;
}
std::cout << count <<std::endl;
fin.close();
fout.close();
}
// convert weights
void load_weight(std::string input_filename, std::string output_filename) {
std::ifstream fin(input_filename.c_str());
std::ofstream fout(output_filename.c_str(), std::ios::binary | std::ios::out);
long count = 0;
long wid;
int isfixed;
double initial_value;
while(fin >> wid >> isfixed >> initial_value){
wid = bswap_64(wid);
uint64_t initval = bswap_64(*(uint64_t *)&initial_value);
fout.write((char*)&wid, 8);
fout.write((char*)&isfixed, 1);
fout.write((char*)&initval, 8);
++count;
}
std::cout << count <<std::endl;
fin.close();
fout.close();
}
// load factors
// fid, wid, vids
void load_factor_with_fid(std::string input_filename, std::string output_factors_filename, std::string output_edges_filename, short funcid, long nvar, char** positives) {
std::ifstream fin(input_filename.c_str());
std::ofstream fout(output_factors_filename.c_str(), std::ios::binary | std::ios::out);
std::ofstream fedgeout(output_edges_filename.c_str(), std::ios::binary | std::ios::out);
long factorid = 0;
long weightid = 0;
long variableid = 0;
long nedge = 0;
long nvars_big = bswap_64(nvar);
long predicate = funcid == 5 ? -1 : 1;
vector<int> positives_vec;
funcid = bswap_16(funcid);
for (int i = 0; i < nvar; i++) {
positives_vec.push_back(atoi(positives[i]));
}
predicate = bswap_64(predicate);
const char field_delim = '\t'; // tsv file delimiter
const char array_delim = ','; // array delimiter
string line;
while (getline(fin, line)) {
string field;
istringstream ss(line);
// factor id
getline(ss, field, field_delim);
factorid = atol(field.c_str());
factorid = bswap_64(factorid);
// weightid
getline(ss, field, field_delim);
weightid = atol(field.c_str());
weightid = bswap_64(weightid);
fout.write((char *)&factorid, 8);
fout.write((char *)&weightid, 8);
fout.write((char *)&funcid, 2);
// fout.write((char *)&nvars_big, 8);
uint64_t position = 0;
uint64_t position_big;
long n_vars = 0;
for (long i = 0; i < nvar; i++) {
getline(ss, field, field_delim);
// array type
if (field.at(0) == '{') {
string subfield;
istringstream ss1(field);
ss1.get(); // get '{'
bool ended = false;
while (getline(ss1, subfield, array_delim)) {
if (subfield.at(subfield.length() - 1) == '}') {
ended = true;
subfield = subfield.substr(0, subfield.length() - 1);
}
variableid = atol(subfield.c_str());
variableid = bswap_64(variableid);
position_big = bswap_64(position);
fedgeout.write((char *)&variableid, 8);
fedgeout.write((char *)&factorid, 8);
fedgeout.write((char *)&position_big, 8);
fedgeout.write((char *)&positives_vec[i], 1);
fedgeout.write((char *)&predicate, 8);
nedge++;
position++;
n_vars++;
if (ended) break;
}
} else {
variableid = atol(field.c_str());
variableid = bswap_64(variableid);
position_big = bswap_64(position);
fedgeout.write((char *)&variableid, 8);
fedgeout.write((char *)&factorid, 8);
fedgeout.write((char *)&position_big, 8);
fedgeout.write((char *)&positives_vec[i], 1);
fedgeout.write((char *)&predicate, 8);
nedge++;
position++;
n_vars++;
}
}
n_vars = bswap_64(n_vars);
fout.write((char *)&n_vars, 8);
}
std::cout << nedge << std::endl;
fin.close();
fout.close();
fedgeout.close();
}
// load factors
// wid, vids
void load_factor(std::string input_filename, std::string output_filename, short funcid, long nvar, char** positives) {
std::ifstream fin(input_filename.c_str());
std::ofstream fout(output_filename.c_str(), std::ios::binary | std::ios::out);
long weightid = 0;
long variableid = 0;
long nedge = 0;
long predicate = funcid == 5 ? -1 : 1;
vector<int> positives_vec;
vector<long> variables;
funcid = bswap_16(funcid);
for (int i = 0; i < nvar; i++) {
positives_vec.push_back(atoi(positives[i]));
}
predicate = bswap_64(predicate);
const char field_delim = '\t'; // tsv file delimiter
const char array_delim = ','; // array delimiter
string line;
while (getline(fin, line)) {
string field;
istringstream ss(line);
variables.clear();
// weightid
getline(ss, field, field_delim);
weightid = atol(field.c_str());
weightid = bswap_64(weightid);
fout.write((char *)&weightid, 8);
fout.write((char *)&funcid, 2);
uint64_t position = 0;
uint64_t position_big;
long n_vars = 0;
for (long i = 0; i < nvar; i++) {
getline(ss, field, field_delim);
// array type
if (field.at(0) == '{') {
string subfield;
istringstream ss1(field);
ss1.get(); // get '{'
bool ended = false;
while (getline(ss1, subfield, array_delim)) {
if (subfield.at(subfield.length() - 1) == '}') {
ended = true;
subfield = subfield.substr(0, subfield.length() - 1);
}
variableid = atol(subfield.c_str());
variableid = bswap_64(variableid);
variables.push_back(variableid);
nedge++;
n_vars++;
if (ended) break;
}
} else {
variableid = atol(field.c_str());
variableid = bswap_64(variableid);
variables.push_back(variableid);
nedge++;
n_vars++;
}
}
n_vars = bswap_64(n_vars);
fout.write((char *)&predicate, 8);
fout.write((char *)&n_vars, 8);
for (long i = 0; i < variables.size(); i++) {
fout.write((char *)&variables[i], 8);
fout.write((char *)&positives_vec[i], 1);
}
}
std::cout << nedge << std::endl;
fin.close();
fout.close();
}
void load_active(std::string input_filename, std::string output_filename) {
std::ifstream fin(input_filename.c_str());
std::ofstream fout(output_filename.c_str(), std::ios::binary);
long count = 0;
long id;
while (fin >> id) {
id = bswap_64(id);
fout.write((char*)&id, 8);
++count;
}
std::cout << count << std::endl;
fin.close();
fout.close();
}
int main(int argc, char** argv){
std::string app(argv[1]);
if(app.compare("variable")==0){
load_var(argv[2], argv[3]);
} else if(app.compare("weight")==0){
load_weight(argv[2], argv[3]);
} else if(app.compare("factor")==0){
std::string inc(argv[6]);
if (inc.compare("inc") == 0 || inc.compare("mat") == 0)
load_factor_with_fid(argv[2], argv[3], argv[7], atoi(argv[4]), atoi(argv[5]), &argv[8]);
else
load_factor(argv[2], argv[3], atoi(argv[4]), atoi(argv[5]), &argv[7]);
} else if (app.compare("active") == 0) {
load_active(argv[2], argv[3]);
} else {
std::cerr << "Unsupported type" << std::endl;
exit(1);
}
return 0;
}