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 = {}
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);
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);
50 for (
int i = 0; i < n_rows; ++i) {
51 int start_idx = std::max(0, i - k_max + 1);
54 int cur_seg = segment_ids[i];
55 for (
int s = i; s >= start_idx; --s) {
56 if (segment_ids[s] != cur_seg) {
63 int win_len = i - start_idx + 1;
64 if (win_len <= 0)
continue;
66 arma::rowvec acc(n_cols, arma::fill::zeros);
67 arma::rowvec denom(n_cols, arma::fill::zeros);
69 for (
int j = start_idx; j <= i; ++j) {
71 if (weighting_linear) {
73 w =
static_cast<double>(j - start_idx + 1);
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;
85 for (
int col = 0; col < n_cols; ++col) {
86 acc[col] = (denom[col] > 0.0) ? (acc[col] / denom[col]) : arma::datum::nan;
108 const arma::mat& block,
110 const std::vector<int>& segment_ids = {}
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);
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);
120 out.row(0) = block.row(0);
122 for (
int i = 1; i < n_rows; ++i) {
123 bool reset = has_segments && (segment_ids[i] != segment_ids[i - 1]);
125 out.row(i) = block.row(i);
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);
132 out(i, col) = a * cur + (1.0 - a) * out(i - 1, col);
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 = {}
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);
163 for (
size_t u = 0; u < n_units; ++u) {
164 const auto& rows = unit_row_indices[u];
165 if (rows.empty())
continue;
167 int k = (u < lookback_sizes.size()) ? lookback_sizes[u] : 20;
168 int n_u =
static_cast<int>(rows.size());
170 arma::mat block(n_u, conn_counts.n_cols);
171 std::vector<int> block_segs(n_u, 0);
173 for (
int i = 0; i < n_u; ++i) {
175 block.row(i) = conn_counts.row(r);
176 if (has_segments) block_segs[i] = segment_ids[r];
179 arma::mat smoothed =
lookback_block(block, k, aggregate_mean, weighting_linear,
180 has_segments ? block_segs : std::vector<int>{});
182 for (
int i = 0; i < n_u; ++i) {
183 out.row(rows[i]) = smoothed.row(i);
193 const arma::mat& conn_counts,
194 const std::vector<std::vector<int>>& unit_row_indices,
196 const std::vector<int>& segment_ids = {}
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);
202 for (
size_t u = 0; u < n_units; ++u) {
203 const auto& rows = unit_row_indices[u];
204 if (rows.empty())
continue;
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);
210 for (
int i = 0; i < n_u; ++i) {
212 block.row(i) = conn_counts.row(r);
213 if (has_segments) block_segs[i] = segment_ids[r];
216 arma::mat smoothed =
ema_block(block, alpha, has_segments ? block_segs : std::vector<int>{});
218 for (
int i = 0; i < n_u; ++i) {
219 out.row(rows[i]) = smoothed.row(i);
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