def get_par_from_lines(lines, blim=(2, 15), Nlim=(14, 19.), dz=5e-5): """ Get all the parameters we want to vary from a list of lines. """ P = adict(names=[], min=[], max=[], tied={}) i = 1 for l in lines: if l.zpar == '': P.names.append('z%i' % i) P.min.append(l.z - dz) P.max.append(l.z + dz) elif l.zpar.islower(): if l.zpar not in P.tied: # entry gives the index of the parameter corresponding # to this tied character. Note all tied lines must # have the same lower case character (this is # different to VPFIT, where one character is lower # case, and all the others are the same letter but # upper case) P.tied[l.zpar] = len(P.names) P.names.append('z%i' % i) P.min.append(l.z - dz) P.max.append(l.z + dz) if l.logNpar == '': P.names.append('N%i' % i) P.min.append(Nlim[0]) P.max.append(Nlim[1]) if l.bpar == '': P.names.append('b%i' % i) P.min.append(blim[0]) P.max.append(blim[1]) elif l.bpar.islower(): if l.bpar not in P.tied: # entry gives the index of the parameter corresponding # to this tied character. Note all tied lines must # have the same lower case character (this is # different to VPFIT, where one character is lower # case, and all the others are the same letter but # upper case) P.tied[l.bpar] = len(P.names) P.names.append('b%i' % i) P.min.append(blim[0]) P.max.append(blim[1]) if '' in (l.zpar, l.logNpar, l.bpar) or l.bpar.islower() or \ l.zpar.islower(): i += 1 return P
def get_fig_axes(nrows, ncols, npar, width=11.7, height=None, aspect=0.5): """ Generate a figure with a number of subplots. Parameters ---------- nrows : int Number of rows ncols : int Number of columns npar : int Number of plots in total width : float Width of the figure in inches aspect : float width / height of each sub-plot. Returns ------- fig : matplotlib figure object axes : list of matplotlib axes objects ordered from top left going down coluns. Notes """ if height is None: height = width*aspect*nrows/ncols fig = pl.figure(figsize=(width, height)) axes = [fig.add_subplot(nrows, ncols, i+1) for i in range(npar)] # reorder axes so they go from top left down columns instead of across axes1 = [] ileft = [] ibottom = [] for j in range(ncols): for i in range(nrows): ind = j + i*ncols if ind > npar - 1: continue axes1.append(axes[ind]) # find the indices of the left and bottom plots (used to set axes # labels) ileft = range(nrows) ibottom = [i*nrows - 1 for i in range(1, ncols+1)] for i in range(ncols*nrows - npar): ibottom[-(i+1)] -= ncols*nrows - npar - i velplot = adict(axes=axes1, nrows=nrows, ncols=ncols, ileft=ileft, ibottom=ibottom) return fig, velplot
def make_interpolators_uvbtilt(trans, simnames): """ Make interpolators including different UV slopes, given by the simulation names. simname naming scheme should be (uvb_k00, uvb_k01, uvb_k02, ...), uvb k values must be sorted in ascending order! """ Models = [] aUV = [] for simname in simnames: # need to define prefix, SIMNAME gridname = os.path.join(simname, 'grid.cfg') print 'Reading', gridname cfg = parse_config(gridname) aUV.append(cfg.uvb_tilt) name = os.path.join(simname, cfg.prefix + '_grid.sav.gz') print 'Reading', name M = loadobj(name) M = adict(M) Uconst = (M.U + M.nH)[0] print 'Uconst', Uconst, cfg.uvb_tilt assert np.allclose(Uconst, M.U + M.nH) Models.append(M) ########################################################################## # Interpolate cloudy grids onto a finer scale for plotting and # likelihood calculation ########################################################################## roman_map = {'I':0, 'II':1, 'III':2, 'IV':3, 'V':4, 'VI':5, 'VII':6, 'VIII':7, 'IX':8, 'X':9, '2':2} Ncloudy = {} Ncloudy_raw = {} print 'Interpolating...' for tr in trans + ['NH']: shape = len(M.NHI), len(M.nH), len(M.Z), len(aUV) Nvals = np.zeros(shape) if tr in ['CII*']: for i,M in enumerate(Models): Nvals[:,:,:,i] = M.Nex[tr][:,:,:] elif tr == 'NH': for i,M in enumerate(Models): logNHI = M.N['H'][:,:,:,0] logNHII = M.N['H'][:,:,:,1] logNHtot = np.log10(10**logNHI + 10**logNHII) Nvals[:,:,:,i] = logNHtot else: atom, stage = split_trans_name(tr) ind = roman_map[stage] for i,M in enumerate(Models): Nvals[:,:,:,i] = M.N[atom][:,:,:,ind] # use ndimage.map_coordinates (which is spline interpolation) coord = M.NHI, M.nH, M.Z, aUV try: Ncloudy[tr] = MapCoord_Interpolator(Nvals, coord) except: import pdb; pdb.set_trace() Ncloudy_raw[tr] = Nvals print 'done' return Ncloudy, Ncloudy_raw, Models, np.array(aUV, np.float)
def make_interpolators_uvbtilt(trans, simnames): """ Make interpolators including different UV slopes, given by the simulation names. simname naming scheme should be (uvb_k00, uvb_k01, uvb_k02, ...), uvb k values must be sorted in ascending order! """ Models = [] aUV = [] for simname in simnames: # need to define prefix, SIMNAME gridname = os.path.join(simname, 'grid.cfg') print 'Reading', gridname cfg = parse_config(gridname) aUV.append(cfg.uvb_tilt) name = os.path.join(simname, cfg.prefix + '_grid.sav.gz') print 'Reading', name M = loadobj(name) M = adict(M) Uconst = (M.U + M.nH)[0] print 'Uconst', Uconst, cfg.uvb_tilt assert np.allclose(Uconst, M.U + M.nH) Models.append(M) ########################################################################## # Interpolate cloudy grids onto a finer scale for plotting and # likelihood calculation ########################################################################## roman_map = { 'I': 0, 'II': 1, 'III': 2, 'IV': 3, 'V': 4, 'VI': 5, 'VII': 6, 'VIII': 7, 'IX': 8, 'X': 9, '2': 2 } Ncloudy = {} Ncloudy_raw = {} print 'Interpolating...' for tr in trans + ['NH']: shape = len(M.NHI), len(M.nH), len(M.Z), len(aUV) Nvals = np.zeros(shape) if tr in ['CII*']: for i, M in enumerate(Models): Nvals[:, :, :, i] = M.Nex[tr][:, :, :] elif tr == 'NH': for i, M in enumerate(Models): logNHI = M.N['H'][:, :, :, 0] logNHII = M.N['H'][:, :, :, 1] logNHtot = np.log10(10**logNHI + 10**logNHII) Nvals[:, :, :, i] = logNHtot else: atom, stage = split_trans_name(tr) ind = roman_map[stage] for i, M in enumerate(Models): Nvals[:, :, :, i] = M.N[atom][:, :, :, ind] # use ndimage.map_coordinates (which is spline interpolation) coord = M.NHI, M.nH, M.Z, aUV try: Ncloudy[tr] = MapCoord_Interpolator(Nvals, coord) except: import pdb pdb.set_trace() Ncloudy_raw[tr] = Nvals print 'done' return Ncloudy, Ncloudy_raw, Models, np.array(aUV, np.float)
def process_options(args): opt = adict() filename = os.path.abspath(__file__).rsplit('/', 1)[0] + '/default.cfg' opt = parse_config(filename) if os.path.lexists('./plot.cfg'): opt = parse_config('./plot.cfg', opt) opt.atom = readatom(molecules=True) if opt.Rfwhm is not None: if isinstance(opt.Rfwhm, basestring): if opt.Rfwhm == 'convolve_with_COS_FOS': if convolve_with_COS_FOS is None: raise ValueError('convolve_with_COS_FOS() not available') print('Using tailored FWHM for COS/FOS data') opt.Rfwhm = 'convolve_with_COS_FOS' elif opt.Rfwhm.endswith('fits'): print('Reading Resolution FWHM from', opt.Rfwhm) res = readtabfits(opt.Rfwhm) opt.Rfwhm = res.res / 2.354 else: print('Reading Resolution FWHM from', opt.Rfwhm) fh = open(opt.Rfwhm) opt.Rfwhm = 1 / 2.354 * np.array([float(r) for r in fh]) fh.close() else: opt.Rfwhm = float(opt.Rfwhm) if opt.features is not None: print('Reading feature list from', opt.features) opt.features = readtabfits(opt.features) if opt.f26 is not None: name = opt.f26 print('Reading ions and fitting regions from', name) opt.f26 = readf26(name) opt.f26.filename = name if opt.transitions is not None: print('Reading transitions from', opt.transitions) fh = open(opt.transitions) trans = list(fh) fh.close() temp = [] for tr in trans: tr = tr.strip() if tr and not tr.startswith('#'): junk = tr.split() tr = junk[0] + ' ' + junk[1] t = findtrans(tr, atomdat=opt.atom) temp.append(dict(name=t[0], wa=t[1][0], tr=t[1])) opt.linelist = temp else: opt.linelist = readtxt(get_data_path() + 'linelists/qsoabs_lines', names='wa,name,select') if opt.f26 is None and opt.taulines is not None: print('Reading ions from', opt.taulines) fh = open(opt.taulines) lines = [] for row in fh: if row.lstrip().startswith('#'): continue items = row.split() lines.append([items[0]] + list(map(float, items[1:]))) fh.close() opt.lines = lines if opt.show_regions is None: opt.show_regions = True if hasattr(opt, 'aodname'): opt.aod = Table.read(opt.aodname) return opt
x0,x1 = ax.get_xlim() const = (grid.U + grid.nH)[0] assert np.allclose(const, grid.U + grid.nH) ax1.set_xlim(const - x0, const - x1) ax1.set_xlabel('$\log_{10}\ U$') if 1: ############################################## # Read the model ############################################## cfg = parse_config(gridname) M = loadobj(os.path.join(prefix, simname, cfg.prefix + '_grid.sav.gz')) M = adict(M) # A finer grid of parameter values for interpolation below NHI = np.linspace(M.NHI[0], M.NHI[-1], 100) nH = np.linspace(M.nH[0], M.nH[-1], 101) Z = np.linspace(M.Z[0], M.Z[-1], 102) dNHI = NHI[1] - NHI[0] dnH = nH[1] - nH[0] dZ = Z[1] - Z[0] if 1: ############################################## # Read the observed column densities ##############################################
- a get_initial_positions(nwalkers) function that generates an array of shape (nwalkers, npar) with parameter values for the initial walker positions. - a plot_model(par) function that plots a model fit to the data given a set of parameter values, in the same order as they are listed in P. """ from __future__ import division from barak.absorb import calc_iontau, readatom from barak.utilities import adict from barak.io import writetxt import pylab as pl import numpy as np P = adict() # these are what we'll find the posterior for P.names = 'z1', 'logN1', 'b1', 'z2', 'logN2', 'b2' # these are the upper and lower limits on the flat prior ranges. these # will be used to generate guess positions for the sampler. If you get # these wrong, the sampler can get confused (poor acceptance fractions # or walkers getting lost in very low likelihood regions of parameter # space). It will take some trial and error and inspection of the # burn-in parameter distribution to find input ranges that give # plausible results. P.min = 2.4997, 12.5, 5, 2.500, 12.5, 5 P.max = 2.5003, 16.5, 70, 2.501, 15.5, 70
5 & 72 &$-0.21\pm0.11$ &$0.37\pm0.14$ &$-2.70\pm0.11$ &$\mathbf{16.37\pm0.09}$ &$18.90\pm0.16$ &$4.11\pm0.03$ &$-2.15\pm0.32(0.12)$&$1.97\pm0.32(0.12)$ &$-0.42\pm0.39(0.26)$ &$4.34\pm0.89(0.66)$ 6 & 92 &$-0.26\pm0.11$ &$0.20\pm0.18$ &$-2.66\pm0.10$ &$\mathbf{16.02\pm0.10}$ &$18.62\pm0.13$ &$4.12\pm0.03$ &$-2.21\pm0.32(0.11)$&$1.91\pm0.32(0.12)$ &$-0.65\pm0.36(0.21)$ &$3.59\pm0.80(0.53)$ 7 & 235 &$-0.69\pm0.23$ &$\mathbf{0.01\pm0.38}$ &$-2.43\pm0.17$ &$\mathbf{14.70\pm0.01}$ &$17.64\pm0.25$ &$4.27\pm0.04$ &$-2.46\pm0.37(0.21)$&$1.80\pm0.35(0.17)$ &$-1.38\pm0.55(0.46)$ &$1.17\pm1.31(1.16)$ 8 & 324 &$-1.07\pm0.42$ &$\mathbf{-0.24\pm0.48}$ &$-1.63\pm0.44$ &$\mathbf{13.59\pm0.01}$ &$17.76\pm0.57$ &$4.49\pm0.12$ &$-3.34\pm0.55(0.46)$&$1.09\pm0.45(0.34)$ &$-0.22\pm1.08(1.04)$ &$3.60\pm2.71(2.64)$ """ fh = StringIO(tab) #T = readtxt(fh, sep='&', names=['comp', 'nH', 'Z', 'aUV', 'NHI', # 'U', 'T', 'NH', 'D', 'Pk']) T = readtxt(fh, sep='&', names=['comp', 'vel', 'Z', 'aUV', 'U', 'NHI', 'NH', 'T', 'nH', 'Pk', 'D', 'M']) from barak.utilities import adict V = adict() for n in ('nH', 'Z', 'aUV','NHI','NH', 'U','T','D','Pk', 'M'): V[n] = readcol(T[n]) if n == 'T': v = V[n]['val'] V[n]['erlo'] = (10**v - 10**(v - V[n]['erlo'])) / 1e3 V[n]['erhi'] = (10**(v + V[n]['erhi']) - 10**v)/ 1e3 V[n]['val'] = 10**v / 1e3 len(V.nH) # make plots dv = np.array(T['vel']) ######################################### # make Z plot
kappa = t1 * t2 # upper limit t1 = 10**calc_abund(elnodep, 'H', Nnodephi, NHlo) t2 = (1 - 10**calc_abund(eldep, elnodep, Ndeplo, Nnodephi)) kappahi = t1 * t2 # lower limit t1 = 10**calc_abund(elnodep, 'H', Nnodeplo, NHhi) t2 = (1 - 10**calc_abund(eldep, elnodep, Ndephi, Nnodeplo)) kappalo = t1 * t2 return np.array([kappa, kappalo, kappahi]) obs = adict(read_observed('../cloudy/observed_logN/total')) from barak.abundances import calc_abund if 1: print np.log10(calc_kappa('FeII', 'OI', obs)) print np.log10(calc_kappa('FeII', 'SiII',obs)) print np.log10(calc_kappa('MgII', 'OI', obs)) print '[Fe/O]', calc_abund_range('FeII', 'OI', obs) print '[Mg/O]', calc_abund_range('MgII', 'OI', obs) print '[Fe/Si]', calc_abund_range('FeII', 'SiII',obs) print '[Mg/Si]', calc_abund_range('MgII', 'SiII',obs) print '[Si/Fe]', calc_abund_range('SiII', 'FeII',obs)