Skip to content

Conversation

yifan0330
Copy link
Contributor

@yifan0330 yifan0330 commented Jun 23, 2025

Closes # .

Changes proposed in this pull request:

  • For CBMR inference, add robust inverse of a Fisher information matrix using SVD and a thresholding approach to ignore small singular values

Summary by Sourcery

Add a robust inverse function for Fisher information matrices using SVD thresholding and integrate it into CBMR inference to replace direct matrix inversion for enhanced numerical stability, and update CBMR tests accordingly.

New Features:

  • Introduce robust_inverse function in utils to compute Fisher information matrix inverses via SVD with thresholding.

Enhancements:

  • Use robust_inverse in CBMR inference to replace np.linalg.inv for spatial and moderator coefficient covariance computations.

Documentation:

  • Add documentation for the robust_inverse function detailing its parameters and behavior.

Tests:

  • Skip CBMR import error test when Torch is installed to avoid false negatives.

…using SVD and a thresholding approach to ignore small singular values
Copy link
Contributor

sourcery-ai bot commented Jun 23, 2025

Reviewer's Guide

This PR adds a numerically stable inverse function for Fisher information matrices via SVD thresholding and integrates it into the CBMR inference pipeline, replacing direct matrix inversions and updating related imports, docs, and tests.

Class diagram for robust_inverse utility function

classDiagram
    class robust_inverse {
        +robust_inverse(FI: np.ndarray, eps: float = 1e-8) np.ndarray
    }
    class np.linalg {
        +svd()
    }
    robust_inverse ..> np.linalg : uses
Loading

Class diagram for CBMREstimator changes with robust_inverse integration

classDiagram
    class CBMREstimator {
        _glh_con_group()
        _glh_con_moderator()
    }
    class robust_inverse {
        +robust_inverse(FI: np.ndarray, eps: float = 1e-8) np.ndarray
    }
    CBMREstimator ..> robust_inverse : uses in _glh_con_group, _glh_con_moderator
Loading

File-Level Changes

Change Details Files
Implement robust inverse for Fisher information matrices
  • Added robust_inverse definition
  • Symmetrize input matrix before inversion
  • Apply SVD, threshold small singular values, and reconstruct inverse
nimare/utils.py
Integrate robust_inverse into CBMR inference
  • Imported robust_inverse and updated dependencies
  • Replaced np.linalg.inv calls with robust_inverse in spatial and moderator covariance calculations
  • Added footcite reference in CBMREstimator docstring
nimare/meta/cbmr.py
Adjust CBMR tests for Torch import error
  • Decorated test_cbmr_importerror with skipif when Torch is installed
nimare/tests/test_meta_cbmr.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @yifan0330 - I've reviewed your changes - here's some feedback:

  • In robust_inverse, you’re only zeroing out small singular vectors (via U * M), but you still invert all singular values in S_inv; you should mask S_inv too (e.g. S_inv = np.where(S > eps, 1/S, 0)) to avoid amplifying tiny values.
  • Consider adding targeted unit tests for robust_inverse (e.g., on singular or ill-conditioned matrices) to ensure it behaves as expected and thresholds values properly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `robust_inverse`, you’re only zeroing out small singular vectors (via `U * M`), but you still invert all singular values in `S_inv`; you should mask `S_inv` too (e.g. `S_inv = np.where(S > eps, 1/S, 0)`) to avoid amplifying tiny values.
- Consider adding targeted unit tests for `robust_inverse` (e.g., on singular or ill-conditioned matrices) to ensure it behaves as expected and thresholds values properly.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

nimare/utils.py Outdated
Comment on lines 1396 to 1397
FI_inv = U @ np.diag(S_inv) @ U.T
return FI_inv
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
FI_inv = U @ np.diag(S_inv) @ U.T
return FI_inv
return U @ np.diag(S_inv) @ U.T

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.

1 participant