Skip to content

Fix multiple bugs: division by zero, undefined variable, edge cases#180

Open
bbuchsbaum wants to merge 4 commits into
masterfrom
claude/fix-bugs-907gR
Open

Fix multiple bugs: division by zero, undefined variable, edge cases#180
bbuchsbaum wants to merge 4 commits into
masterfrom
claude/fix-bugs-907gR

Conversation

@bbuchsbaum

Copy link
Copy Markdown
Owner
  • Fix division by zero in Huber weighting (fmri_robust_fitting.R)
    When residuals are exactly 0, abs(u) would be 0 causing division
    by zero. Added protection using pmax with machine epsilon.

  • Fix undefined variable xf in estimate_hrf (fmri_betas.R)
    The variable xf was used in predict() but its definition was
    commented out, causing runtime errors when has_fixed=TRUE.
    Properly handle both has_fixed=TRUE and has_fixed=FALSE cases.

  • Fix bootstrap block size edge case (bootstrap.R)
    When nrows < block_size, nblocks becomes 0 and rep(1:0, ...)
    causes unexpected behavior. Now handles this by using a single
    block when nblocks < 1.

  • Change silent zero fallback to warning with NA (fmri_betas.R)
    When mixed model solvers fail, returning zeros silently could
    produce misleading results. Now returns NA_real_ with warning
    to make failures visible.

  • Remove debug code and commented browser() statements

claude added 4 commits January 6, 2026 13:32
- Fix division by zero in Huber weighting (fmri_robust_fitting.R)
  When residuals are exactly 0, abs(u) would be 0 causing division
  by zero. Added protection using pmax with machine epsilon.

- Fix undefined variable xf in estimate_hrf (fmri_betas.R)
  The variable xf was used in predict() but its definition was
  commented out, causing runtime errors when has_fixed=TRUE.
  Properly handle both has_fixed=TRUE and has_fixed=FALSE cases.

- Fix bootstrap block size edge case (bootstrap.R)
  When nrows < block_size, nblocks becomes 0 and rep(1:0, ...)
  causes unexpected behavior. Now handles this by using a single
  block when nblocks < 1.

- Change silent zero fallback to warning with NA (fmri_betas.R)
  When mixed model solvers fail, returning zeros silently could
  produce misleading results. Now returns NA_real_ with warning
  to make failures visible.

- Remove debug code and commented browser() statements
- Add get_ar_order() helper in fmri_lm_config.R to centralize
  AR order extraction from config, replacing 12+ duplicated
  switch statements across the codebase

- Remove ~80 lines of commented-out generic definitions in
  all_generic.R, consolidating import notes into single block
  at top of file

- Net reduction of 113 lines through DRY improvements

Files affected: fmrilm.R, fmri_lm_runwise.R, fmri_lm_chunkwise.R,
fmri_lm_strategies.R, fmri_lm_config.R, all_generic.R

https://claude.ai/code/session_01BkYFTd6Q7J1A4UozMa6TEw
Major improvements:
- Add compute_chunk_stats() helper to unify beta stats + contrast computation
  across runwise and chunkwise paths
- Add validation helpers (check_required_fields, check_field_type) for
  group_data validation
- Add utility helpers (is_empty_indices, as_indices, ensure_matrix, etc.)
- Add named constants for magic numbers (.DEFAULT_CHUNK_SIZE_*, etc.)

Bug fixes:
- Fix return type inconsistency in pool_runwise_results() - now always
  returns list of contrast tibbles
- Standardize null/empty index handling in fmri_betas.R

Code reduction:
- Remove duplicated beta_stats_matrix + fit_lm_contrasts_fast calls
- Consolidate validation logic in group_data files
- Use consistent patterns for type checking and null handling

https://claude.ai/code/session_01BkYFTd6Q7J1A4UozMa6TEw
- Add build_fmri_lm_cfg() helper in fmri_lm_config.R that consolidates
  all config building logic with backward compatibility parameter handling
- Replace ~140 lines of duplicated config building code in both
  fmri_lm.formula and fmri_lm.fmri_model with calls to helper
- Net reduction of ~15 lines with cleaner, more maintainable code

https://claude.ai/code/session_01BkYFTd6Q7J1A4UozMa6TEw
@bbuchsbaum

Copy link
Copy Markdown
Owner Author

@copilot resolve the merge conflicts in this pull request

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79b52fd10c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread R/fmri_lm_runwise.R
# conres_list[[1]] is already a list of contrast tibbles
list(
contrasts = combined_contrasts, # Single tibble with all contrasts
contrasts = conres_list[[1]], # List of contrast tibbles (same as meta_contrasts)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve tibble shape for single-run contrasts

Return contrasts in the same tabular structure as the multi-run branch: this line now returns the raw list (conres_list[[1]]) instead of a bound tibble, so single-run outputs have a different type than multi-run outputs. Downstream consumers expect x$result$contrasts to be a data frame (e.g., pull_stat() filters on type), so runwise fits with only one run will fail when users request contrast coefficients/statistics.

Useful? React with 👍 / 👎.

@bbuchsbaum

Copy link
Copy Markdown
Owner Author

@codex resolve the merge conflicts in this pull request

@chatgpt-codex-connector

Copy link
Copy Markdown

Summary

  • Updated pool_runwise_results() so the single-run branch now returns contrasts as a bound tibble (or an empty tibble), instead of returning the raw contrast list. This restores shape consistency with downstream expectations and avoids failures in consumers expecting tabular contrast output. R/fmri_lm_runwise.RL509-L520
  • Kept all other pooled outputs unchanged (betas, sigma, rss, rdf, resvar) while only normalizing the single-run contrasts container type. R/fmri_lm_runwise.RL518-L528

Testing

  • ⚠️ Rscript -e "parse(file='R/fmri_lm_runwise.R'); cat('parse ok\n')" (warning: Rscript is not available in this environment, so syntax validation could not be executed).

View task →

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.

2 participants