libqe 0.2.6-dev.9001
QE C++ library
Loading...
Searching...
No Matches
nesting.hpp
Go to the documentation of this file.
1
19#ifndef LIBQE_NESTING_HPP
20#define LIBQE_NESTING_HPP
21
22#include <armadillo>
23#include <cmath>
24#include <stdexcept>
25#include <vector>
26
27namespace qe {
28
31 arma::umat cooccurrence;
32 arma::umat pairs;
33 arma::vec containment;
34 arma::umat duplicates;
35};
36
64inline NestingCandidates nesting_candidates(const arma::mat& codes,
65 double threshold = 0.95,
66 int min_rows = 5) {
67 if (!(threshold > 0.0 && threshold <= 1.0))
68 throw std::invalid_argument("nesting_candidates: threshold must be in (0, 1]");
69 if (min_rows < 0)
70 throw std::invalid_argument("nesting_candidates: min_rows must be >= 0");
71
72 const arma::uword n_rows = codes.n_rows;
73 const arma::uword n_codes = codes.n_cols;
74
76 out.cooccurrence.zeros(n_codes, n_codes);
77
78 // One pass over the lines: for each, the codes present, then every pair.
79 std::vector<arma::uword> hits;
80 hits.reserve(n_codes);
81 for (arma::uword r = 0; r < n_rows; ++r) {
82 hits.clear();
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);
86 }
87 for (std::size_t a = 0; a < hits.size(); ++a) {
88 const arma::uword i = hits[a];
89 out.cooccurrence(i, i) += 1;
90 for (std::size_t b = a + 1; b < hits.size(); ++b) {
91 const arma::uword j = hits[b];
92 out.cooccurrence(i, j) += 1;
93 out.cooccurrence(j, i) += 1;
94 }
95 }
96 }
97 const arma::umat& co = out.cooccurrence;
98 auto count = [&co](arma::uword i) { return co(i, i); };
99
100 // Duplicates: identical presence (mutual containment), once per unordered pair.
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)) {
108 dup_i.push_back(i);
109 dup_j.push_back(j);
110 is_dup[i * n_codes + j] = is_dup[j * n_codes + i] = 1;
111 }
112 }
113 }
114
115 // Candidate pairs by containment.
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) {
127 sub.push_back(s);
128 parent.push_back(p);
129 contain.push_back(ratio);
130 }
131 }
132 }
133
134 out.pairs.set_size(sub.size(), 2);
135 out.containment.set_size(sub.size());
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];
139 out.containment(k) = contain[k];
140 }
141 out.duplicates.set_size(dup_i.size(), 2);
142 for (std::size_t k = 0; k < dup_i.size(); ++k) {
143 out.duplicates(k, 0) = dup_i[k];
144 out.duplicates(k, 1) = dup_j[k];
145 }
146 return out;
147}
148
149} // namespace qe
150
151#endif // LIBQE_NESTING_HPP
Definition adjacency.hpp:16
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
Result of nesting_candidates(). All indices are 0-based code columns.
Definition nesting.hpp:30
arma::umat duplicates
d × 2 rows of (i, j), i < j, identical presence on at least one line
Definition nesting.hpp:34
arma::umat pairs
k × 2 rows of (sub, parent) whose containment reaches the threshold
Definition nesting.hpp:32
arma::umat cooccurrence
n_codes × n_codes; (i, i) = lines with code i, (i, j) = lines with both
Definition nesting.hpp:31
arma::vec containment
k; cooccurrence(sub, parent) / cooccurrence(sub, sub) per pair
Definition nesting.hpp:33