def test_aggregate_region_with_other_method(simple_df, variable, data): # use other method (max) both as string and passing the function _df = data.copy() if simple_df.time_col == 'time': _df.year = _df.year.replace(DTS_MAPPING) _df.rename({'year': 'time'}, axis='columns', inplace=True) exp = IamDataFrame(_df).filter(region='World') for m in ['max', np.max]: assert_frame_equal(simple_df.aggregate_region(variable, method=m), exp)
def test_aggregate_region(simple_df, variable): # check that `variable` is a a direct sum across regions exp = simple_df.filter(variable=variable, region='World') assert_frame_equal(simple_df.aggregate_region(variable), exp) # check custom `region` (will include `World`, so double-count values) foo = exp.rename(region={'World': 'foo'}) foo.data.value = foo.data.value * 2 assert_frame_equal(simple_df.aggregate_region(variable, region='foo'), foo)
def test_aggregate(simple_df, variable, data): # check that `variable` is a a direct sum and matches given total exp = simple_df.filter(variable=variable) assert_frame_equal(simple_df.aggregate(variable), exp) # use other method (max) both as string and passing the function _df = data.copy() if simple_df.time_col == 'time': _df.year = _df.year.replace(DTS_MAPPING) _df.rename({'year': 'time'}, axis='columns', inplace=True) exp = IamDataFrame(_df) for m in ['max', np.max]: assert_frame_equal(simple_df.aggregate(variable, method=m), exp)
def test_downscale_region(simple_df, variable): simple_df.set_meta([1], name='test') regions = ['reg_a', 'reg_b'] # return as new IamDataFrame obs = simple_df.downscale_region(variable, proxy='Population') exp = simple_df.filter(variable=variable, region=regions) assert_frame_equal(exp, obs) # append to `self` (after removing to-be-downscaled timeseries) inplace = simple_df.filter(variable=variable, region=regions, keep=False) inplace.downscale_region(variable, proxy='Population', append=True) assert_frame_equal(inplace, simple_df)
def test_aggregate_region_with_weights(simple_df): # carbon price shouldn't be summed but be weighted by emissions v = 'Price|Carbon' w = 'Emissions|CO2' assert simple_df.check_aggregate_region(v) is not None assert simple_df.check_aggregate_region(v, weight=w) is None exp = simple_df.filter(variable=v, region='World') assert_frame_equal(simple_df.aggregate_region(v, weight=w), exp) # inconsistent index of variable and weight raises an error _df = simple_df.filter(variable=w, region='reg_b', keep=False) pytest.raises(ValueError, _df.aggregate_region, v, weight=w) # using weight and method other than 'sum' raises an error pytest.raises(ValueError, simple_df.aggregate_region, v, method='max', weight='bar')
def test_aggregate_region_with_subregions(simple_df, variable): # check that custom `subregions` works (assumes only `reg_a` is in `World`) exp = ( simple_df.filter(variable=variable, region='reg_a') .rename(region={'reg_a': 'World'}) ) obs = simple_df.aggregate_region(variable, subregions='reg_a') assert_frame_equal(obs, exp) # check that both custom `region` and `subregions` work foo = exp.rename(region={'World': 'foo'}) obs = simple_df.aggregate_region(variable, region='foo', subregions='reg_a') assert_frame_equal(obs, foo) # check that invalid list of subregions returns empty assert simple_df.aggregate_region(variable, subregions=['reg_c']).empty
def test_aggregate_region_append(simple_df, variable): # remove `variable`, do aggregate and append, check equality to original _df = simple_df.filter(variable=variable, region='World', keep=False) _df.aggregate_region(variable, append=True) assert_frame_equal(_df, simple_df)