예제 #1
0
파일: functions.py 프로젝트: aphics/Bubbly
    def gaussianfit(self, num_zeros, params, limitedmin, limitedmax, minpars,
                    maxpars):  #wrapper for the fitting algorithm

        ajuste = multigaussfit(self.velocity,
                               self.intensity,
                               ngauss=num_zeros / 2,
                               params=params,
                               limitedmin=limitedmin,
                               limitedmax=limitedmax,
                               minpars=minpars,
                               maxpars=maxpars,
                               quiet=True,
                               shh=True)
        fit_params = ajuste[0][:]
        return fit_params
예제 #2
0
파일: myfitting.py 프로젝트: ezbc/planckpy
def fit_gaussians(x, y, guesses=None, ncomps=1):

    """ Fits multiple Gaussians to y(x).

    Parameters
    ----------
    x, y : array-like
        Data.
    guesses : array-like, optional
        Initial guesses for fitting: [amp, x0, width] for each componenet. If
        ncomps > len(guesses) / 3  then the ncomps will be lowered to
        len(guesses) / 3.
    ncomps : int
        Number of Gaussians to fit

    Returns
    -------
    model : array-like
    model_comps : list
        Model components
    model_params : list
        list of model_params

    """

    import numpy as np
    from gaussfitter import multigaussfit

    def gauss(x, amp, x0, width):
        return amp * np.exp(-(x-x0)**2 / (2 * width**2))

    fit = multigaussfit(x, y, ngauss=int(ncomps), params=np.asarray(guesses))

    model = fit[1]
    model_params, model_comps = [], []

    for i in xrange(len(fit[0]) / 3):
        model_param = fit[0][3*i:3*i+3]
        model_params.append(model_param)
        model_comps.append(gauss(x, *model_param))

    return model, model_comps, model_params
예제 #3
0
파일: myfitting.py 프로젝트: ezbc/planckpy
def fit_gaussians(x, y, guesses=None, ncomps=1):
    """ Fits multiple Gaussians to y(x).

    Parameters
    ----------
    x, y : array-like
        Data.
    guesses : array-like, optional
        Initial guesses for fitting: [amp, x0, width] for each componenet. If
        ncomps > len(guesses) / 3  then the ncomps will be lowered to
        len(guesses) / 3.
    ncomps : int
        Number of Gaussians to fit

    Returns
    -------
    model : array-like
    model_comps : list
        Model components
    model_params : list
        list of model_params

    """

    import numpy as np
    from gaussfitter import multigaussfit

    def gauss(x, amp, x0, width):
        return amp * np.exp(-(x - x0)**2 / (2 * width**2))

    fit = multigaussfit(x, y, ngauss=int(ncomps), params=np.asarray(guesses))

    model = fit[1]
    model_params, model_comps = [], []

    for i in xrange(len(fit[0]) / 3):
        model_param = fit[0][3 * i:3 * i + 3]
        model_params.append(model_param)
        model_comps.append(gauss(x, *model_param))

    return model, model_comps, model_params
예제 #4
0
 def multifit(self):
     self.ngauss = len(self.guesses)/3
     self.setfitspec()
     if self.fitkwargs.has_key('negamp'): self.fitkwargs.pop('negamp')
     mpp,model,mpperr,chi2 = gaussfitter.multigaussfit(
             self.specplotter.vind[self.gx1:self.gx2], 
             self.spectofit[self.gx1:self.gx2], 
             err=self.errspec[self.gx1:self.gx2],
             ngauss=self.ngauss,
             params=self.guesses,
             **self.fitkwargs)
     self.chi2 = chi2
     self.dof  = self.gx2-self.gx1-self.ngauss*3
     self.model = model
     self.modelpars = mpp.tolist()
     self.modelerrs = mpperr.tolist()
     self.modelplot = self.specplotter.axis.plot(
             self.specplotter.vind[self.gx1:self.gx2],
             self.model+self.specplotter.offset, color=self.fitcolor, linewidth=0.5)
     self.residuals = self.spectofit[self.gx1:self.gx2] - self.model
     if self.autoannotate:
         self.annotate()
예제 #5
0
 def multifit(self):
     self.ngauss = len(self.guesses)/3
     self.setfitspec()
     if self.fitkwargs.has_key('negamp'): self.fitkwargs.pop('negamp')
     mpp,model,mpperr,chi2 = gaussfitter.multigaussfit(
             self.specplotter.vind[self.gx1:self.gx2], 
             self.spectofit[self.gx1:self.gx2], 
             err=self.errspec[self.gx1:self.gx2],
             ngauss=self.ngauss,
             params=self.guesses,
             **self.fitkwargs)
     self.chi2 = chi2
     self.dof  = self.gx2-self.gx1-self.ngauss*3
     self.model = model
     self.modelpars = mpp.tolist()
     self.modelerrs = mpperr.tolist()
     self.modelplot = self.specplotter.axis.plot(
             self.specplotter.vind[self.gx1:self.gx2],
             self.model+self.specplotter.offset, color=self.fitcolor, linewidth=0.5)
     self.residuals = self.spectofit[self.gx1:self.gx2] - self.model
     if self.autoannotate:
         self.annotate()
