Skip to content

Refactor group analysis CLI: replace contrast_column/values with formula-based model + pairwise contrasts#147

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-group-analysis-cli
Draft

Refactor group analysis CLI: replace contrast_column/values with formula-based model + pairwise contrasts#147
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-group-analysis-cli

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 5, 2026

The old group-level CLI was limited to two-group t-tests via --contrast_column/--contrast_values. This replaces it with a statsmodels formula-based approach supporting arbitrary models, multiple pairwise factors, and stratified contrasts.

New CLI

pixi run spimquant /bids /out group \
  --model "metric ~ C(treatment) * C(genotype) * C(sex) + age" \
  --pairwise treatment \
  --within genotype sex \
  --cores all
Old New
--contrast_column treatment --model "metric ~ C(treatment) + age"
--contrast_values control drug --pairwise treatment
(no strata support) --within genotype sex

Statistical approach

  • Fits a single global OLS model per region/metric (formula passed as-is, no magic expansion)
  • Pairwise contrast stats (t-stat, p-val, Cohen's d) computed via marginal mean predictions + model covariance matrix
  • --within strata fix specific factor values for marginal mean evaluation; model is still fit on all data

Output naming

Contrast identity is encoded in a contrast BIDS entity:

contrast-treatment+controlvsdrug_groupstats.tsv
contrast-treatment+controlvsdrug+genotype-WT+sex-M_groupstats.tsv   # with --within

Pairwise contrast labels are enumerated at Snakemake planning time by reading participants.tsv, so all outputs are statically deterministic.

Key changes

  • snakebids.yml: Replaced --contrast_column/--contrast_values with --model, --pairwise, --within
  • Snakefile: Reads participants.tsv at DAG-planning time to enumerate contrast labels; updated wildcard_constraints and all_group_stats/all_group_stats_coloc target rules
  • groupstats.smk: Added contrast={pairwise_contrast} entity to perform_group_stats, create_stats_heatmap, map_groupstats_to_template_nii; removed concat_subj_parquet_contrast, group_counts_per_voxel_contrast, group_coloc_counts_per_voxel_contrast, concat_subj_segstats_contrast, map_groupavg_segstats_to_template_nii
  • perform_group_stats.py: Rewritten — statsmodels OLS + patsy design matrix for contrast SE, replaces direct scipy.stats.ttest_ind
  • Deleted: concat_subj_parquet_contrast.py, concat_subj_segstats_contrast.py

Breaking change: old --contrast_column/--contrast_values arguments are removed entirely.

Copilot AI and others added 3 commits April 5, 2026 10:30
…l/pairwise/within

- Remove --contrast_column/--contrast_values CLI args from snakebids.yml
- Add --model (statsmodels formula), --pairwise (factor for pairwise comparisons),
  and --within (strata factors) CLI args to snakebids.yml
- Update Snakefile: remove old validation, add pairwise contrast label generation
  by reading participants.tsv at planning time; update wildcard constraints;
  update all_group_stats and all_group_stats_coloc target rules to use
  pairwise_contrast wildcard and remove old contrast-filtered outputs
- Update groupstats.smk: add contrast={pairwise_contrast} entity to
  perform_group_stats, create_stats_heatmap, and map_groupstats_to_template_nii
  rules; remove concat_subj_parquet_contrast, group_counts_per_voxel_contrast,
  group_coloc_counts_per_voxel_contrast, concat_subj_segstats_contrast, and
  map_groupavg_segstats_to_template_nii rules
- Rewrite perform_group_stats.py: fit single global OLS model per region/metric
  using statsmodels, compute pairwise contrast t-stat/pval/cohensd via model
  covariance matrix with marginal mean predictions at specified strata values
- Delete concat_subj_parquet_contrast.py and concat_subj_segstats_contrast.py

Agent-Logs-Url: https://github.com/khanlab/SPIMquant/sessions/5c9691e9-6355-4c0d-85e1-aa07f5c127e3

Co-authored-by: akhanf <11492701+akhanf@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor group analysis CLI to support arbitrary stats models Refactor group analysis CLI: replace contrast_column/values with formula-based model + pairwise contrasts Apr 5, 2026
Copilot AI requested a review from akhanf April 5, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor group analysis CLI: Support arbitrary stats models and pairwise contrasts (replace contrast_column/values approach)

2 participants