def tran_quad(f, b, r, D0, D1, D2, fe, be, re, D0e, D1e, D2e): result = f + D0 + D1 * (b - r) + D2 * (b - r)**2 c = b - r ce = err_Add([be, re]) Err_lin = err_Mul([np.ones_like(f) * D1, c], [np.ones_like(f) * D1e, ce]) Err_quad = err_Mul([np.ones_like(f) * D2, c**2], [np.ones_like(f) * D2e, c * ce * (2**0.5)]) return result, err_Add([fe, D0e, Err_lin, Err_quad])
def drawPCA_simple(ax, F1, F1e, F2, F2e, F3, F3e, F4, F4e, a_star, a_star_err, fsize=17, col='k', alp=1., err=True): F1F2 = F1 - F2 F1F2e = err_Add([F1e, F2e]) F2F3 = F2 - F3 F2F3e = err_Add([F2e, F3e]) F3F4 = F3 - F4 F3F4e = err_Add([F3e, F4e]) plotx = np.array( [np.mean(F1F2) - 2 * np.std(F1F2), np.mean(F1F2) + 2 * np.std(F1F2)]) # ax.set_ylabel(FILTERS[2] + '-' + FILTERS[3], fontsize=fsize) # ax.set_xlabel('a_star', fontsize=fsize) if err is True: ax.errorbar(a_star, F3F4, xerr=a_star_err, yerr=F3F4e, fmt='o', markersize=2, ecolor=col, elinewidth=0.5, capsize=0.5, capthick=0.5, c=col, alpha=alp) else: ax.scatter(a_star, F3F4, s=2, c=col, alpha=alp) ax.tick_params(axis='x', labelsize=fsize) ax.tick_params(axis='y', labelsize=fsize)
def standardization(self, m_ins, e_ins, c_ins, ce_ins): """ m_std - m_ins = C2 + C1 (b_std - r_std) --- eq 1 b_std - r_std = C4 + C3 (b_ins - r_ins) --- eq 2 """ c_std = self.C4 + self.C3 * c_ins #try: print(len(m_ins)) #except: print(c_ins, self.C3err, ce_ins, self.C1, self.C1err, self.C2err) ce_std = err_Add([np.ones_like(c_std)*self.C4err, err_Mul([np.ones_like(c_std)*self.C3, c_ins], [np.ones_like(c_std)*self.C3err, ce_ins])]) if type(c_ins) is np.float64: if c_ins == 0.: ce_std = err_Add([np.ones_like(c_std)*self.C4err, ((self.C3err/self.C3)**2 + ce_ins**2)**0.5]) m_std = m_ins + self.C2 + self.C1 * c_std e_std = err_Add([e_ins, np.ones_like(c_std)*self.C2err, err_Mul([np.ones_like(c_std)*self.C1, c_std], [np.ones_like(c_std)*self.C1err, ce_std])]) return m_std, e_std, c_std, ce_std
def __init__(self, table, FILTERS, constant=True, coe0=np.array([0, 1]), sigmaclip=10, maxiter=2): self.FILTERS = FILTERS self.HeaderName = ['F1', 'F1e', 'F2', 'F2e', 'F3', 'F3e', 'F4', 'F4e'] self.AstTab = pd.DataFrame(table, columns=self.HeaderName, dtype=float) x = self.AstTab['F1'] - self.AstTab['F2'] xerr = err_Add([self.AstTab['F1e'], self.AstTab['F2e']]) y = self.AstTab['F2'] - self.AstTab['F3'] yerr = err_Add([self.AstTab['F2e'], self.AstTab['F3e']]) mask, fit, Niter = linfit_sigclip(x, y, xerr, yerr, coe0=coe0, sigmaclip=sigmaclip, maxiter=maxiter) theta = np.arctan(fit[0][1]) coserr = (np.cos(np.arctan(fit[0][1] - fit[1][1, 1]**0.5)) - np.cos(np.arctan(fit[0][1])) + np.cos(np.arctan(fit[0][1] + fit[1][1, 1]**0.5)) - np.cos(np.arctan(fit[0][1]))) / 2 sinerr = (np.sin(np.arctan(fit[0][1] - fit[1][1, 1]**0.5)) - np.sin(np.arctan(fit[0][1])) + np.sin(np.arctan(fit[0][1] + fit[1][1, 1]**0.5)) - np.sin(np.arctan(fit[0][1]))) / 2 a_star = np.cos(theta) * x + np.sin(theta) * y a_star_err = err_Add([ err_Mul([ np.full(len(x), np.cos(np.arctan(fit[0][1])), dtype=np.float), x ], [np.full(len(x), coserr, dtype=np.float), xerr]), err_Mul([ np.full(len(y), np.sin(np.arctan(fit[0][1])), dtype=np.float), y ], [np.full(len(y), sinerr, dtype=np.float), yerr]) ]) if constant is False: print('a_star = %.3f * (%s - %s) + %.3f * (%s - %s)' % (np.cos(theta), self.FILTERS[0], self.FILTERS[1], np.sin(theta), self.FILTERS[1], self.FILTERS[2])) elif constant is True: print('a_star = %.3f * (%s - %s) + %.3f * (%s - %s) - %.3f' % (np.cos(theta), self.FILTERS[0], self.FILTERS[1], np.sin(theta), self.FILTERS[1], self.FILTERS[2], np.mean(a_star))) a_star = a_star - np.mean(a_star) self.AstTab['a_star'] = a_star self.AstTab['a_star_err'] = a_star_err self.a_star = a_star self.a_star_err = a_star_err self.PCAfit = fit
def tran_lin(f, b, r, C0, C1, fe, be, re, C0e, C1e): result = f + C0 + C1 * (b - r) c = b - r ce = err_Add([be, re]) Err_lin = err_Mul([np.ones_like(f) * C1, c], [np.ones_like(f) * C1e, ce]) return result, err_Add([fe, C0e, Err_lin])
def Figure_Fit(self, dpi=150, save=False, dir_save='', AddSaveName=''): TabF1, TabF2, TabF3, TabF4 = self.MatTabSet AstFSet = [0, TabF1[0], TabF2[0], TabF3[0], TabF4[0]] F1, F2, F3, F4 = self.FILTERS eq1_Xlabels = [ '%s_{std} - %s_{std}' % (F1, F2), '%s_{std} - %s_{std}' % (F1, F2), '%s_{std} - %s_{std}' % (F2, F3), '%s_{std} - %s_{std}' % (F3, F4) ] eq1_Ylabels = [ '%s_{std} - %s_{ins}' % (F1, F1), '%s_{std} - %s_{ins}' % (F2, F2), '%s_{std} - %s_{ins}' % (F3, F3), '%s_{std} - %s_{ins}' % (F4, F4) ] eq2_Xlabels = [ '%s_{ins} - %s_{ins}' % (F1, F2), '%s_{ins} - %s_{ins}' % (F1, F2), '%s_{ins} - %s_{ins}' % (F2, F3), '%s_{ins} - %s_{ins}' % (F3, F4) ] eq2_Ylabels = [ '%s_{std} - %s_{std}' % (F1, F2), '%s_{std} - %s_{std}' % (F1, F2), '%s_{std} - %s_{std}' % (F2, F3), '%s_{std} - %s_{std}' % (F3, F4) ] fig = plt.figure(figsize=(19, 9)) fig.subplots_adjust(top=0.90, bottom=0.10, left=0.05, right=0.97) fig.suptitle('Std. Fit. %s #%s' % (self.MODE, self.AstNum)) axs = [] for k in range(8): axs.append(fig.add_subplot(2, 4, k + 1)) for k in range(4): axs[k].set_title(self.FILTERS[k]) self.FitSet[k].plot_fitting(axs[k], axs[k + 4], eq1_Xlabel=eq1_Xlabels[k], eq1_Ylabel=eq1_Ylabels[k], eq2_Xlabel=eq2_Xlabels[k], eq2_Ylabel=eq2_Ylabels[k]) # ast g # axs[0].errorbar(self.AstF1_cal[0] - self.AstF2_cal[0], self.AstF1_cal[0] - AstFSet[1][5], xerr=err_Add([self.AstF1_cal[1], self.AstF2_cal[1]]), yerr=err_Add([self.AstF1_cal[1], AstFSet[1][6]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') axs[4].errorbar(AstFSet[1][5] - AstFSet[2][5], self.AstF1_cal[0] - self.AstF2_cal[0], xerr=err_Add([AstFSet[1][6], AstFSet[2][6]]), yerr=err_Add([self.AstF1_cal[1], self.AstF2_cal[1]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') # ast r # axs[1].errorbar(self.AstF1_cal[0] - self.AstF2_cal[0], self.AstF2_cal[0] - AstFSet[2][5], xerr=err_Add([self.AstF1_cal[1], self.AstF2_cal[1]]), yerr=err_Add([self.AstF2_cal[1], AstFSet[2][6]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') axs[5].errorbar(AstFSet[1][5] - AstFSet[2][5], self.AstF1_cal[0] - self.AstF2_cal[0], xerr=err_Add([AstFSet[1][6], AstFSet[2][6]]), yerr=err_Add([self.AstF1_cal[1], self.AstF2_cal[1]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') # ast i # axs[2].errorbar(self.AstF2_cal[0] - self.AstF3_cal[0], self.AstF3_cal[0] - AstFSet[3][5], xerr=err_Add([self.AstF2_cal[1], self.AstF3_cal[1]]), yerr=err_Add([self.AstF3_cal[1], AstFSet[3][6]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') axs[6].errorbar(AstFSet[2][5] - AstFSet[3][5], self.AstF2_cal[0] - self.AstF3_cal[0], xerr=err_Add([AstFSet[2][6], AstFSet[3][6]]), yerr=err_Add([self.AstF2_cal[1], self.AstF3_cal[1]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') # ast z # axs[3].errorbar(self.AstF3_cal[0] - self.AstF4_cal[0], self.AstF4_cal[0] - AstFSet[4][5], xerr=err_Add([self.AstF3_cal[1], self.AstF4_cal[1]]), yerr=err_Add([self.AstF4_cal[1], AstFSet[4][6]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') axs[7].errorbar(AstFSet[3][5] - AstFSet[4][5], self.AstF3_cal[0] - self.AstF4_cal[0], xerr=err_Add([AstFSet[3][6], AstFSet[4][6]]), yerr=err_Add([self.AstF3_cal[1], self.AstF4_cal[1]]), fmt='o', markersize=4, ecolor='r', elinewidth=1, capsize=1.2, capthick=0.8, c='r') #plt.show() if save is True: fig.savefig(dir_save + 'StdFit' + AddSaveName + '_%s_%s.png' % (self.MODE, self.AstNum), dpi=dpi) plt.close('all')
def StdAsteroid(self, sig_err=[1, 1, 1, 1], sig_eq1=[5, 5, 5, 5], sig_eq2=[2, 2, 2, 2], maxiter=[10, 10], fit_losstype=['basic', 'basic'], fit_cliptype=['fit', 'fit'], fit_clip_err=True, Mconsist=2, fit_Nlim_min=34): """ fittype=['basic', 'basic'] !!! Recent tables from GetMatTabSet are used !!! :output: self.FitSet, self.AstF_cal, self.TabF_cal """ TabF1, TabF2, TabF3, TabF4 = self.MatTabSet AstF1 = TabF1[0] AstF2 = TabF2[0] AstF3 = TabF3[0] AstF4 = TabF4[0] TabF1 = TabF1[1:] TabF2 = TabF2[1:] TabF3 = TabF3[1:] TabF4 = TabF4[1:] # [0] newID, [1] X_Image, [2] Y_Image, # [3] MagStd, [4] MerrStd, [5] MagIns, [6] MerrIns, [7] ra_obs, [8] dec_obs # standardization_fit(m_ins, e_ins, m_std, e_std, r_ins, re_ins, r_std, re_std, b_ins, be_ins, b_std, be_std) ### FLAGING ### # FLAG_edge if AstF1[1] < 100 or AstF1[1] > 9116 or AstF1[2] < 100 or AstF1[2] > 9132 or \ AstF2[1] < 100 or AstF2[1] > 9116 or AstF2[2] < 100 or AstF2[2] > 9132 or \ AstF3[1] < 100 or AstF3[1] > 9116 or AstF3[2] < 100 or AstF3[2] > 9132 or \ AstF4[1] < 100 or AstF4[1] > 9116 or AstF4[2] < 100 or AstF4[2] > 9132: self.FLAG += 1 # FLAG_extrapolation_bright if AstF1[5] < np.min(TabF1[:, 5]) or AstF2[5] < np.min(TabF2[:, 5]) or \ AstF3[5] < np.min(TabF3[:, 5]) or AstF4[5] < np.max(TabF4[:, 5]): self.FLAG += 2 # FALG_extrapolation_faint if AstF1[5] > np.max(TabF1[:, 5]) or AstF2[5] > np.min(TabF2[:, 5]) or \ AstF3[5] > np.max(TabF3[:, 5]) or AstF4[5] > np.max(TabF4[:, 5]): self.FLAG += 4 # 9 mag is the brightest in 60s EXPTIME img. #if AstF1[5] > 11 : mag Flaging sigset = np.array([sig_eq1, sig_eq2, sig_err]).T ### Fitting ### # FitF1 : g, g-r [F1, F1-F2] FitF1 = standardization_fit(TabF1[:, 5], TabF1[:, 6], TabF1[:, 3], TabF1[:, 4], TabF1[:, 5], TabF1[:, 6], TabF1[:, 3], TabF1[:, 4], TabF2[:, 5], TabF2[:, 6], TabF2[:, 3], TabF2[:, 4]) FitF1.fit(coe1=np.array([np.mean(TabF1[:, 3] - TabF1[:, 5]), 0.15]), coe2=np.array([0.1, 1]), sigmaclip=sigset[0], maxiter=maxiter, losstype=fit_losstype, cliptype=fit_cliptype, clip_err=fit_clip_err, Nlim_min=fit_Nlim_min) # FitF2 : r, g-r [F2, F1-F2] FitF2 = standardization_fit(TabF2[:, 5], TabF2[:, 6], TabF2[:, 3], TabF2[:, 4], TabF1[:, 5], TabF1[:, 6], TabF1[:, 3], TabF1[:, 4], TabF2[:, 5], TabF2[:, 6], TabF2[:, 3], TabF2[:, 4]) FitF2.fit(coe1=np.array([np.mean(TabF2[:, 3] - TabF2[:, 5]), 0.1]), coe2=np.array([0.1, 1]), sigmaclip=sigset[1], maxiter=maxiter, losstype=fit_losstype, cliptype=fit_cliptype, clip_err=fit_clip_err, Nlim_min=fit_Nlim_min) # FitF3 : i, r-i [F3, F2-F3] FitF3 = standardization_fit(TabF3[:, 5], TabF3[:, 6], TabF3[:, 3], TabF3[:, 4], TabF2[:, 5], TabF2[:, 6], TabF2[:, 3], TabF2[:, 4], TabF3[:, 5], TabF3[:, 6], TabF3[:, 3], TabF3[:, 4]) FitF3.fit(coe1=np.array([np.mean(TabF3[:, 3] - TabF3[:, 5]), 0.1]), coe2=np.array([0.1, 1]), sigmaclip=sigset[2], maxiter=maxiter, losstype=fit_losstype, cliptype=fit_cliptype, clip_err=fit_clip_err, Nlim_min=fit_Nlim_min) # FitF4 : z, i-z [F4, F3-F4] FitF4 = standardization_fit(TabF4[:, 5], TabF4[:, 6], TabF4[:, 3], TabF4[:, 4], TabF3[:, 5], TabF3[:, 6], TabF3[:, 3], TabF3[:, 4], TabF4[:, 5], TabF4[:, 6], TabF4[:, 3], TabF4[:, 4]) FitF4.fit(coe1=np.array([np.mean(TabF4[:, 3] - TabF4[:, 5]), 0.1]), coe2=np.array([0.1, 1]), sigmaclip=sigset[3], maxiter=maxiter, losstype=fit_losstype, cliptype=fit_cliptype, clip_err=fit_clip_err, Nlim_min=fit_Nlim_min) if (FitF1.FitFailureFlag == True) or (FitF2.FitFailureFlag == True) \ or (FitF3.FitFailureFlag == True) or (FitF4.FitFailureFlag == True): return -1 self.sig_eq1 = sig_eq1 self.FitSet = [FitF1, FitF2, FitF3, FitF4] ### Asteroid ### # standardization(self, m_ins, e_ins, c_ins, ce_ins) self.AstF1_cal = FitF1.standardization(AstF1[5], AstF1[6], AstF1[5] - AstF2[5], err_Add([AstF1[6], AstF2[6]])) self.AstF2_cal = FitF2.standardization(AstF2[5], AstF2[6], AstF1[5] - AstF2[5], err_Add([AstF1[6], AstF2[6]])) self.AstF3_cal = FitF3.standardization(AstF3[5], AstF3[6], AstF2[5] - AstF3[5], err_Add([AstF2[6], AstF3[6]])) self.AstF4_cal = FitF4.standardization(AstF4[5], AstF4[6], AstF3[5] - AstF4[5], err_Add([AstF3[6], AstF4[6]])) print('AstMag\t%.3f\t%.3f\t%.3f\t%.3f' % (self.AstF1_cal[0], self.AstF2_cal[0], self.AstF3_cal[0], self.AstF4_cal[0])) print('AstMerr\t%.4f\t%.4f\t%.4f\t%.4f' % (self.AstF1_cal[1], self.AstF2_cal[1], self.AstF3_cal[1], self.AstF4_cal[1])) ### Stars ### self.TabF1_cal = FitF1.standardization( TabF1[:, 5], TabF1[:, 6], TabF1[:, 5] - TabF2[:, 5], err_Add([TabF1[:, 6], TabF2[:, 6]])) self.TabF2_cal = FitF2.standardization( TabF2[:, 5], TabF2[:, 6], TabF1[:, 5] - TabF2[:, 5], err_Add([TabF1[:, 6], TabF2[:, 6]])) self.TabF3_cal = FitF3.standardization( TabF3[:, 5], TabF3[:, 6], TabF2[:, 5] - TabF3[:, 5], err_Add([TabF2[:, 6], TabF3[:, 6]])) self.TabF4_cal = FitF4.standardization( TabF4[:, 5], TabF4[:, 6], TabF3[:, 5] - TabF4[:, 5], err_Add([TabF3[:, 6], TabF4[:, 6]])) # consistency mag_diff = np.max([self.AstF1_cal[0], self.AstF2_cal[0], self.AstF3_cal[0], self.AstF4_cal[0]])-\ np.min([self.AstF1_cal[0], self.AstF2_cal[0], self.AstF3_cal[0], self.AstF4_cal[0]]) if mag_diff > Mconsist: print('StdAsteroid - consistency error %.3f' % mag_diff) return -1 else: return 0