예제 #6
0
def gaussparty(gausspars, nspec, CCFvaluelist, CCFxaxis, error_array, ngauss=2,
               fixed=[False, False, False],
               limitedmin=[False, False, True],
               limitedmax=[False, False, False],
               minpars=[0, 0, 0], maxpars=[0, 0, 0]):
    '''
    Fit 2 gaussians to some data.

    This function is old and doesn't work very well. Use gaussbetter instead.

    Parameters
    ----------
    gausspars : `str`
        Filename containing initial Gaussian parameter guesses
    nspec : `int`
        Number of spectra (visits)
    CCFvaluelist : `list`
        2D list with CCF values, one per visit
    CCFxaxis : `list`
        2D list with CCF RV x-axis values corresponding to CCFvaluelist, one per visit
    error_array: `list`
        2D list with errors corresponding to CCFvaluelist
    fixed : `list` of bools
        Is parameter fixed? (amplitude, position, width)
    limitedmin/minpars : `list`
        Set lower limits on each parameter (default: width > 0)
    limitedmax/maxpars : `list`
        Set upper limits on each parameter
    minpars : `list` (amplitude, position, width)
    maxpars : `list` (amplitude, position, width)

    Returns
    -------
    gaussfitlist : `list`
        2D list of results from calling multigaussfit
    '''
    assert len(CCFvaluelist) == nspec
    assert len(CCFxaxis) == nspec
    assert len(error_array) == nspec

    param = []
    with open(gausspars) as f1:
        for line in f1:
            if line[0] != '#':
                param.append(line.rstrip())

    assert len(param) == nspec

    gaussfitlist = []
    print(' ')
    print('Gaussian fit results: peak amplitude, width, rvraw, rvraw_err')
    print ('-------------------------------------------------------------')
    for i in range(0, nspec):
        if '#' in param[i]:
            commentbegin = param[i].find('#')
            partest = param[i][0:commentbegin].split()
        else:
            partest = param[i].split()
        partest = [float(item) for item in partest]
        gaussfit = gf.multigaussfit(CCFxaxis[i], CCFvaluelist[i], ngauss=ngauss,
                                    params=partest, err=error_array[i], fixed=fixed,
                                    limitedmin=limitedmin, limitedmax=limitedmax,
                                    minpars=minpars, maxpars=maxpars,
                                    quiet=True, shh=True, veryverbose=False)
        gaussfitlist.append(gaussfit)
        print('{0:.3f} {1:.2f} {2:.4f} {3:.4f} \t {4:.3f} {5:.2f} {6:.4f} {7:.4f}'.format(
              gaussfit[0][0], gaussfit[0][2], gaussfit[0][1], gaussfit[2][1],
              gaussfit[0][3], gaussfit[0][5], gaussfit[0][4], gaussfit[2][4]))
    return gaussfitlist
예제 #7
0
def gaussparty(gausspars, nspec, filenamelist, bfsmoothlist, bf_ind, amplimits, threshold, widlimits):
    '''
    Fits 2 or 3 gaussians to some data
    '''
    param = []
    with open(gausspars) as f1:
        for line in f1:
            if line[0] != '#':
                param.append( line.rstrip() )
    #param = np.loadtxt(gausspars, comments='#')
    bffitlist = []
    bffitlist.append(0)
    gauss1 = [[] for i in range(nspec)]
    gauss2 = [[] for i in range(nspec)]
    gauss3 = [[] for i in range(nspec)]
    gauss1[0] = [0,0]
    gauss2[0] = [0,0]
    gauss3[0] = [0,0]
    error_array = np.ones(len(bfsmoothlist[0]))*0.01 # dummy array with 0.01 error values
    print(' ')
    print('Gaussian fit results: peak amplitude, width, rvraw, rvraw_err')
    print ('-------------------------------------------------------------')
    for i in range(1, nspec):
        # check to see if we are fitting a third gaussian, i.e., one near zero
        # don't print out the result of this fit, but do return it for plotting
        # handle comments in gausspars file without exploding
        if '#' in param[i]:
            commentbegin = param[i].find('#')
            partest = param[i][0:commentbegin].split()
        else:
            partest = param[i].split()
        if len(partest) == 6: ngauss = 2
        elif len(partest) == 9: ngauss = 3
        else: print('something is wrong with your gausspars file!')       
        # min and max pars for peak 1: amp, rv, width
        #minpars=[0.8, float(partest[1])-threshold, 0]
        #maxpars=[1.0,   float(partest[1])+threshold, 7]
        # min and max pars for peak 2: amp, rv, width
        #minpars.extend([0.05, float(partest[4])-threshold, 0])
        #maxpars.extend([0.20, float(partest[4])+threshold, 40])
        minpars = [amplimits[0], float(partest[1])-threshold, widlimits[0]]
        maxpars = [amplimits[1], float(partest[1])+threshold, widlimits[1]]
        minpars.extend([amplimits[2], float(partest[4])-threshold, widlimits[2]])
        maxpars.extend([amplimits[3], float(partest[4])+threshold, widlimits[3]])
        if ngauss == 2:
            bffit = gf.multigaussfit(bf_ind, bfsmoothlist[i], ngauss=ngauss, 
                    params=partest, err=error_array,
                    limitedmin=[True,True,True], limitedmax=[True,True,True], 
                    minpars=minpars, maxpars=maxpars, quiet=True, shh=True)
        elif ngauss == 3:
            # min and max pars for peak 3: amp, rv, width (hardwired)
            minpars.extend([0.05, float(partest[7])-threshold, 0])
            maxpars.extend([1.0,  float(partest[7])+threshold, 30])
            bffit = gf.multigaussfit(bf_ind, bfsmoothlist[i], ngauss=ngauss, 
                    params=partest, err=error_array,
                    limitedmin=[True,True,True], limitedmax=[True,True,True], 
                    minpars=minpars, maxpars=maxpars, quiet=True, shh=True)
        newbffit = [[] for x in range(len(bffit))]
        # Sometimes bffit[2] is None, or contains None. Set it to zeros instead.
        try:
            if not any(bffit[2]): # this will fail if bffit[2] = None
                newbffit[0] = bffit[0]
                newbffit[1] = bffit[1]
                newbffit[2] = [0, 0, 0, 0, 0]
            else:
                newbffit = bffit
        except:
            print('WARNING - gaussfit is acting up, fit failed for the next row, adjust gausspars file:')
            if not bffit[2]: # this catches the case where bffit[2] = None
                newbffit[0] = bffit[0]
                newbffit[1] = bffit[1]
                newbffit[2] = [0, 0, 0, 0, 0]
            else:
                newbffit = bffit
        bffitlist.append(newbffit)
        # NOTE: to get the gaussian fit corresponding to bfsmoothlist[i], use bffitlist[i][1].
        # RV1 for observation i is bffitlist[i][0][1] +/- bffitlist[i][2][1].
        # RV2 for observation i is bffitlist[i][0][4] +/- bffitlist[i][2][4].
        # (note: need to check if bffit[2] == None before calling bffit[2][1] or bffit[2][4])
        print('{0:s}    {1:.3f} {2:.2f} {3:.4f} {4:.4f} \t {5:.3f} {6:.2f} {7:.4f} {8:.4f}'.format(
            filenamelist[i][-20:], newbffit[0][0], newbffit[0][2], newbffit[0][1], newbffit[2][1],
            newbffit[0][3], newbffit[0][5], newbffit[0][4], newbffit[2][4]))
    print(' ')
    print('You MUST manually guesstimate the location of each Gaussian\'s peak in %s!' % gausspars)
    print('Until you do, the above values will be WRONG and the plot will look TERRIBLE.')
    print(' ')
    return bffitlist
