From 97bbc37a7375e835f3db7d2d99f8168f7338abae Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Fri, 28 Apr 2023 11:12:13 -0400 Subject: [PATCH] MAINT: Use black --- .github/workflows/tests.yml | 1 + .pre-commit-config.yaml | 10 +++++----- azure-pipelines.yml | 2 +- ignore_words.txt | 2 ++ mne/channels/tests/test_montage.py | 12 ++++++------ mne/chpi.py | 11 ++++++----- mne/conftest.py | 6 +++--- mne/coreg.py | 14 ++++++++++---- mne/io/kit/tests/test_kit.py | 19 ++++++++++++------- mne/io/tests/test_raw.py | 12 +++++++++--- mne/io/tests/test_reference.py | 3 ++- mne/source_space.py | 9 +++++++-- mne/tests/test_annotations.py | 4 ++-- mne/tests/test_docstring_parameters.py | 5 ++++- mne/tests/test_source_estimate.py | 2 +- mne/tests/test_source_space.py | 2 +- mne/transforms.py | 2 +- mne/utils/tests/test_check.py | 4 +++- mne/viz/backends/_notebook.py | 10 ++++++++-- mne/viz/backends/_qt.py | 12 +++++++++--- pyproject.toml | 4 +++- requirements_testing.txt | 1 + .../forward/50_background_freesurfer_mne.py | 2 +- 23 files changed, 98 insertions(+), 51 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3e8a3195c7a..d535e037c0a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,6 +23,7 @@ jobs: - uses: actions/setup-python@v4 with: python-version: '3.11' + - uses: psf/black@stable - uses: pre-commit/action@v3.0.0 pytest: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7ef76755eae..59a60c19015 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,9 +1,9 @@ repos: -# - repo: https://github.com/psf/black -# rev: 23.1.0 -# hooks: -# - id: black -# args: [--quiet] +- repo: https://github.com/psf/black + rev: 23.3.0 + hooks: + - id: black + args: [--quiet] # Ruff mne - repo: https://github.com/charliermarsh/ruff-pre-commit diff --git a/azure-pipelines.yml b/azure-pipelines.yml index b050cc191c1..f0665efc164 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -57,7 +57,7 @@ stages: displayName: Install dependencies - bash: | make pre-commit - displayName: make ruff + displayName: make pre-commit condition: always() - bash: | make nesting diff --git a/ignore_words.txt b/ignore_words.txt index 8dde5403c07..c09662e1a1a 100644 --- a/ignore_words.txt +++ b/ignore_words.txt @@ -14,6 +14,7 @@ nd cas thes ba +bu ist od fo @@ -33,3 +34,4 @@ recuse ro nam shs +pres diff --git a/mne/channels/tests/test_montage.py b/mne/channels/tests/test_montage.py index 5fe16a2294d..f78e6bb3f2d 100644 --- a/mne/channels/tests/test_montage.py +++ b/mne/channels/tests/test_montage.py @@ -223,12 +223,12 @@ def test_documented(): pytest.param( partial(read_custom_montage, head_size=None, coord_frame='mri'), - ('// MatLab Sphere coordinates [degrees] Cartesian coordinates\n' # noqa: E501 - '// Label Theta Phi Radius X Y Z off sphere surface\n' # noqa: E501 - 'E1 37.700 -14.000 1.000 0.7677 0.5934 -0.2419 -0.00000000000000011\n' # noqa: E501 - 'E3 51.700 11.000 1.000 0.6084 0.7704 0.1908 0.00000000000000000\n' # noqa: E501 - 'E31 90.000 -11.000 1.000 0.0000 0.9816 -0.1908 0.00000000000000000\n' # noqa: E501 - 'E61 158.000 -17.200 1.000 -0.8857 0.3579 -0.2957 -0.00000000000000022'), # noqa: E501 + "// MatLab Sphere coordinates [degrees] Cartesian coordinates\n" # noqa: E501 + "// Label Theta Phi Radius X Y Z off sphere surface\n" # noqa: E501 + "E1 37.700 -14.000 1.000 0.7677 0.5934 -0.2419 -0.00000000000000011\n" # noqa: E501 + "E3 51.700 11.000 1.000 0.6084 0.7704 0.1908 0.00000000000000000\n" # noqa: E501 + "E31 90.000 -11.000 1.000 0.0000 0.9816 -0.1908 0.00000000000000000\n" # noqa: E501 + "E61 158.000 -17.200 1.000 -0.8857 0.3579 -0.2957 -0.00000000000000022", # noqa: E501 make_dig_montage( ch_pos={ 'E1': [0.7677, 0.5934, -0.2419], diff --git a/mne/chpi.py b/mne/chpi.py index 648ad6ca78a..9d80fa6efde 100644 --- a/mne/chpi.py +++ b/mne/chpi.py @@ -847,11 +847,12 @@ def compute_head_pos(info, chpi_locs, dist_limit=0.005, gof_limit=0.98, # 1. Check number of good ones # if len(use_idx) < 3: - msg = (_time_prefix(fit_time) + '%s/%s good HPI fits, cannot ' - 'determine the transformation (%s GOF)!' - % (len(use_idx), n_coils, - ', '.join('%0.2f' % g for g in g_coils))) - warn(msg) + gofs = ', '.join(f"{g:0.2f}" for g in g_coils) + warn( + f"{_time_prefix(fit_time)}{len(use_idx)}/{n_coils} " + "good HPI fits, cannot determine the transformation " + f"({gofs} GOF)!" + ) continue # diff --git a/mne/conftest.py b/mne/conftest.py index 9a64066b852..72e95b6e788 100644 --- a/mne/conftest.py +++ b/mne/conftest.py @@ -101,8 +101,8 @@ def pytest_configure(config): first_kind = 'error' else: first_kind = 'always' - warning_lines = r""" - {0}:: + warning_lines = f" {first_kind}::" + warning_lines += r""" # matplotlib->traitlets (notebook) ignore:Passing unrecognized arguments to super.*:DeprecationWarning # notebook tests @@ -142,7 +142,7 @@ def pytest_configure(config): ignore:pkg_resources is deprecated as an API.*:DeprecationWarning # h5py ignore:`product` is deprecated as of NumPy.*:DeprecationWarning - """.format(first_kind) # noqa: E501 + """ # noqa: E501 for warning_line in warning_lines.split('\n'): warning_line = warning_line.strip() if warning_line and not warning_line.startswith('#'): diff --git a/mne/coreg.py b/mne/coreg.py index db0b3645633..3e21f3ff917 100644 --- a/mne/coreg.py +++ b/mne/coreg.py @@ -23,11 +23,17 @@ from .io._digitization import _get_data_as_dict_from_dig # keep get_mni_fiducials for backward compat (no burden to keep in this # namespace, too) -from ._freesurfer import (_read_mri_info, get_mni_fiducials, # noqa: F401 - estimate_head_mri_t) # noqa: F401 +from ._freesurfer import ( + _read_mri_info, + get_mni_fiducials, + estimate_head_mri_t, # noqa: F401 +) from .label import read_label, Label -from .source_space import (add_source_space_distances, read_source_spaces, # noqa: E501,F401 - write_source_spaces) +from .source_space import ( + add_source_space_distances, + read_source_spaces, # noqa: F401 + write_source_spaces, +) from .surface import (read_surface, write_surface, _normalize_vectors, complete_surface_info, decimate_surface, _DistanceQuery) diff --git a/mne/io/kit/tests/test_kit.py b/mne/io/kit/tests/test_kit.py index d3746012328..696d10a83da 100644 --- a/mne/io/kit/tests/test_kit.py +++ b/mne/io/kit/tests/test_kit.py @@ -69,8 +69,9 @@ def test_data(tmp_path): # check functionality raw_mrk = read_raw_kit(sqd_path, [mrk2_path, mrk3_path], elp_txt_path, hsp_txt_path) - assert raw_mrk.info['description'] == \ - 'NYU 160ch System since Jan24 2009 (34) V2R004 EQ1160C' + assert ( + raw_mrk.info['description'] == 'NYU 160ch System since Jan24 2009 (34) V2R004 EQ1160C' # noqa: E501 + ) raw_py = _test_raw_reader(read_raw_kit, input_fname=sqd_path, mrk=mrk_path, elp=elp_txt_path, hsp=hsp_txt_path, stim=list(range(167, 159, -1)), slope='+', @@ -123,8 +124,9 @@ def test_data(tmp_path): # KIT-UMD data _test_raw_reader(read_raw_kit, input_fname=sqd_umd_path, test_rank='less') raw = read_raw_kit(sqd_umd_path) - assert raw.info['description'] == \ - 'University of Maryland/Kanazawa Institute of Technology/160-channel MEG System (53) V2R004 PQ1160R' # noqa: E501 + assert ( + raw.info['description'] == 'University of Maryland/Kanazawa Institute of Technology/160-channel MEG System (53) V2R004 PQ1160R' # noqa: E501 + ) assert_equal(raw.info['kit_system_id'], KIT.SYSTEM_UMD_2014_12) # check number/kind of channels assert_equal(len(raw.info['chs']), 193) @@ -135,8 +137,9 @@ def test_data(tmp_path): # KIT Academia Sinica raw = read_raw_kit(sqd_as_path, slope='+') - assert raw.info['description'] == \ - 'Academia Sinica/Institute of Linguistics//Magnetoencephalograph System (261) V2R004 PQ1160R-N2' # noqa: E501 + assert ( + raw.info['description'] == 'Academia Sinica/Institute of Linguistics//Magnetoencephalograph System (261) V2R004 PQ1160R-N2' # noqa: E501 + ) assert_equal(raw.info['kit_system_id'], KIT.SYSTEM_AS_2008) assert_equal(raw.info['chs'][100]['ch_name'], 'MEG 101') assert_equal(raw.info['chs'][100]['kind'], FIFF.FIFFV_MEG_CH) @@ -374,7 +377,9 @@ def test_berlin(): """Test data from Berlin.""" # gh-8535 raw = read_raw_kit(berlin_path) - assert raw.info['description'] == 'Physikalisch Technische Bundesanstalt, Berlin/128-channel MEG System (124) V2R004 PQ1128R-N2' # noqa: E501 + assert ( + raw.info['description'] == 'Physikalisch Technische Bundesanstalt, Berlin/128-channel MEG System (124) V2R004 PQ1128R-N2' # noqa: E501 + ) assert raw.info['kit_system_id'] == 124 assert raw.info['highpass'] == 0. assert raw.info['lowpass'] == 200. diff --git a/mne/io/tests/test_raw.py b/mne/io/tests/test_raw.py index 694cd46c941..4c728df90ef 100644 --- a/mne/io/tests/test_raw.py +++ b/mne/io/tests/test_raw.py @@ -739,9 +739,15 @@ def test_describe_print(): assert re.match( r'', s[0]) is not None, s[0] - assert s[1] == " ch name type unit min Q1 median Q3 max" # noqa - assert s[2] == " 0 MEG 0113 GRAD fT/cm -221.80 -38.57 -9.64 19.29 414.67" # noqa - assert s[-1] == "375 EOG 061 EOG µV -231.41 271.28 277.16 285.66 334.69" # noqa + assert ( + s[1] == " ch name type unit min Q1 median Q3 max" # noqa: E501 + ) + assert ( + s[2] == " 0 MEG 0113 GRAD fT/cm -221.80 -38.57 -9.64 19.29 414.67" # noqa: E501 + ) + assert ( + s[-1] == "375 EOG 061 EOG µV -231.41 271.28 277.16 285.66 334.69" # noqa: E501 + ) @requires_pandas diff --git a/mne/io/tests/test_reference.py b/mne/io/tests/test_reference.py index 8ab37fb5879..0cfb2a5349e 100644 --- a/mne/io/tests/test_reference.py +++ b/mne/io/tests/test_reference.py @@ -329,7 +329,8 @@ def test_set_eeg_reference_rest(): # load('leadfield.mat', 'G'); # dat_ref = ft_preproc_rereference(dat, 'all', 'rest', true, G); # sprintf('%g ', dat_ref(:, 171)); - want = np.array('-3.3265e-05 -3.2419e-05 -3.18758e-05 -3.24079e-05 -3.39801e-05 -3.40573e-05 -3.24163e-05 -3.26896e-05 -3.33814e-05 -3.54734e-05 -3.51289e-05 -3.53229e-05 -3.51532e-05 -3.53149e-05 -3.4505e-05 -3.03462e-05 -2.81848e-05 -3.08895e-05 -3.27158e-05 -3.4605e-05 -3.47728e-05 -3.2459e-05 -3.06552e-05 -2.53255e-05 -2.69671e-05 -2.83425e-05 -3.12836e-05 -3.30965e-05 -3.34099e-05 -3.32766e-05 -3.32256e-05 -3.36385e-05 -3.20796e-05 -2.7108e-05 -2.47054e-05 -2.49589e-05 -2.7382e-05 -3.09774e-05 -3.12003e-05 -3.1246e-05 -3.07572e-05 -2.64942e-05 -2.25505e-05 -2.67194e-05 -2.86e-05 -2.94903e-05 -2.96249e-05 -2.92653e-05 -2.86472e-05 -2.81016e-05 -2.69737e-05 -2.48076e-05 -3.00473e-05 -2.73404e-05 -2.60153e-05 -2.41608e-05 -2.61937e-05 -2.5539e-05 -2.47104e-05 -2.35194e-05'.split(' '), float) # noqa: E501 + data_array = "-3.3265e-05 -3.2419e-05 -3.18758e-05 -3.24079e-05 -3.39801e-05 -3.40573e-05 -3.24163e-05 -3.26896e-05 -3.33814e-05 -3.54734e-05 -3.51289e-05 -3.53229e-05 -3.51532e-05 -3.53149e-05 -3.4505e-05 -3.03462e-05 -2.81848e-05 -3.08895e-05 -3.27158e-05 -3.4605e-05 -3.47728e-05 -3.2459e-05 -3.06552e-05 -2.53255e-05 -2.69671e-05 -2.83425e-05 -3.12836e-05 -3.30965e-05 -3.34099e-05 -3.32766e-05 -3.32256e-05 -3.36385e-05 -3.20796e-05 -2.7108e-05 -2.47054e-05 -2.49589e-05 -2.7382e-05 -3.09774e-05 -3.12003e-05 -3.1246e-05 -3.07572e-05 -2.64942e-05 -2.25505e-05 -2.67194e-05 -2.86e-05 -2.94903e-05 -2.96249e-05 -2.92653e-05 -2.86472e-05 -2.81016e-05 -2.69737e-05 -2.48076e-05 -3.00473e-05 -2.73404e-05 -2.60153e-05 -2.41608e-05 -2.61937e-05 -2.5539e-05 -2.47104e-05 -2.35194e-05" # noqa: E501 + want = np.array(data_array.split(" "), float) norm = np.linalg.norm(want) idx = np.argmin(np.abs(evoked.times - 0.083)) assert idx == 170 diff --git a/mne/source_space.py b/mne/source_space.py index 8c7e8899ea1..6eb6c000537 100644 --- a/mne/source_space.py +++ b/mne/source_space.py @@ -31,8 +31,13 @@ complete_surface_info, _compute_nearest, fast_cross_3d, _CheckInside) # keep get_mni_fiducials here just for easy backward compat -from ._freesurfer import (_get_mri_info_data, _get_atlas_values, # noqa: F401 - read_freesurfer_lut, get_mni_fiducials, _check_mri) +from ._freesurfer import ( + _get_mri_info_data, + _get_atlas_values, + read_freesurfer_lut, + get_mni_fiducials, # noqa: F401 + _check_mri, +) from .utils import (get_subjects_dir, check_fname, logger, verbose, fill_doc, _ensure_int, _get_call_line, warn, object_size, sizeof_fmt, _check_fname, _path_like, _check_sphere, _import_nibabel, diff --git a/mne/tests/test_annotations.py b/mne/tests/test_annotations.py index d1a311bc9ae..d1e35cebc0d 100644 --- a/mne/tests/test_annotations.py +++ b/mne/tests/test_annotations.py @@ -987,7 +987,7 @@ def test_io_annotation_txt(dummy_annotation_txt_file, tmp_path_factory, pytest.param(None, None, id='None'), pytest.param(42, 42.0, id='Scalar'), pytest.param(3.14, 3.14, id='Float'), - pytest.param((3, 140000), 3.14, id='Scalar touple'), + pytest.param((3, 140000), 3.14, id="Scalar tuple"), pytest.param('2002-12-03 19:01:11.720100', 1038942071.7201, id='valid iso8601 string'), pytest.param('2002-12-03T19:01:11.720100', None, @@ -1355,7 +1355,7 @@ def test_annotation_ch_names(): assert raw_2.annotations.ch_names[1] == tuple(raw.ch_names[4:5]) for ch_drop in raw_2.annotations.ch_names: assert all(name in raw_2.ch_names for name in ch_drop) - with pytest.raises(ValueError, match='channel name in annotations missin'): + with pytest.raises(ValueError, match='channel name in annotations miss'): raw_2.set_annotations(annot) with pytest.warns(RuntimeWarning, match='channel name in annotations mis'): raw_2.set_annotations(annot, on_missing='warn') diff --git a/mne/tests/test_docstring_parameters.py b/mne/tests/test_docstring_parameters.py index ddfec15686e..7a3f59783bc 100644 --- a/mne/tests/test_docstring_parameters.py +++ b/mne/tests/test_docstring_parameters.py @@ -130,7 +130,10 @@ def check_parameters_match(func, cls=None): msg = str(exc) # E ValueError: no signature found for builtin type # - if inspect.isclass(callable_) and 'no signature found for buil' in msg: + if ( + inspect.isclass(callable_) and + "no signature found for builtin type" in msg + ): pass else: raise diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index ec6da53cf56..02e174556c1 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -1898,5 +1898,5 @@ def test_label_extraction_subject(kind): with pytest.raises(ValueError, match=r'label\.sub.*not match.* stc\.'): extract_label_time_course(stc, labels_fs, src) stc.subject = None - with pytest.raises(ValueError, match=r'label\.sub.*not match.* sourc'): + with pytest.raises(ValueError, match=r"label\.sub.*not match.* sour"): extract_label_time_course(stc, labels_fs, src) diff --git a/mne/tests/test_source_space.py b/mne/tests/test_source_space.py index 364e250284a..83ad939d7ef 100644 --- a/mne/tests/test_source_space.py +++ b/mne/tests/test_source_space.py @@ -479,7 +479,7 @@ def test_setup_source_space(tmp_path): setup_source_space('sample', spacing='7emm', add_dist=False, subjects_dir=subjects_dir) with pytest.raises(ValueError, match='must be a string with values'): - setup_source_space('sample', spacing='alls', + setup_source_space("sample", spacing="ally", add_dist=False, subjects_dir=subjects_dir) # ico 5 (fsaverage) - write to temp file diff --git a/mne/transforms.py b/mne/transforms.py index 39ab647f479..1514b2ad2d3 100644 --- a/mne/transforms.py +++ b/mne/transforms.py @@ -1822,7 +1822,7 @@ def apply_volume_registration(moving, static, reg_affine, sdr_morph=None, moving.shape, moving_affine) reg_data = affine_map.transform(moving, interpolation=interpolation) if sdr_morph is not None: - logger.info('Appling SDR warp ...') + logger.info("Applying SDR warp ...") reg_data = sdr_morph.transform( reg_data, interpolation=interpolation, image_world2grid=np.linalg.inv(static_affine), diff --git a/mne/utils/tests/test_check.py b/mne/utils/tests/test_check.py index 44caa61ba10..5763649dd5d 100644 --- a/mne/utils/tests/test_check.py +++ b/mne/utils/tests/test_check.py @@ -204,7 +204,9 @@ def test_suggest(): sug = _suggest('Left-cerebellum', names) assert sug == " Did you mean 'Left-Cerebellum-Cortex'?" sug = _suggest('Cerebellum-Cortex', names) - assert sug == " Did you mean one of ['Left-Cerebellum-Cortex', 'Right-Cerebellum-Cortex', 'Left-Cerebral-Cortex']?" # noqa: E501 + assert ( + sug == " Did you mean one of ['Left-Cerebellum-Cortex', 'Right-Cerebellum-Cortex', 'Left-Cerebral-Cortex']?" # noqa: E501 + ) def test_on_missing(): diff --git a/mne/viz/backends/_notebook.py b/mne/viz/backends/_notebook.py index c239aa9e42c..187c02e23c9 100644 --- a/mne/viz/backends/_notebook.py +++ b/mne/viz/backends/_notebook.py @@ -34,8 +34,14 @@ _AbstractWidgetList, _AbstractAction, _AbstractDialog, _AbstractKeyPress) from ._pyvista import _PyVistaRenderer, Plotter -from ._pyvista import (_close_3d_figure, _check_3d_figure, _close_all, # noqa: F401,E501 analysis:ignore - _set_3d_view, _set_3d_title, _take_3d_screenshot) # noqa: F401,E501 analysis:ignore +from ._pyvista import ( + _close_3d_figure, # noqa: F401 + _check_3d_figure, # noqa: F401 + _close_all, # noqa: F401 + _set_3d_view, # noqa: F401 + _set_3d_title, # noqa: F401 + _take_3d_screenshot, # noqa: F401 +) from ._utils import _notebook_vtk_works diff --git a/mne/viz/backends/_qt.py b/mne/viz/backends/_qt.py index fa8b3b9b9be..d058a505c34 100644 --- a/mne/viz/backends/_qt.py +++ b/mne/viz/backends/_qt.py @@ -32,9 +32,15 @@ QSpinBox, QStyle, QStyleOptionSlider) from ._pyvista import _PyVistaRenderer -from ._pyvista import (_close_3d_figure, _check_3d_figure, _close_all, # noqa: F401,E501 analysis:ignore - _set_3d_view, _set_3d_title, _take_3d_screenshot, # noqa: F401,E501 analysis:ignore - _is_mesa) # noqa: F401,E501 analysis:ignore +from ._pyvista import ( + _close_3d_figure, # noqa: F401 + _check_3d_figure, # noqa: F401 + _close_all, # noqa: F401 + _set_3d_view, # noqa: F401 + _set_3d_title, # noqa: F401 + _take_3d_screenshot, # noqa: F401 + _is_mesa, # noqa: F401 +) from ._abstract import (_AbstractAppWindow, _AbstractHBoxLayout, _AbstractVBoxLayout, _AbstractGridLayout, _AbstractWidget, _AbstractCanvas, diff --git a/pyproject.toml b/pyproject.toml index b8b664ab193..8e7f703106d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,5 @@ [tool.codespell] ignore-words = "ignore_words.txt" -uri-ignore-words-list = "bu" builtin = "clear,rare,informal,names,usage" skip = "doc/references.bib" @@ -48,3 +47,6 @@ addopts = """--durations=20 --doctest-modules -ra --cov-report= --tb=short \ --ignore=mne/report/js_and_css \ --color=yes --capture=sys""" junit_family = "xunit2" + +[tool.black] +exclude = "(dist/)|(build/)|(.*\\.ipynb)" diff --git a/requirements_testing.txt b/requirements_testing.txt index fa6c7b86b3f..aad9e7ea206 100644 --- a/requirements_testing.txt +++ b/requirements_testing.txt @@ -11,3 +11,4 @@ tomli; python_version<'3.11' twine wheel pre-commit +black diff --git a/tutorials/forward/50_background_freesurfer_mne.py b/tutorials/forward/50_background_freesurfer_mne.py index a204272b57f..4d67e3e19b3 100644 --- a/tutorials/forward/50_background_freesurfer_mne.py +++ b/tutorials/forward/50_background_freesurfer_mne.py @@ -128,7 +128,7 @@ def imshow_mri(data, img, vox, xyz, suptitle): # Figure out the title based on the code of this axis ori_slice = dict(P='Coronal', A='Coronal', I='Axial', S='Axial', - L='Sagittal', R='Saggital') + L='Sagittal', R='Sagittal') ori_names = dict(P='posterior', A='anterior', I='inferior', S='superior', L='left', R='right')