pbrod/numdifftools

View on GitHub
src/numdifftools/nd_statsmodels.py

Summary

Maintainability
A
2 hrs
Test Coverage

Function approx_fprime has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

def approx_fprime(x, f, epsilon=None, args=(), kwargs=None, centered=True):
    """
    Gradient of function, or Jacobian if function fun returns 1d array

    Parameters
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py - About 35 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Too many arguments (6/5)
Open

def approx_fprime(x, f, epsilon=None, args=(), kwargs=None, centered=True):
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when a function or method takes too many arguments.

Consider using '{forward: 1, backward: 1}' instead of a call to 'dict'.
Open

        return dict(forward=1, backward=1).get(self.method, 2)
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Emitted when using dict() to create a dictionary instead of a literal '{ ... }'. The literal is faster as it avoids an additional function call.

Class '_Common' inherits from object, can be safely removed from bases in python3
Open

class _Common(object):
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when a class inherit from object, which under python3 is implicit, hence can be safely removed from bases.

Consider using '{complex: approx_hess_cs, forward: approx_hess1, backward: _approx_hess1_backward, ... }' instead of a call to 'dict'.
Open

    _callables = dict(complex=approx_hess_cs,
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Emitted when using dict() to create a dictionary instead of a literal '{ ... }'. The literal is faster as it avoids an additional function call.

Consider using Python 3 style super() without arguments
Open

        return super(Gradient, self).__call__(np.atleast_1d(x).ravel(),
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Emitted when calling the super() builtin with the current class and instance. On Python 3 these arguments are the default and they can be omitted.

Consider using '{complex: approx_fprime_cs, central: partial(approx_fprime, centered=True), ... }' instead of a call to 'dict'.
Open

    _callables = dict(complex=approx_fprime_cs,
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Emitted when using dict() to create a dictionary instead of a literal '{ ... }'. The literal is faster as it avoids an additional function call.

Unable to import 'numpy'
Open

import numpy as np
Severity: Critical
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when pylint has been unable to import a module.

TODO: see if this can be vectorized, but usually dim is small
Open

    # TODO: see if this can be vectorized, but usually dim is small
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when a warning note as FIXME or XXX is detected.

TODO found
Open

    # TODO: see if this can be vectorized, but usually dim is small
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by fixme

Similar blocks of code found in 2 locations. Consider refactoring.
Open

class Gradient(Jacobian):
    """
    Calculate Gradient with finite difference approximation

    Parameters
Severity: Major
Found in src/numdifftools/nd_statsmodels.py and 1 other location - About 1 hr to fix
src/numdifftools/nd_scipy.py on lines 87..137

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 46.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Missing whitespace around operator
Open

    approx_hess1=approx_hess2=approx_hess3=approx_hess_cs=_get_epsilon=None
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around operator
Open

    approx_hess1=approx_hess2=approx_hess3=approx_hess_cs=_get_epsilon=None
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around operator
Open

    approx_hess1=approx_hess2=approx_hess3=approx_hess_cs=_get_epsilon=None
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around operator
Open

    approx_hess1=approx_hess2=approx_hess3=approx_hess_cs=_get_epsilon=None
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around operator
Open

    approx_hess1=approx_hess2=approx_hess3=approx_hess_cs=_get_epsilon=None
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around arithmetic operator
Open

    axes = list(range(partials[0].ndim+1))
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Missing whitespace around arithmetic operator
Open

    partials = [f(x+ih, *args, **kwargs).imag / epsilon[i]
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Surround operators with a single space on either side.

- Always surround these binary operators with a single space on
  either side: assignment (=), augmented assignment (+=, -= etc.),
  comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
  Booleans (and, or, not).

- If operators with different priorities are used, consider adding
  whitespace around the operators with the lowest priorities.

Okay: i = i + 1
Okay: submitted += 1
Okay: x = x * 2 - 1
Okay: hypot2 = x * x + y * y
Okay: c = (a + b) * (a - b)
Okay: foo(bar, key='word', *args, **kwargs)
Okay: alpha[:-i]

E225: i=i+1
E225: submitted +=1
E225: x = x /2 - 1
E225: z = x **y
E225: z = 1and 1
E226: c = (a+b) * (a-b)
E226: hypot2 = x*x + y*y
E227: c = a|b
E228: msg = fmt%(errno, errmsg)

Line break after binary operator
Open

            grad[k, :] = (f(*(x + ei,) + args, **kwargs) -
Severity: Minor
Found in src/numdifftools/nd_statsmodels.py by pep8

Avoid breaks after binary operators.

The preferred place to break around a binary operator is before the
operator, not after it.

W504: (width == 0 +\n height == 0)
W504: (width == 0 and\n height == 0)
W504: var = (1 &\n       ~2)

Okay: foo(\n    -x)
Okay: foo(x\n    [])
Okay: x = '''\n''' + ''
Okay: x = '' + '''\n'''
Okay: foo(x,\n    -y)
Okay: foo(x,  # comment\n    -y)

The following should be W504 but unary_context is tricky with these
Okay: var = (1 /\n       -2)
Okay: var = (1 +\n       -1 +\n       -2)

Missing function or method docstring
Open

    def order(self):
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when a function or method has no docstring. Some special methods like init do not require a docstring.

Variable name ei doesn't conform to '[a-z_][a-z0-9_]{2,30}$' pattern ('[a-z_][a-z0-9_]{2,30}$' pattern)
Open

    ei = np.zeros(np.shape(x), float)
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when the name doesn't conform to naming rules associated to its type (constant, variable, class...).

Formatting a regular string which could be a f-string
Open

            warnings.warn('{} is an illegal method! Setting method="central"'.format(method))
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when we detect a string that is being formatted with format() or % which could potentially be a f-string. The use of f-strings is preferred. Requires Python 3.6 and py-version >= 3.6.

Missing function or method docstring
Open

    def method(self):
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when a function or method has no docstring. Some special methods like init do not require a docstring.

Variable name f0 doesn't conform to '[a-z_][a-z0-9_]{2,30}$' pattern ('[a-z_][a-z0-9_]{2,30}$' pattern)
Open

    f0 = f(*(x,) + args, **kwargs)
Severity: Info
Found in src/numdifftools/nd_statsmodels.py by pylint

Used when the name doesn't conform to naming rules associated to its type (constant, variable, class...).

There are no issues that match your filters.

Category
Status