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] Format nilearn/plotting: apply isort and fix flake8 errors #3648

Merged
merged 38 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
19da0cf
apply isort
Remi-Gau Apr 3, 2023
394ed23
apply isort
Remi-Gau Apr 3, 2023
6fde794
apply pyupgrade
Remi-Gau Apr 3, 2023
4abf045
flake8 fix
Remi-Gau Apr 3, 2023
efa4530
[DATALAD] Recorded changes
Remi-Gau Apr 3, 2023
a74ca9f
[DATALAD] Recorded changes
Remi-Gau Apr 3, 2023
77c9873
[DATALAD] Recorded changes
Remi-Gau Apr 3, 2023
9f8af57
fix typos
Remi-Gau Apr 4, 2023
278d047
fix fixtures
Remi-Gau Apr 4, 2023
e6e6055
rename fixture
Remi-Gau Apr 4, 2023
44c32f2
Merge remote-tracking branch 'upstream/main' into maint-format_plotting
Remi-Gau Apr 5, 2023
4057849
Merge branch 'main' into maint-format_plotting
Remi-Gau Apr 5, 2023
4335097
Update nilearn/plotting/html_stat_map.py
Remi-Gau Apr 5, 2023
421dc8a
Update nilearn/plotting/img_plotting.py
Remi-Gau Apr 5, 2023
593ee9f
Update nilearn/plotting/surf_plotting.py
Remi-Gau Apr 5, 2023
26583aa
Update nilearn/plotting/img_plotting.py
Remi-Gau Apr 5, 2023
782965b
[DATALAD] Recorded changes
Remi-Gau Apr 5, 2023
3ad4813
[DATALAD] Recorded changes
Remi-Gau Apr 5, 2023
aeb5666
Merge branch 'main' into maint-format_plotting
Remi-Gau Apr 6, 2023
8ef50ad
lint
Remi-Gau Apr 6, 2023
3324d6f
fix test
Remi-Gau Apr 6, 2023
7e192b0
fix tests
Remi-Gau Apr 6, 2023
a36e40b
fix isort
Remi-Gau Apr 7, 2023
a2e84a8
fix catch
Remi-Gau Apr 7, 2023
5e77731
Merge remote-tracking branch 'upstream/main' into maint-format_plotting
Remi-Gau Apr 13, 2023
a907e3f
move fixtures
Remi-Gau Apr 13, 2023
878696d
revert cfg change
Remi-Gau Apr 13, 2023
f748b3a
isort
Remi-Gau Apr 13, 2023
41a7097
Merge remote-tracking branch 'upstream/main' into maint-format_plotting
Remi-Gau Apr 18, 2023
bd97bff
Merge branch 'main' into maint-format_plotting
Remi-Gau Apr 18, 2023
7e2fd12
isort
Remi-Gau Apr 18, 2023
b78f45e
rm old imports
Remi-Gau Apr 18, 2023
3b993df
Merge remote-tracking branch 'upstream/main' into maint-format_plotting
Remi-Gau Apr 21, 2023
d0f3755
merge lines
Remi-Gau Apr 24, 2023
af97112
Merge remote-tracking branch 'upstream/main' into maint-format_plotting
Remi-Gau Apr 24, 2023
fd63139
try skipping fixture import if matplotlib is missing
Remi-Gau Apr 24, 2023
b94cbec
keep all fixtures in conftest.py
Remi-Gau Apr 25, 2023
cf9d5ee
stringify parameters in error message
Remi-Gau Apr 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ max_function_length = 150
# W504: line break after binary operator
per-file-ignores =
*/__init__.py: D104
*/*/__init__.py: D104
# - docstrings rules that should not be applied to tests
*/tests/*: D100, D101, D102, D103, D104, D205, D400, D401
*/*/tests/*: D100, D101, D102, D103, D104, D205, D400, D401
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/isort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
nilearn/input_data \
nilearn/maskers \
nilearn/mass_univariate \
nilearn/plotting \
nilearn/regions \
nilearn/rerporting \
nilearn/surface \
Expand Down
19 changes: 19 additions & 0 deletions nilearn/conftest.py
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of thoughts:

  • The new plotting fixtures seem to be specific to the plotting module while the other fixtures are a bit more general, perhaps they can stay in the plotting module
  • Can any "create fake data" function from _utils.data_gen be reused to create these fixtures? Or we can specify new functions in data_gen and then specify the fixtures here (or under plotting) if we see that they can be useful in other modules/fixtures

These are maybe for another PR but I also think that moving the fixtures would be better done under a refactoring PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW to add locations of fixtures for pytest to find you can add the following code to conftest.py:

pytest_plugins = [
    "nilearn.plotting.tests.test_img_plotting.testing_utils",
]

And then change the names of the fixtures and then you don't have to undo the changes in the files that use those fixtures

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only moved the fixtures. I started seeing ways to improve them but refactoring that is beyond the scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ymzayek
are you reading this failure of the tests on python 3.7 without matplotlib the same way I am?

+ python -m pytest --pyargs nilearn --cov=nilearn
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.7.16/x64/lib/python3.7/site-packages/_pytest/config/__init__.py", line 774, in import_plugin
    __import__(importspec)
  File "/home/runner/work/nilearn/nilearn/nilearn/plotting/__init__.py", line 53, in <module>
    _set_mpl_backend()
  File "/home/runner/work/nilearn/nilearn/nilearn/plotting/__init__.py", line 14, in _set_mpl_backend
    import matplotlib
ModuleNotFoundError: No module named 'matplotlib'

see the rest here:

https://github.com/nilearn/nilearn/actions/runs/4787578339/jobs/8512995221?pr=3648#step:7:92

It seems that when trying to use the plugin fixtures that we have now moved to be specific to the plotting we now get an matplotlib import error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Remi-Gau regarding the last few lines of the trace back of that link:

  File "/home/runner/work/nilearn/nilearn/nilearn/plotting/__init__.py", line 53, in <module>
    _set_mpl_backend()
  File "/home/runner/work/nilearn/nilearn/nilearn/plotting/__init__.py", line 14, in _set_mpl_backend
    import matplotlib
ImportError: Error importing plugin "nilearn.plotting.tests.test_img_plotting._utils": No module named 'matplotlib'

What was in nilearn.plotting.tests.test_img_plotting._utils exactly?

Maybe there was an import of matplotlib that wasn't being flagged

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing you can try is to keep the name testing_utils.py. I'm wondering if it was named as such because of something specific to how pytest handles filenames. Just making guesses here. Feel free to transfer this discussion to a new issue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing you can try is to keep the name testing_utils.py.

that did not work either

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the nature of the fixtures that were moved in conftest.py I think that they can be renamed, refactored to be more reusable in other tests.

This will hopefully become more obvious as we keep refactoring the test suite in general.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

collect_ignore = ["datasets/data/convert_templates.py"]

pytest_plugins = [
"nilearn.plotting.tests.test_img_plotting._utils",
]

try:
import matplotlib # noqa: F401
Expand Down Expand Up @@ -75,3 +78,19 @@ def close_all():
import matplotlib.pyplot as plt

plt.close("all") # takes < 1 us so just always do it


MNI_AFFINE = np.array(
[
[-2.0, 0.0, 0.0, 90.0],
[0.0, 2.0, 0.0, -126.0],
[0.0, 0.0, 2.0, -72.0],
[0.0, 0.0, 0.0, 1.0],
]
)


@pytest.fixture()
def mni_affine():
"""Return an affine corresponding to 2mm isotropic MNI template."""
return MNI_AFFINE
1 change: 1 addition & 0 deletions nilearn/glm/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Authors: Bertrand Thirion, Matthew Brett, Ana Luisa Pinho, 2020
"""

import csv
from warnings import warn

Expand Down
55 changes: 39 additions & 16 deletions nilearn/plotting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""
Plotting code for nilearn
"""
"""Plotting code for nilearn."""
# Original Authors: Chris Filo Gorgolewski, Gael Varoquaux
import importlib
import warnings


OPTIONAL_MATPLOTLIB_MIN_VERSION = "3.3.0"

###############################################################################
Expand All @@ -18,11 +15,14 @@ def _set_mpl_backend():
except ImportError:
if importlib.util.find_spec("pytest") is not None:
from .._utils.testing import skip_if_running_tests

# No need to fail when running tests
skip_if_running_tests('matplotlib not installed')
raise
else:

from .._utils import _compare_version

# When matplotlib was successfully imported we need to check
# that the version is greater that the minimum required one
mpl_version = getattr(matplotlib, "__version__", "0.0.0")
Expand Down Expand Up @@ -54,19 +54,42 @@ def _set_mpl_backend():

###############################################################################
from . import cm
from .img_plotting import (
plot_img, plot_anat, plot_epi, plot_roi, plot_stat_map,
plot_glass_brain, plot_connectome, plot_markers, plot_prob_atlas,
plot_carpet, plot_img_comparison, show)
from .find_cuts import find_xyz_cut_coords, find_cut_slices, \
find_parcellation_cut_coords, find_probabilistic_atlas_cut_coords
from .matrix_plotting import (plot_matrix, plot_contrast_matrix,
plot_design_matrix, plot_event)
from .html_surface import view_surf, view_img_on_surf
from .html_stat_map import view_img
from .find_cuts import (
find_cut_slices,
find_parcellation_cut_coords,
find_probabilistic_atlas_cut_coords,
find_xyz_cut_coords,
)
from .html_connectome import view_connectome, view_markers
from .surf_plotting import (plot_surf, plot_surf_stat_map, plot_surf_roi,
plot_img_on_surf, plot_surf_contours)
from .html_stat_map import view_img
from .html_surface import view_img_on_surf, view_surf
from .img_plotting import (
plot_anat,
plot_carpet,
plot_connectome,
plot_epi,
plot_glass_brain,
plot_img,
plot_img_comparison,
plot_markers,
plot_prob_atlas,
plot_roi,
plot_stat_map,
show,
)
from .matrix_plotting import (
plot_contrast_matrix,
plot_design_matrix,
plot_event,
plot_matrix,
)
from .surf_plotting import (
plot_img_on_surf,
plot_surf,
plot_surf_contours,
plot_surf_roi,
plot_surf_stat_map,
)

__all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',
'plot_roi', 'plot_stat_map', 'plot_glass_brain',
Expand Down