From eabe24962039f323ecb05a326dd975d7cab50582 Mon Sep 17 00:00:00 2001 From: Davide Laghi Date: Tue, 6 Aug 2024 17:42:38 +0200 Subject: [PATCH 1/4] custom log substitued with logging class --- jade/computational.py | 30 ++++++-------- jade/configuration.py | 81 ++----------------------------------- jade/constants.py | 15 +++++++ jade/excelsupport.py | 2 +- jade/exceptions.py | 8 +--- jade/gui.py | 75 +++++++++++++++------------------- jade/main.py | 19 ++++++++- jade/output.py | 6 +-- jade/postprocess.py | 41 +++++++++++-------- jade/status.py | 7 ++-- jade/testrun.py | 15 +------ tests/configuration_test.py | 33 +-------------- tests/excelsupport_test.py | 2 +- tests/testrun_test.py | 26 +++++------- 14 files changed, 128 insertions(+), 232 deletions(-) diff --git a/jade/computational.py b/jade/computational.py index 737a0200..93354b1a 100644 --- a/jade/computational.py +++ b/jade/computational.py @@ -27,6 +27,7 @@ import sys import jade.testrun as testrun +import logging def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> None: @@ -57,8 +58,6 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N config = session.conf.exp_default.set_index("Description") else: config = session.conf.comp_default.set_index("Description") - # Get the log - log = session.log for testname, row in config.iterrows(): # Check for active test first @@ -98,11 +97,8 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N print(" -- " + testname.upper() + " STARTED --\n") print(" Generating input files:" + " " + str(datetime.datetime.now())) - log.adjourn( - testname.upper() - + " run started" - + " " - + str(datetime.datetime.now()) + logging.info( + "%s run started %s", testname.upper(), str(datetime.datetime.now()) ) # --- Input Generation --- @@ -138,7 +134,7 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N # Generate test lib_name = session.conf.get_lib_name(var) - args = (inppath, var, row, log, confpath, runoption, lib_name) + args = (inppath, var, row, confpath, runoption, lib_name) # Handle special cases if testname == "Sphere Leakage Test": test = testrun.SphereTest(*args) @@ -177,11 +173,10 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N else: test.generate_test(outpath, libmanager) # Adjourn log - log.adjourn( - testname.upper() - + " test input generated with success" - + " " - + str(datetime.datetime.now()) + logging.info( + "%s test input generated with success %s", + testname.upper(), + str(datetime.datetime.now()), ) if bool(row["OnlyInput"]): @@ -193,11 +188,10 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N test.run(session.conf, session.lib_manager, runoption) print("\n -- " + testname.upper() + " COMPLETED --\n") # Adjourn log - log.adjourn( - testname.upper() - + " run completed with success" - + " " - + str(datetime.datetime.now()) + logging.info( + "%s run completed with success %s", + testname.upper(), + str(datetime.datetime.now()), ) diff --git a/jade/configuration.py b/jade/configuration.py index 8a212286..4ec9f2b3 100644 --- a/jade/configuration.py +++ b/jade/configuration.py @@ -166,7 +166,9 @@ def run_option(self, exp=False) -> str: print( " Cannot submit as a batch job, as no batch system has been defined in the config file." ) - elif (pd.isnull(self.mpi_exec_prefix)) and (pd.isnull(self.mpi_tasks) is not True): + elif (pd.isnull(self.mpi_exec_prefix)) and ( + pd.isnull(self.mpi_tasks) is not True + ): if int(self.mpi_tasks) > 1: print( " Cannot submit batch job as MPI, as no MPI executable prefix has been defined in the config file." @@ -206,80 +208,3 @@ def get_lib_name(self, suffix: str) -> str: except KeyError: name = suffix return name - - -jade = """ - 8888888 oo 888oo o8888o - 88 o8 8o 88 8oo 88 8 - 88 o8 8o 88 8o 88 8 - 88 o8=====8o 88 8o 88====° - 88 88 o8 8o 88 8oo 8o - °°8888 o8 8o 888oo oo88888 -""" - -initialtext = """ - Welcome to JADE, a nuclear libraries V&V test suite - - This is the log file, all main events will be recorded here -------------------------------------------------------------------------------- -############################################################################### -""" - -bar = """ -############################################################################## -""" - - -class Log: - """ - This object stores important milestones of the session - """ - - def __init__(self, logfile): - """ - Initialize the log file - - logfile: (str) path to logfile - """ - # outpath for log file - self.file = logfile - - self.text = jade + initialtext - - def adjourn(self, text, spacing=True, time=False): - """ - Adjourn the log file - """ - if spacing: - text = "\n\n" + text.strip("\n") - self.text = self.text + text - - if time: - self.text = self.text + " " + str(datetime.datetime.now()) - - with open(self.file, "w") as outfile: - outfile.write(self.text) - - def bar_adjourn(self, text, spacing=True): - """ - Adjourn the log file with hashtag style - """ - if len(text) > 75: # There is no space, the bar request is ignored - self.text = self.text + text - else: - hashlen = (len(bar) - len(text)) / 2 - if hashlen % 1 != 0: - after = int(hashlen - 1) - else: - after = int(hashlen) - before = int(hashlen) - - bartext = bar[: before - 1] + " " + text + " " + bar[-after + 1 :] - - if spacing: - bartext = "\n\n" + bartext - - self.text = self.text + bartext - - with open(self.file, "w") as outfile: - outfile.write(self.text) diff --git a/jade/constants.py b/jade/constants.py index 599f8c22..8e1edc88 100644 --- a/jade/constants.py +++ b/jade/constants.py @@ -21,3 +21,18 @@ """ CODES = {"MCNP": "mcnp", "Serpent": "serpent", "OpenMC": "openmc", "d1S": "d1s"} + +JADE_TITLE = """ + 8888888 oo 888oo o8888o + 88 o8 8o 88 8oo 88 8 + 88 o8 8o 88 8o 88 8 + 88 o8=====8o 88 8o 88====° + 88 88 o8 8o 88 8oo 8o + °°8888 o8 8o 888oo oo88888 + + Welcome to JADE, a nuclear libraries V&V test suite + + This is the log file, all main events will be recorded here +------------------------------------------------------------------------------- +############################################################################### +""" diff --git a/jade/excelsupport.py b/jade/excelsupport.py index 5d8e9142..0d678cf9 100644 --- a/jade/excelsupport.py +++ b/jade/excelsupport.py @@ -29,7 +29,7 @@ from xlsxwriter.utility import xl_rowcol_to_cell -def single_excel_writer(self, outpath, lib, testname, tallies, stats=None): +def single_excel_writer(outpath, lib, testname, tallies, stats=None): """ Produces single library summary excel file using XLSXwriter diff --git a/jade/exceptions.py b/jade/exceptions.py index 92c9ea3b..25f5a2ca 100644 --- a/jade/exceptions.py +++ b/jade/exceptions.py @@ -1,9 +1,5 @@ import sys - -# colors -CRED = "\033[91m" -CORANGE = "\033[93m" -CEND = "\033[0m" +import logging def fatal_exception(message=None): @@ -24,5 +20,5 @@ def fatal_exception(message=None): message = "A Fatal exception have occured" message = message + ", the application will now exit" - print(CRED + " FATAL EXCEPTION: \n" + message + CEND) + logging.critical(" FATAL EXCEPTION: \n%s", message) sys.exit() diff --git a/jade/gui.py b/jade/gui.py index 8f6b8c4e..8d570310 100644 --- a/jade/gui.py +++ b/jade/gui.py @@ -24,6 +24,7 @@ from __future__ import annotations import os +import logging import sys import json from typing import TYPE_CHECKING @@ -155,7 +156,7 @@ def mainloop(session: Session): uty.print_XS_EXFOR(session) elif option == "exit": - session.log.adjourn("\nSession concluded normally \n") + logging.info("\nSession concluded normally \n") sys.exit() else: @@ -207,23 +208,21 @@ def comploop(session: Session): if lib == "back": comploop(session) if lib == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() runoption = session.conf.run_option() if runoption == "back": comploop(session) if runoption == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() ans = session.state.check_override_run(lib, session) # If checks are ok perform assessment if ans: # Logging bartext = "Computational benchmark execution started" - session.log.bar_adjourn(bartext) - session.log.adjourn( - "Selected Library: " + lib, spacing=False, time=True - ) + logging.info(bartext) + logging.info("Selected Library: %s", lib) print( " ########################### COMPUTATIONAL BENCHMARKS EXECUTION ###########################\n" ) @@ -232,7 +231,7 @@ def comploop(session: Session): " ####################### COMPUTATIONAL BENCHMARKS RUN ENDED ###############################\n" ) t = "Computational benchmark execution ended" - session.log.bar_adjourn(t) + logging.info(t) else: clear_screen() print(computational_menu) @@ -246,7 +245,7 @@ def comploop(session: Session): if lib == "back": comploop(session) if lib == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() try: unfinished, motherdir = session.state.get_unfinished_zaids(lib) @@ -260,9 +259,7 @@ def comploop(session: Session): else: runoption = session.conf.run_option() print(" Completing sphere assessment:") - session.log.adjourn( - "Assessment of: " + lib + " started", spacing=False, time=True - ) + logging.info("Assessment of: %s started", lib) flagOk = True for code, directories in unfinished.items(): for directory in tqdm(directories, desc=code): @@ -299,9 +296,7 @@ def comploop(session: Session): if flag: flagOk = False - session.log.adjourn( - name + " reached timeout, eliminate folder" - ) + logging.info("%s reached timeout, eliminate folder", name) if not flagOk: print( @@ -312,15 +307,13 @@ def comploop(session: Session): print(" Assessment completed") - session.log.adjourn( - "Assessment of: " + lib + " completed", spacing=True, time=True - ) + logging.info("Assessment of: %s completed", lib) elif option == "back": mainloop(session) elif option == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() else: @@ -372,13 +365,13 @@ def exploop(session: Session): if lib == "back": comploop(session) if lib == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() runoption = session.conf.run_option(exp=True) if runoption == "back": comploop(session) if runoption == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() # it may happen that lib are two but only the first is the assessed pieces = lib.split("-") @@ -391,10 +384,8 @@ def exploop(session: Session): if ans: # Logging bartext = "Experimental benchmark execution started" - session.log.bar_adjourn(bartext) - session.log.adjourn( - "Selected Library: " + lib, spacing=False, time=True - ) + logging.info(bartext) + logging.info("Selected Library: %s", lib) print( " ########################### EXPERIMENTAL BENCHMARKS EXECUTION ###########################\n" ) @@ -404,7 +395,7 @@ def exploop(session: Session): " ####################### EXPERIMENTAL BENCHMARKS RUN ENDED ###############################\n" ) t = "Experimental benchmark execution ended" - session.log.bar_adjourn(t) + logging.info(t) else: clear_screen() print(computational_menu) @@ -421,7 +412,7 @@ def exploop(session: Session): mainloop(session) elif option == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() else: @@ -489,8 +480,8 @@ def pploop(session: Session): # Logging bartext = "Post-Processing started" - session.log.bar_adjourn(bartext) - session.log.adjourn("Selected Library: " + lib, spacing=False) + logging.info(bartext) + logging.info("Selected Library: %s", lib) print( "\n ########################### POST-PROCESSING STARTED ###########################\n" ) @@ -510,7 +501,7 @@ def pploop(session: Session): "\n ######################### POST-PROCESSING ENDED ###############################\n" ) t = "Post-Processing completed" - session.log.bar_adjourn(t, spacing=False) + logging.info(t) elif option == "compare": # Update the configuration file @@ -522,8 +513,8 @@ def pploop(session: Session): if ans: # Logging bartext = "Comparison Post-Processing started" - session.log.bar_adjourn(bartext) - session.log.adjourn("Selected Library: " + lib_input, spacing=True) + logging.info(bartext) + logging.info("Selected Library: %s", lib_input) print( "\n ########################### COMPARISON STARTED ###########################\n" ) @@ -537,12 +528,10 @@ def pploop(session: Session): try: print(" Single PP of library " + lib + " required") pp.postprocessBenchmark(session, lib, code, testnames) - session.log.adjourn( + logging.info( """ -Additional Post-Processing of library:""" - + lib - + " completed\n", - spacing=False, +Additional Post-Processing of library: %s completed\n""", + lib, ) except PermissionError as e: clear_screen() @@ -566,7 +555,7 @@ def pploop(session: Session): "\n ######################### COMPARISON ENDED ###############################\n" ) t = "Post-Processing completed" - session.log.bar_adjourn(t, spacing=False) + logging.info(t) elif option == "compexp": # Update the configuration file @@ -580,8 +569,8 @@ def pploop(session: Session): if ans: # Logging bartext = "Comparison Post-Processing started" - session.log.bar_adjourn(bartext) - session.log.adjourn("Selected Library: " + lib_input, spacing=True) + logging.info(bartext) + logging.info("Selected Library: %s", lib_input) print( "\n ########################### COMPARISON STARTED ###########################\n" ) @@ -595,7 +584,7 @@ def pploop(session: Session): # try: # print(' Single PP of library '+lib+' required') # pp.postprocessBenchmark(session, lib, testname) - # session.log.adjourn(""" + # logging.info(""" # Additional Post-Processing of library:"""+lib+' completed\n', spacing=False) # except PermissionError as e: # clear_screen() @@ -622,13 +611,13 @@ def pploop(session: Session): "\n ######################### COMPARISON ENDED ###############################\n" ) t = "Post-Processing completed" - session.log.bar_adjourn(t, spacing=False) + logging.info(t) elif option == "back": mainloop(session) elif option == "exit": - session.log.adjourn(exit_text) + logging.info(exit_text) sys.exit() else: diff --git a/jade/main.py b/jade/main.py index 1aaa310f..054478aa 100644 --- a/jade/main.py +++ b/jade/main.py @@ -28,6 +28,7 @@ import sys import time import warnings +import logging import jade.configuration as cnf import jade.gui as gui @@ -35,6 +36,7 @@ import jade.status as status from jade.exceptions import fatal_exception from jade.input_fetch import fetch_iaea_inputs +from jade.constants import JADE_TITLE # Long messages FIRST_INITIALIZATION = """ @@ -161,7 +163,22 @@ def initialize(self) -> None: log = os.path.join( self.path_logs, "Log " + time.ctime().replace(":", "-") + ".txt" ) - self.log = cnf.Log(log) + # set the logging to a file and keep warnings to video + # Create a file handler for logging INFO level messages + file_handler = logging.FileHandler(log) + file_handler.setLevel(logging.INFO) + # Create a console handler for logging WARNING and ERROR level messages + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.WARNING) + # Add the handlers to the root logger + logging.basicConfig( + # format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + handlers=[ + file_handler, + console_handler, + ], + ) + logging.info(JADE_TITLE) # --- Create the library manager --- # dl = self.conf.default_lib diff --git a/jade/output.py b/jade/output.py index 38f9da1b..7f870947 100644 --- a/jade/output.py +++ b/jade/output.py @@ -293,7 +293,7 @@ def _read_code_version(self, pathtofile: os.PathLike) -> str | None: return None def _read_mcnp_code_version(self, pathtofile: os.PathLike) -> str | None: - if self.testname in ['Sphere', 'SphereSDDR']: + if self.testname in ["Sphere", "SphereSDDR"]: if not os.path.exists(pathtofile): # this can happen the first time return None @@ -792,9 +792,7 @@ def _generate_single_excel_output(self): # ws.range("A9").options(index=False, header=False).value = df # ex.save() - exsupp.single_excel_writer( - self, outpath, self.lib, self.testname, outputs, stats - ) + exsupp.single_excel_writer(outpath, self.lib, self.testname, outputs, stats) def _print_raw(self): if self.mcnp: diff --git a/jade/postprocess.py b/jade/postprocess.py index 09caca0f..5b4559d5 100644 --- a/jade/postprocess.py +++ b/jade/postprocess.py @@ -22,13 +22,16 @@ along with JADE. If not, see . """ import datetime +import logging import jade.expoutput as expo import jade.output as bencho import jade.sphereoutput as spho -def compareBenchmark(session, lib_input: str, code: str, testnames: list , exp=False) -> None: +def compareBenchmark( + session, lib_input: str, code: str, testnames: list, exp=False +) -> None: """Compare benchmark results and perform post-processing. Parameters @@ -41,7 +44,7 @@ def compareBenchmark(session, lib_input: str, code: str, testnames: list , exp=F Named of the test to be compared and post-processed """ - #print("\n Comparing " + testname + ":" + " " + str(datetime.datetime.now())) + # print("\n Comparing " + testname + ":" + " " + str(datetime.datetime.now())) lib = lib_input.split("-") """# get the correct output object out = _get_output('compare', testname, lib, session) @@ -56,20 +59,25 @@ def compareBenchmark(session, lib_input: str, code: str, testnames: list , exp=F config = session.conf.exp_default.set_index("Description") else: config = session.conf.comp_default.set_index("Description") - # Get the log - log = session.log for testname in testnames: - print("\n Comparing " + code + " " + testname + ":" + " " + str(datetime.datetime.now())) + print( + "\n Comparing " + + code + + " " + + testname + + ":" + + " " + + str(datetime.datetime.now()) + ) # get the correct output object out = _get_output("compare", code, testname, lib, session) if out: out.compare() - log.adjourn( - testname - + " benchmark post-processing completed" - + " " - + str(datetime.datetime.now()) + logging.info( + "%s benchmark post-processing completed %s", + testname, + str(datetime.datetime.now()), ) @@ -86,16 +94,15 @@ def postprocessBenchmark(session, lib: str, code: str, testnames: list) -> None: # Get the settings for the tests config = session.conf.comp_default.set_index("Description") - # Get the log - log = session.log post_process = False - + for testname in testnames: post_process = True print( "\n Post-Processing " - + code + " " + + code + + " " + testname + ":" + " " @@ -105,7 +112,7 @@ def postprocessBenchmark(session, lib: str, code: str, testnames: list) -> None: out = _get_output("pp", code, testname, lib, session) if out: out.single_postprocess() - log.adjourn( + logging.info( testname + " benchmark post-processing completed" + " " @@ -136,7 +143,9 @@ def _get_output(action, code, testname, lib, session): elif testname in ["Tiara-BC", "FNS-TOF", "TUD-Fe", "TUD-W"]: if action == "compare": - out = expo.MultipleSpectrumOutput(lib, code, testname, session, multiplerun=True) + out = expo.MultipleSpectrumOutput( + lib, code, testname, session, multiplerun=True + ) elif action == "pp": print(exp_pp_message) return False diff --git a/jade/status.py b/jade/status.py index cc5c5c83..c33fbdd6 100644 --- a/jade/status.py +++ b/jade/status.py @@ -25,6 +25,7 @@ import os import re +import logging from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -396,7 +397,7 @@ def check_override_run(self, lib: str, session: Session, exp: bool = False) -> b for code, test_runned in all_test_runned.items(): for test in test_runned: logtext += f"\n- {code}: {test} [{lib}]" - session.log.adjourn(logtext) + logging.info(logtext) return True elif i == "n": @@ -632,7 +633,7 @@ def check_override_pp( + str(lib) + " has been overwritten" ) - session.log.adjourn(logtext) + logging.info(logtext) break elif i == "n": ans = False @@ -674,7 +675,7 @@ def check_override_pp( + str(lib) + " has been overwritten" ) - session.log.adjourn(logtext) + logging.info(logtext) break elif i == "n": ans = False diff --git a/jade/testrun.py b/jade/testrun.py index bc599983..640addd2 100644 --- a/jade/testrun.py +++ b/jade/testrun.py @@ -59,7 +59,6 @@ def __init__( inp: os.PathLike, lib: str, config: pd.DataFrame, - log, confpath: os.PathLike, runoption: str, lib_name: str, @@ -76,8 +75,6 @@ def __init__( library suffix to use (e.g. 31c). config : pd.DataFrame (single row) configuration options for the test. - log : Log - Jade log file access. confpath : path like object path to the test configuration folder. runoption : str @@ -109,9 +106,6 @@ def __init__( # MCNP original input self.original_inp = inp - # Log for warnings - self.log = log - # VRT path self.path_VRT = inp @@ -171,9 +165,7 @@ def __init__( self.d1s_inp.irrad_file = self.irrad self.d1s_inp.reac_file = self.react except FileNotFoundError: - self.log.adjourn( - "d1S irradition and reaction files not found, skipping..." - ) + logging.info("d1S irradition and reaction files not found, skipping...") self.name = os.path.basename(d1s_ipt).split(".")[0] if self.mcnp: mcnp_ipt = os.path.join(inp, "mcnp", os.path.basename(inp) + ".i") @@ -1540,7 +1532,6 @@ def __init__( inpsfolder: os.PathLike, lib: str, config: pd.DataFrame, - log, confpath: os.PathLike, runoption: str, lib_name: str, @@ -1557,8 +1548,6 @@ def __init__( library suffix to use (e.g. 31c). config : pd.DataFrame (single row) configuration options for the test. - log : Log - Jade log file access. confpath : path like object path to the test configuration folder. lib_name : str @@ -1576,7 +1565,7 @@ def __init__( inp = os.path.join(inpsfolder, folder) if os.path.isfile(inp): continue - test = TestOb(inp, lib, config, log, confpath, runoption, lib_name) + test = TestOb(inp, lib, config, confpath, runoption, lib_name) tests.append(test) self.tests = tests self.name = os.path.basename(inpsfolder) diff --git a/tests/configuration_test.py b/tests/configuration_test.py index d72813cb..c03306f5 100644 --- a/tests/configuration_test.py +++ b/tests/configuration_test.py @@ -23,7 +23,7 @@ import os import sys import pytest -from jade.configuration import Configuration, Log +from jade.configuration import Configuration cp = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(cp) @@ -39,13 +39,6 @@ def config(): return configob -@pytest.fixture -def log(tmpdir): - file = os.path.join(tmpdir, "tmplog.txt") - logob = Log(file) - return logob - - class TestConfiguration: def test_read(self, config): @@ -58,27 +51,3 @@ def test_get_lib_name(self, config): expected_list = ["FENDL 2.1c", "33c", "pincopalle"] for suffix, expected in zip(suffix_list, expected_list): assert config.get_lib_name(suffix) == expected - - -class TestLog: - # Here it is tested that the class just works without prompting errors - # In depth test makes no sense because the class should be substitued - # with the pre-built python log module. - - def test_bar_adjourn(self, log): - txt = "assdad" - log.bar_adjourn(txt) - log.bar_adjourn(txt, spacing=True) - - txt = "asdadasdadasdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - log.bar_adjourn(txt) - - txt = "asa" - log.bar_adjourn(txt) - - assert True - - def test_adjourn(self, log): - txt = "adsdadasdadasd" - log.adjourn(txt, spacing=True, time=True) - assert True diff --git a/tests/excelsupport_test.py b/tests/excelsupport_test.py index 21b01db2..9505cd80 100644 --- a/tests/excelsupport_test.py +++ b/tests/excelsupport_test.py @@ -55,7 +55,7 @@ def test_single_excel_writer(self, tmpdir): # Test case: Write data to excel outpath = tmpdir.join("test.xlsx") single_excel_writer( - self, outpath, self.lib, self.testname, self.tallies, self.stat_df + outpath, self.lib, self.testname, self.tallies, self.stat_df ) # Assert that the file exists diff --git a/tests/testrun_test.py b/tests/testrun_test.py index 77c2af0b..5b753086 100644 --- a/tests/testrun_test.py +++ b/tests/testrun_test.py @@ -26,7 +26,6 @@ import pandas as pd from shutil import rmtree -from jade.configuration import Log from jade.testrun import Test, SphereTest, SphereTestSDDR, MultipleTest from f4enix.input.libmanager import LibManager from f4enix.input.MCNPinput import D1S_Input @@ -51,11 +50,6 @@ FILES = os.path.join(cp, "TestFiles", "testrun") -@pytest.fixture -def LOGFILE(tmpdir): - return Log(tmpdir.join("log.txt")) - - @pytest.fixture def LM(): df_rows = [ @@ -80,7 +74,7 @@ class TestTest: files = os.path.join(FILES, "Test") dummyout = os.path.join(FILES, "dummy") - def test_build_normal(self, LM: LibManager, tmpdir, LOGFILE: Log): + def test_build_normal(self, LM: LibManager, tmpdir): # Just check that nothing breaks lib = "81c" inp_name = "ITER_1D" @@ -103,7 +97,7 @@ def test_build_normal(self, LM: LibManager, tmpdir, LOGFILE: Log): conf_path = "dummy" # Build the test - test = Test(inp, lib, config, LOGFILE, conf_path, "c", "dummy") + test = Test(inp, lib, config, conf_path, "c", "dummy") test.generate_test(tmpdir, LM) metadata_file = os.path.join(tmpdir, "ITER_1D", "mcnp", "metadata.json") assert os.path.exists(metadata_file) @@ -112,7 +106,7 @@ def test_build_normal(self, LM: LibManager, tmpdir, LOGFILE: Log): assert metadata["jade_run_version"] == __version__ assert metadata["benchmark_version"] == "1.0" - def test_build_d1s(self, LM: LibManager, LOGFILE: Log, tmpdir): + def test_build_d1s(self, LM: LibManager, tmpdir): # Just check that nothing breaks lib = "99c-31c" inp_name = "ITER_Cyl_SDDR" @@ -134,7 +128,7 @@ def test_build_d1s(self, LM: LibManager, LOGFILE: Log, tmpdir): conf_path = os.path.join(self.files, "ITER_Cyl_SDDR_cnf") # Build the test - test = Test(inp, lib, config, LOGFILE, conf_path, "c", "dummy") + test = Test(inp, lib, config, conf_path, "c", "dummy") test.generate_test(tmpdir, LM) translated_inp = D1S_Input.from_input( os.path.join(tmpdir, "ITER_Cyl_SDDR", "d1s", "ITER_Cyl_SDDR"), @@ -153,7 +147,7 @@ class TestSphereTest: files = os.path.join(FILES, "SphereTest") dummyout = os.path.join(FILES, "dummy") - def test_build(self, LM: LibManager, LOGFILE: Log, tmpdir): + def test_build(self, LM: LibManager, tmpdir): # Just check that nothing breaks lib = "31c" inp_name = "Sphere" @@ -175,7 +169,7 @@ def test_build(self, LM: LibManager, LOGFILE: Log, tmpdir): conf_path = os.path.join(self.files, "Spherecnf") # Build the test - test = SphereTest(inp, lib, config, LOGFILE, conf_path, "c", "dummy") + test = SphereTest(inp, lib, config, conf_path, "c", "dummy") test.generate_test(tmpdir, LM) metadata_file = os.path.join( tmpdir, "Sphere", "Sphere_1001_H-1", "mcnp", "metadata.json" @@ -195,7 +189,7 @@ def test_build(self, LM: LibManager, LOGFILE: Log, tmpdir): class TestSphereTestSDDR: files = os.path.join(FILES, "SphereTestSDDR") - def test_build(self, LM: LibManager, tmpdir, LOGFILE: Log): + def test_build(self, LM: LibManager, tmpdir): # Just check that nothing breaks lib = "93c-31c" inp_name = "SphereSDDR" @@ -216,7 +210,7 @@ def test_build(self, LM: LibManager, tmpdir, LOGFILE: Log): conf_path = os.path.join(self.files, "cnf") # Build the test - test = SphereTestSDDR(inp, lib, config, LOGFILE, conf_path, "c", "dummy") + test = SphereTestSDDR(inp, lib, config, conf_path, "c", "dummy") test.generate_test(tmpdir, LM) # Ensure only one channel is used reac_file = os.path.join( @@ -244,7 +238,7 @@ class TestMultipleTest: files = os.path.join(FILES, "MultipleTest") dummyout = os.path.join(FILES, "dummy") - def test_build(self, LM: LibManager, tmpdir, LOGFILE: Log): + def test_build(self, LM: LibManager, tmpdir): # Just check that nothing breaks lib = "31c" # inp_folder = os.path.join(self.files, "Inputs") @@ -268,7 +262,7 @@ def test_build(self, LM: LibManager, tmpdir, LOGFILE: Log): conf_path = os.path.join(self.files, "cnf") # Build the test - test = MultipleTest(inp, lib, config, LOGFILE, conf_path, "c", "dummy") + test = MultipleTest(inp, lib, config, conf_path, "c", "dummy") test.generate_test(tmpdir, LM) metadata_file = os.path.join( tmpdir, "Oktavian", "Oktavian_Al", "mcnp", "metadata.json" From 91ea566e8367da4f4358cbaa09910323e8507cb9 Mon Sep 17 00:00:00 2001 From: Davide Laghi Date: Wed, 7 Aug 2024 11:55:02 +0200 Subject: [PATCH 2/4] fix configuration of the loggers --- jade/main.py | 41 +++++++++++++++++++++++++---------------- tests/main_test.py | 38 +++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/jade/main.py b/jade/main.py index 054478aa..2d64b51a 100644 --- a/jade/main.py +++ b/jade/main.py @@ -163,22 +163,7 @@ def initialize(self) -> None: log = os.path.join( self.path_logs, "Log " + time.ctime().replace(":", "-") + ".txt" ) - # set the logging to a file and keep warnings to video - # Create a file handler for logging INFO level messages - file_handler = logging.FileHandler(log) - file_handler.setLevel(logging.INFO) - # Create a console handler for logging WARNING and ERROR level messages - console_handler = logging.StreamHandler() - console_handler.setLevel(logging.WARNING) - # Add the handlers to the root logger - logging.basicConfig( - # format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", - handlers=[ - file_handler, - console_handler, - ], - ) - logging.info(JADE_TITLE) + self._initialize_log(log) # --- Create the library manager --- # dl = self.conf.default_lib @@ -191,6 +176,30 @@ def initialize(self) -> None: # --- Initialize status --- self.state = status.Status(self) + @staticmethod + def _initialize_log(log: str | os.PathLike) -> None: + logger = logging.getLogger() + logger.setLevel(logging.INFO) + + # set the logging to a file and keep warnings to video + # Create a file handler for logging INFO level messages + file_handler = logging.FileHandler(log) + file_handler.setLevel(logging.INFO) + file_handler.setFormatter( + logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + ) + # Create a console handler for logging WARNING and ERROR level messages + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.WARNING) + + for handler in logger.handlers: + # there should already be a streamhandler + if isinstance(handler, logging.StreamHandler): + handler.setLevel(logging.WARNING) + logger.addHandler(file_handler) + # logger.addHandler(console_handler) + logging.info(JADE_TITLE) + def check_active_tests(self, action: str, exp=False) -> dict[str, list[str]]: """ Check the configuration file for active benchmarks to perform or diff --git a/tests/main_test.py b/tests/main_test.py index 0a05ed78..589e62bc 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -23,6 +23,8 @@ import os import sys import pytest +import logging + from jade.main import Session from jade.configuration import Configuration @@ -40,21 +42,31 @@ def __init__(self): self.conf = Configuration(MAIN_CONFIG_FILE) -@pytest.mark.parametrize( - ["action", "expected"], - [ - ( - "Run", - {"mcnp": ["Oktavian"], "openmc": ["FNG"], "serpent": ["FNG"]}, - ), - ( - "Post-Processing", - {"mcnp": ["Oktavian"], "openmc": ["FNG"], "serpent": ["FNG"]}, - ), - ], -) class TestSession: + @pytest.mark.parametrize( + ["action", "expected"], + [ + ( + "Run", + {"mcnp": ["Oktavian"], "openmc": ["FNG"], "serpent": ["FNG"]}, + ), + ( + "Post-Processing", + {"mcnp": ["Oktavian"], "openmc": ["FNG"], "serpent": ["FNG"]}, + ), + ], + ) def test_check_active_tests(self, action, expected): session = MockUpSession() active_tests = session.check_active_tests(action, exp=True) assert active_tests == expected + + def test_initialize_log(self, tmpdir): + session = MockUpSession() + logfile = os.path.join(tmpdir, "log.txt") + session._initialize_log(logfile) + assert os.path.exists(logfile) + assert os.stat(logfile).st_size > 0 + logging.warning("This is a test") + with open(logfile, "r", encoding="utf-8") as f: + assert "This is a test" in f.read() From 91d2c90b1f2c99acb9714df486e9391350131430 Mon Sep 17 00:00:00 2001 From: Davide Laghi Date: Wed, 7 Aug 2024 12:12:50 +0200 Subject: [PATCH 3/4] try fixing encoding --- jade/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jade/main.py b/jade/main.py index 2d64b51a..9316abd4 100644 --- a/jade/main.py +++ b/jade/main.py @@ -183,7 +183,7 @@ def _initialize_log(log: str | os.PathLike) -> None: # set the logging to a file and keep warnings to video # Create a file handler for logging INFO level messages - file_handler = logging.FileHandler(log) + file_handler = logging.FileHandler(log, encoding="utf-8") file_handler.setLevel(logging.INFO) file_handler.setFormatter( logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") From f6358f198e1c90597765a5144bb176fa85bfe5b4 Mon Sep 17 00:00:00 2001 From: Davide Laghi Date: Fri, 4 Oct 2024 15:42:32 +0200 Subject: [PATCH 4/4] added extra log calls --- jade/computational.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jade/computational.py b/jade/computational.py index 93354b1a..7e8cd3b2 100644 --- a/jade/computational.py +++ b/jade/computational.py @@ -181,9 +181,12 @@ def executeBenchmarksRoutines(session, lib: str, runoption: str, exp=False) -> N if bool(row["OnlyInput"]): print("\n -- " + testname.upper() + " COMPLETED --\n") + logging.info("Only inputs were generated for %s", testname.upper()) else: # --- Input Run --- - print(" Simulation running: " + str(datetime.datetime.now())) + text = " Simulation running: " + str(datetime.datetime.now()) + print(text) + logging.info(text) # test.run(cpu=session.conf.cpu) test.run(session.conf, session.lib_manager, runoption) print("\n -- " + testname.upper() + " COMPLETED --\n")