def fit_skewedgauss(led_esp, led): model = SkewedGaussianModel() params = model.make_params(amplitude=1, center=400, sigma=5, gamma=1) result = model.fit(led_esp[led][:, 1], params, x=led_esp[led][:, 0], nan_policy='propagate') return result
def skewed_gaussian_fit(x, y, title_name): mod = SkewedGaussianModel() params = mod.make_params(amplitude=-2000, center=6562.801, sigma=1, gamma=0) result = mod.fit(y, params, x=x) print(result.fit_report()) #out = mod.fit(y, pars, x=x) plt.figure() plt.plot(x, y) plt.plot(x, result.best_fit) plt.title(title_name) plt.show()
def SkewedGaussianFit(x, y, plot=True): from lmfit.models import SkewedGaussianModel model = SkewedGaussianModel() params = model.make_params(amplitude=1, center=60, sigma=30, gamma=0) # adjust parameters to best fit data. result = model.fit(y, params, x=x) v = result.values plt.plot(x, result.best_fit) return v['center'], v['gamma']
def _computeLag(self, data): """ Given the output of conditional average compute the time lag corresponding to the maximum correlation of each of the structure with respect to the first. In order increase resolution we make a gaussian fit of the cross-correlation function in analogy to what done for TCV using the lmfit class and determine the center of the gaussian Parameters ---------- data xarray DataArray containing the saved Conditional Average structure Returns ------- Dictionary with keys indicating the names of the signals for which cross-correlation are saved and reporting correlation and error """ lag = np.arange(data.shape[1], dtype='float') - data.shape[1]/2. lag *= self.dt outDictionary = {} _Name = [n for n in data.sig.values if n != self.refSignal] for n in _Name: a = data.sel(sig=n).values b = data.sel(sig=self.refSignal).values xcor = np.correlate(a, b, mode='same') xcor /= np.sqrt(np.dot(a, a) * np.dot(b, b)) # the gaussian fit is not approriate for AUG # since the cross-correlation function is strongly asymmetric # we used for fit a Skewed Gaussian Distribution # This is used to estimate the error mod = SkewedGaussianModel() pars = mod.guess(xcor, x=lag) pars['sigma'].set(value=1e-5, vary=True) pars['gamma'].set(value=stats.skew(xcor),vary=True) out = mod.fit(xcor, pars, x=lag) # for a better estimate of the lag we use the computation of the # roots of the hilbert transform of the cross-correlation # function h = hilbert(xcor) S = UnivariateSpline(lag, np.imag(h), s=0) tau = S.roots()[np.argmin(np.abs(S.roots()))] outDictionary[n + '-' + self.refSignal] = {'tau': tau, 'err': out.params['center'].stderr, 'maxlag':lag[np.argmax(xcor)]} return outDictionary
def scan_dcmpitch(sgnl): line1 = '%s, %s, %.3f, %.3f, %d -- starting at %.3f\n' % \ (motor.name, sgnl, start, stop, nsteps, motor.user_readback.get()) yield from abs_set(user_ns['_locked_dwell_time'], 0.1, wait=True) yield from dcm.kill_plan() yield from mv(slits3.vsize, 3) if sgnl == 'Bicron': yield from mv(slitsg.vsize, 5) uid = yield from rel_scan(dets, motor, start, stop, nsteps) #yield from rel_adaptive_scan(dets, 'I0', motor, # start=start, # stop=stop, # min_step=0.002, # max_step=0.03, # target_delta=.15, # backstep=True) t = db[-1].table() signal = t[sgnl] if choice.lower() == 'com': position = com(signal) top = t[motor.name][position] elif choice.lower() == 'fit': pitch = t['dcm_pitch'] mod = SkewedGaussianModel() pars = mod.guess(signal, x=pitch) out = mod.fit(signal, pars, x=pitch) print(whisper(out.fit_report(min_correl=0))) out.plot() top = out.params['center'].value else: position = peak(signal) top = t[motor.name][position] yield from sleep(3.0) yield from abs_set(motor.kill_cmd, 1, wait=True) RE.msg_hook = BMM_msg_hook BMM_log_info('rocking curve scan: %s\tuid = %s, scan_id = %d' % (line1, uid, user_ns['db'][-1].start['scan_id'])) yield from mv(motor, top) if sgnl == 'Bicron': yield from mv(slitsg.vsize, gonio_slit_height)
def fit_GC_residual__(x, y, peak, peak_center): mod = SkewedGaussianModel(prefix='peak_') + LinearModel(prefix='bkg_') pars = mod.make_params() pars['peak_amplitude'].value = 1e6 pars['peak_center'].value = peak_center pars['peak_gamma'].value = 4 pars['peak_sigma'].value = 0.4 pars['bkg_intercept'].value = 1e5 # pars['bkg_slope'].value = 500 out = mod.fit(y, pars, x=x) #, iter_cb=per_iteration) if peak == 'H2': #print('Nfev = ', out.nfev) print(out.fit_report()) #print(out.pars['peak_amplitude'].value) return out
def clean(spectra, sigma=2.6): md = np.median(spectra[1]) n = int(len(spectra[0])*0.8) offset = (len(spectra[0])-n)/2 absor = md - min(spectra[1][offset:n-offset]) freq, bin = np.histogram(spectra[1], bins=50, range=(md-absor, md+absor)) rebin = [(bin[b+1]+bin[b])/2 for b in range(len(bin)-1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(value=md, vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma*out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) ist=0 pts=[] errflg = False for i in range(len(xn)): if (yn[i] > mode+var) and (errflg == False): cnt = 0 errflg = True ist = np.copy(i) cnt += 1 if (yn[i] > mode+var) and (errflg == True): cnt += 1 if (yn[i] < mode+var) and (errflg == True): pts = np.linspace(yn[ist-1], yn[i], cnt+2)[1:-1] for p in range(ist, i): yn[p] = pts[p-ist] errflg = False return np.array([xn, yn])
def clean(spectra, sigma=4.5): rej = [] for i, w in enumerate(spectra[1]): if w <= 0: rej.append(i) spectra[0] = np.delete(spectra[0], rej) spectra[1] = np.delete(spectra[1], rej) md = np.median(spectra[1]) n = int(len(spectra[0]) * 0.8) offset = (len(spectra[0]) - n) / 2 absor = md - min(spectra[1][offset:n - offset]) freq, bin = np.histogram(spectra[1], bins=30, range=(md - absor, md + absor)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma * out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) var = mode for i, w in enumerate(yn): if w > mode + var: rej.append(i) xn = np.delete(xn, rej) yn = np.delete(yn, rej) return np.array([xn, yn])
def scan_dcm_pitch(): line1 = "%s, %s, %.3f, %.3f, %d -- starting at %.3f\n" % ( pitch.name, "I0", start, stop, nsteps, pitch.readback.get(), ) uid = yield from rel_scan([I0], pitch, start, stop, nsteps) # The data that we just acquired has been cached in memory by src. # Access it as a pandas DataFrame so that we can conveniently do some # math on it. run = src.retrieve() t = run.primary.read().to_dataframe() if choice.lower() == "com": signal = numpy.array(t["I0"]) position = com(signal) top = t['pitch'].iloc[position] elif choice.lower() == "fit": signal = numpy.array(t["I0"]) pitch_ = numpy.array(t["pitch"]) mod = SkewedGaussianModel() pars = mod.guess(signal, x=pitch_) out = mod.fit(signal, pars, x=pitch_) print(out.fit_report(min_correl=0)) out.plot() top = out.params["center"].value else: signal = t['I0'] position = peak(signal) top = t[pitch.name][position] print( "rocking curve scan: %s\tuid = %s, scan_id = %d" % (line1, uid, run.metadata["start"]["scan_id"]) ) print(f"Found and moved to peak at {top:.3} via method {choice}") yield from mv(pitch, top)
def BuiltInModels(self) : FitInfo = self.FitInfo ModelString = list() for key in FitInfo['Models'] : ModelString.append((key,FitInfo['Models'][key]['model'])) for Model in ModelString : try : FitModel except : if Model[1] == 'Constant' : FitModel = ConstantModel(prefix=Model[0]+'_') if Model[1] == 'Linear' : FitModel = LinearModel(prefix=Model[0]+'_') if Model[1] == 'Gaussian' : FitModel = GaussianModel(prefix=Model[0]+'_') if Model[1] == 'SkewedGaussian' : FitModel = SkewedGaussianModel(prefix=Model[0]+'_') if Model[1] == 'Voigt' : FitModel = VoigtModel(prefix=Model[0]+'_') else : if Model[1] == 'Constant' : FitModel = FitModel + ConstantModel(prefix=Model[0]+'_') if Model[1] == 'Linear' : FitModel = FitModel + LinearModel(prefix=Model[0]+'_') if Model[1] == 'Gaussian' : FitModel = FitModel + GaussianModel(prefix=Model[0]+'_') if Model[1] == 'SkewedGaussian' : FitModel = FitModel + SkewedGaussianModel(prefix=Model[0]+'_') if Model[1] == 'Voigt' : FitModel = FitModel + VoigtModel(prefix=Model[0]+'_') self.FitModel = FitModel self.ModelParameters = FitModel.make_params()
v_model = VoigtModel() v_pars = v_model.guess(y, x=x) v_out = v_model.fit(y, v_pars, x=x) v_qc = v_out.redchi # print(v_out.fit_report(min_correl=0.25)) # other models to try do_model = DampedOscillatorModel() do_pars = do_model.guess(y, x=x) do_out = do_model.fit(y, do_pars, x=x) do_qc = do_out.redchi sg_model = SkewedGaussianModel() sg_pars = sg_model.guess(y, x=x) sg_out = sg_model.fit(y, sg_pars, x=x) sg_qc = sg_out.redchi sv_model = SkewedVoigtModel() sv_pars = sv_model.guess(y, x=x) sv_out = sv_model.fit(y, sv_pars, x=x) sv_qc = sv_out.redchi d_model = DonaichModel() d_pars = d_model.guess(y, x=x) d_out = d_model.fit(y, d_pars, x=x) d_qc = d_out.redchi # try-except for Pearson as if fails sometimes
def fit(spectra, obj, sigma=2.0, ord=4, iter=4): poly = PolynomialModel(3) pars = poly.make_params() for p in range(4): label = 'c'+str(p) pars[label].set(value=1., vary=True) wkcopy = np.copy(spectra[1]) truesp = [i for i in wkcopy if i > 5] truex = [spectra[0][i] for i in range(len(spectra[1])) if spectra[1][i] > 5] outcont = poly.fit(truesp, pars, x=truex) firstcont = outcont.eval(x=spectra[0]) xn = np.copy(spectra[0]) yn = np.copy(spectra[1])/firstcont pl1=plt.subplot((iter+1)*100+11) pl1.plot(xn, spectra[1], 'k-', linewidth=0.3) pl1.plot(xn, firstcont, 'r-', linewidth=0.6) pl1.set_ylim([0, np.mean(firstcont)*1.5]) for i in range(iter): i_=np.copy(i) niter=str(i_+1) sigma = sigma-i*0.21*sigma md = np.median(yn) n = len([i for i in yn if i > 0.1]) offset = (len(xn)-n)/2 absor = md - min(yn[offset:n-offset]) freq, bin = np.histogram(yn, bins=50, range=(md-absor, md+absor)) rebin = [(bin[b+1]+bin[b])/2 for b in range(len(bin)-1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(value=md, vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma*out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] ync = np.copy(spectra[1]) xnc = np.copy(spectra[0]) mask = [] for j in range(len(yn)): if (yn[j] > mode+var/2) or (yn[j] < mode-var/2): mask.append(False) else: mask.append(True) mask = np.array(mask) ync = ync[mask] xnc = xnc[mask] poly2 = PolynomialModel(ord) pars2 = poly2.make_params() for p in range(ord+1): label = 'c'+str(p) pars2[label].set(value=1., vary=True) outcont2 = poly2.fit(ync, pars2, x=xnc) contf = outcont2.eval(x=xn) yn = spectra[1]/contf err = spectra[2]/contf pln=plt.subplot(int((iter+1)*100+10+(i_+2))) pln.plot(xn, yn*(np.mean(contf)*0.8), 'k-', linewidth=0.3) pln.plot(xnc, ync, 'r-', linewidth=0.3) pln.plot(xn, contf, 'b-', linewidth=0.6) pln.set_ylim([0, np.mean(contf)*1.2]) plt.savefig(obj[0]+'_fit.png', dpi=300) plt.clf() return np.array([xn, yn, err])
if savefigures==True: plt.savefig('PDF1-modified.pdf') plt.show() n_hist, b_hist, patches_hist = plt.hist(hist_values1,bins=200,normed=1,edgecolor="black",facecolor="black",histtype="step",label="PDF") plt.hist(hist_values1,bins=200,normed=1,facecolor="black",edgecolor='None',alpha=0.1,label="PDF") plt.xlabel('Midpoint Phase Position') plt.ylabel('Normalised PDF') if savefigures == True: plt.savefig('plottemp.pdf') bin_max = np.where(n_hist == n_hist.max()) print "Mode:", b_hist[bin_max][0] ### CONFIDENCE INTERVAL SELECTOR: ######################################## bin_heights, bin_borders, _ = n_hist, b_hist, patches_hist bin_center = bin_borders[:-1] + np.diff(bin_borders) / 2 xvals, yvals = bin_center, bin_heights model = SkewedGaussianModel() params = model.guess(yvals, x=xvals) result = model.fit(yvals, params, x=xvals) print result.fit_report() plt.plot(xvals, result.best_fit,c='c',lw=2) #Mode Finder: maxval=0 maxvalx=0 for i in range(len(xvals)): if result.best_fit[i]>maxval: maxval=result.best_fit[i] maxvalx=xvals[i] print "Curve Mode:", maxvalx
def fit(spectra, sigma=4.0, ptreg=8., ord=4, iter=2): rej = [] for i, w in enumerate(spectra[1]): if w <= 0: rej.append(i) spectra[0] = np.delete(spectra[0], rej) spectra[1] = np.delete(spectra[1], rej) # prepare first kick poly = PolynomialModel(3) pars = poly.make_params() for p in range(4): label = 'c' + str(p) pars[label].set(value=1., vary=True) wkcopy = np.copy(spectra[1]) truesp = [i for i in wkcopy if i >= 0] truex = [ spectra[0][i] for i in range(len(spectra[1])) if spectra[1][i] >= 0 ] outcont = poly.fit(truesp, pars, x=truex) firstcont = outcont.eval(x=spectra[0]) xn = np.copy(spectra[0]) yn = np.copy(spectra[1]) / firstcont # start cont. cleaning iterations for i in range(iter): i_ = np.copy(i) niter = str(i_ + 1) sigma = sigma - i * 0.21 * sigma md = np.median(yn) n = len([i for i in yn if i > 0.1]) offset = (len(xn) - n) / 2 absor = md - min(yn[offset:n - offset]) freq, bin = np.histogram(yn, bins=50, range=(md - absor, md + absor)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] gauss = SkewedGaussianModel() pars = gauss.make_params() pars['center'].set(vary=True) pars['amplitude'].set(vary=True) pars['sigma'].set(vary=True) pars['gamma'].set(vary=True) out = gauss.fit(freq, pars, x=rebin) var = sigma * out.best_values['sigma'] xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] # clean cont. ync = np.copy(spectra[1]) xnc = np.copy(spectra[0]) mask = [] for j in range(len(yn)): if (yn[j] > mode + var / 2) or (yn[j] < mode - var / 2): mask.append(False) else: mask.append(True) mask = np.array(mask) ync = ync[mask] xnc = xnc[mask] # re-fitting poly2 = PolynomialModel(ord) pars2 = poly2.make_params() for p in range(ord + 1): label = 'c' + str(p) pars2[label].set(value=1., vary=True) try: outcont2 = poly2.fit(ync, pars2, x=xnc) except: plt.plot(xn, yn, 'k-') plt.plot([xn[0], xn[-1]], [mode, mode], 'b-') plt.plot([xn[0], xn[-1]], [mode + var / 2, mode + var / 2], 'r-') plt.plot([xn[0], xn[-1]], [mode - var / 2, mode - var / 2], 'r-') plt.show() contf = outcont2.eval(x=xn) yn = spectra[1] / contf clspec = [xnc, ync] # start slicing firstv = clspec[0][0] wavrange = clspec[0][-1] - firstv sliceno = wavrange / ptreg slisize = wavrange / sliceno points = [[], []] # continuum point definition for s in range(int(sliceno)): i = bissec(clspec[0], firstv + s * slisize) f = bissec(clspec[0], firstv + (s + 1) * slisize) slc = [clspec[0][i:f], clspec[1][i:f]] if len(slc[1]) > 2.: md = np.median(slc[1]) absor = min(slc[1]) high = max(slc[1]) freq, bin = np.histogram(slc[1], bins=20, range=(absor, high)) rebin = [(bin[b + 1] + bin[b]) / 2 for b in range(len(bin) - 1)] fmode = rebin[list(freq).index(max(freq))] fsigma = rebin[-1] - rebin[0] gauss = GaussianModel() pars = gauss.make_params() pars['center'].set(value=fmode, vary=True) pars['amplitude'].set(value=max(freq), vary=True) pars['sigma'].set(value=fsigma, vary=True) out = gauss.fit(freq, pars, x=rebin) xrbn = np.linspace(rebin[0], rebin[-1], num=100) yrbn = list(out.eval(x=xrbn)) mode = xrbn[yrbn.index(max(yrbn))] xp = slc[0][len(slc[0]) / 2] points[0].append(xp) points[1].append(mode) spline = splrep(points[0], points[1], k=3) contx = splev(clspec[0], spline) continuum = splev(spectra[0], spline) return [spectra[0], spectra[1] / continuum]
def convert_catalog(cat_table): ''' This function selects stars from a size-mag catalogue Inputs: cat_table: directory of catalogue of all sources on an image. Must contain FWHM_IMAGE or FLUX_RADIUS; MAG_APER or FLUX_APER; FLAGS; VIGNET; etc cat_4PSFEx_table: FITS_LDAC catalogue ready to be processed by PSFEx. ''' path = os.getcwd() direc = cat_table.split('/')[-1] if not os.path.exists(direc): os.makedirs(direc) os.chdir(direc) hdu = p.open(cat_table) data = hdu[2].data reff = data['FLUX_RADIUS'] flux = data['FLUX_APER'] mags = 30 - 2.5 * n.log10(flux) flags = data['FLAGS'] mask = n.where((flux > 0) & (flags < 4) & (reff > 0))[0] reff1 = reff[mask] mags1 = mags[mask] s = n.argsort(mags1) mags1 = mags1[s] reff1 = reff1[s] perc = 0.1 i = 1 medians = [] mad_stds = [] gammas = [] chisq = [] centers = [] sigmas = [] while perc < 5: mask2 = n.where(mags1 < n.percentile(mags1, perc))[0] reff2 = reff1[mask2] mags2 = mags1[mask2] # N,bins,patches=hist(reff2,bins='scotts',label='clean, n_obj='+str(len(reff2)),normed=True,histtype='step') # hist(reff1,bins=bins,label='not so clean, n_obj='+str(len(reff1)),normed=True,histtype='step') # hist(reff,bins=bins,label='not clean at all, n_obj='+str(len(reff)),normed=True,histtype='step') # pl.legend() # pl.xlim(bins[0],bins[-1]) # pl.savefig('hist_'+str(i)+'.png') # pl.clf() # print 'done hist' medians.append(n.median(reff2)) mad_stds.append(mad_std(reff2)) N, bins, patches = hist(reff2, bins='scotts', histtype='stepfilled', color='g', normed=False) pl.axvline(n.median(reff2), color='red') pl.axvline(n.median(reff2) + mad_std(reff2), color='black') pl.axvline(n.median(reff2) - mad_std(reff2), color='black') pl.axvline(n.median(reff2) + n.std(reff2), color='grey') pl.axvline(n.median(reff2) - n.std(reff2), color='grey') X = reff2[:, n.newaxis] X_plot = n.linspace(min(X)[0], max(X)[0], 10000)[:, n.newaxis] kde = KDE(kernel='gaussian', bandwidth=min(mad_stds)).fit(X) ld = kde.score_samples(X_plot) pl.plot(X_plot[:, 0], n.exp(ld) * min(mad_stds) * len(reff2), lw=3) # pl.xscale('log') # pl.yscale('log') ''' model = SkewedGaussianModel() x = n.array([0.5 * (bins[i] + bins[i+1]) for i in xrange(len(bins)-1)]) pars = model.guess(N, x=x) result=model.fit(N,pars,x=x) print(result.fit_report()) pl.plot(x, result.best_fit,'k--',lw=3) ''' pl.savefig('hist_clean_log_' + str(i) + '.png') pl.clf() mask3 = n.where((reff2 >= n.median(reff2) - mad_std(reff2)) & (reff2 <= n.median(reff2) + mad_std(reff2)))[0] reff3 = reff2[mask3] N, bins, patches = hist(reff3, bins='scotts', histtype='stepfilled', color='g', alpha=.5, normed=True) x = n.array( [0.5 * (bins[j] + bins[j + 1]) for j in xrange(len(bins) - 1)]) #X=reff3[:,n.newaxis] x_plot = n.linspace(min(bins), max(bins), 100) #kde=KDE(kernel='epanechnikov',bandwidth=min(mad_stds)).fit(X) #ld=kde.score_samples(X_plot) #pl.plot(X_plot[:,0],n.exp(ld),lw=3) model = SkewedGaussianModel() pars = model.guess(N, x=x) result = model.fit(N, pars, x=x) gammas.append(result.params['gamma'].value) chisq.append(result.redchi) centers.append(result.params['center'].value) sigmas.append(result.params['sigma'].value) #print result.fit_report() pl.plot(x_plot, result.eval(x=x_plot), 'k--', lw=3) # pl.axvline(result.params['center'].value,color='red') # pl.axvline(result.params['center'].value+result.params['sigma'].value,color='grey') # pl.axvline(result.params['center'].value-result.params['sigma'].value,color='grey') pl.axvline(result.params['center'].value, color='red') pl.axvline(result.params['center'].value + result.params['sigma'].value, color='black') pl.axvline(result.params['center'].value - result.params['sigma'].value, color='black') mu, sigma = norm.fit(reff3, loc=max(result.eval(x=x_plot)), scale=result.params['sigma'].value) pdf = norm.pdf(x_plot, loc=mu, scale=sigma) pl.plot(x_plot, pdf, color='yellow') pl.savefig('hist_stellarseq_' + str(i) + '.png') pl.clf() print 'done hist stellarseq' pl.plot(reff, mags, 'b.', label='not clean at all', alpha=.5) #pl.plot(reff1,mags1,'k.',label='not so clean',alpha=.5) pl.plot(reff2, mags2, 'r.', label='clean, mag_lims = ' + str(n.min(mags2)) + ', ' + str(n.max(mags2)), alpha=.5) pl.axvline(n.median(reff2), color='green') pl.axhline(n.max(mags2), color='black') pl.xlim([0, 20]) pl.ylim([n.percentile(mags1, 99), n.percentile(mags1, 0)]) pl.legend() pl.savefig('magsize_' + str(i) + '.png') pl.clf() print 'done all' perc = perc + 0.05 print 'end loop ' + str(i) i = i + 1 pl.clf() f, axes = pl.subplots(2, 3, sharex=True) names = ('median', 'mad_std', 'gamma', 'redchi', 'center', 'sigma') for ax, ind, name in zip( axes.flat, (medians, mad_stds, gammas, chisq, centers, sigmas), names): ax.plot(ind, 'o') ax.set_title(name) # plt.show() # pl.plot(medians/n.max(medians),'o',label='median') # pl.plot(mad_stds/n.max(mad_stds),'o',label='mad_std') # pl.plot(gammas/n.max(gammas),'o',label='gammas') # pl.plot(chisq/n.max(chisq),'o',label='chisqr') # pl.legend(loc='best') pl.savefig('param_variations.png') pl.clf() os.chdir(path) '''
def convert_catalog(cat_table): ''' This function selects stars from a size-mag catalogue Inputs: cat_table: directory of catalogue of all sources on an image. Must contain FWHM_IMAGE or FLUX_RADIUS; MAG_APER or FLUX_APER; FLAGS; VIGNET; etc cat_4PSFEx_table: FITS_LDAC catalogue ready to be processed by PSFEx. ''' path=os.getcwd() direc=cat_table.split('/')[-1] if not os.path.exists(direc): os.makedirs(direc) os.chdir(direc) hdu=p.open(cat_table) data=hdu[2].data reff=data['FLUX_RADIUS'] flux=data['FLUX_APER'] mags=30-2.5*n.log10(flux) flags=data['FLAGS'] mask = n.where((flux > 0) & (flags < 4) & (reff>0))[0] reff1=reff[mask] mags1=mags[mask] s=n.argsort(mags1) mags1=mags1[s] reff1=reff1[s] perc=0.1 i=1 medians=[] mad_stds=[] gammas=[] chisq=[] centers=[] sigmas=[] while perc<5: mask2=n.where(mags1<n.percentile(mags1,perc))[0] reff2=reff1[mask2] mags2=mags1[mask2] # N,bins,patches=hist(reff2,bins='scotts',label='clean, n_obj='+str(len(reff2)),normed=True,histtype='step') # hist(reff1,bins=bins,label='not so clean, n_obj='+str(len(reff1)),normed=True,histtype='step') # hist(reff,bins=bins,label='not clean at all, n_obj='+str(len(reff)),normed=True,histtype='step') # pl.legend() # pl.xlim(bins[0],bins[-1]) # pl.savefig('hist_'+str(i)+'.png') # pl.clf() # print 'done hist' medians.append(n.median(reff2)) mad_stds.append(mad_std(reff2)) N,bins,patches=hist(reff2,bins='scotts',histtype='stepfilled',color='g',normed=False) pl.axvline(n.median(reff2),color='red') pl.axvline(n.median(reff2)+mad_std(reff2),color='black') pl.axvline(n.median(reff2)-mad_std(reff2),color='black') pl.axvline(n.median(reff2)+n.std(reff2),color='grey') pl.axvline(n.median(reff2)-n.std(reff2),color='grey') X=reff2[:,n.newaxis] X_plot=n.linspace(min(X)[0],max(X)[0],10000)[:,n.newaxis] kde=KDE(kernel='gaussian',bandwidth=min(mad_stds)).fit(X) ld=kde.score_samples(X_plot) pl.plot(X_plot[:,0],n.exp(ld)*min(mad_stds)*len(reff2),lw=3) # pl.xscale('log') # pl.yscale('log') ''' model = SkewedGaussianModel() x = n.array([0.5 * (bins[i] + bins[i+1]) for i in xrange(len(bins)-1)]) pars = model.guess(N, x=x) result=model.fit(N,pars,x=x) print(result.fit_report()) pl.plot(x, result.best_fit,'k--',lw=3) ''' pl.savefig('hist_clean_log_'+str(i)+'.png') pl.clf() mask3=n.where((reff2 >= n.median(reff2)-mad_std(reff2)) & (reff2 <= n.median(reff2)+mad_std(reff2)))[0] reff3=reff2[mask3] N,bins,patches=hist(reff3,bins='scotts',histtype='stepfilled',color='g',alpha=.5,normed=True) x = n.array([0.5 * (bins[j] + bins[j+1]) for j in xrange(len(bins)-1)]) #X=reff3[:,n.newaxis] x_plot=n.linspace(min(bins),max(bins),100) #kde=KDE(kernel='epanechnikov',bandwidth=min(mad_stds)).fit(X) #ld=kde.score_samples(X_plot) #pl.plot(X_plot[:,0],n.exp(ld),lw=3) model = SkewedGaussianModel() pars = model.guess(N, x=x) result=model.fit(N,pars,x=x) gammas.append(result.params['gamma'].value) chisq.append(result.redchi) centers.append(result.params['center'].value) sigmas.append(result.params['sigma'].value) #print result.fit_report() pl.plot(x_plot, result.eval(x=x_plot),'k--',lw=3) # pl.axvline(result.params['center'].value,color='red') # pl.axvline(result.params['center'].value+result.params['sigma'].value,color='grey') # pl.axvline(result.params['center'].value-result.params['sigma'].value,color='grey') pl.axvline(result.params['center'].value,color='red') pl.axvline(result.params['center'].value+result.params['sigma'].value,color='black') pl.axvline(result.params['center'].value-result.params['sigma'].value,color='black') mu,sigma=norm.fit(reff3,loc=max(result.eval(x=x_plot)),scale=result.params['sigma'].value) pdf=norm.pdf(x_plot,loc=mu,scale=sigma) pl.plot(x_plot,pdf,color='yellow') pl.savefig('hist_stellarseq_'+str(i)+'.png') pl.clf() print 'done hist stellarseq' pl.plot(reff,mags,'b.',label='not clean at all',alpha=.5) #pl.plot(reff1,mags1,'k.',label='not so clean',alpha=.5) pl.plot(reff2,mags2,'r.',label='clean, mag_lims = '+str(n.min(mags2))+', '+str(n.max(mags2)),alpha=.5) pl.axvline(n.median(reff2),color='green') pl.axhline(n.max(mags2),color='black') pl.xlim([0,20]) pl.ylim([n.percentile(mags1,99),n.percentile(mags1,0)]) pl.legend() pl.savefig('magsize_'+str(i)+'.png') pl.clf() print 'done all' perc=perc+0.05 print 'end loop '+str(i) i=i+1 pl.clf() f, axes = pl.subplots(2,3,sharex=True) names=('median','mad_std','gamma','redchi','center','sigma') for ax,ind,name in zip(axes.flat,(medians,mad_stds,gammas,chisq,centers,sigmas),names): ax.plot(ind,'o') ax.set_title(name) # plt.show() # pl.plot(medians/n.max(medians),'o',label='median') # pl.plot(mad_stds/n.max(mad_stds),'o',label='mad_std') # pl.plot(gammas/n.max(gammas),'o',label='gammas') # pl.plot(chisq/n.max(chisq),'o',label='chisqr') # pl.legend(loc='best') pl.savefig('param_variations.png') pl.clf() os.chdir(path) '''