Commit 7df9f31a authored by Valentin Valls's avatar Valentin Valls

Create a min state item

parent 4195a1c4
......@@ -314,3 +314,77 @@ class MaxCurveItem(plot_model.AbstractIncrementalComputableItem, CurveStatisticM
max_index, max_location_y, max_location_x, min_y_value, nb + len(xx)
)
return result
class MinData(NamedTuple):
min_index: int
min_location_y: float
min_location_x: float
max_y_value: float
nb_points: int
class MinCurveItem(plot_model.AbstractIncrementalComputableItem, CurveStatisticMixIn):
"""Statistic identifying the minimum location of a curve."""
def isResultValid(self, result):
return result is not None
def compute(self, scan: scan_model.Scan) -> Optional[MinData]:
sourceItem = self.source()
xx = sourceItem.xArray(scan)
yy = sourceItem.yArray(scan)
if xx is None or yy is None:
return None
min_index = numpy.argmin(yy)
max_y_value = numpy.max(yy)
min_location_x, min_location_y = xx[min_index], yy[min_index]
result = MinData(
min_index, min_location_y, min_location_x, max_y_value, len(xx)
)
return result
def incrementalCompute(
self, previousResult: MinData, scan: scan_model.Scan
) -> MinData:
sourceItem = self.source()
xx = sourceItem.xArray(scan)
yy = sourceItem.yArray(scan)
if xx is None or yy is None:
raise ValueError("Non empty data is expected")
nb = previousResult.nb_points
if nb == len(xx):
# obviously nothing to compute
return previousResult
xx = xx[nb:]
yy = yy[nb:]
min_index = numpy.argmin(yy)
max_y_value = numpy.max(yy)
min_location_x, min_location_y = xx[min_index], yy[min_index]
min_index = min_index + nb
if previousResult.max_y_value < max_y_value:
max_y_value = previousResult.max_y_value
if previousResult.min_location_y < min_location_y:
# Update and return the previous result
return MinData(
previousResult.min_index,
previousResult.min_location_y,
previousResult.min_location_x,
max_y_value,
nb + len(xx),
)
# Update and new return the previous result
result = MinData(
min_index, min_location_y, min_location_x, max_y_value, nb + len(xx)
)
return result
......@@ -41,6 +41,23 @@ def test_max_compute():
assert result.min_y_value == -10
def test_min_compute():
scan = None
yy = [0, -10, 2, 5, 9, 500, 100]
xx = numpy.arange(len(yy)) * 10
item = plot_state_model.MinCurveItem()
curveItem = CurveMock(xx=xx, yy=yy)
item.setSource(curveItem)
result = item.compute(scan)
assert result.nb_points == len(xx)
assert result.min_index == 1
assert result.min_location_x == 10
assert result.min_location_y == -10
assert result.max_y_value == 500
def test_max_incremental_compute_1():
"""The result is part of the increment"""
scan = None
......
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