def test_adoption_low_med_high_with_zero(): data_sources = { 'Baseline Cases': { 'zero': str(datadir.joinpath('ad_all_zero.csv')), 'one': str(datadir.joinpath('ad_all_one.csv')), }, 'Conservative Cases': {}, 'Ambitious Cases': {}, '100% RES2050 Case': {}, } ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='ALL SOURCES', soln_pds_adoption_prognostication_growth='Medium') ad = adoptiondata.AdoptionData(ac=ac, data_sources=data_sources, adconfig=g_adconfig) result = ad.adoption_low_med_high(region='World') # Zero should be dropped for the mean, to match Excel behavior. assert all(result.loc[:, 'Medium'] == 1.0) # With a single source, zero should not be dropped. ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='zero', soln_pds_adoption_prognostication_growth='Medium') ad = adoptiondata.AdoptionData(ac=ac, data_sources=data_sources, adconfig=g_adconfig) result = ad.adoption_low_med_high(region='World') expected = pd.read_csv(str(datadir.joinpath('ad_all_zero.csv')), index_col=0) pd.testing.assert_series_equal(result.loc[:, 'Medium'], expected.loc[:, 'World'], check_names=False, check_exact=True)
def test_soln_ref_adoption_args(): ac = advanced_controls.AdvancedControls(soln_ref_adoption_basis="Default") assert ac.soln_ref_adoption_basis == "Default" ac = advanced_controls.AdvancedControls(soln_ref_adoption_basis="Custom") assert ac.soln_ref_adoption_basis == "Custom" with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls(soln_ref_adoption_basis="???")
def test_regional_data_sources(): data_sources = { 'Baseline Cases': { '10': str(datadir.joinpath('ad_region_constant10.csv')), '20': str(datadir.joinpath('ad_region_constant20.csv')), }, 'Region: OECD90': { 'Baseline Cases': { '10': str(datadir.joinpath('ad_region_constant10.csv')), }, }, } ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='ALL SOURCES', soln_pds_adoption_prognostication_growth='Medium') ad = adoptiondata.AdoptionData(ac=ac, data_sources=data_sources, adconfig=g_adconfig) result = ad.adoption_trend_per_region() assert result.loc[2019, 'World'] == pytest.approx(15.0) assert result.loc[2019, 'OECD90'] == pytest.approx(10.0) assert result.loc[2019, 'Latin America'] == pytest.approx(15.0) ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='Baseline Cases', soln_pds_adoption_prognostication_growth='Medium') ad = adoptiondata.AdoptionData(ac=ac, data_sources=data_sources, adconfig=g_adconfig) result = ad.adoption_trend_per_region() assert result.loc[2019, 'World'] == pytest.approx(15.0) assert result.loc[2019, 'OECD90'] == pytest.approx(10.0)
def test_pds_ref_use_years(): ac = advanced_controls.AdvancedControls(ref_adoption_use_pds_years=[2014], pds_adoption_use_ref_years=[2015]) with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls( ref_adoption_use_pds_years=[2014], pds_adoption_use_ref_years=[2014])
def test_adoption_is_single_source(): s = 'Greenpeace AER' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) assert ad.adoption_is_single_source() == True s = 'ALL SOURCES' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) assert ad.adoption_is_single_source() == False s = 'Ambitious Cases' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) assert ad.adoption_is_single_source() == False s = 'No such name' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) with pytest.raises(ValueError): _ = ad.adoption_is_single_source()
def test_co2eq_conversion_source(): ac = advanced_controls.AdvancedControls( co2eq_conversion_source="ar5 with feedback") assert ac.co2eq_conversion_source == ef.CO2EQ_SOURCE.AR5_WITH_FEEDBACK ac = advanced_controls.AdvancedControls( co2eq_conversion_source=ef.CO2EQ_SOURCE.AR4) assert ac.co2eq_conversion_source == ef.CO2EQ_SOURCE.AR4 with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls(co2eq_conversion_source="???")
def test_has_var_costs(): ac = advanced_controls.AdvancedControls(soln_var_oper_cost_per_funit=0.0, soln_fuel_cost_per_funit=0.0, conv_var_oper_cost_per_funit=0.0, conv_fuel_cost_per_funit=0.0) assert ac.has_var_costs ac = advanced_controls.AdvancedControls(soln_var_oper_cost_per_funit=0.0, soln_fuel_cost_per_funit=0.0, conv_var_oper_cost_per_funit=0.0) assert not ac.has_var_costs
def test_substitute_vma_passthru_value(): ac = advanced_controls.AdvancedControls(seq_rate_global=4.3) assert ac.seq_rate_global == 4.3 class fakeVMA: def avg_high_low(self, key): return (0.0, 0.0, 0.0) vmas = {'Sequestration Rates': fakeVMA()} ac = advanced_controls.AdvancedControls(vmas=vmas, seq_rate_global={'value': 4.3}) assert ac.seq_rate_global == 4.3
def test_emissions_grid(): ac = advanced_controls.AdvancedControls(emissions_grid_source="IPCC Only", emissions_grid_range="high") assert ac.emissions_grid_source == ef.GRID_SOURCE.IPCC assert ac.emissions_grid_range == ef.GRID_RANGE.HIGH ac = advanced_controls.AdvancedControls( emissions_grid_source=ef.GRID_SOURCE.META, emissions_grid_range=ef.GRID_RANGE.MEAN) assert ac.emissions_grid_source == ef.GRID_SOURCE.META assert ac.emissions_grid_range == ef.GRID_RANGE.MEAN with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls(emissions_grid_source="???") with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls(emissions_grid_range="???")
def test_CSP_World(): # ConcentratedSolar World exposed a corner case, test it specifically. data_sources = { 'Ambitious Cases': { 'source1': str(datadir.joinpath('ad_CSP_World_source1.csv')), 'source2': str(datadir.joinpath('ad_CSP_World_source2.csv')), 'source3': str(datadir.joinpath('ad_CSP_World_source3.csv')), 'source4': str(datadir.joinpath('ad_CSP_World_source4.csv')), 'source5': str(datadir.joinpath('ad_CSP_World_source5.csv')), 'source6': str(datadir.joinpath('ad_CSP_World_source6.csv')), }, 'Conservative Cases': {}, 'Baseline Cases': {}, '100% RES2050 Case': {}, } ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='Ambitious Cases', soln_pds_adoption_prognostication_growth='Low') ad = adoptiondata.AdoptionData(ac=ac, data_sources=data_sources, adconfig=g_adconfig) result = ad.adoption_trend(region='World') assert result.loc[2014, 'adoption'] == pytest.approx(34.94818207) assert result.loc[2015, 'adoption'] == pytest.approx(24.85041545) assert result.loc[2016, 'adoption'] == pytest.approx(17.78567283) assert result.loc[2060, 'adoption'] == pytest.approx(4079.461034)
def test_ch4_tons_reduced(): soln_net_annual_funits_adopted = pd.DataFrame( soln_net_annual_funits_adopted_list[1:], columns=soln_net_annual_funits_adopted_list[0]).set_index('Year') ac = advanced_controls.AdvancedControls(report_start_year=2020, report_end_year=2050, ch4_co2_per_funit=0.01, ch4_is_co2eq=False) c4 = ch4calcs.CH4Calcs( ac=ac, soln_net_annual_funits_adopted=soln_net_annual_funits_adopted) result = c4.ch4_tons_reduced() expected = pd.DataFrame(ch4_tons_reduced_list[1:], columns=ch4_tons_reduced_list[0]).set_index('Year') is_data_handler = issubclass(type(c4), DataHandler) json_data = c4.to_json() existing_key = 'ch4_tons_reduced' in json_data pd.testing.assert_frame_equal(result.loc[2015:], expected, check_exact=False) assert is_data_handler == True assert existing_key == True pd.testing.assert_frame_equal(json_data['ch4_tons_reduced'].loc[2015:], expected, check_exact=False)
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 } class fakeVMA: df = pd.DataFrame(0, index=[0, 1], columns=vma.VMA_columns) def avg_high_low(self, key, region=None): if region and key == 'mean': return vals[region] return (None, None, None) vmas = {'SOLUTION First Cost per Implementation Unit': fakeVMA()} ac = advanced_controls.AdvancedControls(vmas=vmas, 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_ElectricityGenOnGrid_conv_ref_grid_CO2eq_per_KWh_invalid_range(): with pytest.raises(ValueError): ac = advanced_controls.AdvancedControls( emissions_grid_source="ipcc_only", emissions_grid_range="nosuchrange") eg = ef.ElectricityGenOnGrid(ac=ac) _ = eg.conv_ref_grid_CO2eq_per_KWh()
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_adoption_data_per_region_source_None(): ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=None) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) assert all(ad.adoption_data_per_region().isna())
def test_lifetime_replacement(): ac = advanced_controls.AdvancedControls(soln_lifetime_capacity=50000, soln_avg_annual_use=1000, conv_lifetime_capacity=10000, conv_avg_annual_use=3) assert ac.soln_lifetime_replacement == 50 assert ac.conv_lifetime_replacement == pytest.approx(3333.333333333333)
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_replacement_raises_error(): """ Lifetime replacement values require different inputs for LAND and RRS """ ac = advanced_controls.AdvancedControls(soln_lifetime_capacity=None, soln_expected_lifetime=None) with pytest.raises(ValueError): ac.soln_lifetime_replacement ac = advanced_controls.AdvancedControls(soln_lifetime_capacity=None, soln_expected_lifetime=None) with pytest.raises(ValueError): ac.soln_lifetime_replacement_rounded ac = advanced_controls.AdvancedControls(conv_lifetime_capacity=None, conv_expected_lifetime=None) with pytest.raises(ValueError): ac.conv_lifetime_replacement ac = advanced_controls.AdvancedControls(conv_lifetime_capacity=None, conv_expected_lifetime=None) with pytest.raises(ValueError): ac.conv_lifetime_replacement_rounded
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_soln_pds_adoption_args(): ac = advanced_controls.AdvancedControls( soln_pds_adoption_basis="Existing Adoption Prognostications", soln_pds_adoption_prognostication_growth="Medium", soln_pds_adoption_prognostication_source="test1") assert ac.soln_pds_adoption_basis == "Existing Adoption Prognostications" assert ac.soln_pds_adoption_prognostication_growth == "Medium" assert ac.soln_pds_adoption_prognostication_source == "test1" ac = advanced_controls.AdvancedControls( soln_pds_adoption_basis="DEFAULT S-Curve", soln_pds_adoption_prognostication_growth="Low", soln_pds_adoption_prognostication_source="test2") assert ac.soln_pds_adoption_basis == "Logistic S-Curve" assert ac.soln_pds_adoption_prognostication_growth == "Low" assert ac.soln_pds_adoption_prognostication_source == "test2" with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls(soln_pds_adoption_basis="???") with pytest.raises(ValueError): _ = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_growth="???")
def test_lookup_vma(): class fakeVMA: def avg_high_low(self, key): return (1.2, 1.4, 1.0) vmas = {'VMA1': fakeVMA(), 'VMA2': fakeVMA()} ac = advanced_controls.AdvancedControls(vmas=vmas, vma_values={ 'VMA1': 2.0, 'statistic': 'mean' }) assert ac.lookup_vma('VMA1') == 2.0
def test_substitute_vma_not_has_data(): class fakeVMA: def __init__(self): self.v = (np.nan, np.nan, np.nan) def avg_high_low(self, key): if key == 'mean': return self.v[0] if key == 'high': return self.v[1] if key == 'low': return self.v[2] return v v = fakeVMA() v.df = pd.DataFrame(0, index=[0, 1], columns=vma.VMA_columns) vmas = {'Sequestration Rates': v} with pytest.raises(KeyError): _ = advanced_controls.AdvancedControls(vmas=vmas, seq_rate_global='mean') v.v = (1, 2, 3) ac = advanced_controls.AdvancedControls(vmas=vmas, seq_rate_global='mean') assert ac.seq_rate_global == 1
def test_ch4_tons_reduced_co2eq(): soln_net_annual_funits_adopted = pd.DataFrame( soln_net_annual_funits_adopted_list[1:], columns=soln_net_annual_funits_adopted_list[0]).set_index('Year') ac = advanced_controls.AdvancedControls(report_start_year=2020, report_end_year=2050, ch4_co2_per_funit=0.01, ch4_is_co2eq=True) c4 = ch4calcs.CH4Calcs( ac=ac, soln_net_annual_funits_adopted=soln_net_annual_funits_adopted) result = c4.ch4_tons_reduced() assert result.values.sum() == 0.0
def test_lifetime_replacement_rounded(): ac = advanced_controls.AdvancedControls(soln_lifetime_capacity=63998.595, soln_avg_annual_use=2844.382, conv_lifetime_capacity=1.5, conv_avg_annual_use=1) assert ac.soln_lifetime_replacement_rounded == 23 assert ac.conv_lifetime_replacement_rounded == 2 ac = advanced_controls.AdvancedControls(conv_lifetime_capacity=63998.595, conv_avg_annual_use=2844.382, soln_lifetime_capacity=1.5, soln_avg_annual_use=1) assert ac.conv_lifetime_replacement_rounded == 23 assert ac.soln_lifetime_replacement_rounded == 2 # From Water Efficiency ac = advanced_controls.AdvancedControls( soln_lifetime_capacity=1086.6259087991305, soln_avg_annual_use=72.44172725327537, conv_lifetime_capacity=1629.9388631986958, conv_avg_annual_use=72.44172725327537) assert ac.conv_lifetime_replacement_rounded == 23 assert ac.soln_lifetime_replacement_rounded == 15
def test_ElectricityGenOnGrid_conv_ref_grid_CO2eq_per_KWh(): ac = advanced_controls.AdvancedControls(emissions_grid_source="ipcc_only", emissions_grid_range="mean") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2025, "OECD90"] == pytest.approx(0.454068989) assert table.loc[2020, 'World'] == pytest.approx(0.483415642) ac = advanced_controls.AdvancedControls(emissions_grid_source="ipcc_only", emissions_grid_range="low") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2020, 'World'] == pytest.approx(0.416520905) ac = advanced_controls.AdvancedControls(emissions_grid_source="ipcc_only", emissions_grid_range="high") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2020, 'World'] == pytest.approx(0.952177536) ac = advanced_controls.AdvancedControls( emissions_grid_source="meta-analysis", emissions_grid_range="mean") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2020, 'World'] == pytest.approx(0.581083120) ac = advanced_controls.AdvancedControls( emissions_grid_source="meta-analysis", emissions_grid_range="low") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2020, 'World'] == pytest.approx(0.446005409) ac = advanced_controls.AdvancedControls( emissions_grid_source="meta-analysis", emissions_grid_range="high") eg = ef.ElectricityGenOnGrid(ac=ac) table = eg.conv_ref_grid_CO2eq_per_KWh() assert table.loc[2020, 'World'] == pytest.approx(0.726403172)
def test_adoption_trend_global(): s = 'Greenpeace AER' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s, soln_pds_adoption_prognostication_growth='Medium') ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) result = ad.adoption_trend(region='World', trend='Linear') expected = pd.DataFrame(linear_trend_global_list[1:], columns=linear_trend_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False) result = ad.adoption_trend(region='World', trend='Degree2') expected = pd.DataFrame(poly_degree2_trend_global_list[1:], columns=poly_degree2_trend_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False) result = ad.adoption_trend(region='World', trend='Degree3') expected = pd.DataFrame(poly_degree3_trend_global_list[1:], columns=poly_degree3_trend_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False) result = ad.adoption_trend(region='World', trend='Exponential') expected = pd.DataFrame(exponential_trend_global_list[1:], columns=exponential_trend_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False) adconfig_mod = g_adconfig.copy() adconfig_mod.loc['trend', 'World'] = 'Exponential' ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=adconfig_mod) result = ad.adoption_trend(region='World') expected = pd.DataFrame(exponential_trend_global_list[1:], columns=exponential_trend_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False)
def test_adoption_low_med_high_global_all_sources(): ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source='ALL SOURCES') ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) result = ad.adoption_low_med_high(region='World') expected = pd.DataFrame( adoption_low_med_high_global_all_sources_list[1:], columns=adoption_low_med_high_global_all_sources_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False)
def test_adoption_min_max_sd(): s = 'Greenpeace AER' ac = advanced_controls.AdvancedControls( soln_pds_adoption_prognostication_source=s) ad = adoptiondata.AdoptionData(ac=ac, data_sources=g_data_sources, adconfig=g_adconfig) result = ad.adoption_min_max_sd(region='World') expected = pd.DataFrame(adoption_min_max_sd_global_list[1:], columns=adoption_min_max_sd_global_list[0], dtype=np.float64).set_index('Year') expected.index = expected.index.astype(int) expected.index.name = 'Year' pd.testing.assert_frame_equal(result, expected, check_exact=False)
def test_electricity_factors(): conv_annual_energy_used = 2.117 class fakeVMA: def avg_high_low(self, key): return (0.0, 0.0, 0.0) vmas = {'SOLUTION Energy Efficiency Factor': fakeVMA()} ac = advanced_controls.AdvancedControls( vmas=vmas, conv_annual_energy_used=conv_annual_energy_used) assert ac.soln_energy_efficiency_factor == 0 assert ac.conv_annual_energy_used == pytest.approx(conv_annual_energy_used) assert ac.soln_annual_energy_used == 0
def test_substitute_vma(): class fakeVMA: df = pd.DataFrame(0, index=[0, 1], columns=vma.VMA_columns) def avg_high_low(self, key): if key == 'mean': return 'mean value' if key == 'high': return 'high value' if key == 'low': return 'low value' return ('mean value', 'high value', 'low value') seq_vma = {'Sequestration Rates': fakeVMA()} ac = advanced_controls.AdvancedControls(vmas=seq_vma, seq_rate_global='mean') assert ac.seq_rate_global == 'mean value'