Skip to content

Commit

Permalink
Replace flake8/isort/black with ruff (#115)
Browse files Browse the repository at this point in the history
* Minor formatting changes

* Replace flake8/isort/black with ruff

* Remove old config entries

* Forgot f in f-string

* Add missing colon

* Add missing space

* Ignore docs directory

* Clean up Makefile

* Do not exclude docs

* Do not globally exclude A002
  • Loading branch information
cbrnr authored Feb 11, 2024
1 parent 80f758c commit cafcd96
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 309 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ you can run tests by making use of the `Makefile` and
From the project root, call:

- `make test` to run tests and coverage
- `make pep` to run style checks
- `pre-commit run` to run style checks (Ruff and some additional hooks)

## Building the documentation

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
- name: Check formatting
if: ${{ matrix.platform == 'ubuntu-22.04' && matrix.python-version == '3.11' }}
run: |
make pep
make check-manifest
pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )
- name: Test with pytest
Expand Down
41 changes: 17 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-ast
- id: check-added-large-files
- id: check-case-conflict
- id: check-docstring-first
- repo: https://github.com/pycqa/isort
rev: "5.12.0"
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: "23.9.1"
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: "6.1.0"
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-ast
- id: check-added-large-files
- id: check-case-conflict
- id: check-docstring-first
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
22 changes: 2 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all inplace test flake pydocstyle check-manifest isort black pep build-doc dist-build
.PHONY: all inplace test check-manifest build-doc dist-build

all: inplace pep test build-doc dist-build
all: inplace check-manifest test build-doc dist-build

inplace:
@echo "Installing pybv"
Expand All @@ -12,28 +12,10 @@ test:
@echo "Running pytest: test modules"
@pytest --cov=./pybv --cov-report=xml --verbose

flake:
@echo "Running flake8"
@flake8 --docstring-convention numpy --count pybv

pydocstyle:
@echo "Running pydocstyle"
@pydocstyle

check-manifest:
@echo "Running check-manifest"
@check-manifest

isort:
@echo "Running check-manifest"
@isort .

black:
@echo "Running black"
@black .

pep: flake pydocstyle check-manifest isort black

build-doc:
@echo "Building documentation"
make -C docs/ clean
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

# General information about the project.
project = "pybv"
copyright = "2018-{}, pybv developers".format(date.today().year)
copyright = f"2018-{date.today().year}, pybv developers" # noqa:A001
author = "pybv developers"
version = pybv.__version__
release = version
Expand Down
6 changes: 5 additions & 1 deletion docs/sphinxext/gh_substitutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
from docutils.parsers.rst.roles import set_classes


def gh_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
def gh_role(name, rawtext, text, lineno, inliner, options=None, content=None):
"""Link to a GitHub issue."""
if content is None:
content = []
if options is None:
options = {}
try:
# issue/PR mode (issues/PR-num will redirect to pull/PR-num)
int(text)
Expand Down
2 changes: 1 addition & 1 deletion pybv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A lightweight I/O utility for the BrainVision data format."""


__version__ = "0.8.0.dev5"

from .io import write_brainvision

__all__ = ["write_brainvision"]
44 changes: 22 additions & 22 deletions pybv/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ def _export_mne_raw(*, raw, fname, events=None, overwrite=False):
The raw data to export.
fname : str | pathlib.Path
The name of the file where raw data will be exported to. Must end with
``".vhdr"``, and accompanying *.vmrk* and *.eeg* files will be written
inside the same directory.
``".vhdr"``, and accompanying *.vmrk* and *.eeg* files will be written inside
the same directory.
events : np.ndarray | None
Events to be written to the marker file (*.vmrk*). When array, must be in
`MNE-Python format <https://mne.tools/stable/glossary.html#term-events>`_.
When ``None`` (default), events will be written based on ``raw.annotations``.
Events to be written to the marker file (*.vmrk*). If array, must be in
`MNE-Python format <https://mne.tools/stable/glossary.html#term-events>`_. If
``None`` (default), events will be written based on ``raw.annotations``.
overwrite : bool
Whether or not to overwrite existing data. Defaults to ``False``.
"""
# Prepare file location
# prepare file location
if not str(fname).endswith(".vhdr"):
raise ValueError("`fname` must have the '.vhdr' extension for BrainVision.")
fname = Path(fname)
folder_out = fname.parents[0]
fname_base = fname.stem

# Prepare data from raw
# prepare data from raw
data = raw.get_data() # gets data starting from raw.first_samp
sfreq = raw.info["sfreq"] # in Hz
meas_date = raw.info["meas_date"] # datetime.datetime
ch_names = raw.ch_names

# write voltage units as micro-volts and all other units without
# scaling
# We write units that we don't know as n/a
# write voltage units as micro-volts and all other units without scaling
# write units that we don't know as n/a
unit = []
for ch in raw.info["chs"]:
if ch["unit"] == FIFF.FIFF_UNIT_V:
Expand All @@ -54,9 +54,9 @@ def _export_mne_raw(*, raw, fname, events=None, overwrite=False):
unit.append(_unit2human.get(ch["unit"], "n/a"))
unit = [u if u != "NA" else "n/a" for u in unit]

# We enforce conversion to float32 format
# XXX: Could add a feature that checks data and optimizes `unit`, `resolution`,
# and `format` so that raw.orig_format could be retained if reasonable.
# enforce conversion to float32 format
# XXX: Could add a feature that checks data and optimizes `unit`, `resolution`, and
# `format` so that raw.orig_format could be retained if reasonable.
if raw.orig_format != "single":
warn(
f"Encountered data in '{raw.orig_format}' format. "
Expand All @@ -67,12 +67,12 @@ def _export_mne_raw(*, raw, fname, events=None, overwrite=False):
fmt = "binary_float32"
resolution = 0.1

# Handle events
# handle events
# if we got an ndarray, this is in MNE-Python format
msg = "`events` must be None or array in MNE-Python format."
if events is not None:
# Subtract raw.first_samp because brainvision marks events starting from
# the first available data point and ignores the raw.first_samp
# Subtract raw.first_samp because brainvision marks events starting from the
# first available data point and ignores the raw.first_samp
assert isinstance(events, np.ndarray), msg
assert events.ndim == 2, msg
assert events.shape[-1] == 3, msg
Expand Down Expand Up @@ -107,14 +107,14 @@ def _mne_annots2pybv_events(raw):
"""Convert mne Annotations to pybv events."""
events = []
for annot in raw.annotations:
# Handle onset and duration: seconds to sample,
# relative to raw.first_samp / raw.first_time
# handle onset and duration: seconds to sample, relative to
# raw.first_samp / raw.first_time
onset = annot["onset"] - raw.first_time
onset = raw.time_as_index(onset).astype(int)[0]
duration = int(annot["duration"] * raw.info["sfreq"])

# Triage type and description
# Defaults to type="Comment", and the full description
# triage type and description
# defaults to type="Comment" and the full description
etype = "Comment"
description = annot["description"]
for start in ["Stimulus/S", "Response/R", "Comment/"]:
Expand All @@ -137,11 +137,11 @@ def _mne_annots2pybv_events(raw):
)

if "ch_names" in annot:
# Handle channels
# handle channels
channels = list(annot["ch_names"])
event_dict["channels"] = channels

# Add a "pybv" event
# add a "pybv" event
events += [event_dict]

return events
Loading

0 comments on commit cafcd96

Please sign in to comment.