State and check assumptions in Power.from_list()

This commit is contained in:
Jose Gracia 2024-02-12 16:45:13 +01:00
parent fa1f981f89
commit f58a6d62ab

View file

@ -39,7 +39,14 @@ class Power:
@classmethod
def from_list(cls, data):
"""Assumes data is a list of tuples (timestamp, node, value)"""
"""
Returns a Power instance from a list of tuples (timestamp, node, value).
Assumptions:
- data is sorted by timestamp first, then by node. Both ascending.
- for each timestamp, there is the same set of nodes.
"""
idx_ts = 0; idx_node = 1; idx_value = 2
nodes = list(set([line[idx_node] for line in data]))
cls = Power(nodes)
@ -54,10 +61,24 @@ class Power:
power = l[idx_value]
values[ts].append(power)
epochs = sorted(values.keys())
epochs = values.keys()
for epoch in epochs:
cls.insert_epoch(epoch, values[epoch])
# check implicit assumptions: 1) ts/epochs are sorted
e = list(epochs)
k = list(values.keys())
if not e == k:
print("Warning: Unexpected unsorted timestamps.")
# check implicit assumptions: 2) each line has #nodes values
nnodes = len(nodes)
for epoch in epochs:
actual = len(values[epoch])
if actual != nnodes:
print("Warning: Unexpected number of nodes ({actual}/{expected})".format(actual=actual, expected=nnodes))
break
return cls
@classmethod