Skip to content

Commit

Permalink
Decorator logging function (#232)
Browse files Browse the repository at this point in the history
* Logging function name where error occurs

* updated text
  • Loading branch information
marshHawk4 authored Jul 12, 2023
1 parent 533965d commit df2d47a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/pyciemss/PetriNetODE/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
StaticParameterInterventionEvent,
)

from pyciemss.custom_decorators import pyciemss_logging_wrappper

# TODO: These interfaces should probably be just in terms of JSON-like objects.

PetriSolution = dict[str, torch.Tensor]
PetriInferredParameters = pyro.nn.PyroModule


@pyciemss_logging_wrappper
def load_and_sample_petri_model(
petri_model_or_path: Union[str, mira.metamodel.TemplateModel, mira.modeling.Model],
num_samples: int,
Expand Down Expand Up @@ -122,7 +124,7 @@ def load_and_sample_petri_model(

return processed_samples


@pyciemss_logging_wrappper
def load_and_calibrate_and_sample_petri_model(
petri_model_or_path: Union[str, mira.metamodel.TemplateModel, mira.modeling.Model],
data_path: str,
Expand Down Expand Up @@ -233,7 +235,7 @@ def load_and_calibrate_and_sample_petri_model(

##############################################################################
# Internal Interfaces Below - TA4 above

@pyciemss_logging_wrappper
def load_petri_model(
petri_model_or_path: Union[str, mira.metamodel.TemplateModel, mira.modeling.Model],
add_uncertainty=True,
Expand All @@ -243,7 +245,6 @@ def load_petri_model(
"""
Load a petri net from a file and compile it into a probabilistic program.
"""

if add_uncertainty:
model = ScaledBetaNoisePetriNetODESystem.from_askenet(petri_model_or_path, compile_rate_law_p=compile_rate_law_p)
model.pseudocount = torch.tensor(pseudocount)
Expand All @@ -253,6 +254,7 @@ def load_petri_model(


@setup_model.register
@pyciemss_logging_wrappper
def setup_petri_model(
petri: PetriNetODESystem,
start_time: float,
Expand All @@ -269,6 +271,7 @@ def setup_petri_model(


@reset_model.register
@pyciemss_logging_wrappper
def reset_petri_model(petri: PetriNetODESystem) -> PetriNetODESystem:
"""
Reset a model to its initial state.
Expand All @@ -280,6 +283,7 @@ def reset_petri_model(petri: PetriNetODESystem) -> PetriNetODESystem:


@intervene.register
@pyciemss_logging_wrappper
def intervene_petri_model(
petri: PetriNetODESystem, interventions: Iterable[Tuple[float, str, float]]
) -> PetriNetODESystem:
Expand All @@ -297,6 +301,7 @@ def intervene_petri_model(


@calibrate.register
@pyciemss_logging_wrappper
def calibrate_petri(
petri: PetriNetODESystem,
data: Iterable[Tuple[float, dict[str, float]]],
Expand Down Expand Up @@ -340,6 +345,7 @@ def calibrate_petri(


@sample.register
@pyciemss_logging_wrappper
def sample_petri(
petri: PetriNetODESystem,
timepoints: Iterable[float],
Expand All @@ -366,6 +372,7 @@ def sample_petri(


@optimize.register
@pyciemss_logging_wrappper
def optimize_petri(
petri: PetriNetODESystem,
timepoints: Iterable,
Expand Down
32 changes: 32 additions & 0 deletions src/pyciemss/custom_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging
import time
import functools

def pyciemss_logging_wrappper( function):
def wrapped(*args, **kwargs):
try:
start_time = time.perf_counter()
result = function(*args, **kwargs)
end_time = time.perf_counter()
logging.info(
f"Elapsed time for {function.__name__}:",
end_time - start_time
)
return result
except Exception as e:

log_message = f"""
###############################
There was an exception in pyciemss
Error occured in function: {function.__name__}
Function docs : {function.__doc__}
################################
"""
logging.exception(log_message)
raise e
functools.update_wrapper(wrapped, function)
return wrapped

0 comments on commit df2d47a

Please sign in to comment.