Skip to content

Commit

Permalink
Move logging handling to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfullard committed Aug 12, 2024
1 parent 9b10be6 commit a521c10
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 97 deletions.
109 changes: 12 additions & 97 deletions tardis/workflows/standard_simulation_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import numpy as np
import pandas as pd
from astropy import units as u

Check warning on line 6 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L4-L6

Added lines #L4 - L6 were not covered by tests
from IPython.display import display

from tardis import constants as const
from tardis.io.atom_data.base import AtomData
from tardis.io.logger.logger import logging_state
from tardis.model import SimulationState
from tardis.plasma.radiation_field import DilutePlanckianRadiationField
from tardis.plasma.standard_plasmas import assemble_plasma
Expand All @@ -21,12 +19,13 @@
from tardis.transport.montecarlo.base import MonteCarloTransportSolver
from tardis.util.base import is_notebook
from tardis.visualization import ConvergencePlots
from tardis.workflows.workflow_logging import WorkflowLogging

Check warning on line 22 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L19-L22

Added lines #L19 - L22 were not covered by tests

# logging support
logger = logging.getLogger(__name__)

Check warning on line 25 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L25

Added line #L25 was not covered by tests


class StandardSimulationSolver:
class StandardSimulationSolver(WorkflowLogging):
def __init__(

Check warning on line 29 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L28-L29

Added lines #L28 - L29 were not covered by tests
self,
configuration,
Expand All @@ -37,8 +36,12 @@ def __init__(
show_convergence_plots=False,
convergence_plots_kwargs={},
):
# Logging
logging_state(log_level, configuration, specific_log_level)
# set up logging
super().__init__(

Check warning on line 40 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L40

Added line #L40 was not covered by tests
configuration,
log_level=log_level,
specific_log_level=specific_log_level,
)
self.show_progress_bars = show_progress_bars

Check warning on line 45 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L45

Added line #L45 was not covered by tests

atom_data = self._get_atom_data(configuration)

Check warning on line 47 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L47

Added line #L47 was not covered by tests
Expand All @@ -61,6 +64,7 @@ def __init__(
enable_virtual_packet_logging=enable_virtual_packet_logging,
)

# Luminosity filter frequencies
self.luminosity_nu_start = (

Check warning on line 68 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L68

Added line #L68 was not covered by tests
configuration.supernova.luminosity_wavelength_end.to(
u.Hz, u.spectral()
Expand Down Expand Up @@ -227,10 +231,6 @@ def get_convergence_estimates(self, transport_state):
estimated_dilution_factor = (

Check warning on line 231 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L231

Added line #L231 was not covered by tests
estimated_radfield_properties.dilute_blackbody_radiationfield_state.dilution_factor
)
self.initialize_spectrum_solver(
transport_state,
None,
)

emitted_luminosity = calculate_filtered_luminosity(

Check warning on line 235 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L235

Added line #L235 was not covered by tests
transport_state.emitted_packet_nu,
Expand All @@ -257,7 +257,9 @@ def get_convergence_estimates(self, transport_state):
}
self.update_convergence_plot_data(plot_data)

Check warning on line 258 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L258

Added line #L258 was not covered by tests

self.log_iteration_results(emitted_luminosity, absorbed_luminosity)
self.log_iteration_results(

Check warning on line 260 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L260

Added line #L260 was not covered by tests
emitted_luminosity, absorbed_luminosity, self.luminosity_requested
)

luminosity_ratios = (

Check warning on line 264 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L264

Added line #L264 was not covered by tests
(emitted_luminosity / self.luminosity_requested).to(1).value
Expand Down Expand Up @@ -341,93 +343,6 @@ def update_convergence_plot_data(self, plot_data_dict):
item_type=item_type,
)

def log_iteration_results(self, emitted_luminosity, absorbed_luminosity):
"""Print current iteration information to log at INFO level
Parameters
----------
emitted_luminosity : Quantity
Current iteration emitted luminosity
absorbed_luminosity : Quantity
Current iteration absorbed luminosity
"""
logger.info(
f"\n\tLuminosity emitted = {emitted_luminosity:.3e}\n"
f"\tLuminosity absorbed = {absorbed_luminosity:.3e}\n"
f"\tLuminosity requested = {self.luminosity_requested:.3e}\n"
)

def log_plasma_state(
self,
t_rad,
dilution_factor,
t_inner,
next_t_rad,
next_dilution_factor,
next_t_inner,
log_sampling=5,
):
"""
Logging the change of the plasma state
Parameters
----------
t_rad : astropy.units.Quanity
current t_rad
dilution_factor : np.ndarray
current dilution_factor
next_t_rad : astropy.units.Quanity
next t_rad
next_dilution_factor : np.ndarray
next dilution_factor
log_sampling : int
the n-th shells to be plotted
Returns
-------
"""
plasma_state_log = pd.DataFrame(
index=np.arange(len(t_rad)),
columns=["t_rad", "next_t_rad", "w", "next_w"],
)
plasma_state_log["t_rad"] = t_rad
plasma_state_log["next_t_rad"] = next_t_rad
plasma_state_log["w"] = dilution_factor
plasma_state_log["next_w"] = next_dilution_factor
plasma_state_log.columns.name = "Shell No."

if is_notebook():
logger.info("\n\tPlasma stratification:")

# Displaying the DataFrame only when the logging level is NOTSET, DEBUG or INFO
if logger.level <= logging.INFO:
if not logger.filters:
display(
plasma_state_log.iloc[::log_sampling].style.format(
"{:.3g}"
)
)
elif logger.filters[0].log_level == 20:
display(
plasma_state_log.iloc[::log_sampling].style.format(
"{:.3g}"
)
)
else:
output_df = ""
plasma_output = plasma_state_log.iloc[::log_sampling].to_string(
float_format=lambda x: f"{x:.3g}",
justify="center",
)
for value in plasma_output.split("\n"):
output_df = output_df + f"\t{value}\n"
logger.info("\n\tPlasma stratification:")
logger.info(f"\n{output_df}")

logger.info(
f"\n\tCurrent t_inner = {t_inner:.3f}\n\tExpected t_inner for next iteration = {next_t_inner:.3f}\n"
)

def solve_simulation_state(

Check warning on line 346 in tardis/workflows/standard_simulation_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/standard_simulation_solver.py#L346

Added line #L346 was not covered by tests
self,
estimated_values,
Expand Down
111 changes: 111 additions & 0 deletions tardis/workflows/workflow_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import logging

Check warning on line 1 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L1

Added line #L1 was not covered by tests

import numpy as np
import pandas as pd
from IPython.display import display

Check warning on line 5 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L3-L5

Added lines #L3 - L5 were not covered by tests

from tardis.io.logger.logger import logging_state
from tardis.util.base import is_notebook

Check warning on line 8 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L7-L8

Added lines #L7 - L8 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 10 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L10

Added line #L10 was not covered by tests


class WorkflowLogging:
def __init__(

Check warning on line 14 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L13-L14

Added lines #L13 - L14 were not covered by tests
self,
configuration,
log_level=None,
specific_log_level=None,
):
logging_state(log_level, configuration, specific_log_level)

Check warning on line 20 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L20

Added line #L20 was not covered by tests

def log_iteration_results(

Check warning on line 22 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L22

Added line #L22 was not covered by tests
self, emitted_luminosity, absorbed_luminosity, luminosity_requested
):
"""Print current iteration information to log at INFO level
Parameters
----------
emitted_luminosity : Quantity
Current iteration emitted luminosity
absorbed_luminosity : Quantity
Current iteration absorbed luminosity
luminosity_requested : Quantity
The requested luminosity for the simulation
"""
logger.info(

Check warning on line 36 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L36

Added line #L36 was not covered by tests
f"\n\tLuminosity emitted = {emitted_luminosity:.3e}\n"
f"\tLuminosity absorbed = {absorbed_luminosity:.3e}\n"
f"\tLuminosity requested = {luminosity_requested:.3e}\n"
)

def log_plasma_state(

Check warning on line 42 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L42

Added line #L42 was not covered by tests
self,
t_rad,
dilution_factor,
t_inner,
next_t_rad,
next_dilution_factor,
next_t_inner,
log_sampling=5,
):
"""
Logging the change of the plasma state
Parameters
----------
t_rad : astropy.units.Quanity
current t_rad
dilution_factor : np.ndarray
current dilution_factor
next_t_rad : astropy.units.Quanity
next t_rad
next_dilution_factor : np.ndarray
next dilution_factor
log_sampling : int
the n-th shells to be plotted
Returns
-------
"""
plasma_state_log = pd.DataFrame(

Check warning on line 71 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L71

Added line #L71 was not covered by tests
index=np.arange(len(t_rad)),
columns=["t_rad", "next_t_rad", "w", "next_w"],
)
plasma_state_log["t_rad"] = t_rad
plasma_state_log["next_t_rad"] = next_t_rad
plasma_state_log["w"] = dilution_factor
plasma_state_log["next_w"] = next_dilution_factor
plasma_state_log.columns.name = "Shell No."

Check warning on line 79 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L75-L79

Added lines #L75 - L79 were not covered by tests

if is_notebook():
logger.info("\n\tPlasma stratification:")

Check warning on line 82 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L81-L82

Added lines #L81 - L82 were not covered by tests

# Displaying the DataFrame only when the logging level is NOTSET, DEBUG or INFO
if logger.level <= logging.INFO:
if not logger.filters:
display(

Check warning on line 87 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L85-L87

Added lines #L85 - L87 were not covered by tests
plasma_state_log.iloc[::log_sampling].style.format(
"{:.3g}"
)
)
elif logger.filters[0].log_level == 20:
display(

Check warning on line 93 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L92-L93

Added lines #L92 - L93 were not covered by tests
plasma_state_log.iloc[::log_sampling].style.format(
"{:.3g}"
)
)
else:
output_df = ""
plasma_output = plasma_state_log.iloc[::log_sampling].to_string(

Check warning on line 100 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L99-L100

Added lines #L99 - L100 were not covered by tests
float_format=lambda x: f"{x:.3g}",
justify="center",
)
for value in plasma_output.split("\n"):
output_df = output_df + f"\t{value}\n"
logger.info("\n\tPlasma stratification:")
logger.info(f"\n{output_df}")

Check warning on line 107 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L104-L107

Added lines #L104 - L107 were not covered by tests

logger.info(

Check warning on line 109 in tardis/workflows/workflow_logging.py

View check run for this annotation

Codecov / codecov/patch

tardis/workflows/workflow_logging.py#L109

Added line #L109 was not covered by tests
f"\n\tCurrent t_inner = {t_inner:.3f}\n\tExpected t_inner for next iteration = {next_t_inner:.3f}\n"
)

0 comments on commit a521c10

Please sign in to comment.