def tsastat_group_by(tsastat, subkey): """ group given tsastat array by some subkey parameters: tsastat <TimeseriesArrayStats> subkey <tuple> subkey to group by returns: <dict> """ # how to aggregate statistical values group_funcs = { u'count' : lambda a, b: a + b, u'std' : lambda a, b: (a + b)/2, u'avg': lambda a, b: (a + b)/2, u'last' : lambda a, b: -1.0, # theres no meaning u'min' : min, u'max' : max, u'sum' : lambda a, b: (a + b) / 2, u'median' : lambda a, b: (a + b)/2, u'mean' : lambda a, b: (a + b)/2, u'diff' : lambda a, b: (a + b)/2, u'dec' : lambda a, b: (a + b)/2, u'inc' : lambda a, b: (a + b)/2, u'first' : lambda a, b: -1.0, # theres no meaning } # create new empty TimeseriesArrayStats Object tsastats_new = TimeseriesArrayStats.__new__(TimeseriesArrayStats) tsastats_new.index_keys = subkey # only subkey tsastats_new.value_keys = tsastat.value_keys # same oas original newdata = {} for index_key, tsstat in tsastat.items(): key_dict = dict(zip(tsastat.index_keynames, index_key)) newkey = None if len(subkey) == 0: # no subkey means total aggregation newkey = ("__total__", ) else: newkey = tuple([key_dict[key] for key in subkey]) if newkey not in newdata: newdata[newkey] = {} for value_key in tsastat.value_keynames: if value_key not in newdata[newkey]: newdata[newkey][value_key] = dict(tsstat[value_key]) else: for stat_funcname in tsstat[value_key].keys(): existing = float(newdata[newkey][value_key][stat_funcname]) to_group = float(tsstat[value_key][stat_funcname]) newdata[newkey][value_key][stat_funcname] = group_funcs[stat_funcname](existing, to_group) tsastats_new.stats = newdata return tsastats_new