Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created January 20, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taldcroft/12249ad7eeacbec12f44 to your computer and use it in GitHub Desktop.
Save taldcroft/12249ad7eeacbec12f44 to your computer and use it in GitHub Desktop.
Generalized table aggregation example using astropy Table
from __future__ import division, print_function
from astropy import table
from astropy.table import Table
from collections import OrderedDict
t = Table([['a', 'a', 'a', 'b', 'b', 'c'],
[1, 2, 3, 4, 5, 6],
[2, 2, 1, 2, 1, 1]],
names=('name', 'value', 'weight'))
grouped = t.group_by('name')
def transform_table(tbl):
"""
Generalized function that takes table ``tbl`` as input
and returns a new Table ``out``. Note that ``out`` does not
necessarily need to have the same types or columns.
The example is just the identity transform. Be aware that
in-place operations will affect the input table.
"""
out = tbl
return out
out_tables = []
for group in grouped.groups:
out_tables.append(transform_table(group))
result = table.vstack(out_tables)
print('transform_table')
print(result)
print()
def average_weighted(tbl, name):
col = tbl[name]
if name == 'weight':
value = col.sum()
else:
weight = tbl['weight']
value = (col * weight).sum() / weight.sum()
return value
def transform_table_to_row(tbl, func):
"""
Generalized function that takes table ``tbl`` as input
and returns a new table row as an OrderedDict. It applies
function ``func`` to each column.
The example computes the weighted average of each field (where
possible) assuming the weights are in column ``weight``.
"""
out = OrderedDict()
for name in t.colnames:
try:
value = func(tbl, name)
except:
# If something went wrong just ignore (could not perform
# operation on this column).
pass
else:
out[name] = value
return out
out_rows = []
for group in grouped.groups:
out_rows.append(transform_table_to_row(group, average_weighted))
result = Table(rows=out_rows)
print('transform_table_to_row')
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment