Skip to content

Commit

Permalink
Merge pull request #150 from nathanaelbosch/pylint
Browse files Browse the repository at this point in the history
Code Linting with Pylint
  • Loading branch information
nathanaelbosch authored Aug 19, 2020
2 parents f0b94f0 + 7c282b3 commit 1ab81a7
Show file tree
Hide file tree
Showing 67 changed files with 747 additions and 907 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/black.yml → .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ jobs:
run: pip install tox
- name: Run black through tox
run: tox -e black

pylint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Install tox
run: pip install tox
- name: Run pylint through tox
run: tox -e pylint
1 change: 0 additions & 1 deletion benchmarks/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

34 changes: 33 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,36 @@ exclude = '''
| build
| dist
)/
'''
'''


[tool.pylint.messages_control]
# Exceptions suggested from Black: bad-continuation, bad-whitespace
disable = """
bad-continuation,
bad-whitespace,
invalid-name,
no-else-return,
no-else-raise,
"""
# Many more `disable`s are defined in `./tox.ini` on a per-module basis!

[tool.pylint.format]
max-line-length = "88"

[tool.pylint.master]
extension-pkg-whitelist = "numpy"
# Extensions that might be of interest in the future:
# pylint.extensions.docstyle,
# pylint.extensions.check_elif,
# pylint.extensions.docparams,
load-plugins = """
pylint.extensions.mccabe,
pylint.extensions.redefined_variable_type,
pylint.extensions.overlapping_exceptions,
"""

[tool.pylint.design]
max-complexity = "14"
1 change: 1 addition & 0 deletions src/probnum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
diffeq
Probabilistic solvers for ordinary differential equations.
"""
# pylint: disable=wrong-import-order

from . import diffeq
from . import filtsmooth
Expand Down
6 changes: 2 additions & 4 deletions src/probnum/diffeq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
"""

from .ode import *
from .odefiltsmooth import *
from .steprule import *
Expand Down Expand Up @@ -30,7 +27,8 @@
"ODESolution",
]

# Set correct module paths (for superclasses). Corrects links and module paths in documentation.
# Set correct module paths (for superclasses).
# Corrects links and module paths in documentation.
ODE.__module__ = "probnum.diffeq"
ODESolver.__module__ = "probnum.diffeq"
StepRule.__module__ = "probnum.diffeq"
3 changes: 0 additions & 3 deletions src/probnum/diffeq/ode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
"""

from .ivp import *
from .ode import *

Expand Down
4 changes: 2 additions & 2 deletions src/probnum/diffeq/ode/ivp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
there might be more ode-based problems,
such as bvp.
"""
# pylint: disable=unused-variable

import numpy as np

Expand Down Expand Up @@ -284,8 +285,7 @@ class IVP(ODE):
"""

def __init__(self, timespan, initrv, rhs, jac=None, hess=None, sol=None):
"""
"""

self.initrv = initrv
super().__init__(timespan=timespan, rhs=rhs, jac=jac, hess=hess, sol=sol)

Expand Down
2 changes: 1 addition & 1 deletion src/probnum/diffeq/ode/ode.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def hessian(self, t, y, **kwargs):
since for any directions :math:`v_1, v_2` the outcome of
:math:`H_f(t_0, y_0) \\cdot v_1 \\cdot v_2` is expected to contain
the incline of :math:`f_i` in direction :math:`(v_1, v_2)`.
"""
""" # pylint: disable=line-too-long
if self._hess is None:
raise NotImplementedError
else:
Expand Down
22 changes: 11 additions & 11 deletions src/probnum/diffeq/odefiltsmooth/ivp2filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"""

import numpy as np
from probnum.filtsmooth import *

from probnum.filtsmooth import ExtendedKalman, UnscentedKalman
from probnum.filtsmooth.statespace.discrete import DiscreteGaussianModel
from probnum.prob import RandomVariable
from probnum.prob.distributions import Normal, Dirac
Expand All @@ -14,19 +15,19 @@ def ivp2ekf0(ivp, prior, evlvar):
"""
Computes measurement model and initial distribution
for KF based on IVP and prior.
**Initialdistribution:**
Conditions the initial distribution of the Gaussian filter
onto the initial values.
- If preconditioning is set to ``False``, it conditions
the initial distribution :math:`\mathcal{N}(0, I)`
the initial distribution :math:`\\mathcal{N}(0, I)`
on the initial values :math:`(x_0, f(t_0, x_0), ...)` using
as many available deri vatives as possible.
- If preconditioning is set to ``True``, it conditions
the initial distribution :math:`\mathcal{N}(0, P P^\\top)`
the initial distribution :math:`\\mathcal{N}(0, P P^\\top)`
on the initial values :math:`(x_0, f(t_0, x_0), ...)` using
as many available derivatives as possible.
Note that the projection matrices :math:`H_0` and :math:`H_1`
Expand All @@ -35,7 +36,7 @@ def ivp2ekf0(ivp, prior, evlvar):
**Measurement model:**
Returns a measurement model :math:`\mathcal{N}(g(m), R)`
Returns a measurement model :math:`\\mathcal{N}(g(m), R)`
involving computing the discrepancy
.. math:: g(m) = H_1 m(t) - f(t, H_0 m(t)).
Expand All @@ -51,7 +52,7 @@ def ivp2ekf0(ivp, prior, evlvar):
model, :math:`H_0` and :math:`H_1`
become :math:`H_0 P^{-1}` and :math:`H_1 P^{-1}` which has
to be taken into account. In this case,
- EKF0 thinks :math:`J_g(m) = H_1 P^{-1}`
- EKF1 thinks :math:`J_g(m) = H_1 P^{-1} - J_f(t, H_0 P^{-1} m(t)) (H_0 P^{-1})^\\top`
- UKF again thinks: ''What is a Jacobian?''
Expand All @@ -65,7 +66,7 @@ def ivp2ekf0(ivp, prior, evlvar):
evlvar : float,
measurement variance; in the literature, this is "R"
"""
""" # pylint: disable=line-too-long
measmod = _measmod_ekf0(ivp, prior, evlvar)
initrv = _initialdistribution(ivp, prior)
return ExtendedKalman(prior, measmod, initrv)
Expand Down Expand Up @@ -143,8 +144,7 @@ def ivp2ukf(ivp, prior, evlvar):


def _measmod_ukf(ivp, prior, measvar):
"""
"""

spatialdim = prior.spatialdim
h0 = prior.proj2coord(coord=0)
h1 = prior.proj2coord(coord=1)
Expand All @@ -160,7 +160,7 @@ def diff(t, **kwargs):

def _initialdistribution(ivp, prior):
"""
Conditions initialdistribution :math:`\mathcal{N}(0, P P^\\top)`
Conditions initialdistribution :math:`\\mathcal{N}(0, P P^\\top)`
on the initial values :math:`(x_0, f(t_0, x_0), ...)` using
as many available derivatives as possible.
Expand Down Expand Up @@ -203,7 +203,7 @@ def _initialdistribution(ivp, prior):


def _initialdistribution_no_precond(ivp, prior):
""" """

x0 = ivp.initialdistribution.mean()
dx0 = ivp.rhs(ivp.t0, x0)
ddx0 = _ddx(ivp.t0, x0, ivp)
Expand Down
2 changes: 0 additions & 2 deletions src/probnum/diffeq/odefiltsmooth/ivpfiltsmooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from probnum.prob.distributions import Normal
from probnum.diffeq import odesolver
from probnum.diffeq.odefiltsmooth.prior import ODEPrior
from probnum.filtsmooth import *
from probnum.diffeq.odesolution import ODESolution


Expand Down Expand Up @@ -172,5 +171,4 @@ def _suggest_step(self, step, errorest):

@property
def prior(self):
""" """
return self.gfilt.dynamicmodel
25 changes: 10 additions & 15 deletions src/probnum/diffeq/odefiltsmooth/odefiltsmooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _create_solver_object(


def _check_step_tol(step, tol):
""" """

both_none = tol is None and step is None
both_not_none = tol is not None and step is not None
if both_none or both_not_none:
Expand All @@ -264,14 +264,13 @@ def _check_step_tol(step, tol):


def _check_method(method):
""" """

if method not in ["ekf0", "ekf1", "ukf", "eks0", "eks1", "uks"]:
raise ValueError("Method not supported.")


def _string2prior(ivp, which_prior, precond_step, **kwargs):
"""
"""

ibm_family = ["ibm1", "ibm2", "ibm3", "ibm4"]
ioup_family = ["ioup1", "ioup2", "ioup3", "ioup4"]
matern_family = ["matern32", "matern52", "matern72", "matern92"]
Expand All @@ -286,8 +285,7 @@ def _string2prior(ivp, which_prior, precond_step, **kwargs):


def _string2ibm(ivp, which_prior, precond_step, **kwargs):
"""
"""

if "diffconst" in kwargs.keys():
diffconst = kwargs["diffconst"]
else:
Expand All @@ -305,8 +303,7 @@ def _string2ibm(ivp, which_prior, precond_step, **kwargs):


def _string2ioup(ivp, which_prior, precond_step, **kwargs):
"""
"""

if "diffconst" in kwargs.keys():
diffconst = kwargs["diffconst"]
else:
Expand All @@ -328,8 +325,7 @@ def _string2ioup(ivp, which_prior, precond_step, **kwargs):


def _string2matern(ivp, which_prior, precond_step, **kwargs):
"""
"""

if "diffconst" in kwargs.keys():
diffconst = kwargs["diffconst"]
else:
Expand All @@ -351,17 +347,16 @@ def _string2matern(ivp, which_prior, precond_step, **kwargs):


def _string2filter(_ivp, _prior, _method, **kwargs):
"""
"""

if "evlvar" in kwargs.keys():
evlvar = kwargs["evlvar"]
else:
evlvar = 0.0
if _method == "ekf0" or _method == "eks0":
if _method in ("ekf0", "eks0"):
return ivp2filter.ivp2ekf0(_ivp, _prior, evlvar)
elif _method == "ekf1" or _method == "eks1":
elif _method in ("ekf1", "eks1"):
return ivp2filter.ivp2ekf1(_ivp, _prior, evlvar)
elif _method == "ukf" or _method == "uks":
elif _method in ("ukf", "uks"):
return ivp2filter.ivp2ukf(_ivp, _prior, evlvar)
else:
raise ValueError("Type of filter not supported.")
8 changes: 4 additions & 4 deletions src/probnum/diffeq/odefiltsmooth/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ODEPrior(LTISDEModel):
matrix that maps to filtering iteration to the Nordsieck vector,
.. math:: P = \\text{diag }(1, h, h^2, ..., h^q).
Here, :math:`h` is some expected average step size. Note that we
ignored the factorials in this matrix. Our setting makes it easy to
recover "no preconditioning" by choosing :math:`h=1`.
Expand Down Expand Up @@ -78,7 +78,6 @@ class ODEPrior(LTISDEModel):
"""

def __init__(self, driftmat, dispmat, ordint, spatialdim, precond_step=1.0):
""" """
self.ordint = ordint
self.spatialdim = spatialdim
self.precond, self.invprecond = self.precond2nordsieck(precond_step)
Expand Down Expand Up @@ -283,7 +282,8 @@ def _trans_ibm(self, start, stop):
step = stop - start

# This seems like the faster solution compared to fully vectorising.
# I suspect it is because np.math.factorial is much faster than scipy.special.factorial
# I suspect it is because np.math.factorial is much faster than
# scipy.special.factorial
ah_1d = np.diag(np.ones(self.ordint + 1), 0)
for i in range(self.ordint):
offdiagonal = (
Expand Down Expand Up @@ -394,7 +394,7 @@ def _driftmat_matern(ordint, spatialdim, lengthscale):

def _dispmat(ordint, spatialdim, diffconst):
"""
Returns I_D \otimes L
Returns I_D \\otimes L
diffconst = sigma**2
"""
dispvec = diffconst * np.eye(ordint + 1)[:, -1]
Expand Down
5 changes: 1 addition & 4 deletions src/probnum/diffeq/steprule.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,4 @@ def suggest(self, laststep, errorest, **kwargs):
return step

def is_accepted(self, proposedstep, errorest, **kwargs):
if errorest * proposedstep < self.tol_per_step:
return True
else:
return False
return errorest * proposedstep < self.tol_per_step
Loading

0 comments on commit 1ab81a7

Please sign in to comment.