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

Features/improve code style #658

Merged
merged 37 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5ea7a7f
Apply black
ClaraBuettner Sep 14, 2023
e5db91b
Apply isort
ClaraBuettner Sep 14, 2023
4a9bad7
Add updated files to noxfile
ClaraBuettner Sep 14, 2023
b115f51
Sort list of files alphabetically
ClaraBuettner Sep 14, 2023
b681867
Changes in docstring of run_etrago for flake8
ClaraBuettner Sep 14, 2023
2fe133d
Remove unused import
ClaraBuettner Sep 14, 2023
2287591
Fix flake8 warnings "line too long"
ClaraBuettner Sep 14, 2023
a528398
Fix flake8 E712
ClaraBuettner Sep 14, 2023
81590fb
Fix flake8 error
ClaraBuettner Sep 14, 2023
fa577fc
Apply black
ClaraBuettner Sep 19, 2023
28f5fbd
Remove unused import
ClaraBuettner Sep 19, 2023
4fb21bc
Fix flake8 errors due to comparision with None
ClaraBuettner Sep 19, 2023
8f74ae3
Manual break overly long lines
ClaraBuettner Sep 19, 2023
f7fecf9
Replace comparison with False
ClaraBuettner Sep 19, 2023
87b3d62
Remove empty comments
ClaraBuettner Sep 19, 2023
73bfb63
Exclude saio imports from flaxe8 checks
ClaraBuettner Sep 19, 2023
cbcbd50
Replace unavailable function
ClaraBuettner Sep 19, 2023
5a85f55
Delete unused variable
ClaraBuettner Sep 19, 2023
a0a408f
Fix flaxe8 issues
ClaraBuettner Sep 19, 2023
d493fb9
Break overly long lines
ClaraBuettner Sep 19, 2023
1fb8cfc
Fix number of whitespaces
ClaraBuettner Sep 19, 2023
fc683c5
Black
ClaraBuettner Sep 19, 2023
326906b
Solve flake8 errors
ClaraBuettner Sep 19, 2023
bb76745
Apply black
ClaraBuettner Sep 19, 2023
fef2eed
Fix flake8 errors
ClaraBuettner Sep 19, 2023
c945847
Fix flake8 errors
ClaraBuettner Sep 19, 2023
25372f3
Specify exception
ClaraBuettner Sep 19, 2023
075749a
Apply black
ClaraBuettner Sep 19, 2023
90785d5
Remove unused imports
ClaraBuettner Sep 19, 2023
3c96c01
Fix flake8 errors
ClaraBuettner Sep 19, 2023
e64585a
Fix flake8 error
ClaraBuettner Sep 19, 2023
f0a8141
Exclude appl.py from noxfile
ClaraBuettner Sep 19, 2023
76a0c69
Temporary exclude E722 and W605 from flake8 checks
ClaraBuettner Sep 19, 2023
dc09191
Apply black
ClaraBuettner Sep 19, 2023
a6634a6
Apply isort
ClaraBuettner Sep 19, 2023
55ba752
Fix comparisions to None and True
ClaraBuettner Sep 20, 2023
cefa07a
Merge branch 'dev' into features/improve-code-style
ClaraBuettner Sep 26, 2023
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
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