|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +from tensorboard.backend.event_processing import event_accumulator |
| 4 | + |
| 5 | +from matplotlib import pyplot as plt |
| 6 | + |
| 7 | +from .pandas import SeriesInterpolationLinearIndex |
| 8 | + |
| 9 | + |
| 10 | +class TensorboardData: |
| 11 | + def __init__(self, events: event_accumulator.EventAccumulator): |
| 12 | + self.events = events |
| 13 | + self.events.Reload() |
| 14 | + |
| 15 | + def get_series(self, tag: str, smoothing_factor: float = 0.0) -> pd.Series: |
| 16 | + """ |
| 17 | + Gets the (smoothed) pandas Series for a specific tensorboard tag. |
| 18 | +
|
| 19 | + :param tag: the tensorboard tag |
| 20 | + :param smoothing_factor: the smoothing factor between 0 and 1 which determines the relative importance of past values. |
| 21 | + 0: no smoothing |
| 22 | + 1: maximum smoothing (all values will be equal to the first value) |
| 23 | + :return: the pandas series with the step as the index |
| 24 | + """ |
| 25 | + if not 0 <= smoothing_factor <= 1: |
| 26 | + raise ValueError("Smoothing factor must be between 0 and 1") |
| 27 | + |
| 28 | + try: |
| 29 | + scalar_events = self.events.Scalars(tag) |
| 30 | + except KeyError: |
| 31 | + raise KeyError(f"Tag '{tag}' not found in tensorboard events") |
| 32 | + |
| 33 | + steps = [event.step for event in scalar_events] |
| 34 | + values = [event.value for event in scalar_events] |
| 35 | + |
| 36 | + if smoothing_factor > 0: |
| 37 | + smoothed_values = [] |
| 38 | + last = values[0] |
| 39 | + for value in values: |
| 40 | + last = smoothing_factor * last + (1 - smoothing_factor) * value |
| 41 | + smoothed_values.append(last) |
| 42 | + values = smoothed_values |
| 43 | + |
| 44 | + return pd.Series(values, index=steps, name=tag) |
| 45 | + |
| 46 | + def get_tags(self) -> list[str]: |
| 47 | + """ |
| 48 | + Get list of available scalar tags in the events. |
| 49 | +
|
| 50 | + :return: list of tag names |
| 51 | + """ |
| 52 | + return self.events.Tags()['scalars'] |
| 53 | + |
| 54 | + def get_data_frame(self, tags: list[str] | None = None, smoothing_factor: float = 0.0) -> pd.DataFrame: |
| 55 | + """ |
| 56 | + Gets multiple series as a DataFrame. |
| 57 | +
|
| 58 | + :param tags: the list of tensorboard tags to consider; if None, use all |
| 59 | + :param smoothing_factor: smoothing factor to apply to all series |
| 60 | + :return: DataFrame with steps as index and tags as columns |
| 61 | + """ |
| 62 | + if tags is None: |
| 63 | + tags = self.get_tags() |
| 64 | + series_dict = {} |
| 65 | + for tag in tags: |
| 66 | + series = self.get_series(tag, smoothing_factor) |
| 67 | + series_dict[series.name] = series |
| 68 | + |
| 69 | + return pd.DataFrame(series_dict) |
| 70 | + |
| 71 | + |
| 72 | +class TensorboardSeriesComparison: |
| 73 | + def __init__(self, tb_reference: TensorboardData, tb_current: TensorboardData, |
| 74 | + tag: str, index_start: int, index_end: int): |
| 75 | + s_ref = tb_reference.get_series(tag) |
| 76 | + s_cur = tb_current.get_series(tag) |
| 77 | + |
| 78 | + interp = SeriesInterpolationLinearIndex(ffill=True, bfill=True) |
| 79 | + s_ref, s_cur = interp.interpolate_all_with_combined_index([s_ref, s_cur]) |
| 80 | + |
| 81 | + self.s_ref = s_ref.loc[index_start:index_end] |
| 82 | + self.s_cur = s_cur.loc[index_start:index_end] |
| 83 | + |
| 84 | + def mean_relative_difference(self): |
| 85 | + """ |
| 86 | + Computes the difference between the current series and the reference series, relative to the reference, |
| 87 | + e.g. if the current series is on average 105% of the reference series (5% relative difference), then |
| 88 | + the value will be 0.05. |
| 89 | + Since we divide by the absolute value of the reference, this also works for negative cases, i.e. |
| 90 | + if the reference series value is -0.10 and the current series value is -0.08, then the relative |
| 91 | + difference is 0.2 (20%). |
| 92 | +
|
| 93 | + :return: the mean relative difference |
| 94 | + """ |
| 95 | + diff = self.s_cur - self.s_ref |
| 96 | + diff_rel = diff / abs(self.s_ref) |
| 97 | + return np.mean(diff_rel) |
| 98 | + |
| 99 | + def plot_series(self, show=False) -> plt.Figure: |
| 100 | + fig = plt.figure() |
| 101 | + self.s_ref.plot() |
| 102 | + self.s_cur.plot() |
| 103 | + plt.title(self.s_ref.name) |
| 104 | + if show: |
| 105 | + plt.show() |
| 106 | + return fig |
0 commit comments