Skip to content

Commit

Permalink
Add pre-commit (#11541)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Larson <[email protected]>
Co-authored-by: Daniel McCloy <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2023
1 parent 4eb9ef0 commit 8325f80
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 209 deletions.
45 changes: 0 additions & 45 deletions .github/workflows/codespell_and_flake.yml

This file was deleted.

14 changes: 14 additions & 0 deletions .github/workflows/precommit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Pre-commit

on: [push, pull_request]

jobs:
style:
name: Pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- uses: pre-commit/[email protected]
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ cover
venv/
*.json
.hypothesis/

.ruff_cache/
.ipynb_checkpoints/
36 changes: 36 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
default_language_version:
python: python3.11

repos:
# - repo: https://github.com/psf/black
# rev: 23.1.0
# hooks:
# - id: black
# args: [--quiet]

# Ruff mne
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.262
hooks:
- id: ruff
name: ruff mne
files: ^mne/

# Ruff tutorials and examples
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.262
hooks:
- id: ruff
name: ruff tutorials and examples
# D103: missing docstring in public function
# D400: docstring first line must end with period
args: ["--ignore=D103,D400"]
files: ^tutorials/|^examples/

# Codespell
- repo: https://github.com/codespell-project/codespell
rev: v2.2.3
hooks:
- id: codespell
files: ^mne/|^doc/|^examples/|^tutorials/
types_or: [python, bib, rst, inc]
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exclude tools
exclude Makefile
exclude .coveragerc
exclude *.yml
exclude *.yaml
exclude ignore_words.txt
exclude .mailmap
exclude codemeta.json
Expand Down
95 changes: 6 additions & 89 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

PYTHON ?= python
PYTESTS ?= py.test
CTAGS ?= ctags
CODESPELL_SKIPS ?= "doc/_build,doc/auto_*,*.fif,*.eve,*.gz,*.tgz,*.zip,*.mat,*.stc,*.label,*.w,*.bz2,*.annot,*.sulc,*.log,*.local-copy,*.orig_avg,*.inflated_avg,*.gii,*.pyc,*.doctree,*.pickle,*.inv,*.png,*.edf,*.touch,*.thickness,*.nofix,*.volume,*.defect_borders,*.mgh,lh.*,rh.*,COR-*,FreeSurferColorLUT.txt,*.examples,.xdebug_mris_calc,bad.segments,BadChannels,*.hist,empty_file,*.orig,*.js,*.map,*.ipynb,searchindex.dat,install_mne_c.rst,plot_*.rst,*.rst.txt,c_EULA.rst*,*.html,gdf_encodes.txt,*.svg,references.bib,*.css,*.edf,*.bdf,*.vhdr"
CODESPELL_DIRS ?= mne/ doc/ tutorials/ examples/
all: clean inplace test test-doc
Expand All @@ -25,13 +24,6 @@ clean-cache:

clean: clean-build clean-pyc clean-so clean-ctags clean-cache

in: inplace # just a shortcut
inplace:
$(PYTHON) setup.py build_ext -i

wheel:
$(PYTHON) setup.py sdist bdist_wheel

wheel_quiet:
$(PYTHON) setup.py -q sdist bdist_wheel

Expand All @@ -43,79 +35,27 @@ testing_data:

pytest: test

test: in
rm -f .coverage
$(PYTESTS) -m 'not ultraslowtest' mne

test-verbose: in
rm -f .coverage
$(PYTESTS) -m 'not ultraslowtest' mne --verbose

test-fast: in
rm -f .coverage
$(PYTESTS) -m 'not slowtest' mne

test-full: in
rm -f .coverage
$(PYTESTS) mne

test-no-network: in
sudo unshare -n -- sh -c 'MNE_SKIP_NETWORK_TESTS=1 py.test mne'

test-no-testing-data: in
@MNE_SKIP_TESTING_DATASET_TESTS=true \
$(PYTESTS) mne

test-no-sample-with-coverage: in testing_data
rm -rf coverage .coverage
$(PYTESTS) --cov=mne --cov-report html:coverage

test-doc: sample_data testing_data
$(PYTESTS) --doctest-modules --doctest-ignore-import-errors --doctest-glob='*.rst' ./doc/ --ignore=./doc/auto_examples --ignore=./doc/auto_tutorials --ignore=./doc/_build --ignore=./doc/conf.py --ignore=doc/sphinxext --fulltrace

test-coverage: testing_data
rm -rf coverage .coverage
$(PYTESTS) --cov=mne --cov-report html:coverage
# what's the difference with test-no-sample-with-coverage?

test-mem: in testing_data
ulimit -v 1097152 && $(PYTESTS) mne

trailing-spaces:
find . -name "*.py" | xargs perl -pi -e 's/[ \t]*$$//'
pre-commit:
@pre-commit run -a

ctags:
# make tags for symbol based navigation in emacs and vim
# Install with: sudo apt-get install exuberant-ctags
$(CTAGS) -R *

upload-pipy:
python setup.py sdist bdist_egg register upload

flake:
@if command -v flake8 > /dev/null; then \
echo "Running flake8"; \
flake8 --count; \
else \
echo "flake8 not found, please install it!"; \
exit 1; \
fi;
@echo "flake8 passed"
# Aliases for stuff we used to support or users might think of
ruff: pre-commit
flake: pre-commit
pep: pre-commit

codespell: # running manually
@codespell --builtin clear,rare,informal,names,usage -w -i 3 -q 3 -S $(CODESPELL_SKIPS) --ignore-words=ignore_words.txt --uri-ignore-words-list=bu $(CODESPELL_DIRS)

codespell-error: # running on travis
@codespell --builtin clear,rare,informal,names,usage -i 0 -q 7 -S $(CODESPELL_SKIPS) --ignore-words=ignore_words.txt --uri-ignore-words-list=bu $(CODESPELL_DIRS)

pydocstyle:
@echo "Running pydocstyle"
@pydocstyle mne

docstring:
@echo "Running docstring tests"
@$(PYTESTS) --doctest-modules mne/tests/test_docstring_parameters.py

check-manifest:
check-manifest -q --ignore .circleci/config.yml,doc,logo,mne/io/*/tests/data*,mne/io/tests/data,mne/preprocessing/tests/data,.DS_Store,mne/_version.py

Expand All @@ -125,26 +65,3 @@ check-readme: clean wheel_quiet
nesting:
@echo "Running import nesting tests"
@$(PYTESTS) mne/tests/test_import_nesting.py

pep:
@$(MAKE) -k flake pydocstyle docstring codespell-error check-manifest nesting check-readme

manpages:
@echo "I: generating manpages"
set -e; mkdir -p _build/manpages && \
cd bin && for f in mne*; do \
descr=$$(grep -h -e "^ *'''" -e 'DESCRIP =' $$f -h | sed -e "s,.*' *\([^'][^']*\)'.*,\1,g" | head -n 1); \
PYTHONPATH=../ \
help2man -n "$$descr" --no-discard-stderr --no-info --version-string "$(uver)" ./$$f \
>| ../_build/manpages/$$f.1; \
done

build-doc-dev:
cd doc; make clean
cd doc; DISPLAY=:1.0 xvfb-run -n 1 -s "-screen 0 1280x1024x24 -noreset -ac +extension GLX +render" make html_dev

build-doc-stable:
cd doc; make clean
cd doc; DISPLAY=:1.0 xvfb-run -n 1 -s "-screen 0 1280x1024x24 -noreset -ac +extension GLX +render" make html_stable

docstyle: pydocstyle
19 changes: 5 additions & 14 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ stages:
pool:
vmImage: 'ubuntu-latest'
variables:
PYTHON_VERSION: '3.9'
PYTHON_VERSION: '3.11'
PYTHON_ARCH: 'x64'
steps:
- bash: echo $(COMMIT_MSG)
Expand All @@ -50,23 +50,14 @@ stages:
addToPath: true
displayName: 'Get Python'
- bash: |
set -e
set -eo pipefail
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install --progress-bar off -r requirements_base.txt -r requirements_hdf5.txt -r requirements_testing.txt
pre-commit install --install-hooks
displayName: Install dependencies
- bash: |
make flake
displayName: make flake
- bash: |
make codespell-error
displayName: make codespell
- bash: |
make pydocstyle
displayName: make pydocstyle
condition: always()
- bash: |
make docstring
displayName: make docstring
make pre-commit
displayName: make ruff
condition: always()
- bash: |
make nesting
Expand Down
16 changes: 10 additions & 6 deletions doc/install/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,16 @@ Adhere to standard Python style guidelines
All contributions to MNE-Python are checked against style guidelines described
in `PEP 8`_. We also check for common coding errors (such as variables that are
defined but never used). We allow very few exceptions to these guidelines, and
use tools such as pep8_, pyflakes_, and flake8_ to check code style
use tools such as ruff_ to check code style
automatically. From the :file:`mne-python` root directory, you can check for
style violations by running::
style violations by first installing our pre-commit hook::

$ make flake
$ pip install pre-commit
$ pre-commit install --install-hooks

Then running::

$ make ruff # alias for `pre-commit run -a`

in the shell. Several text editors or IDEs also have Python style checking,
which can highlight style errors while you code (and train you to make those
Expand Down Expand Up @@ -748,7 +753,7 @@ but complete docstrings are appropriate when private functions/methods are
relatively complex. To run some basic tests on documentation, you can use::

$ pytest mne/tests/test_docstring_parameters.py
$ make docstyle
$ make ruff


Cross-reference everywhere
Expand Down Expand Up @@ -1097,8 +1102,7 @@ it can serve as a useful example of what to expect from the PR review process.
.. linting
.. _PEP 8: https://www.python.org/dev/peps/pep-0008/
.. _pyflakes: https://pypi.org/project/pyflakes
.. _Flake8: http://flake8.pycqa.org/
.. _ruff: https://beta.ruff.rs/docs

.. misc
Expand Down
2 changes: 1 addition & 1 deletion examples/simulation/simulate_evoked_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@


def data_fun(times):
"""Function to generate random source time courses"""
"""Generate random source time courses."""
return (50e-9 * np.sin(30. * times) *
np.exp(- (times - 0.15 + 0.05 * rng.randn(1)) ** 2 / 0.01))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@


def data_fun(times, latency, duration):
"""Function to generate source time courses for evoked responses,
parametrized by latency and duration."""
"""Generate source time courses for evoked responses."""
f = 15 # oscillating frequency, beta band [Hz]
sigma = 0.375 * duration
sinusoid = np.sin(2 * np.pi * f * (times - latency))
Expand Down
3 changes: 2 additions & 1 deletion examples/visualization/topo_customized.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@


def my_callback(ax, ch_idx):
"""
"""Handle axes callback.
This block of code is executed once you click on one of the channel axes
in the plot. To work with the viz internals, this function should only take
two parameters, the axis and the channel or data index.
Expand Down
4 changes: 2 additions & 2 deletions mne/beamformer/tests/test_resolution_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def test_resolution_matrix_lcmv():
# Some rows are off by about 0.1 - not yet clear why
corr = []

for (f, l) in zip(resmat_fwd, resmat_lcmv):
for (f, lf) in zip(resmat_fwd, resmat_lcmv):

corr.append(np.corrcoef(f, l)[0, 1])
corr.append(np.corrcoef(f, lf)[0, 1])

# all row correlations should at least be above ~0.8
assert_allclose(corr, 1., atol=0.2)
Expand Down
4 changes: 2 additions & 2 deletions mne/chpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,8 @@ def compute_chpi_locs(info, chpi_amplitudes, t_step_max=1., too_close='raise',
# check if data has sufficiently changed
if last['sin_fit'] is not None: # first iteration
corrs = np.array(
[np.corrcoef(s, l)[0, 1]
for s, l in zip(sin_fit, last['sin_fit'])])
[np.corrcoef(s, lst)[0, 1]
for s, lst in zip(sin_fit, last['sin_fit'])])
corrs *= corrs
# check to see if we need to continue
if fit_time - last['coil_fit_time'] <= t_step_max - 1e-7 and \
Expand Down
5 changes: 4 additions & 1 deletion mne/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ def time_as_index(self, times, use_rounding=False, origin=None):

@property
def _raw_lengths(self):
return [l - f + 1 for f, l in zip(self._first_samps, self._last_samps)]
return [
last - first + 1
for first, last in zip(self._first_samps, self._last_samps)
]

@property
def annotations(self): # noqa: D401
Expand Down
1 change: 1 addition & 0 deletions mne/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ def _to_rgb(*args, name='color', alpha=False):

@deprecated('has_nibabel is deprecated and will be removed in 1.5')
def has_nibabel():
"""Check if nibabel is installed."""
return check_version('nibabel') # pragma: no cover


Expand Down
Loading

0 comments on commit 8325f80

Please sign in to comment.