I have tried to best fit my data set for more than 700 txt files with a single run. And I want the output parameter obtained from all files into a tabular format.I have tried in this way
def resid(params, x, ydata):
decay = params['decay'].value
phase = params['phase'].value
omega = params['omega'].value
amp = params['amp'].value
ampb=params['ampb'].value
phaseb=params['phaseb'].value
#omegab = params['omegab'].value
y_model = (amp * np.sin(x*2*np.pi*omega+phase)+ ampb*np.sin(x*4*np.pi*omega+phaseb)) * np.exp(-x*decay)
return y_model - ydata
filelist = glob.glob("C:/Users/USER/Desktop/B3/*.txt")
#print(filelist)
for file in filelist:
data = np.loadtxt(file)
x = data[:,0]
y = data[:,1]
params = lmfit.Parameters()
params.add('phase', 0.0, min=-np.pi, max=np.pi)
params.add('omega', 10, min=7, max=12)
params.add('amp', 7, min=0, max=10.0)
params.add('decay', 0.05, min=0, max=10.0)
params.add('ampb', 3, min=0, max=8.0)
params.add('phaseb', 0.0, min=-np.pi, max=np.pi)
#params.add('omegab', 18, min=16, max=20)
fit = lmfit.minimize(resid, params, args=(x, y), method='differential_evolution')
#print("\n\n# Fit using differential_evolution:")
#print(lmfit.report_fit(fit))
for name, param in fit.params.items():
if name=='phase':
print('phase',' ' , param.value,' ',param.stderr, )
elif name=='amp ':
print('amp', ' ',param.value,' ',param.stderr )
elif name=='omega':
print('omega', ' ',param.value,' ',param.stderr)
elif name=='decay':
print('decay', ' ',param.value,' ',param.stderr)
elif name=='ampb':
print('ampb', ' ',param.value,' ',param.stderr)
else:
print('phaseb',' ',param.value,' ',param.stderr)
print(fit.success)
I expect the output in this format enter image description here
but I have got values of each output separately like this enter image description here
You can create an empty
dictbefore the innerforloop, make the loop fill thatdictwith the params, and then print them all on 1 line after theforloop is done. The below example will print the values next to each other, separated with the TAB character, which should be readable enough but the "table" might not be perfectly aligned. If you want it to look better in terminal, there are some solutions mentioned here. Alternatively you could dump the TAB- or comma-separated values to a file, then open that file e.g. with Excel.