1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
from typing import Optional
import numpy as np
TRIM_COLOR = "orange"
BURN_COLOR = "red"
def convergence_plot(
convergence: np.ndarray,
dof: float,
cutoff: float = 0.25,
trim_index: Optional[int] = None,
burn_index: Optional[int] = None,
max_points: Optional[int] = None,
) -> dict:
"""
Generate a convergence plot from the convergence data.
Parameters:
- convergence: A 2D numpy array where the first column contains the best values
and the remaining columns contain the population values.
- dof: Degrees of freedom for normalization.
Returns:
- A dictionary suitable for JSON serialization containing the plot data and layout.
"""
# Normalize the population data
normalized_pop = 2 * convergence / dof
best, pop = normalized_pop[:, 0], normalized_pop[:, 1:]
ni, npop = pop.shape
x = np.arange(1, ni + 1)
tail = int(cutoff * ni)
nout = ni - tail
step = nout // max_points if (max_points is not None and nout > max_points) else 1
x_out = x[tail::step]
best_out = best[tail::step]
pop_out = pop[tail::step, :]
# print(f"Convergence plot: {ni} steps, {npop} population, {nout} output, step={step}")
traces = []
layout = {}
hovertemplate = "(%{{x}}, %{{y}})<br>{label}<extra></extra>"
if npop == 5:
if (trim_index is not None and trim_index > tail) or (burn_index is not None and burn_index > tail):
layout["shapes"] = []
layout["annotations"] = []
# fig['data'].append(dict(type="scattergl", x=x, y=pop[tail:,4].tolist(), name="95%", mode="lines", line=dict(color="lightblue", width=1), showlegend=True, hovertemplate=hovertemplate.format(label="95%")))
if trim_index is not None and trim_index > tail:
layout["shapes"].append(
{
"line": {"color": TRIM_COLOR, "dash": "dash", "width": 2},
"type": "line",
"x0": trim_index,
"x1": trim_index,
"xref": "x",
"y0": 0,
"y1": 0.95,
"yref": "y domain",
},
)
layout["annotations"].append(
{
"text": "←trim<br>use→",
"x": trim_index,
"y": 0.95,
"xref": "x",
"xanchor": "left",
"yref": "y domain",
"showarrow": False,
"font": {"color": TRIM_COLOR, "size": 12},
},
)
if burn_index is not None and burn_index > tail:
layout["shapes"].append(
{
"line": {"color": BURN_COLOR, "dash": "dash", "width": 2},
"type": "line",
"x0": burn_index,
"x1": burn_index,
"xref": "x",
"y0": 0,
"y1": 1,
"yref": "y domain",
}
)
layout["annotations"].append(
{
"text": "←burn<br>samples→",
"x": burn_index,
"y": 1.0,
"xref": "x",
"xanchor": "left",
"yref": "y domain",
"showarrow": False,
"font": {"color": BURN_COLOR, "size": 12},
}
)
traces.append(
dict(
type="scattergl",
x=x_out,
y=pop_out[:, 3],
mode="lines",
line=dict(color="lightgreen", width=0),
showlegend=False,
hovertemplate=hovertemplate.format(label="80%"),
)
)
traces.append(
dict(
type="scattergl",
x=x_out,
y=pop_out[:, 1],
name="20% to 80% range",
fill="tonexty",
mode="lines",
line=dict(color="lightgreen", width=0),
hovertemplate=hovertemplate.format(label="20%"),
)
)
traces.append(
dict(
type="scattergl",
x=x_out,
y=pop_out[:, 2],
name="population median",
mode="lines",
line=dict(color="green"),
opacity=0.5,
)
)
traces.append(
dict(
type="scattergl",
x=x_out,
y=pop_out[:, 0],
name="population best",
mode="lines",
line=dict(color="green", dash="dot"),
)
)
traces.append(
dict(
type="scattergl",
x=x_out,
y=best_out,
name="best",
line=dict(color="red", width=1),
mode="lines",
)
)
layout.update(
dict(
template="simple_white",
legend=dict(x=1, y=1, xanchor="right", yanchor="top"),
)
)
layout.update(dict(title=dict(text="Convergence", xanchor="center", x=0.5)))
layout.update(dict(uirevision=1))
layout.update(
dict(
xaxis=dict(
title={"text": "Steps"},
showline=True,
showgrid=False,
zeroline=False,
)
)
)
layout.update(
dict(
yaxis=dict(
title={"text": "Normalized <i>\u03c7</i><sup>2</sup>"}, showline=True, showgrid=False, zeroline=False
)
)
)
return {
"data": traces,
"layout": layout,
}
|