Skip to content

Fix: Handle zero valid samples in importance_reweight to avoid ValueError crash#40

Merged
kmzzhang merged 1 commit into
kmzzhang:mainfrom
jackieblaum:fix/importance-reweight-nan-guard
Aug 12, 2025
Merged

Fix: Handle zero valid samples in importance_reweight to avoid ValueError crash#40
kmzzhang merged 1 commit into
kmzzhang:mainfrom
jackieblaum:fix/importance-reweight-nan-guard

Conversation

@jackieblaum

Copy link
Copy Markdown
Collaborator

Summary

This PR adds a guard in importance_reweight to handle cases where all log weights are NaN or Inf, which previously caused a crash at .max(). If no valid samples are found, the function now returns zero weights as a safe fallback.

What this fixes

Previously, if all samples were invalid (e.g., failed simulations, out-of-support prior samples), the code would hit:

log_weights -= log_weights[~bad].max()

leading to:

ValueError: zero-size array to reduction operation maximum which has no identity

Changes

  • Added a check for empty valid samples after masking out NaN/Inf log weights.
  • If no valid samples are found, zero weights are returned to prevent a crash.
  • Added a warning printout for diagnostics.

Related Issue

Fixes #39

Notes

This is a minimal fix to avoid the crash. A future enhancement could include diagnostics when invalid samples dominate, and possibly resampling strategies.

@profjsb
profjsb requested review from Copilot and kmzzhang August 7, 2025 20:37

Copilot AI 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.

Pull Request Overview

This PR fixes a crash in the importance_reweight function that occurs when all log weights are NaN or Inf values. The fix adds a guard to check for zero valid samples and returns zero weights as a safe fallback instead of crashing.

  • Added validation to handle empty arrays before calling .max() operation
  • Returns zero weights when no valid samples are available
  • Added diagnostic warning message for debugging purposes

Comment thread src/nbi/engine.py
bad = np.isnan(log_weights) + np.isinf(log_weights)
valid_weights = log_weights[~bad]
if len(valid_weights) == 0:
print("All log weights are NaN or Inf — skipping this round!")

Copilot AI Aug 7, 2025

Copy link

Choose a reason for hiding this comment

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

Using print() for warnings is not ideal. Consider using the logging module or warnings.warn() for better control over diagnostic output and to follow Python best practices.

Suggested change
print("All log weights are NaN or Inf — skipping this round!")
warnings.warn("All log weights are NaN or Inf — skipping this round!")

Copilot uses AI. Check for mistakes.
@kmzzhang
kmzzhang merged commit a5c3944 into kmzzhang:main Aug 12, 2025
7 checks passed
jackieblaum added a commit to jackieblaum/nbi that referenced this pull request Apr 10, 2026
…id weights

Aligns importance_reweight with upstream PR kmzzhang#40, which silently skips a
round when every weight is NaN/Inf rather than crashing. Updates the
test accordingly to assert zero weights are returned.
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.

[BUG] importance_reweight fails when all log weights are NaN or Inf (ValueError in log_weights.max())

3 participants