def plot_one(rates, plotname, *p, frac=0.05, age=13.6): brates = u.gimme_rebinned_data(rates, splits=arange(0, 1.167, 0.167).tolist()) scale_k = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale_k * 0.7**2. * 1e4 ## factors of h... dt = 0.05 tt = arange(0.1, age, dt) ## forward time lbt = age - tt zz = [ct.cosmoz(x) for x in lbt] sfh = rz.sfr_behroozi_12(tt) dtd = step_dtd(tt, *p) norm_dtd = sum(step_dtd(tt, *p)) * dt dtd = dtd / norm_dtd tmp = convolve( sfh, dtd, 'full') * dt * frac * scale ## now convolved result in forward time rate_fn = tmp[:len(dtd)] clf() ax = subplot(111) ax2 = ax.twinx() ax2.plot(zz, sfh, 'r-') ax.plot(zz, rate_fn, 'k-') ax.errorbar(rates[:, 0], rates[:, 1], yerr=[rates[:, 3], rates[:, 2]], fmt='o', color='0.6') ax.errorbar(brates[:, 0], brates[:, 1], yerr=[brates[:, 3], brates[:, 2]], xerr=[brates[:, 5], brates[:, 4]], fmt='o', color='0.0', zorder=10) pwrl = (-1.0, 1.0) ax.set_xlim(0, 2.5) ax.set_xlabel('Redshift') ax.set_ylabel(r'SN Ia Rate') ax.set_ylabel(r'$10^{-4}$ SNe Ia per year per Mpc$^3$') ax2.set_ylabel(r'SF Rate') ax3 = axes([0.65, 0.6, 0.23, 0.2]) ax3.plot(tt, dtd, 'b-', label='Fit') #label='Norm = %1.1f' %(simps(dtd,x=time))) ax3.plot(tt, rz.powerdtd(tt, *pwrl), 'b:', label=r'$t^{%.1f}$' % (pwrl[0])) pn = (-20, 8.8, 11) ax3.plot(tt, rz.dtdfunc(tt, *pn), 'b--', label='Best model') ax.set_title(r' $k=%.4f$ M$_{\odot}^{-1}$, $f=%2.1f\%%$' % (scale_k, frac * 100)) ax3.set_ylabel('$\Phi$') ax3.set_xlabel('Delay Time (Gyr)') ax3.set_xlim(0, 12) ax3.set_ylim(0, 1.3) ax3.legend(frameon=False) savefig(plotname) return ()
def run_model(tt, *p): ddt = average(diff(tt)) sfh = rz.csfh_time(tt, *csfh_model) dtd = rz.dtdfunc(tt, *p[1:]) tmp = convolve(sfh, dtd, 'full') * ddt * np.exp( p[0]) #*frac*scale ## now convolved result in forward time rate_fn = tmp[:len(dtd)] return (rate_fn)
def run_model(tt, *p): ddt = average(diff(tt)) sfh = rz.csfh_time(tt, *csfh_model) dtd = rz.dtdfunc(tt, *p[1:]) tmp = convolve(sfh, dtd, 'full')*ddt*p[0]#*frac*scale ## now convolved result in forward time rate_fn=tmp[:len(dtd)] ## print(p,sum(rate_fn)) ## if sum(rate_fn)==0.: ## pdb.set_trace() return(rate_fn)
def dtdfit(time, *p): ff, aa, bb, cc = p scale = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale * 0.7**2. * 1e4 par_model = [0.0134, 2.55, 3.3, 6.1] sfh = rz.csfh_time(time, *par_model) dt = sum(diff(time)) / (len(time) - 1) p1 = (aa, bb, cc) res = rz.dtdfunc(time, *p1, norm=True) tmp = convolve(sfh, res, 'full') return (ff * tmp[:len(time)] * dt * scale)
def dtdfit(time, *p): ff, m, w, k = p scale = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale * 0.7**2. * 1e4 par_model = [0.0134, 2.55, 3.3, 6.1] ## Compendium ## par_model = [0.013, 2.6, 3.2, 6.1] ## MD14 ## par_model = [0.009, 2.7, 2.5, 4.1] ## Driver18 sfh = rz.csfh_time(time, *par_model) dt = sum(diff(time)) / (len(time) - 1) p1 = (m, w, k) res = rz.dtdfunc(time, *p1, norm=True) tmp = convolve(sfh, res, 'full') return (np.exp(ff) * tmp[:len(time)] * dt * scale)
def rate_per_galaxy( sfh_data, lbu=13.65, lbl=0.05, p0=None, frac_ia=0.05, ): scale = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale * 0.70**2. if not tuple(p0): p0 = (-1.4, 3.5, -1.0) ii = where((sfh_data[:, 0] > lbl) & (sfh_data[:, 0] <= lbu)) # cuts the data range sfh_data = sfh_data[ii] sfh_data[:, 0] = lbu - sfh_data[:, 0][::-1] sfh_data[:, 1] = sfh_data[:, 1][::-1] ## now in forward time warnings.simplefilter('ignore', RuntimeWarning) dtd = rz.dtdfunc(sfh_data[:, 0], *p0) if sum(dtd) == 0.0: return (-np.inf) dt = sum(diff(sfh_data[:, 0])) / (len(sfh_data[:, 0]) - 1) rate_fn = zeros((len(sfh_data), 2), ) tmp = convolve( sfh_data[:, 1], dtd, 'full') * dt * scale * frac_ia ## now convolved result in forward time rate_fn[:, 1] = tmp[:len(dtd)] rate_fn[:, 0] = sfh_data[:, 0] rate_fn[:, 1] = rate_fn[:, 1] rate = rate_fn[-1, 1] ## only need the current value return (rate_fn[-1, 1])
#!/usr/bin/env python import os,sys,pdb,scipy,glob from pylab import * from strolger_util import util as u from strolger_util import rates_z as rz if __name__=='__main__': p0 = (3.5, 2.5, 2.5) tt = arange(-10.0, 13.6, 0.1) ax1 = subplot(131) for mm in arange(-2.5, 12.5, 3): p = (mm,p0[1],p0[2]) ax1.plot(tt, rz.dtdfunc(tt,*p, norm=False)) ax1.set_xlim(0,10) ax2 = subplot(132) for ww in [1.0, 2.0, 2.5, 5.0, 7.5]: p = (p0[0],ww,p0[2]) ax2.plot(tt, rz.dtdfunc(tt,*p, norm=False)) ax2.set_xlim(0,10) ax3 = subplot(133) for kk in arange(-5.0, 7.5, 2.5): p = (p0[0],p0[1],kk) ax3.plot(tt, rz.dtdfunc(tt,*p, norm=False)) ax3.set_xlim(0,10) u.adjust_spines(ax1,['left','bottom']) u.adjust_spines(ax2,['bottom'])
def plot_one(rates, plotname, *p, frac=0.05, age=13.6): brates = u.gimme_rebinned_data(rates, splits=arange(0, 1.125, 0.125).tolist()) scale_k = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale_k * 0.7**2. * 1e4 ## factors of h... dt = 0.05 tt = arange(dt, age, dt) lbt = age - tt zz = [ct.cosmoz(x, ho=70) for x in lbt] par_model = [0.0134, 2.55, 3.3, 6.1] sfh = rz.csfh_time(tt, *par_model) dtd = rz.dtdfunc(tt, *p) pwrl = (-1.0, 1.0) dud = rz.powerdtd(tt, *pwrl) tmp = convolve( sfh, dtd, 'full') * dt * frac * scale ## now convolved result in forward time rate_fn = tmp[:len(dtd)] jdud = convolve(sfh, dud, 'full') * dt * scale * 0.065 jdud = jdud[:len(dtd)] clf() ax = subplot(111) ax2 = ax.twinx() ax2.plot(zz, sfh, 'r-', label='CSFH') ax.plot(zz, rate_fn, 'k-', label='Fit', zorder=3) ax.plot(zz, jdud, 'k:', label=r'$t^{%.1f}$' % (pwrl[0])) ax.errorbar(rates[:, 0], rates[:, 1], yerr=[rates[:, 3], rates[:, 2]], fmt='o', color='0.6', alpha=0.4) ax.errorbar(brates[:, 0], brates[:, 1], yerr=[brates[:, 3], brates[:, 2]], xerr=[brates[:, 5], brates[:, 4]], fmt='o', color='0.0', zorder=10) pwrl = (-1.0, 1.0) ax.set_xlim(0, 2.6) ax.set_ylim(0, 1.8) ax2.set_ylim(0, 0.16) ax3 = axes([0.65, 0.62, 0.23, 0.2]) ## ax3 = axes([0.55, 0.6, 0.33, 0.25]) ax3.plot(tt, log10(dtd), 'b-', label='Fit') #label='Norm = %1.1f' %(simps(dtd,x=time))) ax3.plot(tt, log10(rz.powerdtd(tt, *pwrl)), 'b:', label=r'$t^{%.1f}$' % (pwrl[0])) ax3.set_ylabel('$\log(\Phi)$') ax3.set_xlabel('Delay Time (Gyr)') ax3.set_xlim(0, 12) ax3.set_ylim(-3, 0.5) ##ax3.set_ylim(0,1) ## ax3.legend(loc=1,ncol=1,frameon=False, fontsize=8) ax.set_xlabel('Redshift') ax.set_ylabel(r'$10^{-4}$ SNe Ia per year per Mpc$^3$') ax2.set_ylabel(r'M$_{\odot}$ per year per Mpc$^3$') ## ax.set_title(r' $k=%.4f$ M$_{\odot}^{-1}$, $f=%2.1f\%%$' %(scale_k,frac*100)) ax.set_title(r'$k=%.4f\,M_{\odot}^{-1},\,\,\varepsilon=%2.1f\%%$' % (scale_k, frac * 100)) ax.legend(loc=2, frameon=False) ax2.legend(loc=4, frameon=False) savefig(plotname) return ()
def func(x, *p): aa = p return (aa * rz.dtdfunc(x, *p0) * scale)
#!/usr/bin/env python import os, sys, pdb, scipy, glob from pylab import * from strolger_util import util as u from strolger_util import rates_z as rz from scipy.optimize import curve_fit from scipy.stats import chisquare import pickle scale = (0.0210) * 0.062 * (0.7)**2 ax = subplot(111) p0 = [-1257.93, 59.32, 248.88] ## from MCMC time = arange(0.05, 13.5, 0.1) dtd = rz.dtdfunc(time, *p0) def func(x, *p): aa = p return (aa * rz.dtdfunc(x, *p0) * scale) p_t = [1.0] color = '#4BADFF' ## pwrl = (-1.0,1.0) ## ax.plot(time,rz.powerdtd(time, *pwrl, normed=False)*(1.3e-3), 'b--', label=r'$t^{%.1f}$'%(pwrl[0])) pwrl = (-1.10, 1.0) perr = (0.09, 0.0) cov = diag(array(perr)**2)
''' if __name__ == '__main__': xx = arange(0, 10, 0.1) ax1 = subplot(131) ax2 = subplot(132) ax3 = subplot(133) p0 = [-2.1, 0.4, -12.8] p = p0 cnt = 0 for i in arange(-12, 9, 3.0): p[0] = i ax1.plot(xx, cnt + rz.dtdfunc(xx, *p, norm=False), 'k-', alpha=0.3) cnt += 0.1 p = p0 cnt = 0 for i in arange(0.1, 12., 0.5): p[1] = i ax2.plot(xx, cnt + rz.dtdfunc(xx, *p), 'k-', alpha=0.3) cnt += 0.1 p = p0 cnt = 0 for i in arange(-12, 9, 3.0): p[2] = i ax3.plot(xx, cnt + rz.dtdfunc(xx, *p), 'k-', alpha=0.3) cnt += 0.1
rate_fn = tmp[:len(dtd)] return (rate_fn) if __name__ == '__main__': dt = 0.05 age = 13.6 tt = arange(dt, age, dt) ## p_val = [-2.810, -1518.39108091, 51.06020462, 49.98728772] ## from optimized fit p_val = [-2.814, -1257.93, 59.32, 248.88] ## from MCMC ax = subplot(111) ax2 = axes([0.55, 0.6, 0.33, 0.25]) ## ax2 = axes([0.65, 0.62, 0.23, 0.2]) ax2.plot(tt, log10(rz.dtdfunc(tt, *p_val[1:])), 'b-') files = glob.glob('mc_sfd_*.pkl') tot = 0 for file in files: samples = pickle.load(open(file, 'rb')) samples = samples[:, 50:, :].reshape((-1, 5)) print('adding %d samples from %s... ' % (len(samples), file)) try: temp = concatenate((temp, samples), axis=0) except: temp = samples #tta = linspace(min(tt), max(tt), 100) tta = tt print('%d total samples' % len(temp))
def plot_one(rates, plotname, *p, frac=0.05, age=13.6): brates = u.gimme_rebinned_data(rates, splits=arange(0, 1.125, 0.125).tolist()) scale_k = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale_k * 0.7**2. * 1e4 ## factors of h... dt = 0.05 tt = arange(dt, age, dt) ## forward time lbt = age - tt zz = [ct.cosmoz(x) for x in lbt] par_model = [0.0134, 2.55, 3.3, 6.1] sfh = rz.csfh_time(tt, *par_model) dtd = rz.dtdfunc(tt, *p) pwrl = (-1.0, 1.0) dud = rz.powerdtd(tt, *pwrl) tmp = convolve( sfh, dtd, 'full') * dt * frac * scale ## now convolved result in forward time rate_fn = tmp[:len(dtd)] jdud = convolve(sfh, dud, 'full') * dt * scale * 0.065 jdud = jdud[:len(dtd)] clf() ax = subplot(111) ax2 = ax.twinx() ax2.plot(zz, sfh, 'r-') ax.plot(zz, rate_fn, 'k-') ax.plot(zz, jdud, 'k:') ax.errorbar(rates[:, 0], rates[:, 1], yerr=[rates[:, 3], rates[:, 2]], fmt='o', color='0.6') ax.errorbar(brates[:, 0], brates[:, 1], yerr=[brates[:, 3], brates[:, 2]], xerr=[brates[:, 5], brates[:, 4]], fmt='o', color='0.0', zorder=10) ax.set_xlim(0, 2.5) ax.set_ylim(0, 1.8) ax.set_xlabel('Redshift') ax.set_ylabel(r'SN Ia Rate') ax.set_ylabel(r'$10^{-4}$ SNe Ia per year per Mpc$^3$') ax2.set_ylabel(r'SF Rate') ax3 = axes([0.65, 0.6, 0.23, 0.2]) ax3.plot(tt, dtd, 'b-', label='Fit') #label='Norm = %1.1f' %(simps(dtd,x=time))) ax3.plot(tt, dud, 'b:', label=r'$t^{%.1f}$' % (pwrl[0])) ax3.set_ylabel('$\Phi$') ax3.set_xlabel('Delay Time (Gyr)') ax3.set_xlim(0, 12) ax3.set_ylim(0, 1.3) ax3.legend(frameon=False) savefig(plotname) return ()
dt = 0.05 age=13.6 tt = arange(dt,age,dt) ## p_val = [0.060, -1156., 58., 220.] p_val = [0.060, -1500., 58., 220.] p_err = [[0.003, -0.004], [839., -596.], [14., -16.], [191., -186.] ] ## fout, (ax, ax2) = plt.subplots(1,2, gridspec_kw = {'width_ratios':[3, 1]}) ax = subplot(111) ax2 = axes([0.55, 0.6, 0.33, 0.25]) ax2.plot(tt, rz.dtdfunc(tt, *p_val[1:]), 'b-') p_err=sqrt(array(p_err)[:,1]**2.) ## p_err=sqrt(p_err[:,0]**2.+p_err[:,1]**2.) tta = linspace(min(tt), max(tt), 300) cov = diag(array(p_err)**2.) ## ps = np.random.multivariate_normal(p_val, cov, 10000) outfile = 'bfres0.pkl' if os.path.isfile(outfile): print ('%s exists' %outfile) ps=pickle.load(open(outfile,'rb')) else: ps = np.random.multivariate_normal(p_val, cov, 100)
def rate_per_galaxy( sfh, lbu=13.65, lbl=0.05, p0=None, frac_ia=0.05, plotit=True, title=None, testing=False, ): scale = quad(imf.salpeter, 3, 8)[0] / quad(imf.salpeter1, 0.1, 125)[0] scale = scale * 0.70**2. if not p0: p0 = (-1.4, 3.5, -1.0) sfh_data = loadtxt(sfh) ii = where((sfh_data[:, 0] > lbl) & (sfh_data[:, 0] <= lbu)) # cuts the data range sfh_data = sfh_data[ii] sfh_data[:, 0] = lbu - sfh_data[:, 0][::-1] sfh_data[:, 1] = sfh_data[:, 1][::-1] ## now in forward time if testing: ## reframes everything in a more continuous time. n_out = IUS(sfh_data[:, 0], sfh_data[:, 1]) time = arange(0, lbu + 0.1, 0.1) sfh_data = zeros((len(time), 2), ) sfh_data[:, 0] = time sfh_data[:, 1] = n_out( time) ## now continuous, evenly spaced, not sure that's important. if testing: ## just makes a SFH box box_ll = 4. ii = where((sfh_data[:, 0] > box_ll) & (sfh_data[:, 0] < box_ll + 2.)) sfh_data[:, 1] = 0.0 sfh_data[:, 1][ii] = 10. #warnings.simplefilter('ignore',RuntimeWarning) dtd = rz.dtdfunc(sfh_data[:, 0], *p0) if testing: ## makes the dtd function gaussian p1 = (1., 4.0, 0.01) dtd = u.gauss(sfh_data[:, 0], *p1) dt = sum(diff(sfh_data[:, 0])) / (len(sfh_data[:, 0]) - 1) rate_fn = zeros((len(sfh_data), 2), ) tmp = convolve( sfh_data[:, 1], dtd, 'full') * dt * scale * frac_ia ## now convolved result in forward time rate_fn[:, 1] = tmp[:len( dtd)] #*concatenate((array([0.]),diff(sfh_data[:,0])),) rate_fn[:, 0] = sfh_data[:, 0] rate_fn[:, 1] = rate_fn[:, 1] rate = rate_fn[-1, 1] if testing: ## a check of the shift in the effective time on each tmp1 = sum((sfh_data[:, 0]) * sfh_data[:, 1]) / sum(sfh_data[:, 1]) tmp2 = sum(rate_fn[:, 1] * (rate_fn[:, 0])) / sum(rate_fn[:, 1]) print(tmp1, tmp2, tmp2 - tmp1) if plotit: clf() ax1 = subplot(111) ax1.plot(lbu - sfh_data[:, 0], sfh_data[:, 1], '--', color='0.25') #,alpha=0.3) ax1.set_xlabel('Lookback Time (Gyr)') ax1.set_ylabel('$\psi(M_{\odot}\,yr^{-1})$') if title: ax1.set_title(title) ## ax1.axvline(x=0, color='r') ax3 = ax1.twinx() ax3.plot(lbu - rate_fn[:, 0], rate_fn[:, 1] * 1.e3, 'k-', label='$R_{Ia}(0)=%2.2f\, (1000\, yr)^{-1}$' % (rate * 1.e3), alpha=0.3) ax3.set_ylabel('$R_{Ia}[\#\,(1000\, yr)^{-1}]$') ax3.legend(frameon=False) ttt, ddd = rz.greggio() ttt = array(ttt) ddd = array(ddd) ii = where(ttt > 0) ttt = ttt[ii] ddd = ddd[ii] time = arange(0.1, 15, 0.1) dtd = rz.dtdfunc(time, *p0) pwrl = (-1.0, 1.0) ax2 = axes([0.6, 0.55, 0.25, 0.25]) ax2.plot(time, dtd, 'r-', label='Best Fit') #label='Norm = %1.1f' %(simps(dtd,x=time))) ax2.plot(time, rz.powerdtd(time, *pwrl), 'b--', label=r'$t^{%.1f}$' % (pwrl[0])) ## ax2.plot(ttt,ddd,'b--', label='Greggio') ax2.set_ylabel('$\Phi$') ax2.set_xlabel('Delay Time (Gyr)') ax2.set_xlim(0, 12) ax2.set_ylim(0, 1.3) ax2.legend(frameon=False) #tight_layout() ax4 = axes([0.6, 0.2, 0.25, 0.25]) img = mpimg.imread('107.jpg') ## Thoth ax4.imshow(img[75:135, 50:140]) #img = mpimg.imread('206.jpg') ## Borg #ax4.imshow(img[75:135,70:160]) ax4.set_yticks([]) ax4.set_xticks([]) if title: savefig(title + '.png') else: savefig('figure_sfh_demo.png') return (rate)