def main(argv):
	inputfile = ''
	outputfile = ''
	try:
			opts, args = getopt.getopt(argv,"hi:j:o:",["ifile=","ifile2","ofile="])
	except getopt.GetoptError:
		print 'test.py -i <inputfile> -j <inputfile2> -o <outputfile>'
		sys.exit(2)
	for opt, arg in opts:
		if opt == '-h':
			print 'test.py -i <inputfile> -j <inputfile2> -o <outputfile>'
			sys.exit()
		elif opt in ("-i", "--ifile"):
			inputfile = arg
		elif opt in ("-j", "--ifile2"):
			inputfile2 = arg
		elif opt in ("-o", "--ofile"):
			outputfile = arg

	# data
	f = open (inputfile, "rt")
	#print inputfile
	l = f.readline()
	time = []
	counts = []
	is_traj = 0
	
	while l:
		va = l.split()
		if (len(va)==5 and va[0]=="record#"):
			is_traj = 1  
			l = f.readline()
			va = l.split()
		if is_traj == 1:
			time.append(float(va[0]))
			counts.append(float(va[1]))
		l = f.readline()
	f.close() #close the data file

	# data thresh
	f2 = open (inputfile2, "rt")
	l2 = f2.readline()
	th_u = []
	th_d = []

	f3 = open('../tmp/gaussFitTEX.txt','w')
	while l2:
		va = l2.split()
		th_u.append(float(va[0]))
		th_d.append(float(va[1]))
		l2 = f2.readline()

	## calculate histogram
	## now determine nice limits by hand:
	binwidth = 1
	xmax = np.max(np.fabs(time))
	ymax = np.max(np.fabs(counts))

	bins = np.arange(-0.5, ymax + binwidth-0.5, binwidth)
	hist_y, hist_x = np.histogram(counts, bins=bins)#, normed = True
	hist_x = bins + 0.5



	#find init pars
	first_x = hist_x[0:int(th_u[0])]
	first_y = hist_y[0:int(th_u[0])]
	second_x = hist_x[int(th_u[0]):]
	second_y = hist_y[int(th_u[0]):]

	#print th_u[0]
	#print first_x[:]
	#print second_x[0]
	#print max(first_y)
	#print max(second_y)
	##print hist_x.index(20.0)
	#print np.where(first_y==max(first_y))
	#print np.where(hist_y==max(second_y))

	a_f = max(first_y) #amplitude for first max
	o_f = np.where(first_y==max(first_y))[0][0] # offset for first max
	w_f = mt.sqrt(o_f) #width for first max
	#print array[tmp_where[0][0]]

	a_s = max(second_y)#amplitude for second max
	o_s = np.where(hist_y==max(second_y))[0][-1]# offset for second max
	w_s = mt.sqrt(o_s)#width  for first max

	#print a_f
	#print o_f
	#print w_f
	#print a_s
	#print o_s
	#print w_s

	# parameters: ampl, offset, width
	limits = [0.,False, False]
	AIC1 = []
	AIC2 = []
	print "2 Gauss fit"
	ngauss_now = 2
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss = ngauss_now, params=[a_f,o_f,w_f, a_s,o_s,w_s])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss = ngauss_now, params=[a_f,o_f,w_f, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)
	#-----------------------------------------------------------------
	#!!!!!!!!!!!!!!!!!! I can also add errors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	#-----------------------------------------------------------------
	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3]) + '&&&&&&& \\\\\n')
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4]) + '&&&&&&& \\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5]) + '&&&&&&& \\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])

	pl.subplot(2,4,1)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	#f3.write(pars)
	#print "pars"
	#print pars
	#print "n_gauss"
	#print n_gauss
	#print "errors" 
	#print errors 
	#print "chii"
	#print chii/len(hist_y)



	print "3 Gauss fit"
	ngauss_now = 3
	o_m = int((o_f+o_s)/2)
	a_m = hist_y[o_m]
	w_m = mt.sqrt(o_m)
	#print o_m
	#print a_m
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss = ngauss_now, params=[a_f,o_f,w_f, a_m,o_m,w_m, a_s,o_s,w_s])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss = ngauss_now, params=[a_f,o_f,w_f, a_m,o_m,w_m, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)
	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) + '&&&&&& \\\\\n')
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) + '&&&&&& \\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) + '&&&&&& \\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,2)
	plot_fits(hist_x, hist_y, n_gauss, pars)

	
	#print  chii/len(counts)
	#print "pars"
	#print pars
	#print "n_gauss"
	#print n_gauss
	#print "errors" 
	#print errors 
	#print "chii"
	#print chii/len(hist_y)


	print "4 Gauss fit"
	ngauss_now = 4
	o_1 = int((o_f+o_s)/4)
	a_1 = hist_y[o_1]
	w_1 = mt.sqrt(o_1)
	o_2 = int(3*(o_f+o_s)/4)
	a_2 = hist_y[o_2]
	w_2 = mt.sqrt(o_2)
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y, ngauss = ngauss_now, params=[a_f,o_f,w_f, a_1,o_1,w_1, a_2,o_2,w_2, a_s,o_s,w_s])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y, ngauss = ngauss_now, params=[a_f,o_f,w_f, a_1,o_1,w_1, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)
	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) + '&&&&& \\\\\n')
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) + '&&&&& \\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) + '&&&&& \\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,3)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	#print  chii/len(counts)

	print "5 Gauss fit"
	ngauss_now = 5
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)
	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) +  '&' + "%.3f" %(pars[12]) + '&&&& \\\\\n')
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) +  '&' + "%.3f" %(pars[3]) + '&&&& \\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) +  '&' + "%.3f" %(pars[14]) + '&&&& \\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&&\\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,4)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	#print  chii/len(counts)

	print "6 Gauss fit"
	ngauss_now = 6
	o_0 = int(o_f*0.8)
	a_0 = hist_y[o_0]
	w_0 = mt.sqrt(o_0)
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)

	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) +  '&' + "%.3f" %(pars[12]) +  '&' + "%.3f" %(pars[15]) + '&&&\\\\\n')	
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) +  '&' + "%.3f" %(pars[3]) +  '&' + "%.3f" %(pars[16]) + '&&&\\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) +  '&' + "%.3f" %(pars[14]) +  '&' + "%.3f" %(pars[17]) + '&&&\\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,5)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	
	print "7 Gauss fit"
	ngauss_now = 7
	o_n = int(o_s*1.2)
	a_n = hist_y[o_n]
	w_n = mt.sqrt(o_n)
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s, a_n,o_n,w_n])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)

	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) +  '&' + "%.3f" %(pars[12]) +  '&' + "%.3f" %(pars[15]) +  '&' + "%.3f" %(pars[18]) + '&&\\\\\n')	
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) +  '&' + "%.3f" %(pars[3]) +  '&' + "%.3f" %(pars[16]) +  '&' + "%.3f" %(pars[19]) + '&&\\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) +  '&' + "%.3f" %(pars[14]) +  '&' + "%.3f" %(pars[17]) +  '&' + "%.3f" %(pars[20]) + '&&\\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,6)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	
	print "8 Gauss fit"
	ngauss_now = 8
	o_5_1 = int((o_f+o_s)/5)
	a_5_1 = hist_y[o_5_1]
	w_5_1 = mt.sqrt(o_5_1)
	o_5_2 = int(2*(o_f+o_s)/5)
	a_5_2 = hist_y[o_5_2]
	w_5_2 = mt.sqrt(o_5_2)
	o_5_3 = int(3*(o_f+o_s)/5)
	a_5_3 = hist_y[o_5_3]
	w_5_3 = mt.sqrt(o_5_3)
	o_5_4 = int(4*(o_f+o_s)/5)
	a_5_4 = hist_y[o_5_4]
	w_5_4 = mt.sqrt(o_5_4)
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_5_1,o_5_1,w_5_1, a_5_2,o_5_2,w_5_2, a_5_3,o_5_3,w_5_3, a_5_4,o_5_4,w_5_4, a_s,o_s,w_s, a_n,o_n,w_n])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)

	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) +  '&' + "%.3f" %(pars[12]) +  '&' + "%.3f" %(pars[15]) +  '&' + "%.3f" %(pars[18]) +  '&' + "%.3f" %(pars[21]) + '& \\\\\n')	
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) +  '&' + "%.3f" %(pars[3]) +  '&' + "%.3f" %(pars[16]) +  '&' + "%.3f" %(pars[19]) +  '&' + "%.3f" %(pars[22]) + '& \\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) +  '&' + "%.3f" %(pars[14]) +  '&' + "%.3f" %(pars[17]) +  '&' + "%.3f" %(pars[20]) +  '&' + "%.3f" %(pars[23]) + '& \\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,7)
	plot_fits(hist_x, hist_y, n_gauss, pars)
	
	print "9 Gauss fit"
	ngauss_now = 9
	o_s2 = int(o_s*1.1)
	a_s2 = hist_y[o_s2]
	w_s2 = mt.sqrt(o_s2)
	(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_5_1,o_5_1,w_5_1, a_5_2,o_5_2,w_5_2, a_5_3,o_5_3,w_5_3, a_5_4,o_5_4,w_5_4, a_s,o_s,w_s, a_s2,o_s2,w_s2, a_n,o_n,w_n])
	#(pars,n_gauss,errors, chii) =  multigaussfit(hist_x[:-1],hist_y,ngauss=ngauss_now, params=[a_0,o_0,w_0, a_f,o_f,w_f, a_1,o_1,w_1, a_m,o_m,w_m, a_2,o_2,w_2, a_s,o_s,w_s], limitedmin=limits*ngauss_now)
	chi_norm = chii/len(hist_y)

	f3.write('amplitude & ' + "%.3f" %(pars[0]) + '&' + "%.3f" %(pars[3])  + '&' + "%.3f" %(pars[6]) +  '&' + "%.3f" %(pars[9]) +  '&' + "%.3f" %(pars[12]) +  '&' + "%.3f" %(pars[15]) +  '&' + "%.3f" %(pars[18]) +  '&' + "%.3f" %(pars[21]) +  '&' + "%.3f" %(pars[24]) + '\\\\\n')	
	f3.write('offset & ' + "%.3f" %(pars[1]) + '&' + "%.3f" %(pars[4])  + '&' + "%.3f" %(pars[7]) +  '&' + "%.3f" %(pars[10]) +  '&' + "%.3f" %(pars[3]) +  '&' + "%.3f" %(pars[16]) +  '&' + "%.3f" %(pars[19]) +  '&' + "%.3f" %(pars[22]) +  '&' + "%.3f" %(pars[25]) + '\\\\\n')
	f3.write('width & ' + "%.3f" %(pars[2]) + '&' + "%.3f" %(pars[5])  + '&' + "%.3f" %(pars[8]) +  '&' + "%.3f" %(pars[11]) +  '&' + "%.3f" %(pars[14]) +  '&' + "%.3f" %(pars[17]) +  '&' + "%.3f" %(pars[20]) +  '&' + "%.3f" %(pars[23]) +  '&' + "%.3f" %(pars[26]) + '\\\\\n')
	f3.write('chi&'+ "%.3f" %(chii) + '& chi norm&'+ "%.3f" %(chi_norm) + '&&&&& \\\\\\hline\n')
	
	#AIC model selection
	AIC1.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[0])
	AIC2.append(calc_AIC(chii, ngauss_now*3, len(hist_y) )[1])
	
	pl.subplot(2,4,8)
	plot_fits(hist_x, hist_y, n_gauss, pars)

	#print  chii/len(counts)
	minAIC1 = min(AIC1)
	for l in range(len(AIC1)):
		if (AIC1[l]==minAIC1):
			AIC1[l] = '\\textbf{' + "%.2f" % (AIC1[l])+'}'
		else:
			AIC1[l] = "%.2f" % (AIC1[l])
	
	AIC1_f = open('../GaussFit/PCH_modelSel/aic1Gauss_TEX.txt','a')
	AIC1_f.write('$' + inputfile[12:] +'$&'+AIC1[0]+'&'+AIC1[1]+'&'+AIC1[2]+'&'+AIC1[3]+'&'+AIC1[4]+'&'+AIC1[5]+'&'+AIC1[6]+'&'+AIC1[7]+'\\\\\n')#
	AIC1_f.close()
	 
	minAIC2 = min(AIC2)
	for l in range(len(AIC2)):
		if (AIC2[l]==minAIC2):
			AIC2[l] = '\\textbf{' + "%.2f" % (AIC2[l])+'}'
		else:
			AIC2[l] = "%.2f" % (AIC2[l])
	AIC2_f = open('../GaussFit/PCH_modelSel/aic2Gauss_TEX.txt','a')
	AIC2_f.write('$' + inputfile[12:]+'$&'+AIC2[0]+'&'+AIC2[1]+'&'+AIC2[2]+'&'+AIC2[3]+'&'+AIC2[4]+'&'+AIC2[5]+'&'+AIC2[6]+'&'+AIC2[7]+'\\\\\n')#+'&'+AIC2[5]
	AIC2_f.close()
	
	pl.savefig(outputfile)
	f3.closed
