Skip to content

Commit

Permalink
handle failure generating summary data
Browse files Browse the repository at this point in the history
  • Loading branch information
tmichela committed Jan 20, 2025
1 parent 4786032 commit f546e7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion damnit/ctxsupport/damnit_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
than the DAMNIT code in general, to allow running context files in other Python
environments.
"""
import logging
import re
import sys
from collections.abc import Sequence
Expand All @@ -15,6 +16,8 @@

__all__ = ["RunData", "Variable", "Cell"]

log = logging.getLogger(__name__)


THUMBNAIL_SIZE = 300 # px

Expand Down Expand Up @@ -169,7 +172,10 @@ def get_summary(self):
if self.summary_value is not None:
return self.summary_value
elif self.summary is not None:
return np.asarray(getattr(np, self.summary)(self.data))
try:
return np.asarray(getattr(np, self.summary)(self.data))
except Exception:
log.error("Failed to produce summary data", exc_info=True)

return None

Expand Down
22 changes: 22 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,28 @@ def var3(run):

assert f['.reduced/var3'][()] == 4


def test_results_empty_array(mock_run, tmp_path, caplog):
# test failing summary
empty_array = """
from damnit_ctx import Variable
@Variable(title='Foo', summary='max')
def foo(run):
import numpy as np
return np.array([])
"""
ctx = mkcontext(empty_array)
with caplog.at_level(logging.ERROR):
results = ctx.execute(mock_run, 1000, 123, {})
results.save_hdf5(tmp_path / 'results.h5', reduced_only=True)

# One warning about foo should have been logged
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
assert caplog.records[0].msg == "Failed to produce summary data"


@pytest.mark.skip(reason="Depending on user variables is currently disabled")
def test_results_with_user_vars(mock_ctx_user, mock_user_vars, mock_run, caplog):

Expand Down

0 comments on commit f546e7d

Please sign in to comment.