diff --git a/app/ptxboa_functions.py b/app/ptxboa_functions.py index 31e5989d..8184292a 100644 --- a/app/ptxboa_functions.py +++ b/app/ptxboa_functions.py @@ -7,13 +7,14 @@ import streamlit as st from ptxboa.api import PtxboaAPI +from ptxboa.utils import is_test def calculate_results_single( _api: PtxboaAPI, settings: dict, user_data: pd.DataFrame | None = None, - optimize_flh: bool = False, + optimize_flh: bool = True, ) -> pd.DataFrame: """Calculate results for a single set of settings. @@ -88,6 +89,10 @@ def calculate_results_list( # copy settings from session_state: settings = {key: st.session_state[key] for key in setting_keys} + # in test environment: do not optimize by default + # NOTE: does not work in global, must be called here in a function + optimize_flh = not is_test() + # update settings from session state with custom values if override_session_state is not None: if not set(override_session_state.keys()).issubset(set(setting_keys)): @@ -109,10 +114,7 @@ def calculate_results_list( for parameter in parameter_list: settings.update({parameter_to_change: parameter}) # only optimize when using actual chosen parameter set: - if st.session_state[parameter_to_change] == parameter: - optimize_flh = True - else: - optimize_flh = False + res_single = calculate_results_single( api, settings, diff --git a/ptxboa/__init__.py b/ptxboa/__init__.py index dd395f09..4c1419a6 100644 --- a/ptxboa/__init__.py +++ b/ptxboa/__init__.py @@ -2,20 +2,13 @@ """Common Paths and settings.""" import logging import os -import warnings from pathlib import Path -# disable warnings for loading networks - -warnings.filterwarnings("ignore", category=DeprecationWarning) - - -IS_TEST = "PYTEST_CURRENT_TEST" in os.environ # TODO unused KEY_SEPARATOR = "," + # PATHS _THIS_DIR = Path(__file__).parent.resolve() - PROFILES_DIR = _THIS_DIR / ".." / "flh_opt" / "renewable_profiles" STATIC_DATA_DIR = _THIS_DIR / "static" DEFAULT_CACHE_DIR = Path(os.environ.get("PTXBOA_CACHE_DIR") or _THIS_DIR / "cache") diff --git a/ptxboa/api.py b/ptxboa/api.py index ce32cfaf..058c2f7e 100644 --- a/ptxboa/api.py +++ b/ptxboa/api.py @@ -121,7 +121,7 @@ def calculate( ship_own_fuel: bool, output_unit: OutputUnitType = "USD/MWh", user_data: pd.DataFrame | None = None, - optimize_flh: bool = False, + optimize_flh: bool = True, ) -> Tuple[pd.DataFrame, object]: """Calculate results based on user selection. diff --git a/ptxboa/utils.py b/ptxboa/utils.py index 6e47e9a3..c9975137 100644 --- a/ptxboa/utils.py +++ b/ptxboa/utils.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """Utilities.""" +import os + def annuity(rate: float, periods: int, value: float) -> float: """Calculate annuity. @@ -38,3 +40,10 @@ def __call__(cls, *args): if key not in cls._instances: cls._instances[key] = super().__call__(*args) return cls._instances[key] + + +def is_test(): + return ( + "PYTEST_CURRENT_TEST" in os.environ + or "STREAMLIT_GLOBAL_UNIT_TEST" in os.environ + ) diff --git a/ptxboa_streamlit.py b/ptxboa_streamlit.py index 12f79883..aa710229 100644 --- a/ptxboa_streamlit.py +++ b/ptxboa_streamlit.py @@ -8,7 +8,6 @@ __version__ = "0.4.2" -# TODO how do I use the treamlit logger? import logging import warnings @@ -44,6 +43,11 @@ r"through chained assignment using an inplace method.*" ), ) +warnings.filterwarnings( # filter DeprecationWarning for read network + action="ignore", + category=DeprecationWarning, +) + # setup logging # level can be changed on strartup with: --logger.level=LEVEL @@ -167,10 +171,7 @@ # calculate results over different data dimensions: costs_per_region = calculate_results_list( - api, - parameter_to_change="region", - parameter_list=None, - optimize_flh=False, # @markushal: use FLH optimizer disabled in region compare + api, parameter_to_change="region", parameter_list=None ) costs_per_scenario = calculate_results_list( api, diff --git a/tests/test_ptxboa_streamlit.py b/tests/test_ptxboa_streamlit.py index 068ae94b..8bbb37cf 100644 --- a/tests/test_ptxboa_streamlit.py +++ b/tests/test_ptxboa_streamlit.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Tests for the streamlit app.""" + import pytest from streamlit.testing.v1 import AppTest