From 2436928e7ab1b71bb31ff1b88158f7c9c4fd9231 Mon Sep 17 00:00:00 2001 From: Jose Gracia Date: Thu, 22 Feb 2024 11:32:20 +0100 Subject: [PATCH] Refactor contructors for class Power --- monitoring/db/scripts/get_detailed_power.py | 41 ++++++++++----------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/monitoring/db/scripts/get_detailed_power.py b/monitoring/db/scripts/get_detailed_power.py index f56ce3f..898f60f 100755 --- a/monitoring/db/scripts/get_detailed_power.py +++ b/monitoring/db/scripts/get_detailed_power.py @@ -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): - dataframe.rename(columns=columns, inplace=True) + 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."""