Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions src/inversion_ideas/base/objective_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def n_params(self) -> int:

def __call__(self, model: Model):
if self.multiplier == 0.0:
return self.multiplier
return 0.0
return self.multiplier * self.function(model)

def gradient(self, model: Model) -> npt.NDArray[np.float64]:
Expand All @@ -298,7 +298,6 @@ def hessian(
self, model: Model
) -> npt.NDArray[np.float64] | SparseArray | LinearOperator:
if self.multiplier == 0.0:
# TODO: replace this with a Zero operator? # ruff: ignore[FIX002]
shape = (self.n_params, self.n_params)
return csr_array(shape, dtype=np.float64)
return self.multiplier * self.function.hessian(model)
Expand Down
61 changes: 52 additions & 9 deletions tests/base/test_objective_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,46 +744,89 @@ def test_hessian_diagonal(self, model, hessian_type):
self.scalar * phi.hessian_diagonal(model),
)

def test_call_null(self, model):
@pytest.mark.parametrize("multiplier_type", ["float", "Multiplier"])
def test_call_null(self, model, multiplier_type):
"""Test calling a Scaled with a zero multiplier."""
# Define an objective function that will fail if called.
# This way we can check if the Scaled returns the 0.0 float directly without
# calling the function.
phi = Failed(self.n_params)
with pytest.raises(NotImplementedError):
phi(model)

scaled = 0.0 * phi
# Define a null multiplier
match multiplier_type:
case "float":
multiplier = 0.0
case "Multiplier":
multiplier = Multiplier(0.0)
case _: # pragma: nocover
raise ValueError()

scaled = multiplier * phi
result = scaled(model)
assert np.isscalar(result)
assert isinstance(result, float)
assert result == 0.0

def test_gradient_null(self, model):
@pytest.mark.parametrize("multiplier_type", ["float", "Multiplier"])
def test_gradient_null(self, model, multiplier_type):
"""Test gradient of Scaled with a zero multiplier."""
phi = Failed(self.n_params)
with pytest.raises(NotImplementedError):
phi.gradient(model)

scaled = 0.0 * phi
# Define a null multiplier
match multiplier_type:
case "float":
multiplier = 0.0
case "Multiplier":
multiplier = Multiplier(0.0)
case _: # pragma: nocover
raise ValueError()

scaled = multiplier * phi
gradient = scaled.gradient(model)
assert gradient.shape == (self.n_params,)
np.testing.assert_equal(gradient, 0.0)

def test_hessian_null(self, model):
@pytest.mark.parametrize("multiplier_type", ["float", "Multiplier"])
def test_hessian_null(self, model, multiplier_type):
"""Test hessian of Scaled with a zero multiplier."""
phi = Failed(self.n_params)
with pytest.raises(NotImplementedError):
phi.hessian(model)

scaled = 0.0 * phi
# Define a null multiplier
match multiplier_type:
case "float":
multiplier = 0.0
case "Multiplier":
multiplier = Multiplier(0.0)
case _: # pragma: nocover
raise ValueError()

scaled = multiplier * phi
hessian = scaled.hessian(model)
assert hessian.shape == (self.n_params, self.n_params)
np.testing.assert_equal(hessian.toarray(), 0.0)

def test_hessian_diagonal_null(self, model):
@pytest.mark.parametrize("multiplier_type", ["float", "Multiplier"])
def test_hessian_diagonal_null(self, model, multiplier_type):
"""Test hessian_diagonal of Scaled with a zero multiplier."""
phi = Failed(self.n_params)
with pytest.raises(NotImplementedError):
phi.hessian_diagonal(model)

scaled = 0.0 * phi
# Define a null multiplier
match multiplier_type:
case "float":
multiplier = 0.0
case "Multiplier":
multiplier = Multiplier(0.0)
case _: # pragma: nocover
raise ValueError()

scaled = multiplier * phi
hessian_diagonal = scaled.hessian_diagonal(model)
assert hessian_diagonal.shape == (self.n_params,)
np.testing.assert_equal(hessian_diagonal, 0.0)
Expand Down
Loading