def get_data(table_file='default'): if table_file == 'default': table_file = snap_src + '/tables/ancients_0.1_0.2_galaxies.dat' ags = sfh_tests_multi_proc.AncientGalaxies() ags.read_trgb_table(table_file) data_dict = {} for i, target in enumerate(ags.data.target): for band in ['opt', 'ir']: ratio = ags.data[i]['n%s_agb' % band] / \ ags.data[i]['n%s_rgb' % band] data_dict['%s_%s' % (target, band)] = ratio data_dict['%s_%s_err' % (target, band)] = \ galaxy_tests.count_uncert_ratio(ags.data[i]['n%s_agb' % band], ags.data[i]['n%s_rgb' % band]) return data_dict
def data_table(targets, table_file='default'): # target, av, dist, [opt: frac comp, trgb, nrgb, nagb ratio] [ir: ..] if table_file == 'default': table_file = snap_src + '/tables/ancients_0.1_0.2_galaxies.dat' ags = sfh_tests_multi_proc.AncientGalaxies() ags.read_trgb_table(table_file) data_dict = get_data(table_file=table_file) comp_data = tables.read_completeness_table() row = '' row2 = '' ts = list(set([t.lower() for t in targets]) & set(ags.data.target)) assert len(ts) == len(targets), 'cant find all targets in ags.data' inds = list([np.where(t.lower() == ags.data.target)[0][0] for t in targets]) opt_agb_tot = np.sum(ags.data.nopt_agb[inds]) opt_rgb_tot = np.sum(ags.data.nopt_rgb[inds]) ir_agb_tot = np.sum(ags.data.nir_agb[inds]) ir_rgb_tot = np.sum(ags.data.nir_rgb[inds]) totfmt = 'Total & %i & %i & $%.3f\\pm%.3f$ & %i & %i & $%.3f\\pm%.3f$ \\\\' total = totfmt % (opt_agb_tot, opt_rgb_tot, opt_agb_tot/opt_rgb_tot, galaxy_tests.count_uncert_ratio(opt_agb_tot, opt_rgb_tot), ir_agb_tot, ir_rgb_tot, ir_agb_tot/ir_rgb_tot, galaxy_tests.count_uncert_ratio(ir_agb_tot, ir_rgb_tot)) for target in targets: if target.upper() == 'NGC2976-DEEP': extra_key='F606W,F814W' else: extra_key=None (Av, dmod) = [angst_data.get_item(target, i, extra_key=extra_key) for i in ['Av', 'dmod']] comp_row = rsp.fileIO.get_row(comp_data, 'target', target) nstars_row = rsp.fileIO.get_row(ags.data, 'target', target) sub_dict = {} opt_err_pct = [] ir_err_pct = [] for (k,v) in data_dict.items(): if '404' in target: target = target.lower().replace('-deep', '') if target in k or target.lower() in k: sub_dict[k] = v opt_err_pct.append(sub_dict['%s_opt_err' % target.lower()] / sub_dict['%s_opt' % target.lower()]) ir_err_pct.append(sub_dict['%s_ir_err' % target.lower()] / sub_dict['%s_ir' % target.lower()]) row += '%s & %.2f & %.2f & ' % (target, Av, dmod) row += '%(opt_filter2).2f & ' % comp_row row += '%(opt_trgb).2f & ' % nstars_row row += '%(ir_filter2).2f & ' % comp_row row += '%(ir_trgb).2f \\\\ \n' % nstars_row row2 += '%s & ' % target row2 += '%(nopt_agb)i & %(nopt_rgb)i & ' % nstars_row row2 += '$%.3f\\pm%.3f$ & ' % (sub_dict['%s_opt' % target.lower()], sub_dict['%s_opt_err' % target.lower()]) row2 += '%(nir_agb)i & %(nir_rgb)i & ' % nstars_row row2 += '$%.3f\\pm%.3f$ \\\\ \n' % (sub_dict['%s_ir' % target.lower()], sub_dict['%s_ir_err' % target.lower()]) with open('data_table.tex', 'w') as out: out.write(row) out.write(row2) out.write(total) out.write('# max err pct. opt: %.3f ir: %.3f \n' % \ (np.max(opt_err_pct), np.max(ir_err_pct))) return total
def narratio_table(narratio_files, table_file='default'): '''write the latex table''' data_dict = get_data(table_file=table_file) nar_dict = result2dict(narratio_files) targets = list(np.unique([k.split('_')[0] for k in nar_dict.keys()])) agb_mods = list(np.unique(['_'.join(k.split('_')[1:4]) for k in nar_dict.keys()])) # table columns: target, data ratio, (ratio, frac diff) per agb mod ir_table = np.empty(shape=(len(targets) + 1, len(agb_mods)*2 + 1), dtype='|S20') ir_table[:, :] = '' opt_table = np.empty(shape=(len(targets) + 1, len(agb_mods)*2 + 1), dtype='|S20') opt_table[:, :] = '' fmt = r'$%.3f\pm%.3f$ & ' fmt2 = r'$%.3f\pm%.3f$ \\' for key, val in nar_dict.items(): # go through half the dict (see below) if 'err' in key: continue # choose which table if 'ir' in key: table = ir_table band = 'ir' if 'opt' in key: table = opt_table band = 'opt' # choose the correct row and column placement in the table target = key.split('_')[0] agb_mod = '_'.join(key.split('_')[1:4]) row = targets.index(target) # column 0 is target, columns 1, 3, 5 have ratios column = (agb_mods.index(agb_mod) * 2) + 1 #print row, column # target table[row, 0] = '%s &' % target.upper() # data dnarr = data_dict['%s_%s' % (target.lower(), band)] derr = data_dict['%s_%s_err' % (target.lower(), band)] #dstr = fmt % (dnarr, derr) #table[row, 1] = dstr # model mnarr = val # grab the error from the dict err_key = key.replace('mean', 'err_mean') mnerr = nar_dict[err_key] mstr = fmt % (mnarr, mnerr) table[row, column] = mstr # frac difference #pct_diff = (mnarr - dnarr) / dnarr #pct_diff_err = np.abs(pct_diff * (mnerr/mnarr + derr/dnarr)) pct_diff = (mnarr / dnarr) pct_diff_err = np.abs(pct_diff * (mnerr/mnarr + derr/dnarr)) f = fmt # if final column, put \\ not & if column + 1 == table.shape[1] - 1: f = fmt2 pdstr = f % (pct_diff, pct_diff_err) table[row, column + 1] = pdstr # totals: nar_dict = result2dict(narratio_files, search='all') data_total = data_table(targets) data_total = np.array(data_total.translate(None, '\\$Total&').replace('pm', ' ').split(), dtype=float) for i, agb_mod in enumerate(agb_mods): for band, table in zip(['ir', 'opt'], [ir_table, opt_table]): f = fmt column = (i * 2) + 1 nrgb = np.sum([v for k, v in nar_dict.items() if agb_mod in k and band in k and 'rgb' in k]) nagb = np.sum([v for k, v in nar_dict.items() if agb_mod in k and band in k and 'agb' in k]) ratio = nagb / nrgb err = galaxy_tests.count_uncert_ratio(nagb, nrgb) table[-1, column] = f % (ratio, err) column += 1 if column == table.shape[1] - 1: f = fmt2 # frac difference if band == 'ir': j = 6 if band == 'opt': j = 2 dratio = data_total[j] derr = data_total[j+1] pct_diff = (ratio / dratio) pct_diff_err = np.abs(pct_diff * (err/ratio + derr/dratio)) #table[-1, column] = f % (pct_diff, pct_diff_err) #table[-1, 0] = 'Total & ' # mean for i, agb_mod in enumerate(agb_mods): for table in [ir_table, opt_table]: f = fmt column = (i * 2) + 1 val, err = \ np.mean(np.array([l.translate(None, ' $&\\').split('pm') for l in table[:, column][:-1]], dtype=float), axis=0) print agb_mod, err/val table[-1, column] = f % (val, err) column += 1 if column == table.shape[1] - 1: f = fmt2 val, err = \ np.mean(np.array([l.translate(None, ' $&\\').split('pm') for l in table[:, column][:-1]], dtype=float), axis=0) table[-1, column] = f % (val, err) table[-1, 0] = 'Mean & ' # write the file ratio_str = '$\\frac{N_{\\rm TP-AGB}}{N_{\\rm RGB}}$' header = 'Target & ' for i in range(len(agb_mods)): header += '%s %s & Frac. Difference & ' % (ratio_str, agb_mods[i].split('_')[-1]) header = header[:-2] + '\\\\ \n \\hline \n' outfile_dir = os.path.split(narratio_files[0])[0] for band, table in zip(['opt', 'ir'], [opt_table, ir_table]): outfile = os.path.join(outfile_dir, '%s_narratio_table.tex' % band) with open(outfile, 'w') as out: out.write(header) np.savetxt(out, table, fmt='%s') return ir_table, opt_table