Skip to content

Commit

Permalink
Replace flake8/isort/black with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrnr committed Feb 11, 2024
1 parent fb694fe commit 9e0d0c3
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 262 deletions.
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
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 9e0d0c3

Please sign in to comment.