ICB-DCM / pyPESTO

python Parameter EStimation TOolbox
https://pypesto.readthedocs.io
BSD 3-Clause "New" or "Revised" License
217 stars 47 forks source link

dynesty: new method to save only the raw dynesty sample results #1331

Closed dilpath closed 6 months ago

dilpath commented 6 months ago

With this change, things like AMICI models that exist in the dynesty loglikelihood, are not saved in the dynesty internal result.

Dummy pyPESTO objects (e.g. for analysis or plotting) can be created by supplying the raw_results to get_original_dynesty_samples or get_mcmc_like_dynesty_samples.

codecov-commenter commented 6 months ago

Codecov Report

Attention: Patch coverage is 70.83333% with 14 lines in your changes are missing coverage. Please review.

Project coverage is 82.38%. Comparing base (81ff7ff) to head (29a5069).

Files Patch % Lines
pypesto/sample/dynesty.py 70.83% 14 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1331 +/- ## =========================================== - Coverage 84.29% 82.38% -1.91% =========================================== Files 152 152 Lines 12432 12452 +20 =========================================== - Hits 10479 10259 -220 - Misses 1953 2193 +240 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

arrjon commented 6 months ago

Thanks Dilan! I check your branch and something seems not quite right yet

dilpath commented 6 months ago
  • some results seem to be discared: sampler.results.logz does not have the same size before and after loading it

Works fine for me, logz is the same shape before and after using save_raw_results and load_raw_results. However, these methods do not save the dynesty DynestySampler.sampler object. Could you try e.g.

print(pypesto_sampler.sampler.results.logz.shape)
pypesto.sample.dynesty.save_raw_results(pypesto_sampler, "dynesty.raw")
raw_results = pypesto.sample.dynesty.load_raw_results("dynesty.raw")
print(raw_results.logz.shape)
  • Moreover, if I call restore_internal_sampler I get AttributeError: 'DynestySampler' object has no attribute 'results'. However, using directly dynesty.utils.restore_sampler works fine. (this issue was already there before your updates)

Also cannot reproduce, e.g. with

print(pypesto_sampler_original.sampler.results.logz.shape)
pypesto_sampler_original.save_internal_sampler("dynesty.dill")
pypesto_sampler = pypesto.sample.DynestySampler(objective_type="negloglike")
pypesto_sampler.restore_internal_sampler("dynesty.dill")
print(pypesto_sampler.sampler.results.logz.shape)

AttributeError: 'DynestySampler' object has no attribute 'results'

Maybe you are trying to access the pyPESTO sampler object, instead of the dynesty sampler object?

# pyPESTO sampler object is `DynestySampler`
pypesto_sampler = pypesto.sample.DynestySampler()
# dynesty sampler object is at `DynestySampler.sampler`
pypesto_sampler.sampler

pypesto_sampler.sampler will have pypesto_sampler.sampler.results.

arrjon commented 6 months ago

to reproduce the attribute error use in the sampler notebook

sampler = sample.DynestySampler(objective_type="negloglike")
result = sample.sample(
    problem=problem,
    n_samples=None,
    sampler=sampler,
    filename=None,
)
print(sampler.results.logz)

sampler.save_internal_sampler("dynesty.dill")

dummy_sampler = sample.DynestySampler()
dummy_sampler.restore_internal_sampler("dynesty.dill")

print(dummy_sampler.results.logz)