Skip to content

Commit 11e43f1

Browse files
committed
Catch exceptions from warm starting
1 parent 23e3e5b commit 11e43f1

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

switch_model/tools/graph/main.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,13 @@
2121
import plotnine
2222

2323
# Local imports
24-
from switch_model.utilities import StepTimer, get_module_list, query_yes_no
24+
from switch_model.utilities import StepTimer, get_module_list, query_yes_no, catch_exceptions
2525

2626
# When True exceptions that are thrown while graphing will be caught
2727
# and outputted to console as a warning instead of an error
2828
CATCH_EXCEPTIONS = True
2929

3030

31-
def catch_exceptions(func):
32-
"""
33-
Decorator that wraps a function such that exceptions are caught and outputted as warnings instead.
34-
"""
35-
36-
@functools.wraps(func)
37-
def wrapper(*args, **kwargs):
38-
if not CATCH_EXCEPTIONS:
39-
return func(*args, **kwargs)
40-
try:
41-
return func(*args, **kwargs)
42-
except:
43-
warnings.warn(f"The following error was caught and we are moving on."
44-
f"{traceback.format_exc()}")
45-
46-
return wrapper
47-
48-
4931
# List of graphing functions. Every time a function uses the @graph() decorator,
5032
# the function gets registered here.
5133
registered_graphs = {}
@@ -75,7 +57,7 @@ def graph(
7557

7658
def decorator(func):
7759
@functools.wraps(func)
78-
@catch_exceptions
60+
@catch_exceptions("Failed to run a graphing function.", should_catch=CATCH_EXCEPTIONS)
7961
def wrapper(tools: GraphTools):
8062
if tools.skip_long and is_long:
8163
return

switch_model/utilities/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def query_yes_no(question, default="yes"):
957957
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
958958

959959

960-
def catch_exceptions(warning_msg=None, should_catch=True):
960+
def catch_exceptions(warning_msg="An exception was caught and ignored.", should_catch=True):
961961
"""Decorator that catches exceptions."""
962962

963963
def decorator(func):
@@ -969,8 +969,7 @@ def wrapper(*args, **kwargs):
969969
try:
970970
return func(*args, **kwargs)
971971
except:
972-
if warning_msg is not None:
973-
warnings.warn(warning_msg)
972+
warnings.warn(warning_msg + "\nDetailed error log: " + traceback.format_exc())
974973

975974
return wrapper
976975

switch_model/utilities/gurobi_aug.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pyomo.solvers.plugins.solvers.gurobi_direct import GurobiDirect
2323
from pyomo.environ import *
2424

25-
from switch_model.utilities import StepTimer
25+
from switch_model.utilities import StepTimer, catch_exceptions
2626

2727

2828
class PicklableData:
@@ -222,6 +222,7 @@ def _postsolve(self):
222222
print(f"Created warm start pickle file in {timer.step_time_as_str()}")
223223
return results
224224

225+
@catch_exceptions(warning_msg="Failed to save warm start information.")
225226
def _save_warm_start(self, save_c_v_basis):
226227
"""Create a pickle file containing the CBasis/VBasis information."""
227228
# Setup our data objects

0 commit comments

Comments
 (0)