def get_normalized_flux(w, f, e): #Redden/Un-redden the spectra. aux_flux = pyasl.unred(w, f, ebv=e, R_V=3.1) #Wavelength window where the mean flux is computed. window_condition = ((w >= 4000.) & (w <= 9000.)) flux_window = aux_flux[window_condition] normalization_factor = np.mean(flux_window) aux_flux /= normalization_factor return aux_flux, normalization_factor
def lnlike(params, lbd, logF, dlogF, logF_mod, include_rv, model): """ Returns the likelihood probability function. # p0 = mass # p1 = oblat # p2 = age # p3 = inclination # p4 = ebmv # p5 = distance """ if model == 'befavor' or model == 'befavor_new': dist = params[4] ebmv = params[5] if model == 'aara' or model == 'acol' or\ model == 'bcmi': dist = params[7] ebmv = params[8] if model == 'beatlas': dist = params[5] ebmv = params[6] if include_rv is True: RV = params[6] else: RV = 3.1 if model == 'bcmi_pol' or model == 'aara_pol': # print(np.mean(logF), np.mean(logF_mod), np.mean(dlogF)) chi2 = np.sum((logF - logF_mod)**2 / (dlogF)**2.) else: norma = (10 / dist)**2 uplim = dlogF == 0 keep = np.logical_not(uplim) logF_mod += np.log10(norma) # print(logF_mod) tmp_flux = 10**logF_mod flux_mod = pyasl.unred(lbd * 1e4, tmp_flux, ebv=-1 * ebmv, R_V=RV) # print(flux_mod) logF_mod = np.log10(flux_mod) # print(logF_mod) chi2 = np.sum(((logF[keep] - logF_mod[keep])**2 / (dlogF[keep])**2.)) # chi2 = np.sum((logF - logF_mod)**2 / (dlogF)**2.) if chi2 is np.nan: chi2 = np.inf return -0.5 * chi2
def lnlike(params, lbd, logF, dlogF, logF_mod, ranges, include_rv, model): """ Returns the likelihood probability function. # p0 = mass # p1 = oblat # p2 = age # p3 = inclination # p4 = ebmv # p5 = parallax """ if model == 'befavor': plx = params[4] ebmv = params[5] if model == 'aara' or model == 'acol' or model == 'bcmi': plx = params[7] ebmv = params[8] if model == 'beatlas': plx = params[5] ebmv = params[6] if include_rv is True: RV = params[6] else: RV = 3.1 dist = 1e3 / plx norma = (10 / dist)**2 uplim = dlogF == 0 keep = np.logical_not(uplim) logF_mod += np.log10(norma) tmp_flux = 10**logF_mod flux_mod = pyasl.unred(lbd * 1e4, tmp_flux, ebv=-1 * ebmv, R_V=RV) logF_mod = np.log10(flux_mod) if uplim.any(): if (logF_mod[uplim] > logF[uplim]).any(): chi2 = np.inf else: chi2 = np.sum(((logF[keep] - logF_mod[keep])**2 / (dlogF[keep])**2.)) # chi2 = np.sum((logF - logF_mod)**2 / (dlogF)**2.) if chi2 is np.nan: chi2 = np.inf return -0.5 * chi2
def sanity_example(self): """ Check the example """ from PyAstronomy import pyasl import numpy as np import matplotlib.pylab as plt # Approximate a solar spectrum using a Planck # function with a temperature of 5778 K between # 3000 A and 8000 A. wvl = np.arange(3000., 8000., 1.0) flux = pyasl.planck(T=5778., lam=wvl*1e-10) # Deredden the spectrum assuming ebv=0.1 fluxUnred = pyasl.unred(wvl, flux, ebv=0.1, R_V=3.1) # Plot the result plt.title("Reddened flux (red) and dereddened flux (blue)") plt.plot(wvl, flux, 'r--') plt.plot(wvl, fluxUnred, 'b--')
def lnlike(params, logF_mod): """ Returns the likelihood probability function (-0.5 * chi2). Usage prob = lnlike(params, logF_mod) """ if flag.SED: u = np.where(info.lista_obs == 'UV') index = u[0][0] lbd_UV = info.wave[index] logF_UV = info.logF[index] dlogF_UV = info.dlogF[index] if flag.model == 'befavor' or flag.model == 'aeri': dist = params[4] ebmv = params[5] if flag.model == 'aara' or flag.model == 'acol': dist = params[7] ebmv = params[8] if flag.model == 'beatlas': dist = params[5] ebmv = params[6] if flag.include_rv and not flag.binary_star: RV = params[-1] elif flag.binary_star: RV = params[-2] else: RV = 3.1 #print(logF_UV) dist = 1e3 / dist norma = (10 / dist)**2 uplim = dlogF_UV == 0.0 keep = np.logical_not(uplim) logF_mod[index] += np.log10(norma) tmp_flux = 10**logF_mod[index] flux_mod = pyasl.unred(info.wave[index] * 1e4, tmp_flux, ebv=-1 * ebmv, R_V=RV) logF_mod_UV = np.log10(flux_mod) rms = np.array([ jy2cgs(1e-3 * 0.1, 20000), jy2cgs(1e-3 * 0.1, 35000), jy2cgs(1e-3 * 0.1, 63000) ]) #rms = np.array([1e-3*0.1, 1e-3*0.1, 1e-3*0.1]) # upper_lim = jy2cgs(10**logF_UV[uplim], lbd_UV[uplim], inverse=True) mod_upper = jy2cgs(10**logF_mod_UV[uplim], lbd_UV[uplim], inverse=True) #a parte dos uplims não é em log! chi2_UV = np.sum( ((logF_UV[keep] - logF_mod_UV[keep])**2. / (dlogF_UV[keep])**2.)) #chi2_uplim = - 2. * np.sum( np.log( (np.pi/2.)**0.5 * rms * (1. + erf(((upper_lim - mod_upper)/((2**0.5)*rms)))))) #chi2_UV = chi2_UV + chi2_uplim N_UV = len(logF_UV[keep]) chi2_UV_red = chi2_UV / N_UV #print('chi2_UV = {:.2f}'.format(chi2_UV)) #print(upper_lim-mod_upper) #print('chi2_uplim = {:.2f}'.format(chi2_uplim)) else: chi2_UV_red = 0. N_UV = 0. #if flag.Ha: chi2_Ha_red, N_Ha = get_line_chi2(flag.Ha, 'Ha', logF_mod) #if flag.Hb: chi2_Hb_red, N_Hb = get_line_chi2(flag.Hb, 'Hb', logF_mod) #if flag.Hd: chi2_Hd_red, N_Hd = get_line_chi2(flag.Hd, 'Hd', logF_mod) #if flag.Hg: chi2_Hg_red, N_Hg = get_line_chi2(flag.Hg, 'Hg', logF_mod) if flag.pol: chi2 = np.sum((info.logF[0] - logF_mod)**2 / (info.dlogF[0])**2.) else: chi2 = (chi2_UV_red + chi2_Ha_red + chi2_Hb_red+ chi2_Hd_red+ chi2_Hg_red)\ *(N_UV + N_Ha + N_Hb + N_Hd+ N_Hg) #print(chi2) if chi2 is np.nan: chi2 = np.inf return -0.5 * chi2
def plot_fit(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid, isig, dims, Nwalk, Nmcmc, par_list, ranges, include_rv, npy, log_scale): ''' Plots best model fit over data Usage: plot_fit(par, lbd, logF, dlogF, minfo, logF_grid, isig, Nwalk, Nmcmc) where par = np.array([Mstar, oblat, Sig0, Rd, n, cosi, dist]) ''' # model parameters if include_rv is True: Mstar, oblat, Hfrac, cosi, dist, ebv, rv = par lim = 3 lim2 = 2 else: Mstar, oblat, Hfrac, cosi, dist, ebv = par lim = 2 lim2 = 1 rv = 3.1 # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables) # t = np.max(np.array([hfrac2tms(hfrac), 0.])) t = np.max(np.array([hfrac2tms(Hfrac), 0.])) Rpole, logL = geneva_interp_fast(Mstar, oblat, t, neighbours_only=True, isRpole=False) norma = (10. / dist)**2 # (Lstar*Lsun) / (4. * pi * (dist*pc)**2) uplim = dlogF == 0 keep = np.logical_not(uplim) # chain = np.load(npy) # interpolate models logF_mod = griddataBA(minfo, logF_grid, par[:-lim], listpar, dims) logF_list = np.zeros([len(par_list), len(logF_mod)]) chi2 = np.zeros(len(logF_list)) for i in range(len(par_list)): logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :-lim], listpar, dims) # convert to physical units logF_mod += np.log10(norma) logF_list += np.log10(norma) for j in range(len(logF_list)): chi2[j] = np.sum( (logF[keep] - logF_list[j][keep])**2 / (dlogF[keep])**2) # chib = chi2[np.argsort(chi2)[-30:]] / max(chi2) flux = 10.**logF dflux = dlogF * flux flux_mod = 10.**logF_mod flux_mod = pyasl.unred(lbd * 1e4, flux_mod, ebv=-1 * ebv, R_V=rv) # Plot definitions bottom, left = 0.75, 0.48 width, height = 0.96 - left, 0.97 - bottom plt.axes([left, bottom, width, height]) # plot fit if log_scale is True: for i in range(len(par_list)): if i % 80 == 0: ebv_temp = np.copy(par_list[i][-lim2]) F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd, lbd * F_temp, color='gray', alpha=0.1) plt.errorbar(lbd, lbd * flux, yerr=lbd * dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) plt.plot(lbd, lbd * flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.xlabel('$\lambda\,\mathrm{[\mu m]}$', fontsize=20) plt.ylabel(r'$\lambda F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}]}$', fontsize=20) plt.yscale('log') else: for i in range(len(par_list)): if i % 80 == 0: ebv_temp = np.copy(par_list[i][-lim2]) F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd, F_temp, color='gray', alpha=0.1) plt.errorbar(lbd, flux, yerr=lbd * dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) plt.plot(lbd, flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.xlabel('$\lambda\,\mathrm{[\mu m]}$', fontsize=20) plt.ylabel(r'$F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2} \mu m]}$', fontsize=20) plt.xlim(min(lbd), max(lbd)) plt.tick_params(direction='in', length=6, width=2, colors='gray', which='both') plt.legend(loc='upper right') return
def plot_residuals(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid, isig, dims, Nwalk, Nmcmc, ranges, include_rv, npy, log_scale, model): ''' Plots best model fit over data Usage: plot_fit(par, lbd, logF, dlogF, minfo, logF_grid, isig, Nwalk, Nmcmc) where par = np.array([Mstar, oblat, Sig0, Rd, n, cosi, dist]) ''' # model parameters if include_rv is True: Mstar, oblat, Hfrac, cosi, dist, ebv, rv = par lim = 3 lim2 = 2 else: if model == 'befavor': Mstar, oblat, Hfrac, cosi, dist, ebv = par if model == 'aara' or model == 'acol' or model == 'bcmi': Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv = par if model == 'beatlas': Mstar, oblat, Sig0, n, cosi, dist, ebv = par Hfrac = 0.3 lim = 2 lim2 = 1 rv = 3.1 # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables) # t = np.max(np.array([hfrac2tms(hfrac), 0.])) t = np.max(np.array([hfrac2tms(Hfrac), 0.])) Rpole, logL = geneva_interp_fast(Mstar, oblat, t, neighbours_only=True, isRpole=False) norma = (10. / dist)**2 # (Lstar*Lsun) / (4. * pi * (dist*pc)**2) uplim = dlogF == 0 keep = np.logical_not(uplim) # *** chain = np.load(npy) par_list = chain[:, -1, :] # interpolate models if model == 'beatlas': logF_mod = griddataBAtlas(minfo, logF_grid, par[:-lim], listpar, dims, isig) else: logF_mod = griddataBA(minfo, logF_grid, par[:-lim], listpar, dims) logF_list = np.zeros([len(par_list), len(logF_mod)]) chi2 = np.zeros(len(logF_list)) for i in range(len(par_list)): if model == 'beatlas': logF_list[i] = griddataBAtlas(minfo, logF_grid, par_list[i, :-lim], listpar, dims, isig) else: logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :-lim], listpar, dims) # convert to physical units logF_mod += np.log10(norma) logF_list += np.log10(norma) for j in range(len(logF_list)): chi2[j] = np.sum( (logF[keep] - logF_list[j][keep])**2 / (dlogF[keep])**2) # chib = chi2[np.argsort(chi2)[-30:]] / max(chi2) flux = 10.**logF dflux = dlogF * flux flux_mod = 10.**logF_mod flux_mod = pyasl.unred(lbd * 1e4, flux_mod, ebv=-1 * ebv, R_V=rv) # alphas = (1. - chi2 / max(chi2)) / 50. # Plot definitions bottom, left = 0.71, 0.48 width, height = 0.96 - left, 0.785 - bottom plt.axes([left, bottom, width, height]) # plot fit for i in range(len(par_list)): ebv_temp = np.copy(par_list[i][-lim2]) F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd, (flux - F_temp) / dflux, 'bs', alpha=0.2) # plt.plot(lbd, (flux - flux_mod) / dflux, 'bs', markersize=5, alpha=0.1) # plt.ylabel('$(\mathrm{F}-F_\mathrm{model})/\sigma$', fontsize=20) plt.ylabel('$(F-F_\mathrm{m})/\sigma$', fontsize=20) plt.xlabel('$\lambda\,\mathrm{[\mu m]}$', fontsize=20) # plt.ylim(-100, 100) plt.hlines(y=0, xmin=min(lbd), xmax=max(lbd), linestyles='--', color='black') plt.xlim(min(lbd), max(lbd)) if model == 'aara' or model == 'beatlas' or\ model == 'acol' or model == 'bcmi': plt.xscale('log') plt.tick_params(direction='in', length=6, width=2, colors='gray', which='both') plt.legend(loc='upper right') return
def main(): # 1. Reading the Paris DR12Q catalog X = cfg.ConfigMap('MakeCat') if (X == 'FAIL'): sys.exit("Errors! Couldn't locate section in .ini file") data = fitsio.read(X['drqcat'], ext=int(X['drq_dataext'])) plate, mjd, fiberid = data['PLATE'], data['MJD'], data['FIBERID'] # Which redshift to use? z_use = data[cfg.drq_z] # Getting the extinction g_ext = data['EXTINCTION'][:, 1] ebv = g_ext / 3.793 # ref: Schlegel, Finkbeiner & Davis 1998 (ApJ 500, 525, 1998; SFD98) print("The total number of objects in the catalog are %d" % len(ebv)) # ------------------------------------------------------------------------- # BAL flagged from visual inspection # Ref: http://www.sdss.org/dr12/algorithms/boss-dr12-quasar-catalog/ balFlag = data['BAL_FLAG_VI'] # Ref: http://www.sdss.org/dr12/algorithms/bitmasks/#BOSSTILE_STATUS # targetFlag = data['BOSS_TARGET1'] ----- currently using everything ind = np.where((balFlag == 0) & (data['ZWARNING'] == 0) & (z_use > float(cfg.zmin)))[0] # -------------------------------------------------------------------------- # 2b. Remove DLA's - Ref: (Noterdaeme et al. 2012c) # http://adsabs.harvard.edu/abs/2012A%26A...547L...1N dla_data = np.loadtxt(X['dla_file'], skiprows=2, usecols=([1]), dtype='str').T dla_list = np.array([dla_data[i].split('-') for i in range(len(dla_data))]) myinfo = np.array([mjd[ind], plate[ind], fiberid[ind]]).T target = dict((tuple(k), i) for i, k in enumerate(myinfo)) candidate = [tuple(i) for i in dla_list.astype(int)] # Way faster than before - http://stackoverflow.com/questions/1388818/ # how-can-i-compare-two-lists-in-python-and-return-matches inter = set(target).intersection(candidate) indices = [target[x] for x in inter] ind[indices] = -1 print("The total number of DLAs removed are %d" % len(indices)) ind = ind[ind != -1] # In case a test run is needed if cfg.run != 0: ind = np.random.choice(ind, size=cfg.run, replace=False) # -------------------------------------------------------------------------- # Load in the sky-lines file - There are a lot of skylines!!! spec_dir = X['spec_dir'] skylines = np.loadtxt(X['skyline_file']).T # These will be written to the final catalogue calFlux, calInvar = np.zeros((len(ind), cfg.NPIX)), np.zeros( (len(ind), cfg.NPIX)) nObj = len(ind) print("Processing data. The total number of objects in" "the final catalog are % d" % nObj) # -------------------------------------------------------------------------- start_time = timeit.default_timer() for k in range(nObj): x = ind[k] # Fetch the spectra SpecName = os.path.join( spec_dir, str(plate[x]), 'spec-%04d-%5d-%04d.fits' % (plate[x], mjd[x], fiberid[x])) try: a = fitsio.read(SpecName, ext=1) except IOError: continue else: corr_flux, corr_ivar, loglam = a['flux'], a['ivar'], a['loglam'] # Deredden the flux vector using the Fitzpatrick parameterization corr_flux, corr_ivar = pyasl.unred(10**loglam, corr_flux, corr_ivar, ebv=ebv[x]) # 6. Shift to rest-frame of the quasar log2rest = loglam - np.log10(1 + z_use[x]) # Nearest pixel assignment for flux and invar s = np.floor((log2rest - cfg.COEFF0) / cfg.COEFF1).astype(int) temp1 = np.where((s >= 0) & (s < cfg.NPIX))[0] calFlux[k, s[temp1]], calInvar[ k, s[temp1]] = corr_flux[temp1], corr_ivar[temp1] # More efficient code for masking of skylines - halves the time as used before minSky = np.log10(skylines - 2) - np.log10(1 + z_use[x]) maxSky = np.log10(skylines + 2) - np.log10(1 + z_use[x]) minT = np.floor((minSky - cfg.COEFF0) / cfg.COEFF1).astype(int) maxT = np.ceil((maxSky - cfg.COEFF0) / cfg.COEFF1).astype(int) for t in range(len(minT)): calInvar[k, minT[t]:maxT[t]] = 0 elapsed_time = timeit.default_timer() - start_time print(elapsed_time) # 7. Write everything to a fits file cat_file = X['cat_file'] if os.path.exists(cat_file): os.remove(cat_file) fits = fitsio.FITS(cat_file, 'rw') mydict = OrderedDict() keys = X['paramstowrite'].split(', ') for key in keys: mydict[key] = data[key][ind] # Write images and table to fits file hdict = { 'COEFF0': cfg.COEFF0, 'COEFF1': cfg.COEFF1, 'NPIX': cfg.NPIX, 'AUTHOR': X['author'] } fits.write(calFlux, header=hdict) fits.write(calInvar) fits.write(mydict) fits.close() print("Creation of catalogue complete. The file is %s." % cat_file)
def plot_fit_last(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid, isig, dims, Nwalk, Nmcmc, ranges, include_rv, npy, log_scale, model): ''' Plots best model fit over data Usage: plot_fit(par, lbd, logF, dlogF, minfo, logF_grid, isig, Nwalk, Nmcmc) where par = np.array([Mstar, oblat, Sig0, Rd, n, cosi, dist]) ''' # model parameters if include_rv is True: if model == 'befavor': Mstar, oblat, Hfrac, cosi, dist, ebv, rv = par if model == 'befavor_new': Mstar, oblat, t, cosi, dist, ebv, rv = par if model == 'aara' or model == 'acol' or\ model == 'bcmi': Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv, rv = par if model == 'bcmi_pol' or model == 'aara_pol': Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par if model == 'beatlas': Mstar, oblat, Sig0, n, cosi, dist, ebv, rv = par Hfrac = 0.30 lim = 3 lim2 = 2 else: if model == 'befavor': Mstar, oblat, Hfrac, cosi, dist, ebv = par if model == 'befavor_new': Mstar, oblat, t, cosi, dist, ebv = par if model == 'aara' or model == 'acol' or\ model == 'bcmi': Mstar, oblat, Hfrac, Sig0, Rd, n, cosi, dist, ebv = par if model == 'bcmi_pol' or model == 'aara_pol': Mstar, oblat, Hfrac, Sig0, Rd, n, cosi = par if model == 'beatlas': Mstar, oblat, Sig0, n, cosi, dist, ebv = par Hfrac = 0.30 lim = 2 lim2 = 1 rv = 3.1 # Rpole, Lstar, Teff = vkg.geneve_par(Mstar, oblat, Hfrac, folder_tables) # t = np.max(np.array([hfrac2tms(hfrac), 0.])) if model != 'befavor_new': t = np.max(np.array([hfrac2tms(Hfrac), 0.])) Rpole, logL = geneva_interp_fast(Mstar, oblat, t, neighbours_only=True, isRpole=False) if model != 'bcmi_pol' and model != 'aara_pol': norma = (10. / dist)**2 # (Lstar*Lsun) / (4. * pi * (dist*pc)**2) uplim = dlogF == 0 keep = np.logical_not(uplim) # *** chain = np.load(npy) par_list = chain[:, -1, :] # interpolate model if model == 'beatlas': logF_mod = griddataBAtlas(minfo, logF_grid, par[:-lim], listpar, dims, isig) elif model == 'befavor_new': # print(isig, par[:-lim]) logF_mod = bat.griddataBA(minfo, logF_grid, par[:-lim], isig=isig, silent=True) elif model == 'bcmi_pol' or model == 'aara_pol': logF_mod = griddataBA(minfo, logF_grid, par, listpar, dims) else: logF_mod = griddataBA(minfo, logF_grid, par[:-lim], listpar, dims) logF_list = np.zeros([len(par_list), len(logF_mod)]) chi2 = np.zeros(len(logF_list)) for i in range(len(par_list)): if model == 'beatlas': logF_list[i] = griddataBAtlas(minfo, logF_grid, par_list[i, :-lim], listpar, dims, isig) elif model == 'befavor_new': logF_list[i] = bat.griddataBA(minfo, logF_grid, par_list[i, :-lim], isig=isig, silent=True) elif model == 'bcmi_pol' or model == 'aara_pol': logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :], listpar, dims) else: logF_list[i] = griddataBA(minfo, logF_grid, par_list[i, :-lim], listpar, dims) # convert to physical units if model != 'bcmi_pol' and model != 'aara_pol': logF_mod += np.log10(norma) logF_list += np.log10(norma) for j in range(len(logF_list)): chi2[j] = np.sum( (logF[keep] - logF_list[j][keep])**2 / (dlogF[keep])**2) flux = 10.**logF dflux = dlogF * flux flux_mod = 10.**logF_mod if model != 'bcmi_pol' and model != 'aara_pol': flux_mod = pyasl.unred(lbd * 1e4, flux_mod, ebv=-1 * ebv, R_V=rv) # Plot definitions bottom, left = 0.79, 0.51 #0.80, 0.48 # 0.75, 0.48 width, height = 0.96 - left, 0.97 - bottom plt.axes([left, bottom, width, height]) # plot fit if log_scale is True: for i in range(len(par_list)): if model != 'bcmi_pol' and model != 'aara_pol': ebv_temp = np.copy(par_list[i][-lim2]) F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd, lbd * F_temp, color='gray', alpha=0.1) else: F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=0, R_V=rv) plt.plot(lbd, F_temp, color='gray', alpha=0.1) if model != 'bcmi_pol' and model != 'aara_pol': plt.plot(lbd, lbd * flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.errorbar(lbd, lbd * flux, yerr=lbd * dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) else: plt.plot(lbd, flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.errorbar(lbd, flux, yerr=dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) if model != 'bcmi_pol' and model != 'aara_pol': plt.ylabel( r'$\lambda F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}]}$', fontsize=17) plt.xscale('log') else: plt.ylabel(r'$P [\%]$', fontsize=17) plt.tick_params(labelbottom='off') else: for i in range(len(par_list)): if model != 'bcmi_pol' and model != 'aara_pol': ebv_temp = np.copy(par_list[i][-lim2]) F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd, lbd * F_temp, color='gray', alpha=0.1) else: F_temp = pyasl.unred(lbd * 1e4, 10**logF_list[i], ebv=0, R_V=rv) plt.plot(lbd, F_temp, color='gray', alpha=0.1) if model != 'bcmi_pol' and model != 'aara_pol': plt.plot(lbd, lbd * flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.errorbar(lbd, lbd * flux, yerr=lbd * dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) plt.ylabel( r'$\lambda F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}]}$', fontsize=20) else: plt.plot(lbd, flux_mod, color='red', ls='-', lw=3.5, alpha=0.4, label='$\mathrm{Best\, fit}$') plt.errorbar(lbd, flux, yerr=dflux, ls='', marker='o', alpha=0.5, ms=10, color='blue', linewidth=2) plt.ylabel(r'$P [\%]$', fontsize=20) plt.tick_params(labelbottom='off') plt.xscale('linear') plt.xlim(min(lbd), max(lbd)) if model != 'bcmi_pol' and model != 'aara_pol': plt.yscale('log') if model == 'aara' or model == 'beatlas' or\ model == 'acol' or model == 'bcmi': plt.xscale('log') if log_scale is True: plt.xscale('log') else: plt.xscale('linear') if model == 'bcmi_pol' or model == 'aara_pol': plt.xlim(0.9 * min(lbd), 1.1 * max(lbd)) plt.tick_params(direction='in', length=6, width=2, colors='gray', which='both') plt.legend(loc='upper right') return
def plot_residuals_new(par, lbd, logF, dlogF, minfo, listpar, lbdarr, logF_grid, isig, dims, Nwalk, Nmcmc, npy, box_lim, lista_obs, current_folder, fig_name): ''' Create residuals plot separated from the corner ''' chain = np.load(npy) par_list = [] flat_samples = chain.reshape((-1, Ndim)) inds = np.random.randint(len(flat_samples), size=100) for ind in inds: params = flat_samples[ind] par_list.append(params) #oblat = 1 + 0.5*(W**2) # Rimulo 2017 #Rpole, logL, _ = geneva_interp_fast(Mstar, oblat, tms, Zstr='014') #print(cosi) #cosi = np.cos(cosi * np.pi/180.) #par[3] = np.cos(par[3] * np.pi/180.) #print(cosi) # *** #chain = np.load(npy) #par_list = chain[:, -1, :] if flag.SED: if flag.model == 'aeri': dist = par[4][0] ebv = par[5][0] if flag.include_rv: rv = par[6][0] else: rv = 3.1 elif flag.model == 'acol': dist = par[7][0] ebv = par[8][0] if flag.include_rv: rv = par[9][0] else: rv = 3.1 elif flag.model == 'beatlas': dist = par[5][0] ebv = par[6][0] if flag.include_rv: rv = par[7][0] else: rv = 3.1 lim = find_lim() # Finding position u = np.where(lista_obs == 'UV') index = u[0][0] # Finding the corresponding flag.model (interpolation) #logF_mod_UV = griddataBA(minfo, logF_grid[index], par[:-lim], # listpar, dims) # Observations logF_UV = logF[index] flux_UV = 10.**logF_UV dlogF_UV = dlogF[index] lbd_UV = lbd[index] dist = 1e3 / dist norma = (10. / dist)**2 # (Lstar*Lsun) / (4. * pi * (dist*pc)**2) uplim = dlogF[index] == 0 keep = np.logical_not(uplim) # convert to physical units #logF_mod_UV += np.log10(norma) #flux_mod_UV = 10.**logF_mod_UV dflux = dlogF_UV * flux_UV #flux_mod_UV = pyasl.unred(lbd_UV * 1e4, flux_mod_UV, ebv=-1 * ebv, R_V=rv) #chi2_UV = np.sum((flux_UV - flux_mod_UV)**2. / dflux**2.) N_UV = len(logF_UV) #chi2_UV = chi2_UV/N_UV logF_list = np.zeros([len(par_list), len(logF_UV)]) #chi2 = np.zeros(len(logF_list)) #for i in range(len(par_list)): # logF_list[i] = griddataBA(minfo, logF_grid[index], par_list[i, :-lim], # listpar, dims) for i, params in enumerate(par_list): if flag.binary_star: logF_mod_UV_1_list = griddataBA(minfo, logF_grid[index], params[:-lim], listpar, dims) logF_mod_UV_2_list = griddataBA( minfo, logF_grid[index], np.array([params[-1], 0.1, params[2], params[3]]), info.listpar, info.dims) logF_list[i] = np.log10(10.**np.array(logF_mod_UV_1_list) + 10.**np.array(logF_mod_UV_2_list)) else: if flag.model != 'beatlas': logF_list[i] = griddataBA(minfo, logF_grid[index], params[:-lim], listpar, dims) else: logF_list[i] = griddataBAtlas(minfo, logF_grid[index], params[:-lim], listpar, dims, isig=dims["sig0"]) #par_list[i] = params #logF_list += np.log10(norma) # Plot #fig, (ax1,ax2) = plt.subplots(2,1,gridspec_kw={'height_ratios': [3, 1]}) logF_list += np.log10(norma) bottom, left = 0.84, 0.51 #0.80, 0.48 # 0.75, 0.48 width, height = 0.96 - left, 0.97 - bottom ax1 = plt.axes([left, bottom, width, height]) #for j in range(len(logF_list)): # chi2[j] = np.sum((logF_UV[keep] - logF_list[j][keep])**2 / (dlogF_UV[keep])**2) # Plot Models for i in range(len(par_list)): if flag.model == 'aeri': ebv_temp = par_list[i][5] elif flag.model == 'beatlas': ebv_temp = par_list[i][6] else: ebv_temp = par_list[i][8] #ebv_temp = np.copy(ebv) F_temp = pyasl.unred(lbd_UV * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) plt.plot(lbd_UV, F_temp, color='gray', alpha=0.1) # Residuals --- desta forma plota os residuos de todos os modelos, mas acho que nao eh o que quero #ax2.plot(lbd_UV, (flux_UV - F_temp) / dflux, 'bs', alpha=0.2) # Applying reddening to the best model # Best fit #ax1.plot(lbd_UV, flux_mod_UV, color='red', ls='-', lw=3.5, alpha=0.4, # label=r'Best Fit' '\n' '$\chi^2$ = {0:.2f}'.format(chi2_UV)) #ax2.plot(lbd_UV, (flux_UV - flux_mod_UV) / dflux, 'bs', alpha=0.2) #ax2.set_ylim(-10,10) # Plot Data keep = np.where(flux_UV > 0) # avoid plot zero flux ax1.errorbar(lbd_UV[keep], flux_UV[keep], yerr=dflux[keep], ls='', marker='o', alpha=0.5, ms=4, color='k', linewidth=1) ax1.set_xlabel('$\lambda\,\mathrm{[\mu m]}$', fontsize=14) ax1.set_ylabel( r'$F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2} \mu m^{-1}}$', fontsize=14) #ax2.set_ylabel('$(F-F_\mathrm{m})/\sigma$', fontsize=14) #plt.tick_params(labelbottom='off') ax1.set_xlim(min(lbd_UV), max(lbd_UV)) #ax2.set_xlim(min(lbd_UV), max(lbd_UV)) #plt.tick_params(direction='in', length=6, width=2, colors='gray', # which='both') #ax1.legend(loc='upper right', fontsize=13) ax1.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) #plt.tight_layout() #plt.savefig(current_folder + fig_name + '_new_residuals-UV-REDONE' + '.png', dpi=100) #plt.close() if flag.Ha: # Finding position line = 'Ha' plot_line(line, lista_obs, minfo, logF_grid, par, listpar, dims, logF, dlogF, lbd, par_list, current_folder, fig_name) if flag.Hb: line = 'Hb' plot_line(line, lista_obs, minfo, logF_grid, par, listpar, dims, logF, dlogF, lbd, par_list, current_folder, fig_name) if flag.Hd: line = 'Hd' plot_line(line, lista_obs, minfo, logF_grid, par, listpar, dims, logF, dlogF, lbd, par_list, current_folder, fig_name) if flag.Hg: line = 'Hg' plot_line(line, lista_obs, minfo, logF_grid, par, listpar, dims, logF, dlogF, lbd, par_list, current_folder, fig_name)
#ebv = 0.99 # 3D map Bayestar17 R_V = 3.1 # (Fitzpatrick and Massa 2007) #R_V = 3.1 A_V = 2.4 #Claire ebv = A_V / R_V somme_file = "/Users/azuri/daten/uni/HKU/Pa30/_pa30_somme6_scaled.fits" somme_hdulist = pyfits.open(somme_file) somme_header = somme_hdulist[0].header somme_wavelength = getWavelength(somme_header, 1) somme_spectrum = fits.getdata(somme_file) print('somme_wavelength = ', somme_wavelength) somme_spectrum_smoothed = ndimage.median_filter(somme_spectrum, size=21) somme_spectrum_smoothed_dereddened = pyasl.unred(somme_wavelength, somme_spectrum_smoothed, ebv=ebv, R_V=R_V) plt.plot(somme_wavelength, np.log10(somme_spectrum_smoothed), 'm-', label='Buil scaled and smoothed') plt.plot(somme_wavelength, np.log10(somme_spectrum_smoothed_dereddened), 'm-') #, label='Somme scaled, smoothed, and dereddened') plt.legend() plotname = '/Users/azuri/daten/uni/HKU/Pa30/pa30_buil_uvuex.pdf' plt.savefig(plotname, format='pdf', frameon=False, bbox_inches='tight', pad_inches=0.1) plt.show()
somme_header = somme_hdulist[0].header somme_wavelength = getWavelength(somme_header, 1) somme_spectrum = fits.getdata(somme_file) # somme_spectrum_absolute = calibratedFluxToAbsoluteFlux(somme_spectrum, 3370.0) print('somme_wavelength = ', somme_wavelength) leDu_spectrum_smoothed = scipy.ndimage.median_filter( leDu_spectrum, 21) #boxCarMeanSmooth(leDu_spectrum, 0, 21) somme_spectrum_smoothed = scipy.ndimage.median_filter( somme_spectrum, 21) #boxCarMeanSmooth(somme_spectrum, 0, 21) #leDu_spectrum_absolute_smoothed = scipy.ndimage.median_filter(leDu_spectrumboxCarMeanSmooth(leDu_spectrum_absolute, 0, 21) #somme_spectrum_absolute_smoothed = boxCarMeanSmooth(somme_spectrum_absolute, 0, 21) leDu_spectrum_smoothed_dereddened = pyasl.unred(leDu_wavelength, leDu_spectrum_smoothed, ebv=ebv, R_V=R_V) plt.plot(leDu_wavelength, np.log10(leDu_spectrum_smoothed), '-', color='#64a030', label='$\mathrm{LeD\^u\,scaled\,and\,smoothed}$') plt.plot(leDu_wavelength, np.log10(leDu_spectrum_smoothed_dereddened), '-', color='#64a030') #, label='Le Du scaled, smoothed, and dereddened') somme_spectrum_smoothed_dereddened = pyasl.unred(somme_wavelength, somme_spectrum_smoothed, ebv=ebv, R_V=R_V)
def plot_residuals(par, npy, current_folder, fig_name): ''' Create residuals plot separated from the corner For the SED and the lines Usage: plot_residuals(par, npy, current_folder, fig_name) ''' plt.rc('xtick', labelsize='medium') plt.rc('ytick', labelsize='medium') chain = np.load(npy) par_list = [] flat_samples = chain.reshape((-1, info.Ndim)) inds = np.random.randint(len(flat_samples), size=100) for ind in inds: params = flat_samples[ind] par_list.append(params) if flag.SED: if flag.model == 'aeri': dist = par[4][0] ebv = par[5][0] if flag.include_rv: rv = par[6][0] else: rv = 3.1 elif flag.model == 'acol': dist = par[7][0] ebv = par[8][0] if flag.include_rv: rv = par[9][0] else: rv = 3.1 elif flag.model == 'beatlas': dist = par[5][0] ebv = par[6][0] if flag.include_rv: rv = par[7][0] else: rv = 3.1 #print(dist) u = np.where(info.lista_obs == 'UV') index = u[0][0] # Observations logF_UV = info.logF[index] flux_UV = 10.**logF_UV dlogF_UV = info.dlogF[index] lbd_UV = info.wave[index] dist = 1e3 / dist norma = (10. / dist)**2 # (Lstar*Lsun) / (4. * pi * (dist*pc)**2) uplim = info.dlogF[index] == 0.0 keep = np.logical_not(uplim) dflux = dlogF_UV * flux_UV logF_list = np.zeros([len(par_list), len(logF_UV)]) #par_list = chain[:, -1, :] #inds = np.random.randint(len(flat_samples), size=100) for i, params in enumerate(par_list): if flag.binary_star: logF_mod_UV_1_list = griddataBA(info.minfo, info.logF_grid[index], params[:-info.lim], info.listpar, info.dims) logF_mod_UV_2_list = griddataBA( info.minfo, info.logF_grid[index], np.array([params[-1], 0.1, params[2], params[3]]), info.listpar, info.dims) logF_list[i] = np.log10(10.**np.array(logF_mod_UV_1_list) + 10.**np.array(logF_mod_UV_2_list)) else: if flag.model != 'beatlas': logF_list[i] = griddataBA(info.minfo, info.logF_grid[index], params[:-info.lim], info.listpar, info.dims) else: logF_list[i] = griddataBAtlas(info.minfo, info.logF_grid[index], params[:-info.lim], info.listpar, info.dims, isig=info.dims["sig0"]) #par_list[i] = params logF_list += np.log10(norma) fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [3, 1]}) # Plot Models for i in range(len(par_list)): if flag.model == 'aeri': ebv_temp = par_list[i][5] elif flag.model == 'beatlas': ebv_temp = par_list[i][6] else: ebv_temp = par_list[i][8] F_temp = pyasl.unred(lbd_UV * 1e4, 10**logF_list[i], ebv=-1 * ebv_temp, R_V=rv) ax1.plot(lbd_UV, F_temp, color='gray', alpha=0.1, lw=0.6) ax2.plot(lbd_UV, (flux_UV - F_temp) / dflux, 'ks', ms=5, alpha=0.2) #ax2.set_ylim(-10,10) if flag.votable or flag.data_table: #ax2.set_xscale('log') ax1.set_xscale('log') ax1.set_yscale('log') # Plot Data keep = np.where(flux_UV > 0) # avoid plot zero flux ax1.errorbar(lbd_UV[keep], flux_UV[keep], yerr=dflux[keep], ls='', marker='o', alpha=0.5, ms=5, color='k', linewidth=1) ax2.set_xlabel('$\lambda\,\mathrm{[\mu m]}$') #, fontsize=12) ax1.set_ylabel( r'$F_{\lambda}\,\mathrm{[erg\, s^{-1}\, cm^{-2}\, \mu m^{-1}]}$') #fontsize=12) ax2.set_ylabel('$(F-F_\mathrm{m})/\sigma$') #, fontsize=12) plt.tight_layout() plt.savefig(current_folder + fig_name + '_new_residuals-UV' + '.png', dpi=100) plt.close() if flag.Ha: line = 'Ha' plot_line(line, par, par_list, current_folder, fig_name) if flag.Hb: line = 'Hb' plot_line(line, par, par_list, current_folder, fig_name) if flag.Hd: line = 'Hd' plot_line(line, par, par_list, current_folder, fig_name) if flag.Hg: line = 'Hg' plot_line(line, par, par_list, current_folder, fig_name) if flag.pol: index = 0 lbd_pol = info.wave[index] flux = info.logF[index] sigma = info.dlogF[index] logF_list = np.zeros([len(par_list), len(flux)]) for i, params in enumerate(par_list): logF_list[i] = griddataBA(info.minfo, info.logF_grid[index], params, info.listpar, info.dims) fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [3, 1]}) for i in range(len(par_list)): ax1.plot(lbd_pol, logF_list[i], color='gray', alpha=0.1, lw=0.6) ax2.plot(lbd_pol, (flux - logF_list[-1]) / sigma, 'ks', ms=5, alpha=0.2) ax1.errorbar(lbd_pol, flux, yerr=sigma, ls='', marker='o', alpha=0.5, ms=5, color='k', linewidth=1) ax1.set_xlim(0.3, 0.9) ax2.set_xlabel('$\lambda\,\mathrm{[\mu m]}$') ax1.set_ylabel(r'$P_{\%}$') ax2.set_ylabel('$(P-P_\mathrm{m})/\sigma$') plt.tight_layout() plt.savefig(current_folder + fig_name + '_new_residuals-POL' + '.png', dpi=100) plt.close() return
#for chosen_bin in range(5): fig = plt.figure(figsize=(15, 5)) img_spectrum = img[:,0,0]*0 for index, bin_num in enumerate(bins): if bin_num == chosen_bin: img_spectrum = img_spectrum+img[:,y_pix[index],x_pix[index]] spaxel_x = x_pix[index]+1 spaxel_y = y_pix[index]+1 spectrum = img_spectrum if max(spectrum) < 0.00001: continue #Correction for reddening #E_B_minus_V = 1.97*np.log10(balmer_dec/2.86) if len(sys.argv) > 5: RsubV = 3.1 fluxUnred = pyasl.unred(wavelength_angstroms, spectrum, ebv=float(color_excess), R_V=RsubV) spectrum = fluxUnred noise_level = np.std(spectrum[310:390])*2 #noise_out.write(str(noise_level)+'\n') if iteration != 0: #print("Measured noise level: "+str(noise_level)) noise_array = [random.normalvariate(0.0, noise_level) for _ in xrange(len(spectrum))] spectrum = spectrum+noise_array fit_window_size = 500.0 mask_window_size = 40.0 first_cut = np.all([wavelength_angstroms>(wavelengths[1]-fit_window_size), wavelength_angstroms<(wavelengths[1]+fit_window_size)], axis=0) fit_cut = first_cut max_mask = np.all([wavelength_angstroms<3000, wavelength_angstroms>7000], axis=0) for line in wavelengths: