libqe 0.2.6-dev.9001
QE C++ library
Loading...
Searching...
No Matches
door.hpp
Go to the documentation of this file.
1
8#ifndef LIBQE_DOOR_HPP
9#define LIBQE_DOOR_HPP
10
11#include <armadillo>
12#include <vector>
13#include <string>
14#include <cmath>
15#include <algorithm>
16
17namespace qe {
18
35inline arma::mat lookback_block(
36 const arma::mat& block,
37 int lookback_size = 20,
38 bool aggregate_mean = false,
39 bool weighting_linear = false,
40 const std::vector<int>& segment_ids = {}
41) {
42 int n_rows = static_cast<int>(block.n_rows);
43 int n_cols = static_cast<int>(block.n_cols);
44 arma::mat out(n_rows, n_cols, arma::fill::zeros);
45
46 if (n_rows == 0 || n_cols == 0) return out;
47 int k_max = std::max(1, lookback_size);
48 bool has_segments = (!segment_ids.empty() && static_cast<int>(segment_ids.size()) >= n_rows);
49
50 for (int i = 0; i < n_rows; ++i) {
51 int start_idx = std::max(0, i - k_max + 1);
52
53 if (has_segments) {
54 int cur_seg = segment_ids[i];
55 for (int s = i; s >= start_idx; --s) {
56 if (segment_ids[s] != cur_seg) {
57 start_idx = s + 1;
58 break;
59 }
60 }
61 }
62
63 int win_len = i - start_idx + 1;
64 if (win_len <= 0) continue;
65
66 arma::rowvec acc(n_cols, arma::fill::zeros);
67 arma::rowvec denom(n_cols, arma::fill::zeros);
68
69 for (int j = start_idx; j <= i; ++j) {
70 double w = 1.0;
71 if (weighting_linear) {
72 // Linear weight: 1 for earliest in window, win_len for current row i
73 w = static_cast<double>(j - start_idx + 1);
74 }
75 for (int col = 0; col < n_cols; ++col) {
76 double value = block(j, col);
77 if (!std::isnan(value)) {
78 acc[col] += w * value;
79 denom[col] += w;
80 }
81 }
82 }
83
84 if (aggregate_mean) {
85 for (int col = 0; col < n_cols; ++col) {
86 acc[col] = (denom[col] > 0.0) ? (acc[col] / denom[col]) : arma::datum::nan;
87 }
88 }
89
90 out.row(i) = acc;
91 }
92
93 return out;
94}
95
107inline arma::mat ema_block(
108 const arma::mat& block,
109 double alpha = 0.1,
110 const std::vector<int>& segment_ids = {}
111) {
112 int n_rows = static_cast<int>(block.n_rows);
113 int n_cols = static_cast<int>(block.n_cols);
114 arma::mat out(n_rows, n_cols, arma::fill::zeros);
115
116 if (n_rows == 0 || n_cols == 0) return out;
117 double a = std::max(1e-6, std::min(1.0, alpha));
118 bool has_segments = (!segment_ids.empty() && static_cast<int>(segment_ids.size()) >= n_rows);
119
120 out.row(0) = block.row(0);
121
122 for (int i = 1; i < n_rows; ++i) {
123 bool reset = has_segments && (segment_ids[i] != segment_ids[i - 1]);
124 if (reset) {
125 out.row(i) = block.row(i);
126 } else {
127 for (int col = 0; col < n_cols; ++col) {
128 double cur = block(i, col);
129 if (std::isnan(cur)) {
130 cur = out(i - 1, col);
131 }
132 out(i, col) = a * cur + (1.0 - a) * out(i - 1, col);
133 }
134 }
135 }
136
137 return out;
138}
139
151inline arma::mat apply_door_lookback(
152 const arma::mat& conn_counts,
153 const std::vector<std::vector<int>>& unit_row_indices,
154 const std::vector<int>& lookback_sizes,
155 bool aggregate_mean = false,
156 bool weighting_linear = false,
157 const std::vector<int>& segment_ids = {}
158) {
159 arma::mat out = conn_counts;
160 size_t n_units = unit_row_indices.size();
161 bool has_segments = (!segment_ids.empty() && segment_ids.size() >= conn_counts.n_rows);
162
163 for (size_t u = 0; u < n_units; ++u) {
164 const auto& rows = unit_row_indices[u];
165 if (rows.empty()) continue;
166
167 int k = (u < lookback_sizes.size()) ? lookback_sizes[u] : 20;
168 int n_u = static_cast<int>(rows.size());
169
170 arma::mat block(n_u, conn_counts.n_cols);
171 std::vector<int> block_segs(n_u, 0);
172
173 for (int i = 0; i < n_u; ++i) {
174 int r = rows[i];
175 block.row(i) = conn_counts.row(r);
176 if (has_segments) block_segs[i] = segment_ids[r];
177 }
178
179 arma::mat smoothed = lookback_block(block, k, aggregate_mean, weighting_linear,
180 has_segments ? block_segs : std::vector<int>{});
181
182 for (int i = 0; i < n_u; ++i) {
183 out.row(rows[i]) = smoothed.row(i);
184 }
185 }
186
187 return out;
188}
189
192inline arma::mat apply_door_ema(
193 const arma::mat& conn_counts,
194 const std::vector<std::vector<int>>& unit_row_indices,
195 double alpha = 0.1,
196 const std::vector<int>& segment_ids = {}
197) {
198 arma::mat out = conn_counts;
199 size_t n_units = unit_row_indices.size();
200 bool has_segments = (!segment_ids.empty() && segment_ids.size() >= conn_counts.n_rows);
201
202 for (size_t u = 0; u < n_units; ++u) {
203 const auto& rows = unit_row_indices[u];
204 if (rows.empty()) continue;
205
206 int n_u = static_cast<int>(rows.size());
207 arma::mat block(n_u, conn_counts.n_cols);
208 std::vector<int> block_segs(n_u, 0);
209
210 for (int i = 0; i < n_u; ++i) {
211 int r = rows[i];
212 block.row(i) = conn_counts.row(r);
213 if (has_segments) block_segs[i] = segment_ids[r];
214 }
215
216 arma::mat smoothed = ema_block(block, alpha, has_segments ? block_segs : std::vector<int>{});
217
218 for (int i = 0; i < n_u; ++i) {
219 out.row(rows[i]) = smoothed.row(i);
220 }
221 }
222
223 return out;
224}
225
226} // namespace qe
227
228#endif // LIBQE_DOOR_HPP
Definition adjacency.hpp:16
arma::mat apply_door_lookback(const arma::mat &conn_counts, const std::vector< std::vector< int > > &unit_row_indices, const std::vector< int > &lookback_sizes, bool aggregate_mean=false, bool weighting_linear=false, const std::vector< int > &segment_ids={})
Apply lookback door pooling to an entire connection matrix across multiple units.
Definition door.hpp:151
arma::mat apply_door_ema(const arma::mat &conn_counts, const std::vector< std::vector< int > > &unit_row_indices, double alpha=0.1, const std::vector< int > &segment_ids={})
Apply EMA door smoothing to an entire connection matrix across multiple units.
Definition door.hpp:192
arma::mat lookback_block(const arma::mat &block, int lookback_size=20, bool aggregate_mean=false, bool weighting_linear=false, const std::vector< int > &segment_ids={})
Apply a sliding lookback window pool over a single unit's connection matrix.
Definition door.hpp:35
arma::mat ema_block(const arma::mat &block, double alpha=0.1, const std::vector< int > &segment_ids={})
Apply Exponential Moving Average (EMA) smoothing over a single unit's connection matrix.
Definition door.hpp:107