mu = 12.57E-7;
R = 0.0125; #Radius of metal sphere(m)
m = 0.06676;#mass of sphere(kg)
M0 = 1.931;
def modelFunction(x,M0,x_err,y_err):
return mu/2./pi*M0/((x-x_err)**3.)+y_err
#make residual
def residual(par,x,data=None,std=None):
parvals=par.valuesdict()
M0=parvals['M0']
x_err=parvals['x_err']
y_err=parvals['y_err']
B = modelFunction(x,M0,x_err,y_err)
B=B**(-1./3.)
if data is None:
return B
if std is None:
return (B-data)**2.
return ((B-data)**2./std)
if __name__ == "__main__":
text=np.loadtxt("./sphereMagnet.txt")
x=text[:,0]
data=text[:,1]**(-1./3.)
#initialize Fitting parameters**************************************************
p=lmf.Parameters()
# (Name, Initial Value, Vary, Min ,Max ,Expr)
p.add_many( ( 'M0', 1.8 ,True , 0 , 3. , None),
( 'x_err' , -R ,False, -0.013 , 0. , None),
( 'y_err' , 0.0 ,True, -2. , 2. , None),
)
mini=lmf.Minimizer(residual,p,fcn_args=(x[xmin:xmax],data[xmin:xmax]))
result = mini.minimize(method='leastsq')
print(lmf.fit_report(result,show_correl=True)) #print the statistics
--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/eb230801-e001-42ca-b5a2-734499b0d33e%40googlegroups.com.
Oh yeah I tried it but it returns an error like this.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-9fcc1c47b4c2> in <module>
1 from lmfit.model import save_modelresult
----> 2 save_modelresult(result, 'gauss_modelresult.sav')
c:\program files\python37\lib\site-packages\lmfit\model.py in save_modelresult(modelresult, fname)
1238 """
1239 with open(fname, 'w') as fout:
-> 1240 modelresult.dump(fout)
1241
1242
AttributeError: 'MinimizerResult' object has no attribute 'dump'
So I thought if I want to use residual & minimizer, I can't use save_modelresult functions from lmfit.model.
Regards, kooktae Kim
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CA%2B7ESbp%2BjaJ9aQ83Wj8ZcHOjTsXNjZ2N_zv9wgy%3DGMpeE5Nhsg%40mail.gmail.com.
Oh yeah I tried it but it returns an error like this.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-9fcc1c47b4c2> in <module>
1 from lmfit.model import save_modelresult
----> 2 save_modelresult(result, 'gauss_modelresult.sav')
c:\program files\python37\lib\site-packages\lmfit\model.py in save_modelresult(modelresult, fname)
1238 """
1239 with open(fname, 'w') as fout:
-> 1240 modelresult.dump(fout)
1241
1242
AttributeError: 'MinimizerResult' object has no attribute 'dump'
So I thought if I want to use residual & minimizer, I can't use save_modelresult functions from lmfit.model.
Dear Matt,
Ok I think I solved the problem with your help.
I tried below code.
# start
import pickle
with open('par.pkl', 'wb') as f:
pickle.dump(result, f)
with open('./par.pkl', 'rb') as f:
list2 = pickle.load(f)
#end
It looks working well thanks!
Regards, kooktae Kim.
-----Original Message-----
From: "Matt Newville"<newv...@cars.uchicago.edu>
To: "lmfit-py"<lmfi...@googlegroups.com>;
Cc:
Sent: 2019-06-28 (금) 21:28:43 (GMT+09:00)
Subject: Re: saving params from minimizer result??
Dear Matt,
Ok I think I solved the problem with your help.
I tried below code.
# start
import pickle
with open('par.pkl', 'wb') as f:
pickle.dump(result, f)
with open('./par.pkl', 'rb') as f:
list2 = pickle.load(f)#end
It looks working well thanks!
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/3fad3d5e71bbf41c7ac74a7e56fb70ea%40cweb001.nm.nfra.io.
Dear Matt,
Instead using pickle, I wrote a code like this to use dumps & loads of params.
#start
def savePar( name, result ) : #get the name and result from fitting.
par_j = result.params.dumps() # dump the results into json string.
with open( name,'w' ) as json_file :
json.dump( par_j, json_file ) # wirte json string into file
def readPar( name, result ) : #get the name and result object
with open( 'p.par' , 'r' ) as json_file : #open previous result.
json_str = json.load( json_file )
result.params.loads( json_str ) # load previous result into input variable.
return result
#end
Does this look safe way to save and load the parameters?? (actually I wasn't able to figure out how to use dump & load. , instead I used dumps & loads that changes
fitting result into json string.) Please let me know if there is simpler & better way to do it using minimizer
Thanks!
Regards, kooktae Kim
--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/3fad3d5e71bbf41c7ac74a7e56fb70ea%40cweb001.nm.nfra.io.