def test_invalid_discards(): s = """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region a, 10000, , b, 10000, , c, 10000, , d, 10000, , e, 10000, , f, 10000, , g, 10000, , h, 10000, , i, 10000, , j, 10000, , k, 10000, , l, 10000, , m, 10000, , n, 10000, , o, 10000, , p, 10000000000, , q, 1, , """ f = io.StringIO(s) v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0) result = v.avg_high_low() expected = (10000, 10000, 10000 ) # The 10,000,000,000 and 1 values should be discarded. assert result == pytest.approx(expected) f = io.StringIO(s) v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0, stat_correction=False) result = v.avg_high_low() assert result != pytest.approx(expected)
def test_source_data(): s = """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region Check that this text is present, 0%, %, 0, 0 """ f = io.StringIO(s) v = vma.VMA(filename=f) assert 'Check that this text is present' in v.source_data.to_html()
def test_no_filename(): v = vma.VMA(filename=None) assert v.df.empty (mean, high, low) = v.avg_high_low() assert pd.isna(mean) assert pd.isna(high) assert pd.isna(low)
def test_vals_from_real_soln(): """ Values from Silvopasture Variable Meta Analysis """ f = datadir.joinpath('vma1_silvopasture.csv') v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0) result = v.avg_high_low() expected = (314.15, 450.0, 178.3) assert result == pytest.approx(expected)
def test_no_discards_if_weights(): """Same test as test_invalid_discards but with weights, so there should be no discards.""" s = """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region a, 10000, , , 1.0, b, 10000, , , 1.0, c, 10000, , , 1.0, d, 10000, , , 1.0, e, 10000, , , 1.0, f, 10000, , , 1.0, g, 10000, , , 1.0, h, 10000, , , 1.0, i, 10000, , , 1.0, j, 10000, , , 1.0, k, 10000, , , 1.0, l, 10000, , , 1.0, m, 10000, , , 1.0, n, 10000, , , 1.0, o, 10000, , , 1.0, p, 10000000000, , , 1.0, q, 1, , , 1.0, """ f = io.StringIO(s) v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0, use_weight=True) result = v.avg_high_low() expected = (10000, 10000, 10000) assert result != pytest.approx(expected)
def test_substitute_vma_regional_statistics(): vals = { 'World': 0, 'OECD90': 1, 'Eastern Europe': 2, 'Asia (Sans Japan)': 3, 'Middle East and Africa': 4, 'Latin America': 5, 'China': 0, 'India': 0, 'EU': 0, 'USA': 0 } with mock.patch.object(vma.VMA, '__init__', new=lambda *args, **kwargs: None): with mock.patch.object( vma.VMA, 'avg_high_low', new=lambda *args, **kwargs: vals[kwargs['region']]): ac = advanced_controls.AdvancedControls( vmas={ 'SOLUTION First Cost per Implementation Unit': vma.VMA() }, pds_2014_cost='mean per region') expected = pd.Series(data=vals, name='regional values') pd.testing.assert_series_equal(expected, ac.pds_2014_cost)
def test_substitute_vma(): with mock.patch('model.vma.VMA') as MockVMA: MockVMA.return_value.avg_high_low.return_value = 'expected return' seq_vma = vma.VMA() ac = advanced_controls.AdvancedControls( vmas={'Sequestration Rates': seq_vma}, seq_rate_global='mean') assert ac.seq_rate_global == 'expected return'
def test_spelling_correction(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 1.0, Mha,, 0.0, False,, Asia (sans Japan) B, 1.0, Mha,, 0.0, False,, Middle East & Africa """) v = vma.VMA(filename=f) assert v.df.loc[0, 'Region'] == 'Asia (Sans Japan)' assert v.df.loc[1, 'Region'] == 'Middle East and Africa'
def test_avg_high_low_key(): f = datadir.joinpath('vma1_silvopasture.csv') v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0) avg = v.avg_high_low(key='mean') assert avg == pytest.approx(314.15) low = v.avg_high_low(key='low') assert low == pytest.approx(178.3) with pytest.raises(ValueError): v.avg_high_low(key='not a key')
def test_missing_columns(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 1000 """) v = vma.VMA(filename=f) result = v.avg_high_low() expected = (1000, 1000, 1000) assert result == pytest.approx(expected)
def test_single_study(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 39%, %, """) v = vma.VMA(filename=f) result = v.avg_high_low() expected = (0.39, 0.39, 0.39) assert result == pytest.approx(expected)
def test_substitute_vma_handles_raw_value_discrepancy(): with mock.patch('model.vma.VMA') as MockVMA: MockVMA.return_value.avg_high_low.return_value = 1.2 ac = advanced_controls.AdvancedControls( vmas={'Sequestration Rates': vma.VMA()}, seq_rate_global={ 'value': 1.1, 'statistic': 'mean' }) assert ac.seq_rate_global == 1.1
def test_inverse(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 43%, %, """) postprocess = lambda x, y, z: (1.0 - x, 1.0 - y, 1.0 - z) v = vma.VMA(filename=f, postprocess=postprocess) result = v.avg_high_low() expected = (0.57, 0.57, 0.57) assert result == pytest.approx(expected)
def test_vals(self, filetype, title, expected): """ Checks known values from Silvopasture Variable Meta Analysis, taken from the xlsx file. """ v = vma.VMA(filename=datadir.joinpath(f'silvopasture.{filetype}'), title=title, low_sd=1.0, high_sd=1.0) assert v.avg_high_low() == pytest.approx(expected)
def test_excluded_data(): s = """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region a, 10000, , , 1.0, False b, 10000, , , 1.0, False c, 40000, , , 1.0, True """ f = io.StringIO(s) v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0) result = v.avg_high_low() expected = (10000, 10000, 10000) # The 40000 value should be excluded. assert result == pytest.approx(expected)
def test_no_warnings_in_avg_high_low(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 1.0, Mha,, 0.0, False B, 1.0, Mha,, 0.0, False C, 1.0, Mha,, 0.0, False """) with pytest.warns(None) as warnings: v = vma.VMA(filename=f) _ = v.avg_high_low() assert len(warnings) == 0
def test_populate_fixed_summary(): VMAs = { 'Testing Fixed Summary': vma.VMA(filename=datadir.joinpath("vma1_silvopasture.csv"), use_weight=False), } vma.populate_fixed_summaries( vma_dict=VMAs, filename=datadir.joinpath('VMA_info_w_summary.csv')) v = VMAs['Testing Fixed Summary'] (avg, high, low) = v.avg_high_low() assert (avg, high, low) == (2.0, 3.0, 1.0)
def test_empty_vmas(self, filetype, title): """ Check that existing titles with no data raise an error. Titles taken from the test xlsx file. """ filename = f'silvopasture.{filetype}' with pytest.raises(ValueError) as error: vma.VMA(filename=datadir.joinpath(filename), title=title) assert filename in error.exconly() assert title in error.exconly() assert 'is that VMA empty?' in error.exconly()
def test_avg_high_low_by_region(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 0.4, Mha,, 1.0, False, Temperate/Boreal-Humid, OECD90 B, 0.5, Mha,, 1.0, False, Temperate/Boreal-Humid, OECD90 C, 0.6, Mha,, 1.0, False, Tropical-Humid, Latin America """) v = vma.VMA(filename=f) result = v.avg_high_low() assert result[0] == pytest.approx(0.5) result = v.avg_high_low(region='OECD90') assert result[0] == pytest.approx(0.45)
def test_write_to_file(): f = tempfile.NamedTemporaryFile(mode='w') f.write( r"""Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 1.0,,,, B, 1.0,,,, C, 1.0,,,, """) f.flush() v = vma.VMA(filename=f.name) df = v.source_data.copy(deep=True) df.loc[0, 'Source ID'] = 'updated source ID' v.write_to_file(df) assert 'updated source ID' in open(f.name).read()
def test_excluded_data_weights_are_incorrect(): s = """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region a, 10000, , , 0.5, False b, 10000, , , 0.5, False c, 40000, , , 1.0, True """ f = io.StringIO(s) v = vma.VMA(filename=f, low_sd=1.0, high_sd=1.0, use_weight=True) result = v.avg_high_low() # The 40000 value should be excluded, but its weight is included in the Excel implementation. # Expected value generated by entering the datapoints from this test into Excel. expected = (5000, 9330.127, 669.873) # When https://docs.google.com/document/d/19sq88J_PXY-y_EnqbSJDl0v9CdJArOdFLatNNUFhjEA/edit#heading=h.qkdzs364y2t2 # handling is removed this test will fail. It should be removed at that point, as it no longer # serves a purpose. assert result == pytest.approx(expected)
def test_categorical_validation(): f = io.StringIO( """Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region A, 1.0, Mha,, 0.0, False, Global Arid, Invalid Region B, 1.0, Mha,, 0.0, False,, USA C, 1.0, Mha,, 0.0, False, Invalid TMR, China """) v = vma.VMA(filename=f) assert pd.isna(v.df.loc[0, 'Region']) assert v.df.loc[0, 'TMR'] == 'Global Arid' assert v.df.loc[1, 'Region'] == 'USA' assert v.df.loc[1, 'TMR'] == '' assert v.df.loc[2, 'Region'] == 'China' assert v.df.loc[2, 'TMR'] == '' assert pd.isna(v.source_data.loc[0, 'World / Drawdown Region']) assert pd.isna(v.source_data.loc[2, 'Thermal-Moisture Regime'])
def test_reload_from_file(): f = tempfile.NamedTemporaryFile(mode='w') f.write( r"""Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region original source ID, 1.0,,,, """) f.flush() v = vma.VMA(filename=f.name) df = v.source_data.copy(deep=True) assert df.loc[0, 'Source ID'] == 'original source ID' f.seek(0) f.write( r"""Source ID, Raw Data Input, Original Units, Conversion calculation, Weight, Exclude Data?, Thermal-Moisture Regime, World / Drawdown Region updated source ID, 1.0,,,, """) f.flush() v.reload_from_file() df = v.source_data.copy(deep=True) assert df.loc[0, 'Source ID'] == 'updated source ID'
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tam from solution import rrs DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA(filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA(filename=THISDIR.joinpath( "vma_data", "CONVENTIONAL_First_Cost_per_Implementation_Unit.csv"), use_weight=True), 'SOLUTION First Cost per Implementation Unit': vma.VMA(filename=THISDIR.joinpath( "vma_data", "SOLUTION_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'CONVENTIONAL Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Lifetime_Capacity.csv"), use_weight=True), 'SOLUTION Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "SOLUTION_Lifetime_Capacity.csv"),
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tam from solution import rrs DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA(filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA(filename=DATADIR.joinpath( *('energy', 'vma_CONVENTIONAL_First_Cost_per_Implementation_Unit_1.csv')), use_weight=True), 'SOLUTION First Cost per Implementation Unit': vma.VMA(filename=THISDIR.joinpath( "vma_data", "SOLUTION_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'CONVENTIONAL Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Lifetime_Capacity.csv"), use_weight=True), 'SOLUTION Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data",
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tam from solution import rrs DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA( filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA( filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'SOLUTION First Cost per Implementation Unit': vma.VMA( filename=THISDIR.joinpath("vma_data", "SOLUTION_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'CONVENTIONAL Lifetime Capacity': vma.VMA( filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Lifetime_Capacity.csv"), use_weight=False), 'SOLUTION Lifetime Capacity': vma.VMA( filename=THISDIR.joinpath("vma_data", "SOLUTION_Lifetime_Capacity.csv"), use_weight=False), 'CONVENTIONAL Average Annual Use': vma.VMA( filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Average_Annual_Use.csv"),
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tam from solution import rrs DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA(filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA(filename=DATADIR.joinpath( *('energy', 'vma_CONVENTIONAL_First_Cost_per_Implementation_Unit.csv')), use_weight=True), 'SOLUTION First Cost per Implementation Unit': vma.VMA(filename=THISDIR.joinpath( "vma_data", "SOLUTION_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'CONVENTIONAL Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Lifetime_Capacity.csv"), use_weight=True), 'SOLUTION Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data",
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tla from solution import land DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA(filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA(filename=None, use_weight=False), 'SOLUTION First Cost per Implementation Unit': vma.VMA(filename=None, use_weight=False), 'CONVENTIONAL Operating Cost per Functional Unit per Annum': vma.VMA(filename=None, use_weight=False), 'SOLUTION Operating Cost per Functional Unit per Annum': vma.VMA(filename=None, use_weight=False), 'Yield from CONVENTIONAL Practice': vma.VMA(filename=None, use_weight=False), 'Yield Gain (% Increase from CONVENTIONAL to SOLUTION)': vma.VMA(filename=None, use_weight=False), 'Average Electricty Used DEGRADED LAND': vma.VMA(filename=None, use_weight=False), 'Energy Efficiency Factor UNDEGRADED LAND':
from model import dd from model import emissionsfactors from model import firstcost from model import helpertables from model import operatingcost from model import s_curve from model import unitadoption from model import vma from model import tam from solution import rrs DATADIR = pathlib.Path(__file__).parents[2].joinpath('data') THISDIR = pathlib.Path(__file__).parents[0] VMAs = { 'Current Adoption': vma.VMA(filename=THISDIR.joinpath("vma_data", "Current_Adoption.csv"), use_weight=False), 'CONVENTIONAL First Cost per Implementation Unit': vma.VMA(filename=None, use_weight=False), 'SOLUTION First Cost per Implementation Unit': vma.VMA(filename=THISDIR.joinpath( "vma_data", "SOLUTION_First_Cost_per_Implementation_Unit.csv"), use_weight=False), 'CONVENTIONAL Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "CONVENTIONAL_Lifetime_Capacity.csv"), use_weight=False), 'SOLUTION Lifetime Capacity': vma.VMA(filename=THISDIR.joinpath("vma_data", "SOLUTION_Lifetime_Capacity.csv"), use_weight=False), 'CONVENTIONAL Average Annual Use':