Skip to content

Commit b02cf21

Browse files
committed
Draw Valley-Bowl backgrounds as filled contours
1 parent 42a062b commit b02cf21

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎tests/trajectories/_plotters.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,50 @@ def __call__(self, ax: plt.Axes) -> None:
312312
)
313313

314314

315+
class FilledContourPlotter(Plotter):
316+
"""
317+
Plotter that draws filled contours of the given values on a regular grid extending between the
318+
provided coordinates.
319+
320+
:param values: Array of shape ``[n_x, n_y]``, where ``values[i, j]`` is the value at the i-th x
321+
coordinate and the j-th y coordinate.
322+
:param x_min: Smallest x coordinate of the grid.
323+
:param x_max: Largest x coordinate of the grid.
324+
:param y_min: Smallest y coordinate of the grid.
325+
:param y_max: Largest y coordinate of the grid.
326+
:param cmap: Name of the matplotlib colormap.
327+
:param alpha: Opacity of the filled contours.
328+
:param levels: Number of contour levels.
329+
"""
330+
331+
def __init__(
332+
self,
333+
values: np.ndarray,
334+
x_min: float,
335+
x_max: float,
336+
y_min: float,
337+
y_max: float,
338+
cmap: str,
339+
alpha: float,
340+
levels: int = 35,
341+
) -> None:
342+
self.values = values
343+
self.x_min = x_min
344+
self.x_max = x_max
345+
self.y_min = y_min
346+
self.y_max = y_max
347+
self.cmap = cmap
348+
self.alpha = alpha
349+
self.levels = levels
350+
351+
def __call__(self, ax: plt.Axes) -> None:
352+
xs = np.linspace(self.x_min, self.x_max, self.values.shape[0])
353+
ys = np.linspace(self.y_min, self.y_max, self.values.shape[1])
354+
ax.contourf(
355+
xs, ys, self.values.T, levels=self.levels, cmap=self.cmap, alpha=self.alpha, zorder=0
356+
)
357+
358+
315359
class LimAdjuster(Plotter):
316360
"""Plotter that adjusts the xlim and ylim of the plot to the specified xlim and ylim."""
317361

‎tests/trajectories/plot_params.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
AxesPlotter,
3232
ContentLimAdjuster,
3333
ContourCirclesPlotter,
34+
FilledContourPlotter,
3435
HeatmapPlotter,
3536
LimAdjuster,
3637
MultiTrajPlotter,
@@ -108,15 +109,14 @@ def main() -> None:
108109
x1 = torch.linspace(adjust_plotter.ylim[0], adjust_plotter.ylim[1], 200)
109110
grid = torch.cartesian_prod(x0, x1)
110111
mean_loss = torch.vmap(objective)(grid).mean(dim=1).reshape(200, 200)
111-
common_plotter += HeatmapPlotter(
112+
common_plotter += FilledContourPlotter(
112113
values=mean_loss.numpy(),
113114
x_min=adjust_plotter.xlim[0],
114115
x_max=adjust_plotter.xlim[1],
115116
y_min=adjust_plotter.ylim[0],
116117
y_max=adjust_plotter.ylim[1],
117-
vmin=float(mean_loss.min()),
118-
vmax=float(mean_loss.max()),
119118
cmap="viridis",
119+
alpha=0.55,
120120
)
121121
elif objective.n_values == 2:
122122
similarities = compute_gradient_cosine_similarities(

‎tests/trajectories/plot_values.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from trajectories._paths import get_value_plots_dir, get_values_dir
2929
from trajectories._plotters import (
3030
ContentLimAdjuster,
31+
FilledContourPlotter,
3132
HeatmapPlotter,
3233
LimAdjuster,
3334
MultiTrajPlotter,
@@ -113,15 +114,14 @@ def main() -> None:
113114
[torch.cdist(chunk, front).min(dim=1).values for chunk in grid.split(512)]
114115
)
115116
distances = distances.reshape(200, 200)
116-
common_plotter += HeatmapPlotter(
117+
common_plotter += FilledContourPlotter(
117118
values=distances.numpy(),
118119
x_min=xlim[0],
119120
x_max=xlim[1],
120121
y_min=ylim[0],
121122
y_max=ylim[1],
122-
vmin=0,
123-
vmax=float(distances.max()),
124123
cmap="magma_r",
124+
alpha=0.5,
125125
)
126126
elif isinstance(objective, WithSPSMappingMixin):
127127
distances = compute_normalized_2d_pf_distances(

0 commit comments

Comments
 (0)