Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: use ruff for linting and formatting #52

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/python-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Python Style

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v1
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +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: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.1
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
File renamed without changes.
1 change: 0 additions & 1 deletion examples/snr_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import mne
import numpy as np
import matplotlib.pyplot as plt

from pathlib import Path
Expand Down
42 changes: 0 additions & 42 deletions examples/usage.py

This file was deleted.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ Issues = "https://github.com/ctrltz/meegsim/issues"
dev = [
"harmoni",
"mock",
"pre-commit",
"pytest",
"pytest-cov"
"pytest-cov",
"ruff"
]
docs = [
"intersphinx-registry",
Expand Down
2 changes: 1 addition & 1 deletion src/meegsim/_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def check_names(names, n_sources, existing):
raise ValueError(f"Expected all names to be strings, got {actual_type}: {name}")

if not name:
raise ValueError(f"All names should not be empty")
raise ValueError("All names should not be empty")

if name.startswith('auto'):
raise ValueError(f"Name {name} should not start with auto, this prefix "
Expand Down
2 changes: 1 addition & 1 deletion src/meegsim/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import mne

from .utils import vertices_to_mne, get_sfreq, _extract_hemi
from .utils import vertices_to_mne, _extract_hemi


class _BaseSource:
Expand Down
6 changes: 3 additions & 3 deletions src/meegsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def _extract_hemi(src):
if src['id'] == FIFF.FIFFV_MNE_SURF_RIGHT_HEMI:
return 'rh'

raise ValueError(f"Unexpected ID for the provided surface source space. "
f"Please check the code that was used to generate and/or "
f"manipulate the src, it should not change the 'id' field.")
raise ValueError("Unexpected ID for the provided surface source space. "
"Please check the code that was used to generate and/or "
"manipulate the src, it should not change the 'id' field.")


def get_sfreq(times):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def test_sourceconfiguration_to_raw(apply_forward_mock):
apply_forward_mock.assert_called()
stc = apply_forward_mock.call_args.args[1]
assert np.all(stc.data == 1e-6), \
f"Default scaling factor was not applied correctly"
assert raw == 0, f"Output of apply_forward_raw should not be changed"
"Default scaling factor was not applied correctly"
assert raw == 0, "Output of apply_forward_raw should not be changed"

raw = sc.to_raw([], [], scaling_factor=10)
apply_forward_mock.assert_called()
stc = apply_forward_mock.call_args.args[1]
assert np.all(stc.data == 10), \
f"Custom scaling factor was not applied correctly"
assert raw == 0, f"Output of apply_forward_raw should not be changed"
"Custom scaling factor was not applied correctly"
assert raw == 0, "Output of apply_forward_raw should not be changed"
6 changes: 3 additions & 3 deletions tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_sourcesimulator_add_point_sources():
assert len(sim._sources) == 8, \
f"Expected eight sources to be created, got {len(sim._sources)}"
assert all([name in sim._sources for name in custom_names]), \
f"Provided source names were not used properly"
"Provided source names were not used properly"

# Add one group with already existing names
with pytest.raises(ValueError):
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_sourcesimulator_add_patch_sources():
assert len(sim._sources) == 4, \
f"Expected four sources to be created, got {len(sim._sources)}"
assert all([name in sim._sources for name in custom_names]), \
f"Provided source names were not used properly"
"Provided source names were not used properly"

# Add one group with already existing names
with pytest.raises(ValueError):
Expand Down Expand Up @@ -354,7 +354,7 @@ def test_simulate():
src, times=times, fwd=None, random_state=0)

assert len(simulate_mock.call_args_list) == 3, \
f"Expected three calls of PointSourceGroup.simulate method"
"Expected three calls of PointSourceGroup.simulate method"

random_states = [kall.kwargs['random_state'] == 0
for kall in simulate_mock.call_args_list]
Expand Down
5 changes: 2 additions & 3 deletions tests/test_sources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from unittest.mock import MagicMock, patch

import numpy as np
Expand Down Expand Up @@ -61,7 +60,7 @@ def test_pointsource_to_stc(src_idx, vertno):
assert vertno in stc.vertices[src_idx], \
f"Expected the vertex to be put in src {src_idx}, but it is not there"
assert np.allclose(stc.data, waveform), \
f"The source waveform should not change during conversion to stc"
"The source waveform should not change during conversion to stc"


@pytest.mark.parametrize("tstep", [0.01, 0.025, 0.05])
Expand Down Expand Up @@ -220,7 +219,7 @@ def test_patchsource_to_stc(src_idx, vertno):
assert np.all(vertno in stc.vertices[src_idx]), \
f"Expected all vertno to be put in src {src_idx}"
assert np.allclose(stc.data, waveform), \
f"The source waveform should not change during conversion to stc"
"The source waveform should not change during conversion to stc"


def test_patchsource_to_stc_bad_src_raises():
Expand Down