Skip to content

Commit

Permalink
Merge pull request #658 from openego/features/improve-code-style
Browse files Browse the repository at this point in the history
Features/improve code style
  • Loading branch information
ClaraBuettner authored Sep 26, 2023
2 parents 23e1da3 + cefa07a commit 17e3eba
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 302 deletions.
328 changes: 176 additions & 152 deletions etrago/appl.py

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions etrago/cluster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
__copyright__ = "tba"
__license__ = "tba"
__author__ = "tba"


50 changes: 25 additions & 25 deletions etrago/cluster/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
strategies_generators,
strategies_one_ports,
)
from etrago.tools.utilities import *

logger = logging.getLogger(__name__)

Expand All @@ -69,7 +68,7 @@

def _leading(busmap, df):
"""
Returns a function that computes the leading bus_id for a given mapped
Returns a function that computes the leading bus_id for a given mapped
list of buses.
Parameters
Expand All @@ -95,7 +94,7 @@ def leader(x):

def adjust_no_electric_network(etrago, busmap, cluster_met):
"""
Adjusts the non-electric network based on the electrical network
Adjusts the non-electric network based on the electrical network
(esp. eHV network), adds the gas buses to the busmap, and creates the
new buses for the non-electric network.
Expand All @@ -117,7 +116,7 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
"""
network = etrago.network
# network2 is supposed to contain all the not electrical or gas buses
# network2 is supposed to contain all the not electrical or gas buses
# and links
network2 = network.copy(with_time=False)
network2.buses = network2.buses[
Expand Down Expand Up @@ -148,7 +147,8 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
# eHV network
busmap2 = {}

# Map crossborder AC buses in case that they were not part of the k-mean clustering
# Map crossborder AC buses in case that they were not part of the k-mean
# clustering
if (not etrago.args["network_clustering"]["cluster_foreign_AC"]) & (
cluster_met in ["kmeans", "kmedoids-dijkstra"]
):
Expand Down Expand Up @@ -178,7 +178,8 @@ def adjust_no_electric_network(etrago, busmap, cluster_met):
& (network2.links["carrier"] == map_carrier[carry])
].copy()
df["elec"] = df["bus0"].isin(busmap.keys())
df = df[df["elec"] == True]

df = df[df["elec"]]
if len(df) > 0:
bus_hv = df["bus0"][0]

Expand Down Expand Up @@ -386,9 +387,9 @@ def cluster_on_extra_high_voltage(etrago, busmap, with_time=True):

def delete_ehv_buses_no_lines(network):
"""
When there are AC buses totally isolated, this function deletes them in
When there are AC buses totally isolated, this function deletes them in
order to make possible the creation of busmaps based on electrical
connections and other purposes. Additionally, it throws a warning to
connections and other purposes. Additionally, it throws a warning to
inform the user in case that any correction should be done.
Parameters
Expand All @@ -411,10 +412,10 @@ def delete_ehv_buses_no_lines(network):
buses_ac["with_gen"] = buses_ac.index.isin(network.generators.bus)

delete_buses = buses_ac[
(buses_ac["with_line"] == False)
& (buses_ac["with_load"] == False)
& (buses_ac["with_link"] == False)
& (buses_ac["with_gen"] == False)
(buses_ac["with_line"] is False)
& (buses_ac["with_load"] is False)
& (buses_ac["with_link"] is False)
& (buses_ac["with_gen"] is False)
].index

if len(delete_buses):
Expand Down Expand Up @@ -750,7 +751,7 @@ def preprocessing(etrago):
----------------------- WARNING ---------------------------
THE FOLLOWING BUSES HAVE NOT COUNTRY DATA:
{network.buses[network.buses.country.isna()].index.to_list()}.
{network.buses[network.buses.country.isna()].index.to_list()}.
THEY WILL BE ASSIGNED TO GERMANY, BUT IT IS POTENTIALLY A
SIGN OF A PROBLEM IN THE DATASET.
----------------------- WARNING ---------------------------
Expand All @@ -769,9 +770,9 @@ def preprocessing(etrago):
if settings["method"] == "kmedoids-dijkstra":
lines_col = network_elec.lines.columns

# The Dijkstra clustering works using the shortest electrical path
# The Dijkstra clustering works using the shortest electrical path
# between buses. In some cases, a bus has just DC connections, which
# are considered links. Therefore it is necessary to include
# are considered links. Therefore it is necessary to include
# temporarily the DC links into the lines table.
dc = network.links[network.links.carrier == "DC"]
str1 = "DC_"
Expand Down Expand Up @@ -825,7 +826,7 @@ def postprocessing(etrago, busmap, busmap_foreign, medoid_idx=None):
method = settings["method"]
num_clusters = settings["n_clusters_AC"]

