Skip to content

Commit

Permalink
perf: reduce unnecessary object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jenhagg committed Sep 1, 2022
1 parent f3b3bfb commit f991a28
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions powersimdata/input/change_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
remove_plant,
scale_plant_pmin,
)
from powersimdata.input.profile_input import ProfileInput
from powersimdata.input.transform_grid import TransformGrid

_resources = (
Expand Down Expand Up @@ -148,6 +149,7 @@ def __init__(self, grid):
"demand_flexibility",
}
}
self._profile_input = None

@staticmethod
def _check_resource(resource):
Expand Down Expand Up @@ -478,6 +480,8 @@ def add_demand_flexibility(self, info):
See :func:`powersimdata.input.changes.demand_flex.add_demand_flexibility`
"""
if self._profile_input is None:
self._profile_input = ProfileInput()
add_demand_flexibility(self, info)

def add_electrification(self, kind, info):
Expand Down
4 changes: 1 addition & 3 deletions powersimdata/input/changes/demand_flex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import copy

from powersimdata.input.profile_input import ProfileInput


def add_demand_flexibility(obj, info):
"""Adds demand flexibility to the system.
Expand Down Expand Up @@ -46,7 +44,7 @@ def add_demand_flexibility(obj, info):
obj.ct["demand_flexibility"][k] = info[k]
else:
# Determine the available profile versions
possible = ProfileInput().get_profile_version(obj.grid.grid_model, k)
possible = obj._profile_input.get_profile_version(obj.grid.grid_model, k)

# Add the profile to the change table
if len(possible) == 0:
Expand Down
9 changes: 5 additions & 4 deletions powersimdata/input/tests/test_change_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,11 @@ def test_remove_bus(ct):
ct.remove_bus({845})


def test_add_demand_flexibility(ct, monkeypatch):
def test_add_demand_flexibility(monkeypatch):
monkeypatch.setattr(Context, "get_data_access", MockContext().get_data_access)
data_access = Context.get_data_access()
ct = ChangeTable(grid)

with pytest.raises(ValueError):
# Fails because "demand_flexibility_dn", a required key, is not included
ct.add_demand_flexibility(
Expand Down Expand Up @@ -538,9 +542,6 @@ def test_add_demand_flexibility(ct, monkeypatch):
}
)

monkeypatch.setattr(Context, "get_data_access", MockContext().get_data_access)
data_access = Context.get_data_access()

# Create fake files in the expected directory path
exp_path = f"raw/{grid.grid_model}"

Expand Down
8 changes: 5 additions & 3 deletions powersimdata/input/tests/test_input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

from powersimdata.input.input_data import InputData

_input_data = InputData()


def test_get_file_components():
s_info = {"id": "123"}
ct_file = InputData()._get_file_path(s_info, "ct")
grid_file = InputData()._get_file_path(s_info, "grid")
ct_file = _input_data._get_file_path(s_info, "ct")
grid_file = _input_data._get_file_path(s_info, "grid")
assert "data/input/123_ct.pkl" == ct_file
assert "data/input/123_grid.mat" == grid_file


def test_check_field():
_check_field = InputData()._check_field
_check_field = _input_data._check_field
_check_field("grid")
_check_field("ct")
with pytest.raises(ValueError):
Expand Down
4 changes: 2 additions & 2 deletions powersimdata/scenario/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Analyze(Ready):
def __init__(self, scenario):
"""Constructor."""
super().__init__(scenario)

self.refresh(scenario)
self._output_data = OutputData()

def refresh(self, scenario):
print(
Expand Down Expand Up @@ -112,7 +112,7 @@ def print_infeasibilities(self):
)

def _get_data(self, field):
return OutputData().get_data(self._scenario_info["id"], field)
return self._output_data.get_data(self._scenario_info["id"], field)

def get_pg(self):
"""Returns PG data frame.
Expand Down
3 changes: 2 additions & 1 deletion powersimdata/scenario/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def __init__(self, grid_model, interconnect, table, **kwargs):

self.exported_methods |= {"set_base_profile", "get_base_profile"}

self._profile_input = ProfileInput()
self.print_existing_study()
self.print_available_profile()

Expand Down Expand Up @@ -390,7 +391,7 @@ def get_base_profile(self, kind):
:param str kind: one of *'demand'*, *'hydro'*, *'solar'*, *'wind'*.
:return: (*list*) -- available version for selected profile kind.
"""
return ProfileInput().get_profile_version(self.grid_model, kind)
return self._profile_input.get_profile_version(self.grid_model, kind)

def set_base_profile(self, kind, version):
"""Set base profile.
Expand Down
3 changes: 2 additions & 1 deletion powersimdata/tests/mock_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from powersimdata.data_access.data_access import TempDataAccess, get_blob_fs
from powersimdata.data_access.data_access import TempDataAccess
from powersimdata.data_access.fs_helper import get_blob_fs
from powersimdata.utility import templates


Expand Down

0 comments on commit f991a28

Please sign in to comment.