From 0384b4ff834f4d2141c9c1e5c29dd1c649258a6a Mon Sep 17 00:00:00 2001 From: Seth Date: Thu, 10 Mar 2022 13:55:53 +0100 Subject: [PATCH 1/4] fix the plot_network snakemake rule --- scripts/_helpers.py | 2 +- scripts/plot_network.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/scripts/_helpers.py b/scripts/_helpers.py index f1e5e887..410e05af 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -120,7 +120,7 @@ def load_network_for_plots(fn, tech_costs, config, combine_hydro_ps=True): # n.storage_units.loc[bus_carrier == "heat","carrier"] = "water tanks" Nyears = n.snapshot_weightings.objective.sum() / 8760. - costs = load_costs(Nyears, tech_costs, config['costs'], config['electricity']) + costs = load_costs(tech_costs, config['costs'], config['electricity'], Nyears) update_transmission_costs(n, costs) return n diff --git a/scripts/plot_network.py b/scripts/plot_network.py index 645c8c39..24d49473 100755 --- a/scripts/plot_network.py +++ b/scripts/plot_network.py @@ -20,8 +20,7 @@ Description """ import logging -from _helpers import (retrieve_snakemake_keys, load_network_for_plots, - aggregate_p, aggregate_costs, configure_logging) +from _helpers import (load_network_for_plots, aggregate_p, aggregate_costs, configure_logging) import pandas as pd import numpy as np @@ -182,7 +181,7 @@ def plot_map(n, ax=None, attribute='p_nom', opts={}): return fig -def plot_total_energy_pie(n, ax=None): +def plot_total_energy_pie(n, ax=None, opts={}): if ax is None: ax = plt.gca() ax.set_title('Energy per technology', fontdict=dict(fontsize="medium")) @@ -200,7 +199,7 @@ def plot_total_energy_pie(n, ax=None): t1.remove() t2.remove() -def plot_total_cost_bar(n, ax=None): +def plot_total_cost_bar(n, ax=None, opts={}): if ax is None: ax = plt.gca() total_load = (n.snapshot_weightings.generators * n.loads_t.p.sum(axis=1)).sum() @@ -259,25 +258,31 @@ if __name__ == "__main__": set_plot_style() - paths, config, wildcards, logs, out = retrieve_snakemake_keys(snakemake) + paths, config, wildcards, logs, out = ( + snakemake.input, + snakemake.config, + snakemake.wildcards, + snakemake.log, + snakemake.output, + ) - map_figsize = config['map']['figsize'] - map_boundaries = config['map']['boundaries'] + map_figsize = config["plotting"]['map']['figsize'] + map_boundaries = config["plotting"]['map']['boundaries'] n = load_network_for_plots(paths.network, paths.tech_costs, config) scenario_opts = wildcards.opts.split('-') fig, ax = plt.subplots(figsize=map_figsize, subplot_kw={"projection": ccrs.PlateCarree()}) - plot_map(n, ax, wildcards.attr, config) + plot_map(n, ax, wildcards.attr, config["plotting"]) fig.savefig(out.only_map, dpi=150, bbox_inches='tight') ax1 = fig.add_axes([-0.115, 0.625, 0.2, 0.2]) - plot_total_energy_pie(n, ax1) + plot_total_energy_pie(n, ax1, config["plotting"]) ax2 = fig.add_axes([-0.075, 0.1, 0.1, 0.45]) - plot_total_cost_bar(n, ax2) + plot_total_cost_bar(n, ax2, config["plotting"]) ll = wildcards.ll ll_type = ll[0] From e45c7a65ffb767a5831ac6623813d2dd11228afb Mon Sep 17 00:00:00 2001 From: Seth <78690362+thesethtruth@users.noreply.github.com> Date: Mon, 28 Mar 2022 12:10:51 +0200 Subject: [PATCH 2/4] Apply suggestions from Martha's code review Co-authored-by: Martha Frysztacki --- scripts/plot_network.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/scripts/plot_network.py b/scripts/plot_network.py index 24d49473..3f95e16d 100755 --- a/scripts/plot_network.py +++ b/scripts/plot_network.py @@ -181,7 +181,7 @@ def plot_map(n, ax=None, attribute='p_nom', opts={}): return fig -def plot_total_energy_pie(n, ax=None, opts={}): +def plot_total_energy_pie(n, opts, ax=None): if ax is None: ax = plt.gca() ax.set_title('Energy per technology', fontdict=dict(fontsize="medium")) @@ -199,7 +199,7 @@ def plot_total_energy_pie(n, ax=None, opts={}): t1.remove() t2.remove() -def plot_total_cost_bar(n, ax=None, opts={}): +def plot_total_cost_bar(n, opts, ax=None): if ax is None: ax = plt.gca() total_load = (n.snapshot_weightings.generators * n.loads_t.p.sum(axis=1)).sum() @@ -258,13 +258,7 @@ if __name__ == "__main__": set_plot_style() - paths, config, wildcards, logs, out = ( - snakemake.input, - snakemake.config, - snakemake.wildcards, - snakemake.log, - snakemake.output, - ) + config, wildcards = snakemake.config, snakemake.wildcards map_figsize = config["plotting"]['map']['figsize'] map_boundaries = config["plotting"]['map']['boundaries'] From c37171b01d772b0b8cdec19b386cbd4493461faf Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 28 Mar 2022 13:42:55 +0200 Subject: [PATCH 3/4] feedback code review (opts argument and unpacking) --- scripts/plot_network.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/plot_network.py b/scripts/plot_network.py index 3f95e16d..71a6e627 100755 --- a/scripts/plot_network.py +++ b/scripts/plot_network.py @@ -74,7 +74,7 @@ def set_plot_style(): }]) -def plot_map(n, ax=None, attribute='p_nom', opts={}): +def plot_map(n, opts, ax=None, attribute='p_nom'): if ax is None: ax = plt.gca() @@ -263,20 +263,20 @@ if __name__ == "__main__": map_figsize = config["plotting"]['map']['figsize'] map_boundaries = config["plotting"]['map']['boundaries'] - n = load_network_for_plots(paths.network, paths.tech_costs, config) + n = load_network_for_plots(snakemake.input.network, snakemake.input.tech_costs, config) scenario_opts = wildcards.opts.split('-') fig, ax = plt.subplots(figsize=map_figsize, subplot_kw={"projection": ccrs.PlateCarree()}) - plot_map(n, ax, wildcards.attr, config["plotting"]) + plot_map(n, config["plotting"], ax=ax, attribute=wildcards.attr) - fig.savefig(out.only_map, dpi=150, bbox_inches='tight') + fig.savefig(snakemake.output.only_map, dpi=150, bbox_inches='tight') ax1 = fig.add_axes([-0.115, 0.625, 0.2, 0.2]) - plot_total_energy_pie(n, ax1, config["plotting"]) + plot_total_energy_pie(n, config["plotting"], ax=ax1) ax2 = fig.add_axes([-0.075, 0.1, 0.1, 0.45]) - plot_total_cost_bar(n, ax2, config["plotting"]) + plot_total_cost_bar(n, config["plotting"], ax=ax2) ll = wildcards.ll ll_type = ll[0] @@ -286,4 +286,4 @@ if __name__ == "__main__": fig.suptitle('Expansion to {amount} {label} at {clusters} clusters' .format(amount=amnt, label=lbl, clusters=wildcards.clusters)) - fig.savefig(out.ext, transparent=True, bbox_inches='tight') + fig.savefig(snakemake.output.ext, transparent=True, bbox_inches='tight') From 9eac6d9bbea17f0c8dacf003132df5a942002ff8 Mon Sep 17 00:00:00 2001 From: davide-f Date: Mon, 28 Mar 2022 21:45:03 +0200 Subject: [PATCH 4/4] Fix environment --- envs/environment.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/envs/environment.yaml b/envs/environment.yaml index 0c881720..00b7830a 100644 --- a/envs/environment.yaml +++ b/envs/environment.yaml @@ -37,6 +37,7 @@ dependencies: - pyomo - matplotlib - proj + - fiona <= 1.18.20 # Till issue https://github.com/Toblerity/Fiona/issues/1085 is not solved # Keep in conda environment when calling ipython - ipython