Skip to content

Commit

Permalink
adjusting readability
Browse files Browse the repository at this point in the history
  • Loading branch information
joda9 committed Sep 4, 2024
1 parent 5ae6934 commit 5c11cd0
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions edisgo/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,24 +2472,20 @@ def get_add_function(comp_type):
)
return add_func

def find_nearest_bus(geolocation, buses):
return geo.find_nearest_bus(geolocation, buses)

def connect_to_lv_bus(edisgo_object, target_bus, comp_type, comp_data):
return self._connect_to_lv_bus(
edisgo_object, target_bus, comp_type, comp_data
)

def handle_voltage_level_6():
substations = self.buses_df.loc[self.transformers_df.bus1.unique()]
if comp_type == "charging_point":
mv_buses = self.buses_df.loc[self.mv_grid.buses_df.index]
else:
mv_buses = pd.DataFrame()
substations = pd.concat([substations, mv_buses])
target_bus, target_bus_distance = find_nearest_bus(geolocation, substations)
target_bus, target_bus_distance = geo.find_nearest_bus(
geolocation, substations
)
if target_bus_distance > max_distance_from_target_bus:
bus = connect_to_lv_bus(edisgo_object, target_bus, comp_type, comp_data)
bus = self._connect_to_lv_bus(
edisgo_object, target_bus, comp_type, comp_data
)
else:
bus = target_bus
return bus
Expand Down Expand Up @@ -2543,49 +2539,46 @@ def handle_voltage_level_7():
mv_buses_masked = mv_buses[
mv_buses.distance == mv_buses.distance.min()
]
target_bus = mv_buses_masked.loc[mv_buses_masked.distance.idxmin()]
return target_bus.name
else:
target_bus = connect_to_lv_bus(
target_bus = self._connect_to_lv_bus(
edisgo_object, lv_buses.distance.idxmin(), comp_type, comp_data
)
lv_buses_masked = pd.DataFrame(self.buses_df.loc[target_bus]).T
lv_buses_masked["distance"] = 0
mv_buses_masked = pd.DataFrame()
else:
mv_buses_masked = pd.DataFrame()
return target_bus
comp_df = {
"charging_point": self.charging_points_df,
"generator": self.generators_df,
"heat_pump": self.loads_df[self.loads_df.type == "heat_pump"],
"storage_unit": self.storage_units_df,
}.get(comp_type)

comp_type_counts = (
comp_df.loc[comp_df.bus.isin(lv_buses_masked.index)]
.groupby("bus")
.size()
)
lv_buses_masked.loc[:, "num_comps"] = (
lv_buses_masked.index.map(comp_type_counts).fillna(0).astype(int)
)
lv_buses_masked = lv_buses_masked[
lv_buses_masked.num_comps == lv_buses_masked.num_comps.min()
]

if not mv_buses_masked.empty:
target_bus = mv_buses_masked.loc[mv_buses_masked.distance.idxmin()]
else:
comp_df = {
"charging_point": self.charging_points_df,
"generator": self.generators_df,
"heat_pump": self.loads_df[self.loads_df.type == "heat_pump"],
"storage_unit": self.storage_units_df,
}.get(comp_type)

comp_type_counts = (
comp_df.loc[comp_df.bus.isin(lv_buses_masked.index)]
.groupby("bus")
.size()
)
lv_buses_masked.loc[:, "num_comps"] = (
lv_buses_masked.index.map(comp_type_counts).fillna(0).astype(int)
if lv_buses_masked.num_comps.min() >= allowed_number_of_comp_per_bus:
target_bus = self._connect_to_lv_bus(
edisgo_object, lv_buses.distance.idxmin(), comp_type, comp_data
)
lv_buses_masked = lv_buses_masked[
lv_buses_masked.num_comps == lv_buses_masked.num_comps.min()
]

if lv_buses_masked.num_comps.min() >= allowed_number_of_comp_per_bus:
target_bus = connect_to_lv_bus(
edisgo_object, lv_buses.distance.idxmin(), comp_type, comp_data
)
lv_buses_masked = pd.DataFrame(self.buses_df.loc[target_bus]).T
lv_buses_masked["distance"] = 0
mv_buses_masked = pd.DataFrame()
lv_buses_masked = pd.DataFrame(self.buses_df.loc[target_bus]).T
lv_buses_masked["distance"] = 0
mv_buses_masked = pd.DataFrame()

target_bus = lv_buses_masked.loc[lv_buses_masked.distance.idxmin()]
if isinstance(target_bus, pd.DataFrame):
target_bus = target_bus.iloc[0]
target_bus = lv_buses_masked.loc[lv_buses_masked.distance.idxmin()]
if isinstance(target_bus, pd.DataFrame):
target_bus = target_bus.iloc[0]
return target_bus.name

# Ensure 'p' is in comp_data, defaulting to 'p_set' or 'p_nom'
Expand Down

0 comments on commit 5c11cd0

Please sign in to comment.