Skip to content

Bayesian post-analysis using BUMPS DREAM#244

Merged
rozyczko merged 17 commits into
developfrom
bayesian
Jun 3, 2026
Merged

Bayesian post-analysis using BUMPS DREAM#244
rozyczko merged 17 commits into
developfrom
bayesian

Conversation

@rozyczko
Copy link
Copy Markdown
Member

@rozyczko rozyczko commented May 6, 2026

A new Bayesian MCMC sampling interface. This enables users to perform posterior sampling using the BUMPS DREAM sampler directly from both the MultiFitter and BUMPS minimizer classes.

  • Added a sample method to MultiFitter that flattens multi-dataset arrays, resolves parameter aliases, and delegates to the minimizer’s sampling method, supporting both 1D and vectorized multi-dimensional fits.
  • Implemented a sample method in the BUMPS minimizer (minimizer_bumps.py) that builds a BUMPS FitProblem and runs the DREAM sampler, returning posterior draws, parameter names, sampler state, and log-probabilities.

@rozyczko rozyczko added [scope] enhancement Adds/improves features (major.MINOR.patch) [priority] medium Normal/default priority labels May 6, 2026
@rozyczko rozyczko changed the title initial implementation Bayesian post-analysis using BUMPS DREAM May 6, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 89.28571% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.65%. Comparing base (d4a4c23) to head (e397973).

Files with missing lines Patch % Lines
.../easyscience/fitting/minimizers/minimizer_bumps.py 86.15% 9 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #244      +/-   ##
===========================================
+ Coverage    82.55%   82.65%   +0.10%     
===========================================
  Files           62       62              
  Lines         4946     5023      +77     
===========================================
+ Hits          4083     4152      +69     
- Misses         863      871       +8     
Flag Coverage Δ
integration 43.87% <70.23%> (+0.38%) ⬆️
unittests 82.14% <88.09%> (+0.86%) ⬆️

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

Files with missing lines Coverage Δ
src/easyscience/fitting/fitter.py 97.27% <100.00%> (+0.37%) ⬆️
src/easyscience/fitting/multi_fitter.py 98.55% <100.00%> (+1.40%) ⬆️
.../easyscience/fitting/minimizers/minimizer_bumps.py 95.95% <86.15%> (-3.52%) ⬇️

@rozyczko rozyczko marked this pull request as ready for review May 18, 2026 09:46
@rozyczko
Copy link
Copy Markdown
Member Author

rozyczko commented May 19, 2026

I might want to extend this to multiprocessing, since BUMPS already supports this.
The implementation should speed things up quite noticeably.
(in a separate branch/PR)

Copy link
Copy Markdown
Member

@henrikjacobsenfys henrikjacobsenfys left a comment

Choose a reason for hiding this comment

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

Very nice, found no issues except a few things in the notebook:

It's using ObjBase, which we're deprecating
Yo ucould add the hash to the pooch retrieve call

@henrikjacobsenfys
Copy link
Copy Markdown
Member

Some of the notes don't get rendered right now
image

@rozyczko
Copy link
Copy Markdown
Member Author

Some of the notes don't get rendered right now image

hmmm
for me it displays the table nicely

Screenshot 2026-05-29 104430

@henrikjacobsenfys
Copy link
Copy Markdown
Member

Some of the notes don't get rendered right now image

hmmm for me it displays the table nicely

Screenshot 2026-05-29 104430

The tables are fine, it's the markdown that I'm unsure of. But could just be a vs code thing

@rozyczko
Copy link
Copy Markdown
Member Author

Fixed both issues

@damskii9992
Copy link
Copy Markdown
Contributor

It's using ObjBase, which we're deprecating

Sadly we don't have the replacement yet. We need a new class for these kinds of easyscience standalone fits where you can just feed the class the parameters and the model function. To make it easy. Using ModelBase requires defining a class which is not something a user should be doing . . . .

So I think we should stick with using ObjBase (at least in tutorials) until we have created the replacement class.

Copy link
Copy Markdown
Contributor

@damskii9992 damskii9992 left a comment

Choose a reason for hiding this comment

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

I have only reviewed the notebook so far, but I feel like there is enough here to work on while I review the code.

Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
"\n",
" return pooch.retrieve(\n",
" url=f'https://public.esss.dk/groups/scipp/dmsc-summer-school/2025/{name}',\n",
" known_hash=known_hash,\n",
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.

We should probably have a .datasets folder in easyscience to make import of data-files for tutorials easy. A tutorial really shouldn't have cells like these . . .

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You mean relying on dmsc-summer-school location or using pooch.retrieve?

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.

I mean something like what I have in my documentation EasyImaging branch.

A datasets module with loader functions, which loads in files from its own pooch registry.

Copy link
Copy Markdown
Member Author

@rozyczko rozyczko Jun 2, 2026

Choose a reason for hiding this comment

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

This, again, is a good refactoring, which needs to be done for all the tutorials, hence outside the scope of this PR. Maybe we can move it out to EasyUtils

#256

Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
Comment thread docs/docs/tutorials/fitting-bayesian.ipynb
Comment thread docs/docs/tutorials/fitting-bayesian.ipynb
Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
"ax.plot(omega, mid, '-', color='C1', label='posterior median')\n",
"ax.set(xlabel='$\\\\omega$/meV', ylabel='$I(\\\\omega)$')\n",
"ax.legend()\n",
"plt.show()"
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.

This should really be hidden away by a method in easyscience to make it a simple call like:

results.plot_model(confidence_intervals = [2.5, 97.5])

Or something like that in terms if the user-facing API.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As mentioned above - we don't want the core to provide us with plotting. Users choose what plotting mechanism they use with the data they get from fitting. Hence - no plopp/matplotlib/plotly dependence in the core itself.

Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
Comment thread docs/docs/tutorials/fitting-bayesian.ipynb Outdated
Copy link
Copy Markdown
Contributor

@damskii9992 damskii9992 left a comment

Choose a reason for hiding this comment

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

Comments after reading the code and not just the user-facing API.

Comment thread docs/mkdocs.yml Outdated
Comment thread src/easyscience/fitting/minimizers/minimizer_bumps.py Outdated
Comment thread src/easyscience/fitting/minimizers/minimizer_bumps.py Outdated
Comment thread src/easyscience/fitting/minimizers/minimizer_bumps.py
Comment thread src/easyscience/fitting/minimizers/minimizer_bumps.py
Comment thread src/easyscience/fitting/multi_fitter.py Outdated
Comment thread src/easyscience/fitting/multi_fitter.py Outdated
Comment thread tests/integration/fitting/test_multi_fitter.py Outdated
pytest.skip('BUMPS is not installed')

result = f.sample(
x=[x2D], y=[y2D], weights=[weights], samples=100, burn=20, thin=2, vectorized=True
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.

What is this vectorized keyword? And since we don't expose it in our API, why do we even test for it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

vectorized is indeed in mcmc_sample signature. It is quite important so I'll leave it as is

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.

Can you then add a comment to the test to explain why/what? And should it be exposed in the API?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is not just in sampling but in regular fitting as well. Nothing new.
But I added more comments explaining the usage cases.

Comment thread pyproject.toml Outdated
@damskii9992
Copy link
Copy Markdown
Contributor

It's using ObjBase, which we're deprecating

Sadly we don't have the replacement yet. We need a new class for these kinds of easyscience standalone fits where you can just feed the class the parameters and the model function. To make it easy. Using ModelBase requires defining a class which is not something a user should be doing . . . .

So I think we should stick with using ObjBase (at least in tutorials) until we have created the replacement class.

Actually, come to think of it, I believe we can replace it with an EasyList for now. All the class needs to do is hold the parameters and have an get_all_fittable_parameters method implemented. I think that would be cleaner.

@rozyczko
Copy link
Copy Markdown
Member Author

rozyczko commented Jun 2, 2026

It's using ObjBase, which we're deprecating

Sadly we don't have the replacement yet. We need a new class for these kinds of easyscience standalone fits where you can just feed the class the parameters and the model function. To make it easy. Using ModelBase requires defining a class which is not something a user should be doing . . . .
So I think we should stick with using ObjBase (at least in tutorials) until we have created the replacement class.

Actually, come to think of it, I believe we can replace it with an EasyList for now. All the class needs to do is hold the parameters and have an get_all_fittable_parameters method implemented. I think that would be cleaner.

As discussed, I'll move back to ObjBase so all the tutorials can be modified to use EasyList at the same time, in a separate PR

#253

@rozyczko rozyczko requested a review from damskii9992 June 2, 2026 13:11
Copy link
Copy Markdown
Contributor

@damskii9992 damskii9992 left a comment

Choose a reason for hiding this comment

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

LGTM for now.

@rozyczko rozyczko merged commit aadbd48 into develop Jun 3, 2026
34 checks passed
rozyczko added a commit that referenced this pull request Jun 5, 2026
Integrate the squash-merged Bayesian DREAM feature from develop (PR #244)
while retaining this branch's multiprocessing and pickling additions.

Reconciliation notes:
- Adopt develop's polymorphic `Fitter.mcmc_sample` (replaces the branch's
  separate `MultiFitter.sample`) and develop's API naming
  (`mcmc_sample`, return key `internal_bumps_object`) and input validation.
- Extend `Fitter.mcmc_sample` with `chains`, `seed`, and `n_workers`,
  forwarding them to the BUMPS minimizer so multiprocessing works for both
  single- and multi-dataset fitters.
- Keep the minimizer-level multiprocessing machinery: `BumpsPoolMapper`,
  worker helpers, cloudpickle-based problem serialization, the `chains`
  alias / `seed` handling, and `n_workers` wiring with mapper cleanup.
- Merge the test suites: develop's mcmc_sample tests plus the branch's MP,
  pickling, alias, and seed tests (adapted to the mcmc_sample API). Make the
  n_workers subprocess test import the model by file path instead of via the
  shadowable `tests` namespace package.
- Update the sampling benchmark tool to call `mcmc_sample`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[priority] medium Normal/default priority [scope] enhancement Adds/improves features (major.MINOR.patch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants