-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_features.cpp
79 lines (53 loc) · 1.97 KB
/
show_features.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
// show sparse matrix
#include <iostream>
#include <fstream>
#include "feature_map.hpp"
#include "sparse_matrix.hpp"
#include "sparse_matrix_io.hpp"
using namespace std;
/////////////////////////////////////////////////////////////////
// output sparse matrix of pairwise feature values as a tsv list
void show_feature_matrix(const sparse_matrix& F,
const feature_map& features,
const sparse_matrix& L,
std::ostream& outs=cout) {
// iterate non-zero values...
for (int j = 0; j < F.outerSize(); ++j) {
for (sparse_matrix::InnerIterator it(F, j); it; ++it) {
if (L.size()) outs << L.coeff(it.index(), j) << "\t";
outs << it.value() << "\t" << features.right(j) << "\t" << features.right(it.index()) << "\n";
}
}
}
int main(int argc, char *argv[]) {
sparse_matrix B, L;
feature_map features;
// filename on command line
if (argc > 1) {
string filename(argv[1]);
std::ifstream ins;
ins.open(filename);
if (ins.good()) {
cerr << "reading feature index from: " << filename << endl;
// do we have a loglmatrix for annotation?
if (argc > 2) {
string loglmat(argv[2]);
cerr << "reading logl matrix from: " << loglmat << endl;
std::ifstream lmi;
lmi.open(loglmat);
if (lmi.good() && deserialize_matrix(L, lmi)) {
lmi.close();
} else {
cerr << "cannot de-serialize matrix from: " << loglmat << endl;
}
}
cerr << "reading matrix data from stdin..." << endl;
if (features.deserialize(ins) && deserialize_matrix(B)) {
ins.close();
show_feature_matrix(B, features, L);
return 0;
} else cerr << "cannot deserialize!" << endl;
} else cerr << "cannot read file: " << filename << "!" << endl;
} else cerr << "required argument: feature_file" << endl;
return 1;
}