Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 15, 2024
1 parent ba33406 commit faee342
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 5 additions & 2 deletions scripts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,12 +747,15 @@ def read_csv_nafix(file, **kwargs):
else:
return pd.DataFrame()


def safe_divide(numerator, denominator):
"""
Safe division function that returns NaN when the denominator is zero
"""
if (denominator != 0.0):
if denominator != 0.0:
return numerator / denominator
else:
logging.warning(f"Division by zero: {numerator} / {denominator}, returning NaN.")
logging.warning(
f"Division by zero: {numerator} / {denominator}, returning NaN."
)
return np.nan
22 changes: 17 additions & 5 deletions scripts/prepare_sector_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
mock_snakemake,
override_component_attrs,
prepare_costs,
safe_divide,
three_2_two_digits_country,
two_2_three_digits_country,
safe_divide,
)
from prepare_transport_data import prepare_transport_data

Expand Down Expand Up @@ -2230,12 +2230,24 @@ def add_residential(n, costs):
)

heat_buses = (n.loads_t.p_set.filter(regex="heat").filter(like=country)).columns

n.loads_t.p_set.loc[:, heat_buses] = (
safe_divide(n.loads_t.p_set.filter(like=country)[heat_buses], n.loads_t.p_set.filter(like=country)[heat_buses].sum().sum())

n.loads_t.p_set.loc[:, heat_buses] = (
(
safe_divide(
n.loads_t.p_set.filter(like=country)[heat_buses],
n.loads_t.p_set.filter(like=country)[heat_buses].sum().sum(),
)
* rem_heat_demand
* 1e6
) if not np.isnan(safe_divide(n.loads_t.p_set.filter(like=country)[heat_buses], n.loads_t.p_set.filter(like=country)[heat_buses].sum().sum())) else 0.0
)
if not np.isnan(
safe_divide(
n.loads_t.p_set.filter(like=country)[heat_buses],
n.loads_t.p_set.filter(like=country)[heat_buses].sum().sum(),
)
)
else 0.0
)

# if snakemake.config["custom_data"]["elec_demand"]:
for country in countries:
Expand Down

0 comments on commit faee342

Please sign in to comment.