Reimplement from_list in terms of pandas
This commit is contained in:
parent
711daa3a5d
commit
d1bae309a9
1 changed files with 3 additions and 38 deletions
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from collections import OrderedDict
|
||||
import os.path
|
||||
|
||||
|
@ -46,38 +46,9 @@ class Power:
|
|||
|
||||
Assumptions:
|
||||
- data is sorted by timestamp ascending
|
||||
- for each timestamp, there is the same set of nodes and in the same order
|
||||
"""
|
||||
|
||||
idx_ts = 0; idx_node = 1; idx_value = 2
|
||||
nodes = list(OrderedDict.fromkeys([line[idx_node] for line in data])) # preserves order of nodes
|
||||
power = Power(nodes)
|
||||
|
||||
values = {}
|
||||
for l in data:
|
||||
ts = l[idx_ts]
|
||||
if ts not in values:
|
||||
values[ts] = []
|
||||
value = l[idx_value]
|
||||
values[ts].append(value)
|
||||
|
||||
epochs = values.keys()
|
||||
for epoch in epochs:
|
||||
power.insert_epoch(epoch, values[epoch])
|
||||
|
||||
# check implicit assumptions: 1) ts/epochs are sorted
|
||||
e = list(epochs)
|
||||
k = list(values.keys())
|
||||
if not e == k:
|
||||
power.warnings += "# Warning: Unexpected unsorted timestamps.\n"
|
||||
|
||||
# check implicit assumptions: 2) each line has #nodes values
|
||||
nnodes = len(nodes)
|
||||
for epoch in epochs:
|
||||
actual = len(values[epoch])
|
||||
if actual != nnodes:
|
||||
power.warnings += "# Warning: Unexpected number of nodes ({actual}/{expected})\n".format(actual=actual, expected=nnodes)
|
||||
break
|
||||
df = pd.DataFrame(data, columns=['time', 'node', 'power'])
|
||||
power = cls.from_pandas(df, columns={})
|
||||
|
||||
return power
|
||||
|
||||
|
@ -125,12 +96,6 @@ class Power:
|
|||
|
||||
return fname
|
||||
|
||||
def insert_epoch(self, ts, values):
|
||||
self.epochs[ts] = values
|
||||
if not self.first_ts:
|
||||
self.first_ts = ts
|
||||
self.last_ts = ts
|
||||
|
||||
def header(self):
|
||||
hd = "# all timestamp have unit miliseconds since unix epoch\n"
|
||||
hd += "# all power values have unit Watt\n"
|
||||
|
|
Loading…
Reference in a new issue