diff --git a/pybop/costs/_likelihoods.py b/pybop/costs/_likelihoods.py index 1150ce62a..eb7610ef3 100644 --- a/pybop/costs/_likelihoods.py +++ b/pybop/costs/_likelihoods.py @@ -211,7 +211,17 @@ class LogPosterior(BaseLikelihood): Computes the log posterior which is proportional to the sum of the log likelihood and the log prior. - Inherits all parameters and attributes from ``BaseLikelihood``. + Parameters + ---------- + log_likelihood : BaseLikelihood + The likelihood class of type ``BaseLikelihood``. + log_prior : Optional, Union[pybop.BasePrior, stats.rv_continuous] + The prior class of type ``BasePrior`` or ``stats.rv_continuous``. + If not provided, the prior class will be taken from the parameter priors + constructed in the `pybop.Parameters` class. + gradient_step : float, default: 1e-3 + The step size for the finite-difference gradient calculation + if the ``log_prior`` is not of type ``BasePrior``. """ def __init__( @@ -260,7 +270,7 @@ def compute( self.verify_args(dy, calculate_grad) if calculate_grad: - if hasattr(self._prior, "logpdfS1"): + if isinstance(self._prior, BasePrior): log_prior, dp = self._prior.logpdfS1(self.parameters.current_value()) else: # Compute log prior first diff --git a/pybop/plotting/plot2d.py b/pybop/plotting/plot2d.py index efe12486e..09a49c0ad 100644 --- a/pybop/plotting/plot2d.py +++ b/pybop/plotting/plot2d.py @@ -4,7 +4,7 @@ import numpy as np from scipy.interpolate import griddata -from pybop import BaseOptimiser, Optimisation, PlotlyManager +from pybop import BaseCost, BaseOptimiser, Optimisation, PlotlyManager def plot2d( @@ -64,11 +64,11 @@ def plot2d( cost = cost_or_optim plot_optim = False - if hasattr(cost, "parameters") and len(cost.parameters) < 2: + if isinstance(cost, BaseCost) and len(cost.parameters) < 2: raise ValueError("This cost function takes fewer than 2 parameters.") additional_values = [] - if hasattr(cost, "parameters") and len(cost.parameters) > 2: + if isinstance(cost, BaseCost) and len(cost.parameters) > 2: warnings.warn( "This cost function requires more than 2 parameters. " "Plotting in 2d with fixed values for the additional parameters.",