From f546e7d06589a5ab00ab44c8aea569ffa790efc3 Mon Sep 17 00:00:00 2001 From: Thomas Michelat Date: Mon, 20 Jan 2025 15:06:10 +0100 Subject: [PATCH] handle failure generating summary data --- damnit/ctxsupport/damnit_ctx.py | 8 +++++++- tests/test_backend.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/damnit/ctxsupport/damnit_ctx.py b/damnit/ctxsupport/damnit_ctx.py index b0c8dc9e..08b6fabf 100644 --- a/damnit/ctxsupport/damnit_ctx.py +++ b/damnit/ctxsupport/damnit_ctx.py @@ -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 @@ -15,6 +16,8 @@ __all__ = ["RunData", "Variable", "Cell"] +log = logging.getLogger(__name__) + THUMBNAIL_SIZE = 300 # px @@ -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 diff --git a/tests/test_backend.py b/tests/test_backend.py index cb63753f..0ffc20c8 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -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):