Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop' into release-v24.9.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	noxfile.py
  • Loading branch information
BradyPlanden committed Sep 17, 2024
2 parents edaa711 + 764bfb0 commit b8b8f45
Show file tree
Hide file tree
Showing 20 changed files with 1,301 additions and 135 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@
"contributions": [
"example"
]
},
{
"login": "Dibyendu-IITKGP",
"name": "Dibyendu-IITKGP",
"avatar_url": "https://avatars.githubusercontent.com/u/32595915?v=4",
"profile": "https://github.com/Dibyendu-IITKGP",
"contributions": [
"example"
]
}
],
"contributorsPerLine": 7,
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.6.4"
rev: "v0.6.5"
hooks:
- id: ruff
args: [--fix, --show-fixes]
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Bug Fixes

- [#505](https://github.com/pybop-team/PyBOP/pull/505) - Bug fixes for `LogPosterior` with transformed `GaussianLogLikelihood` likelihood.

## Breaking Changes

# [v24.9.1](https://github.com/pybop-team/PyBOP/tree/v24.9.0) - 2024-09-16
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/muhammedsogut/"><img src="https://avatars.githubusercontent.com/u/34511375?v=4?s=100" width="100px;" alt="Muhammed Nedim Sogut"/><br /><sub><b>Muhammed Nedim Sogut</b></sub></a><br /><a href="https://github.com/pybop-team/PyBOP/commits?author=muhammedsogut" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/MarkBlyth"><img src="https://avatars.githubusercontent.com/u/20501619?v=4?s=100" width="100px;" alt="MarkBlyth"/><br /><sub><b>MarkBlyth</b></sub></a><br /><a href="https://github.com/pybop-team/PyBOP/commits?author=MarkBlyth" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/f-g-r-i-m-m"><img src="https://avatars.githubusercontent.com/u/137511310?v=4?s=100" width="100px;" alt="f-g-r-i-m-m"/><br /><sub><b>f-g-r-i-m-m</b></sub></a><br /><a href="#example-f-g-r-i-m-m" title="Examples">💡</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Dibyendu-IITKGP"><img src="https://avatars.githubusercontent.com/u/32595915?v=4?s=100" width="100px;" alt="Dibyendu-IITKGP"/><br /><sub><b>Dibyendu-IITKGP</b></sub></a><br /><a href="#example-Dibyendu-IITKGP" title="Examples">💡</a></td>
</tr>
</tbody>
</table>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

165 changes: 84 additions & 81 deletions examples/notebooks/equivalent_circuit_identification.ipynb

Large diffs are not rendered by default.

550 changes: 550 additions & 0 deletions examples/notebooks/equivalent_circuit_identification_hppc.ipynb

Large diffs are not rendered by default.

595 changes: 595 additions & 0 deletions examples/notebooks/equivalent_circuit_identification_multipulse.ipynb

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/scripts/spm_MAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
bounds=[0.3, 0.8],
initial_value=0.653,
true_value=parameter_set["Negative electrode active material volume fraction"],
transformation=pybop.LogTransformation(),
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Uniform(0.3, 0.8),
bounds=[0.4, 0.7],
initial_value=0.657,
true_value=parameter_set["Positive electrode active material volume fraction"],
transformation=pybop.LogTransformation(),
),
)

Expand All @@ -44,7 +46,7 @@
),
]
)
values = model.predict(initial_state={"Initial SoC": 0.7}, experiment=experiment)
values = model.predict(initial_state={"Initial SoC": 0.5}, experiment=experiment)
corrupt_values = values["Voltage [V]"].data + np.random.normal(
0, sigma, len(values["Voltage [V]"].data)
)
Expand All @@ -60,7 +62,7 @@

# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.LogPosterior(pybop.GaussianLogLikelihoodKnownSigma(problem, sigma0=sigma))
cost = pybop.LogPosterior(pybop.GaussianLogLikelihood(problem))
optim = pybop.IRPropMin(
cost,
sigma0=0.05,
Expand Down
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def examples(session):
@nox.session
def notebooks(session):
"""Run the Jupyter notebooks."""
session.install("openpyxl", "ipywidgets")
session.install("-e", ".[all,dev]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
Expand All @@ -80,6 +81,7 @@ def notebooks(session):
@nox.session(name="notebooks-overwrite")
def notebooks_overwrite(session):
"""Run the Jupyter notebooks."""
session.install("openpyxl", "ipywidgets")
session.install("-e", ".[all,dev]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
Expand Down
10 changes: 6 additions & 4 deletions pybop/costs/_likelihoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _add_single_sigma(self, index, value):
Parameter(
f"Sigma for output {index+1}",
initial_value=value,
prior=Uniform(0.5 * value, 1.5 * value),
prior=Uniform(1e-3 * value, 1e3 * value),
bounds=[1e-8, 3 * value],
)
)
Expand Down Expand Up @@ -235,10 +235,12 @@ def __init__(
super().__init__(problem=log_likelihood.problem)
self.gradient_step = gradient_step

# Store the likelihood and prior
# Store the likelihood, prior, update parameters and transformation
self.join_parameters(log_likelihood.parameters)
self._log_likelihood = log_likelihood
self._parameters = self._log_likelihood.parameters
self._has_separable_problem = self._log_likelihood.has_separable_problem

for attr in ["transformation", "_has_separable_problem"]:
setattr(self, attr, getattr(log_likelihood, attr))

if log_prior is None:
self._prior = JointLogPrior(*self._parameters.priors())
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/test_optimisation_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class TestOptimisation:

@pytest.fixture(autouse=True)
def setup(self):
self.ground_truth = np.asarray([0.55, 0.55]) + np.random.normal(
loc=0.0, scale=0.05, size=2
self.ground_truth = np.clip(
np.asarray([0.55, 0.55]) + np.random.normal(loc=0.0, scale=0.05, size=2),
a_min=0.4,
a_max=0.75,
)

@pytest.fixture
Expand Down
13 changes: 10 additions & 3 deletions tests/integration/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def noise(self, sigma, values):
pybop.SumofPower,
pybop.Minkowski,
pybop.LogPosterior,
pybop.LogPosterior, # Second for GaussianLogLikelihood
]
)
def cost_cls(self, request):
Expand All @@ -90,13 +91,19 @@ def cost(self, model, parameters, init_soc, cost_cls):
problem = pybop.FittingProblem(model, parameters, dataset)

# Construct the cost
first_map = True
if cost_cls is pybop.GaussianLogLikelihoodKnownSigma:
return cost_cls(problem, sigma0=self.sigma0)
elif cost_cls is pybop.GaussianLogLikelihood:
return cost_cls(problem)
elif cost_cls is pybop.LogPosterior and first_map:
first_map = False
return cost_cls(log_likelihood=pybop.GaussianLogLikelihood(problem))
elif cost_cls is pybop.LogPosterior:
return cost_cls(
pybop.GaussianLogLikelihoodKnownSigma(problem, sigma0=self.sigma0)
log_likelihood=pybop.GaussianLogLikelihoodKnownSigma(
problem, sigma0=self.sigma0
)
)
else:
return cost_cls(problem)
Expand All @@ -114,7 +121,7 @@ def test_thevenin_transformation(self, optimiser, cost):
optim = optimiser(
cost=cost,
sigma0=[0.03, 0.03, 1e-3]
if isinstance(cost, pybop.GaussianLogLikelihood)
if isinstance(cost, (pybop.GaussianLogLikelihood, pybop.LogPosterior))
else [0.03, 0.03],
max_unchanged_iterations=35,
absolute_tolerance=1e-6,
Expand All @@ -125,7 +132,7 @@ def test_thevenin_transformation(self, optimiser, cost):
x, final_cost = optim.run()

# Add sigma0 to ground truth for GaussianLogLikelihood
if isinstance(optim.cost, pybop.GaussianLogLikelihood):
if isinstance(optim.cost, (pybop.GaussianLogLikelihood, pybop.LogPosterior)):
self.ground_truth = np.concatenate(
(self.ground_truth, np.asarray([self.sigma0]))
)
Expand Down
10 changes: 4 additions & 6 deletions tests/unit/test_posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ def prior(self):
def test_log_posterior_construction(self, likelihood, prior):
# Test log posterior construction
posterior = pybop.LogPosterior(likelihood, prior)
keys = likelihood.parameters.keys()

assert posterior._log_likelihood == likelihood
assert posterior._prior == prior

# Test log posterior construction without parameters
likelihood.parameters.priors = None

with pytest.raises(TypeError, match="'NoneType' object is not callable"):
pybop.LogPosterior(likelihood, log_prior=None)
assert posterior.parameters[keys[0]] == likelihood.parameters[keys[0]]
assert posterior.has_separable_problem == likelihood.has_separable_problem
assert posterior.transformation == likelihood.transformation

@pytest.mark.unit
def test_log_posterior_construction_no_prior(self, likelihood):
Expand Down

0 comments on commit b8b8f45

Please sign in to comment.