Skip to content

Commit c8dabc2

Browse files
author
Circle CI
committed
Update docs for dev (build 4348)
1 parent 011e0c6 commit c8dabc2

132 files changed

Lines changed: 22905 additions & 5118 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev/.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 1b77bf6a6caf24ebdc5f7b8531dc16ff
3+
config: 9f6349362a41aa79b0d87111c8947c2b
44
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file not shown.
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Create and run a Julia solver benchmark\n\nThis example shows how to add a Julia solver in a simple benchmark using\nbenchopt's helpers to call Julia code from Python.\n\nThe benchmark objective is a simple minimization task:\n\n\\begin{align}\\min_{\\hat{X}} \\; \\mathrm{MSE}(X, \\hat{X})\\end{align}\n\nWe define:\n\n- a Python ``Objective`` that evaluates MSE between ``X`` and ``X_hat``;\n- a Python ``Dataset`` that generates a random matrix ``X``;\n- two solvers:\n\n - ``Python-GD`` implemented in Python;\n - ``Julia-GD`` implemented in Julia and called through ``JuliaSolver``.\n\nAt the end, we run the benchmark and display the comparison.\n"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": false
15+
},
16+
"outputs": [],
17+
"source": [
18+
"# Import example helpers to define the benchmark and\n# programmatically call the CLI.\nfrom benchopt.helpers.run_examples import ExampleBenchmark\nfrom benchopt.helpers.run_examples import benchopt_cli\nfrom benchopt.helpers.run_examples import EXAMPLES_ROOT"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"First, we define the initial Python benchmark, based on the benchmark\n``examples/minimal_benchmark``. It contains an ``objective.py`` file,\na simulated dataset and a full python solver based on gradient descent.\n\n"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": false
33+
},
34+
"outputs": [],
35+
"source": [
36+
"benchmark = ExampleBenchmark(\n base=\"minimal_benchmark\", name=\"julia_solver\",\n ignore=[\"custom_plot.py\", \"example_config.yml\"]\n)\nbenchmark"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"We can now add solver in Julia with the same algorithm.\nTo do this, we create a new file ``julia_gd.py`` that defines a solver based\non the ``JuliaSolver`` class. This class provides helpers to call Julia code\nfrom Python, and to define the dependencies of the solver.\nThe Julia code is defined in a separate file ``julia_gd.jl``, that is loaded\nand called from the Python solver.\n\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {
50+
"collapsed": false
51+
},
52+
"outputs": [],
53+
"source": [
54+
"JULIA_SOLVER = EXAMPLES_ROOT / \"language_solvers\" / \"julia_gd.py\"\nJULIA_SOLVER_PY = JULIA_SOLVER.read_text(encoding=\"utf-8\")\nJULIA_SOLVER_JL = JULIA_SOLVER.with_suffix(\".jl\").read_text(encoding=\"utf-8\")\n\nbenchmark.update(\n solvers={\"julia_gd.py\": JULIA_SOLVER_PY, \"julia_gd.jl\": JULIA_SOLVER_JL},\n)"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"In order to load the Julia interpreter, we use ``get_jl_interpreter``. This\nfunction returns a ``Julia`` object from ``PyJulia``, that can be used to\ninteract with Julia. In particular, we can use the ``include`` method to load\na Julia file and retrieve the functions defined in it as attributes of the\nreturned object.\n\nNote that the Julia solver cannot call a Python callback to report\nintermediate results, so we call iteratively the Julia solver with a growing\nnumber of iterations to be able to report the curve of the convergence.\n\n"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"To be able to run this benchmark, we need to install its dependencies. We can\ndo this with ``benchopt install``, using with the ``-s`` option which allow\nto select only this solver if multiple solvers are present. If Julia is not\navailable in your environment, this command will use ``conda`` to install it.\n\n"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {
75+
"collapsed": false
76+
},
77+
"outputs": [],
78+
"source": [
79+
"benchopt_cli(f\"install {benchmark.benchmark_dir} -s julia-gd\")"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"Then, we can run the benchmark and show the comparison.\n\n"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {
93+
"collapsed": false
94+
},
95+
"outputs": [],
96+
"source": [
97+
"benchopt_cli(f\"run {benchmark.benchmark_dir} -n 20 -r 4\")"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"Here, you see that the Julia solver is faster than the Python one.\nYou also notice that the first iteration seems to take much longer than the\nother, hinting to a loading time for the solver.\n\n"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.12.13"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 0
129+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
r"""Create and run an R solver benchmark
2+
====================================
3+
4+
This example shows how to add an R solver in a simple benchmark using
5+
benchopt's helpers to call R code from Python.
6+
7+
The benchmark objective is a simple minimization task:
8+
9+
.. math::
10+
11+
\min_{\hat{X}} \; \mathrm{MSE}(X, \hat{X})
12+
13+
We define:
14+
15+
- a Python ``Objective`` that evaluates MSE between ``X`` and ``X_hat``;
16+
- a Python ``Dataset`` that generates a random matrix ``X``;
17+
- two solvers:
18+
19+
- ``Python-GD`` implemented in Python;
20+
- ``R-PGD`` implemented in R and called through ``rpy2``.
21+
22+
At the end, we run the benchmark and display the comparison.
23+
"""
24+
25+
# Import example helpers to define the benchmark and
26+
# programmatically call the CLI.
27+
from benchopt.helpers.run_examples import ExampleBenchmark
28+
from benchopt.helpers.run_examples import benchopt_cli
29+
from benchopt.helpers.run_examples import EXAMPLES_ROOT
30+
31+
32+
# %%
33+
# First, we define the initial Python benchmark, based on the benchmark
34+
# ``examples/minimal_benchmark``. It contains an ``objective.py`` file,
35+
# a simulated dataset and a full python solver based on gradient descent.
36+
37+
benchmark = ExampleBenchmark(
38+
base="minimal_benchmark", name="r_solver",
39+
ignore=["custom_plot.py", "example_config.yml"]
40+
)
41+
benchmark
42+
43+
# %%
44+
# We can now add a solver in R with the same algorithm.
45+
# To do this, we create a new file ``r_pgd.py`` that defines a solver calling
46+
# an R function via ``benchopt.helpers.r_lang`` and ``rpy2``.
47+
#
48+
# The R code is defined in a separate file ``r_pgd.R``, loaded from Python.
49+
50+
R_SOLVER = EXAMPLES_ROOT / "language_solvers" / "r_pgd.py"
51+
R_SOLVER_PY = R_SOLVER.read_text(encoding="utf-8")
52+
R_SOLVER_R = R_SOLVER.with_suffix(".R").read_text(encoding="utf-8")
53+
54+
benchmark.update(
55+
solvers={"r_pgd.py": R_SOLVER_PY, "r_pgd.R": R_SOLVER_R},
56+
)
57+
58+
# %%
59+
# To run this benchmark, we need to install solver dependencies.
60+
# We use ``benchopt install`` with ``-s`` to select only this solver.
61+
# If R is not available in your environment, this command can install it
62+
# through conda using the solver requirements.
63+
64+
benchopt_cli(f"install {benchmark.benchmark_dir} -s r-pgd")
65+
66+
# %%
67+
# Then, we can run the benchmark and show the comparison.
68+
69+
benchopt_cli(f"run {benchmark.benchmark_dir} -n 20 -r 4")
70+
71+
# %%
72+
# Here, you should see that the R solver and Python solver obtain similar
73+
# convergence profiles, with runtime differences depending on your setup.

dev/_downloads/46d3545d2de3fcee6756b7d2ee7fb39f/plot_run_benchmark_python_R.py

Lines changed: 0 additions & 40 deletions
This file was deleted.
Binary file not shown.

dev/_downloads/6a65fd6b50c38318be8d3d22e519e51b/run_minimal_benchmark.ipynb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Running an existing benchmark\n\nThis Example demonstrates how to run an existing benchmark with benchopt.\nIt uses the `benchopt_run` helper function to run the benchmark, which runs\nprogrammatically the equivalent of the command line interface:\n"
7+
"# Running an existing benchmark\n\nThis example demonstrates how to run an existing benchmark with benchopt.\n"
88
]
99
},
1010
{
@@ -15,7 +15,25 @@
1515
},
1616
"outputs": [],
1717
"source": [
18-
"from benchopt.helpers.run_examples import benchopt_run"
18+
"# Import example helpers to define the benchmark and\n# programmatically call the CLI.\nfrom benchopt.helpers.run_examples import ExampleBenchmark\nfrom benchopt.helpers.run_examples import benchopt_cli"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"We will use the minimal benchmark defined in the ``examples`` folder.\nThe benchmark objective is a simple minimization task:\n\n\\begin{align}\\min_{\\hat{X}} \\; \\mathrm{MSE}(X, \\hat{X})\\end{align}\n\nWe define:\n\n- an ``Objective`` that evaluates MSE between ``X`` and ``X_hat``;\n- a ``Dataset`` that generates a random matrix ``X``;\n- a ``Solver`` that minimizes this objective with gradient descent. It is\n parametrized with parameter ``lr`` for the step size.\n\n"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": false
33+
},
34+
"outputs": [],
35+
"source": [
36+
"benchmark = ExampleBenchmark(\n base=\"minimal_benchmark\", name=\"minimal_benchmark\",\n ignore=[\"custom_plot.py\", \"example_config.yml\"]\n)\nbenchmark"
1937
]
2038
},
2139
{
@@ -33,7 +51,7 @@
3351
},
3452
"outputs": [],
3553
"source": [
36-
"benchopt_run('minimal_benchmark', n=20, r=2)"
54+
"benchopt_cli(f\"run {benchmark.benchmark_dir} -n 20 -r 2\")"
3755
]
3856
},
3957
{
@@ -51,7 +69,7 @@
5169
},
5270
"outputs": [],
5371
"source": [
54-
"benchopt_run('minimal_benchmark', n=30, r=5)"
72+
"benchopt_cli(f\"run {benchmark.benchmark_dir} -n 30 -r 5\")"
5573
]
5674
},
5775
{
@@ -78,7 +96,7 @@
7896
"name": "python",
7997
"nbconvert_exporter": "python",
8098
"pygments_lexer": "ipython3",
81-
"version": "3.10.20"
99+
"version": "3.12.13"
82100
}
83101
},
84102
"nbformat": 4,
Binary file not shown.

dev/_downloads/95cd19d514d099dccb635d0bbfe85947/run_minimal_benchmark.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
"""Running an existing benchmark
22
=============================
33
4-
This Example demonstrates how to run an existing benchmark with benchopt.
5-
It uses the `benchopt_run` helper function to run the benchmark, which runs
6-
programmatically the equivalent of the command line interface:
4+
This example demonstrates how to run an existing benchmark with benchopt.
75
"""
86

9-
from benchopt.helpers.run_examples import benchopt_run
7+
# Import example helpers to define the benchmark and
8+
# programmatically call the CLI.
9+
from benchopt.helpers.run_examples import ExampleBenchmark
10+
from benchopt.helpers.run_examples import benchopt_cli
11+
12+
# %%
13+
# We will use the minimal benchmark defined in the ``examples`` folder.
14+
# The benchmark objective is a simple minimization task:
15+
#
16+
# .. math::
17+
#
18+
# \min_{\hat{X}} \; \mathrm{MSE}(X, \hat{X})
19+
#
20+
# We define:
21+
#
22+
# - an ``Objective`` that evaluates MSE between ``X`` and ``X_hat``;
23+
# - a ``Dataset`` that generates a random matrix ``X``;
24+
# - a ``Solver`` that minimizes this objective with gradient descent. It is
25+
# parametrized with parameter ``lr`` for the step size.
26+
27+
benchmark = ExampleBenchmark(
28+
base="minimal_benchmark", name="minimal_benchmark",
29+
ignore=["custom_plot.py", "example_config.yml"]
30+
)
31+
benchmark
1032

1133
# %%
1234
# To run the benchmark, just execute:
1335
#
1436

15-
benchopt_run('minimal_benchmark', n=20, r=2)
37+
benchopt_cli(f"run {benchmark.benchmark_dir} -n 20 -r 2")
1638

1739
# %%
1840
# This runs the benchmark named ``minimal_benchmark`` located in the
@@ -24,7 +46,7 @@
2446
#
2547
# To get a more precise curve, you can increase ``n`` and ``r``:
2648

27-
benchopt_run('minimal_benchmark', n=30, r=5)
49+
benchopt_cli(f"run {benchmark.benchmark_dir} -n 30 -r 5")
2850

2951
# %%
3052
# Here, the display is not ideal because both solvers reach convergence very

0 commit comments

Comments
 (0)