예제 #9
0
			#mask_out.write(str(wavelength_angstroms[fit_cut]))
			
			plot = fig.add_subplot(2,2,2)
			
			plt.plot(wavelength_angstroms[first_cut], cont_subtracted_img[first_cut], color="blue", label="Data")
			
		redshift_array = np.array([0.0, 0.0, 0.0])
		dispersion_array = np.array([0.0, 0.0, 0.0])
		intensity_array = np.array([0.0, 0.0, 0.0])
		peak_array = np.array([0.0, 0.0, 0.0])
		
		for index, line in enumerate(wavelengths):
			p0 = [0.25, line, 7.5]
			if max(spectrum) > 10:
				output = gaussfitter.multigaussfit(wavelength_angstroms, cont_subtracted_img, ngauss=1, err=None, params=[0.25, line, 7.5],
	    	    	fixed=[False,False,False], limitedmin=[True,False,True],
		    	    limitedmax=[False,False,True], minpars=[0,0,0], maxpars=[0,0,10.0],
					quiet=True, shh=True, veryverbose=False)
				coeff = output[0]
				gauss_line = gauss(wavelength_angstroms, *coeff)
			else:
				gauss_line = np.arange(len(spectrum))
				coeff=[0,line,7.5]
	
			redshift_array[index] = (coeff[1]-emitted_wavelengths[index])/emitted_wavelengths[index]
			dispersion_array[index] = coeff[2]
			intensity_array[index] = coeff[0]
			peak_array[index] = coeff[1]
			#print(redshift_array)
			#stop()
		
		
예제 #10
0
def gaussparty(gausspars, nspec, filenamelist, bfsmoothlist, bf_ind, amplimits,
               threshold, widlimits):
    '''
    Fits 2 or 3 gaussians to some data
    '''
    param = []
    with open(gausspars) as f1:
        for line in f1:
            if line[0] != '#':
                param.append(line.rstrip())
    #param = np.loadtxt(gausspars, comments='#')
    bffitlist = []
    bffitlist.append(0)
    gauss1 = [[] for i in range(nspec)]
    gauss2 = [[] for i in range(nspec)]
    gauss3 = [[] for i in range(nspec)]
    gauss1[0] = [0, 0]
    gauss2[0] = [0, 0]
    gauss3[0] = [0, 0]
    error_array = np.ones(len(
        bfsmoothlist[0])) * 0.01  # dummy array with 0.01 error values
    print(' ')
    print('Gaussian fit results: peak amplitude, width, rvraw, rvraw_err')
    print('-------------------------------------------------------------')
    for i in range(1, nspec):
        # check to see if we are fitting a third gaussian, i.e., one near zero
        # don't print out the result of this fit, but do return it for plotting
        # handle comments in gausspars file without exploding
        if '#' in param[i]:
            commentbegin = param[i].find('#')
            partest = param[i][0:commentbegin].split()
        else:
            partest = param[i].split()
        if len(partest) == 6: ngauss = 2
        elif len(partest) == 9: ngauss = 3
        else: print('something is wrong with your gausspars file!')
        minpars = [amplimits[0], float(partest[1]) - threshold, widlimits[0]]
        maxpars = [amplimits[1], float(partest[1]) + threshold, widlimits[1]]
        minpars.extend(
            [amplimits[2],
             float(partest[4]) - threshold, widlimits[2]])
        maxpars.extend(
            [amplimits[3],
             float(partest[4]) + threshold, widlimits[3]])
        if ngauss == 2:
            bffit = gf.multigaussfit(bf_ind,
                                     bfsmoothlist[i],
                                     ngauss=ngauss,
                                     params=partest,
                                     err=error_array,
                                     limitedmin=[True, True, False],
                                     limitedmax=[True, True, False],
                                     minpars=minpars,
                                     maxpars=maxpars,
                                     quiet=True,
                                     shh=True)
        elif ngauss == 3:
            # min and max pars for peak 3: amp, rv, width
            # this will not work if len(amplimits) < 6 or len(widlimits) < 6
            minpars.extend(
                [amplimits[4],
                 float(partest[7]) - threshold, widlimits[4]])
            maxpars.extend(
                [amplimits[5],
                 float(partest[7]) + threshold, widlimits[5]])
            bffit = gf.multigaussfit(bf_ind,
                                     bfsmoothlist[i],
                                     ngauss=ngauss,
                                     params=partest,
                                     err=error_array,
                                     limitedmin=[True, True, True],
                                     limitedmax=[True, True, True],
                                     minpars=minpars,
                                     maxpars=maxpars,
                                     quiet=True,
                                     shh=True)
        newbffit = [[] for x in range(len(bffit))]
        # Sometimes bffit[2] is None, or contains None. Set it to zeros instead.
        try:
            if not any(bffit[2]):  # this will fail if bffit[2] = None
                newbffit[0] = bffit[0]
                newbffit[1] = bffit[1]
                newbffit[2] = [0, 0, 0, 0, 0]
            else:
                newbffit = bffit
        except:
            print(
                'WARNING - gaussfit is acting up, fit failed for the next row, adjust gausspars file:'
            )
            if not bffit[2]:  # this catches the case where bffit[2] = None
                newbffit[0] = bffit[0]
                newbffit[1] = bffit[1]
                newbffit[2] = [0, 0, 0, 0, 0]
            else:
                newbffit = bffit
        bffitlist.append(newbffit)
        # NOTE: to get the gaussian fit corresponding to bfsmoothlist[i], use bffitlist[i][1].
        # RV1 for observation i is bffitlist[i][0][1] +/- bffitlist[i][2][1].
        # RV2 for observation i is bffitlist[i][0][4] +/- bffitlist[i][2][4].
        # (note: need to check if bffit[2] == None before calling bffit[2][1] or bffit[2][4])
        if ngauss == 2:
            print(
                '{0:s}    {1:.3f} {2:.2f} {3:.4f} {4:.4f} \t {5:.3f} {6:.2f} {7:.4f} {8:.4f}'
                .format(filenamelist[i], newbffit[0][0], newbffit[0][2],
                        newbffit[0][1], newbffit[2][1], newbffit[0][3],
                        newbffit[0][5], newbffit[0][4], newbffit[2][4]))
        elif ngauss == 3:
            #\t {9:.3f} {10:.2f} {11:.4f} {12:.4f}'
            print(
                '{0:s}    {1:.3f} {2:.2f} {3:.4f} {4:.4f} \t {5:.3f} {6:.2f} {7:.4f} {8:.4f}'
                .format(filenamelist[i], newbffit[0][0], newbffit[0][2],
                        newbffit[0][1], newbffit[2][1], newbffit[0][3],
                        newbffit[0][5], newbffit[0][4], newbffit[2][4])
            )  #, newbffit[0][6], newbffit[0][8], newbffit[0][7], newbffit[2][7]))
    print(' ')
    print(
        'You MUST manually guesstimate the location of each Gaussian\'s peak in %s!'
        % gausspars)
    print(
        'Until you do, the above values will be WRONG and the plot will look TERRIBLE.'
    )
    print(' ')
    return bffitlist
예제 #11
0
파일: functions.py 프로젝트: artemic/Bubbly
 def gaussianfit(self,num_zeros,params,limitedmin,limitedmax,minpars,maxpars):#wrapper for the fitting algorithm
   
   ajuste=multigaussfit(self.velocity,self.intensity,ngauss=num_zeros/2,params=params,limitedmin=limitedmin,limitedmax=limitedmax,minpars=minpars,maxpars=maxpars,quiet=True,shh=True)
   fit_params=ajuste[0][:]
   return fit_params
예제 #12
0
    def fit_110(wave, csubSI, csuberrSI, rms):

        # print("-----------")
        # print("FITTING 11.0")
        # print("-----------")

        is_SH_data = 0

        wave_feat = wave
        flux_feat = csubSI
        fluxerr_feat = csuberrSI

        if is_SH_data == 0:
            my_ind = np.where((wave_feat > 10.6) & (wave_feat < 11.8))
            w = wave_feat[my_ind]
            f = flux_feat[my_ind]
            e = fluxerr_feat[my_ind]

            amp_guess = max(f[np.where(w < 11.1)])
            params = [
                10.989,
                to_sigma(0.1538), amp_guess, 11.258,
                to_sigma(0.236),
                max(f)
            ],
            limitedmin = [True, True, True, True, True, True],
            limitedmax = [True, True, False, True, True, False],
            minpars = [
                10.97,
                to_sigma(0.12), amp_guess / 20., 11.2,
                to_sigma(0.226), 0.
            ],
            maxpars = [11.05, to_sigma(0.19), 0., 11.3, to_sigma(0.246), 0.],

            yfit = multigaussfit(w,
                                 f,
                                 err=e,
                                 ngauss=2,
                                 params=params,
                                 limitedmin=limitedmin,
                                 limitedmax=limitedmax,
                                 minpars=minpars,
                                 maxpars=maxpars,
                                 quiet=True,
                                 shh=True)

        else:
            my_ind = np.where((wave_feat > 10.6) & (wave_feat < 11.3))
            w = wave_feat[my_ind]
            f = flux_feat[my_ind]
            e = fluxerr_feat[my_ind]

            amp_guess = max(f[np.where(w < 11.1)])
            params = [
                10.989,
                to_sigma(0.1538), amp_guess, 11.258,
                to_sigma(0.236),
                max(f)
            ],
            limitedmin = [True, True, True, True, True, True],
            limitedmax = [True, True, False, True, True, False],
            minpars = [
                10.97,
                to_sigma(0.02), amp_guess / 20., 11.2,
                to_sigma(0.16), 0.
            ],
            maxpars = [11.05, to_sigma(0.19), 0., 11.3, to_sigma(0.246), 0.],

            yfit = multigaussfit(w,
                                 f,
                                 err=e,
                                 ngauss=2,
                                 params=params,
                                 limitedmin=limitedmin,
                                 limitedmax=limitedmax,
                                 minpars=minpars,
                                 maxpars=maxpars,
                                 quiet=True,
                                 shh=True)

        y1r = onedgaussian(w, 0, yfit[0][2], yfit[0][0], yfit[0][1])
        # y2r = onedgaussian(w, 0, yfit[0][5], yfit[0][3], yfit[0][4])

        # 11.0
        small_gauss_area = sp.integrate.trapz(y1r, x=w)
        position = yfit[0][0]
        sigma = yfit[0][1]
        amp = yfit[0][2]
        # position_err = yfit[2][0]
        sigma_err = yfit[2][1]
        amp_err = yfit[2][2]
        small_gauss_area_err = np.sqrt(
            (amp_err / amp)**2 + (sigma_err / sigma)**2) * small_gauss_area
        myrange = [position - (3. * sigma), position + (3. * sigma)]
        N = np.where((wave_feat >= myrange[0]) & (wave_feat <= myrange[1]))[0]
        dl = w[1] - w[0]
        measured_flux_noise110 = (rms * np.sqrt(len(N)) * dl * 2)

        # 11.2
        # gauss_area = sp.integrate.trapz(y2r, x=w)
        position = yfit[0][3]
        sigma = yfit[0][4]
        amp = yfit[0][5]
        # position_err = yfit[2][3]
        sigma_err = yfit[2][4]
        amp_err = yfit[2][5]
        # gauss_area_err = np.sqrt(
        #     (amp_err / amp)**2 + (sigma_err / sigma)**2) * gauss_area
        myrange = [position - (3. * sigma), position + (3. * sigma)]
        N = np.where((wave_feat >= myrange[0]) & (wave_feat <= myrange[1]))[0]
        dl = w[1] - w[0]
        measured_flux_noise112 = (rms * np.sqrt(len(N)) * dl * 2)

        ######################################
        new_flux_feat = f - y1r
        trap_flux_high = sp.integrate.trapz(new_flux_feat + e, x=w)
        trap_flux_low = sp.integrate.trapz(new_flux_feat - e, x=w)
        trap_flux = np.mean([trap_flux_high, trap_flux_low])
        # trap_flux_std = 0.67 * np.std([trap_flux_high, trap_flux_low])
        ######################################

        FINAL_112_FLUX = trap_flux  # full_trap_flux - small_gauss_area
        FINAL_112_FLUX_ERR = measured_flux_noise112  # + trap_flux_std
        FINAL_110_FLUX = small_gauss_area
        FINAL_110_FLUX_ERR = small_gauss_area_err + measured_flux_noise110

        return FINAL_110_FLUX, FINAL_110_FLUX_ERR, \
            FINAL_112_FLUX, FINAL_112_FLUX_ERR, myrange