|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""QA functioins. |
| 4 | +""" |
| 5 | + |
| 6 | +import sys |
| 7 | +import logging |
| 8 | +import pandas as pd |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +logger = logging.getLogger('QA') |
| 12 | +this = sys.modules[__name__] |
| 13 | + |
| 14 | + |
| 15 | +def _gen_indicator_key_list(d): |
| 16 | + for k, v in d.items(): |
| 17 | + for i in v: |
| 18 | + yield (k, i) |
| 19 | + |
| 20 | + |
| 21 | +def compare_with_func(dataset1, dataset2, fns=['rval', 'avg_pct_chg'], |
| 22 | + indicators=None, key=None): |
| 23 | + """compare 2 datasets with functions""" |
| 24 | + |
| 25 | + indicators1 = [(k, v) for k, v in _gen_indicator_key_list(dataset1.indicator_dict)] |
| 26 | + indicators2 = [(k, v) for k, v in _gen_indicator_key_list(dataset2.indicator_dict)] |
| 27 | + |
| 28 | + # check availability for indicators |
| 29 | + s1 = set(indicators1) |
| 30 | + s2 = set(indicators2) |
| 31 | + |
| 32 | + diff12 = s1 - s2 |
| 33 | + diff21 = s2 - s1 |
| 34 | + |
| 35 | + if len(diff12) > 0: |
| 36 | + msg = ["below indicators are noly available in {}".format(dataset1.ddf_id)] |
| 37 | + for item in diff12: |
| 38 | + msg.append("- {} by {}".format(item[0], ', '.join(item[1]))) |
| 39 | + msg.append('') |
| 40 | + logger.warning('\n'.join(msg)) |
| 41 | + if len(diff21) > 0: |
| 42 | + msg = ["below indicators are noly available in {}".format(dataset2.ddf_id)] |
| 43 | + for item in diff21: |
| 44 | + msg.append("- {} by {}".format(item[0], ', '.join(item[1]))) |
| 45 | + msg.append('') |
| 46 | + logger.warning('\n'.join(msg)) |
| 47 | + |
| 48 | + # construct a dataframe, including all indicators in both dataset. |
| 49 | + result = pd.DataFrame(list(s1.union(s2)), columns=['indicator', 'primary_key']) |
| 50 | + |
| 51 | + def get_comp_df(indicator, k): |
| 52 | + '''get dataframes from old and new datasets, and combine them into one dataframe''' |
| 53 | + # FIXME: support multiple indicator in one file |
| 54 | + # like the indicators in ddf--sodertorn--stockholm_lan_basomrade |
| 55 | + try: |
| 56 | + i1 = dataset1.get_datapoint_df(indicator, k) |
| 57 | + except KeyError: |
| 58 | + raise |
| 59 | + try: |
| 60 | + i2 = dataset2.get_datapoint_df(indicator, k) |
| 61 | + except KeyError: |
| 62 | + raise |
| 63 | + i1 = i1.rename(columns={indicator: 'old'}) |
| 64 | + i2 = i2.rename(columns={indicator: 'new'}) |
| 65 | + comp = pd.concat([i1, i2], axis=1) |
| 66 | + |
| 67 | + return comp |
| 68 | + |
| 69 | + def do_compare(fns, indicator, k): |
| 70 | + try: |
| 71 | + comp_df = get_comp_df(indicator, k) |
| 72 | + except KeyError: |
| 73 | + return [np.nan] * len(fns) |
| 74 | + |
| 75 | + return [f(comp_df) if callable(f) else getattr(this, f)(comp_df) |
| 76 | + for f in fns] |
| 77 | + |
| 78 | + # only keep indicators we want to compare |
| 79 | + if indicators: |
| 80 | + result = result[result.indicator.isin(indicators)] |
| 81 | + if key: |
| 82 | + result = result[result.primary_key.isin(key)] |
| 83 | + |
| 84 | + # append new columns before we do calculation |
| 85 | + for f in fns: |
| 86 | + result[f] = np.nan |
| 87 | + |
| 88 | + result = result.set_index(['indicator', 'primary_key']) |
| 89 | + |
| 90 | + for i in result.index: |
| 91 | + result.ix[i, fns] = do_compare(fns, i[0], i[1]) |
| 92 | + |
| 93 | + return result.reset_index() |
| 94 | + |
| 95 | + |
| 96 | +def rval(comp_df): |
| 97 | + return comp_df.corr().ix['old', 'new'] |
| 98 | + |
| 99 | + |
| 100 | +def avg_pct_chg(comp_df): |
| 101 | + res = (comp_df['new'] - comp_df['old']) / comp_df['old'] * 100 |
| 102 | + return res.replace([np.inf, -np.inf], np.nan).mean() |
0 commit comments