Skip to content

Commit

Permalink
MAINT: Enable more ruff rules (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Jan 26, 2024
1 parent 742e27e commit cc93c66
Show file tree
Hide file tree
Showing 78 changed files with 444 additions and 469 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: pip install --upgrade pip
- run: pip install -ve .[tests] codespell tomli
- run: make codespell-error
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
rev: v0.1.14
hooks:
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
Expand Down
6 changes: 3 additions & 3 deletions docs/hooks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
from typing import Dict, Any
from typing import Any

from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page

logger = logging.getLogger("mkdocs")

Expand All @@ -13,7 +13,7 @@
# Ideally there would be a better hook, but it's unclear if context can
# be obtained any earlier
def on_template_context(
context: Dict[str, Any],
context: dict[str, Any],
template_name: str,
config: MkDocsConfig,
) -> None:
Expand Down
14 changes: 8 additions & 6 deletions docs/source/examples/gen_examples.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env python

from collections import defaultdict
import contextlib
import logging
import shutil
from pathlib import Path
import sys
from typing import Union, Iterable
from collections import defaultdict
from collections.abc import Iterable
from pathlib import Path
from typing import Union

from tqdm import tqdm

import mne_bids_pipeline
from mne_bids_pipeline._config_import import _import_config
import mne_bids_pipeline.tests.datasets
from mne_bids_pipeline.tests.test_run import TEST_SUITE
from mne_bids_pipeline._config_import import _import_config
from mne_bids_pipeline.tests.datasets import DATASET_OPTIONS
from tqdm import tqdm
from mne_bids_pipeline.tests.test_run import TEST_SUITE

this_dir = Path(__file__).parent
root = Path(mne_bids_pipeline.__file__).parent.resolve(strict=True)
Expand Down
1 change: 1 addition & 0 deletions docs/source/features/gen_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import importlib
from pathlib import Path

from mne_bids_pipeline._config_utils import _get_step_modules

pre = """\
Expand Down
2 changes: 1 addition & 1 deletion docs/source/v1.6.md.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
### :medical_symbol: Code health

- The package build backend has been switched from `setuptools` to `hatchling`. (#825 by @hoechenberger)
- Code formatting now uses `ruff format` instead of `black` (#834 by @larsoner)
- Code formatting now uses `ruff format` instead of `black` (#834, #838 by @larsoner)
- Code caching is now tested using GitHub Actions (#836 by @larsoner)
2 changes: 1 addition & 1 deletion mne_bids_pipeline/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from importlib.metadata import version, PackageNotFoundError
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("mne_bids_pipeline")
Expand Down
55 changes: 27 additions & 28 deletions mne_bids_pipeline/_config.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Default settings for data processing and analysis.

from typing import Optional, Union, Iterable, List, Tuple, Dict, Callable, Literal
from typing import Callable, Iterable, Literal, Optional, Union

from mne import Covariance
from mne_bids import BIDSPath

from mne_bids_pipeline.typing import (
PathLike,
ArbitraryContrast,
FloatArrayLike,
DigMontageType,
FloatArrayLike,
PathLike,
)


###############################################################################
# Config parameters
# -----------------
Expand Down Expand Up @@ -84,7 +83,7 @@
Enabling interactive mode deactivates parallel processing.
"""

sessions: Union[List, Literal["all"]] = "all"
sessions: Union[list, Literal["all"]] = "all"
"""
The sessions to process. If `'all'`, will process all sessions found in the
BIDS dataset.
Expand All @@ -101,7 +100,7 @@
BIDS dataset.
"""

exclude_runs: Optional[Dict[str, List[str]]] = None
exclude_runs: Optional[dict[str, list[str]]] = None
"""
Specify runs to exclude from analysis, for each participant individually.
Expand All @@ -117,7 +116,7 @@
did not understand the instructions, etc.).
"""

crop_runs: Optional[Tuple[float, float]] = None
crop_runs: Optional[tuple[float, float]] = None
"""
Crop the raw data of each run to the specified time interval `[tmin, tmax]`,
in seconds. The runs will be cropped before Maxwell or frequency filtering is
Expand Down Expand Up @@ -288,7 +287,7 @@
```
"""

eeg_bipolar_channels: Optional[Dict[str, Tuple[str, str]]] = None
eeg_bipolar_channels: Optional[dict[str, tuple[str, str]]] = None
"""
Combine two channels into a bipolar channel, whose signal is the **difference**
between the two combined channels, and add it to the data.
Expand Down Expand Up @@ -688,7 +687,7 @@
Number of extended SSS (eSSS) basis projectors to use from empty-room data.
"""

mf_esss_reject: Optional[Dict[str, float]] = None
mf_esss_reject: Optional[dict[str, float]] = None
"""
Rejection parameters to use when computing the extended SSS (eSSS) basis.
"""
Expand Down Expand Up @@ -980,7 +979,7 @@
```
""" # noqa: E501

conditions: Optional[Union[Iterable[str], Dict[str, str]]] = None
conditions: Optional[Union[Iterable[str], dict[str, str]]] = None
"""
The time-locked events based on which to create evoked responses.
This can either be name of the experimental condition as specified in the
Expand Down Expand Up @@ -1048,7 +1047,7 @@
and when the annotations do not contain any stimulation or behavior events.
"""

baseline: Optional[Tuple[Optional[float], Optional[float]]] = (None, 0)
baseline: Optional[tuple[Optional[float], Optional[float]]] = (None, 0)
"""
Specifies which time interval to use for baseline correction of epochs;
if `None`, no baseline correction is applied.
Expand All @@ -1059,7 +1058,7 @@
```
"""

contrasts: Iterable[Union[Tuple[str, str], ArbitraryContrast]] = []
contrasts: Iterable[Union[tuple[str, str], ArbitraryContrast]] = []
"""
The conditions to contrast via a subtraction of ERPs / ERFs. The list elements
can either be tuples or dictionaries (or a mix of both). Each element in the
Expand Down Expand Up @@ -1156,12 +1155,12 @@

# Rejection based on SSP
# ~~~~~~~~~~~~~~~~~~~~~~
n_proj_eog: Dict[str, float] = dict(n_mag=1, n_grad=1, n_eeg=1)
n_proj_eog: dict[str, float] = dict(n_mag=1, n_grad=1, n_eeg=1)
"""
Number of SSP vectors to create for EOG artifacts for each channel type.
"""

n_proj_ecg: Dict[str, float] = dict(n_mag=1, n_grad=1, n_eeg=1)
n_proj_ecg: dict[str, float] = dict(n_mag=1, n_grad=1, n_eeg=1)
"""
Number of SSP vectors to create for ECG artifacts for each channel type.
"""
Expand Down Expand Up @@ -1189,7 +1188,7 @@
`'separate'` otherwise.
"""

ssp_reject_ecg: Optional[Union[Dict[str, float], Literal["autoreject_global"]]] = None
ssp_reject_ecg: Optional[Union[dict[str, float], Literal["autoreject_global"]]] = None
"""
Peak-to-peak amplitude limits of the ECG epochs to exclude from SSP fitting.
This allows you to remove strong transient artifacts, which could negatively
Expand All @@ -1207,7 +1206,7 @@
```
"""

ssp_reject_eog: Optional[Union[Dict[str, float], Literal["autoreject_global"]]] = None
ssp_reject_eog: Optional[Union[dict[str, float], Literal["autoreject_global"]]] = None
"""
Peak-to-peak amplitude limits of the EOG epochs to exclude from SSP fitting.
This allows you to remove strong transient artifacts, which could negatively
Expand All @@ -1233,11 +1232,11 @@

# Rejection based on ICA
# ~~~~~~~~~~~~~~~~~~~~~~
ica_reject: Optional[Union[Dict[str, float], Literal["autoreject_local"]]] = None
ica_reject: Optional[Union[dict[str, float], Literal["autoreject_local"]]] = None
"""
Peak-to-peak amplitude limits to exclude epochs from ICA fitting. This allows you to
remove strong transient artifacts from the epochs used for fitting ICA, which could
negatively affect ICA performance.
negatively affect ICA performance.
The parameter values are the same as for [`reject`][mne_bids_pipeline._config.reject],
but `"autoreject_global"` is not supported. `"autoreject_local"` here behaves
Expand All @@ -1264,7 +1263,7 @@
to **not** specify rejection thresholds for EOG and ECG channels here –
otherwise, ICA won't be able to "see" these artifacts.
???+ info
???+ info
This setting is applied only to the epochs that are used for **fitting** ICA. The
goal is to make it easier for ICA to produce a good decomposition. After fitting,
ICA is applied to the epochs to be analyzed, usually with one or more components
Expand All @@ -1280,7 +1279,7 @@
ica_reject = "autoreject_global" # find global (per channel type) PTP thresholds before fitting ICA
ica_reject = "autoreject_local" # find local (per channel) thresholds and repair epochs before fitting ICA
```
"""
""" # noqa: E501

ica_algorithm: Literal[
"picard", "fastica", "extended_infomax", "picard-extended_infomax"
Expand Down Expand Up @@ -1373,7 +1372,7 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

reject: Optional[
Union[Dict[str, float], Literal["autoreject_global", "autoreject_local"]]
Union[dict[str, float], Literal["autoreject_global", "autoreject_local"]]
] = None
"""
Peak-to-peak amplitude limits to mark epochs as bad. This allows you to remove
Expand All @@ -1386,7 +1385,7 @@
If `None` (default), do not apply artifact rejection.
If a dictionary, manually specify rejection thresholds (see examples).
If a dictionary, manually specify rejection thresholds (see examples).
The thresholds provided here must be at least as stringent as those in
[`ica_reject`][mne_bids_pipeline._config.ica_reject] if using ICA. In case of
`'autoreject_global'`, thresholds for any channel that do not meet this
Expand All @@ -1409,7 +1408,7 @@
reject = "autoreject_global" # find global (per channel type) PTP thresholds
reject = "autoreject_local" # find local (per channel) thresholds and repair epochs
```
"""
""" # noqa: E501

reject_tmin: Optional[float] = None
"""
Expand Down Expand Up @@ -1689,7 +1688,7 @@
```
"""

decoding_csp_freqs: Optional[Dict[str, FloatArrayLike]] = None
decoding_csp_freqs: Optional[dict[str, FloatArrayLike]] = None
"""
The edges of the frequency bins to use for CSP decoding.
Expand Down Expand Up @@ -1733,7 +1732,7 @@
}
"""

time_frequency_baseline: Optional[Tuple[float, float]] = None
time_frequency_baseline: Optional[tuple[float, float]] = None
"""
Baseline period to use for the time-frequency analysis. If `None`, no baseline.
???+ example "Example"
Expand Down Expand Up @@ -1964,7 +1963,7 @@ def mri_landmarks_kind(bids_path):
"""

noise_cov: Union[
Tuple[Optional[float], Optional[float]],
tuple[Optional[float], Optional[float]],
Literal["emptyroom", "rest", "ad-hoc"],
Callable[[BIDSPath], Covariance],
] = (None, 0)
Expand Down Expand Up @@ -2031,7 +2030,7 @@ def noise_cov(bids_path):
```
"""

source_info_path_update: Optional[Dict[str, str]] = dict(suffix="ave")
source_info_path_update: Optional[dict[str, str]] = dict(suffix="ave")
"""
When computing the forward and inverse solutions, by default the pipeline
retrieves the `mne.Info` object from the cleaned evoked data. However, in
Expand All @@ -2049,7 +2048,7 @@ def noise_cov(bids_path):
```
"""

inverse_targets: List[Literal["evoked"]] = ["evoked"]
inverse_targets: list[Literal["evoked"]] = ["evoked"]
"""
On which data to apply the inverse operator. Currently, the only supported
Expand Down
17 changes: 8 additions & 9 deletions mne_bids_pipeline/_config_import.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import ast
import copy
from dataclasses import field
import difflib
from functools import partial
import importlib
import os
import pathlib
from dataclasses import field
from functools import partial
from types import SimpleNamespace
from typing import Optional, List
from typing import Optional

import matplotlib
import numpy as np
import mne

import numpy as np
from pydantic import ValidationError
from pydantic.dataclasses import dataclass

from ._logging import logger, gen_log_kwargs
from ._logging import gen_log_kwargs, logger
from .typing import PathLike


Expand Down Expand Up @@ -150,7 +149,7 @@ def _update_with_user_config(
config_path: Optional[PathLike],
overrides: Optional[SimpleNamespace],
log: bool = False,
) -> List[str]:
) -> list[str]:
# 1. Basics and hidden vars
from . import __version__

Expand Down Expand Up @@ -433,8 +432,8 @@ def _pydantic_validate(
def _check_misspellings_removals(
config: SimpleNamespace,
*,
valid_names: List[str],
user_names: List[str],
valid_names: list[str],
user_names: list[str],
log: bool,
) -> None:
# for each name in the user names, check if it's in the valid names but
Expand Down
8 changes: 3 additions & 5 deletions mne_bids_pipeline/_config_template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from pathlib import Path
from typing import List

from ._logging import logger, gen_log_kwargs

from ._logging import gen_log_kwargs, logger

CONFIG_SOURCE_PATH = Path(__file__).parent / "_config.py"

Expand All @@ -17,8 +15,8 @@ def create_template_config(
raise FileExistsError(f"The specified path already exists: {target_path}")

# Create a template by commenting out most of the lines in _config.py
config: List[str] = []
with open(CONFIG_SOURCE_PATH, "r", encoding="utf-8") as f:
config: list[str] = []
with open(CONFIG_SOURCE_PATH, encoding="utf-8") as f:
for line in f:
line = (
line if line.startswith(("#", "\n", "import", "from")) else f"# {line}"
Expand Down
Loading

0 comments on commit cc93c66

Please sign in to comment.