65 double threshold = 0.95,
67 if (!(threshold > 0.0 && threshold <= 1.0))
68 throw std::invalid_argument(
"nesting_candidates: threshold must be in (0, 1]");
70 throw std::invalid_argument(
"nesting_candidates: min_rows must be >= 0");
72 const arma::uword n_rows = codes.n_rows;
73 const arma::uword n_codes = codes.n_cols;
79 std::vector<arma::uword> hits;
80 hits.reserve(n_codes);
81 for (arma::uword r = 0; r < n_rows; ++r) {
83 for (arma::uword i = 0; i < n_codes; ++i) {
84 const double v = codes(r, i);
85 if (std::isfinite(v) && v > 0.0) hits.push_back(i);
87 for (std::size_t a = 0; a < hits.size(); ++a) {
88 const arma::uword i = hits[a];
90 for (std::size_t b = a + 1; b < hits.size(); ++b) {
91 const arma::uword j = hits[b];
98 auto count = [&co](arma::uword i) {
return co(i, i); };
101 std::vector<arma::uword> dup_i, dup_j;
102 std::vector<char> is_dup(n_codes * n_codes, 0);
103 for (arma::uword i = 0; i < n_codes; ++i) {
104 if (count(i) == 0)
continue;
105 for (arma::uword j = i + 1; j < n_codes; ++j) {
106 const arma::uword both = co(i, j);
107 if (both == count(i) && both == count(j)) {
110 is_dup[i * n_codes + j] = is_dup[j * n_codes + i] = 1;
116 std::vector<arma::uword> sub, parent;
117 std::vector<double> contain;
118 const arma::uword min_lines =
static_cast<arma::uword
>(min_rows);
119 for (arma::uword s = 0; s < n_codes; ++s) {
120 const arma::uword lines = count(s);
121 if (lines == 0 || lines < min_lines)
continue;
122 for (arma::uword p = 0; p < n_codes; ++p) {
123 if (p == s || is_dup[s * n_codes + p])
continue;
124 const arma::uword both = co(s, p);
125 const double ratio =
static_cast<double>(both) /
static_cast<double>(lines);
126 if (ratio >= threshold && count(p) > both) {
129 contain.push_back(ratio);
134 out.
pairs.set_size(sub.size(), 2);
136 for (std::size_t k = 0; k < sub.size(); ++k) {
137 out.
pairs(k, 0) = sub[k];
138 out.
pairs(k, 1) = parent[k];
142 for (std::size_t k = 0; k < dup_i.size(); ++k) {
NestingCandidates nesting_candidates(const arma::mat &codes, double threshold=0.95, int min_rows=5)
Pairs of codes where one is (nearly) always coded with another.
Definition nesting.hpp:64
arma::umat duplicates
d × 2 rows of (i, j), i < j, identical presence on at least one line
Definition nesting.hpp:34
arma::umat cooccurrence
n_codes × n_codes; (i, i) = lines with code i, (i, j) = lines with both
Definition nesting.hpp:31