def test_harmonize_linear_interpolation(): df = _df.copy() hist = _hist.copy() methods = _methods.copy() h = harmonize.Harmonizer(df, hist) methods['method'] = ['linear_interpolate_2060'] * nvals res = h.harmonize(overrides=methods['method']) # base year obs = res['2015'] exp = _hist['2015'] npt.assert_array_almost_equal(obs, exp) # future year x1, x2, x = '2015', '2060', '2040' y1, y2 = _hist[x1], _df[x2] m = (y2 - y1) / (float(x2) - float(x1)) b = y1 - m * float(x1) obs = res[x] exp = m * float(x) + b npt.assert_array_almost_equal(obs, exp) # year after interp obs = res['2060'] exp = _df['2060'] npt.assert_array_almost_equal(obs, exp)
def test_harmonize_reduce_ratio(): df = _df.copy() hist = _hist.copy() methods = _methods.copy() h = harmonize.Harmonizer(df, hist) # this is bad, there should be a test for each case for tf in [2050, 2100, 2150]: print(tf) method = 'reduce_ratio_{}'.format(tf) methods['method'] = [method] * nvals res = h.harmonize(overrides=methods['method']) # base year obs = res['2015'] exp = _hist['2015'] npt.assert_array_almost_equal(obs, exp) # future year obs = res['2040'] ratio = _hist['2015'] / _df['2015'] exp = _df['2040'] * (ratio + _t_frac(tf) * (1 - ratio)) npt.assert_array_almost_equal(obs, exp) # future year if tf < 2060: obs = res['2060'] exp = _df['2060'] npt.assert_array_almost_equal(obs, exp)
def test_harmonize_budget(): df = _df.copy() hist = _hist.copy() methods = _methods.copy() h = harmonize.Harmonizer(df, hist) methods['method'] = 'budget' res = h.harmonize(overrides=methods['method']) # base year obs = res['2015'] exp = _hist['2015'] npt.assert_array_almost_equal(obs, exp) # carbon budget conserved def _carbon_budget(emissions): # trapezoid rule dyears = np.diff(emissions.columns.astype(int)) emissions = emissions.values demissions = np.diff(emissions, axis=1) budget = (dyears * (emissions[:, :-1] + demissions / 2)).sum(axis=1) return budget npt.assert_array_almost_equal( _carbon_budget(res), _carbon_budget(df) - _carbon_budget(hist.loc[:, '2010':'2015']), )
def test_harmonize_constant_offset(): df = _df.copy() hist = _hist.copy() methods = _methods.copy() h = harmonize.Harmonizer(df, hist) res = h.harmonize(overrides=methods['method']) # base year obs = res['2015'] exp = _hist['2015'] npt.assert_array_almost_equal(obs, exp) # future year obs = res['2060'] exp = _df['2060'] + (_hist['2015'] - _df['2015']) npt.assert_array_almost_equal(obs, exp)
def test_harmonize_mix(): df = _df.copy() hist = _hist.copy() methods = _methods.copy() h = harmonize.Harmonizer(df, hist) methods['method'] = ['constant_offset'] * nvals res = h.harmonize(overrides=methods['method']) # base year obs = res['2015'] exp = _hist['2015'] npt.assert_array_almost_equal(obs, exp) # future year obs = res['2060'][:2] exp = [_df['2060'][0] + (_hist['2015'][0] - _df['2015'][0]), _df['2060'][1] * (_hist['2015'][1] / _df['2015'][1])] npt.assert_array_almost_equal(obs, exp)