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

Conversation

Remi-Gau
Copy link
Collaborator

@Remi-Gau Remi-Gau commented Apr 3, 2023

Relates to #2528

Changes proposed in this pull request:

  • run isort
  • run pyupgrade
  • run flynt
  • fix remaining flake8 issues
  • update flake8 / isort CI

@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2023

👋 @Remi-Gau Thanks for creating a PR!

Until this PR is ready for review, you can include the [WIP] tag in its title, or leave it as a github draft.

Please make sure it is compliant with our contributing guidelines. In particular, be sure it checks the boxes listed below.

  • PR has an interpretable title.
  • PR links to Github issue with mention Closes #XXXX (see our documentation on PR structure)
  • Code is PEP8-compliant (see our documentation on coding style)
  • Changelog or what's new entry in doc/changes/latest.rst (see our documentation on PR structure)

For new features:

  • There is at least one unit test per new function / class (see our documentation on testing)
  • The new feature is demoed in at least one relevant example.

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

We will review it as quick as possible, feel free to ping us with questions if needed.

@codecov
Copy link

codecov bot commented Apr 3, 2023

Codecov Report

Merging #3648 (cf9d5ee) into main (51a651c) will decrease coverage by 0.01%.
The diff coverage is 92.55%.

@@            Coverage Diff             @@
##             main    #3648      +/-   ##
==========================================
- Coverage   91.57%   91.56%   -0.01%     
==========================================
  Files         139      139              
  Lines       16576    16566      -10     
  Branches     3233     3233              
==========================================
- Hits        15179    15169      -10     
  Misses       1394     1394              
  Partials        3        3              
Impacted Files Coverage Δ
nilearn/glm/_utils.py 93.16% <ø> (ø)
nilearn/plotting/displays/_figures.py 81.25% <ø> (ø)
...lotting/glass_brain_files/svg_to_json_converter.py 19.11% <50.00%> (ø)
nilearn/plotting/rm_file.py 80.00% <50.00%> (ø)
...learn/plotting/glass_brain_files/plot_align_svg.py 13.79% <66.66%> (ø)
nilearn/plotting/cm.py 90.90% <77.77%> (-0.19%) ⬇️
nilearn/plotting/find_cuts.py 91.37% <83.33%> (ø)
nilearn/plotting/surf_plotting.py 97.16% <83.33%> (-0.03%) ⬇️
nilearn/plotting/glass_brain.py 85.89% <85.71%> (-0.36%) ⬇️
nilearn/plotting/img_plotting.py 94.52% <95.23%> (ø)
... and 12 more

... and 9 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Remi-Gau Remi-Gau added this to In progress in R&R (reformat and refactor) Apr 4, 2023
Copy link
Member

@ymzayek ymzayek left a comment

Choose a reason for hiding this comment

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

Minor comments. I haven't reviewed the tests yet.

nilearn/plotting/cm.py Outdated Show resolved Hide resolved
nilearn/plotting/cm.py Show resolved Hide resolved
nilearn/plotting/cm.py Outdated Show resolved Hide resolved
nilearn/plotting/cm.py Show resolved Hide resolved
nilearn/plotting/html_stat_map.py Outdated Show resolved Hide resolved
nilearn/plotting/img_plotting.py Outdated Show resolved Hide resolved
nilearn/plotting/surf_plotting.py Outdated Show resolved Hide resolved
Remi-Gau and others added 11 commits April 5, 2023 17:56
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
@Remi-Gau Remi-Gau marked this pull request as draft April 7, 2023 11:13
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.

@Remi-Gau Remi-Gau marked this pull request as ready for review April 13, 2023 12:23
Copy link
Member

@ymzayek ymzayek left a comment

Choose a reason for hiding this comment

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

Thx!

nilearn/plotting/tests/test_displays.py Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

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

LGTM, thx !

Copy link
Member

@NicolasGensollen NicolasGensollen left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @Remi-Gau !

nilearn/plotting/displays/_slicers.py Outdated Show resolved Hide resolved
@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Apr 24, 2023

TODO

  • fix failing test on 3.7
  • there seems to be some flaky tests: confirm and fix if needed

"img_mask": img_mask,
"img_atlas": img_atlas,
"atlas_labels": atlas_labels,
}
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
Only I have found to make the test without matplotlib pass, is to keep those fixture outside of the nilearn plotting package.

I suspect this could be improved / fixed but for now I would suggest to keep those here and address the location of those fixtures when refactoring the tests for plotting.

@Remi-Gau
Copy link
Collaborator Author

There definitely is a flaky test:

FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_tri_error[None] - AssertionError: Regex pattern did not match.
 Regex: "Parameter tri needs to be one of {'full', 'lower', 'diag'}"
 Input: "Parameter tri needs to be one of {'lower', 'full', 'diag'}."
FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_tri_error[foo] - AssertionError: Regex pattern did not match.
 Regex: "Parameter tri needs to be one of {'full', 'lower', 'diag'}"
 Input: "Parameter tri needs to be one of {'lower', 'full', 'diag'}."
FAILED nilearn/plotting/tests/test_matrix_plotting.py::test_sanitize_tri_error[2] - AssertionError: Regex pattern did not match.
 Regex: "Parameter tri needs to be one of {'full', 'lower', 'diag'}"
 Input: "Parameter tri needs to be one of {'lower', 'full', 'diag'}."

@Remi-Gau
Copy link
Collaborator Author

This will be merged after the next release #3724

@Remi-Gau Remi-Gau merged commit 1158167 into nilearn:main Apr 28, 2023
29 checks passed
@Remi-Gau Remi-Gau deleted the maint-format_plotting branch April 28, 2023 20:01
@Remi-Gau Remi-Gau moved this from In progress to Done in R&R (reformat and refactor) Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants