def test_us_age(self): input = { "country/USA": [ "Count_Person_Upto4Years", "Count_Person_5To9Years", "Count_Person_10To14Years", "Count_Person_15To19Years", "Count_Person_20To24Years", "Count_Person_25To29Years", "Count_Person_30To34Years", "Count_Person_35To39Years", "Count_Person_40To44Years", "Count_Person_45To49Years", "Count_Person_50To54Years", "Count_Person_55To59Years", "Count_Person_60To64Years", "Count_Person_65To69Years", "Count_Person_70To74Years", "Count_Person_75To79Years", "Count_Person_80OrMoreYears", "Count_Person_5To17Years", "Count_Person_18To24Years", "Count_Person_25To34Years", "Count_Person_35To44Years", "Count_Person_45To54Years", "Count_Person_55To59Years", "Count_Person_60To61Years", "Count_Person_62To64Years", "Count_Person_65To74Years", "Count_Person_65OrMoreYears", "Count_Person_75OrMoreYears" ] } expected = { 'country/USA': { 'Count_Person_5To17Years': ['Count_Person_5To17Years'], 'Count_Person_18To24Years': ['Count_Person_18To24Years'], 'Count_Person_25To34Years': ['Count_Person_25To34Years'], 'Count_Person_35To44Years': ['Count_Person_35To44Years'], 'Count_Person_45To54Years': ['Count_Person_45To54Years'], 'Count_Person_55To64Years': [ 'Count_Person_55To59Years', 'Count_Person_60To61Years', 'Count_Person_62To64Years' ], 'Count_Person_65To74Years': ['Count_Person_65To74Years'], 'Count_Person_75OrMoreYears': ['Count_Person_75OrMoreYears'] } } assert lib_range.aggregate_stat_var(input, lib_range.AGE) == expected
def get_stat_var_group(cc, data, places): """Get the stat var grouping for aggregation.""" if 'aggregate' in cc: agg_type = lib_range.get_aggregate_config(cc['aggregate']) place_stat_vars = defaultdict(list) for place in places: if place not in data or 'data' not in data[place]: continue for sv in cc['statsVars']: if sv in data[place]['data']: place_stat_vars[place].append(sv) result = lib_range.aggregate_stat_var(place_stat_vars, agg_type) for place in places: if place not in result: result[place] = {} else: result = {} for place in places: result[place] = {sv: [sv] for sv in cc['statsVars']} return result