Similar lines in 2 files Open
#!/usr/bin/env python
- Read upRead up
- Exclude checks
Indicates that a set of similar lines has been detected among multiple file. This usually means that the code should be refactored to avoid this duplication. ==numdifftools.ndscipy:[86:143] ==numdifftools.ndstatsmodels:[298:355] class Gradient(Jacobian): ``` Calculate Gradient with finite difference approximation
Parameters
fun : function
function of one array fun(x, *args
, **kwds
)
step : float, optional
Stepsize, if None, optimal stepsize is used, i.e.,
x * _EPS for method==complex
x * _EPS(1/2) for method==forward
x * _EPS(1/3) for method==central
.
method : {'central', 'complex', 'forward', 'backward'}
defines the method used in the approximation.
Examples
import numpy as np import numdifftools.nd_statsmodels as nd fun = lambda x: np.sum(x**2) dfun = nd.Gradient(fun) np.allclose(dfun([1,2,3]), [ 2., 4., 6.]) True
# At [x,y] = [1,1], compute the numerical gradient # of the function sin(x-y) + y*exp(x)
sin = np.sin; exp = np.exp z = lambda xy: sin(xy[0]-xy[1]) + xy[1]*exp(xy[0]) dz = nd.Gradient(z) grad2 = dz([1, 1]) np.allclose(grad2, [ 3.71828183, 1.71828183]) True
# At the global minimizer (1,1) of the Rosenbrock function, # compute the gradient. It should be essentially zero.
rosen = lambda x : (1-x[0])2 + 105.*(x[1]-x[0]2)**2 rd = nd.Gradient(rosen) grad3 = rd([1,1]) np.allclose(grad3,[0, 0]) True
See also
Hessian, Jacobian ```
def call(self, x, args, *kwds): return super(Gradient, self).call(np.atleast_1d(x).ravel(), args, *kwds).squeeze()
if name == 'main': from numdifftools.testing import testdocstrings testdocstrings(file)
Similar lines in 2 files Open
#!/usr/bin/env python
- Read upRead up
- Exclude checks
Indicates that a set of similar lines has been detected among multiple file. This usually means that the code should be refactored to avoid this duplication. ==numdifftools.finitedifference:[562:582] ==numdifftools.limits:[210:219] originalshape = np.shape(sequence[0]) fdel = np.vstack([np.ravel(r) for r in sequence]) one = np.ones(originalshape) h = np.vstack([np.ravel(one * step) for step in steps]) assert(fdel.size == h.size, 'fun did not return data of correct ' 'size (it must be vectorized)') return fdel, h, originalshape
def apply(self, sequence, steps, step_ratio=2.0): ``` Apply finite difference rule along the first axis.
Return derivative estimates of fun at x0 for a sequence of stepsizes h
Parameters
sequence: finite differences steps: steps
Using open without explicitly specifying an encoding Open
with open(file_path, 'r') as fp:
- Read upRead up
- Exclude checks
It is better to specify an encoding when opening documents. Using the system default implicitly can create problems on other operating systems. See https://peps.python.org/pep-0597/
Unable to import 'sphinx.application' Open
from sphinx.application import Sphinx
- Read upRead up
- Exclude checks
Used when pylint has been unable to import a module.
At least two spaces before inline comment Open
pkg_resources.require('setuptools>=39.2') # setuptools >=38.3.0 # version with most `setup.cfg` bugfixes
- Read upRead up
- Exclude checks
Separate inline comments by at least two spaces.
An inline comment is a comment on the same line as a statement.
Inline comments should be separated by at least two spaces from the
statement. They should start with a # and a single space.
Each line of a block comment starts with a # and a single space
(unless it is indented text inside the comment).
Okay: x = x + 1 # Increment x
Okay: x = x + 1 # Increment x
Okay: # Block comment
E261: x = x + 1 # Increment x
E262: x = x + 1 #Increment x
E262: x = x + 1 # Increment x
E265: #Block comment
E266: ### Block comment
Line too long (108/100) Open
pkg_resources.require('setuptools>=39.2') # setuptools >=38.3.0 # version with most `setup.cfg` bugfixes
- Read upRead up
- Exclude checks
Used when a line is longer than a given number of characters.
Missing function or method docstring Open
def setup_package():
- Read upRead up
- Exclude checks
Used when a function or method has no docstring. Some special methods like init do not require a docstring.
Formatting a regular string which could be a f-string Open
print("Version: {}".format(version))
- Read upRead up
- Exclude checks
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 run(self):
- Read upRead up
- Exclude checks
Used when a function or method has no docstring. Some special methods like init do not require a docstring.
Missing function or method docstring Open
def initialize_options(self):
- Read upRead up
- Exclude checks
Used when a function or method has no docstring. Some special methods like init do not require a docstring.
Missing class docstring Open
class Doctest(Command):
- Read upRead up
- Exclude checks
Used when a class has no docstring. Even an empty class must have a docstring.
Missing function or method docstring Open
def finalize_options(self):
- Read upRead up
- Exclude checks
Used when a function or method has no docstring. Some special methods like init do not require a docstring.
Import outside toplevel (sphinx.application.Sphinx) Open
from sphinx.application import Sphinx
- Read upRead up
- Exclude checks
Used when an import statement is used anywhere other than the module toplevel. Move this import to the top of the file.
Variable name fp
doesn't conform to '[a-z_][a-z0-9_]{2,30}$' pattern ('[a-z_][a-z0-9_]{2,30}$' pattern) Open
with open(file_path, 'r') as fp:
- Read upRead up
- Exclude checks
Used when the name doesn't conform to naming rules associated to its type (constant, variable, class...).