Refactor class Power to use pandas dataframes

This commit is contained in:
Jose Gracia 2024-02-20 14:21:12 +01:00
parent eeba6f7942
commit c22959f4b9

View file

@ -34,7 +34,7 @@ def parse_jobid(s):
class Power:
def __init__(self, nodes):
self.nodes = nodes
self.epochs = OrderedDict()
self.epochs = None
self.first_ts = None
self.last_ts = None
self.warnings = ""
@ -83,20 +83,25 @@ class Power:
@classmethod
def from_db(cls, db, jobid, interval, hawk_ai):
all_list = db.db_to_list(jobid, interval, hawk_ai)
if not all_list:
raise RuntimeError
df = db.db_to_pf(jobid, interval, hawk_ai)
power = cls.from_pandas(df, {'time': 'time', 'name': 'node', 'avg': 'power'})
power = cls.from_list(all_list)
return power
power.df = db.db_to_pf(jobid, interval, hawk_ai)
# power.df.to_pickle("pickle_df")
power.by_node = power.df.groupby('name')
power.by_epoch = power.df.groupby('time')
@classmethod
def from_pandas(cls, dataframe, columns):
dataframe.rename(columns=columns, inplace=True)
by_node = dataframe.groupby('node')
nodes = list(by_node.groups.keys())
power = cls(nodes)
power.epochs = dataframe.groupby('time')
power.dataframe = dataframe
power.by_node = power.dataframe.groupby('node')
power.energy = power.summarize_energy()
return power
return power
def to_file(self, jobid, header=""):
"""Dumps power data to file. Returns filename is succesfull and None if unsucessfull."""
fname = self.filename(jobid)
@ -133,7 +138,7 @@ class Power:
def body(self):
_body = ""
for epoch in self.epochs.items():
for epoch in self.epochs:
_body += self.pretty_print(self.summarize_epoch(epoch))
return _body
@ -141,8 +146,8 @@ class Power:
return ts, ""
@staticmethod
def summarize_values(val):
values = np.asarray(val)
def summarize_values(df):
values = df['power'].values
head = values[0]
min, max = values.min(), values.max()
avg, stddev = values.mean(), values.std()
@ -153,7 +158,10 @@ class Power:
ts, values = epoch
return self.summarize_time(ts) \
+ self.summarize_values(values) \
+ tuple(values)
+ self.all_values(values)
def all_values(self, values):
return tuple(values['power'].tolist())
def energy_total(self):
energy = None
@ -165,7 +173,7 @@ class Power:
def energy_node(group):
"""Left-sided Riemann sum is enough, as time is lower bound of bucket"""
delta_t = group["time"].diff().shift(-1)/1000. # in seconds
pow = group["avg"]
pow = group['power']
return (delta_t * pow).iloc[:-1].sum()
def summarize_energy(self):