def fit_dlogM_mw(tab, sfrsd_tab, mltype='ring', mlb='i'): merge_tab = t.join(tab, sfrsd_tab, 'plateifu') is_agn = m.mask_from_maskbits(merge_tab['mngtarg3'], [1, 2, 3, 4]) mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab[f'logsollum_in_ifu_{mlb}'] merge_tab['dlogmass_lw'] = logmass_in_ifu - logmass_in_ifu_lw std_atten_mwtd = merge_tab['std_atten_mwtd'] mean_atten_mwtd = merge_tab['mean_atten_mwtd'] ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 outer_mass = (merge_tab[f'outerml_{mltype}'] + \ merge_tab[f'logsollum_outer_{mlb}']).to(u.Msun) mass_pca = merge_tab['mass_in_ifu'].to(u.Msun) + outer_mass ssfrsd = sfrsd / mass_pca merge_tab['log_ssfrsd'] = ssfrsd.to(u.dex(ssfrsd.unit)) merge_tab['log_ssfrsd'][~np.isfinite(merge_tab['log_ssfrsd'])] = np.nan * merge_tab['log_ssfrsd'].unit ols = OLS( endog=np.array(merge_tab['dlogmass_lw'][~is_agn]), exog=sm_add_constant( t.Table(merge_tab['mean_atten_mwtd', 'std_atten_mwtd', 'log_ssfrsd'])[~is_agn].to_pandas(), prepend=False), hasconst=True, missing='drop') olsfit = ols.fit() return olsfit
def fit_dlogM_mw(tab, sfrsd_tab, mltype='ring', mlb='i'): merge_tab = t.join(tab, sfrsd_tab, 'plateifu') is_agn = m.mask_from_maskbits(merge_tab['mngtarg3'], [1, 2, 3, 4]) mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab['ifu_absmag'][:, mlb_ix].to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits(absmag_sun_mlb)) merge_tab['dlogmass_lw'] = logmass_in_ifu - logmass_in_ifu_lw ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 mass_pca = merge_tab['mass_in_ifu'] + merge_tab['outer_mass_{}'.format(mltype)] ssfrsd = sfrsd / mass_pca merge_tab['log_ssfrsd'] = ssfrsd.to(u.dex(ssfrsd.unit)) merge_tab['log_ssfrsd'][~np.isfinite(merge_tab['log_ssfrsd'])] = np.nan * merge_tab['log_ssfrsd'].unit ols = OLS( endog=np.array(merge_tab['dlogmass_lw'][~is_agn]), exog=sm_add_constant( t.Table(merge_tab['mean_atten_mwtd', 'std_atten_mwtd', 'log_ssfrsd'])[~is_agn].to_pandas(), prepend=False), hasconst=True, missing='drop') olsfit = ols.fit() return olsfit
def compare_missing_mass(tab, mlb='i', cb1='g', cb2='r'): '''make figure showing effect of aperture correction on total mass Figure shows P+ (red) and S (blue) samples, with their integrated color, difference between aperture-corrected masses using CMLR and ring method Parameters ---------- tab : astropy.table.Table full aggregation results table mlb : {str}, optional bandpass for mass-to-light ratio (the default is 'i', which [default_description]) cb1 : {str}, optional bluer bandpass for color (the default is 'g', SDSS g band) cb2 : {str}, optional redder bandpass for color (the default is 'r', SDSS r band) ''' cb1_nsa_mag = tab[f'mag_nsa_{cb1}'] cb2_nsa_mag = tab[f'mag_nsa_{cb2}'] broadband_color = cb1_nsa_mag - cb2_nsa_mag outermass_cmlr = (tab[f'logsollum_outer_{mlb}'] + tab['outerml_cmlr']).to( u.Msun) mass_cmlr = (tab['mass_in_ifu'].to(u.Msun) + outermass_cmlr) outermass_ring = (tab[f'logsollum_outer_{mlb}'] + tab['outerml_ring']).to( u.Msun) mass_ring = (tab['mass_in_ifu'].to(u.Msun) + outermass_ring) dlogmass_cmlr_ring = mass_cmlr.to(u.dex(u.Msun)) - mass_ring.to(u.dex(u.Msun)) primarysample = m.mask_from_maskbits(tab['mngtarg1'], [10]) secondarysample = m.mask_from_maskbits(tab['mngtarg1'], [11]) fig, main_ax, hist_ax = make_panel_hist(top=0.9, left=.225) valid = np.isfinite(dlogmass_cmlr_ring) for selection, label, marker, color in zip( [primarysample, secondarysample], ['Primary', 'Secondary'], ['o', 'D'], ['r', 'b']): main_ax.scatter( x=broadband_color[selection * valid], y=dlogmass_cmlr_ring[selection * valid], c=color, edgecolor='None', s=3., marker=marker, label=label, alpha=0.5) hist_ax.hist(dlogmass_cmlr_ring[selection * valid], color=color, density=True, bins='auto', histtype='step', orientation='horizontal', linewidth=0.75) main_ax.set_xlim(np.percentile(broadband_color[valid], [5., 99.])) main_ax.set_ylim(np.percentile(dlogmass_cmlr_ring[valid], [5., 99.])) main_ax.legend(loc='best', prop={'size': 'xx-small'}) main_ax.tick_params(labelsize='xx-small') main_ax.set_xlabel(r'${}-{}$'.format(cb1, cb2), size='x-small') main_ax.set_ylabel(r'$\log \frac{M^{\rm tot}_{\rm CMLR}}{M^{\rm tot}_{\rm ring}}$', size='x-small') fig.suptitle(r'Impact of aperture-correction on $M^{\rm tot}$', size='small') fig.savefig(os.path.join(csp_basedir, 'lib_diags/', 'mtot_compare_cmlr_ring.png'))
def test_cds_function_units(reader_cls): data_and_readme = 'data/cdsFunctional.dat' reader = ascii.get_reader(reader_cls) table = reader.read(data_and_readme) assert table['logg'].unit == u.dex(u.cm / u.s**2) assert table['logTe'].unit == u.dex(u.K) assert table['Mass'].unit == u.Msun assert table['e_Mass'].unit == u.Msun assert table['Age'].unit == u.Myr assert table['e_Age'].unit == u.Myr
def test_cds_function_units(): from astropy.units import dex data_and_readme = 'data/cdsFunctional.dat' reader = ascii.get_reader(ascii.Cds) table = reader.read(data_and_readme) assert table['logg'].unit == u.dex(u.cm / u.s**2) assert table['logTe'].unit == u.dex(u.K) assert table['Mass'].unit == u.Msun assert table['e_Mass'].unit == u.Msun assert table['Age'].unit == u.Myr assert table['e_Age'].unit == u.Myr
def test_cds_function_units2(reader_cls): # This one includes some dimensionless dex. data_and_readme = 'data/cdsFunctional2.dat' reader = ascii.get_reader(reader_cls) table = reader.read(data_and_readme) assert table['Teff'].unit == u.K assert table['logg'].unit == u.dex(u.cm / u.s**2) assert table['vturb'].unit == u.km / u.s assert table['[Fe/H]'].unit == u.dex(u.one) assert table['e_[Fe/H]'].unit == u.dex(u.one) assert_almost_equal(table['[Fe/H]'].to(u.one), 10.**(np.array([-2.07, -1.50, -2.11, -1.64])))
def test_to_funcunit_quantity(self, Column): """ Tests for #8424, check if function-unit can be retrieved from column. """ d = Column([1, 2, 3], name='a', dtype="f8", unit="dex(AA)") assert np.all(d.quantity == ([1, 2, 3] * u.dex(u.AA))) assert np.all(d.quantity.value == ([1, 2, 3] * u.dex(u.AA)).value) assert np.all(d.quantity == d.to("dex(AA)")) assert np.all(d.quantity.value == d.to("dex(AA)").value) # make sure, casting to linear unit works q = [10, 100, 1000] * u.AA np.testing.assert_allclose(d.to(u.AA), q)
def make_stdtauV_vs_dMass_ssfrsd_fig(tab, sfrsd_tab, mltype='ring', mlb='i'): merge_tab = t.join(tab, sfrsd_tab, 'plateifu') mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] fig, ax, cax, hist_ax = make_panel_hcb_hist(figsize=(3, 3), dpi=300, top=.8) logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab['ifu_absmag'][:, mlb_ix].to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits(absmag_sun_mlb)) std_atten_mwtd = merge_tab['std_atten_mwtd'] mean_atten_mwtd = merge_tab['mean_atten_mwtd'] ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 mass_pca = merge_tab['mass_in_ifu'] + merge_tab['outer_mass_{}'.format(mltype)] ssfrsd = sfrsd / mass_pca sc = ax.scatter( x=std_atten_mwtd, y=(logmass_in_ifu - logmass_in_ifu_lw), c=ssfrsd.to(u.dex(ssfrsd.unit)), edgecolor='k', linewidths=.125, s=2., cmap='viridis_r', vmin=-15., vmax=-10.) cb = colorbartop(fig, sc, cax) cb.set_label(r'$\log \frac{{\Sigma}^{\rm SFR}_{R<R_e}}{M^*_{\rm tot}}$', size='xx-small') ax.tick_params(which='major', labelsize='xx-small') ax.tick_params(which='minor', labelbottom=False, labelleft=False) ax.set_xscale('log') ax.set_xlabel(r'$\sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='x-small') hist_ax.hist(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), bins='auto', histtype='step', orientation='horizontal', linewidth=.5, density=True, color='k') for yloc, lw, ls, c in zip( np.percentile(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), [16., 50., 84.]), [.5, 1., .5], ['--', '-', '--'], ['gray', 'k', 'gray']): hist_ax.axhline(yloc, linestyle=ls, linewidth=lw, color=c) fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.savefig( os.path.join(basedir, 'lib_diags/', 'stdtauV_dMglobloc_ssfrsd.png'), dpi=fig.dpi)
def update_mass_table(drpall, mass_table_old=None, limit=None, mlband='i'): ''' ''' # what galaxies are available to aggregate? res_fnames = glob(os.path.join(basedir, 'results/*-*/*-*_res.fits'))[:limit] # filter out whose that have not been done if mass_table_old is None: already_aggregated = [False for _ in range(len(res_fnames))] else: already_aggregated = [os.path.split(fn)[1].split('_')[0] in old_mass_table['plateifu'] for fn in res_fnames] res_fnames = [fn for done, fn in zip(already_aggregated, res_fnames)] # aggregate individual galaxies, and stack them mass_tables_new = list(ProgressBar.map( partial(mass_agg_onegal, mlband=mlband), res_fnames, multiprocess=False, step=5)) mass_table_new = t.vstack(mass_tables_new) # if there was an old mass table, stack it with the new one if mass_table_old is None: mass_table = mass_table_new else: mass_table = t.vstack([mass_table_old, mass_table_new], join_type='inner') cmlr = totalmass.cmlr_kwargs missing_flux = (mass_table['nsa_absmag'].to(m.Mgy) - \ mass_table['ifu_absmag'].to(m.Mgy)).clip( a_min=0.*m.Mgy, a_max=np.inf*m.Mgy) mag_missing_flux = missing_flux.to(u.ABmag) cb1, cb2 = cmlr['cb1'], cmlr['cb2'] color_missing_flux = mag_missing_flux[:, totalmass.StellarMass.bands_ixs[cb1]] - \ mag_missing_flux[:, totalmass.StellarMass.bands_ixs[cb2]] color_missing_flux[~np.isfinite(color_missing_flux)] = np.inf mass_table['outer_ml_cmlr'] = np.polyval(cmlr['cmlr_poly'], color_missing_flux.value) * \ u.dex(m.m_to_l_unit) mass_table['outer_lum'] = mag_missing_flux.to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits(totalmass.StellarMass.absmag_sun)) mass_table['outer_mass_ring'] = \ (mass_table['outer_lum'][:, totalmass.StellarMass.bands_ixs['i']] + \ mass_table['outer_ml_ring']).to(u.Msun) mass_table['outer_mass_cmlr'] = \ (mass_table['outer_lum'][:, totalmass.StellarMass.bands_ixs['i']] + \ mass_table['outer_ml_cmlr']).to(u.Msun) return mass_table
def sollum_bands(self): with catch_warnings(): simplefilter('ignore', category=RuntimeWarning) sollum = self.absmag_bands.to( u.dex(m.bandpass_sol_l_unit), bandpass_flux_to_solarunits(self.absmag_sun[..., None, None])) return sollum
def test_str(self): """Do some spot checks that str, repr, etc. work as expected.""" lu1 = u.mag(u.Jy) assert str(lu1) == 'mag(Jy)' assert repr(lu1) == 'Unit("mag(Jy)")' assert lu1.to_string('generic') == 'mag(Jy)' with pytest.raises(ValueError): lu1.to_string('fits') with pytest.raises(ValueError): lu1.to_string(format='cds') lu2 = u.dex() assert str(lu2) == 'dex' assert repr(lu2) == 'Unit("dex(1)")' assert lu2.to_string() == 'dex(1)' lu3 = u.MagUnit(u.Jy, function_unit=2 * u.mag) assert str(lu3) == '2 mag(Jy)' assert repr(lu3) == 'MagUnit("Jy", unit="2 mag")' assert lu3.to_string() == '2 mag(Jy)' lu4 = u.mag(u.ct) assert lu4.to_string('generic') == 'mag(ct)' assert lu4.to_string('latex') == ('$\\mathrm{mag}$$\\mathrm{\\left( ' '\\mathrm{ct} \\right)}$') assert lu4._repr_latex_() == lu4.to_string('latex')
def __init__(self, results, pca_system, drp, dap, drpall_row, cosmo, mlband='i'): self.results = results self.pca_system = pca_system self.drp = drp self.dap = dap self.drpall_row = drpall_row self.cosmo = cosmo self.mlband = mlband with catch_warnings(): simplefilter('ignore') self.results.setup_photometry(pca_system) self.s2p = results.spec2phot # stellar mass to light ratio self.ml0 = results.cubechannel('ML{}'.format(mlband), 0) self.badpdf = results.cubechannel('GOODFRAC', 2) < 1.0e-4 self.ml_mask = np.logical_or.reduce((self.results.mask, self.badpdf)) # infer values in interior spaxels affected by one of the following: # bad PDF, foreground star, dead fiber self.low_or_no_cov = m.mask_from_maskbits( self.drp['MASK'].data, [0, 1]).mean(axis=0) > .3 self.drp3dmask_interior = m.mask_from_maskbits( self.drp['MASK'].data, [2, 3]).mean(axis=0) > .3 self.interior_mask = np.logical_or.reduce((self.badpdf, self.drp3dmask_interior)) self.logml_final = infer_masked( q_trn=self.ml0, bad_trn=self.ml_mask, infer_here=self.interior_mask) * u.dex(m.m_to_l_unit)
def test_using_quantity_class(self): """Check that we can use Quantity if we have subok=True""" # following issue #5851 lu = u.dex(u.AA) with pytest.raises(u.UnitTypeError): u.Quantity(1., lu) q = u.Quantity(1., lu, subok=True) assert type(q) is lu._quantity_class
def test_inequality(): """Check __ne__ works (regression for #5342).""" lu1 = u.mag(u.Jy) lu2 = u.dex(u.Jy) lu3 = u.mag(u.Jy**2) lu4 = lu3 - lu1 assert lu1 != lu2 assert lu1 != lu3 assert lu1 == lu4
def test_inequality(): """Check __ne__ works (regresssion for #5342).""" lu1 = u.mag(u.Jy) lu2 = u.dex(u.Jy) lu3 = u.mag(u.Jy**2) lu4 = lu3 - lu1 assert lu1 != lu2 assert lu1 != lu3 assert lu1 == lu4
def cmlr_equivalency(slope, intecept): ''' ''' def color_to_logml(c): return slope * c + intercept def logml_to_color(logml): return (logml - intercept) / slope return [(u.mag, u.dex(m.m_to_l_unit), color_to_logml, logml_to_color)]
def cmlr_equivalency(slope, intercept): ''' ''' def color_to_logml(c): return slope * c + intercept def logml_to_color(logml): return (logml - intercept) / slope return [(u.mag, u.dex(m.m_to_l_unit), color_to_logml, logml_to_color)]
def compare_mtot_pca_nsa(tab, jhu_mpa, mltype='ring', mlb='i', cb1='g', cb2='r'): jointab = t.join(tab, jhu_mpa, 'plateifu') cb1_nsa_mag = jointab[f'mag_nsa_{cb1}'] cb2_nsa_mag = jointab[f'mag_nsa_{cb2}'] broadband_color = cb1_nsa_mag - cb2_nsa_mag outer_mass = (jointab[f'outerml_{mltype}'] + \ jointab[f'logsollum_outer_{mlb}']).to(u.dex(u.Msun)) mass_pca = jointab['mass_in_ifu'].to(u.Msun) + outer_mass.to(u.Msun) nsa_h = 1. mass_nsa = (jointab['nsa_elpetro_mass'] * u.Msun * (nsa_h * u.littleh)**-2).to( u.Msun, u.with_H0(cosmo.H0)) jhumpa_h = 1. / .7 chabrier_to_kroupa_dex = .05 mass_jhumpa = (10.**(jointab['LOG_MSTAR'] + chabrier_to_kroupa_dex) * \ u.Msun * (jhumpa_h * u.littleh)**-2.).to(u.Msun, u.with_H0(cosmo.H0)) lowess_grid = np.linspace(np.nanmin(broadband_color), np.nanmax(broadband_color), 100).value lowess_pca_nsa, swt_nsa = smooth( x=broadband_color.value, y=np.log10(mass_pca / mass_nsa).value, xgrid=lowess_grid, bw=.01) lowess_pca_jhumpa, swt_jhumpa = smooth( x=broadband_color.value, y=np.log10(mass_pca / mass_jhumpa).value, xgrid=lowess_grid, bw=.01) swt_th = .2 * swt_nsa.max() good_lowess_nsa = (swt_nsa >= swt_th) good_lowess_jhumpa = (swt_jhumpa >= swt_th) fig, ax = plt.subplots(1, 1, figsize=(3, 3), dpi=300) ax.scatter(broadband_color, np.log10(mass_pca / mass_nsa), s=2., edgecolor='None', c='C0', label='NSA') ax.plot(lowess_grid[good_lowess_nsa], lowess_pca_nsa[good_lowess_nsa], linewidth=0.5, c='k', linestyle='-') ax.scatter(broadband_color, np.log10(mass_pca / mass_jhumpa), s=2., edgecolor='None', c='C1', label='JHU-MPA') ax.plot(lowess_grid[good_lowess_jhumpa], lowess_pca_jhumpa[good_lowess_jhumpa], linewidth=0.5, c='k', linestyle='--') ax.set_ylim([-.2, .5]); ax.set_xlim([-.1, 1.]) ax.legend(loc='best', prop={'size': 'xx-small'}) ax.tick_params(labelsize='xx-small') ax.set_xlabel(r'${}-{}$'.format(cb1, cb2), size='x-small') ax.set_ylabel(r'$\log \frac{M^*_{\rm PCA}}{M^*_{\rm catalog}}$', size='x-small') fig.tight_layout() fig.subplots_adjust(top=.95, left=.21, right=.97) fig.savefig(os.path.join(csp_basedir, 'lib_diags/', 'dMasses.png'), dpi=fig.dpi)
def make_stdtauV_vs_ssfrsd_dMass_fig(tab, sfrsd_tab, mltype='ring', mlb='i'): merge_tab = t.join(tab, sfrsd_tab, 'plateifu') mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] fig, ax = plt.subplots(1, 1, figsize=(3, 3), dpi=300) logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab[f'logsollum_in_ifu_{mlb}'] std_atten_mwtd = merge_tab['std_atten_mwtd'] mean_atten_mwtd = merge_tab['mean_atten_mwtd'] ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 outer_mass = (merge_tab[f'outerml_{mltype}'] + \ merge_tab[f'logsollum_outer_{mlb}']).to(u.Msun) mass_pca = merge_tab['mass_in_ifu'].to(u.Msun) + outer_mass ssfrsd = sfrsd / mass_pca sc = ax.scatter( x=np.log10(std_atten_mwtd), c=(logmass_in_ifu - logmass_in_ifu_lw), y=ssfrsd.to(u.dex(ssfrsd.unit)), edgecolor='k', linewidths=.125, s=1., cmap='viridis_r', alpha=0.5, vmin=.01, vmax=.12) cb = fig.colorbar(sc, ax=ax, extend='both') cb.set_label(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='xx-small') cb.ax.tick_params(labelsize='xx-small') ax.tick_params(which='major', labelsize='xx-small') ax.tick_params(which='minor', labelbottom=False, labelleft=False) ax.set_xlabel(r'$\log \sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log \frac{{\Sigma}^{\rm SFR}_{R<R_e}}{M^*_{\rm tot}}$', size='x-small') fig.tight_layout() fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.subplots_adjust(left=.225, bottom=.125, right=.9, top=.925) fig.savefig( os.path.join(csp_basedir, 'lib_diags/', 'stdtauV_ssfrsd_dMglobloc.png'), dpi=fig.dpi)
def to_table(self): ''' make table of stellar-mass results ''' tab = t.QTable() tab['plateifu'] = [self.drpall_row['plateifu']] # tabulate mass in IFU tab['mass_in_ifu'] = self.mstar_in_ifu[ None, ...][:, self.bands_ixs[self.mlband]] nsa_absmag = self.nsa_absmags_cosmocorr #tab['nsa_absmag'].meta['bands'] = self.bands ifu_absmag = (self.ifu_flux_bands.to(u.ABmag) - self.distmod) #tab['ifu_absmag'].meta['bands'] = self.bands missing_flux = ((nsa_absmag + self.distmod).to(m.Mgy) - (ifu_absmag + self.distmod).to(m.Mgy)).clip( a_min=0. * m.Mgy, a_max=np.inf * m.Mgy) for i, b in enumerate(self.bands): outer_flux = missing_flux[i] if outer_flux <= 0. * m.Mgy: tab['outer_absmag_{}'.format(b)] = np.inf * u.ABmag tab['outer_lum_{}'.format(b)] = -np.inf * u.dex( m.bandpass_sol_l_unit) else: tab['outer_absmag_{}'.format(b)] = outer_flux.to( u.ABmag) - self.distmod tab['outer_lum_{}'.format(b)] = tab['outer_absmag_{}'.format( b)].to( u.dex(m.bandpass_sol_l_unit), bandpass_flux_to_solarunits(StellarMass.absmag_sun[i])) tab['outer_ml_ring'] = self.ml_ring() #tab['outer_ml_ring'].meta['band'] = self.mlband tab['ml_fluxwt'] = self.logml_fnuwt #tab['ml_fluxwt'].meta['band'] = self.mlband tab['distmod'] = self.distmod[None, ...] return tab
def logspace(start, stop, *args, **kwargs): from astropy.units import LogQuantity, dex if (not isinstance(start, LogQuantity) or not isinstance(stop, LogQuantity)): raise NotImplementedError # Get unit from end point as for linspace. stop = stop.to(dex(stop.unit.physical_unit)) start = start.to(stop.unit) unit = stop.unit.physical_unit return (start.value, stop.value) + args, kwargs, unit, None
def compare_missing_mass(tab, mlb='i', cb1='g', cb2='r'): mlb_ix = totalmass.StellarMass.bands_ixs[mlb] cb1_ix = totalmass.StellarMass.bands_ixs[cb1] cb2_ix = totalmass.StellarMass.bands_ixs[cb2] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] broadband_color = (tab['nsa_absmag'][:, cb1_ix] - tab['nsa_absmag'][:, cb2_ix]) outerlum_frac = tab['outer_lum'][:, mlb_ix].to(m.bandpass_sol_l_unit) / \ tab['nsa_absmag'][:, mlb_ix].to(m.bandpass_sol_l_unit, totalmass.bandpass_flux_to_solarunits(absmag_sun_mlb)) dlogmass_cmlr_ring = ((tab['mass_in_ifu'] + tab['outer_mass_cmlr']).to(u.dex(u.Msun)) - \ (tab['mass_in_ifu'] + tab['outer_mass_ring']).to(u.dex(u.Msun))) primarysample = m.mask_from_maskbits(tab['mngtarg1'], [10]) secondarysample = m.mask_from_maskbits(tab['mngtarg1'], [11]) fig, main_ax, hist_ax = make_panel_hist(top=0.9, left=.225) valid = np.isfinite(dlogmass_cmlr_ring) for selection, label, marker, color in zip( [primarysample, secondarysample], ['Primary', 'Secondary'], ['o', 'D'], ['r', 'b']): main_ax.scatter( x=broadband_color[selection * valid], y=dlogmass_cmlr_ring[selection * valid], c=color, edgecolor='None', s=5., marker=marker, label=label) hist_ax.hist(dlogmass_cmlr_ring[selection * valid], color=color, density=True, bins='auto', histtype='step', orientation='horizontal', linewidth=0.75) main_ax.set_xlim(np.percentile(broadband_color[valid], [1., 99.])) main_ax.set_ylim(np.percentile(dlogmass_cmlr_ring[valid], [1., 99.])) main_ax.legend(loc='best', prop={'size': 'xx-small'}) main_ax.tick_params(labelsize='xx-small') main_ax.set_xlabel(r'${}-{}$'.format(cb1, cb2), size='x-small') main_ax.set_ylabel(r'$\log \frac{M^{\rm tot}_{\rm CMLR}}{M^{\rm tot}_{\rm ring}}$', size='x-small') fig.suptitle(r'Impact of aperture-correction on $M^{\rm tot}$', size='small') fig.savefig(os.path.join(basedir, 'lib_diags/', 'mtot_compare_cmlr_ring.png'))
def make_stdtauV_vs_ssfrsd_dMass_fig(tab, sfrsd_tab, mltype='ring', mlb='i'): merge_tab = t.join(tab, sfrsd_tab, 'plateifu') mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] fig, ax = plt.subplots(1, 1, figsize=(3, 3), dpi=300) logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab['ifu_absmag'][:, mlb_ix].to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits(absmag_sun_mlb)) std_atten_mwtd = merge_tab['std_atten_mwtd'] mean_atten_mwtd = merge_tab['mean_atten_mwtd'] ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 mass_pca = merge_tab['mass_in_ifu'] + merge_tab['outer_mass_{}'.format(mltype)] ssfrsd = sfrsd / mass_pca sc = ax.scatter( x=np.log10(std_atten_mwtd), c=(logmass_in_ifu - logmass_in_ifu_lw), y=ssfrsd.to(u.dex(ssfrsd.unit)), edgecolor='k', linewidths=.125, s=2., cmap='viridis_r', vmin=.01, vmax=.12) cb = fig.colorbar(sc, ax=ax, extend='both') cb.set_label(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='xx-small') cb.ax.tick_params(labelsize='xx-small') ax.tick_params(which='major', labelsize='xx-small') ax.tick_params(which='minor', labelbottom=False, labelleft=False) ax.set_xlabel(r'$\log \sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log \frac{{\Sigma}^{\rm SFR}_{R<R_e}}{M^*_{\rm tot}}$', size='x-small') fig.tight_layout() fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.subplots_adjust(left=.2, bottom=.125, right=.9, top=.925) fig.savefig( os.path.join(basedir, 'lib_diags/', 'stdtauV_ssfrsd_dMglobloc.png'), dpi=fig.dpi)
def make_stdtauV_vs_dMass_ba_fig(tab, mlb='i'): mlb_ix = totalmass.StellarMass.bands_ixs[mlb] absmag_sun_mlb = totalmass.StellarMass.absmag_sun[mlb_ix] fig, ax, cax, hist_ax = make_panel_hcb_hist(figsize=(3, 3), dpi=300) logmass_in_ifu = tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = tab['ml_fluxwt'] + tab['ifu_absmag'][:, mlb_ix].to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits(absmag_sun_mlb)) std_atten_mwtd = tab['std_atten_mwtd'] mean_atten_mwtd = tab['mean_atten_mwtd'] ba = tab['nsa_elpetro_ba'] sc = ax.scatter( x=std_atten_mwtd, y=(logmass_in_ifu - logmass_in_ifu_lw), c=ba, edgecolor='k', linewidths=.125, s=2., cmap='viridis_r', vmin=.15, vmax=.8) cb = colorbartop(fig, sc, cax) cb.set_label(r'$\frac{b}{a}$', size='x-small', labelpad=0) ax.set_xlabel(r'$\sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='x-small') hist_ax.hist(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), bins='auto', histtype='step', orientation='horizontal', linewidth=.5, density=True, color='k') for yloc, lw, ls, c in zip( np.percentile(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), [16., 50., 84.]), [.5, 1., .5], ['--', '-', '--'], ['gray', 'k', 'gray']): hist_ax.axhline(yloc, linestyle=ls, linewidth=lw, color=c) fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.savefig( os.path.join(basedir, 'lib_diags/', 'stdtauV_dMglobloc_ba.png'), dpi=fig.dpi)
def make_stdtauV_vs_dMass_ba_fig(tab, mlb='i'): '''global-local plot: mean tau, dmass, b/a make figure plotting mass deficit on y, mass weighted atten on x, colored by NSA b/a Parameters ---------- tab : astropy.table.Table full aggregation results table mlb : {str}, optional mass-to-light bandpass (the default is 'i', SDSS i-band) ''' fig, ax, cax, hist_ax = make_panel_hcb_hist(figsize=(3, 3), dpi=300) logmass_in_ifu = tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = tab['ml_fluxwt'] + tab[f'logsollum_in_ifu_{mlb}'] std_atten_mwtd = tab['std_atten_mwtd'] mean_atten_mwtd = tab['mean_atten_mwtd'] ba = tab['nsa_elpetro_ba'] sc = ax.scatter( x=std_atten_mwtd, y=(logmass_in_ifu - logmass_in_ifu_lw), c=ba, edgecolor='k', linewidths=.125, s=1., cmap='viridis_r', alpha=0.5, vmin=.15, vmax=.8) ax.set_ylim([-.1, 0.3]) cb = colorbartop(fig, sc, cax) cb.set_label(r'$\frac{b}{a}$', size='x-small', labelpad=0) ax.set_xlabel(r'$\sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='x-small') hist_ax.hist(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), bins='auto', histtype='step', orientation='horizontal', linewidth=.5, density=True, color='k') for yloc, lw, ls, c in zip( np.percentile(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), [16., 50., 84.]), [.5, 1., .5], ['--', '-', '--'], ['gray', 'k', 'gray']): hist_ax.axhline(yloc, linestyle=ls, linewidth=lw, color=c) fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.savefig( os.path.join(csp_basedir, 'lib_diags/', 'stdtauV_dMglobloc_ba.png'), dpi=fig.dpi)
def p_main(p): ''' main : factor combined_units | combined_units | OPEN_BRACKET combined_units CLOSE_BRACKET | factor ''' from astropy.units.core import Unit from astropy.units import dex if len(p) == 3: p[0] = Unit(p[1] * p[2]) elif len(p) == 4: p[0] = dex(p[2]) else: p[0] = Unit(p[1])
def update_mass_table(res_fnames, mlband='i'): ''' ''' # filter out whose that have not been done if mass_table_old is None: already_aggregated = [False for _ in range(len(res_fnames))] else: already_aggregated = [ os.path.split(fn)[1].split('_')[0] in mass_table_old['plateifu'] for fn in res_fnames ] res_fnames = [fn for done, fn in zip(already_aggregated, res_fnames)] # aggregate individual galaxies, and stack them mass_tables_new = list( ProgressBar.map(partial(mass_agg_onegal, mlband=mlband), res_fnames, multiprocess=False, step=5)) mass_table_new = t.vstack(mass_tables_new) # if there was an old mass table, stack it with the new one if mass_table_old is None: mass_table = mass_table_new else: mass_table = t.vstack([mass_table_old, mass_table_new], join_type='inner') cmlr = cmlr_kwargs cb1, cb2 = cmlr['cb1'], cmlr['cb2'] color_missing_flux = mass_table['outer_absmag_{}'.format(cb1)] - \ mass_table['outer_absmag_{}'.format(cb2)] mass_table['outer_ml_cmlr'] = np.polyval( cmlr['cmlr_poly'], color_missing_flux.value) * u.dex(m.m_to_l_unit) mass_table['outer_mass_ring'] = \ (mass_table['outer_lum_{}'.format(mlband)] + \ mass_table['outer_ml_ring']).to(u.Msun) mass_table['outer_mass_cmlr'] = \ (mass_table['outer_lum_{}'.format(mlband)] + \ mass_table['outer_ml_cmlr']).to(u.Msun) return mass_table['plateifu', 'mass_in_ifu', 'outer_mass_cmlr', 'outer_mass_ring']
def make_meanstdtauV_vs_dMass_fig(tab, mlb='i'): '''global-local plot: sig tau, mean tau, dmass make figure plotting mass weighted atten on x, std of mass weighted atten on y, colored by mass deficit Parameters ---------- tab : astropy.table.Table full aggregation results table mlb : {str}, optional mass-to-light bandpass (the default is 'i', SDSS i-band) ''' fig, ax, cax, hist_ax = make_panel_hcb_hist(figsize=(3, 3), dpi=300) logmass_in_ifu = tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = tab['ml_fluxwt'] + tab[f'logsollum_in_ifu_{mlb}'] std_atten_mwtd = tab['std_atten_mwtd'] mean_atten_mwtd = tab['mean_atten_mwtd'] sc = ax.scatter( y=std_atten_mwtd, x=mean_atten_mwtd, c=(logmass_in_ifu - logmass_in_ifu_lw), edgecolor='k', linewidths=.125, s=1., cmap='viridis_r', alpha=0.5, vmin=.005, vmax=.15) cb = colorbartop(fig, sc, cax) cb.set_label(r'$\log \frac{M^*}{M_{\rm LW}}$', size='x-small', labelpad=0) ax.set_ylabel(r'$\sigma_{\tau_V}$', size='x-small') ax.set_xlabel(r'$\bar{\tau_V}$', size='x-small') hist_ax.hist(std_atten_mwtd, bins='auto', histtype='step', range=[0.1, 1.5], orientation='horizontal', linewidth=.5, density=True, color='k') for yloc, lw, ls, c in zip( np.percentile(std_atten_mwtd[np.isfinite(std_atten_mwtd)], [16., 50., 84.]), [.5, 1., .5], ['--', '-', '--'], ['gray', 'k', 'gray']): hist_ax.axhline(yloc, linestyle=ls, linewidth=lw, color=c) fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.savefig( os.path.join(csp_basedir, 'lib_diags/', 'mean+stdtauV_dMglobloc.png'), dpi=fig.dpi)
class TestRoundtripCDS(RoundtripBase): format_ = 'cds' @pytest.mark.parametrize('unit', [ unit for unit in u_format.CDS._units.values() if (isinstance(unit, core.UnitBase) and not isinstance(unit, core.PrefixUnit))]) def test_roundtrip(self, unit): self.check_roundtrip(unit) if unit == u.mag: # Skip mag: decomposes into dex, which is unknown to CDS. return self.check_roundtrip_decompose(unit) @pytest.mark.parametrize('unit', [u.dex(unit) for unit in (u.cm/u.s**2, u.K, u.Lsun)]) def test_roundtrip_dex(self, unit): string = unit.to_string(format='cds') recovered = u.Unit(string, format='cds') assert recovered == unit
def __init__(self, results, pca_system, drp, dap, drpall_row, cosmo, mlband='i'): self.results = results self.pca_system = pca_system self.drp = drp self.dap = dap self.drpall_row = drpall_row self.cosmo = cosmo self.mlband = mlband with catch_warnings(): simplefilter('ignore') self.results.setup_photometry(pca_system) self.s2p = results.spec2phot # stellar mass to light ratio self.ml0 = results.cubechannel('ML{}'.format(mlband), 0) self.badpdf = results.cubechannel('GOODFRAC', 2) < 1.0e-4 self.ml_mask = np.logical_or.reduce((self.results.mask, self.badpdf)) # infer values in interior spaxels affected by one of the following: # bad PDF, foreground star, dead fiber self.low_or_no_cov = m.mask_from_maskbits(self.drp['MASK'].data, [0, 1]).mean(axis=0) > .3 self.drp3dmask_interior = m.mask_from_maskbits( self.drp['MASK'].data, [2, 3]).mean(axis=0) > .3 self.interior_mask = np.logical_or.reduce( (self.badpdf, self.drp3dmask_interior)) self.logml_final = infer_masked(q_trn=self.ml0, bad_trn=self.ml_mask, infer_here=self.interior_mask) * u.dex( m.m_to_l_unit)
def test_qtable_column_conversion(): """ Ensures that a QTable that gets assigned a unit switches to be Quantity-y """ qtab = table.QTable([[1, 2], [3, 4.2]], names=['i', 'f']) assert isinstance(qtab['i'], table.column.Column) assert isinstance(qtab['f'], table.column.Column) qtab['i'].unit = 'km/s' assert isinstance(qtab['i'], u.Quantity) assert isinstance(qtab['f'], table.column.Column) # should follow from the above, but good to make sure as a #4497 regression test assert isinstance(qtab['i'][0], u.Quantity) assert isinstance(qtab[0]['i'], u.Quantity) assert not isinstance(qtab['f'][0], u.Quantity) assert not isinstance(qtab[0]['f'], u.Quantity) # Regression test for #5342: if a function unit is assigned, the column # should become the appropriate FunctionQuantity subclass. qtab['f'].unit = u.dex(u.cm/u.s**2) assert isinstance(qtab['f'], u.Dex)
def test_qtable_column_conversion(): """ Ensures that a QTable that gets assigned a unit switches to be Quantity-y """ qtab = table.QTable([[1, 2], [3, 4.2]], names=['i', 'f']) assert isinstance(qtab['i'], table.column.Column) assert isinstance(qtab['f'], table.column.Column) qtab['i'].unit = 'km/s' assert isinstance(qtab['i'], u.Quantity) assert isinstance(qtab['f'], table.column.Column) # should follow from the above, but good to make sure as a #4497 regression test assert isinstance(qtab['i'][0], u.Quantity) assert isinstance(qtab[0]['i'], u.Quantity) assert not isinstance(qtab['f'][0], u.Quantity) assert not isinstance(qtab[0]['f'], u.Quantity) # Regression test for #5342: if a function unit is assigned, the column # should become the appropriate FunctionQuantity subclass. qtab['f'].unit = u.dex(u.cm / u.s**2) assert isinstance(qtab['f'], u.Dex)
def test_str(self): """Do some spot checks that str, repr, etc. work as expected.""" lu1 = u.mag(u.Jy) assert str(lu1) == 'mag(Jy)' assert repr(lu1) == 'Unit("mag(Jy)")' assert lu1.to_string('generic') == 'mag(Jy)' with pytest.raises(ValueError): lu1.to_string('fits') lu2 = u.dex() assert str(lu2) == 'dex' assert repr(lu2) == 'Unit("dex(1)")' assert lu2.to_string() == 'dex(1)' lu3 = u.MagUnit(u.Jy, function_unit=2*u.mag) assert str(lu3) == '2 mag(Jy)' assert repr(lu3) == 'MagUnit("Jy", unit="2 mag")' assert lu3.to_string() == '2 mag(Jy)' lu4 = u.mag(u.ct) assert lu4.to_string('generic') == 'mag(ct)' assert lu4.to_string('latex') == ('$\\mathrm{mag}$$\\mathrm{\\left( ' '\\mathrm{ct} \\right)}$') assert lu4._repr_latex_() == lu4.to_string('latex')
'tm': tm, 'dt': TimeDelta([1, 2] * u.day), 'sc': sc, 'scc': scc, 'scd': SkyCoord([1, 2], [3, 4], [5, 6], unit='deg,deg,m', frame='fk4', obstime=['J1990.5', 'J1991.5']), 'x': [1, 2] * u.m, 'qdb': [10, 20] * u.dB(u.mW), 'qdex': [4.5, 5.5] * u.dex(u.cm / u.s**2), 'qmag': [21, 22] * u.ABmag, 'lat': Latitude([1, 2] * u.deg), 'lon': Longitude([1, 2] * u.deg, wrap_angle=180. * u.deg), 'ang': Angle([1, 2] * u.deg), 'el2': el2, } time_attrs = ['value', 'shape', 'format', 'scale', 'location'] compare_attrs = { 'c1': ['data'], 'c2': ['data'],
return olsfit if __name__ == '__main__': mlband = 'i' mass_table = update_mass_table(drpall, mass_table_old=None, limit=None, mlband=mlband) drpall.keep_columns(['plateifu', 'mangaid', 'objra', 'objdec', 'ebvgal', 'mngtarg1', 'mngtarg2', 'mngtarg3', 'nsa_iauname', 'ifudesignsize', 'nsa_z', 'nsa_zdist', 'nsa_nsaid', 'nsa_elpetro_ba', 'nsa_elpetro_mass']) full_table = t.join(mass_table, drpall, 'plateifu') mlband_ix = totalmass.StellarMass.bands_ixs[mlband] mlband_absmag_sun = totalmass.StellarMass.absmag_sun[mlband_ix] mass_deficit = full_table['mass_in_ifu'].to(u.dex(u.Msun)) - \ (full_table['ml_fluxwt'] + full_table['ifu_absmag'][:, mlband_ix].to( u.dex(m.bandpass_sol_l_unit), totalmass.bandpass_flux_to_solarunits( mlband_absmag_sun))) mass_deficit_order = np.argsort(mass_deficit)[::-1] compare_outerml_ring_cmlr(full_table) compare_missing_mass(full_table) make_missing_mass_fig(full_table, mltype='ring') make_missing_mass_fig(full_table, mltype='cmlr') make_missing_flux_fig(full_table) compare_mtot_pca_nsa(full_table, jhumpa, mltype='ring') #compare_mtot_pca_nsa(full_table, jhumpa, mltype='cmlr') make_meanstdtauV_vs_dMass_fig(full_table) make_stdtauV_vs_dMass_ba_fig(full_table)
"M_E": u.M_earth, "Earth Mass": u.M_earth, "M_J": u.M_jupiter, "Jupiter Mass": u.M_jupiter, "R_Earth": u.R_earth, # Add u.R_jupiter "Earth Radius": u.R_earth, "Jupiter Radius": u.R_jupiter, "R_Sun": u.R_sun, "Rstar": u.R_sun, "a_perp": u.au, "arc-sec/year": u.arcsec / u.yr, "cm/s**2": u.cm / u.s**2, "g/cm**3": u.g / u.cm**3, "days": u.day, "degrees": u.deg, "dexincgs": u.dex(u.cm / u.s**2), "hours": u.hr, "hrs": u.hr, "kelvin": u.K, "logLsun": u.dex(u.L_sun), "log(Solar)": u.dex(u.L_sun), "mags": u.mag, "microas": u.uas, "perc": u.percent, "pi_E": None, "pi_EE": None, "pi_EN": None, "pi_rel": None, "ppm": cds.ppm, "seconds": u.s, "Solar mass": u.M_sun,
def __init__(self, fitfilename, sedfilename): """ potato potato potato """ fitfile = open(fitfilename) sedfile = open(sedfilename) fitinfo = fitfile.readlines() sedinfo = sedfile.readlines() fitfile.close() sedfile.close() #strip out newline characters for i in range(len(fitinfo)): fitinfo[i] = fitinfo[i].strip() for i in range(len(sedinfo)): sedinfo[i] = sedinfo[i].strip() #first go through fitinfo filternames = fitinfo[1].strip("#") filternames = filternames.split() flux = np.array(fitinfo[2].split(), dtype=float) * u.Jy fluxerr = np.array(fitinfo[3].split(), dtype=float) * u.Jy predicted = np.array(fitinfo[12].split(), dtype=float) * u.Jy self.obs_filters = filternames self.obs_flux = flux self.obs_flux_err = fluxerr self.obs_predict = predicted bestfitmodel = fitinfo[8].split() self.bestfit_i_sfh = int(bestfitmodel[0]) self.bestfit_i_ir = int(bestfitmodel[1]) self.bestfit_chi2 = float(bestfitmodel[2]) self.bestfit_redshift = float(bestfitmodel[3]) bestfitparams = fitinfo[9].strip('.#') bestfitparams = re.split('\.+', bestfitparams) bestfitresults = list(map(float, fitinfo[10].split())) assert len(bestfitparams) == len(bestfitresults) for i, paramname in enumerate(bestfitparams): setattr(self, self.clean_param_names(paramname), bestfitresults[i]) #now working on the marginal PDF histograms for each parameter marginalpdfs = fitinfo[15:] #first, need to split the pdfs into each parameter self.marginal_pdfs = {} self.marginal_percentiles = {} hash_idx = [] for i in range(len(marginalpdfs)): if '#' in marginalpdfs[i]: hash_idx.append(i) assert len(hash_idx) % 2 == 0 for i in range(len(hash_idx) // 2): param = marginalpdfs[hash_idx[2 * i]].strip(' #.') marginal = marginalpdfs[hash_idx[2 * i] + 1:hash_idx[2 * i + 1]] marginal = np.array([j.split() for j in marginal], dtype=float) percentile = np.array(marginalpdfs[hash_idx[2 * i + 1] + 1].split(), dtype=float) self.marginal_pdfs[self.clean_param_names(param)] = marginal self.marginal_percentiles[self.clean_param_names( param)] = percentile #now time for the SED file self.sed_model_params = {} #there are model names and params on lines 2 & 3 and 5 & 6 modelparams = sedinfo[2].strip('.#') modelparams = re.split('\.+', modelparams) model_vals = list(map(float, sedinfo[3].split())) assert len(modelparams) == len(model_vals) for i, paramname in enumerate(modelparams): self.sed_model_params[self.clean_param_names( paramname)] = model_vals[i] modelparams = sedinfo[5].strip('.#') modelparams = re.split('\.+', modelparams) model_vals = list(map(float, sedinfo[6].split())) assert len(modelparams) == len(model_vals) for i, paramname in enumerate(modelparams): self.sed_model_params[self.clean_param_names( paramname)] = model_vals[i] #sed is from line 10 to the end. #three columns, log lambda, log L_lam attenuated, log L_lam unattenuated model_sed = sedinfo[10:] model_sed = [i.split() for i in model_sed] self.sed_model = np.array(model_sed, dtype=float) self.sed_model_logwaves = self.sed_model[:, 0] * u.dex(u.AA) self.sed_model_logluminosity_lambda = self.sed_model[:, 1] * u.dex( u.Lsun / u.AA)
import sys # Related third party imports. from astropy import units as u from astropy.io import ascii import pandas from math import isnan import matplotlib.pyplot as plt import numpy from scipy.interpolate import interp1d #import splat from splat import SPLAT_PATH, SPLAT_URL EVOLUTIONARY_MODEL_FOLDER = '/reference/EvolutionaryModels/' EMODELS = ['baraffe','burrows','saumon'] EPARAMETERS = ['mass','age','temperature','gravity','luminosity','radius'] EPARAMETER_UNITS = {'mass': u.solMass, 'age': u.Gyr, 'temperature': u.K, 'gravity': u.dex(u.cm / u.s**2),\ 'luminosity': u.dex(u.solLum), 'radius': u.solRad} # change the command prompt sys.ps1 = 'splat evolve> ' ############################################################################### ############################################################################### def readModel(*model,**kwargs): """ :Description: This class reads in evolutionary models that are defined in the methods below, and their data is acquired `here <http://pono.ucsd.edu/~adam/splat/EvolutionaryModels/>`_. Units are the following: masses are in M/Msun, luminosities in log L/Lsun, radius in R/Rsun, surface
def test_cds_log10_dimensionless(): assert u.Unit('[-]', format='cds') == u.dex(u.dimensionless_unscaled) assert u.dex(u.dimensionless_unscaled).to_string(format='cds') == "[-]"
(["uarcmin"], u.uarcmin), (["uarcsec"], u.uarcsec), (["kbarn"], u.kbarn), (["Gbit"], u.Gbit), (["Gibit"], 2 ** 30 * u.bit), (["kbyte"], u.kbyte), (["mRy"], 0.001 * u.Ry), (["mmag"], u.mmag), (["Mpc"], u.Mpc), (["Gyr"], u.Gyr), (["°"], u.degree), (["°/s"], u.degree / u.s), (["Å"], u.AA), (["Å/s"], u.AA / u.s), (["\\h"], si.h), (["[cm/s2]"], dex(u.cm / u.s ** 2)), (["[K]"], dex(u.K)), (["[-]"], dex(u.dimensionless_unscaled))]) def test_cds_grammar(strings, unit): for s in strings: print(s) unit2 = u_format.CDS.parse(s) assert unit2 == unit @pytest.mark.parametrize('string', [ '0.1 nm', 'solMass(3/2)', 'km / s', 'km s-1', 'pix0.1nm',
def __init__(self, fitfilename, sedfilename): """ potato potato potato """ fitfile = open(fitfilename) sedfile = open(sedfilename) fitinfo = fitfile.readlines() sedinfo = sedfile.readlines() fitfile.close() sedfile.close() #strip out newline characters for i in range(len(fitinfo)): fitinfo[i] = fitinfo[i].strip() for i in range(len(sedinfo)): sedinfo[i] = sedinfo[i].strip() #first go through fitinfo filternames = fitinfo[1].strip("#") filternames = filternames.split() flux = np.array(fitinfo[2].split(), dtype=float) * u.Jy fluxerr = np.array(fitinfo[3].split(), dtype=float) * u.Jy predicted = np.array(fitinfo[12].split(), dtype=float) * u.Jy self.obs_filters = filternames self.obs_flux = flux self.obs_flux_err = fluxerr self.obs_predict = predicted bestfitmodel = fitinfo[8].split() self.bestfit_i_sfh = int(bestfitmodel[0]) self.bestfit_i_ir = int(bestfitmodel[1]) self.bestfit_chi2 = float(bestfitmodel[2]) self.bestfit_redshift = float(bestfitmodel[3]) bestfitparams = fitinfo[9].strip('.#') bestfitparams = re.split('\.+',bestfitparams) bestfitresults = list(map(float,fitinfo[10].split())) assert len(bestfitparams) == len(bestfitresults) for i,paramname in enumerate(bestfitparams): setattr(self,self.clean_param_names(paramname),bestfitresults[i]) #now working on the marginal PDF histograms for each parameter marginalpdfs = fitinfo[15:] #first, need to split the pdfs into each parameter self.marginal_pdfs = {} self.marginal_percentiles = {} hash_idx = [] for i in range(len(marginalpdfs)): if '#' in marginalpdfs[i]: hash_idx.append(i) assert len(hash_idx) % 2 == 0 for i in range(len(hash_idx)//2): param = marginalpdfs[hash_idx[2*i]].strip(' #.') marginal = marginalpdfs[hash_idx[2*i]+1:hash_idx[2*i+1]] marginal = np.array([j.split() for j in marginal],dtype=float) percentile = np.array(marginalpdfs[hash_idx[2*i+1]+1].split(),dtype=float) self.marginal_pdfs[self.clean_param_names(param)] = marginal self.marginal_percentiles[self.clean_param_names(param)] = percentile #now time for the SED file self.sed_model_params = {} #there are model names and params on lines 2 & 3 and 5 & 6 modelparams = sedinfo[2].strip('.#') modelparams = re.split('\.+',modelparams) model_vals = list(map(float,sedinfo[3].split())) assert len(modelparams) == len(model_vals) for i,paramname in enumerate(modelparams): self.sed_model_params[self.clean_param_names(paramname)] = model_vals[i] modelparams = sedinfo[5].strip('.#') modelparams = re.split('\.+',modelparams) model_vals = list(map(float,sedinfo[6].split())) assert len(modelparams) == len(model_vals) for i,paramname in enumerate(modelparams): self.sed_model_params[self.clean_param_names(paramname)] = model_vals[i] #sed is from line 10 to the end. #three columns, log lambda, log L_lam attenuated, log L_lam unattenuated model_sed = sedinfo[10:] model_sed = [i.split() for i in model_sed] self.sed_model = np.array(model_sed,dtype=float) self.sed_model_logwaves = self.sed_model[:,0] * u.dex(u.AA) self.sed_model_logluminosity_lambda = self.sed_model[:,1] * u.dex(u.Lsun / u.AA)
UNIT_MAPPER = { "--": None, "BJD": None, # TODO: optionally supprot mapping columns to Time objects "BKJD": None, # TODO: optionally supprot mapping columns to Time objects "D_L": u.pc, "D_S": u.pc, "Earth flux": None, # TODO: Include Earth insolation units "Fearth": None, # TODO: Include Earth insolation units "M_E": u.M_earth, "M_J": u.M_jupiter, "R_Earth": u.R_earth, "R_Sun": u.R_sun, "Rstar": u.R_sun, "a_perp": u.au, "arc-sec/year": u.arcsec / u.yr, "cm/s**2": u.dex(u.dm / u.s**2), "days": u.day, "degrees": u.deg, "dexincgs": u.dex(u.cm / u.s**2), "hours": u.hr, "hrs": u.hr, "kelvin": u.K, "logLsun": u.dex(u.L_sun), "mags": u.mag, "microas": u.uas, "perc": u.percent, "pi_E": None, "pi_EE": None, "pi_EN": None, "pi_rel": None, "ppm": cds.ppm,
def mag_to_dexmasstolight(q, cmlr_poly): qmag = q.to('mag') return np.polyval(cmlr_poly, qmag.value) * u.dex(m.m_to_l_unit)
def test_pickle(): lu1 = u.dex(u.cm/u.s**2) s = pickle.dumps(lu1) lu2 = pickle.loads(s) assert lu1 == lu2
def make_stdtauV_vs_dMass_ssfrsd_fig(tab, sfrsd_tab, mltype='ring', mlb='i'): '''global-local plot: sig tau, dmass, ssfrsd [description] Parameters ---------- tab : astropy.table.Table full aggregation results table sfrsd_tab : astropy.table.Table specific star formation rate table mlb : {str}, optional mass-to-light bandpass (the default is 'i', SDSS i-band) mltype : {str} 'ring' or 'cmlr', the M/L applied to the ouside flux (the default is 'ring') ''' merge_tab = t.join(tab, sfrsd_tab, 'plateifu') fig, ax, cax, hist_ax = make_panel_hcb_hist(figsize=(3, 3), dpi=300, top=.8) logmass_in_ifu = merge_tab['mass_in_ifu'].to(u.dex(u.Msun)) logmass_in_ifu_lw = merge_tab['ml_fluxwt'] + merge_tab[f'logsollum_in_ifu_{mlb}'] std_atten_mwtd = merge_tab['std_atten_mwtd'] mean_atten_mwtd = merge_tab['mean_atten_mwtd'] ha_corr = np.exp(merge_tab['mean_atten_mwtd'] * (6563 / 5500)**-1.3) sfrsd = merge_tab['sigma_sfr'] * ha_corr * u.Msun / u.yr / u.pc**2 outer_mass = (merge_tab[f'outerml_{mltype}'] + \ merge_tab[f'logsollum_outer_{mlb}']).to(u.Msun) mass_pca = merge_tab['mass_in_ifu'].to(u.Msun) + outer_mass ssfrsd = sfrsd / mass_pca sc = ax.scatter( x=std_atten_mwtd, y=(logmass_in_ifu - logmass_in_ifu_lw), c=ssfrsd.to(u.dex(ssfrsd.unit)), edgecolor='k', linewidths=.125, s=1., cmap='viridis_r', alpha=0.5, vmin=-15., vmax=-10.) ax.set_ylim([-.1, 0.3]) cb = colorbartop(fig, sc, cax) cb.set_label(r'$\log \frac{{\Sigma}^{\rm SFR}_{R<R_e}}{M^*_{\rm tot}}$', size='xx-small') ax.tick_params(which='major', labelsize='xx-small') ax.tick_params(which='minor', labelbottom=False, labelleft=False) ax.set_xscale('log') ax.set_xlabel(r'$\sigma_{\tau_V}$', size='x-small') ax.set_ylabel(r'$\log{ \frac{M^*}{M^*_{\rm LW}} ~ {\rm [dex]} }$', size='x-small') hist_ax.hist(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), bins='auto', histtype='step', orientation='horizontal', linewidth=.5, density=True, color='k') for yloc, lw, ls, c in zip( np.percentile(np.ma.masked_invalid(logmass_in_ifu - logmass_in_ifu_lw).compressed(), [16., 50., 84.]), [.5, 1., .5], ['--', '-', '--'], ['gray', 'k', 'gray']): hist_ax.axhline(yloc, linestyle=ls, linewidth=lw, color=c) fig.suptitle('Mass excess from luminosity-weighting', size='x-small') fig.subplots_adjust(left=0.25) fig.savefig( os.path.join(csp_basedir, 'lib_diags/', 'stdtauV_dMglobloc_ssfrsd.png'), dpi=fig.dpi)
# 'morley12': 'Morley et al. (2012)',\ # 'morley14': 'Morley et al. (2014)',\ # 'saumon12': 'Saumon et al. (2012)',\ # 'drift': 'Witte et al. (2011)'} SPECTRAL_MODELS = {\ 'burrows06': {'name': 'Burrows et al. (2006)', 'bibcode': '2006ApJ...640.1063B', 'altnames': ['burrows','burrows2006'], 'rawfolder': HOME_FOLDER+'/models/burrows/burrows06/'}, \ 'btsettl08': {'name': 'BT-Settl (2008)', 'bibcode': '2012RSPTA.370.2765A', 'altnames': ['allard','allard12','allard2012','btsettl','btsettled','btsettl08','btsettl2008','BTSettl2008'], 'rawfolder': HOME_FOLDER+'/models/allard/cifist2011/'}, \ 'madhusudhan11': {'name': 'Madhusudhan et al. (2011)', 'bibcode': '2011ApJ...737...34M', 'altnames': ['madhu','madhusudhan','madhu11','madhu2011','madhusudhan2011'], 'rawfolder': HOME_FOLDER+'/models/burrows/burrows11/all/'}, \ 'morley12': {'name': 'Morley et al. (2012)', 'bibcode': '2012ApJ...756..172M', 'altnames': ['morley','morley2012'], 'rawfolder': HOME_FOLDER+'/models/ames/Morley12/'}, \ 'morley14': {'name': 'Morley et al. (2014)', 'bibcode': '2014ApJ...787...78M', 'altnames': ['morley2014'], 'rawfolder': HOME_FOLDER+'/models/ames/Morley14/'}, \ 'saumon12': {'name': 'Saumon et al. (2012)', 'bibcode': '2012ApJ...750...74S', 'altnames': ['saumon','saumon2012'], 'rawfolder': HOME_FOLDER+'/models/ames/Saumon12/'}, \ 'drift': {'name': 'Witte et al. (2011)', 'bibcode': '2011A&A...529A..44W', 'altnames': ['witte','witte11','witte2011','helling'], 'rawfolder': HOME_FOLDER+'/models/drift/'}} SPECTRAL_MODEL_PARAMETERS_INORDER = ['teff','logg','z','fsed','cld','kzz'] SPECTRAL_MODEL_PARAMETERS = {\ 'teff': {'unit': u.K, 'default': 1000.0, 'title': '$T_{eff}$'}, \ 'logg': {'unit': u.dex(u.cm/u.s/u.s), 'default': 5.0, 'title': '$\log{g}$'}, \ 'z': {'unit': u.dex(), 'default': 0., 'title': '$[M/H]$'}, \ 'fsed': {'unit': u.m/u.m, 'default': 'nc', 'title': '$f_{sed}$'}, \ 'cld': {'unit': u.m/u.m, 'default': 'nc', 'title': '$cld$'}, \ 'kzz': {'unit': u.m/u.m, 'default': 'eq', 'title': '$log\ \kappa_{zz}$'}} # 'kzz': u.dex(u.cm*u.cm/u.s), \ # 'slit': {'unit': u.arcsec, 'default': 0.5, 'title': 'slit'}} SPECTRAL_MODEL_FLUX_UNIT = u.erg/(u.s*u.micron*(u.cm**2)) SPECTRAL_MODEL_WAVE_UNIT = u.micron #DEFINED_MODEL_BIBCODES = {\ # 'BTSettl2008': '', \ # 'burrows06': '2006ApJ...640.1063B',\ # 'morley12': '2012ApJ...756..172M',\ # 'morley14': '2014ApJ...787...78M',\ # 'saumon12': '2012ApJ...750...74S',\ # 'drift': '2011A&A...529A..44W'}