Manipulating tuples rather than lists in PiecewiseconstantFunction objects (also in tests), for hashing purposes

This commit is contained in:
Nicolas Mounet 2020-11-04 23:35:06 +01:00
parent d1246c463e
commit 15aee76ca9
2 changed files with 15 additions and 15 deletions

View file

@ -111,7 +111,7 @@ class PiecewiseconstantFunction:
def __post_init__(self):
if len(self.transition_times) != len(self.values)+1:
raise ValueError("transition_times should contain one more element than values")
if sorted(list(set(self.transition_times))) != self.transition_times:
if tuple(sorted(set(self.transition_times))) != self.transition_times:
raise ValueError("transition_times should not contain duplicated elements and should be sorted")
def value(self,time) -> float:
@ -134,8 +134,8 @@ class PiecewiseconstantFunction:
# Geneva hourly temperatures as piecewise constant function (in Kelvin)
GenevaTemperatures = {
month: PiecewiseconstantFunction(list(range(24)),
(273.15+np.array(temperatures)).tolist())
month: PiecewiseconstantFunction(tuple(range(24)),
tuple(273.15+np.array(temperatures)))
for month,temperatures in Geneva_hourly_temperatures_celsius_per_hour.items()
}

View file

@ -152,33 +152,33 @@ def test_expiration_aerosols():
def test_piecewiseconstantfunction_wrongarguments():
# number of values should be 1+number of transition times
pytest.raises(ValueError,models.PiecewiseconstantFunction,[0,1],[0,0])
pytest.raises(ValueError,models.PiecewiseconstantFunction,[0],[0,0])
pytest.raises(ValueError,models.PiecewiseconstantFunction,(0,1),(0,0))
pytest.raises(ValueError,models.PiecewiseconstantFunction,(0,),(0,0))
# two transition times cannot be equal
pytest.raises(ValueError,models.PiecewiseconstantFunction,[0,2,2],[0,0])
pytest.raises(ValueError,models.PiecewiseconstantFunction,(0,2,2),(0,0))
# unsorted transition times are not allowed
pytest.raises(ValueError,models.PiecewiseconstantFunction,[2,0],[0,0])
pytest.raises(ValueError,models.PiecewiseconstantFunction,(2,0),(0,0))
def test_piecewiseconstantfunction():
transition_times = [0,8,16,24]
values = [2,5,8]
transition_times = (0,8,16,24)
values = (2,5,8)
fun = models.PiecewiseconstantFunction(transition_times,values)
assert (fun.value(10) == 5) and (fun.value(20.5) == 8) and \
(fun.value(8) == 2) and (fun.value(0) == 2) and (fun.value(24) == 8)
def test_constantfunction():
transition_times = [0,24]
values = [20]
transition_times = (0,24)
values = (20,)
fun = models.PiecewiseconstantFunction(transition_times,values)
for t in [0,1,8,10,16,20.1,24]:
assert (fun.value(t) == 20)
def test_piecewiseconstantfunction_vs_interval():
transition_times = [0,8,16,24]
values = [0,1,0]
transition_times = (0,8,16,24)
values = (0,1,0)
fun = models.PiecewiseconstantFunction(transition_times,values)
interval = models.SpecificInterval(present_times=[(8,16)])
assert interval.transition_times() == fun.interval().transition_times()
@ -187,8 +187,8 @@ def test_piecewiseconstantfunction_vs_interval():
def test_windowopening():
tempOutside = models.PiecewiseconstantFunction([0,10,24],[273.15,283.15])
tempInside = models.PiecewiseconstantFunction([0,24],[293.15])
tempOutside = models.PiecewiseconstantFunction((0,10,24),(273.15,283.15))
tempInside = models.PiecewiseconstantFunction((0,24),(293.15,))
w = models.WindowOpening(active=models.SpecificInterval([(0,24)]),
inside_temp=tempInside,outside_temp=tempOutside,
window_height=1.,opening_length=0.6)