From 710c367aeb49b0f1abf7a486a735079a02216fb2 Mon Sep 17 00:00:00 2001 From: Thomas Nipen Date: Sun, 12 May 2024 21:35:56 +0200 Subject: [PATCH] Added -agg change This computes the difference between the last and the first element --- verif/aggregator.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/verif/aggregator.py b/verif/aggregator.py index 116c860..a4639ba 100644 --- a/verif/aggregator.py +++ b/verif/aggregator.py @@ -145,3 +145,25 @@ def __call__(self, array, axis=None): def __eq__(self, other): return (self.__class__ == other.__class__) and (self.quantile == other.quantile) + + +class Change(Aggregator): + """Difference between the last and the first element. Is most useful + when used with -Tagg since it implies an array representing a sequence + such as time. + """ + def __call__(self, array, axis=None): + if axis is None: + return array.flatten()[-1] - array.flatten()[0] + elif axis == 0: + return array[-1, ...] - array[0, ...] + elif axis == 1: + return array[:, -1, ...] - array[:, 0, ...] + elif axis == 2: + return array[:, :, -1, ...] - array[:, :, 0, ...] + elif axis == 3: + return array[:, :, :, -1, ...] - array[:, :, :, 0, ...] + elif axis == 4: + return array[:, :, :, :, -1, ...] - array[:, :, :, :, 0, ...] + else: + raise NotImplementedError(f"This function not implemented for axis {axis}")