Skip to content

3d Visualizations of optimization problems #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b8c33dd
Refactored slice_plot to support different projections (3D and contour)
shammeer-s Apr 12, 2025
8dcb9e2
Refactored codebase to decouple data processing logic from visualizat…
shammeer-s Apr 13, 2025
8e2bfe0
Enhance sliceplot with comprehensive docstrings and minor refactoring
shammeer-s Apr 13, 2025
8b4cc8c
Minor bug fixes
shammeer-s Apr 13, 2025
9316ea8
Merge branch 'optimagic-dev:main' into visualization
shammeer-s Apr 23, 2025
7782546
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 24, 2025
7108c7d
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s Apr 24, 2025
1fdde7f
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 27, 2025
8087689
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 27, 2025
834a779
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2025
1164bd4
Add notebook for visualizing 3D slice plots and update sandbox imports
shammeer-s Apr 28, 2025
e995aa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
300a460
Enhance 3D slice plot visualization notebook and refactor plotting fu…
shammeer-s Apr 28, 2025
5ba721c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
1c8243f
Documentation strings are updated for all functions in slice_plot_3d.…
shammeer-s Apr 28, 2025
14d5a32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
5cdfb56
Merge branch 'main' into visualization
timmens Apr 29, 2025
2a53a89
Minor changes according to previous slice_plot.py file logic
shammeer-s May 4, 2025
382cd15
Minor fixes with code login on evaluating kwargs
shammeer-s May 4, 2025
b3caa04
Minor fixes with code login on evaluating kwargs
shammeer-s May 6, 2025
091eb80
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s May 6, 2025
a9405d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
5c08b18
Merge branch 'main' into visualization
timmens May 6, 2025
40464a1
Minor fixes with code login on evaluating kwargs
shammeer-s May 7, 2025
e7b9efa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
6bcf72f
Minor fixes
shammeer-s May 9, 2025
5a8af1c
Refactor slice_plot_3d.py to improve parameter handling and streamlin…
shammeer-s May 12, 2025
0335529
Merge branch 'optimagic-dev:main' into visualization
shammeer-s May 12, 2025
c88f69d
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s May 12, 2025
8d2d2e9
Test cases addition
shammeer-s May 12, 2025
072029d
Merge branch 'main' into visualization
timmens May 19, 2025
e1a838f
Merge branch 'main' into visualization
timmens May 19, 2025
da1055a
Enhance slice_plot_3d functionality with univariate and multivariate …
shammeer-s May 19, 2025
8011650
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s May 19, 2025
a421d74
Minor Fixes
shammeer-s May 19, 2025
f6b9174
Minor document fixes
shammeer-s May 19, 2025
f05b5e4
Test cases fixes
shammeer-s May 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 342 additions & 0 deletions docs/source/how_to/how_to_slice_plot_3d.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Visualizing Objective Functions with `slice_plot_3d`\n",
"\n",
"In optimization, understanding the shape of your objective function is a key step toward choosing the right algorithm.\n",
"\n",
"This notebook introduces the `slice_plot_3d` tool, which provides flexible ways to visualize:\n",
"- Single-parameter sensitivity through **univariate slice plots**,\n",
"- Pairwise interactions through **contour** or **surface plots**,\n",
"- Full parameter relationships through **subplot grids**.\n",
"\n",
"We will progress from basic to advanced usage, learning how to create clean and insightful plots easily.\n"
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"## Univariate slice Plot\n",
"\n",
"We start with a **univariate slice plot**.\n",
"This plots the function along each parameter individually to the function value,\n",
"while fixing others at their current values. This provides a clean view of how sensitive the function is to each parameter separately. We use the **Sphere function**, which sums the squares of each input.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import optimagic as om"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"# Define the Sphere function\n",
"def sphere(params):\n",
" x = np.array(list(params.values()))\n",
" return np.sum(x**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"params = {\"alpha\": 0, \"beta\": 0, \"gamma\": 0, \"delta\": 0}\n",
"bounds = om.Bounds(\n",
" lower={name: -5 for name in params},\n",
" upper={name: i + 2 for i, name in enumerate(params)},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "6",
"metadata": {},
"source": [
"## Univariate slice plot with selected parameters\n",
"\n",
"In many situations, we are interested in exploring only specific parameters.\n",
"Using the `selector` argument, we can restrict the univariate plots to\n",
"chosen parameters — here, we select `\"alpha\"` and `\"beta\"`.\n",
"\n",
"This focuses our visualization on dimensions of interest."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"univariate\",\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "8",
"metadata": {},
"source": [
"## 3D Surface Plot for Two Parameters\n",
"\n",
"To better understand interaction between parameters,\n",
"we can switch to a **3D surface plot**.\n",
"\n",
"Surface plots reveal valleys, ridges, and general landscape shapes clearly.\n",
"Here, we vary `\"alpha\"` and `\"beta\"` simultaneously and plot the resulting surface."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"surface\",\n",
" n_gridpoints=30,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "10",
"metadata": {},
"source": [
"## 2D Contour Plot for Two Parameters\n",
"\n",
"Contour plots offer a 2D view with iso-function-value curves.\n",
"\n",
"They are especially useful for:\n",
"- Finding basins or valleys.\n",
"- Visualizing optimization paths.\n",
"- Detecting steep or flat regions easily.\n",
"\n",
"Again, we use `\"alpha\"` and `\"beta\"` to generate the plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"contour\",\n",
" n_gridpoints=30,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"## Grid View for Multiple Parameters\n",
"When selecting more than two parameters, the slice_plot_3d function automatically constructs a grid-based visualization to analyze both individual and pairwise parameter effects.\n",
"\n",
"- **Diagonal** cells display 1D univariate slice plots, representing the isolated\n",
"effect of each parameter on the function output.\n",
"- **Off-diagonal** cells visualize pairwise interactions between parameters using\n",
"either 3D surface or contour plots.\n",
"\n",
"\n",
"### Single projection type\n",
"##### (eg: `projection: \"surface\"`)\n",
"\n",
"By default, when a single projection type is specified (e.g., \"surface\" or \"contour\"), the following behavior is applied:\n",
"\n",
"- The **lower triangle** of the grid (i.e., plots below the diagonal) displays the\n",
"specified projection type.\n",
"- The **upper triangle** remains empty to avoid redundancy.\n",
"\n",
"This allows for a quick and uncluttered visualization of pairwise parameter interactions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" projection=\"surface\",\n",
" n_gridpoints=20,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"### Multiple projection types\n",
"##### (eg: `projection: {\"lower\": \"surface\", \"upper\": \"contour\"}`)\n",
"\n",
"For enhanced flexibility, slice_plot_3d also supports customizing projection types independently for the upper and lower halves of the grid. This is done by passing a dictionary to the projection argument:\n",
"\n",
"- The **\"lower\"** key controls the projection type for plots below the diagonal.\n",
"- The **\"upper\"** key controls the projection type for plots above the diagonal.\n",
"\n",
"For example, setting \"lower\" to \"surface\" and \"upper\" to \"contour\" enables simultaneous display of both 3D and 2D representations, maximizing interpretability."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {
"jupyter": {
"is_executing": true
}
},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" projection={\"lower\": \"surface\", \"upper\": \"contour\"},\n",
" n_gridpoints=20,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "16",
"metadata": {},
"source": [
"This **dual-projection** layout is particularly useful when analyzing high-dimensional\n",
"functions, as it provides both detailed surface representations and compact contour visualizations in a single coherent grid."
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"## Full Customization of the Visualization\n",
"\n",
"`s‍lice_plot_3d` allows fine control over plot styling:\n",
"\n",
"- `layout_kwargs` adjusts figure size, titles, background themes.\n",
"- `plot_kwargs` controls color maps, marker options, and plot styles.\n",
"- `make_subplot_kwargs` configures grid spacing, axis sharing, and more.\n",
"\n",
"Here, we demonstrate a fully customized plot combining all these features."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"], p[\"gamma\"]],\n",
" projection=\"surface\",\n",
" n_gridpoints=40,\n",
" layout_kwargs={\n",
" \"width\": 800,\n",
" \"height\": 800,\n",
" \"title\": {\"text\": \"Customized Sphere Function Visualization\"},\n",
" \"template\": \"plotly_dark\",\n",
" },\n",
" make_subplot_kwargs={\n",
" \"horizontal_spacing\": 0.1,\n",
" \"vertical_spacing\": 0.1,\n",
" },\n",
" plot_kwargs={\n",
" \"surface_plot\": {\"colorscale\": \"Viridis\", \"opacity\": 0.7},\n",
" },\n",
")\n",
"fig.show(renderer=\"png\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 2 additions & 1 deletion src/optimagic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from optimagic import constraints, mark, timing, utilities
from optimagic import constraints, mark, sandbox, timing, utilities
from optimagic.algorithms import algos
from optimagic.benchmarking.benchmark_reports import (
convergence_report,
Expand Down Expand Up @@ -103,4 +103,5 @@
"__version__",
"algos",
"timing",
"sandbox",
]
3 changes: 3 additions & 0 deletions src/optimagic/sandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from optimagic.visualization.slice_plot_3d import slice_plot_3d

__all__ = ["slice_plot_3d"]
Loading
Loading