Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:agoenergy/ptx-boa into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wingechr committed May 14, 2024
2 parents 8960e1c + 6dc6ea4 commit 851e267
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
12 changes: 7 additions & 5 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)):
Expand All @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions ptxboa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion ptxboa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions ptxboa/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""Utilities."""

import os


def annuity(rate: float, periods: int, value: float) -> float:
"""Calculate annuity.
Expand Down Expand Up @@ -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
)
11 changes: 6 additions & 5 deletions ptxboa_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

__version__ = "0.5.0"

# TODO how do I use the treamlit logger?
import logging
import warnings

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/test_ptxboa_streamlit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Tests for the streamlit app."""

import pytest
from streamlit.testing.v1 import AppTest

Expand Down

0 comments on commit 851e267

Please sign in to comment.