-1

I am iterating over N files and fitting all the data using lmfit Gaussian Model. I would like to obtain an array of all the parameters for the different files so I can plot them against another variable. Any idea how to do so ?

this is my code:

def gaussian(x,center,sigma,amplitude):
    return amplitude*(1/(sigma*(np.sqrt(2*np.pi))))*np.exp(((-(x-center)**2))/(2*(sigma**2)))

for f in os.listdir(directory):    
    f= os.path.join(directory, f)  
    data=np.loadtxt(f)            

    x=data[np.where(data[:,0]<wavemax),0]
    y=data[np.where(data[:,0]<wavemax),1]
    x=x[0,np.where(x[0]>wavemin)][0]
    y=y[0,np.where(x>wavemin)][0]

    gauss1 = Model(gaussian,prefix='g1_')
    pars=gauss1.make_params()
    pars['g1_center'].set(2855.0, True, 2830.0, 2900.0)
    pars['g1_sigma'].set(6, True, 1.0, 20.0)
    pars['g1_amplitude'].set(0.6, True, 0.01, 30)

    gauss2 = Model(gaussian,prefix='g2_')
    pars.update(gauss2.make_params())
    pars['g2_center'].set(2900.0, True, 2850.0, 2950.0)
    pars['g2_sigma'].set(10.0, True, 1.0, 20.0)
    pars['g2_amplitude'].set(0.6, True, 0.01, 30)

    gauss3 = Model(gaussian,prefix='g3_')
    pars.update(gauss3.make_params())
    pars['g3_center'].set(2925.0, True, 2850.0, 2950.0)
    pars['g3_sigma'].set(10.0, True, 1.0, 50.0)
    pars['g3_amplitude'].set(3, True, 0.01, 30)

    gauss4 = Model(gaussian,prefix='g4_')
    pars.update(gauss4.make_params())
    pars['g4_center'].set(2970.0, True, 2850.0, 3000.0)
    pars['g4_sigma'].set(14.0, True, 1.0, 20.0)
    pars['g4_amplitude'].set(3, True, 0.01, 30)

    gauss5= Model(gaussian,prefix='g5_')
    pars.update(gauss5.make_params())
    pars['g5_center'].set(3050, True, 2750, 3050, None, None)
    pars['g5_sigma'].set(100, True, None, None, None, None)
    pars['g5_amplitude'].set(5, True, None, None, None, None)


    mod=gauss1+gauss2+gauss3+gauss4+gauss5 # fitting the entire curve 

    init = mod.eval(pars, x=x) #specifying the parameters for the fitting of all the curve
    out = mod.fit(y, pars, x=x)
AMC
  • 2,642
  • 7
  • 13
  • 35

1 Answers1

0

make a results list before your loop, and for each fit, after the fit is complete, append out.params to that results list (and maybe other stuff to help mark fit details).

Then use the list of resulting fit parameters as you like.

M Newville
  • 7,486
  • 2
  • 16
  • 29