Skip to content

Fixing #1047#1061

Open
GregoryAshton wants to merge 1 commit intomainfrom
1047-fix-numpy-incompatibility
Open

Fixing #1047#1061
GregoryAshton wants to merge 1 commit intomainfrom
1047-fix-numpy-incompatibility

Conversation

@GregoryAshton
Copy link
Copy Markdown
Collaborator

Issue

GitHub #1047

sample_subset_constrained raised a TypeError when sampling with size=None or size=1:

TypeError: only 0-dimensional arrays can be converted to Python scalars

Root Cause

evaluate_constraints returns a numpy array via np.ones_like. When sampling a single point, this is a 1-element array rather than a scalar. The subsequent int(is_valid) call on line 464 fails because int() only accepts 0-dimensional arrays.

Fix

Wrap is_valid with np.squeeze before converting to int, reducing the 1-element array to a 0-d scalar:

# before
n_valid_samples += int(is_valid)

# after
n_valid_samples += int(np.squeeze(is_valid))

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.24%. Comparing base (d052622) to head (7f3e429).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1061   +/-   ##
=======================================
  Coverage   66.24%   66.24%           
=======================================
  Files          89       89           
  Lines       17064    17064           
  Branches     2642     2642           
=======================================
  Hits        11304    11304           
  Misses       5021     5021           
  Partials      739      739           
Flag Coverage Δ
python310 66.05% <100.00%> (+0.02%) ⬆️
python311 66.19% <100.00%> (-0.01%) ⬇️
python312 66.21% <100.00%> (-0.03%) ⬇️
python313 66.20% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ColmTalbot ColmTalbot force-pushed the 1047-fix-numpy-incompatibility branch from 61d5159 to 4071122 Compare March 25, 2026 18:37
@ColmTalbot
Copy link
Copy Markdown
Collaborator

I thought we had fixed this already...

@GregoryAshton
Copy link
Copy Markdown
Collaborator Author

My apologies @ColmTalbot I may have missed your MR that fixed it. If that is the case, please do close this

@mj-will mj-will added the bug Something isn't working label Apr 2, 2026
@mj-will mj-will requested a review from a team April 2, 2026 15:48
@mj-will
Copy link
Copy Markdown
Collaborator

mj-will commented Apr 2, 2026

Agreed on the call we would backport this to 2.7.x

Squeeze the output to one-dimension to fix incompatibility with numpy >
2.4
Copilot AI review requested due to automatic review settings April 24, 2026 02:06
@ColmTalbot ColmTalbot force-pushed the 1047-fix-numpy-incompatibility branch from 4071122 to 7f3e429 Compare April 24, 2026 02:06
Copy link
Copy Markdown

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

Fixes a TypeError in PriorDict.sample_subset_constrained when constrained sampling is invoked with size=None or size=1, which occurs in sampler parameter verification paths (e.g., dynesty).

Changes:

  • Ensure the constraint validity indicator can be converted to an int by squeezing 1-element numpy arrays to a 0-d scalar before counting valid samples.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bilby/core/prior/dict.py
Comment on lines 459 to 467
if size is None or size == 1:
while True:
sample = self.sample_subset(keys=keys, size=size)
is_valid = self.evaluate_constraints(sample)
n_tested_samples += 1
n_valid_samples += int(is_valid)
n_valid_samples += int(np.squeeze(is_valid))
check_efficiency(n_tested_samples, n_valid_samples)
if is_valid:
return sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants