Skip to content

Commit

Permalink
Merge pull request #382 from kinnala/add-flake8-lint
Browse files Browse the repository at this point in the history
Add flake8 linting to CI and fix errors
  • Loading branch information
kinnala authored Jun 1, 2020
2 parents 4b7e45c + 83a27aa commit b78a29b
Show file tree
Hide file tree
Showing 52 changed files with 297 additions and 235 deletions.
9 changes: 9 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[flake8]
ignore = W504,W503,E226,E126,E123,E121,E24,E704,E741
exclude =
docs
.git
.github
paper
tests
setup.py
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Lint

on: push

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: Install Python dependencies
run: pip install flake8

- name: Run linters
uses: samuelmeuli/lint-action@v1
with:
github_token: ${{ secrets.github_token }}
flake8: true
flake8_dir: skfem
flake8_args: "--extend-ignore=E741"
14 changes: 7 additions & 7 deletions skfem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""Support for wildcard import."""

from skfem.mesh import *
from skfem.assembly import *
from skfem.mapping import *
from skfem.element import *
from skfem.utils import *
from skfem.version import __version__
from skfem.mesh import * # noqa
from skfem.assembly import * # noqa
from skfem.mapping import * # noqa
from skfem.element import * # noqa
from skfem.utils import * # noqa
from skfem.version import __version__ # noqa

from skfem.assembly import __all__ as all_assembly
from skfem.mesh import __all__ as all_mesh
from skfem.element import __all__ as all_element


__all__ = all_mesh + all_assembly + all_element + [
__all__ = all_mesh + all_assembly + all_element + [ # noqa
'MappingAffine',
'MappingIsoparametric',
'MappingMortar',
Expand Down
2 changes: 1 addition & 1 deletion skfem/assembly/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""This module contains rest of the tools for performing the finite element
r"""This module contains rest of the tools for performing the finite element
assembly. The basic workflow of assembly is the following:
1. Initialize :class:`~skfem.mesh.Mesh` and :class:`~skfem.mesh.Element`.
Expand Down
6 changes: 3 additions & 3 deletions skfem/assembly/basis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .basis import Basis
from .interior_basis import InteriorBasis
from .facet_basis import FacetBasis
from .basis import Basis # noqa
from .interior_basis import InteriorBasis # noqa
from .facet_basis import FacetBasis # noqa
49 changes: 25 additions & 24 deletions skfem/assembly/basis/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Basis:
Please see the following implementations:
- :class:`~skfem.assembly.InteriorBasis`, basis functions inside elements
- :class:`~skfem.assembly.FacetBasis`, basis functions on element boundaries
- :class:`~skfem.assembly.FacetBasis`, basis functions on boundaries
"""

Expand Down Expand Up @@ -70,15 +70,15 @@ def _build_dofnum(self, mesh, element):
offset = element.nodal_dofs * mesh.p.shape[1]

# edge dofs
if mesh.dim() == 3:
if mesh.dim() == 3:
self.edge_dofs = np.reshape(
np.arange(element.edge_dofs * mesh.edges.shape[1],
dtype=np.int64),
(element.edge_dofs, mesh.edges.shape[1]),
order='F') + offset
offset = offset + element.edge_dofs * mesh.edges.shape[1]
else:
self.edge_dofs = np.empty((0,0))
self.edge_dofs = np.empty((0, 0))

# facet dofs
self.facet_dofs = np.reshape(
Expand Down Expand Up @@ -172,7 +172,8 @@ def _get_dofs(self,
off = n_nodal

facets = {
self.dofnames[i + off]: np.zeros((0, len(facet_ix)), dtype=np.int64)
self.dofnames[i + off]: np.zeros((0, len(facet_ix)),
dtype=np.int64)
for i in range(n_facet) if self.dofnames[i + off] not in skip
}
for i in range(n_facet):
Expand Down Expand Up @@ -213,13 +214,13 @@ def find_dofs(self,
This corresponds to a list of facet indices that can be passed over:
>>> basis = InteriorBasis(m, ElementTriP1())
>>> basis.find_dofs({'left': m.facets_satisfying(lambda x: x[0] == 0)})
{'left': Dofs(nodal={'u': array([0, 2, 5])}, facet={}, edge={}, interior={})}
>>> basis.find_dofs({'left': np.array([1, 5])})['left']
Dofs(nodal={'u': array([0, 2, 5])}, facet={}, edge={}, interior={})
Parameters
----------
facets
A dictionary of facet indices. If `None`, use `self.mesh.boundaries`
A dictionary of facets. If `None`, use `self.mesh.boundaries`
if set or otherwise use `{'all': self.mesh.boundary_facets()}`.
skip
List of dofnames to skip.
Expand All @@ -242,17 +243,17 @@ def get_dofs(self, facets: Optional[Any] = None) -> Any:
Parameters
----------
facets
A list of facet indices. If None, find facets by
Mesh.boundary_facets(). If callable, call Mesh.facets_satisfying to
get facets. If array, find the corresponding dofs. If dict of
arrays, find dofs for each entry. If dict of callables, call
A list of facet indices. If `None`, find facets by
Mesh.boundary_facets. If callable, call Mesh.facets_satisfying
to get facets. If array, find the corresponding DOFs. If dict of
arrays, find DOFs for each entry. If dict of callables, call
Mesh.facets_satisfying for each entry to get facets and then find
dofs for those.
DOFs for those.
Returns
-------
Dofs or Dict[str, Dofs]
A subset of degrees-of-freedom as :class:`skfem.assembly.dofs.Dofs`.
A subset of DOFs as :class:`skfem.assembly.dofs.Dofs`.
"""
if facets is None:
Expand Down Expand Up @@ -313,12 +314,12 @@ def linear_combination(n, refn):
out[j, k, :, :] += \
values * self.basis[i][c][n][j, k]
elif len(refn.shape) == 5: # third derivatives
#import pdb; pdb.set_trace()
for j in range(out.shape[0]):
for k in range(out.shape[1]):
for l in range(out.shape[2]):
out[j, k, l, :, :] += \
values * self.basis[i][c][-1][n][j, k, l]
values * \
self.basis[i][c][-1][n][j, k, l]
elif len(refn.shape) == 6: # fourth derivatives
for j in range(out.shape[0]):
for k in range(out.shape[1]):
Expand Down Expand Up @@ -355,20 +356,20 @@ def linear_combination(n, refn):
def split_indices(self) -> List[ndarray]:
"""Return indices for the solution components."""
if isinstance(self.elem, ElementComposite):
off = np.zeros(4, dtype=np.int)
o = np.zeros(4, dtype=np.int)
output = [None] * len(self.elem.elems)
for k in range(len(self.elem.elems)):
e = self.elem.elems[k]
output[k] = np.concatenate((
self.nodal_dofs[off[0]:(off[0] + e.nodal_dofs)].flatten(),
self.edge_dofs[off[1]:(off[1] + e.edge_dofs)].flatten(),
self.facet_dofs[off[2]:(off[2] + e.facet_dofs)].flatten(),
self.interior_dofs[off[3]:(off[3] + e.interior_dofs)].flatten()
self.nodal_dofs[o[0]:(o[0] + e.nodal_dofs)].flatten(),
self.edge_dofs[o[1]:(o[1] + e.edge_dofs)].flatten(),
self.facet_dofs[o[2]:(o[2] + e.facet_dofs)].flatten(),
self.interior_dofs[o[3]:(o[3] + e.interior_dofs)].flatten()
)).astype(np.int)
off += np.array([e.nodal_dofs,
e.edge_dofs,
e.facet_dofs,
e.interior_dofs])
o += np.array([e.nodal_dofs,
e.edge_dofs,
e.facet_dofs,
e.interior_dofs])
return output
raise ValueError("Basis.elem has only a single component!")

Expand Down
16 changes: 8 additions & 8 deletions skfem/assembly/basis/facet_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FacetBasis(Basis):
def __init__(self,
mesh,
elem,
mapping = None,
mapping=None,
intorder: int = None,
side: int = None,
facets: ndarray = None,
Expand All @@ -39,7 +39,7 @@ def __init__(self,
integrated exactly by the used quadrature. Not used if `quadrature`
is specified.
side
If 0 or 1, the basis functions are evaluated on the interior facets.
If 0 or 1, basis functions are evaluated on the interior facets.
The numbers 0 and 1 refer to the different sides of the facets.
Side 0 corresponds to the indices `mesh.f2t[0]`. If `None`, basis
is evaluated only on the exterior facets.
Expand All @@ -65,7 +65,7 @@ def __init__(self,
self.find = np.nonzero(self.mesh.f2t[1] == -1)[0]
self.tind = self.mesh.f2t[0, self.find]
elif hasattr(self.mapping, 'helper_to_orig') and side in [0, 1]:
self.mapping.side = side # side effect
self.mapping.side = side
self.find = self.mapping.helper_to_orig[side]
self.tind = self.mesh.f2t[0, self.find]
elif side in [0, 1]:
Expand All @@ -86,10 +86,10 @@ def __init__(self,
# construct normal vectors from side=0 always
Y0 = self.mapping.invF(x, tind=self.mesh.f2t[0, self.find])
self.normals = DiscreteField(
value = self.mapping.normals(Y0,
self.mesh.f2t[0, self.find],
self.find,
self.mesh.t2f)
value=self.mapping.normals(Y0,
self.mesh.f2t[0, self.find],
self.find,
self.mesh.t2f)
)

self.nelems = len(self.find)
Expand All @@ -113,5 +113,5 @@ def global_coordinates(self) -> ndarray:

def mesh_parameters(self) -> ndarray:
return DiscreteField((np.abs(self.mapping.detDG(self.X, self.find))
** (1. / (self.mesh.dim() - 1.)))\
** (1. / (self.mesh.dim() - 1.)))
if self.mesh.dim() != 1 else np.array([0.]))
7 changes: 4 additions & 3 deletions skfem/assembly/basis/interior_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ def refinterp(self,
nt = self.nelems
t = np.tile(m.t, (1, nt))
dt = np.max(t)
t += (dt + 1) * (np.tile(np.arange(nt), (m.t.shape[0] * m.t.shape[1], 1))
.flatten('F')
.reshape((-1, m.t.shape[0])).T)
t += (dt + 1) *\
(np.tile(np.arange(nt), (m.t.shape[0] * m.t.shape[1], 1))
.flatten('F')
.reshape((-1, m.t.shape[0])).T)

if X.shape[0] == 1:
p = np.array([x.flatten()])
Expand Down
8 changes: 4 additions & 4 deletions skfem/assembly/form/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .form import Form
from .bilinear_form import bilinear_form, BilinearForm
from .linear_form import linear_form, LinearForm
from .functional import functional, Functional
from .form import Form # noqa
from .bilinear_form import bilinear_form, BilinearForm # noqa
from .linear_form import linear_form, LinearForm # noqa
from .functional import functional, Functional # noqa
3 changes: 2 additions & 1 deletion skfem/assembly/form/form_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from numpy import ndarray

# TODO: Deprecated. Remove after old style forms are gone.

class FormParameters(NamedTuple):
"""Deprecated. Remove after old style forms are gone."""
w: Optional[ndarray] = None
dw: Optional[ndarray] = None
ddw: Optional[ndarray] = None
Expand Down
18 changes: 10 additions & 8 deletions skfem/element/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""This module defines finite elements (in a very generic sense). The naming of
the element classes reflects their compatibility with the mesh types.
"""This module defines finite elements (in a very generic sense).
The naming of the element classes reflects their compatibility with the mesh
types.
>>> from skfem.element import ElementTriP1
>>> ElementTriP1.mesh_type
skfem.mesh.mesh2d.mesh_tri.MeshTri
In order to use an element, you simply initialize the respective object and pass
it to the constructor of :class:`~skfem.assembly.InteriorBasis` or
In order to use an element, you simply initialize the respective object and
pass it to the constructor of :class:`~skfem.assembly.InteriorBasis` or
:class:`~skfem.assembly.FacetBasis`.
The supported elements include
Expand Down Expand Up @@ -54,10 +56,10 @@
ElementQuadBFS
from .element_tet import ElementTetP0, ElementTetP1, ElementTetP2,\
ElementTetRT0, ElementTetN0, ElementTetMini
from .element_hex import ElementHex1, ElementHexS2
from .element_line import ElementLineP1, ElementLineP2, ElementLinePp,\
ElementLineHermite
from .element_composite import ElementComposite
from .element_hex import ElementHex1, ElementHexS2 # noqa
from .element_line import ElementLineP1, ElementLineP2,\
ElementLinePp, ElementLineHermite # noqa
from .element_composite import ElementComposite # noqa


__all__ = [
Expand Down
6 changes: 3 additions & 3 deletions skfem/element/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Element():
Mesh type for calculating number of edges, etc.
"""
nodal_dofs: int = 0
nodal_dofs: int = 0
facet_dofs: int = 0
interior_dofs: int = 0
edge_dofs: int = 0
Expand Down Expand Up @@ -99,9 +99,9 @@ def _bfun_counts(self):
"""
return np.array([self.nodal_dofs * self.mesh_type.t.shape[0],
self.edge_dofs * self.mesh_type.t2e.shape[0]\
self.edge_dofs * self.mesh_type.t2e.shape[0]
if hasattr(self.mesh_type, 'edges') else 0,
self.facet_dofs * self.mesh_type.t2f.shape[0]\
self.facet_dofs * self.mesh_type.t2f.shape[0]
if hasattr(self.mesh_type, 'facets') else 0,
self.interior_dofs])

Expand Down
9 changes: 5 additions & 4 deletions skfem/element/element_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def __init__(self, *elems):
self.dofnames = dofnames

doflocs = []
for i in range(np.sum(np.array([e._bfun_counts() for e in self.elems]))):
for i in range(np.sum(np.array([e._bfun_counts()
for e in self.elems]))):
n, ind = self._deduce_bfun(i)
doflocs.append(self.elems[n].doflocs[ind])
self.doflocs = np.array(doflocs)
Expand All @@ -60,15 +61,15 @@ def _deduce_bfun(self, i: int):
ns += sum([tmp for j in range(int(counts[0] / len(tmp)))], [])
if counts[1] > 0:
tmp = sum([[j] * self.elems[j].edge_dofs
for j in range(len(self.elems))], [])
for j in range(len(self.elems))], [])
ns += sum([tmp for j in range(int(counts[1] / len(tmp)))], [])
if counts[2] > 0:
tmp = sum([[j] * self.elems[j].facet_dofs
for j in range(len(self.elems))], [])
for j in range(len(self.elems))], [])
ns += sum([tmp for j in range(int(counts[2] / len(tmp)))], [])
if counts[3] > 0:
tmp = sum([[j] * self.elems[j].interior_dofs
for j in range(len(self.elems))], [])
for j in range(len(self.elems))], [])
ns += sum([tmp for j in range(int(counts[3] / len(tmp)))], [])

mask = np.array(ns)
Expand Down
Loading

0 comments on commit b78a29b

Please sign in to comment.