def test_get_interpolated_spline_extrapolated_linear_function(): # If there is only one unique x, expect default_slope f = get_interpolated_spline_extrapolated_linear_function( [(100, 100)], default_slope=5) assert np.isclose(f(102), 110) # If x is inside the interpolated domain, y should match original # If x is on the left, y should extend the trend from the leftmost point # If x is on the right, y should extend the trend from the rightmost point f = get_interpolated_spline_extrapolated_linear_function( [(100, 1), (300, 2), (500, 5)]) assert np.isclose(f(300), 2) assert np.isclose(f(99), 0.99) assert np.isclose(f(501), 5.01) assert np.allclose(f([300, 99, 501]), [2, 0.99, 5.01]) # Get ys from xs
def test_get_interpolated_spline_extrapolated_linear_function(): # If there is only one unique x, expect default_slope f = get_interpolated_spline_extrapolated_linear_function([(100, 100)], default_slope=5) assert np.isclose(f(102), 110) # If x is inside the interpolated domain, y should match original # If x is on the left, y should extend the trend from the leftmost point # If x is on the right, y should extend the trend from the rightmost point f = get_interpolated_spline_extrapolated_linear_function([(100, 1), (300, 2), (500, 5)]) assert np.isclose(f(300), 2) assert np.isclose(f(99), 0.99) assert np.isclose(f(501), 5.01) assert np.allclose(f([300, 99, 501]), [2, 0.99, 5.01]) # Get ys from xs
def estimate_population(target_year, country_name): t = POPULATION_BY_YEAR_BY_COUNTRY_TABLE country_t = _get_country_table(t, "Country or Area", country_name) try: earliest_estimated_year = min(country_t[country_t["Variant"] == "Low variant"]["Year(s)"]) except ValueError: raise InvalidData("missing population") # Get actual population for each year year_packs = country_t[country_t["Year(s)"] < earliest_estimated_year][["Year(s)", "Value"]].values # Estimate population for the given year estimate_population = get_interpolated_spline_extrapolated_linear_function(year_packs) return estimate_population(target_year)
def estimate_population(target_year, country_name): t = POPULATION_BY_YEAR_BY_COUNTRY_TABLE country_t = _get_country_table(t, 'Country or Area', country_name) try: earliest_estimated_year = min( country_t[country_t['Variant'] == 'Low variant']['Year(s)']) except ValueError: raise EmptyDataset('Missing population') # Get actual population for each year year_packs = country_t[country_t['Year(s)'] < earliest_estimated_year][[ 'Year(s)', 'Value' ]].values # Estimate population for the given year estimate_population = get_interpolated_spline_extrapolated_linear_function( year_packs) return estimate_population(target_year)
def estimate_electricity_consumption_per_capita(target_year, country_name): t = ELECTRICITY_CONSUMPTION_PER_CAPITA_BY_YEAR_TABLE country_t = _get_country_table(t, "Country Name", country_name) if not len(country_t): raise InvalidData("missing country_name") year_packs = [] for column_name in country_t.columns: try: year = int(column_name) except ValueError: continue value = country_t[column_name].values[0] if isnull(value): continue year_packs.append((year, value)) if not year_packs: raise InvalidData("missing year_value") estimate_electricity_consumption_per_capita = get_interpolated_spline_extrapolated_linear_function(year_packs) return estimate_electricity_consumption_per_capita(target_year)
def estimate_electricity_consumption_per_capita(target_year, country_name): t = ELECTRICITY_CONSUMPTION_PER_CAPITA_BY_YEAR_TABLE country_t = _get_country_table(t, 'Country Name', country_name) if not len(country_t): raise InvalidData('missing country_name') year_packs = [] for column_name in country_t.columns: try: year = int(column_name) except ValueError: continue value = country_t[column_name].values[0] if isnull(value): continue year_packs.append((year, value)) if not year_packs: raise InvalidData('missing year_value') estimate_electricity_consumption_per_capita = \ get_interpolated_spline_extrapolated_linear_function(year_packs) return estimate_electricity_consumption_per_capita(target_year)