Refactor contructors for class Power

This commit is contained in:
Jose Gracia 2024-02-22 11:32:20 +01:00
parent d1bae309a9
commit 2436928e7a

View file

@ -32,13 +32,6 @@ def parse_jobid(s):
class Power:
def __init__(self, nodes):
self.nodes = nodes
self.epochs = None
self.first_ts = None
self.last_ts = None
self.warnings = ""
@classmethod
def from_list(cls, data):
"""
@ -48,7 +41,7 @@ class Power:
- data is sorted by timestamp ascending
"""
df = pd.DataFrame(data, columns=['time', 'node', 'power'])
power = cls.from_pandas(df, columns={})
power = cls(df, columns={})
return power
@ -57,25 +50,31 @@ class Power:
df = db.db_to_pf(jobid, interval, hawk_ai)
if df.empty:
raise RuntimeError
power = cls.from_pandas(df, {'time': 'time', 'name': 'node', 'avg': 'power'})
power = cls(df, {'time': 'time', 'name': 'node', 'avg': 'power'})
return power
@classmethod
def from_pandas(cls, dataframe, columns):
def __init__(self, dataframe, columns={}):
if columns:
dataframe.rename(columns=columns, inplace=True)
_required_cols = {'time', 'node', 'power'}
if not _required_cols.issubset(set(dataframe.columns)):
raise RuntimeError
if not dataframe['time'].is_monotonic_increasing:
raise RuntimeError
by_node = dataframe.groupby('node')
nodes = list(by_node.groups.keys())
epochs = dataframe.groupby('time')
times = list(epochs.groups.keys())
power = cls(nodes)
power.epochs = dataframe.groupby('time')
times = list(power.epochs.groups.keys())
power.first_ts, power.last_ts = times[0], times[-1]
power.dataframe = dataframe
power.by_node = power.dataframe.groupby('node')
power.energy = power.summarize_energy()
return power
self.dataframe = dataframe
self.nodes = nodes
self.epochs = epochs
self.by_node = by_node
self.first_ts, self.last_ts = times[0], times[-1]
self.warnings = "" # add check for warning, i.e. data gaps due to missing nodes
self.energy = self.summarize_energy()
def to_file(self, jobid, header=""):
"""Dumps power data to file. Returns filename is succesfull and None if unsucessfull."""