Skip to content

Commit

Permalink
draft fix for #174, providing exception traceback information
Browse files Browse the repository at this point in the history
  • Loading branch information
enzbus committed Aug 29, 2024
1 parent bd806ed commit e7cf37b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions cvxportfolio/costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ class SumCost(Cost):
This should not be instatiated directly, use cost + cost.
"""

# skip this cost in exception reporting traceback
_is_internal = True

def __init__(self, left, right):
self.left = left
assert isinstance(left, Cost)
Expand Down Expand Up @@ -230,6 +233,9 @@ class MulCost(Cost):
This should not be instatiated directly, use scalar * cost.
"""

# skip this cost in exception reporting traceback
_is_internal = True

def __init__(self, scalar, cost):
self.scalar = scalar
assert isinstance(scalar, (Number, HyperParameter))
Expand Down
28 changes: 19 additions & 9 deletions cvxportfolio/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Estimator:
:meth:`values_in_time_recursive`.
"""

# we use this property for exception reporting to the user, skipping
# internal estimators on the recursive evaluation path
_is_internal = False

# explicitely list subestimators, only needed for those not defined
# at class attribute level
__subestimators__ = ()
Expand Down Expand Up @@ -190,16 +194,22 @@ def values_in_time_recursive(self, **kwargs):
something there.
:rtype: numpy.array, pandas.Series, pandas.DataFrame, ...
"""
for _, subestimator in self.__dict__.items():
if hasattr(subestimator, "values_in_time_recursive"):
try:
for _, subestimator in self.__dict__.items():
if hasattr(subestimator, "values_in_time_recursive"):
subestimator.values_in_time_recursive(**kwargs)
for subestimator in self.__subestimators__:
subestimator.values_in_time_recursive(**kwargs)
for subestimator in self.__subestimators__:
subestimator.values_in_time_recursive(**kwargs)
if hasattr(self, "values_in_time"):
# pylint: disable=assignment-from-no-return
self._current_value = self.values_in_time(**kwargs)
return self.current_value
return self.current_value # pragma: no cover
if hasattr(self, "values_in_time"):
# pylint: disable=assignment-from-no-return
self._current_value = self.values_in_time(**kwargs)
return self.current_value
return self.current_value # pragma: no cover
except Exception as e:
if self._is_internal: # skip this object
raise e
raise e.__class__(
f'This error happened in {self}, look at traceback') from e

def collect_hyperparameters(self):
"""Collect (recursively) all hyperparameters defined in a policy.
Expand Down

0 comments on commit e7cf37b

Please sign in to comment.