Skip to content

fix: Check instance of prior so ValueError is rised after .append_simultions was executed, see #1793 - #1803

Merged
manuelgloeckler merged 4 commits into
sbi-dev:mainfrom
Jocho-Smith:isinstance_check_for__resolve_prior
Mar 20, 2026
Merged

manuelgloeckler merged 4 commits into
sbi-dev:mainfrom
Jocho-Smith:isinstance_check_for__resolve_prior

Conversation

@Jocho-Smith

Copy link
Copy Markdown
Contributor

This PR implements the small fix mentioned in #1793

Either way, I would propose to replace line 545, in _resolve_prior in base.py with a check like the one in line 224 in npe_base.py. That way both cases are taken care of, with and without running append_simulations before build_posterior.

@manuelgloeckler manuelgloeckler left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Jocho-Smith . This solution would always raise an error when no prior is passed, right?

This is somewhat suboptimal, because for e.g. standard NPE we would not like to raise an error here (i.e. with sample_with=direct).

I think the _resolve_prior method should be aware of "for what" it should resolve the prior (e.g. it should get the sample_with argument. If a certain sample_with method does require a proper prior then we should raise an Error, otherwise not. Maybe @janfb also has some idea on this.

@codecov

codecov Bot commented Mar 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.80%. Comparing base (3c60a97) to head (8677620).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1803      +/-   ##
==========================================
- Coverage   87.86%   87.80%   -0.07%     
==========================================
  Files         140      140              
  Lines       12726    12726              
==========================================
- Hits        11182    11174       -8     
- Misses       1544     1552       +8     
Flag Coverage Δ
fast 82.74% <100.00%> (?)

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

Files with missing lines Coverage Δ
sbi/inference/trainers/base.py 94.10% <100.00%> (+0.56%) ⬆️

... and 2 files with indirect coverage changes

@Jocho-Smith

Copy link
Copy Markdown
Contributor Author

Hey @manuelgloeckler , thanks or reviewing. I'll think about it and implement a solution. I like your idea of passing sample_with to _resolve_prior. I'll experiment with it and let you know, if it works.

Also it seems like my local tests were not configured properly, b/c I didn't get errors on my machine. I'll also look into that.

@manuelgloeckler

Copy link
Copy Markdown
Contributor

Hey @Jocho-Smith,

I have found the issue for the uninformative error message super().to ... in #1812, but this is fairly orthogonal to this (i.e. here we should still raise here I think and I can fix this seperatly). Fixing this would yield a more informative error NotImplementedError: Sampling from ImproperEmpirical is not supported. If you are using likelihood or ratio estimation, or multi-round inference, you need to define a prior distribution. but also not super clear.

I think what we should do here:

  • add sample_with to resolve_prior
  • raise an error for None + ImproperPrior on sample_with in {'mcmc', 'vi', importance, rejection} with an informative error message that a proper prior distribution is required for these approaches.

@Jocho-Smith

Copy link
Copy Markdown
Contributor Author

Good catch @manuelgloeckler ! I like the idea and will try to implement this (hopefully next week!)

  • raise an error for None + ImproperPrior on sample_with in {'mcmc', 'vi', importance, rejection} with an informative error message that a proper prior distribution is required for these approaches.

Shouldn't we rather test sample_with not in {'direct', 'sde', 'ode'}? That way we make extensions to other sampling methods easier in the future.

@manuelgloeckler

Copy link
Copy Markdown
Contributor

Yeah sample_with not in {'direct', 'sde', 'ode'} makes more sense! Great, next week would be good. We plan to make a next release soonish and would like to include this fix :)

@Jocho-Smith

Copy link
Copy Markdown
Contributor Author

Hi @manuelgloeckler, I implemented it now and appended some tests in ‎tests/posterior_parameters_test.py (like @Dev-Sudarshan suggested), only covering NPE. Local pre-commit + tests passed. Happy to hear your thoughts.

@manuelgloeckler manuelgloeckler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That looks great @Jocho-Smith ! Code looks great, will wait for the CI to run through (might be some interaction I dont foresee) and then merge it.

Thanks for finding and fixing this!

@manuelgloeckler manuelgloeckler mentioned this pull request Mar 20, 2026
3 tasks done
@manuelgloeckler
manuelgloeckler merged commit e4d5bbf into sbi-dev:main Mar 20, 2026
9 checks passed
@Jocho-Smith
Jocho-Smith deleted the isinstance_check_for__resolve_prior branch March 20, 2026 17:24
@Jocho-Smith Jocho-Smith changed the title fix: Check instance of prior so ValueError is rise after .append_simultions was executed, see #1793 fix: Check instance of prior so ValueError is rised after .append_simultions was executed, see #1793 Mar 21, 2026
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