Skip to content

Commit 48601c3

Browse files
bbuchsbaumclaude
andcommitted
Fix vignette data handling and strengthen connectivity validation
benchmark_datasets: use bind_rows to build comparison data frame correctly. functional_connectivity: rebuild dataset after injecting network signal, use name-based column lookup for seed stats, and add stricter recovery assertions (significance rates, top-rank enrichment). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f354079 commit 48601c3

2 files changed

Lines changed: 44 additions & 24 deletions

File tree

vignettes/benchmark_datasets.Rmd

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ cat("Wrong HRF - RMSE:", round(performance_wrong$overall_metrics$rmse, 3), "\n")
189189
true_betas <- data$true_betas_condition
190190
191191
# Create comparison plots
192-
comparison_data <- data.frame(
193-
True = as.vector(true_betas),
194-
Estimated_Correct = as.vector(betas_correct[-1, ]),
195-
Estimated_Wrong = as.vector(betas_wrong[-1, ]),
196-
Condition = rep(paste("Condition", 1:3), each = ncol(true_betas))
197-
)
192+
comparison_data <- dplyr::bind_rows(lapply(seq_len(nrow(true_betas)), function(i) {
193+
data.frame(
194+
True = true_betas[i, ],
195+
Estimated_Correct = betas_correct[-1, ][i, ],
196+
Estimated_Wrong = betas_wrong[-1, ][i, ],
197+
Condition = paste("Condition", i)
198+
)
199+
}))
198200
199201
# Plot true vs estimated (correct HRF)
200202
p1 <- ggplot(comparison_data, aes(x = True, y = Estimated_Correct, color = Condition)) +

vignettes/functional_connectivity.Rmd

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ net_idx <- c(seed_voxel, sample(setdiff(1:V, seed_voxel), 40)) # 41 connecte
9292
9393
# Add the seed signal to network voxels (creating functional connectivity)
9494
Y[, net_idx] <- Y[, net_idx] + 0.6 * seed_ts
95+
96+
# Rebuild the dataset from the modified matrix so the injected network signal
97+
# is the data used by downstream model fitting.
98+
dset_modified <- fmridataset::matrix_dataset(
99+
Y,
100+
TR = TR,
101+
run_length = Tlen,
102+
event_table = data.frame(onset = 0, run = 1)
103+
)
95104
```
96105

97106
## Modeling Scanner Drift
@@ -140,10 +149,6 @@ bmodel <- baseline_model(
140149
sframe = sframe
141150
)
142151
143-
# Update the dataset with our modified data that includes the network signal
144-
dset_modified <- dset
145-
dset_modified$data <- Y
146-
147152
# Combine event and baseline models with the dataset
148153
fmodel <- fmri_model(emodel, bmodel, dset_modified)
149154
@@ -157,45 +162,58 @@ fit <- fmri_lm(
157162
With the model fitted, we can now extract the connectivity statistics. The t-statistic for the seed regressor at each voxel tells us how strongly that voxel's activity relates to the seed after accounting for drift.
158163

159164
```{r extract-connectivity-stats}
160-
# Find the seed coefficient in our design matrix
161-
design_mat <- design_matrix(fmodel)
162-
seed_cols <- grep("seed", colnames(design_mat), value = TRUE)
165+
# Extract connectivity statistics using the estimate output itself
166+
all_stats <- as.matrix(stats(fit, type = "estimates"))
167+
seed_cols <- grep("seed", colnames(all_stats), value = TRUE)
163168
if (length(seed_cols) == 0) {
164-
stop("No seed column found in design matrix")
169+
stop("No seed estimate found in fitted model output")
165170
}
166171
seed_col_name <- seed_cols[1]
167-
seed_col_idx <- which(colnames(design_mat) == seed_col_name)
168-
169-
# Extract connectivity statistics
170-
all_stats <- as.matrix(stats(fit, type = "estimates"))
171-
t_seed <- as.numeric(all_stats[, seed_col_idx])
172+
t_seed <- as.numeric(all_stats[, seed_col_name])
172173
173174
# Also get p-values for significance testing
174175
all_pvals <- as.matrix(p_values(fit, type = "estimates"))
175-
p_seed <- as.numeric(all_pvals[, seed_col_idx])
176+
p_seed <- as.numeric(all_pvals[, seed_col_name])
176177
177178
# Check the distribution of our connectivity map
178179
summary(t_seed)
179180
```
180181

181182
## Validating the Results
182183

183-
Since we know which voxels belong to our simulated network, we can check whether our connectivity analysis successfully recovered them. Voxels in the network should show significantly higher connectivity statistics than background voxels.
184+
Since we know which voxels belong to our simulated network, we can check whether our connectivity analysis successfully recovered them. Voxels in the network should have much larger t-statistics than background voxels and should dominate the top-ranked discoveries.
184185

185186
```{r validate-network-recovery}
186187
mean_abs_t_network <- mean(abs(t_seed[net_idx]), na.rm = TRUE)
187188
mean_abs_t_background <- mean(abs(t_seed[-net_idx]), na.rm = TRUE)
189+
sig_rate_network <- mean(p_seed[net_idx] < 0.05, na.rm = TRUE)
190+
sig_rate_background <- mean(p_seed[-net_idx] < 0.05, na.rm = TRUE)
191+
192+
top_ranked <- order(t_seed, decreasing = TRUE)[seq_along(net_idx)]
193+
top_rank_enrichment <- mean(top_ranked %in% net_idx)
188194
189195
stopifnot(
190196
is.finite(mean_abs_t_network),
191197
is.finite(mean_abs_t_background),
192-
mean_abs_t_network > mean_abs_t_background
198+
is.finite(sig_rate_network),
199+
is.finite(sig_rate_background),
200+
is.finite(top_rank_enrichment),
201+
mean_abs_t_network > 5 * mean_abs_t_background,
202+
sig_rate_network > 0.9,
203+
sig_rate_background < 0.1,
204+
top_rank_enrichment > 0.9
193205
)
194206
195-
c(mean_abs_t_network = mean_abs_t_network, mean_abs_t_background = mean_abs_t_background)
207+
c(
208+
mean_abs_t_network = mean_abs_t_network,
209+
mean_abs_t_background = mean_abs_t_background,
210+
sig_rate_network = sig_rate_network,
211+
sig_rate_background = sig_rate_background,
212+
top_rank_enrichment = top_rank_enrichment
213+
)
196214
```
197215

198-
The network voxels show substantially stronger connectivity with the seed, confirming that our method successfully identifies functionally connected regions.
216+
The network voxels dominate the top-ranked statistics and are significant far more often than background voxels, confirming that the fitted model recovers the injected connectivity pattern rather than a diffuse background effect.
199217

200218
## Visualizing the Connectivity Map
201219

0 commit comments

Comments
 (0)