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