Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make statistic plots ariadne compatible #1113

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rules/postprocess.smk
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ STATISTICS = {
}


rule save_statistics_csv:
rule write_statistics:
params:
statistics=STATISTICS,
input:
Expand All @@ -307,6 +307,9 @@ rule save_statistics_csv:
},
csv_touch=RESULTS
+ "statistics/csv/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}/country_{country}/.statistics_{carrier}_csv",
log:
RESULTS
+ "logs/write_statistics/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_country-{country}_carrier-{carrier}.log",
script:
"../scripts/write_statistics.py"

Expand Down
20 changes: 16 additions & 4 deletions scripts/plot_statistics_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def read_csv(input, output):
try:
# filter required csv to plot the wanted output
files = list(filter(lambda x: output in x, input))
pattern = r"elec_.*?(\d{4})"
network_labels = [re.search(pattern, f).group() for f in files]
# retrieves network labels from folder name
network_labels = [file.split("/")[-3] for file in files]
df = pd.concat(
[
pd.read_csv(f, skiprows=2).set_index(["component", "carrier"])
Expand All @@ -92,9 +92,11 @@ def read_csv(input, output):

snakemake = mock_snakemake(
"plot_statistics_comparison",
country="all",
carrier="electricity",
run="CurrentPolicies",
country="DE",
carrier="H2",
)

configure_logging(snakemake)

tech_colors = pd.Series(snakemake.params.plotting["tech_colors"])
Expand All @@ -108,6 +110,16 @@ def read_csv(input, output):
fig, ax = plt.subplots()
df = read_csv(snakemake.input, output)
if df.empty:
ax.text(
0.5,
0.5,
"No data available.",
ha="center",
va="center",
transform=ax.transAxes,
fontsize=14,
color="red",
)
fig.savefig(snakemake.output[output])
continue

Expand Down
18 changes: 14 additions & 4 deletions scripts/plot_statistics_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def read_csv(input):

snakemake = mock_snakemake(
"plot_statistics_single",
run="240219-test/normal",
run="CurrentPolicies",
simpl="",
ll="v1.2",
ll="vopt",
clusters="22",
opts="",
sector_opts="none",
planning_horizons="2020",
country="DE",
carrier="heat",
carrier="H2",
)
configure_logging(snakemake)

Expand All @@ -78,7 +78,17 @@ def read_csv(input):
fig, ax = plt.subplots()
ds = read_csv(snakemake.input[output])
if ds.empty:
fig.savefig(snakemake.output[output])
ax.text(
0.5,
0.5,
"No data available.",
ha="center",
va="center",
transform=ax.transAxes,
fontsize=18,
color="red",
)
fig.savefig(snakemake.output[output], bbox_inches="tight")
continue
plot_static_single(ds, ax)
fig.savefig(snakemake.output[output], bbox_inches="tight")
11 changes: 5 additions & 6 deletions scripts/write_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call_with_handle(func, **kwargs):
try:
ds = func(**kwargs)
except Exception as e:
print(f"An error occurred: {e}")
logging.info(f"An error occurred: {e}")
ds = pd.Series()
pass
return ds
Expand All @@ -31,10 +31,10 @@ def call_with_handle(func, **kwargs):
from _helpers import mock_snakemake

snakemake = mock_snakemake(
"save_statistics_csv",
run="240219-test/normal",
"write_statistics",
run="CurrentPolicies",
simpl="",
ll="v1.2",
ll="vopt",
clusters="22",
opts="",
sector_opts="none",
Expand All @@ -47,7 +47,6 @@ def call_with_handle(func, **kwargs):
config = snakemake.config

n = pypsa.Network(snakemake.input.network)

kwargs = {"nice_names": False}

wildcards = dict(snakemake.wildcards)
Expand Down Expand Up @@ -101,7 +100,7 @@ def call_with_handle(func, **kwargs):
ds = call_with_handle(func, **kwargs)

if ds.empty:
print(
logging.info(
f"Empty series for {output} with bus carrier {bus_carrier} and country {country}."
)
pd.Series().to_csv(snakemake.output[output])
Expand Down