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

[FIX] check data format passed to surface plotting functions #4323

Merged
merged 6 commits into from Mar 19, 2024

Conversation

Remi-Gau
Copy link
Collaborator

@Remi-Gau Remi-Gau commented Mar 18, 2024

Changes proposed in this pull request:

  • extract a function to check extension of files passed to surface plotting functions
  • make sure that surface plotting functions (except plot_img_on_surf and view_img_on_surf) throw errors when nifti files are passed
  • add appropriate tests

Copy link
Contributor

👋 @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.

Copy link

codecov bot commented Mar 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.13%. Comparing base (abb80ff) to head (b501218).
Report is 29 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4323      +/-   ##
==========================================
+ Coverage   91.85%   92.13%   +0.28%     
==========================================
  Files         144      143       -1     
  Lines       16419    16462      +43     
  Branches     3434     3452      +18     
==========================================
+ Hits        15082    15168      +86     
+ Misses        792      749      -43     
  Partials      545      545              
Flag Coverage Δ
macos-latest_3.10_test_plotting 91.93% <100.00%> (?)
macos-latest_3.11_test_plotting 91.93% <100.00%> (+0.07%) ⬆️
macos-latest_3.8_test_plotting 91.89% <100.00%> (?)
macos-latest_3.9_test_plotting 91.89% <100.00%> (?)
ubuntu-latest_3.10_test_plotting 91.93% <100.00%> (+0.07%) ⬆️
ubuntu-latest_3.11_test_plotting 91.93% <100.00%> (?)
ubuntu-latest_3.12_test_plotting 91.93% <100.00%> (?)
ubuntu-latest_3.12_test_pre 91.93% <100.00%> (?)
ubuntu-latest_3.8_test_min 68.78% <38.46%> (?)
ubuntu-latest_3.8_test_plot_min 91.59% <100.00%> (?)
ubuntu-latest_3.8_test_plotting 91.89% <100.00%> (?)
ubuntu-latest_3.9_test_plotting 91.89% <100.00%> (?)
windows-latest_3.10_test_plotting 91.90% <100.00%> (?)
windows-latest_3.11_test_plotting 91.90% <100.00%> (?)
windows-latest_3.12_test_plotting 91.90% <100.00%> (?)
windows-latest_3.8_test_plotting 91.86% <100.00%> (?)
windows-latest_3.9_test_plotting 91.87% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -879,101 +879,102 @@ def test_plot_surf_roi_colorbar_vmin_equal_across_engines(kwargs):


def test_plot_img_on_surf_hemispheres_and_orientations(img_3d_mni):
nii = img_3d_mni
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Several tests had this futile reassigning of the fixture, cleaning it up here.

@@ -1362,7 +1380,7 @@ def plot_img_on_surf(stat_map, surf_mesh='fsaverage5', mask_img=None,

Parameters
----------
stat_map : str or 3D Niimg-like object
stat_map : :obj:`str` or :class:`pathlib.Path` or 3D Niimg-like object
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tested that the code works with both string and path so updated the doc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On that note, it may be worth checking in the public API that users can also pass a Path in places where they can pass a string to a file.

I have not checked but it seems that many functions require a string and that Paths are not supported (or at least the doc string is not necessarily explicit - or I missed it).

May be done as some internal refactoring to rely more on pathlib rather than os (where relevant) - to be discussed.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we should open an issue and address that later.

Comment on lines +248 to +252
def img_3d_mni_as_file(tmp_path):
"""Return path to a random 3D Nifti1Image in MNI space saved to disk."""
filename = tmp_path / "img.nii"
nb.save(_img_3d_mni(), filename)
return filename
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added in this as I needed it but probably reusable in other tests.

Comment on lines -16 to +18
def _get_img():
@pytest.fixture(scope="session")
def mni152_template_res_2():
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

refactor this as fixture to help reusing it

Comment on lines 246 to 248
def test_view_img_on_surf_errors(img_3d_mni):
with pytest.raises(DimensionError):
view_img_on_surf([img_3d_mni, img_3d_mni])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

extract errors in a separate test

Comment on lines +241 to +243
def test_view_img_on_surf_input_as_file(img_3d_mni_as_file):
view_img_on_surf(img_3d_mni_as_file)
view_img_on_surf(str(img_3d_mni_as_file))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

check that files are accepted as string or paths

img = _get_img()
surfaces = datasets.fetch_surf_fsaverage()
html = html_surface.view_img_on_surf(img, threshold='92.3%')
def test_view_img_on_surf(mni152_template_res_2):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry for the horrible diff on this part but this test was pretty unreadable

@Remi-Gau Remi-Gau marked this pull request as ready for review March 18, 2024 19:20
@Remi-Gau Remi-Gau changed the title [WIP][FIX] check data format passed to surface plotting functions [WIP] check data format passed to surface plotting functions Mar 18, 2024
@Remi-Gau Remi-Gau changed the title [WIP] check data format passed to surface plotting functions [FIX] check data format passed to surface plotting functions Mar 18, 2024
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.

Did not spot any issue. Thx.

@@ -1362,7 +1380,7 @@ def plot_img_on_surf(stat_map, surf_mesh='fsaverage5', mask_img=None,

Parameters
----------
stat_map : str or 3D Niimg-like object
stat_map : :obj:`str` or :class:`pathlib.Path` or 3D Niimg-like object
Copy link
Member

Choose a reason for hiding this comment

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

Yes, we should open an issue and address that later.

@Remi-Gau Remi-Gau requested a review from man-shu March 18, 2024 22:57
@Remi-Gau Remi-Gau merged commit 4691c77 into nilearn:main Mar 19, 2024
33 of 34 checks passed
@Remi-Gau Remi-Gau deleted the fix/4312 branch March 19, 2024 13:00
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.

passing nifti to surface plotting functions fails
3 participants