From 4e31a411a0d35e12b2cb4a0578535622c479ff6e Mon Sep 17 00:00:00 2001 From: Nicolas Mounet Date: Thu, 5 Nov 2020 12:08:08 +0100 Subject: [PATCH] Implementing default value beyond the interval of definition, for PiecewiseConstant objects (models.py) --- cara/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cara/models.py b/cara/models.py index 04d8d2af..805993ac 100644 --- a/cara/models.py +++ b/cara/models.py @@ -102,6 +102,7 @@ class PeriodicInterval(Interval): @dataclass(frozen=True) class PiecewiseConstant: + #: transition times at which the function changes value (hours). transition_times: typing.Tuple[float, ...] @@ -115,8 +116,11 @@ class PiecewiseConstant: raise ValueError("transition_times should not contain duplicated elements and should be sorted") def value(self,time) -> float: - if self.transition_times[0] == time: + if time <= self.transition_times[0]: return self.values[0] + if time > self.transition_times[-1]: + return self.values[-1] + for t1,t2,value in zip(self.transition_times[:-1], self.transition_times[1:],self.values): if time > t1 and time <= t2: