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'] == ureg.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'] == 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')
def test_assert_qty(): # tests without `attr` property, in which case direct pd.Series and # xr.DataArray comparisons are possible a = xr.DataArray([0.8, 0.2], coords=[['oil', 'water']], dims=['p']) b = a.to_series() assert_qty_equal(a, b) assert_qty_equal(b, a) assert_qty_allclose(a, b) assert_qty_allclose(b, a) c = Quantity(a) assert_qty_equal(a, c) assert_qty_equal(c, a) assert_qty_allclose(a, c) assert_qty_allclose(c, a)
def test_assert_qty_attrs(): # tests *with* `attr` property, in which case direct pd.Series and # xr.DataArray comparisons *not* are possible a = xr.DataArray([0.8, 0.2], coords=[['oil', 'water']], dims=['p']) attrs = {'foo': 'bar'} a.attrs = attrs b = Quantity(a) # make sure it has the correct property assert a.attrs == attrs assert b.attrs == attrs assert_qty_equal(a, b) assert_qty_equal(b, a) assert_qty_allclose(a, b) assert_qty_allclose(b, a) a.attrs = {'bar': 'foo'} assert_qty_equal(a, b, check_attrs=False)
def test_assert(self, a): """Test assertions about Quantity. These are tests without `attr` property, in which case direct pd.Series and xr.DataArray comparisons are possible. """ # Convert to pd.Series b = a.to_series() assert_qty_equal(a, b) assert_qty_equal(b, a) assert_qty_allclose(a, b) assert_qty_allclose(b, a) c = Quantity(a) assert_qty_equal(a, c) assert_qty_equal(c, a) assert_qty_allclose(a, c) assert_qty_allclose(c, a)
def test_assert_with_attrs(self, a): """Test assertions about Quantity with attrs. Here direct pd.Series and xr.DataArray comparisons are *not* possible. """ attrs = {'foo': 'bar'} a.attrs = attrs b = Quantity(a) # make sure it has the correct property assert a.attrs == attrs assert b.attrs == attrs assert_qty_equal(a, b) assert_qty_equal(b, a) assert_qty_allclose(a, b) assert_qty_allclose(b, a) # check_attrs=False allows a successful equals assertion even when the # attrs are different a.attrs = {'bar': 'foo'} assert_qty_equal(a, b, check_attrs=False)