Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop' into 464-design-updating
Browse files Browse the repository at this point in the history
# Conflicts:
#	examples/notebooks/spm_electrode_design.ipynb
  • Loading branch information
BradyPlanden committed Sep 7, 2024
2 parents 2abc0f2 + 59c8c79 commit 121187c
Show file tree
Hide file tree
Showing 28 changed files with 3,562 additions and 7,261 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- [#411](https://github.com/pybop-team/PyBOP/pull/411) - Updates notebooks with README in `examples/` directory, removes kaleido dependency and moves to nbviewer rendering, displays notebook figures with `notebook_connected` plotly renderer
- [#6](https://github.com/pybop-team/PyBOP/issues/6) - Adds Monte Carlo functionality, with methods based on Pints' algorithms. A base class is added `BaseSampler`, in addition to `PintsBaseSampler`.
- [#353](https://github.com/pybop-team/PyBOP/issues/353) - Allow user-defined check_params functions to enforce nonlinear constraints, and enable SciPy constrained optimisation methods
- [#222](https://github.com/pybop-team/PyBOP/issues/222) - Adds an example for performing and electrode balancing.
Expand Down
43 changes: 43 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Examples

This directory contains example notebooks and scripts demonstrating how to use PyBOP.

## Directory Structure

- `notebooks/`: Jupyter notebooks of example functionality with explanations
- `scripts/`: Python scripts for quick reference and command-line usage
- `standalone/`: Example scripts for using standalone classes

## Notebooks

The `notebooks/` directory contains Jupyter notebooks that provide detailed, interactive examples of various features and use cases. These notebooks include explanations, code snippets, and visualisations.

To view the notebooks with interactive figures without downloading the repository, please use nbviewer:

<div align="center">

[Notebooks on nbviewer](https://nbviewer.org/github/pybop-team/PyBOP/tree/develop/examples/notebooks/)

</div>

## Scripts

The `scripts/` directory contains standalone Python scripts that demonstrate specific tasks or workflows. These scripts are designed for quick reference and can be run directly from the command line.

## Getting Started

1. Clone the repository: `git clone https://github.com/pybop-team/pybop.git`
2. Navigate to the examples directory: `cd pybop/examples`
3. Explore the notebooks and scripts in their respective directories.
4. To run the Jupyter notebooks locally:
- Install Jupyter: `pip install jupyter`
- Start Jupyter Notebook: `jupyter notebook`
- Navigate to the `notebooks/` directory and open the desired notebook

5. To run the Python scripts:
- Install PyBOP: `pip install pybop`
- Run a script using Python: `python scripts/script_name.py`

## Contributing

If you have additional examples or improvements to existing ones, please feel free to submit a pull request. We appreciate your contributions!
1,254 changes: 170 additions & 1,084 deletions examples/notebooks/LG_M50_ECM/1-single-pulse-circuit-model.ipynb

Large diffs are not rendered by default.

196 changes: 180 additions & 16 deletions examples/notebooks/comparing_cost_functions.ipynb

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions examples/notebooks/cost-compute-methods.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n",
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
Expand Down Expand Up @@ -52,7 +72,9 @@
"source": [
"import numpy as np\n",
"\n",
"import pybop"
"import pybop\n",
"\n",
"pybop.PlotlyManager().pio.renderers.default = \"notebook_connected\""
]
},
{
Expand Down Expand Up @@ -356,14 +378,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
142 changes: 132 additions & 10 deletions examples/notebooks/ecm_trust-constr.ipynb

Large diffs are not rendered by default.

2,006 changes: 84 additions & 1,922 deletions examples/notebooks/electrode_balancing.ipynb

Large diffs are not rendered by default.

213 changes: 161 additions & 52 deletions examples/notebooks/equivalent_circuit_identification.ipynb

Large diffs are not rendered by default.

1,913 changes: 150 additions & 1,763 deletions examples/notebooks/multi_model_identification.ipynb

Large diffs are not rendered by default.

1,458 changes: 1,210 additions & 248 deletions examples/notebooks/multi_optimiser_identification.ipynb

Large diffs are not rendered by default.

855 changes: 749 additions & 106 deletions examples/notebooks/optimiser_calibration.ipynb

Large diffs are not rendered by default.

37 changes: 34 additions & 3 deletions examples/notebooks/optimiser_interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,46 @@
"execution_count": null,
"id": "1",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install --upgrade pip ipywidgets -q\n",
"%pip install pybop -q\n",
"\n",
"# Import the necessary libraries\n",
"import numpy as np\n",
"\n",
"import pybop"
"import pybop\n",
"\n",
"pybop.PlotlyManager().pio.renderers.default = \"notebook_connected\""
]
},
{
Expand Down Expand Up @@ -250,7 +281,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
2,103 changes: 174 additions & 1,929 deletions examples/notebooks/pouch_cell_identification.ipynb

Large diffs are not rendered by default.

72 changes: 62 additions & 10 deletions examples/notebooks/solver_selection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@
"outputId": "0d785b07-7cff-4aeb-e60a-4ff5a669afbf"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n",
"/Users/engs2510/Documents/Git/PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip\r\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
Expand All @@ -46,7 +60,9 @@
"import numpy as np\n",
"import pybamm\n",
"\n",
"import pybop"
"import pybop\n",
"\n",
"pybop.PlotlyManager().pio.renderers.default = \"notebook_connected\""
]
},
{
Expand Down Expand Up @@ -172,10 +188,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate IDA KLU solver: 0.477\n",
"Time Evaluate CasADi solver with 'safe' mode: 0.464\n",
"Time Evaluate CasADi solver with 'fast' mode: 0.446\n",
"Time Evaluate CasADi solver with 'fast with events' mode: 0.443\n"
"Time Evaluate IDA KLU solver: 0.303\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'safe' mode: 1.076\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'fast' mode: 1.003\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'fast with events' mode: 1.008\n"
]
}
],
Expand Down Expand Up @@ -208,10 +242,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 IDA KLU solver: 0.997\n",
"Time EvaluateS1 CasADi solver with 'safe' mode: 2.743\n",
"Time EvaluateS1 CasADi solver with 'fast' mode: 2.689\n",
"Time EvaluateS1 CasADi solver with 'fast with events' mode: 2.688\n"
"Time EvaluateS1 IDA KLU solver: 0.671\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'safe' mode: 4.479\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'fast' mode: 3.440\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'fast with events' mode: 3.287\n"
]
}
],
Expand Down Expand Up @@ -253,7 +305,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
201 changes: 188 additions & 13 deletions examples/notebooks/spm_AdamW.ipynb

Large diffs are not rendered by default.

115 changes: 96 additions & 19 deletions examples/notebooks/spm_electrode_design.ipynb

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ def notebooks(session):
)


@nox.session(name="notebooks-overwrite")
def notebooks_overwrite(session):
"""Run the Jupyter notebooks."""
session.install("setuptools", "wheel")
session.install("-e", ".[all,dev]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
session.run(
"pytest",
"--notebooks",
"--nbmake",
"--overwrite",
"--nbmake-timeout=1000",
"examples/",
)


@nox.session(name="tests")
def run_tests(session):
"""Run all the tests."""
Expand Down
10 changes: 2 additions & 8 deletions pybop/plotting/nyquist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from pybop import StandardPlot
from pybop.parameters.parameter import Inputs

Expand All @@ -17,8 +15,7 @@ def nyquist(problem, problem_inputs: Inputs = None, show=True, **layout_kwargs):
Input parameters for the problem. If not provided, the default parameters from the problem
instance will be used. These parameters are verified before use (default is None).
show : bool, optional
If True, the plots will be displayed. If running in an IPython kernel (e.g., Jupyter Notebook),
the plots will be shown using SVG format for better quality (default is True).
If True, the plots will be displayed.
**layout_kwargs : dict, optional
Additional keyword arguments for customising the plot layout. These arguments are passed to
`fig.update_layout()`.
Expand Down Expand Up @@ -134,10 +131,7 @@ def nyquist(problem, problem_inputs: Inputs = None, show=True, **layout_kwargs):

# Overwrite with user-kwargs
fig.update_layout(**layout_kwargs)

if "ipykernel" in sys.modules and show:
fig.show("svg")
elif show:
if show:
fig.show()

figure_list.append(fig)
Expand Down
13 changes: 4 additions & 9 deletions pybop/plotting/plot2d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import warnings
from typing import Union

Expand Down Expand Up @@ -149,8 +148,8 @@ def plot2d(
title_y=0.9,
width=600,
height=600,
xaxis=dict(range=bounds[0]),
yaxis=dict(range=bounds[1]),
xaxis=dict(range=bounds[0], showexponent="last", exponentformat="e"),
yaxis=dict(range=bounds[1], showexponent="last", exponentformat="e"),
)
if hasattr(cost, "parameters"):
name = cost.parameters.keys()
Expand Down Expand Up @@ -204,9 +203,7 @@ def plot2d(

# Update the layout and display the figure
fig.update_layout(**layout_kwargs)
if "ipykernel" in sys.modules and show:
fig.show("svg")
elif show:
if show:
fig.show()

if gradient:
Expand All @@ -225,9 +222,7 @@ def plot2d(
)
grad_fig.update_layout(**layout_kwargs)

if "ipykernel" in sys.modules and show:
grad_fig.show("svg")
elif show:
if show:
grad_fig.show()

# append grad_fig to list
Expand Down
10 changes: 4 additions & 6 deletions pybop/plotting/plot_convergence.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from pybop import StandardPlot


Expand Down Expand Up @@ -35,17 +33,17 @@ def plot_convergence(optim, show=True, **layout_kwargs):
x=iteration_numbers,
y=cost_log,
layout_options=dict(
xaxis_title="Iteration", yaxis_title="Cost", title="Convergence"
xaxis_title="Iteration",
yaxis_title="Cost",
title="Convergence",
),
trace_names=optim.name(),
)

# Generate and display the figure
fig = plot_dict(show=False)
fig.update_layout(**layout_kwargs)
if "ipykernel" in sys.modules and show:
fig.show("svg")
elif show:
if show:
fig.show()

return fig
Loading

0 comments on commit 121187c

Please sign in to comment.