Skip to content

Commit

Permalink
create function include_busmap_area
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosEpia committed Nov 14, 2023
1 parent 4acd371 commit 06e3476
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
8 changes: 4 additions & 4 deletions etrago/appl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
"method": "kmedoids-dijkstra", # choose clustering method: kmeans or kmedoids-dijkstra
"n_clusters_AC": 30, # total number of resulting AC nodes (DE+foreign)
"cluster_foreign_AC": False, # take foreign AC buses into account, True or False
"exclusion_area": False, # False, path to shapefile or list of nuts names of not cluster area
"cluster_exclusion_area": False, # False or number of buses.
"interest_area": False, # False, path to shapefile or list of nuts names of not cluster area
"cluster_interest_area": False, # False or number of buses.
"method_gas": "kmedoids-dijkstra", # choose clustering method: kmeans or kmedoids-dijkstra
"n_clusters_gas": 17, # total number of resulting CH4 nodes (DE+foreign)
"cluster_foreign_gas": False, # take foreign CH4 buses into account, True or False
Expand Down Expand Up @@ -440,15 +440,15 @@ def run_etrago(args, json_path):
as well and included in number of clusters specified through
``'n_clusters_AC'``.
Default: False.
* "exclusion_area": False, list, string
* "interest_area": False, list, string
Area of especial interest that will be not clustered. It is by
default set to false. When an exclusion area is provided, the given
value for n_clusters_AC will mean the total of AC buses outside the
area.The areas can be provided in two ways: list of
nuts names e.G. ["Cuxhaven", "Bremerhaven", "Bremen"] or a string
with a path to a shape file.
Default: False.
* "cluster_exclusion_area": False, int
* "cluster_interest_area": False, int
Number of buses to cluster all the electrical buses in the
exclusion area. Method provided in the arg "method" is used.
Default: False.
Expand Down
80 changes: 51 additions & 29 deletions etrago/cluster/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def select_elec_network(etrago):
number of clusters used in the clustering process.
area_network : pypsa.Network
Contains the electric network in the area of interest defined in
network_clustering - exclusion_area.
network_clustering - interest_area.
"""
settings = etrago.args["network_clustering"]

Expand Down Expand Up @@ -1054,6 +1054,49 @@ def calc_availability_factor(gen):
return weight


def include_busmap_area(etrago, busmap, medoid_idx, network_area, weight_area):
args = etrago.args["network_clustering"]

if not args["interest_area"]:
return busmap, medoid_idx
if not args["cluster_interest_area"]:
for bus in network_area.buses.index:
busmap[bus] = bus
return busmap, medoid_idx
else:
if args["method"] == "kmeans":
busmap_area = kmean_clustering(
etrago,
network_area,
weight_area,
args["cluster_interest_area"],
)
busmap_area = (
busmap_area.astype(int) + busmap.apply(int).max() + 1
).apply(str)

if args["method"] == "kmedoids-dijkstra":
busmap_area, medoid_idx_area = kmedoids_dijkstra_clustering(
etrago,
network_area.buses,
network_area.lines,
weight_area,
args["cluster_interest_area"],
)

medoid_idx_area.index = (
medoid_idx_area.index.astype(int) + busmap.apply(int).max() + 1
)
busmap_area = (
busmap_area.astype(int) + busmap.apply(int).max() + 1
).apply(str)
medoid_idx = pd.concat([medoid_idx, medoid_idx_area])

busmap = pd.concat([busmap, busmap_area])

return busmap, medoid_idx


def run_spatial_clustering(self):
"""
Main method for running spatial clustering on the electrical network.
Expand Down Expand Up @@ -1091,17 +1134,11 @@ def run_spatial_clustering(self):
busmap_elec = kmean_clustering(
self, elec_network, weight, n_clusters
)
busmap_area = kmean_clustering(
self,
network_area,
weight_area,
self.args["network_clustering"]["cluster_exclusion_area"],
)
busmap_area = (
busmap_area.astype(int) + busmap_elec.apply(int).max() + 1
).apply(str)
busmap = pd.concat([busmap_elec, busmap_area])
medoid_idx = pd.Series(dtype=str)
busmap, medoid_idx = include_busmap_area(
self, busmap_elec, medoid_idx, network_area, weight_area
)

else:
busmap = pd.Series(dtype=str)
medoid_idx = pd.Series(dtype=str)
Expand All @@ -1110,32 +1147,17 @@ def run_spatial_clustering(self):
if not self.args["network_clustering"]["k_elec_busmap"]:
logger.info("Start k-medoids Dijkstra Clustering")

busmap_elec, medoid_idx_elec = kmedoids_dijkstra_clustering(
busmap_elec, medoid_idx = kmedoids_dijkstra_clustering(
self,
elec_network.buses,
elec_network.lines,
weight,
n_clusters,
)

busmap_area, medoid_idx_area = kmedoids_dijkstra_clustering(
self,
network_area.buses,
network_area.lines,
weight_area,
self.args["network_clustering"]["cluster_exclusion_area"],
)

medoid_idx_area.index = (
medoid_idx_area.index.astype(int)
+ busmap_elec.apply(int).max()
+ 1
busmap, medoid_idx = include_busmap_area(
self, busmap_elec, medoid_idx, network_area, weight_area
)
busmap_area = (
busmap_area.astype(int) + busmap_elec.apply(int).max() + 1
).apply(str)
busmap = pd.concat([busmap_elec, busmap_area])
medoid_idx = pd.concat([medoid_idx_elec, medoid_idx_area])

else:
busmap = pd.Series(dtype=str)
Expand Down
12 changes: 6 additions & 6 deletions etrago/cluster/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,20 +779,20 @@ def find_buses_area(etrago, carrier):
"""
settings = etrago.args["network_clustering"]

if settings["exclusion_area"]:
if isinstance(settings["exclusion_area"], list):
if settings["interest_area"]:
if isinstance(settings["interest_area"], list):
con = etrago.engine
query = "SELECT gen, geometry FROM boundaries.vg250_krs"

de_areas = gpd.read_postgis(query, con, geom_col="geometry")
de_areas = de_areas[
de_areas["gen"].isin(settings["exclusion_area"])
de_areas["gen"].isin(settings["interest_area"])
]
elif isinstance(settings["exclusion_area"], str):
de_areas = gpd.read_file(settings["exclusion_area"])
elif isinstance(settings["interest_area"], str):
de_areas = gpd.read_file(settings["interest_area"])
else:
raise Exception(
"not supported format supplied to 'exclusion_area' argument"
"not supported format supplied to 'interest_area' argument"
)

try:
Expand Down

0 comments on commit 06e3476

Please sign in to comment.