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

refactor: remove redundant function for getting bus demand #388

Merged
merged 5 commits into from
Feb 5, 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
6 changes: 3 additions & 3 deletions powersimdata/input/input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def _read_data(filepath):
return data


def get_bus_demand(scenario_id, grid):
def get_bus_demand(scenario_info, grid):
"""Returns demand profiles by bus.

:param str scenario_id: scenario id.
:param dict scenario_info: scenario information.
:param powersimdata.input.grid.Grid grid: grid to construct bus demand for.
:return: (*pandas.DataFrame*) -- data frame of demand.
"""
demand = InputData().get_data(scenario_id, "demand")
demand = InputData().get_data(scenario_info, "demand")
bus = grid.bus
bus["zone_Pd"] = bus.groupby("zone_id")["Pd"].transform("sum")
bus["zone_share"] = bus["Pd"] / bus["zone_Pd"]
Expand Down
2 changes: 1 addition & 1 deletion powersimdata/output/output_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def construct_load_shed(scenario_info, grid, infeasibilities=None):
load_shed = pd.DataFrame.sparse.from_spmatrix(load_shed_data)
else:
print("Infeasibilities, constructing DataFrame")
bus_demand = get_bus_demand(scenario_info["id"], grid)
bus_demand = get_bus_demand(scenario_info, grid)
load_shed = np.zeros((len(hours), len(buses)))
# Convert '24H' to 24
interval = int(scenario_info["interval"][:-1])
Expand Down
7 changes: 2 additions & 5 deletions powersimdata/scenario/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import pandas as pd

from powersimdata.input.grid import Grid
from powersimdata.input.input_data import InputData
from powersimdata.input.input_data import InputData, get_bus_demand
from powersimdata.input.transform_profile import TransformProfile
from powersimdata.output.output_data import OutputData, construct_load_shed
from powersimdata.scenario.helpers import calculate_bus_demand
from powersimdata.scenario.state import State
from powersimdata.utility import server_setup

Expand Down Expand Up @@ -275,10 +274,8 @@ def get_bus_demand(self):

:return: (*pandas.DataFrame*) -- data frame of demand (hour, bus).
"""
profile = TransformProfile(self._scenario_info, self.get_grid(), self.get_ct())
demand = profile.get_profile("demand")
grid = self.get_grid()
return calculate_bus_demand(grid.bus, demand)
return get_bus_demand(self._scenario_info, grid)

def get_hydro(self):
"""Returns hydro profile
Expand Down
10 changes: 3 additions & 7 deletions powersimdata/scenario/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
from powersimdata.data_access.scenario_list import ScenarioListManager
from powersimdata.input.change_table import ChangeTable
from powersimdata.input.grid import Grid
from powersimdata.input.input_data import get_bus_demand
from powersimdata.input.transform_grid import TransformGrid
from powersimdata.input.transform_profile import TransformProfile
from powersimdata.scenario.execute import Execute
from powersimdata.scenario.helpers import (
calculate_bus_demand,
check_interconnect,
interconnect2name,
)
from powersimdata.scenario.helpers import check_interconnect, interconnect2name
from powersimdata.scenario.state import State
from powersimdata.utility import server_setup

Expand Down Expand Up @@ -153,9 +150,8 @@ def get_bus_demand(self):

:return: (*pandas.DataFrame*) -- data frame of demand (hour, bus).
"""
demand = self.get_profile("demand")
grid = self.get_grid()
return calculate_bus_demand(grid.bus, demand)
return get_bus_demand(self._scenario_info, grid)

def get_hydro(self):
"""Returns hydro profile.
Expand Down
26 changes: 0 additions & 26 deletions powersimdata/scenario/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import numpy as np
import pandas as pd


def check_interconnect(interconnect):
"""Sets interconnect.

Expand Down Expand Up @@ -46,25 +42,3 @@ def interconnect2name(interconnect):
return "easternwestern"
else:
return "usa"


def calculate_bus_demand(bus, demand):
"""Calculates bus-level demand from zone-level demand.

:param pandas.DataFrame bus: bus data frame.
:param pandas.DataFrame demand: demand data frame.
:return: (*pandas.DataFrame*) -- dataframe of (hour, bus) demand.
"""
zone_demand = bus.groupby("zone_id").sum().Pd
bus_zone_share = bus.apply(lambda x: x.Pd / zone_demand.loc[x.zone_id], axis=1)
bus_to_zone = np.zeros((len(zone_demand), len(bus)))
bus_idx_lookup = {b: i for i, b in enumerate(bus.index)}
zone_idx_lookup = {z: i for i, z in enumerate(zone_demand.index)}
for b in bus.index:
bus_idx = bus_idx_lookup[b]
zone_idx = zone_idx_lookup[bus.loc[b, "zone_id"]]
bus_to_zone[zone_idx, bus_idx] = bus_zone_share.loc[b]
bus_demand = pd.DataFrame(
(demand.to_numpy() @ bus_to_zone), index=demand.index, columns=bus.index
)
return bus_demand