Exemplo n.º 1
0
def test_units(ureg):
    """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', as_quantity(xr.DataArray([1., 3, 8], **dims),
                                  units='MJ'))
    r.add('time', as_quantity(xr.DataArray([5., 6, 8], **dims), units='hour'))
    r.add('efficiency', as_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'] == ureg.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'] == ureg.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'] == ureg.parse_units('MJ')
Exemplo n.º 2
0
def test_reporter_add_product(test_mp, ureg):
    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'] = ureg('kilogram ** 2').units
    assert_qty_equal(exp, rep.get(key))
Exemplo n.º 3
0
def test_aggregate(test_mp):
    scen = ixmp.Scenario(test_mp, 'Group reporting', 'group reporting', 'new')
    t, t_foo, t_bar, x = add_test_data(scen)

    # Reporter
    rep = Reporter.from_scenario(scen)

    # Define some groups
    t_groups = {'foo': t_foo, 'bar': t_bar, 'baz': ['foo1', 'bar5', 'bar6']}

    # Use the computation directly
    agg1 = computations.aggregate(as_quantity(x), {'t': t_groups}, True)

    # Expected set of keys along the aggregated dimension
    assert set(agg1.coords['t'].values) == set(t) | set(t_groups.keys())

    # Sums are as expected
    assert_qty_allclose(agg1.sel(t='foo', drop=True), x.sel(t=t_foo).sum('t'))
    assert_qty_allclose(agg1.sel(t='bar', drop=True), x.sel(t=t_bar).sum('t'))
    assert_qty_allclose(agg1.sel(t='baz', drop=True),
                        x.sel(t=['foo1', 'bar5', 'bar6']).sum('t'))

    # Use Reporter convenience method
    key2 = rep.aggregate('x:t-y', 'agg2', {'t': t_groups}, keep=True)

    # Group has expected key and contents
    assert key2 == 'x:t-y:agg2'

    # Aggregate is computed without error
    agg2 = rep.get(key2)

    assert_qty_equal(agg1, agg2)

    # Add aggregates, without keeping originals
    key3 = rep.aggregate('x:t-y', 'agg3', {'t': t_groups}, keep=False)

    # Distinct keys
    assert key3 != key2

    # Only the aggregated and no original keys along the aggregated dimension
    agg3 = rep.get(key3)
    assert set(agg3.coords['t'].values) == set(t_groups.keys())

    with pytest.raises(NotImplementedError):
        # Not yet supported; requires two separate operations
        rep.aggregate('x:t-y', 'agg3', {'t': t_groups, 'y': [2000, 2010]})
Exemplo n.º 4
0
def test_file_formats(test_data_path, tmp_path):
    r = Reporter()

    expected = as_quantity(pd.read_csv(test_data_path / 'report-input0.csv',
                                       index_col=['i', 'j'])['value'],
                           units='km')

    # CSV file is automatically parsed to xr.DataArray
    p1 = test_data_path / 'report-input0.csv'
    k = r.add_file(p1, units=pint.Unit('km'))
    assert_qty_equal(r.get(k), expected)

    # Dimensions can be specified
    p2 = test_data_path / 'report-input1.csv'
    k2 = r.add_file(p2, dims=dict(i='i', j_dim='j'))
    assert_qty_equal(r.get(k), r.get(k2))

    # Units are loaded from a column
    assert r.get(k2).attrs['_unit'] == pint.Unit('km')

    # Specifying units that do not match file contents → ComputationError
    r.add_file(p2, key='bad', dims=dict(i='i', j_dim='j'), units='kg')
    with pytest.raises(ComputationError):
        r.get('bad')

    # Write to CSV
    p3 = tmp_path / 'report-output.csv'
    r.write(k, p3)

    # Output is identical to input file, except for order
    assert (sorted(p1.read_text().split('\n')) == sorted(
        p3.read_text().split('\n')))

    # Write to Excel
    p4 = tmp_path / 'report-output.xlsx'
    r.write(k, p4)
Exemplo n.º 5
0
def test_reporter_from_dantzig(test_mp, ureg):
    scen = make_dantzig(test_mp, solve=True)

    # 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'] == ureg.parse_units('km')

    # Summation across all dimensions results a 1-element Quantity
    d = rep.get('d:')
    assert d.shape == ((1, ) if Quantity is AttrSeries else tuple())
    assert d.size == 1
    assert np.isclose(d.values, 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)
    d_ij = rep.get('d:i-j')
    exp = (d_ij * weights).sum(dim=['j']) / weights.sum(dim=['j'])
    # FIXME attrs has to be explicitly copied here because math is done which
    #       returns a pd.Series
    exp.attrs = d_ij.attrs

    assert_qty_equal(exp, obs)

    # 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=[as_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 x z cost cost-margin demand demand-margin supply '
               'supply-margin').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)