Skip to content

Commit

Permalink
Merge pull request #321 from pybop-team/320-bug-fix-integration-tests…
Browse files Browse the repository at this point in the history
…-post-304

Fixes #320
  • Loading branch information
BradyPlanden authored May 17, 2024
2 parents 0d7596c + 0d0e214 commit e149cfb
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 224 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- [#321](https://github.com/pybop-team/PyBOP/pull/321) - Updates Prior classes with BaseClass, adds a `problem.sample_initial_conditions` method to improve stability of SciPy.Minimize optimiser.
- [#249](https://github.com/pybop-team/PyBOP/pull/249) - Add WeppnerHuggins model and GITT example.
- [#304](https://github.com/pybop-team/PyBOP/pull/304) - Decreases the testing suite completion time.
- [#301](https://github.com/pybop-team/PyBOP/pull/301) - Updates default echem solver to "fast with events" mode.
Expand All @@ -19,6 +20,7 @@ codesigned binaries and source distributions via `sigstore-python`.

## Bug Fixes

- [#321](https://github.com/pybop-team/PyBOP/pull/321) - Improves `integration/test_spm_parameterisation.py` stability, adds flakly pytest plugin, and `test_thevenin_parameterisation.py` integration test.
- [#330](https://github.com/pybop-team/PyBOP/issues/330) - Fixes implementation of default plotting options.
- [#317](https://github.com/pybop-team/PyBOP/pull/317) - Installs seed packages into `nox` sessions, ensuring that scheduled tests can pass.
- [#308](https://github.com/pybop-team/PyBOP/pull/308) - Enables testing on both macOS Intel and macOS ARM (Silicon) runners and fixes the scheduled tests.
Expand Down
2 changes: 1 addition & 1 deletion pybop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
#
from .parameters.parameter import Parameter
from .parameters.parameter_set import ParameterSet
from .parameters.priors import Gaussian, Uniform, Exponential
from .parameters.priors import BasePrior, Gaussian, Uniform, Exponential


#
Expand Down
17 changes: 14 additions & 3 deletions pybop/optimisers/scipy_optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, method=None, bounds=None, maxiter=None, tol=1e-5):
super().__init__()
self.method = method
self.bounds = bounds
self.num_resamples = 40
self.tol = tol
self.options = {}
self._max_iterations = maxiter
Expand Down Expand Up @@ -56,11 +57,21 @@ def _runoptimise(self, cost_function, x0):
def callback(x):
self.log.append([x])

# Scale the cost function and eliminate nan values
# Check x0 and resample if required
self.cost0 = cost_function(x0)
self.inf_count = 0
if np.isinf(self.cost0):
raise Exception("The initial parameter values return an infinite cost.")
for i in range(1, self.num_resamples):
x0 = cost_function.problem.sample_initial_conditions(seed=i)
self.cost0 = cost_function(x0)
if not np.isinf(self.cost0):
break
if np.isinf(self.cost0):
raise ValueError(
"The initial parameter values return an infinite cost."
)

# Scale the cost function and eliminate nan values
self.inf_count = 0

def cost_wrapper(x):
cost = cost_function(x) / self.cost0
Expand Down
4 changes: 2 additions & 2 deletions pybop/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
self.set_bounds(bounds)
self.margin = 1e-4

def rvs(self, n_samples):
def rvs(self, n_samples, random_state=None):
"""
Draw random samples from the parameter's prior distribution.
Expand All @@ -59,7 +59,7 @@ def rvs(self, n_samples):
array-like
An array of samples drawn from the prior distribution within the parameter's bounds.
"""
samples = self.prior.rvs(n_samples)
samples = self.prior.rvs(n_samples, random_state=random_state)

# Constrain samples to be within bounds
if self.bounds is not None:
Expand Down
Loading

0 comments on commit e149cfb

Please sign in to comment.