Skip to content

Reduce code duplication in LinOp tests #503

Open
@Michael-T-McCann

Description

@Michael-T-McCann

Many LinOps have tests for scalar multiplication, i.e., that a * (H @ x) = (a * H) @ x. Right now, this involves code duplication, e.g.,

@pytest.mark.parametrize("operator", [op.mul, op.truediv])
def test_scalar_left(self, axes_shape_spec, operator, jit):
input_dtype = np.float32
scalar = np.float32(3.141)
x_shape, ndims, h_shape = axes_shape_spec
h, key = randn(tuple(h_shape), dtype=input_dtype, key=self.key)
A = CircularConvolve(h, x_shape, ndims, input_dtype, jit=jit)
cA = operator(A, scalar)
np.testing.assert_allclose(operator(A.h_dft.ravel(), scalar), cA.h_dft.ravel(), rtol=5e-5)
versus
@pytest.mark.parametrize("operator", [op.mul, op.truediv])
def test_scalar_left(self, operator):
diagonal_dtype = np.float32
input_shape = (8,)
diagonal1, key = randn(input_shape, dtype=diagonal_dtype, key=self.key)
scalar = np.random.randn()
x, key = randn(input_shape, dtype=diagonal_dtype, key=key)
D = linop.Diagonal(diagonal=diagonal1)
scaled_D = operator(D, scalar)
np.testing.assert_allclose(scaled_D @ x, operator(D @ x, scalar), rtol=5e-5)

Can we create a standard function to test scaling (and other similar LinOp properties) rather than copy/pasting variants of this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsPertaining to SCICO tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions