def delta_nu(self, use_errors=False, plot=False, quiet=False): """ To obtain delta_nu and epsilon, we perform a leastsquares fit to the radial (l = 0) frequencies as a function of n. By the asymptotic relation, the gradient of this fit is delta_nu and the intercept is delta_nu * epsilon. """ from linfit import linfit f = self.mx[:,0] # l=0 frequencies f = f[f != 0] # only non-zero ones err = self.mxErr[:,0] # l=0 frequency errors err = err[err != 0] try: n = self.nvector[:len(f)] except AttributeError: print 'There is no information on the radial orders of the frequencies\n' print 'Fit is made assuming consecutive values' n = range(1,len(f)+1) if(use_errors): a, dnu, dic = linfit(n, f, err=err, use_err=True, full_output=True, plot=plot) else: a, dnu, dic = linfit(n,f,full_output=True, plot=plot) if (not quiet): print 'Fit statistics (nu = a + b*n):' print ' b (=dnu) = ' + str(dnu) + ' +/- ' + str(dic['SEb']) print ' R^2 = ' + str(dic['r2']) self.dnu = dnu
def get_linregs_of_long_force(longitudination_orig, force, trans_index): pivot_long = [l for l in longitudination_orig[0:trans_index+1]] pivot_force = [f for f in force[0:trans_index+1]] m = linfit.linfit(pivot_long, pivot_force)[0][0] t = linfit.linfit(pivot_long, pivot_force)[0][1] return m, t
def param(sp,wvlin): " Calculates the parameters from a spectrum." from linfit import linfit from Sp_parameters import nanmasked, norm2max, smooth, deriv, find_closest npar = 16 spc, mask = nanmasked(sp) wvl = wvlin[mask] try: norm = norm2max(spc) except ValueError: par = np.zeros(npar)*np.nan return par [i1000,i1077,i1493,i1600,i1200,i1300,i530,i610, i1565,i1634,i1193,i1198,i1236,i1248,i1270,i1644, i1050,i1040,i1065,i600,i870,i515] = find_closest(wvl,np.array([1000,1077,1493,1600,1200,1300,530, 610,1565,1634,1193,1198,1236,1248, 1270,1644,1050,1040,1065,600,870,515])) if np.isnan(spc[i1000]) or not spc[i1000]: par = np.zeros(npar)*np.nan return par norm2 = spc/spc[i1000] dsp = smooth(deriv(norm2,wvl/1000),2,nan=False) imaxwvl = np.argmax(spc) maxwvl = wvl[mask[imaxwvl]] # now calculate each parameter fit0 = np.polyfit(np.array([wvl[i1000],wvl[i1077]]),np.array([norm2[i1000],norm2[i1077]]),1) fit0_fn = np.poly1d(fit0) fit7 = np.polyfit(np.array([wvl[i1493],wvl[i1600]]),np.array([norm2[i1493],norm2[i1600]]),1) fit7_fn = np.poly1d(fit7) fit8,z = linfit(wvl[i1000:i1077],dsp[i1000:i1077]) fit9,z = linfit(wvl[i1200:i1300],dsp[i1200:i1300]) fit10,z = linfit(wvl[i530:i610]/1000,norm[i530:i610]) fit14,z = linfit(wvl[i1565:i1634],spc[i1565:i1634]/norm[i1565]) par = [sum(norm2[i1000:i1077]-fit0_fn(wvl[i1000:i1077])), # 1 curvature of rad normed to 1000 nm for 1000 nm - 1077 nm dsp[i1198], # 2 deriv of rad normed to 1000 nm at 1198 nm (!=IDL version) dsp[i1493], # 3 deriv of rad normed to 1000 nm at 1493 nm norm[i1198]/norm[i1236], # 4 ratio of normalized rad of 1198 nm / 1236 nm np.nanmean(norm[i1248:i1270]), # 5 mean of normalized rad between 1248 nm - 1270 nm np.nanmean(norm[i1565:i1644]), # 6 mean of normalized rad between 1565 nm - 1644 nm np.nanmean(norm[i1000:i1050]), # 7 mean of normalized rad between 1000 nm - 1050 nm sum(norm2[i1493:i1600]-fit7_fn(wvl[i1493:i1600])), # 8 curvature of rad normed to 1000 nm for 1493 nm - 1600 nm fit8[0], # 9 slope of deriv of rad normed to 1000 nm, 1000 nm - 1077 nm fit9[0], # 10 slope of deriv of rad normed to 1000 nm, 1200 nm - 1300 nm fit10[0], # 11 slope of normalized radiance between 530 nm - 610 nm norm[i1040], # 12 normalized radiance at 1040 nm norm[i1000]/norm[i1065], # 13 ratio of normalized radiance at 1000 nm / 1065 nm norm[i600]/norm[i870], # 14 ratio of normalized radiance at 600 nm / 870 nm np.nanmin([0.003,fit14[0]]), # 15 slope of radiance / rad at 1565 between 1565 nm - 1634 nm spc[i515]] # 16 radiance at 515 nm # do a check for bad points if np.all(np.isnan(par[0:13])): par[14] = np.nan par[15] = np.nan return par
def calc_slope(y, x, y_sig): """ Determine the least-squares linear fit between two arrays. """ z = linfit.linfit(y, x, y_sig) #z = np.polyfit(x, y, 1, full=True) return z
def test_linalg_lstsqNoWt(self): # check that get same answers as Scipy linalg_lstsq when all weighting # is turned off. npts = 100 x, y, dy = randomData(100., npts) fit = linfit(x, y) X = np.vstack([np.ones(npts), x]).T cNoWts, resid, rank, sigma = lstsq(X, y) diff_fit = np.abs((np.flipud(cNoWts)-fit)/fit).sum()/2.0 self.assertTrue(diff_fit<EPS2)
def test_linalg_lstsqWt(self): # check that get same answers as Scipy linalg_lstsq using weighting npts = 100 x, y, dy = randomData(100., npts) fit = linfit(x, y, sigmay=dy) X = np.vstack([np.ones(npts), x]).T A = X/np.array(zip(dy,dy)) cWts, resid, rank, sigma = lstsq(A, y/dy) diff_fit = np.abs((np.flipud(cWts)-fit)/fit).sum()/2.0 self.assertTrue(diff_fit<EPS2)
def main(): # from here ???? base = 'data' # elements to build file names suff = '.txt' ndat = 5 fMOD = ftest # set up figure ftsz = 8 fig = plt.figure(num=1, figsize=(14, 8), dpi=100, facecolor='white') for m in range(ndat): print('m=%1i' % (m)) x, y, sig = readdata("%s%1i%s" % (base, m, suff)) a_lf, siga_lf, chi2_lf, q_lf = linfit.linfit(x, y, sig) a_glf, siga_glf, chi2_glf, q_glf = linfit.glinfit(x, y, sig, 3, fMOD) y_lf = a_lf[0] + a_lf[1] * x y_glf = np.zeros(x.size) for j in range(a_glf.size): y_glf[:] = y_glf[:] + a_glf[j] * (fMOD(x[:]))[j] ax = fig.add_subplot(2, ndat, m + 1) ax.errorbar(x, y, yerr=sig, fmt='o', linewidth=1) plt.title('%s%i' % (base, m)) ax.plot(x, y_lf, linewidth=1, linestyle='-') for j in range(a_lf.size): ax.annotate('a[%1i]=%10.2e+-%10.2e' % (j, a_lf[j], siga_lf[j]), xy=(0, 0), xytext=(0.13 + 0.16 * m, 0.85 - float(j) * 0.03), fontsize=ftsz, textcoords='figure fraction') ax.annotate('$\chi^2$=%10.2e\nq=%10.2e' % (chi2_lf, q_lf), xy=(0, 0), xytext=(0.19 + 0.16 * m, 0.60), fontsize=ftsz, textcoords='figure fraction') ax = fig.add_subplot(2, ndat, ndat + m + 1) ax.errorbar(x, y, yerr=sig, fmt='o', linewidth=1) plt.title('%s%i' % (base, m)) ax.plot(x, y_glf, linewidth=1, linestyle='-') for j in range(a_glf.size): ax.annotate('a[%1i]=%10.2e+-%10.2e' % (j, a_glf[j], siga_glf[j]), xy=(0, 0), xytext=(0.13 + 0.16 * m, 0.42 - float(j) * 0.03), fontsize=ftsz, textcoords='figure fraction') ax.annotate('$\chi^2$=%10.2e\nq=%10.2e' % (chi2_glf, q_glf), xy=(0, 0), xytext=(0.19 + 0.16 * m, 0.15), fontsize=ftsz, textcoords='figure fraction') plt.show()
def test_pfits(self): # check that perfect straight lines are fit perfectly x = np.random.uniform(-100.0, 100.0, 10) a, b = np.random.rand(2) y = a*x+b fit, cvm, redchisq, residuals = linfit(x, y, cov=True, chisq=True, residuals=True) dfit = [np.sqrt(cvm[i,i]) for i in range(2)] diffa, diffb = np.abs(a-fit[0]), np.abs(b-fit[1]) self.assertTrue(diffa<EPS1 and diffb<EPS1) self.assertTrue(np.abs(dfit[0])<EPS1 and np.abs(dfit[1])<EPS1) self.assertTrue(np.abs(cvm[0,1])<EPS1*EPS1) self.assertTrue(redchisq<EPS1*EPS1)
def calc_angs(time,w,aod,flag): 'Program to calculate the angstrom exponent by fitting linearly on the aod' ang = np.zeros_like(time) for i,t in enumerate(time): if not flag[i]==1: c,cm = linfit(np.log10(w),-np.log10(aod[:,i])) p = np.array([c[1],c[0]]) ang[i] = c[0] else: ang[i] = np.nan if (ang[i]<1.0) & (aod[2,i]>0.8): ang[i] = np.nan return ang
def get_trend_pivot(X, Y): fit = linfit.linfit(X,Y) m_pivot = fit[0][0] t_pivot = fit[0][1] # 2nd bunch = return_of_linfit[-1] # 3rd [da,db] = bunch.fiterr bunch = fit[-1] fiterr = bunch.fiterr dm_pivot = fiterr[0] dt_pivot = fiterr[1] return m_pivot, t_pivot, dm_pivot, dt_pivot
def test_polyfitWtInd(self): # check that get same answers as NumPy polyfit when relative weighting # (only type of weighting currently available in polyfit) is used. # Also compare run times. npts = 100 x, y, dy = randomData(100., npts) wts = 1./dy fit, cvm = linfit(x, y, sigmay=dy, relsigma=True, cov=True) pfit, v = polyfit(x, y, 1, w=wts, cov=True) diff_fit = np.abs((pfit - fit)/fit).sum()/2.0 # polyfit uses a nonstandard normalization of the covariance # matrix. It is corrected here for comparison with linfit, which # uses the standard definition. v_corrected = v * (npts-4.)/(npts-2.) diff_cvm = np.abs((v_corrected - cvm)/cvm).sum()/4.0 self.assertTrue(diff_fit<EPS2) self.assertTrue(diff_cvm<EPS2)
def slopedec(X, Y, window_fraction): ### calculates changes in slope ### slopes1 = [] slopes2 = [] for (pos, x) in enumerate(X): slope1 = 0 if pos < len(X) - window_fraction: slope1 = linfit.linfit( X[pos:pos + window_fraction], Y[pos:pos + window_fraction])[0][0] slopes1.append(slope1) for (pos, slope1) in enumerate(slopes1): if pos >= 1 and pos < len(slopes1): slope2 = 0 slope2 = slopes1[pos] - slopes1[pos - 1] slopes2.append(slope2) nd_slopes2 = np.asarray(slopes2) return nd_slopes2
import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec # for unequal plot boxes from linfit import linfit # data set for linear fitting x = np.array([2.3, 4.7, 7.1, 9.6, 11.7, 14.1, 16.4, 18.8, 21.1, 23.0]) y = np.array([-25., 3., 110., 110., 230., 300., 270., 320., 450., 400.]) sigmay = np.array([15., 30., 30., 40., 40., 50., 40., 30., 50., 30.]) # Fit linear data set with weighting fit, cvm, redchisq, residuals = linfit(x, y, sigmay, cov=True, relsigma=False, chisq=True, residuals=True) dfit = [np.sqrt(cvm[i,i]) for i in range(2)] # Open figure window for plotting data with linear fit fig1 = plt.figure(1, figsize=(8,8)) gs = gridspec.GridSpec(2, 1, height_ratios=[2.5, 6]) # Bottom plot: data and fit ax1 = fig1.add_subplot(gs[1]) # Plot data with error bars on top of fit ax1.errorbar(x, y, yerr = sigmay, ecolor="black", fmt="ro", ms=5) # Plot fit (behind data) endt = 0.05 * (x.max()-x.min()) tFit = np.array([x.min()-endt, x.max()+endt]) vFit = fit[0]*tFit + fit[1] ax1.plot(tFit, vFit, "-b", zorder=-1)
from __future__ import division, print_function, absolute_import import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec # for unequal plot boxes from linfit import linfit # data set for linear fitting x = np.array([2.3, 4.7, 7.1, 9.6, 11.7, 14.1, 16.4, 18.8, 21.1, 23.0]) y = np.array([-25., 3., 110., 110., 230., 300., 270., 320., 450., 400.]) sigmay = np.array([15., 30., 30., 40., 40., 50., 40., 30., 50., 30.]) # Fit linear data set with weighting fit, cvm, info = linfit(x, y, sigmay, relsigma=False, return_all=True) dfit = np.sqrt(np.diag(cvm)) # uncertainty estimates for fitting parameters # Open figure window for plotting data with linear fit fig = plt.figure(1, figsize=(8, 8)) gs = gridspec.GridSpec(2, 1, height_ratios=[2.5, 6]) # Bottom plot: data and fit ax1 = fig.add_subplot(gs[1]) # Plot data with error bars ax1.errorbar(x, y, yerr=sigmay, ecolor='k', mec='k', fmt='oC3', ms=6) # Plot fit (behind data) endx = 0.05 * (x.max() - x.min()) xFit = np.array([x.min() - endx, x.max() + endx]) yFit = fit[0] * xFit + fit[1] ax1.plot(xFit, yFit, '-', zorder=-1)
def get_slope_elast(strain_true, stress_true, trans_elast_plast_index): slope_elast = linfit.linfit(strain_true[:trans_elast_plast_index+1], stress_true[:trans_elast_plast_index+1])[0][0] return slope_elast
pu.plot_lin(smooth(er2_vs[er2_vs>2],20),smooth(er2['GPS_Altitude'][1:][er2_vs>2],20)) pu.plot_lin(smooth(er2_vs[er2_vs<0],20),smooth(er2['GPS_Altitude'][1:][er2_vs<0],20),color='r') plt.xlabel('Vertical speed [m/s]') plt.ylabel('Altitude [m]') plt.title('ER2 vertical speed from SEAC4RS') plt.legend(frameon=False) plt.savefig(fp+'ER2_vert_speed.png',dpi=600,transparent=True) # Get the inverse relationship for alt to vert speed # In[11]: import linfit v = linfit.linfit(smooth(er2['GPS_Altitude'][1:][er2_vs>2],20),smooth(er2_vs[er2_vs>2],20)) # In[12]: slope = v[0][0] intercept = v[0][1] # In[13]: slope,intercept
def lc_plots_ddoti(datafile, radecfile, timefile, t0, nmin=2, nmax=100): """ allow a maximum of nmax sources through if present not just in the stack, must be present at least nmin times """ ofile = datafile + '.summary.txt' of = open(ofile, 'w') of.write( "# id RA DEC mag dmag fwhm slope dslope chi2 n_detect\n" ) data = loadtxt(datafile, ndmin=2).T if (len(data[0]) == 0): of.close() sys.exit() id, mag, dmag, fwhm, expos, epoch, part = data id = id.astype('int32') epoch = epoch.astype('int32') gps0 = ut2gps(t0) times = loadtxt(timefile, dtype='string', usecols=(0, ), ndmin=1) gps = ut2gps(times) - gps0 expos = loadtxt(timefile, usecols=(1, ), ndmin=1) gps0 = 0. if (gps.min() < expos.min()): gps0 += 3600. gps += expos / 2. + gps0 gps /= 3600. expos /= 3600. gps = hstack((gps.mean(), gps)) expos = hstack((gps.max() - gps.min() + expos.mean(), expos)) nepoch = len(unique(epoch)) - 1 cat_dat = loadtxt(radecfile, ndmin=2).T if (len(cat_dat[0]) == 0): of.close() sys.exit() ra, dec = cat_dat[:2] remaining = ones(len(ra), dtype='bool') uid = unique(id) i0 = 1 for id0 in uid[:nmax]: h = id == id0 n, p, m, dm, f = epoch[h], part[h], mag[h], dmag[h], fwhm[h] # get rid of multiple detections in an epoch good = ones(len(n), dtype='bool') n1 = unique(n[n > 0]) for nn in n1: h = n == nn if (h.sum() > 0): good[h] = m[h] <= m[h].min() if (good.sum() > 1): n, p, m, dm, f = n[good], p[good], m[good], dm[good], f[good] h1 = (n > 0) * (dm < 999) n0 = h1.sum() mag0, dmag0 = m[n == 0], dm[n == 0] if (n0 < nmin): if (nepoch < nmin): of.write( """%6d %12.8f %12.8f %8.4f %8.4f %8.4f %8s %8s %10s %4d\n""" % (i0, ra[id0 - 1], dec[id0 - 1], mag0, dmag0, f[n == 0], 'NA', 'NA', 'NA', n0)) i0 += 1 else: remaining[id0 - 1] = False continue x, y = floor(p / 10.), p % 10 d = sqrt(0.5 * ((x / 1.5 - 1)**2 + (y / 1.5 - 1)**2)) * 100 j = dm < 999 j1 = j * (n > 0) dm[~j] = 0.5 m[~j] += 0.5 clf() errorbar(gps[n], m, xerr=expos[n] / 2., yerr=dm, marker='o', capsize=0, linestyle='None', markersize=0, mew=1) ylim((m.max() + 0.5, m.min() - 0.5)) scatter(gps[n[j]], m[j], s=d[j]) plot(gps[n[~j]], m[~j] + 0.5, 'bv') plot(gps[n[~j]], m[~j] - 0.5, 'bo') x = -2.5 * log10(gps[n]) res = linfit(x[j1], m[j1], dy=dm[j1]) sig = (m[j1] - res[0] - res[1] * x[j1]).std() if (sig < 0.01): sig = 0.01 var = dm[j1]**2 + sig**2 m0 = (m[j1] / var).sum() / (1. / var).sum() chi2 = ((m[j1] - m0)**2 / var).mean() xm = x[j1].mean() ym = m[j1].mean() res = linfit(x[j1] - xm, m[j1] - ym, dy=sqrt(var)) slp = res[1] dslp = sqrt(res[2][1][1]) # handle over-fitting if (j1.sum() <= 2): if (slp < 0): slp = min(slp + dslp, 0.) else: slp = max(slp - dslp, 0.) of.write( """%6d %12.8f %12.8f %8.4f %8.4f %8.4f %8.4f %8.4f %10.2f %4d\n""" % (i0, ra[id0 - 1], dec[id0 - 1], mag0, dmag0, f[n == 0], slp, dslp, chi2, n0)) ii = gps[n[j1]].argsort() plot(gps[n[j1]][ii], res[0] + slp * (x[j1][ii] - xm) + ym, label="""%.4f +/- %.4f""" % (slp, dslp)) legend() dt0 = expos.min() xlim((gps.min() - dt0, gps.max() + dt0)) xlabel("""Time Since %s - %.2f [hours]""" % (t0, gps0 / 3600.), fontsize=16) ylabel("USNO R", fontsize=16) title("""Light Curve for Source %d""" % i0) savefig("""lc_%d.jpg""" % i0) i0 += 1 of.close() cat_dat = cat_dat[:, remaining] of = open(radecfile, 'r') ln1 = of.readline() of.close() of = open(radecfile, 'w') if (ln1[0] == '#'): of.write(ln1) for i in xrange(len(cat_dat[0])): ra, dec, mag, dmag, mag_big, dmag_big, fwhm, x, y, xa, ya, x2a, y2a, expos, idx = cat_dat[:, i] of.write("""%f %f %f %f %f %f %f %f %f %f %f %f %f %f %d\n""" % (ra, dec, mag, dmag, mag_big, dmag_big, fwhm, x, y, xa, ya, x2a, y2a, expos, i + 1)) of.close()
#Fit it, yo (iterate over each pixel of the unfolded angle cube) print 'Now going to fit for RM and zero angle' print 'Making angle errors' qerr=qin[1].data uerr=uin[1].data angerr=np.sqrt(0.25*(1/(qcube**2+ucube**2))*((qcube*uerr)**2+(ucube*qerr)**2)) coeffs=np.empty((5,sy,sx)) coeffs_temp=np.empty(5) print'Fitting...' for ind in np.ndindex(sy,sx): ytemp=angle[:,ind[0],ind[1]] errtemp=angerr[:,ind[0],ind[1]] coeffs_temp[0],coeffs_temp[1],coeffs_temp[2],coeffs_temp[3],coeffs_temp[4]=linfit.linfit(ytemp,lamsq,errtemp) coeffs[:,ind[0],ind[1]]=coeffs_temp print '...done!' #output output angmap=coeffs[0,:,:] rmmap=coeffs[1,:,:] angerr=coeffs[2,:,:] rmerr=coeffs[3,:,:] chisq=coeffs[4,:,:] #WRITE IT ALL OUT print 'Writing output' angmapout='../rmsyn/'+field+'angmap.fits'
from __future__ import division, print_function, absolute_import import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec # for unequal plot boxes from linfit import linfit # data set for linear fitting x = np.array([2.3, 4.7, 7.1, 9.6, 11.7, 14.1, 16.4, 18.8, 21.1, 23.0]) y = np.array([-25., 3., 110., 110., 230., 300., 270., 320., 450., 400.]) sigmay = np.array([15., 30., 30., 40., 40., 50., 40., 30., 50., 30.]) # Fit linear data set with weighting fit, cvm, info = linfit(x, y, sigmay, relsigma=False, return_all=True) dfit = np.sqrt(np.diag(cvm)) # uncertainty estimates for fitting parameters # Open figure window for plotting data with linear fit fig = plt.figure(1, figsize=(8,8)) gs = gridspec.GridSpec(2, 1, height_ratios=[2.5, 6]) # Bottom plot: data and fit ax1 = fig.add_subplot(gs[1]) # Plot data with error bars ax1.errorbar(x, y, yerr = sigmay, ecolor="black", fmt="ro", ms=5) # Plot fit (behind data) endx = 0.05 * (x.max()-x.min()) xFit = np.array([x.min()-endx, x.max()+endx]) yFit = fit[0]*xFit + fit[1] ax1.plot(xFit, yFit, "-b", zorder=-1)
plt.figure(); plt.plot(emas_v1['lon'][dc8_ind[0,:],dc8_ind[1,:]],emas_tau_v1,label='V01') plt.plot(emas['lon'][dc8_ind[0,:],dc8_ind[1,:]], emas_tau,label='V00') # <headingcell level=1> # 1:1 relationship # <codecell> plt.figure() plt.plot(emas_tau_v1,emas_tau,'+',label=r'eMAS $\tau$') plt.plot([10,35],[10,35],'k--',label='one-to-one') from linfit import linfit emas_fit,z = linfit(emas_tau_v1,emas_tau) plt.plot(np.linspace(10,39), emas_fit[1]+emas_fit[0]*np.linspace(10,39),'r--',label='Linear fit:\n y='+str('%.3f' % emas_fit[0])+'x+'+str('%.3f' % emas_fit[1])) plt.title(r'eMAS version comparison along DC8 flight track on 2013-09-13') plt.xlabel(r'eMAS V01 $\tau$') plt.ylabel(r'eMAS V00 $\tau$') plt.legend(frameon=False,loc=4) plt.savefig(fp+'plots/emas_v00_compare_v01_tau.png',dpi=600,transparent=True) # <codecell> print emas_tau_v1, emas_tau print linfit(emas_tau_v1,emas_tau) # <codecell> plt.figure()
def PM_calc(i): if not dx_fin[i].sum() >= nframes: #Si no esta en el minimo de frames, devuelve NaN return np.nan, np.nan, np.nan, np.nan, 0 else: ma = dx_fin[i]*dy_fin[i] #print dx.shape, dx_fin.shape, ma.shape #print yrs.shape, dx[i].shape, dy[i].shape, see.shape x = yrs[ma] yx = dx[i][ma] yxe = dxe[i][ma] yy = dy[i][ma] yye = dye[i][ma] ss = see[ma] #Algoritmo antiguo ''' #Ajusta pmx popt, pcov = curve_fit(recta, x, yx, sigma=ss) pmxx = popt[0] pmex = np.sqrt(pcov[0,0]) #Ajusta pmy popt, pcov = curve_fit(recta, x, yy, sigma=ss) pmyy = popt[0] pmey = np.sqrt(pcov[0,0]) ''' #Bayesian Linear Regression (no funciona por ahora y mas costoso) ''' br = BayesianRegression() br.fit(x[:, np.newaxis], yx) pmxx = br.coef_[0] pmex = br.beta_ br.fit(x[:, np.newaxis], yy) pmyy = br.coef_[0] pmex = br.beta_ ''' #Con matrices ''' mu, sig = linear_regression(x, yx, 1.0/ss**2) pmxx = mu[0] pmex = sig[0,0] mu, sig = linear_regression(x, yy, 1.0/ss**2) pmyy = mu[0] pmey = sig[0,0] ''' #Con linfit (rapido!) if not weight: yye = np.ones(len(x)) yye = np.ones(len(x)) fitx, cvm = linfit(x, yx, sigmay=yxe) pmxx = fitx[0] pmex = np.sqrt(cvm[0,0]) fity, cvm = linfit(x, yy, sigmay=yye) pmyy = fity[0] pmey = np.sqrt(cvm[0,0]) ##Sigma Clip clip = np.ones(len(x)).astype(bool) if sig_iter>0: for si in range(sig_iter): modelx = recta(x, *fitx) resx = yx - modelx stdx = mad_std(resx[np.isfinite(resx)]) modely = recta(x, *fity) resy = yy - modely stdy = mad_std(resy[np.isfinite(resy)]) res = np.sqrt(resx**2 + resy**2) std = np.sqrt(stdx**2 + stdy**2) clip = clip * (res <= nsigma*std) if clip.sum() < nframes: continue #Vuelve a calcular fitx, cvm = linfit(x[clip], yx[clip], sigmay=yxe[clip]) pmxx = fitx[0] pmex = np.sqrt(cvm[0,0]) fity, cvm = linfit(x[clip], yy[clip], sigmay=yye[clip]) pmyy = fity[0] pmey = np.sqrt(cvm[0,0]) return pmxx, pmyy, pmex, pmey, clip.sum()
def main(): # from here ???? def fMOD(x): return np.array([1, x, np.sin(x)]) # def Q4hw(x): # return ((1**2-x**2)*(1-x**2)) # x = np.arange(-1,1,0.01) # plt.figure(num=1,figsize=(8,8),dpi=100,facecolor='white') # plt.grid() # plt.title("P521 HW 4") # plt.ylabel("φ(x)") # plt.xlabel("x (in terms of a)") # plt.plot(x,Q4hw(x)) # plt.show() #sorry about the code being this bad...it's been a rough week print("\n\n\n###DATA0###") """LINFIT DATA0""" x, y, sig = readdata("data0.txt") a, b, siga, sigb, chi2, q = linfit.linfit(x, y, sig) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data0, linfit") plt.plot(x, y, label="Data") plt.plot(x, b * x + a, label="Linear Regression") plt.show() """GEN LINFIT DATA0""" x, y, sig = readdata("data0.txt") a, siga, chi2, q = linfit.glinfit(x, y, sig, 3, fMOD) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.title("Data0, glinfit") plt.grid() plt.plot(x, y) plt.plot(x, a[0] + a[1] * x + a[2] * np.sin(x)) plt.show() print("\n\n\n###DATA1###") """LINFIT DATA1""" x, y, sig = readdata("data1.txt") a, b, siga, sigb, chi2, q = linfit.linfit(x, y, sig) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data1, linfit") plt.plot(x, y) plt.plot(x, b * x + a) plt.show() """GEN LINFIT DATA1""" x, y, sig = readdata("data1.txt") a, siga, chi2, q = linfit.glinfit(x, y, sig, 3, fMOD) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data1, glinfit") plt.plot(x, y) plt.plot(x, a[0] + a[1] * x + a[2] * np.sin(x)) plt.show() print("\n\n\n###DATA2###") """LINFIT DATA2""" x, y, sig = readdata("data2.txt") a, b, siga, sigb, chi2, q = linfit.linfit(x, y, sig) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data2, linfit") plt.plot(x, y) plt.plot(x, b * x + a) plt.show() """GEN LINFIT DATA2""" x, y, sig = readdata("data2.txt") a, siga, chi2, q = linfit.glinfit(x, y, sig, 3, fMOD) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data2, glinfit") plt.plot(x, y) plt.plot(x, a[0] + a[1] * x + a[2] * np.sin(x)) plt.show() print("\n\n\n###DATA3###") """LINFIT DATA3""" x, y, sig = readdata("data3.txt") a, b, siga, sigb, chi2, q = linfit.linfit(x, y, sig) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data3, linfit") plt.plot(x, y) plt.plot(x, b * x + a) plt.show() """GEN LINFIT DATA3""" x, y, sig = readdata("data3.txt") a, siga, chi2, q = linfit.glinfit(x, y, sig, 3, fMOD) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data3, glinfit") plt.plot(x, y) plt.plot(x, a[0] + a[1] * x + a[2] * np.sin(x)) plt.show() print("\n\n\n###DATA4###") """LINFIT DATA4""" x, y, sig = readdata("data4.txt") a, b, siga, sigb, chi2, q = linfit.linfit(x, y, sig) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data4, linfit") plt.plot(x, y) plt.plot(x, b * x + a) plt.show() """GEN LINFIT DATA4""" x, y, sig = readdata("data4.txt") a, siga, chi2, q = linfit.glinfit(x, y, sig, 3, fMOD) plt.figure(num=1, figsize=(8, 8), dpi=100, facecolor='white') plt.grid() plt.title("Data4, glinfit") plt.plot(x, y) plt.plot(x, a[0] + a[1] * x + a[2] * np.sin(x)) plt.show()
def plot_lin(x,y,x_err=[None],y_err=[None],color='b',labels=True,ci=0.95,shaded_ci=True,use_method='linfit',ax=None,*args,**kwargs): """ function to plot on top of previous a linear fit line, with the line equation in legend. Input: x: independent y: dependent x_err: uncertainty in x (default None) y_err: uncertainty in y (default None) color: color of the plot (default blue) labels if include label in legend of linear equation values (default True) ci: Confidence interval (in percent) (default 95) shaded_ci: plot the shaded confidence interval (default True) use_method: Define which method to use for linear regression options: 'linfit' (default) Use the linfit method from linfit module, when set, x_err and y_err are ignored 'odr' use the scipy ODR method to calculate the linear regression, with x_err and y_err abilities 'statsmodels' use the statsmodels method, Weighted least squares, with weighing of 1/y_err, x_err ignored ax: variable containing the axis to which to plot onto. any other input for matplotlib plot function can be passed via args or kwargs Output: p coefficients (intercept, slope) perr values (error in intercept, error in slope) """ import matplotlib.pyplot as plt import numpy as np from Sp_parameters import doublenanmask, nanmasked from plotting_utils import confidence_envelope, lin if not ax: ax = plt.gca() xn,yn,mask = doublenanmask(x,y,return_mask=True) if use_method=='odr': from scipy import odr model = odr.Model(lin) if any(x_err): if any(y_err): dat = odr.RealData(xn,yn,sx=x_err[mask],sy=y_err[mask]) else: dat = odr.RealData(xn,yn,sx=x_err[mask]) else: if any(y_err): dat = odr.RealData(xn,yn,sy=y_err[mask]) else: dat = odr.RealData(xn,yn) outa = odr.ODR(dat,model,beta0=[1.0,0.5]).run() print outa.cov_beta perr = np.sqrt(np.diag(outa.cov_beta)) p = outa.beta elif use_method=='linfit': from linfit import linfit c,cm = linfit(xn,yn) p = np.array([c[1],c[0]]) cerr = np.sqrt(np.diag(cm)) perr = np.array([cerr[1],cerr[0]]) elif use_method=='statsmodels': import statsmodels.api as sm Xn = sm.add_constant(xn) if any(y_err): results = sm.WLS(yn,Xn,weights=1/y_err[mask]).fit() else: results = sm.OLS(yn,Xn).fit() p = results.params perr = results.bse else: print 'Method: %s is not a valid choice' % use_method return xx = np.linspace(xn.min()-np.abs(xn.min()*0.1),xn.max()+np.abs(xn.max()*0.1)) if labels: ax.plot(xx,lin(p,xx),color=color,label='y=(%2.2f$\pm$%2.2f)+\n(%2.2f$\pm$%2.2f)x' % (p[0],perr[0],p[1],perr[1]),*args,**kwargs) else: ax.plot(xx,lin(p,xx),color=color,*args,**kwargs) if shaded_ci: y_up,y_down = confidence_envelope(xx, p, perr, ci=ci) ax.fill_between(xx,y_down,y_up,color=color,alpha=0.1) return p,perr
def ddoti_distortion(fits_file,nmax=1000,tune_rotation=True,p10=4.6e-4,max_iter=5): """ p10 is the initial guess for distortion parameter, distortion = p10*rad^3 """ fac = 57.29577951 base=fits_file.replace('.fits','') srcfile=base+'_dir/'+base+'_radec.txt.match.txt' dat = loadtxt(srcfile,usecols=(2,4,5,8,9)).T ii = dat[0].argsort()[:nmax] r0,d0,x,y = dat[1:,ii] hdr = getheader(fits_file) i,j = xy2ij(x,y,hdr,distort=False) ra0,dec0 = hdr['CRVAL1'],hdr['CRVAL2'] c=cos(dec0/fac) sd0,cd0 = sin(d0/fac),cos(d0/fac) sr0,cr0 = sin(r0/fac),cos(r0/fac) def get_i0j0(i00,j00): """ apply the tan projection to get i0,j0 from the reference r0,d0 """ da0, dd0 = (i00/c+ra0)/fac, (j00+dec0)/fac sda0,cda0 = sin(da0),cos(da0) sdd0,cdd0 = sin(dd0),cos(dd0) cfac = cr0*cda0+sr0*sda0 cos_c = sdd0*sd0 + cdd0*cd0*cfac i0 = fac*cd0*(sr0*cda0-cr0*sda0) / cos_c j0 = fac*(cdd0*sd0-sdd0*cd0*cfac) / cos_c return i0,j0 def get_p1(rad,rad0,p10=4.6e-4): """ returns the radial dependence parameter p1, after sigma clipping """ dis = rad0-rad rad3 = rad**3 k = nsigma_clip( (dis-rad3*p10)/(1.+rad3*p10*1800.),5)*(rad0>0.5) ks = k.sum() if (ks>10): p1 = (dis[k]*rad3[k]).mean() / (rad3[k]**2).mean() p1 = ( p10 + p1*ks/100. )/(1. + ks/100.) if (p1<p10/2 or p1>2*p10): p1=p10 chi2 = ( (dis[k]-rad3[k]*p1)**2 ).mean() else: p1 = p10 chi2 = ( (dis-rad3*p1)**2 ).mean() return p1,chi2 def fit_optcen(par,i,j): """ optimize to determine the optical center (changing CRVAL1 CRVAL2) """ i00,j00 = max(min(par[0]/360.,0.5),-0.5),max(min(par[1]/360.,0.5),-0.5) i0,j0 = get_i0j0(i00,j00) return get_p1( sqrt((i-i00)**2+(j-j00)**2), sqrt(i0**2+j0**2), p10=p10)[1] if (tune_rotation): h = i**2+j**2 < 2 if (h.sum()<10): h = i**2+j**2 < 16. res = array([0.,0.]) matr = array([[1.,0],[0,1]]) for niter in xrange(max_iter): res = fmin(fit_optcen,res,args=(i,j),disp=False); res = fmin(fit_optcen,res,args=(i,j),disp=False) # i00,j00 = max(min(res[0]/360.,0.5),-0.5),max(min(res[1]/360.,0.5),-0.5) i0,j0 = get_i0j0(i00,j00) rad2 = (i-i00)**2+(j-j00)**2 p1 = get_p1(sqrt(rad2),sqrt(i0**2+j0**2),p10=p10)[0] i1 = i + (i-i00)*rad2*p1 j1 = j + (j-j00)*rad2*p1 # if (tune_rotation): rii,rji = linfit(i1[h]-i00,i1[h]-i00-i0[h])[1], linfit(i1[h]-i00,j1[h]-j00-j0[h])[1] rjj,rij = linfit(j1[h]-j00,j1[h]-j00-j0[h])[1], linfit(j1[h]-j00,i1[h]-i00-i0[h])[1] else: rii=0;rij=0;rji=0;rjj=0; rsd1,rsd2 = abs(rji)*3600.,abs(rij)*3600. print """ Iter %d %.2e %.0f %.0f Resids: %.8f %.8f (arcsec) %s""" % (niter,p1,res[0]*10,res[1]*10,rsd1,rsd2,fits_file) # matr0 = array([[1-rii,-rij],[-rji,1-rjj]]) i = i00 + matr0[0,0]*(i-i00) + matr0[0,1]*(j-j00) j = j00 + matr0[1,0]*(i-i00) + matr0[1,1]*(j-j00) matr = dot(matr0,matr) if (rsd1<1.e-3 and rsd2<1.e-3): break # diagnostic plot rad2 = (i-i00)**2+(j-j00)**2 i1 = i + (i-i00)*rad2*p1 j1 = j + (j-j00)*rad2*p1 rad20 = i0**2+j0**2 dis=sqrt(rad20)-sqrt(rad2) dism = sqrt(rad2)*rad2*p1 dis1=sqrt(rad20)-sqrt((i1-i00)**2+(j1-j00)**2) plot (sqrt(rad2),dis*3600.,'bo',label='Uncorrected',alpha=0.5) s=rad2.argsort() plot (sqrt(rad2[s]),dism[s]*3600.,'k-',lw=3,alpha=0.5,label=("""%.1f""" % (p1*8*3600.))+r'$(r/2)^3$') plot (sqrt(rad2),dis1*3600.,'go',label='Corrected',alpha=0.5) k = nsigma_clip(dis1/(1.+dism*1800.),5) ylim((min(dis1[k].min(),dis[k].min())*3600,max(dis1[k].max(),dis[k].max())*3600.)) grid() xlabel("Radius from Image Center [Degrees]",fontsize=16) ylabel("Source Offset [arcsec]",fontsize=16) legend(loc=2) savefig(fits_file.replace('.fits','_dist.jpg')) # p1*rad^3 os.system("""sethead PV1_1=1.0 PV2_1=1.0 %s""" % fits_file) os.system("""sethead PV1_7=%.8f PV1_9=%.8f PV2_7=%.8f PV2_9=%.8f %s""" % (p1,p1,p1,p1,fits_file)) hdr = getheader(fits_file) cd = array([[hdr['CD1_1'],hdr['CD1_2']],[hdr['CD2_1'],hdr['CD2_2']]]) cd1 = dot(matr,cd) os.system("""sethead CD1_1=%.8f CD1_2=%.8f CD2_1=%.8f CD2_2=%.8f %s""" % (cd1[0,0],cd1[0,1],cd1[1,0],cd1[1,1],fits_file)) hdr = getheader(fits_file) x,y = ij2xy(i00,j00,hdr,distort=True); x,y = round(x),round(y) r,d = xy2ad(x,y,hdr) os.system("""sethead CRPIX1=%.2f CRPIX2=%.2f CRVAL1=%.8f CRVAL2=%.8f %s""" % (x,y,r,d,fits_file))
# <rawcell> # We are now lookign at the linfit routine used in parameters 9, 10, 11, and others # <codecell> f2,a2 = plt.subplots(1) a2.plot(lut.wvl,dsp2,'b+') a2.plot(lut.wvl,dsp2,'b',label='Normalized derivative') a2.set_xlim([1000,1077]) a2.set_ylim([-7,2]) a2.grid(True) # build the linear fit from linfit import linfit f,z = linfit(lut.wvl[i1000:i1077],dsp2[i1000:i1077]) print f print z print f[1] a2.plot(lut.wvl[i1000:i1077],f[1]+f[0]*lut.wvl[i1000:i1077],'r',label='linear fit') a2.set_xlabel('Wavelength [nm]') a2.set_ylabel('Zenith normalized derivative') plt.legend() #linfit output is [m,b], not [b,m] like previously thought # <markdowncell> # Now we look at the derivative at the minimum derivative directly before at 1200 nm. This has to be adjusted for the different wavelength spacing between 4STAR and SSFR. # <codecell>
def main(): # ??????????????????????????????????????? # LINFIT data0 = np.loadtxt('data0.txt') x_0 = data0[0:len(data0), 0] y_0 = data0[0:len(data0), 1] sig_0 = data0[0:len(data0), 2] a_0, b_0, siga_0, sigb_0, chi2_0, q_0 = linfit.linfit(x_0, y_0, sig_0) print(a_0, b_0, siga_0, sigb_0, chi2_0, q_0) # PLOT 0 y_fit0 = a_0 + (b_0 * x_0) plt.plot(x_0, y_0, 'o', x_0, y_fit0, '-') plt.errorbar(x_0, y_0, sig_0, fmt='|') plt.legend(('Data', 'Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 0') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() data1 = np.loadtxt('data1.txt') x_1 = data1[0:len(data1), 0] y_1 = data1[0:len(data1), 1] sig_1 = data1[0:len(data1), 2] a_1, b_1, siga_1, sigb_1, chi2_1, q_1 = linfit.linfit(x_1, y_1, sig_1) print(a_1, b_1, siga_1, sigb_1, chi2_1, q_1) # PLOT 1 y_fit1 = a_1 + (b_1 * x_1) plt.plot(x_1, y_1, 'o', x_1, y_fit1, '-') plt.errorbar(x_1, y_1, sig_1, fmt='|') plt.legend(('Data', 'Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 1') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() data2 = np.loadtxt('data2.txt') x_2 = data2[0:len(data2), 0] y_2 = data2[0:len(data2), 1] sig_2 = data2[0:len(data2), 2] a_2, b_2, siga_2, sigb_2, chi2_2, q_2 = linfit.linfit(x_2, y_2, sig_2) print(a_2, b_2, siga_2, sigb_2, chi2_2, q_2) # PLOT 2 y_fit2 = a_2 + (b_2 * x_2) plt.plot(x_2, y_2, 'o', x_2, y_fit2, '-') plt.errorbar(x_2, y_2, sig_2, fmt='|') plt.legend(('Data', 'Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 2') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() data3 = np.loadtxt('data3.txt') x_3 = data3[0:len(data3), 0] y_3 = data3[0:len(data3), 1] sig_3 = data3[0:len(data3), 2] a_3, b_3, siga_3, sigb_3, chi2_3, q_3 = linfit.linfit(x_3, y_3, sig_3) print(a_3, b_3, siga_3, sigb_3, chi2_3, q_3) # PLOT 3 y_fit3 = a_3 + (b_3 * x_3) plt.plot(x_3, y_3, 'o', x_3, y_fit3, '-') plt.errorbar(x_3, y_3, sig_3, fmt='|') plt.legend(('Data', 'Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 3') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() data4 = np.loadtxt('data4.txt') x_4 = data4[0:len(data4), 0] y_4 = data4[0:len(data4), 1] sig_4 = data4[0:len(data4), 2] a_4, b_4, siga_4, sigb_4, chi2_4, q_4 = linfit.linfit(x_4, y_4, sig_4) print(a_4, b_4, siga_4, sigb_4, chi2_4, q_4) # PLOT 4 y_fit4 = a_4 + (b_4 * x_4) plt.plot(x_4, y_4, 'o', x_4, y_fit4, '-') plt.errorbar(x_4, y_4, sig_4, fmt='|') plt.legend(('Data', 'Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 4') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() # --------------------------------------------------------------------- # GLINFIT m = 3 fMOD = ftest a_0, siga_0, chi2_0, q_0 = linfit.glinfit(x_0, y_0, sig_0, m, fMOD) print(a_0, siga_0, chi2_0, q_0) # PLOT 0 y_fit0 = a_0[0] + (a_0[1] * x_0) + (a_0[2] * np.sin(x_0)) plt.plot(x_0, y_0, 'o', x_0, y_fit0, '-') plt.errorbar(x_0, y_0, sig_0, fmt='|') plt.legend(('Data', 'General Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 0') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() a_1, siga_1, chi2_1, q_1 = linfit.glinfit(x_1, y_1, sig_1, m, fMOD) print(a_1, siga_1, chi2_1, q_1) # PLOT 1 y_fit1 = a_1[0] + (a_1[1] * x_1) + (a_1[2] * np.sin(x_1)) plt.plot(x_1, y_1, 'o', x_1, y_fit1, '-') plt.errorbar(x_1, y_1, sig_1, fmt='|') plt.legend(('Data', 'General Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 1') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() a_2, siga_2, chi2_2, q_2 = linfit.glinfit(x_2, y_2, sig_2, m, fMOD) print(a_2, siga_2, chi2_2, q_2) # PLOT 2 y_fit0 = a_2[0] + (a_2[1] * x_2) + (a_2[2] * np.sin(x_2)) plt.plot(x_2, y_2, 'o', x_2, y_fit2, '-') plt.errorbar(x_2, y_2, sig_2, fmt='|') plt.legend(('Data', 'General Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 2') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() a_3, siga_3, chi2_3, q_3 = linfit.glinfit(x_3, y_3, sig_3, m, fMOD) print(a_3, siga_3, chi2_3, q_3) # PLOT 3 y_fit3 = a_3[0] + (a_3[1] * x_3) + (a_3[2] * np.sin(x_3)) plt.plot(x_3, y_3, 'o', x_3, y_fit3, '-') plt.errorbar(x_3, y_3, sig_3, fmt='|') plt.legend(('Data', 'General Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 3') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show() a_4, siga_4, chi2_4, q_4 = linfit.glinfit(x_4, y_4, sig_4, m, fMOD) print(a_4, siga_4, chi2_4, q_4) # PLOT 4 y_fit4 = a_4[0] + (a_4[1] * x_4) + (a_4[2] * np.sin(x_4)) plt.plot(x_4, y_4, 'o', x_4, y_fit4, '-') plt.errorbar(x_4, y_4, sig_4, fmt='|') plt.legend(('Data', 'General Least-Squares Fit', 'y Error'), loc=0) plt.title('Data Set 4') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.show()