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

Reduce code duplication in LinOp tests #503

Open
Michael-T-McCann opened this issue Feb 9, 2024 · 0 comments
Open

Reduce code duplication in LinOp tests #503

Michael-T-McCann opened this issue Feb 9, 2024 · 0 comments
Labels
tests Pertaining to SCICO tests

Comments

@Michael-T-McCann
Copy link
Contributor

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?

@bwohlberg bwohlberg added the tests Pertaining to SCICO tests label Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Pertaining to SCICO tests
Projects
None yet
Development

No branches or pull requests

2 participants