def get_lum(sim, kappa, tag, BC_fac, IMF='Chabrier_300', bins=np.arange(-24, -16, 0.5), inp='FLARES', LF=True, filters=('FAKE.TH.FUV',), Type='Total', log10t_BC=7., extinction='default', orientation="sim", masslim=None, nlim=None): Lums = lum(sim, kappa, tag, BC_fac=BC_fac, IMF=IMF, inp=inp, LF=LF, filters=filters, Type=Type, log10t_BC=log10t_BC, extinction=extinction, orientation=orientation, masslim=masslim, nlim=nlim) # try: # Lums = lum(sim, kappa, tag, BC_fac=BC_fac, IMF=IMF, inp=inp, LF=LF, # filters=filters, Type=Type, log10t_BC=log10t_BC, # extinction=extinction, orientation=orientation, # masslim=masslim) # # except Exception as e: # Lums = {f: np.array([], dtype=np.float64) for f in filters} # Lums["coords"] = np.array([], dtype=np.float64) # Lums["smls"] = np.array([], dtype=np.float64) # Lums["masses"] = np.array([], dtype=np.float64) # Lums["nstar"] = np.array([], dtype=np.float64) # Lums["begin"] = np.array([], dtype=np.float64) # Lums["end"] = np.array([], dtype=np.float64) # print(e) if LF: tmp, edges = np.histogram(lum_to_M(Lums), bins=bins) return tmp else: return Lums
def lf_for_z(z, log10L_bin_centres, models, legend=False): # THIS WILL BE individual LF plot colors = [ 'b', 'r', 'orange', 'g', 'k', 'c', 'y', 'm', 'darkslateblue', 'gray', 'tomato', 'lawngreen', 'teal', 'wheat' ] fig = plt.figure() if type(models) == str: models = [models] for i, model in enumerate(models): m = getattr(lf_parameters, model)() if hasattr(m, 'beta'): s = abs(np.array(m.redshifts) - z) < 0.1 x = 0.4 * (lum_to_M(10**log10L_bin_centres) - np.array(m.M_star)[s][0]) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] beta = np.array(m.beta)[s][0] phi = phistar / (10**(x * (alpha + 1)) + 10**(x * (beta + 1))) plt.scatter(log10L_bin_centres, np.log10(phi), c=colors[i], label=model) else: s = abs(np.array(m.redshifts) - z) < 0.1 x = 10**(log10L_bin_centres - np.log10(M_to_lum(np.array(m.M_star)[s][0]))) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] plt.scatter(log10L_bin_centres, np.log10(phistar * (x)**(alpha + 1) * np.exp(-x)), c=colors[i], label=model) plt.ylabel(r'$\rm log_{10}(\phi \; / \; cMpc^{-3} \; dex^{-1})$') plt.xlabel(r"$\rm log_{10}(L_{UV} \; / \; erg\, s^{-1}\, Hz^{-1})$") if legend: plt.legend(loc='best') return fig
def get_lum(sim, kappa, tag, BC_fac, IMF='Chabrier_300', bins=np.arange(-24, -16, 0.5), inp='FLARES', LF=True, filters=['FAKE.TH.FUV'], Type='Total', log10t_BC=7., extinction='default', data_folder='data', aperture='30'): try: Lums = lum(sim, kappa, tag, BC_fac=BC_fac, IMF=IMF, inp=inp, LF=LF, filters=filters, Type=Type, log10t_BC=log10t_BC, extinction=extinction, data_folder=data_folder, aperture=aperture) except Exception as e: Lums = np.ones(len(filters)) * np.nan print(e) if LF: tmp, edges = np.histogram(lum_to_M(Lums), bins=bins) return tmp else: return Lums
def m_to_M(m, cosmo, z): flux = photconv.m_to_flux(m) lum = photconv.flux_to_L(flux, cosmo, z) M = photconv.lum_to_M(lum) return M
def lf_multi(zs, log10L_bin_centres, models, binned_lf=False, legend=False): # THIS WILL BE LF PLOT FOR A SELECTION OF MODELS markers = ['D', 's', 'h', '^', 'o', '*', 'v'] cmap = mpl.cm.tab20 cmap_marker = mpl.cm.Dark2_r if len(zs) % 2 == 1: N = int(len(zs) / 2) + 1 else: N = int(len(zs) / 2) fig, axes = plt.subplots(N, 2, figsize=(6, N * 2), sharex=True, sharey=True) plt.subplots_adjust(left=0.10, bottom=0.10, top=0.95, right=0.85, wspace=0., hspace=-0.) if type(models) == str: models = [models] Nxx = len(models) for z, ax in zip(zs, axes.flatten()): if legend: if Nxx > 7: if ax == axes[0, 0]: try: g = [] for i, model in enumerate(models[:7]): label = getattr(lf_parameters, model)().label g.append( Line2D([-99., -98.], [-99., -98.], color=cmap(i / 19), label=label, alpha=0.6)) ax.legend(handles=g, loc='lower left', fontsize=6) except: continue if ax == axes[0, 1]: try: g = [] for i, model in enumerate(models[7:]): i = i + 7 label = getattr(lf_parameters, model)().label g.append( Line2D([-99., -98.], [-99., -98.], color=cmap(i / 19), label=label, alpha=0.6)) ax.legend(handles=g, loc='lower left', fontsize=6) except: continue else: if ax == axes[0, 0]: try: g = [] for i, model in enumerate(models): label = getattr(lf_parameters, model)().label g.append( Line2D([-99., -98.], [-99., -98.], color=cmap(i / 19), label=label, alpha=0.6)) ax.legend(handles=g, loc='lower left', fontsize=6) except: continue if ax == axes[1, 0]: if binned_lf: if type(binned_lf) == list: g = [] for l, lf in enumerate(binned_lf): g.append( plt.errorbar(-99., -99., yerr=1., color=cmap_marker(l / 7), linestyle='', marker=markers[l], mec='k', alpha=0.6, label=lf['label'], markersize=3, elinewidth=1, capsize=1, capthick=1)) ax.legend(handles=g, loc='lower left', fontsize=6) else: g = [] for l in range(1): g.append( plt.errorbar(-99., -99., yerr=1., fmt='kD', alpha=0.6, label=binned_lf['label'], markersize=3, elinewidth=1, capsize=1, capthick=1)) ax.legend(handles=g, loc='lower left', fontsize=6) # except: # continue for i, model in enumerate(models): m = getattr(lf_parameters, model)() if hasattr(m, 'beta'): s = abs(np.array(m.redshifts) - z) < 0.5 try: x = 0.4 * (lum_to_M(10**log10L_bin_centres) - np.array(m.M_star)[s][0]) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] beta = np.array(m.beta)[s][0] phi = phistar / (10**(x * (alpha + 1)) + 10**(x * (beta + 1))) ax.plot(log10L_bin_centres, np.log10(phi), c=cmap(i / 19), alpha=0.6) except: continue else: s = abs(np.array(m.redshifts) - z) < 0.5 try: x = 10**(log10L_bin_centres - np.log10(M_to_lum(np.array(m.M_star)[s][0]))) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] ax.plot(log10L_bin_centres, np.log10(phistar * (x)**(alpha + 1) * np.exp(-x)), c=cmap(i / 19), alpha=0.6) except: continue if binned_lf: if type(binned_lf) == list: for l, lf in enumerate(binned_lf): try: q = abs( np.array([float(k) for k in [*lf['LF'].keys()]]) - z) < 0.5 z_key = str( int( np.array([ float(k) for k in [*lf['LF'].keys()] ])[q])) log10L_bin = np.log10( M_to_lum(np.array(lf['LF'][str(int(z_key))]['M']))) phi_bin = np.array(lf['LF'][str(int(z_key))]['phi']) err = np.array(lf['LF'][str(int(z_key))]['phi_err']) uplims = lf['LF'][str(int(z_key))]['uplim'] if lf['both_err']: if lf['log_err']: phi_err = err else: phi_err = [ logerr_lo(phi_bin, err[0], uplims), logerr_hi(phi_bin, err[1]) ] else: if lf['log_err']: phi_err = err else: phi_err = [ logerr_lo(phi_bin, err, uplims), logerr_hi(phi_bin, err) ] ax.errorbar(log10L_bin, np.log10(phi_bin), yerr=phi_err, color=cmap_marker(l / 7), linestyle='', marker=markers[l], mec='k', alpha=0.8, zorder=50 + l, markersize=3, elinewidth=1, capsize=1, capthick=1, uplims=uplims) except: continue else: try: q = abs( np.array([float(k) for k in [*binned_lf['LF'].keys()]]) - z) < 0.5 z_key = str( int( np.array([ float(k) for k in [*binned_lf['LF'].keys()] ])[q])) log10L_bin = np.log10( M_to_lum( np.array(binned_lf['LF'][str(int(z_key))]['M']))) phi_bin = np.array(binned_lf['LF'][str(int(z_key))]['phi']) err = np.array(binned_lf['LF'][str(int(z_key))]['phi_err']) uplims = binned_lf['LF'][str(int(z_key))]['uplim'] if binned_lf['both_err']: if binned_lf['log_err']: phi_err = err else: phi_err = [ logerr_lo(phi_bin, err[0], uplims), logerr_hi(phi_bin, err[1]) ] print(phi_err) else: if binned_lf['log_err']: phi_err = err else: phi_err = [ logerr_lo(phi_bin, err, uplims), logerr_hi(phi_bin, err) ] ax.errorbar(log10L_bin, np.log10(phi_bin), yerr=phi_err, fmt='kD', alpha=0.6, zorder=50, markersize=3, elinewidth=1, capsize=1, capthick=1, uplims=uplims) except: continue ax.text(0.7, 0.9, r'$\rm z=[{0:.1f}, {1:.1f})$'.format(z - 0.5, z + 0.5), fontsize=8, transform=ax.transAxes) ylim = np.array([-8., -4.01]) ax.set_ylim(-8., -1.5) ax.set_xlim([min(log10L_bin_centres), max(log10L_bin_centres)]) fig.text(0.5, 0.05, r'$\rm\log_{10}[L_{FUV} \; / \; (erg \; s^{-1} \; Hz^{-1})]$', horizontalalignment='center', verticalalignment='center') fig.text(0.05, 0.5, r'$\rm\log_{10}[\phi \; / \; Mpc^{-3}]$', horizontalalignment='center', verticalalignment='center', rotation=90) return fig
def lf_params_zs(zs, log10L_bin_centres, models, legend=False): # THIS WILL BE LF PLOT FOR A SELECTION OF MODELS colours = [ 'b', 'r', 'orange', 'g', 'k', 'c', 'y', 'm', 'darkslateblue', 'gray', 'tomato', 'lawngreen', 'teal', 'wheat' ] if len(zs) % 2 == 1: N = int(len(zs) / 2) + 1 else: N = int(len(zs) / 2) fig, axes = plt.subplots(N, 2, figsize=(6, N * 2), sharex=True, sharey=True) plt.subplots_adjust(left=0.10, bottom=0.10, top=0.95, right=0.85, wspace=0., hspace=-0.) if type(models) == str: models = [models] Nxx = len(models) for z, ax in zip(zs, axes.flatten()): if legend: if Nxx > 7: if ax == axes[0, 0]: try: g = [] for i, model in enumerate(models[:7]): colors_plot = colours[:7] g.append( ax.scatter([-99.], [-99.], c=colors_plot[i], s=5, label=model)) ax.legend(handles=g, loc='lower left') except: continue if ax == axes[0, 1]: try: g = [] for i, model in enumerate(models[7:]): colors_plot = colours[7:] g.append( ax.scatter([-99.], [-99.], c=colors_plot[i], s=5, label=model)) ax.legend(handles=g, loc='lower left') except: continue else: if ax == axes[0, 0]: try: g = [] for i, model in enumerate(models): g.append( ax.scatter([-99.], [-99.], c=colours[i], s=5, label=model)) ax.legend(handles=g, loc='lower left') except: continue for i, model in enumerate(models): m = getattr(lf_parameters, model)() if hasattr(m, 'beta'): s = abs(np.array(m.redshifts) - z) < 0.5 try: x = 0.4 * (lum_to_M(10**log10L_bin_centres) - np.array(m.M_star)[s][0]) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] beta = np.array(m.beta)[s][0] phi = phistar / (10**(x * (alpha + 1)) + 10**(x * (beta + 1))) ax.scatter(log10L_bin_centres, np.log10(phi), s=5, c=colours[i]) except: continue else: s = abs(np.array(m.redshifts) - z) < 0.5 try: x = 10**(log10L_bin_centres - np.log10(M_to_lum(np.array(m.M_star)[s][0]))) phistar = 10**np.array(m.phi_star)[s][0] alpha = np.array(m.alpha)[s][0] ax.scatter(log10L_bin_centres, np.log10(phistar * (x)**(alpha + 1) * np.exp(-x)), s=5, c=colours[i]) except: continue ax.text(0.7, 0.9, r'$\rm z=[{0:.1f}, {1:.1f})$'.format(z - 0.5, z + 0.5), fontsize=8, transform=ax.transAxes) ylim = np.array([-8., -4.01]) ax.set_ylim(-8., -1.5) ax.set_xlim([min(log10L_bin_centres), max(log10L_bin_centres)]) fig.text(0.5, 0.05, r'$\rm\log_{10}[L_{FUV} \; / \; (erg \; s^{-1} \; Hz^{-1})]$', horizontalalignment='center', verticalalignment='center') fig.text(0.05, 0.5, r'$\rm\log_{10}[\phi \; / \; Mpc^{-3}]$', horizontalalignment='center', verticalalignment='center', rotation=90) return fig
def N(self, area=1., cosmo=False, redshift_limits=[8., 15.], log10L_limits=[27.5, 30.], dz=0.05, dlog10L=0.05, per_arcmin=False, return_volumes=False): # calculates the number of galaxies in each bin on a grid defined by redshift_limits, log10L_limits, dz, dlog10L # and area based on a luminosity function evolution model. area_sm = area # Area in square arcmin area_sd = area_sm / 3600. # Area in square degrees area_sr = (np.pi / 180.) ** 2 * area_sd # Area in steradian if not cosmo: cosmo = flare.core.default_cosmo() # Setting the bin edges as well as centres for later operations bin_edges = {'log10L': np.arange(log10L_limits[0], log10L_limits[-1] + dlog10L, dlog10L), 'z': np.arange(redshift_limits[0], redshift_limits[-1] + dz, dz)} bin_centres = {'log10L': bin_edges['log10L'][:-1] + dlog10L / 2., 'z': bin_edges['z'][:-1] + dz / 2.} # Using astropy.cosmology to calculate the volume in each redshift bin volumes = np.asarray([cp.quad(dVc, bin_edges['z'][i - 1], bin_edges['z'][i], args=cosmo)[0] for i in range(1, len(bin_edges['z']))]) params = self.parameters(bin_centres['z']) if 'beta' in params.keys(): alphas = params['alpha'] Lstars = M_to_lum(params['M*']) Mstars = params['M*'] phistars = 10 ** params['log10phi*'] betas = params['beta'] N = phistars[None, :] * np.vectorize(quadfunct2)(_integ_dblpow, 0.4 * (lum_to_M( 10 ** bin_edges['log10L'][1:, None]) - Mstars[None, :]), 0.4 * (lum_to_M( 10 ** bin_edges['log10L'][:-1, None]) - Mstars[None, :]), alphas[None, :], betas[None, :]) * volumes[None, :] * area_sr ''' Left in for possible future implementation #N = phistars[None, :] * np.vectorize(quadfunct2)(_integ_dblpow2, # 10**(bin_edges['log10L'][:-1, None] - Lstars[None, :]), # 10**(bin_edges['log10L'][1:, None] - Lstars[None, :]), # alphas[None, :], betas[None, :] ) * volumes[None, :] * area_sr ''' else: alphas = params['alpha'] Lstars = M_to_lum(params['M*']) phistars = 10 ** params['log10phi*'] Mstars = params['M*'] N = phistars[None, :] * np.vectorize(quadfunct)(_integ, 10 ** bin_edges['log10L'][:-1, None] / Lstars[None, :], 10 ** bin_edges['log10L'][1:, None] / Lstars[None, :], args=(alphas[None, :])) * volumes[None, :] * area_sr ''' Left in for possible future implementation N = phistars[None, :] * np.vectorize(quadfunct)(_integ2, lum_to_M(10**bin_edges['log10L'][1:, None]) - Mstars[None, :], lum_to_M(10**bin_edges['log10L'][:-1, None]) - Mstars[None, :], args=(alphas[None, :])) * volumes[None, :] * area_sr ''' if per_arcmin: N /= area_sm if return_volumes: return bin_edges, bin_centres, volumes, N else: return bin_edges, bin_centres, N