Skip to content

Commit

Permalink
fixing logging level and option issues (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lucas Boucinha <[email protected]>
Co-authored-by: Revathy Venugopal <[email protected]>
  • Loading branch information
4 people authored Jan 24, 2023
1 parent f3aa544 commit a1062f5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ansys/pytwin/evaluate/twin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def _create_dataframe_inputs(self, inputs_df: pd.DataFrame):

@staticmethod
def _get_runtime_log_level():
if not pytwin_logging_is_enabled():
return LogLevel.TWIN_NO_LOG
pytwin_level = get_pytwin_log_level()
if pytwin_level == PyTwinLogLevel.PYTWIN_LOG_DEBUG:
return LogLevel.TWIN_LOG_ALL
Expand All @@ -125,8 +127,6 @@ def _get_runtime_log_level():
return LogLevel.TWIN_LOG_ERROR
if pytwin_level == PyTwinLogLevel.PYTWIN_LOG_CRITICAL:
return LogLevel.TWIN_LOG_FATAL
if not pytwin_logging_is_enabled():
return LogLevel.TWIN_NO_LOG

def _initialize_evaluation(self, parameters: dict = None, inputs: dict = None):
"""
Expand Down Expand Up @@ -224,7 +224,9 @@ def _instantiate_twin_model(self):
self._model_name = self._twin_runtime.twin_get_model_name()
if not os.path.exists(self.model_dir):
os.mkdir(self.model_dir)
os.link(self.model_log, self.model_log_link)
# Create link to log file if any
if os.path.exists(self.model_log):
os.link(self.model_log, self.model_log_link)

# Update TwinModel variables
self._instantiation_time = time.time()
Expand Down
2 changes: 2 additions & 0 deletions src/ansys/pytwin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ def modify_logging(new_option: PyTwinLogOption, new_level: PyTwinLogLevel):
# Modifications in case of new level
if new_level is not None:
if new_level != _PyTwinSettings.LOGGING_LEVEL:
# fix ACE bug on logging level Jan 18th 2023 (test_modify_logging_level)
_PyTwinSettings.LOGGING_LEVEL = new_level
pytwin_logger.setLevel(new_level.value)
for handler in pytwin_logger.handlers:
handler.setLevel(new_level.value)
Expand Down
36 changes: 36 additions & 0 deletions tests/evaluate/test_twin_model_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

from pytwin import PYTWIN_LOGGING_OPT_NOLOGGING, TwinModel
from pytwin.settings import get_pytwin_log_file, modify_pytwin_logging

COUPLE_CLUTCHES_FILEPATH = os.path.join(os.path.dirname(__file__), "data", "CoupleClutches_22R2_other.twin")
UNIT_TEST_WD = os.path.join(os.path.dirname(__file__), "unit_test_wd")


def reinit_settings():
import shutil

from pytwin.settings import reinit_settings_for_unit_tests

reinit_settings_for_unit_tests()
if os.path.exists(UNIT_TEST_WD):
shutil.rmtree(UNIT_TEST_WD)
return UNIT_TEST_WD


class TestTwinModelLogging:
def test_twin_model_no_logging(self):
# Init unit test
reinit_settings()
# Twin Model does not log anything if PYTWIN_LOGGING_OPT_NOLOGGING
modify_pytwin_logging(new_option=PYTWIN_LOGGING_OPT_NOLOGGING)
log_file = get_pytwin_log_file()
assert log_file is None
twin = TwinModel(model_filepath=COUPLE_CLUTCHES_FILEPATH)
twin.initialize_evaluation()
for i in range(100):
new_inputs = {"Clutch1_in": 1.0 * i / 100, "Clutch2_in": 1.0 * i / 100}
twin.evaluate_step_by_step(step_size=0.01, inputs=new_inputs)
temp_dir = twin.model_temp
assert os.path.exists(temp_dir)
assert len(os.listdir(temp_dir)) == 0
17 changes: 17 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@ def test_modify_logging_raises_error(self):
assert "Error while setting pytwin logging options!" in str(e)

def test_modify_logging_no_logging(self):
from pytwin import TwinModel
from pytwin.twin_runtime.log_level import LogLevel

# Init unit test
reinit_settings()
assert pytwin_logging_is_enabled()
# Disable logging
modify_pytwin_logging(new_option=PyTwinLogOption.PYTWIN_LOGGING_OPT_NOLOGGING)
level = TwinModel._get_runtime_log_level()
logger = get_pytwin_logger()
log_file = get_pytwin_log_file()
assert len(logger.handlers) == 0
assert log_file is None
assert not pytwin_logging_is_enabled()
assert level == LogLevel.TWIN_NO_LOG

def test_modify_logging_console(self):
# Init unit test
Expand All @@ -92,10 +97,16 @@ def test_modify_logging_console(self):
assert pytwin_logging_is_enabled()

def test_modify_logging_level(self):
from pytwin import TwinModel
from pytwin.settings import get_pytwin_log_level
from pytwin.twin_runtime.log_level import LogLevel

# Init unit test
reinit_settings()
# Modify logging level works
modify_pytwin_logging(new_level=PyTwinLogLevel.PYTWIN_LOG_CRITICAL)
level = get_pytwin_log_level()
runtime_level = TwinModel._get_runtime_log_level()
log_file = get_pytwin_log_file()
logger = get_pytwin_logger()
logger.debug("Hello 10")
Expand All @@ -106,8 +117,12 @@ def test_modify_logging_level(self):
with open(log_file, "r") as f:
lines = f.readlines()
assert len(lines) == 1
assert level == PyTwinLogLevel.PYTWIN_LOG_CRITICAL
assert runtime_level == LogLevel.TWIN_LOG_FATAL
# Modify logging level can be done dynamically
modify_pytwin_logging(new_level=PyTwinLogLevel.PYTWIN_LOG_DEBUG)
level = get_pytwin_log_level()
runtime_level = TwinModel._get_runtime_log_level()
logger.debug("Hello 10")
logger.info("Hello 20")
logger.warning("Hello 30")
Expand All @@ -116,6 +131,8 @@ def test_modify_logging_level(self):
with open(log_file, "r") as f:
lines = f.readlines()
assert len(lines) == 5 + 1
assert level == PyTwinLogLevel.PYTWIN_LOG_DEBUG
assert runtime_level == LogLevel.TWIN_LOG_ALL

def test_modify_logging_multiple_times(self):
# Init unit test
Expand Down

0 comments on commit a1062f5

Please sign in to comment.