Search code examples
pythonpython-2.7savecurve-fittinglmfit

lmfit saving function in Python


I've got an issue with

save_modelresult(result, 'S:\Doc\Python\Results\modelresult.csv')

Well the save is complete, but the organization of this data is very poor. Does anyone know of any tricks/ways how to store my results in organized columns?

Cheers!


Solution

  • Lmfit's model.save_modelresult() function saves the ModelResult as JSON that is intended to be loaded with load_modelresult which will turn that saved representation into a working ModelResult in another Python session. It's not necessarily meant to be human-readable. Then again, it can be read in with the json library if you want.

    For organizing that output in a human-readable form, I would suggest looking at the fit_report() method of ModelResult and the lmfit.printfuncs.fit_report() function that it uses. The simplest thing to do is probably just save that fit report to a file, say like this:

    # save fit report to a file:
    with open('fit_result.txt', 'w') as fh:
        fh.write(result.fit_report())