Commit d8933038 authored by Valentin Valls's avatar Valentin Valls

Rework the computed items with ComputeError

parent a7df7a9e
......@@ -382,6 +382,16 @@ _NotComputed = object()
"""Allow to flag an attribute as not computed"""
class ComputeError(Exception):
"""Raised when the `compute` method of AbstractComputableItem can't compute
any output"""
def __init__(self, msg: str, result=None):
super(ComputeError, self).__init__(self, msg)
self.msg = msg
self.result = result
class AbstractComputableItem(Item):
"""This item use the scan data to process result before displaying it."""
......@@ -428,7 +438,17 @@ class AbstractComputableItem(Item):
if scan.hasCachedResult(self):
result = scan.getCachedResult(self)
else:
result = self.compute(scan)
try:
result = self.compute(scan)
except ComputeError as e:
scan.setCacheValidation(self, self.version(), e.msg)
result = e.result
except Exception as e:
scan.setCacheValidation(
self, self.version(), "Error while computing:" + str(e)
)
result = None
scan.setCachedResult(self, result)
if not self.isResultValid(result):
return None
......
......@@ -20,12 +20,15 @@ from typing import Optional
from typing import NamedTuple
import numpy
import logging
from . import scan_model
from . import plot_model
from . import plot_item_model
from ..utils import mathutils
_logger = logging.getLogger(__name__)
class CurveStatisticMixIn:
"""This item use the scan data to process result before displaying it."""
......@@ -60,25 +63,18 @@ class DerivativeItem(plot_model.AbstractComputableItem, plot_item_model.CurveMix
) -> Optional[Tuple[numpy.ndarray, numpy.ndarray]]:
sourceItem = self.source()
x = sourceItem.xData(scan)
y = sourceItem.yData(scan)
if x is None or y is None:
return None
x = x.array()
y = y.array()
x = sourceItem.xArray(scan)
y = sourceItem.yArray(scan)
if x is None or y is None:
return None
try:
result = mathutils.derivate(x, y)
except Exception as e:
# FIXME: Maybe it is better to return a special type and then return
# Managed outside to store it into the validation cache
scan.setCacheValidation(
self, self.version(), "Error while creating derivative.\n" + str(e)
_logger.debug("Error while computing derivative", exc_info=True)
raise plot_model.ComputeError(
"Error while creating derivative.\n" + str(e), result=None
)
return None
return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment