Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add naming utility #579

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pybop/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pybamm
import regex as re
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved


def is_numeric(x):
Expand All @@ -11,6 +12,15 @@ def is_numeric(x):
return isinstance(x, (int, float, np.number))


def add_spaces(string):
"""
Return the class name as a string with spaces before each new capitalised word.
"""
re_outer = re.compile(r"([^A-Z ])([A-Z])")
re_inner = re.compile(r"(?<!^)([A-Z])([^A-Z])")
return re_outer.sub(r"\1 \2", re_inner.sub(r" \1\2", string))


class SymbolReplacer:
"""
Helper class to replace all instances of one or more symbols in an expression tree
Expand Down
5 changes: 5 additions & 0 deletions pybop/costs/base_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from numpy import ndarray

from pybop import BaseProblem
from pybop._utils import add_spaces
from pybop.parameters.parameter import Inputs, Parameters


Expand Down Expand Up @@ -196,6 +197,10 @@ def join_parameters(self, parameters):
if original_n_params != self.n_parameters:
self.set_fail_gradient()

@property
def name(self):
return add_spaces(type(self).__name__)

@property
def n_parameters(self):
return len(self._parameters)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies = [
"numpy>=1.16, <2.0",
"scipy>=1.3",
"pints>=0.5",
"regex",
NicolaCourtier marked this conversation as resolved.
Show resolved Hide resolved
]

[project.optional-dependencies]
Expand Down
15 changes: 8 additions & 7 deletions tests/unit/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,20 @@ def design_problem(self, parameters, experiment, signal):
)

@pytest.mark.parametrize(
"cost_class",
"cost_class, expected_name",
[
pybop.DesignCost,
pybop.GravimetricEnergyDensity,
pybop.VolumetricEnergyDensity,
pybop.GravimetricPowerDensity,
pybop.VolumetricPowerDensity,
(pybop.DesignCost, "Design Cost"),
(pybop.GravimetricEnergyDensity, "Gravimetric Energy Density"),
(pybop.VolumetricEnergyDensity, "Volumetric Energy Density"),
(pybop.GravimetricPowerDensity, "Gravimetric Power Density"),
(pybop.VolumetricPowerDensity, "Volumetric Power Density"),
],
)
@pytest.mark.unit
def test_design_costs(self, cost_class, design_problem):
def test_design_costs(self, cost_class, expected_name, design_problem):
# Construct Cost
cost = cost_class(design_problem)
assert cost.name == expected_name

if cost_class in [pybop.DesignCost]:
with pytest.raises(NotImplementedError):
Expand Down
Loading