From 74a5c5dd06e18d42c7f0f457e4b9912d3ff08e0c Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 21 May 2021 19:18:50 -0700 Subject: [PATCH 1/2] refactor: change call signature for get_dcline_differences --- postreise/analyze/transmission/upgrades.py | 41 +++++++++------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/postreise/analyze/transmission/upgrades.py b/postreise/analyze/transmission/upgrades.py index c14cf3b9..4c803deb 100644 --- a/postreise/analyze/transmission/upgrades.py +++ b/postreise/analyze/transmission/upgrades.py @@ -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): @@ -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 From 944ac01967a7b6d2a91f3ae0fb292590ab4c3556 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 21 May 2021 19:21:13 -0700 Subject: [PATCH 2/2] refactor: update call signature for get_dcline_differences in map_transmission_upgrades --- postreise/plot/plot_transmission_upgrades_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postreise/plot/plot_transmission_upgrades_map.py b/postreise/plot/plot_transmission_upgrades_map.py index e59d1943..40267c01 100644 --- a/postreise/plot/plot_transmission_upgrades_map.py +++ b/postreise/plot/plot_transmission_upgrades_map.py @@ -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 )