From f991a2893ac4ec4eccc7a44c248a29c9ffa958c1 Mon Sep 17 00:00:00 2001 From: Jen Hagg Date: Thu, 1 Sep 2022 16:12:14 -0700 Subject: [PATCH] perf: reduce unnecessary object creation --- powersimdata/input/change_table.py | 4 ++++ powersimdata/input/changes/demand_flex.py | 4 +--- powersimdata/input/tests/test_change_table.py | 9 +++++---- powersimdata/input/tests/test_input_data.py | 8 +++++--- powersimdata/scenario/analyze.py | 4 ++-- powersimdata/scenario/create.py | 3 ++- powersimdata/tests/mock_context.py | 3 ++- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/powersimdata/input/change_table.py b/powersimdata/input/change_table.py index 5fab9b42a..16d0dd125 100644 --- a/powersimdata/input/change_table.py +++ b/powersimdata/input/change_table.py @@ -16,6 +16,7 @@ remove_plant, scale_plant_pmin, ) +from powersimdata.input.profile_input import ProfileInput from powersimdata.input.transform_grid import TransformGrid _resources = ( @@ -148,6 +149,7 @@ def __init__(self, grid): "demand_flexibility", } } + self._profile_input = None @staticmethod def _check_resource(resource): @@ -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): diff --git a/powersimdata/input/changes/demand_flex.py b/powersimdata/input/changes/demand_flex.py index 4b3fd66e7..a83c9597e 100644 --- a/powersimdata/input/changes/demand_flex.py +++ b/powersimdata/input/changes/demand_flex.py @@ -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. @@ -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: diff --git a/powersimdata/input/tests/test_change_table.py b/powersimdata/input/tests/test_change_table.py index 04df52571..39e8f53b6 100644 --- a/powersimdata/input/tests/test_change_table.py +++ b/powersimdata/input/tests/test_change_table.py @@ -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( @@ -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}" diff --git a/powersimdata/input/tests/test_input_data.py b/powersimdata/input/tests/test_input_data.py index eeaf42a92..9bca6ee94 100644 --- a/powersimdata/input/tests/test_input_data.py +++ b/powersimdata/input/tests/test_input_data.py @@ -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): diff --git a/powersimdata/scenario/analyze.py b/powersimdata/scenario/analyze.py index 95b3e1b06..0e316b86e 100644 --- a/powersimdata/scenario/analyze.py +++ b/powersimdata/scenario/analyze.py @@ -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( @@ -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. diff --git a/powersimdata/scenario/create.py b/powersimdata/scenario/create.py index e70b54435..35b88f13e 100644 --- a/powersimdata/scenario/create.py +++ b/powersimdata/scenario/create.py @@ -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() @@ -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. diff --git a/powersimdata/tests/mock_context.py b/powersimdata/tests/mock_context.py index 46f2a1840..fe0b287ce 100644 --- a/powersimdata/tests/mock_context.py +++ b/powersimdata/tests/mock_context.py @@ -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