if settings["k_elec_busmap"] == False:
if not settings["k_elec_busmap"]:
busmap.name = "cluster"
busmap_elec = pd.DataFrame(busmap.copy(), dtype="string")
busmap_elec.index.name = "bus"
Expand Down Expand Up @@ -871,7 +872,7 @@ def postprocessing(etrago, busmap, busmap_foreign, medoid_idx=None):
)

# merge busmap for foreign buses with the German buses
if settings["cluster_foreign_AC"] == False:
if not settings["cluster_foreign_AC"]:
for bus in busmap_foreign.index:
busmap[bus] = busmap_foreign[bus]
if bus == busmap_foreign[bus]:
Expand Down Expand Up @@ -936,7 +937,6 @@ def weighting_for_scenario(network, save=None):
"""

def calc_availability_factor(gen):

"""
Calculate the availability factor for a given generator.
Expand All @@ -952,10 +952,10 @@ def calc_availability_factor(gen):
Notes
-----
Availability factor is defined as the ratio of the average power
output of the generator over the maximum power output capacity of
Availability factor is defined as the ratio of the average power
output of the generator over the maximum power output capacity of
the generator. If the generator is time-dependent, its average power
output is calculated using the `network.generators_t` DataFrame.
output is calculated using the `network.generators_t` DataFrame.
Otherwise, its availability factor is obtained from the
`fixed_capacity_fac` dictionary, which contains pre-defined factors
for fixed capacity generators. If the generator's availability factor
Expand All @@ -968,7 +968,7 @@ def calc_availability_factor(gen):
else:
try:
cf = fixed_capacity_fac[gen["carrier"]]
except:
except KeyError:
print(gen)
cf = 1
return cf
Expand Down Expand Up @@ -1058,7 +1058,7 @@ def run_spatial_clustering(self):
elec_network, weight, n_clusters, busmap_foreign = preprocessing(self)

if self.args["network_clustering"]["method"] == "kmeans":
if self.args["network_clustering"]["k_elec_busmap"] == False:
if not self.args["network_clustering"]["k_elec_busmap"]:
logger.info("Start k-means Clustering")

busmap = kmean_clustering(
Expand All @@ -1070,7 +1070,7 @@ def run_spatial_clustering(self):
medoid_idx = pd.Series(dtype=str)

elif self.args["network_clustering"]["method"] == "kmedoids-dijkstra":
if self.args["network_clustering"]["k_elec_busmap"] == False:
if not self.args["network_clustering"]["k_elec_busmap"]:
logger.info("Start k-medoids Dijkstra Clustering")

busmap, medoid_idx = kmedoids_dijkstra_clustering(
Expand All @@ -1090,7 +1090,7 @@ def run_spatial_clustering(self):
)
self.update_busmap(busmap)

if self.args["disaggregation"] != None:
if self.args["disaggregation"] is not None:
self.disaggregated_network = self.network.copy()
else:
self.disaggregated_network = self.network.copy(with_time=False)
Expand Down
15 changes: 10 additions & 5 deletions etrago/cluster/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import os

if "READTHEDOCS" not in os.environ:
import logging

from pypsa import Network
from pypsa.networkclustering import (
aggregatebuses,
Expand All @@ -40,7 +42,8 @@
kmedoids_dijkstra_clustering,
sum_with_inf,
)
from etrago.tools.utilities import *

logger = logging.getLogger(__name__)

__copyright__ = (
"Flensburg University of Applied Sciences, "
Expand Down Expand Up @@ -101,7 +104,8 @@ def preprocessing(etrago):
)
]

# select buses dependent on whether they should be clustered in (only DE or DE+foreign)
# select buses dependent on whether they should be clustered in
# (only DE or DE+foreign)
if not settings["cluster_foreign_gas"]:
network_ch4.buses = network_ch4.buses.loc[
ch4_filter & (network_ch4.buses["country"].values == "DE")
Expand Down Expand Up @@ -344,8 +348,8 @@ def gas_postprocessing(etrago, busmap, medoid_idx=None):
+ str(settings["n_clusters_gas"])
+ "_result.csv"
)
if 'H2' in etrago.network.buses.carrier.unique():

if "H2" in etrago.network.buses.carrier.unique():
busmap = get_h2_clusters(etrago, busmap)

# Add all other buses to busmap
Expand Down Expand Up @@ -997,7 +1001,8 @@ def run_spatial_clustering_gas(self):
self.update_busmap(busmap)

logger.info(
"GAS Network clustered to {} DE-buses and {} foreign buses with {} algorithm.".format(
"""GAS Network clustered to {} DE-buses and {} foreign buses
with {} algorithm.""".format(
len(
self.network.buses.loc[
(self.network.buses.carrier == "CH4")
Expand Down
Loading

0 comments on commit 17e3eba

Please sign in to comment.