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

fix: correct bug where get_dcline_differences fails for DC lines connected to new buses #299

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 17 additions & 24 deletions postreise/analyze/transmission/upgrades.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from postreise.analyze.helpers import _check_data_frame
from postreise.analyze.check import _check_data_frame, _check_grid


def _reindex_as_necessary(df1, df2):
Expand Down Expand Up @@ -44,44 +44,37 @@ def get_branch_differences(branch1, branch2):
return branch_merge


def get_dcline_differences(dcline1, dcline2, bus):
def get_dcline_differences(grid1, grid2):
"""Calculate capacity differences between dcline tables, and add to/from lat/lon.

:param pandas.DataFrame dcline1: data frame containing Pmax, from_bus_id, to_bus_id.
:param pandas.DataFrame dcline2: data frame containing Pmax, from_bus_id, to_bus_id.
:param pandas.DataFrame bus: data frame containing lat & lon.
:raises ValueError: if any dataframe doesn't have required columns.
:param powersimdata.input.grid.Grid grid1: first grid instance.
:param powersimdata.input.grid.Grid grid2: second grid instance.
:return: (*pandas.DataFrame*) -- data frame with all indices, plus new columns:
diff, from_lat, from_lon, to_lat, to_lon.
"""
_check_data_frame(dcline1, "dcline1")
_check_data_frame(dcline2, "dcline2")
_check_data_frame(bus, "bus")
dcline_req_cols = {"Pmax", "from_bus_id", "to_bus_id"}
if not dcline_req_cols <= set(dcline1.columns):
raise ValueError(f"dcline1 must have columns: {dcline_req_cols}")
if not dcline_req_cols <= set(dcline2.columns):
raise ValueError(f"dcline2 must have columns: {dcline_req_cols}")
if not {"lat", "lon"} <= set(bus.columns):
raise ValueError("bus must have 'lat' & 'lon' columns")
dcline1, dcline2 = _reindex_as_necessary(dcline1, dcline2)
_check_grid(grid1)
_check_grid(grid2)
dcline1, dcline2 = _reindex_as_necessary(grid1.dcline, grid2.dcline)
# Get latitudes and longitudes for to & from buses
for dcline, grid in [(dcline1, grid1), (dcline2, grid2)]:
dcline["from_lat"] = grid.bus.loc[dcline.from_bus_id, "lat"].to_numpy()
dcline["from_lon"] = grid.bus.loc[dcline.from_bus_id, "lon"].to_numpy()
dcline["to_lat"] = grid.bus.loc[dcline.to_bus_id, "lat"].to_numpy()
dcline["to_lon"] = grid.bus.loc[dcline.to_bus_id, "lon"].to_numpy()
dc_merge = dcline1.merge(
dcline2, how="outer", right_index=True, left_index=True, suffixes=(None, "_2")
)
# fillna for bus ids, since some lines in one frame won't be in the other frame
dc_merge.fillna(
{
"from_bus_id": dc_merge.from_bus_id_2,
"to_bus_id": dc_merge.to_bus_id_2,
"from_lat": dc_merge.from_lat_2,
"from_lon": dc_merge.from_lon_2,
"to_lat": dc_merge.to_lat_2,
"to_lon": dc_merge.to_lon_2,
},
inplace=True,
)
# Calculate differences in Pmax
dc_merge["diff"] = dc_merge.Pmax_2.fillna(0) - dc_merge.Pmax.fillna(0)
# get lat lon for dclines
dc_merge["from_lon"] = bus.loc[dc_merge.from_bus_id, "lon"].values
dc_merge["from_lat"] = bus.loc[dc_merge.from_bus_id, "lat"].values
dc_merge["to_lon"] = bus.loc[dc_merge.to_bus_id, "lon"].values
dc_merge["to_lat"] = bus.loc[dc_merge.to_bus_id, "lat"].values

return dc_merge
2 changes: 1 addition & 1 deletion postreise/plot/plot_transmission_upgrades_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def map_transmission_upgrades(scenario1, scenario2, b2b_indices=None, **plot_kwa
grid1 = scenario1.state.get_grid()
grid2 = scenario2.state.get_grid()
branch_merge = get_branch_differences(grid1.branch, grid2.branch)
dc_merge = get_dcline_differences(grid1.dcline, grid2.dcline, grid1.bus)
dc_merge = get_dcline_differences(grid1, grid2)
map_plot = _map_transmission_upgrades(
branch_merge, dc_merge, b2b_indices, **plot_kwargs
)
Expand Down