def test_reporter_from_dantzig(test_mp, test_data_path): scen = make_dantzig(test_mp, solve=test_data_path) # Reporter.from_scenario can handle the Dantzig problem rep = Reporter.from_scenario(scen) # Partial sums are available automatically (d is defined over i and j) d_i = rep.get('d:i') # Units pass through summation assert d_i.attrs['_unit'] == UNITS.parse_units('km') # Summation across all dimensions results a 1-element Quantity d = rep.get('d:') assert len(d) == 1 assert np.isclose(d.iloc[0], 11.7) # Weighted sum weights = Quantity( xr.DataArray([1, 2, 3], coords=['chicago new-york topeka'.split()], dims=['j'])) new_key = rep.aggregate('d:i-j', 'weighted', 'j', weights) # ...produces the expected new key with the summed dimension removed and # tag added assert new_key == 'd:i:weighted' # ...produces the expected new value obs = rep.get(new_key) exp = (rep.get('d:i-j') * weights).sum(dim=['j']) / weights.sum(dim=['j']) # TODO: attrs has to be explicitly copied here because math is done which # returns a pd.Series exp = Quantity(exp, attrs=rep.get('d:i-j').attrs) assert_series_equal(obs.sort_index(), exp.sort_index()) # Disaggregation with explicit data # (cases of canned food 'p'acked in oil or water) shares = xr.DataArray([0.8, 0.2], coords=[['oil', 'water']], dims=['p']) new_key = rep.disaggregate('b:j', 'p', args=[Quantity(shares)]) # ...produces the expected key with new dimension added assert new_key == 'b:j-p' b_jp = rep.get('b:j-p') # Units pass through disaggregation assert b_jp.attrs['_unit'] == 'cases' # Set elements are available assert rep.get('j') == ['new-york', 'chicago', 'topeka'] # 'all' key retrieves all quantities obs = {da.name for da in rep.get('all')} exp = set('a b d f demand demand-margin z x'.split()) assert obs == exp # Shorthand for retrieving a full key name assert rep.full_key('d') == 'd:i-j' and isinstance(rep.full_key('d'), Key)
def test_reporting_units(): """Test handling of units within Reporter computations.""" r = Reporter() # Create some dummy data dims = dict(coords=['a b c'.split()], dims=['x']) r.add('energy:x', Quantity(xr.DataArray([1., 3, 8], attrs={'_unit': 'MJ'}, **dims))) r.add('time', Quantity(xr.DataArray([5., 6, 8], attrs={'_unit': 'hour'}, **dims))) r.add('efficiency', Quantity(xr.DataArray([0.9, 0.8, 0.95], **dims))) # Aggregation preserves units r.add('energy', (computations.sum, 'energy:x', None, ['x'])) assert r.get('energy').attrs['_unit'] == UNITS.parse_units('MJ') # Units are derived for a ratio of two quantities r.add('power', (computations.ratio, 'energy:x', 'time')) assert r.get('power').attrs['_unit'] == UNITS.parse_units('MJ/hour') # Product of dimensioned and dimensionless quantities keeps the former r.add('energy2', (computations.product, 'energy:x', 'efficiency')) assert r.get('energy2').attrs['_unit'] == UNITS.parse_units('MJ')
def test_reporter_add_product(test_mp): scen = ixmp.Scenario(test_mp, 'reporter_add_product', 'reporter_add_product', 'new') *_, x = add_test_data(scen) rep = Reporter.from_scenario(scen) # add_product() works key = rep.add_product('x squared', 'x', 'x', sums=True) # Product has the expected dimensions assert key == 'x squared:t-y' # Product has the expected value exp = as_quantity(x * x) exp.attrs['_unit'] = UNITS('kilogram ** 2').units assert_qty_equal(exp, rep.get(key))