Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: handle failure generating summary data #370

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading