Skip to content

Conversation

@jackieblaum
Copy link
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
Copy link

Copilot AI left a comment

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

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!")
Copy link

Copilot AI Aug 7, 2025

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
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())

2 participants