SSAGESLabs / PySAGES

Python Suite for Advanced General Ensemble Simulations
Other
55 stars 24 forks source link

Switching from .dat file writing to pickling #172

Open ricalessandri opened 1 year ago

ricalessandri commented 1 year ago

Writing the results in a .dat file is one way of doing this. I think the more python way of doing it, would be to pickle the results. But either is fine.

Originally posted by @InnocentBug in https://github.com/SSAGESLabs/PySAGES/pull/171#issuecomment-1198373765

e.g. https://github.com/SSAGESLabs/PySAGES/blob/f5bffa5fc14793dc24cc14e129866a17cbfe891f/examples/hoomd-blue/ann/butane_ann.py#L301-L303

ricalessandri commented 1 year ago

May be a basic question but ... shouldn't this just be:

    import pickle

    ...
    # Analysis: Calculate free energy
    result = pysages.analyze(run_result)
    fes_fn = result["fes_fn"]

    # Pickle
    pickle.dump( result, open("result.pickle", "wb") )
    ...

?

I'm getting:

  File "adp_SpectralABF.py", line 133, in main
    pickle.dump( result, open("result.pickle", "wb") )
AttributeError: Can't pickle local object 'analyze.<locals>.build_fes_fn.<locals>.<lambda>'
InnocentBug commented 1 year ago

Hmm, too bad. that is a function it can’t pickle. we probably have to rework this a bit. Wither return data instead of a function. or make it a pickable functor

pabloferz commented 1 year ago

I believe that what we have guaranteed so far to be pickeable are the raw Results and not the analysis results. This might not be ideal, but performing analysis is generally cheap compared to running whole biased simulations, Of course, we might want to improve upon that in the future, but so far it should give us something reasonable to work with.

ricalessandri commented 1 year ago

🤔 also raw_results seems to not work. The following:

import pickle
...
raw_result = pysages.run(method, generate_simulation, args.time_steps)
...
pickle.dump( raw_result, open("raw_result.pickle", "wb") )

Gives:

Traceback (most recent call last):
  File "adp_SpectralABF.py", line 137, in <module>
    main(sys.argv[1:])
  File "adp_SpectralABF.py", line 104, in main
    pickle.dump( raw_result, open("raw_result.pickle", "wb") )
_pickle.PicklingError: Can't pickle <class 'pysages.methods.core.Result[<class 'pysages.methods.spectral_abf.SpectralABF'>]'>: attribute lookup Result[<class 'pysages.methods.spectral_abf.SpectralABF'>] on pysages.methods.core failed
ricalessandri commented 1 year ago

With #235 merged, I should now do this.

InnocentBug commented 12 months ago

Is this is active?

ricalessandri commented 12 months ago

I think this is only partially complete (i.e., only part of the examples have been converted). I should get back to this. Not sure whether we want to move this "issue" to the PySAGES-examples repo, though?

pabloferz commented 12 months ago

Yeah. It does make sense to have one issue within PySAGES-examples, but I would like us to use the serialization support we have now here as well, just in case someone stumbles upon these examples right now.