From 3fabcc80cb2448cb4113ce8a3cc7c797668adf31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Tr=C3=B6ndle?= Date: Fri, 15 Mar 2024 10:39:08 +0100 Subject: [PATCH] Format entire code base based on new code guidelines Following #285. --- scripts/biofuels/allocate.py | 108 +++++++++++++++++++------------ scripts/biofuels/extract.py | 27 ++++---- scripts/biofuels/template_bio.py | 18 ++++-- 3 files changed, 92 insertions(+), 61 deletions(-) diff --git a/scripts/biofuels/allocate.py b/scripts/biofuels/allocate.py index 4a91fafb..0e3c23b5 100644 --- a/scripts/biofuels/allocate.py +++ b/scripts/biofuels/allocate.py @@ -1,4 +1,5 @@ """Take national potentials, allocate to regions based on feedstocks and proxies, and sum over feedstocks.""" + from enum import Enum import pandas as pd @@ -10,6 +11,7 @@ class GlobCover(Enum): """Original categories taken from GlobCover 2009 land cover.""" + POST_FLOODING = "lc_11" RAINFED_CROPLANDS = "lc_14" MOSAIC_CROPLAND = "lc_20" @@ -25,9 +27,11 @@ class GlobCover(Enum): CLOSED_TO_OPEN_SHRUBLAND = "lc_130" CLOSED_TO_OPEN_HERBS = "lc_140" SPARSE_VEGETATION = "lc_150" - CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST = "lc_160" # doesn't exist in Europe - CLOSED_REGULARLY_FLOODED_FOREST = "lc_170" # doesn't exist in Europe - CLOSED_TO_OPEN_REGULARLY_FLOODED_GRASSLAND = "lc_180" # roughly 2.3% of land in Europe + CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST = "lc_160" # doesn't exist in Europe + CLOSED_REGULARLY_FLOODED_FOREST = "lc_170" # doesn't exist in Europe + CLOSED_TO_OPEN_REGULARLY_FLOODED_GRASSLAND = ( + "lc_180" # roughly 2.3% of land in Europe + ) ARTIFICAL_SURFACES_AND_URBAN_AREAS = "lc_190" BARE_AREAS = "lc_200" WATER_BODIES = "lc_210" @@ -35,42 +39,66 @@ class GlobCover(Enum): NO_DATA = "lc_230" -FARM = [GlobCover.POST_FLOODING.value, GlobCover.RAINFED_CROPLANDS.value, - GlobCover.MOSAIC_CROPLAND.value, GlobCover.MOSAIC_VEGETATION.value] -FOREST = [GlobCover.CLOSED_TO_OPEN_BROADLEAVED_FOREST.value, GlobCover.CLOSED_BROADLEAVED_FOREST.value, - GlobCover.OPEN_BROADLEAVED_FOREST.value, GlobCover.CLOSED_NEEDLELEAVED_FOREST.value, - GlobCover.OPEN_NEEDLELEAVED_FOREST.value, GlobCover.CLOSED_TO_OPEN_MIXED_FOREST.value, - GlobCover.MOSAIC_FOREST.value, GlobCover.CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST.value, - GlobCover.CLOSED_REGULARLY_FLOODED_FOREST.value] +FARM = [ + GlobCover.POST_FLOODING.value, + GlobCover.RAINFED_CROPLANDS.value, + GlobCover.MOSAIC_CROPLAND.value, + GlobCover.MOSAIC_VEGETATION.value, +] +FOREST = [ + GlobCover.CLOSED_TO_OPEN_BROADLEAVED_FOREST.value, + GlobCover.CLOSED_BROADLEAVED_FOREST.value, + GlobCover.OPEN_BROADLEAVED_FOREST.value, + GlobCover.CLOSED_NEEDLELEAVED_FOREST.value, + GlobCover.OPEN_NEEDLELEAVED_FOREST.value, + GlobCover.CLOSED_TO_OPEN_MIXED_FOREST.value, + GlobCover.MOSAIC_FOREST.value, + GlobCover.CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST.value, + GlobCover.CLOSED_REGULARLY_FLOODED_FOREST.value, +] -def biofuel_potential(path_to_national_potentials, path_to_national_costs, path_to_units, path_to_land_cover, - path_to_population, scenario, potential_year, cost_year, proxies, paths_to_output): +def biofuel_potential( + path_to_national_potentials, + path_to_national_costs, + path_to_units, + path_to_land_cover, + path_to_population, + scenario, + potential_year, + cost_year, + proxies, + paths_to_output, +): """Take national potentials from JRC report and allocate to regions based on proxies.""" national_potentials = ( - pd - .read_csv(path_to_national_potentials, index_col=["year", "scenario", "country_code", "feedstock"])["value"] + pd.read_csv( + path_to_national_potentials, + index_col=["year", "scenario", "country_code", "feedstock"], + )["value"] .mul(PJ_TO_MWH) .xs((potential_year, scenario), level=("year", "scenario")) ) national_costs = ( - pd - .read_csv(path_to_national_costs, index_col=["year", "scenario", "country_code", "feedstock"])["value"] + pd.read_csv( + path_to_national_costs, + index_col=["year", "scenario", "country_code", "feedstock"], + )["value"] .div(GJ_TO_MWH) .xs((potential_year, scenario), level=("year", "scenario")) ) units = pd.read_csv(path_to_units).set_index("id") - if (len(units.index) == 1) and (units.index[0] == "EUR"): # special case for continental level + if (len(units.index) == 1) and ( + units.index[0] == "EUR" + ): # special case for continental level cost = ( - national_costs - .mul(national_potentials) - .div(national_potentials.sum()) - .sum() + national_costs.mul(national_potentials).div(national_potentials.sum()).sum() + ) + national_costs = pd.Series([cost], index=["EUR"]).rename_axis( + index="country_code" ) - national_costs = pd.Series([cost], index=["EUR"]).rename_axis(index="country_code") national_potentials = ( - national_potentials - .rename(lambda x: "EUR", level="country_code") + national_potentials.rename(lambda x: "EUR", level="country_code") .groupby(level=["country_code", "feedstock"]) .sum() ) @@ -78,35 +106,33 @@ def biofuel_potential(path_to_national_potentials, path_to_national_costs, path_ total_potential = allocate_potentials( national_potentials=national_potentials, units=units, - population=pd.read_csv(path_to_population, index_col=0).reindex(index=units.index)["population_sum"], - land_cover=pd.read_csv(path_to_land_cover, index_col=0).reindex(index=units.index), - proxies=proxies + population=pd.read_csv(path_to_population, index_col=0).reindex( + index=units.index + )["population_sum"], + land_cover=pd.read_csv(path_to_land_cover, index_col=0).reindex( + index=units.index + ), + proxies=proxies, ) total_potential.to_csv(paths_to_output.potentials, index=True, header=True) weighted_cost = ( - national_costs - .mul(national_potentials) - .div(national_potentials.sum()) - .sum() + national_costs.mul(national_potentials).div(national_potentials.sum()).sum() ) with open(paths_to_output.costs, "w") as f_cost: f_cost.write(str(weighted_cost)) def allocate_potentials(national_potentials, units, population, land_cover, proxies): - regional_potentials = ( - pd.merge( - national_potentials.reset_index(), - units["country_code"].reset_index(), - on="country_code" - ) - .pivot(index="id", columns="feedstock", values="value") - ) + regional_potentials = pd.merge( + national_potentials.reset_index(), + units["country_code"].reset_index(), + on="country_code", + ).pivot(index="id", columns="feedstock", values="value") shares = ( pd.concat( [population, land_cover[FOREST].sum(axis=1), land_cover[FARM].sum(axis=1)], axis=1, - keys=['population', 'forest', "farmland"] + keys=["population", "forest", "farmland"], ) .groupby(units.country_code) .transform(lambda x: x / x.sum()) @@ -127,5 +153,5 @@ def allocate_potentials(national_potentials, units, population, land_cover, prox potential_year=snakemake.params.potential_year, cost_year=snakemake.params.cost_year, proxies=snakemake.params.proxies, - paths_to_output=snakemake.output + paths_to_output=snakemake.output, ) diff --git a/scripts/biofuels/extract.py b/scripts/biofuels/extract.py index edf8501f..bdd50e81 100644 --- a/scripts/biofuels/extract.py +++ b/scripts/biofuels/extract.py @@ -1,5 +1,4 @@ import pandas as pd - from eurocalliopelib import utils SHEET_NAME_POTENTIALS = "ENER - NUTS0 EnergyCom" @@ -12,11 +11,7 @@ COL_NAME_VALUE = "Value" COL_NAME_VALUE2 = "NUTS0 Energy Commodity Cost " -SCENARIO_NAME_MAP = { - "ENS_Low": "low", - "ENS_Med": "medium", - "ENS_High": "high" -} +SCENARIO_NAME_MAP = {"ENS_Low": "low", "ENS_Med": "medium", "ENS_High": "high"} COL_NAME_MAP = { COL_NAME_COUNTRY: "country_code", @@ -24,23 +19,29 @@ COL_NAME_SCENARIO: "scenario", COL_NAME_YEAR: "year", COL_NAME_VALUE: "value", - COL_NAME_VALUE2: "value" + COL_NAME_VALUE2: "value", } MISSING_COST_VALUE = (2030, "high", "NOR", "municipal-waste") REPLACEMENT_COST_VALUE = (2030, "medium", "NOR", "municipal-waste") MISSING_POTENTIAL_VALUE = (2010, "medium", "BIH", "sludge") -REPLACEMENT_POTENTIAL_VALUE = (2010, "high", "BIH", "sludge") # 0 PJ +REPLACEMENT_POTENTIAL_VALUE = (2010, "high", "BIH", "sludge") # 0 PJ def extract_biofuels(path_to_raw_data, feedstocks, paths_to_outputs): - potentials = read_and_filter_and_map_names(path_to_raw_data, SHEET_NAME_POTENTIALS, feedstocks) - potentials = replace(potentials, MISSING_POTENTIAL_VALUE, REPLACEMENT_POTENTIAL_VALUE) + potentials = read_and_filter_and_map_names( + path_to_raw_data, SHEET_NAME_POTENTIALS, feedstocks + ) + potentials = replace( + potentials, MISSING_POTENTIAL_VALUE, REPLACEMENT_POTENTIAL_VALUE + ) assert potentials["value"].isna().sum() == 0 potentials.to_csv(paths_to_outputs.potentials, index=True, header=True) - costs = read_and_filter_and_map_names(path_to_raw_data, SHEET_NAME_COSTS, feedstocks) + costs = read_and_filter_and_map_names( + path_to_raw_data, SHEET_NAME_COSTS, feedstocks + ) costs = replace(costs, MISSING_COST_VALUE, REPLACEMENT_COST_VALUE) assert costs["value"].isna().sum() == 0 costs.to_csv(paths_to_outputs.costs, index=True, header=True) @@ -60,7 +61,7 @@ def read_and_filter_and_map_names(path_to_raw_data, sheet_name, feedstocks): df[COL_NAME_FEEDSTOCK] = df[COL_NAME_FEEDSTOCK].map(feedstocks) df[COL_NAME_COUNTRY] = df[COL_NAME_COUNTRY].map(utils.eu_country_code_to_iso3) df.rename(columns=COL_NAME_MAP, inplace=True) - df["value"] = pd.to_numeric(df.value, errors='coerce') + df["value"] = pd.to_numeric(df.value, errors="coerce") return df @@ -77,5 +78,5 @@ def replace(df, index_to_replace, index_replace_from): extract_biofuels( path_to_raw_data=snakemake.input.potentials_and_costs, feedstocks=snakemake.params.feedstocks, - paths_to_outputs=snakemake.output + paths_to_outputs=snakemake.output, ) diff --git a/scripts/biofuels/template_bio.py b/scripts/biofuels/template_bio.py index 6d245b54..e6270c35 100644 --- a/scripts/biofuels/template_bio.py +++ b/scripts/biofuels/template_bio.py @@ -1,22 +1,26 @@ import pandas as pd - from eurocalliopelib.template import parametrise_template def construct_techs_and_locations( - path_to_template, path_to_output, path_to_locations, path_to_biofuel_costs, - biofuel_efficiency, scaling_factors + path_to_template, + path_to_output, + path_to_locations, + path_to_biofuel_costs, + biofuel_efficiency, + scaling_factors, ): - with open(path_to_biofuel_costs, "r") as f_biofuel_costs: + with open(path_to_biofuel_costs) as f_biofuel_costs: biofuel_fuel_cost = float(f_biofuel_costs.readline()) locations = pd.read_csv(path_to_locations, index_col=0) return parametrise_template( - path_to_template, path_to_output, + path_to_template, + path_to_output, biofuel_fuel_cost=biofuel_fuel_cost, biofuel_efficiency=biofuel_efficiency, scaling_factors=scaling_factors, - locations=locations + locations=locations, ) @@ -27,5 +31,5 @@ def construct_techs_and_locations( path_to_biofuel_costs=snakemake.input.biofuel_cost, biofuel_efficiency=snakemake.params.biofuel_efficiency, scaling_factors=snakemake.params.scaling_factors, - path_to_output=snakemake.output[0] + path_to_output=snakemake.output[0], )