Skip to content

Commit

Permalink
fix pylint line-too-long errors in tests (#697)
Browse files Browse the repository at this point in the history
* fix pylint line-too-long errors in tests

* fix linebreak in error message issues and tox line

* fix failed checks on problinsolve.py

* fix small CI/CD with black and isort
  • Loading branch information
alanchen-1 authored Apr 9, 2022
1 parent 275c492 commit 25479a1
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def _setup(self):

def test_call(self, fitzhughnagumo):

# not-yet-registered ODE raises errors as soon as the approximate info operator is called.
# not-yet-registered ODE raises errors as soon as the approximate
# info operator is called.
with pytest.raises(ValueError):
approx_transition = self.approx_strategy(self.info_op).as_transition()
approx_transition.forward_realization(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def test_ivp_to_regression_problem(

# If an approximation strategy is passed, the output should be an EKF component
# which should suppoert forward_rv().
# If not, the output is a generic NonlinearGaussian (which has been tested for above.
# Recall that DiscreteEKFComponent implements NonlinearGaussian.)
# If not, the output is a generic NonlinearGaussian(which has been
# tested for above. Recall that DiscreteEKFComponent implements NonlinearGaussian.)
if approx_strategy is not None:
assert isinstance(
regprob.measurement_models[1],
Expand Down
35 changes: 26 additions & 9 deletions tests/test_linalg/test_problinsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def test_randvar_output(self):
self.assertIsInstance(
rv,
randvars.RandomVariable,
msg="Output of probabilistic linear solver is not a random variable.",
msg=(
"Output of probabilistic linear solver "
"is not a random variable."
),
)

def test_symmetric_posterior_params(self):
Expand Down Expand Up @@ -209,11 +212,15 @@ def test_residual_matches_error(self):
self.assertAlmostEqual(
info["resid_l2norm"],
np.linalg.norm(A @ x_est.mean - b),
msg="Residual in output info does not match l2-error of solution estimate.",
msg=(
"Residual in output info does not match "
"l2-error of solution estimate."
),
)

# def test_solution_equivalence(self):
# """The iteratively computed solution should match the induced solution estimate: x_k = E[A^-1] b"""
# """The iteratively computed solution should match the induced
# solution estimate: x_k = E[A^-1] b"""
# A, f = self.poisson_linear_system
#
# for matblinsolve in self.matblinsolvers:
Expand All @@ -222,9 +229,13 @@ def test_residual_matches_error(self):
# u_solver, Ahat, Ainvhat, info = matblinsolve(A=A, b=f)
#
# # E[x] = E[A^-1] b
# self.assertAllClose(u_solver.mean, (Ainvhat @ f[:, None]).mean.ravel(), rtol=1e-5,
# msg="Solution from matrix-based probabilistic linear solver does not match the " +
# "estimated inverse, i.e. x =/= Ainv @ b ")
# self.assertAllClose(u_solver.mean,
# (Ainvhat @ f[:, None]).mean.ravel(),
# rtol=1e-5,
# msg=(
# "Solution from matrix-based probabilistic linear solver "
# does not match the estimated inverse, i.e. x =/= Ainv @ b "
# ))

def test_posterior_uncertainty_zero_in_explored_space(self):
"""Test whether the posterior uncertainty over the matrices A and Ainv is zero
Expand Down Expand Up @@ -316,11 +327,14 @@ def test_matrixprior(self):
x_true,
rtol=1e-6,
atol=1e-6,
msg="Solution for matrixvariate prior does not match true solution.",
msg=(
"Solution for matrixvariate prior does not match true solution."
),
)

def test_searchdir_conjugacy(self):
"""Search directions should remain A-conjugate up to machine precision, i.e. s_i^T A s_j = 0 for i != j."""
"""Search directions should remain A-conjugate up to machine precision,
i.e. s_i^T A s_j = 0 for i != j."""
searchdirs = []

# Define callback function to obtain search directions
Expand Down Expand Up @@ -424,7 +438,10 @@ def test_iterative_covariance_trace_update(self):
self.assertAlmostEqual(
info["trace_sol_cov"],
x_est.cov.trace(),
msg="Iteratively computed trace not equal to trace of solution covariance.",
msg=(
"Iteratively computed trace not equal to "
"trace of solution covariance."
),
)

def test_uncertainty_calibration_error(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def dense_matrix_based_update(
np.testing.assert_allclose(
updated_belief.A.cov.todense(),
A_naive.cov.todense(),
err_msg="Covariance of system matrix estimate does not match naive implementation.",
err_msg=(
"Covariance of system matrix estimate does not match naive implementation."
),
)

# Inverse
Expand All @@ -105,5 +107,7 @@ def dense_matrix_based_update(
np.testing.assert_allclose(
updated_belief.Ainv.cov.todense(),
Ainv_naive.cov.todense(),
err_msg="Covariance of matrix inverse estimate does not match naive implementation.",
err_msg=(
"Covariance of matrix inverse estimate does not match naive implementation."
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def dense_matrix_based_update(
np.testing.assert_allclose(
updated_belief.A.cov.todense(),
A_naive.cov.todense(),
err_msg="Covariance of system matrix estimate does not match naive implementation.",
err_msg=(
"Covariance of system matrix estimate does not match naive implementation."
),
)

# Inverse
Expand All @@ -111,5 +113,7 @@ def dense_matrix_based_update(
np.testing.assert_allclose(
updated_belief.Ainv.cov.todense(),
Ainv_naive.cov.todense(),
err_msg="Covariance of matrix inverse estimate does not match naive implementation.",
err_msg=(
"Covariance of matrix inverse estimate does not match naive implementation."
),
)
3 changes: 2 additions & 1 deletion tests/test_linops/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def test_idkronecker_matmul():
k1 = IdentityKronecker(4, np.random.rand(2, 3)) # (8, 12)
k2 = IdentityKronecker(2, np.random.rand(6, 2)) # (12, 4)

# Even though the shapes fit, and IdentityKronecker @ IdentityKronecker = IdentityKronecker ....
# Even though the shapes fit, and
# IdentityKronecker @ IdentityKronecker = IdentityKronecker ....
assert k1.shape[1] == k2.shape[0]

# The result does not have a IdentityKronecker structure
Expand Down
5 changes: 2 additions & 3 deletions tests/test_linops/test_arithmetics_fallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import numpy as np
import pytest

from probnum.linops._arithmetic_fallbacks import ( # NegatedLinearOperator,; ProductLinearOperator,; SumLinearOperator,
ScaledLinearOperator,
)
# NegatedLinearOperator,; ProductLinearOperator,; SumLinearOperator,;
from probnum.linops._arithmetic_fallbacks import ScaledLinearOperator
from probnum.linops._linear_operator import Matrix
from probnum.problems.zoo.linalg import random_spd_matrix

Expand Down
10 changes: 7 additions & 3 deletions tests/test_randvars/test_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ def test_normal_dimension_mismatch(self):
(np.array([[-1, 0], [2, 1]]), np.eye(3)),
]:
with self.subTest():
err_msg = "Mean and kernels mismatch in normal distribution did not raise a ValueError."
err_msg = (
"Mean and kernels mismatch in normal distribution "
"did not raise a ValueError."
)
with self.assertRaises(ValueError, msg=err_msg):
assert randvars.Normal(mean=mean, cov=cov)

Expand Down Expand Up @@ -204,8 +207,9 @@ def test_symmetric_samples(self):
B.T,
atol=1e-5,
rtol=1e-5,
msg="Sample {} from symmetric Kronecker distribution is not symmetric.".format(
i
msg=(
f"Sample {i} from symmetric Kronecker "
"distribution is not symmetric."
),
)

Expand Down
12 changes: 8 additions & 4 deletions tests/testing/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ def chi_squared_statistic(realisations, means, covs):
with (assumed) means :math:`m_1, ..., m_N` and covariances
:math:`C_1, ..., C_N`, compute the value
.. math:`\\chi^2 = \\frac{1}{Nd} \\sum_{n=1}^N (x_n - m_n)^\\top C_n^{-1}(x_n - m_n).`
.. math:`\\chi^2
= \\frac{1}{Nd} \\sum_{n=1}^N (x_n - m_n)^\\top C_n^{-1}(x_n - m_n).`
If it is roughly equal to 1, the samples are likely to correspond to given mean and covariance.
If it is roughly equal to 1, the samples are likely to correspond to given
mean and covariance.
Parameters
----------
realisations : array_like
:math:`N` realisations of a :math:`d`-dimensional random variable. Shape (N, d).
means : array_like
:math:`N`, :math:`d`-dimensional (assumed) means of a random variable. Shape (N, d).
:math:`N`, :math:`d`-dimensional (assumed) means of a random variable.
Shape (N, d).
realisations : array_like
:math:`N`, :math:`d \\times d`-dimensional (assumed) covariances of a random variable. Shape (N, d, d).
:math:`N`, :math:`d \\times d`-dimensional (assumed) covariances of a random
variable. Shape (N, d, d).
"""
if not realisations.shape == means.shape == covs.shape[:-1]:
print(realisations.shape, means.shape, covs.shape)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ commands =
# pylint benchmarks --disable="unused-argument,attribute-defined-outside-init,missing-function-docstring" --jobs=0 # not a work in progress, but final
pylint benchmarks --disable="unused-argument,attribute-defined-outside-init,no-else-return,no-self-use,consider-using-from-import,missing-module-docstring,missing-class-docstring,missing-function-docstring" --jobs=0
# pylint tests --disable="missing-function-docstring" --jobs=0 # not a work in progress, but final
pylint tests --disable="arguments-differ,redefined-outer-name,too-many-instance-attributes,too-many-arguments,too-many-locals,too-few-public-methods,protected-access,unnecessary-pass,unused-variable,unused-argument,unused-private-member,attribute-defined-outside-init,no-else-return,no-self-use,consider-using-from-import,duplicate-code,line-too-long,missing-module-docstring,missing-class-docstring,missing-function-docstring,missing-param-doc,missing-type-doc,missing-raises-doc,missing-return-type-doc,redundant-returns-doc" --jobs=0
pylint tests --disable="arguments-differ,redefined-outer-name,too-many-instance-attributes,too-many-arguments,too-many-locals,too-few-public-methods,protected-access,unnecessary-pass,unused-variable,unused-argument,unused-private-member,attribute-defined-outside-init,no-else-return,no-self-use,consider-using-from-import,duplicate-code,missing-module-docstring,missing-class-docstring,missing-function-docstring,missing-param-doc,missing-type-doc,missing-raises-doc,missing-return-type-doc,redundant-returns-doc" --jobs=0

0 comments on commit 25479a1

Please sign in to comment.