State and check assumptions in Power.from_list()
This commit is contained in:
parent
fa1f981f89
commit
f58a6d62ab
1 changed files with 23 additions and 2 deletions
|
@ -39,7 +39,14 @@ class Power:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_list(cls, data):
|
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
|
idx_ts = 0; idx_node = 1; idx_value = 2
|
||||||
nodes = list(set([line[idx_node] for line in data]))
|
nodes = list(set([line[idx_node] for line in data]))
|
||||||
cls = Power(nodes)
|
cls = Power(nodes)
|
||||||
|
@ -54,9 +61,23 @@ class Power:
|
||||||
power = l[idx_value]
|
power = l[idx_value]
|
||||||
values[ts].append(power)
|
values[ts].append(power)
|
||||||
|
|
||||||
epochs = sorted(values.keys())
|
epochs = values.keys()
|
||||||
for epoch in epochs:
|
for epoch in epochs:
|
||||||
cls.insert_epoch(epoch, values[epoch])
|
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
|
return cls
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue