MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / plot_metric_graph

Function plot_metric_graph

monai/utils/jupyter_utils.py:46–90  ·  view source on GitHub ↗

Plot metrics on a single graph with running averages plotted for selected keys. The values in `graphmap` should be lists of (timepoint, value) pairs as stored in MetricLogger objects. Args: ax: Axes object to plot into title: graph title graphmap: dictionary of

(
    ax: plt.Axes,
    title: str,
    graphmap: Mapping[str, list[float] | tuple[list[float], list[float]]],
    yscale: str = "log",
    avg_keys: tuple[str] = (LOSS_NAME,),
    window_fraction: int = 20,
)

Source from the content-addressed store, hash-verified

44
45
46def plot_metric_graph(
47 ax: plt.Axes,
48 title: str,
49 graphmap: Mapping[str, list[float] | tuple[list[float], list[float]]],
50 yscale: str = "log",
51 avg_keys: tuple[str] = (LOSS_NAME,),
52 window_fraction: int = 20,
53) -> None:
54 """
55 Plot metrics on a single graph with running averages plotted for selected keys. The values in `graphmap`
56 should be lists of (timepoint, value) pairs as stored in MetricLogger objects.
57
58 Args:
59 ax: Axes object to plot into
60 title: graph title
61 graphmap: dictionary of named graph values, which are lists of values or (index, value) pairs
62 yscale: scale for y-axis compatible with `Axes.set_yscale`
63 avg_keys: tuple of keys in `graphmap` to provide running average plots for
64 window_fraction: what fraction of the graph value length to use as the running average window
65 """
66 from matplotlib.ticker import MaxNLocator
67
68 for n, v in graphmap.items():
69 if len(v) > 0:
70 if isinstance(v[0], (tuple, list)): # values are (x,y) pairs
71 inds, vals = zip(*v) # separate values into list of indices in X dimension and values
72 else:
73 inds, vals = tuple(range(len(v))), tuple(v) # values are without indices, make indices for them
74
75 ax.plot(inds, vals, label=f"{n} = {vals[-1]:.5g}")
76
77 # if requested compute and plot a running average for the values using a fractional window size
78 if n in avg_keys and len(v) > window_fraction:
79 window = len(v) // window_fraction
80 kernel = np.ones((window,)) / window
81 ra = np.convolve((vals[0],) * (window - 1) + vals, kernel, mode="valid")
82
83 ax.plot(inds, ra, label=f"{n} Avg = {ra[-1]:.5g}")
84
85 ax.set_title(title)
86 ax.set_yscale(yscale)
87 ax.axis("on")
88 ax.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.0)
89 ax.grid(True, "both", "both")
90 ax.xaxis.set_major_locator(MaxNLocator(integer=True))
91
92
93def plot_metric_images(

Callers 1

plot_metric_imagesFunction · 0.85

Calls 1

plotMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…