Skip to content

Instantly share code, notes, and snippets.

@PBarmby
Created January 5, 2015 00:28
Show Gist options
  • Save PBarmby/e4384a81cf2e1c53365c to your computer and use it in GitHub Desktop.
Save PBarmby/e4384a81cf2e1c53365c to your computer and use it in GitHub Desktop.
Deals with making a LaTeX table with uncertainties and correct number of significant figures
from astropy.table import Table, Column, join, vstack
from uncertainties import ufloat
import astropy.units as u
def uncert_str(tab_row, col_name, value_fmt):
''' format tab_row[col_name] single table row in LaTeX $num \pm unc$ format
input: tab_row: single row from an astropy table
col_name: name of column to be extracted and formatted
value_fmt: format for string output
output:
string with correctly formatted number
'''
if col_name+'_pe' in tab_row.colnames and col_name+'_me' in tab_row.colnames: # two-sided uncertainties
formatter_str = '$' + value_fmt + '^{+' + value_fmt +'}_{-' + value_fmt+ '}$'
final_str = formatter_str % (tab_row[col_name], tab_row[col_name+'_pe'], tab_row[col_name+'_me'])
elif col_name+'_unc' in tab_row.colnames: # one-sided uncertainties
if np.isnan(tab_row[col_name+'_unc']): # either an upper limit or no data
if np.isnan(tab_row[col_name]) or tab_row[col_name]<upperlim_tol : # no data
final_str = '\\dots'
else: # upper limit
formatter_str = '$<' + value_fmt +'$'
final_str = formatter_str % (tab_row[col_name])
else: # regular one-sided uncertainty
formatter_str = '$' + value_fmt + '\\pm' + value_fmt +'$'
# final_str = formatter_str % (tab_row[col_name], tab_row[col_name+'_unc'])
final_str = unc_fmt.format(ufloat(tab_row[col_name], tab_row[col_name+'_unc']))
else: # no uncerts
if 's' in value_fmt: # it's a string, do some replacements
newval = string.replace(tab_row[col_name],'_','\\_')
if 'Ref' in col_name:
formatter_str = '\\citet{%s}'
newval = string.strip(newval)
else:
formatter_str = '' + value_fmt +''
else: # not a string, assume it's a number
formatter_str = '$' + value_fmt +'$'
newval = tab_row[col_name]
final_str = formatter_str % (newval)
return(final_str)
def make_latex_table_rows(intab, col_list, outfile, col_sfx='', col_sfx_start=1):
'''
make LaTeX table rows, with number +/- uncertainty correctly formatted in a single column
input:
intab: astropy table
col_list: list of table column names to be output. Corresponding uncertainty columns are
not listed here, are assumed to be name_unc or name_pe, name_me
outfile: output file name
col_sfx: optional suffix to add to column names
col_sfx_start: index in col_list at which to start adding col_sfx
output:
file with LaTeX table rows (does not include table header code)
'''
outf = open(outfile,'w') # overwrites input
# keep track of what columns are in the output
formatted_line = '% '
for j in range (0,len(col_list)):
if j < col_sfx_start:
formatted_line += ' %s ' % col_list[j][0]
else:
formatted_line += ' %s ' % (col_list[j][0]+col_sfx) # column names
outf.write(formatted_line + '\n')
# and what their units are
formatted_line = '% '
for j in range (0,len(col_list)): # skip the first entry since that's a name
if j < col_sfx_start:
formatted_line += ' None '
else:
formatted_line += ' %s ' % intab[col_list[j][0]+col_sfx].unit.to_string().replace(' ','') # column units with no whitespace
outf.write(formatted_line + '\n')
# now write the individual rows
for i in range(0,len(intab)):
formatted_line = ''
for j in range (0,len(col_list)):
if j < col_sfx_start:
formatted_line += uncert_str(intab[i],col_list[j][0],col_list[j][1]) + ' & ' # ID entry
else:
formatted_line += uncert_str(intab[i],col_list[j][0]+col_sfx,col_list[j][1]) # one column entry
if j<len(col_list)-1: formatted_line += ' & ' # column separator
formatted_line +='\\\\\n' # end-of-line marker
outf.write(formatted_line)
outf.close()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment