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

Adding keep_masked_labels parameter to Labels_masker #3722

Merged
merged 17 commits into from Jul 4, 2023

Conversation

mtorabi59
Copy link
Contributor

Addresses #3085 .

Changes proposed in this pull request:

  • Adding keep_masked_labels parameter to img_to_signals_labels, and NiftiLabelsMasker
  • if keep_masked_labels=True, which is the default, it will maintain the old behavior. ow, it will remove the labels, or regions, that were masked and became empty by mask_img.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 25, 2023

👋 @mtorabi59 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 25, 2023

Codecov Report

Merging #3722 (ec827a2) into main (a03175b) will decrease coverage by 0.13%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #3722      +/-   ##
==========================================
- Coverage   91.65%   91.53%   -0.13%     
==========================================
  Files         139      133       -6     
  Lines       16562    15575     -987     
  Branches     3229     3233       +4     
==========================================
- Hits        15180    14256     -924     
+ Misses       1379      772     -607     
- Partials        3      547     +544     
Flag Coverage Δ
macos-latest_3.10 91.45% <100.00%> (?)
macos-latest_3.11 91.45% <100.00%> (?)
macos-latest_3.8 91.41% <100.00%> (?)
macos-latest_3.9 91.41% <100.00%> (?)
ubuntu-latest_3.10 91.45% <100.00%> (?)
ubuntu-latest_3.11 91.45% <100.00%> (?)
ubuntu-latest_3.8 91.41% <100.00%> (?)
ubuntu-latest_3.9 91.41% <100.00%> (?)
windows-latest_3.10 91.39% <100.00%> (?)
windows-latest_3.11 91.39% <100.00%> (?)
windows-latest_3.8 91.36% <100.00%> (?)
windows-latest_3.9 91.36% <100.00%> (?)

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

Impacted Files Coverage Δ
nilearn/_utils/docs.py 92.12% <100.00%> (-1.53%) ⬇️
nilearn/maskers/nifti_labels_masker.py 89.94% <100.00%> (-3.85%) ⬇️
nilearn/regions/signal_extraction.py 100.00% <100.00%> (ø)

... and 87 files with indirect coverage changes

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

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.

Looks good so far!

nilearn/regions/signal_extraction.py Show resolved Hide resolved
@mtorabi59
Copy link
Contributor Author

we also need to change the behavior of the inverse transform. Should we do that after the deprecation cycle of the current change or we should do that in this cycle too? I mean for signals_to_img_labels. The same things that we discussed about empty regions also applies to signals_to_img_labels, and signals_to_img_maps.

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

@jeromedockes jeromedockes left a comment

Choose a reason for hiding this comment

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

easier correspondance between the region names / labels and the masker output will be handled in another pr?

@@ -89,6 +90,22 @@ class NiftiLabelsMasker(BaseMasker, _utils.CacheMixin):
Must be one of: sum, mean, median, minimum, maximum, variance,
standard_deviation. Default='mean'.

keep_masked_labels : :obj:`bool`, optional
When a mask is supplied through the "mask_img" parameter, some
labels in the atlas may not have any brain coverage within the
Copy link
Member

Choose a reason for hiding this comment

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

I think we can still improve the docstring a little bit. maybe instead of "may not have any brain coverage within the masked region" we could say "some atlas regions may lie entirely outside of the brain mask"?

keep_masked_labels : :obj:`bool`, optional
When a mask is supplied through the "mask_img" parameter, some
labels in the atlas may not have any brain coverage within the
masked region, resulting in empty time series for those labels.
Copy link
Member

Choose a reason for hiding this comment

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

you may want to take advantage of the _utils.fill_doc mechanism to avoid repeating this docstring

@mtorabi59
Copy link
Contributor Author

easier correspondance between the region names / labels and the masker output will be handled in another pr?

Yes, I will work on that in another PR. Does that sound good?

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.

@mtorabi59 nice work! I was wondering if there is any reason some files were reformatted (perhaps with a tool like black)? If they are not formatted by us then we don't expect users to format them because it leads to large diffs making it harder to review relevant changes. If it has to do with flake8 failing for new code it's preferable to fix it manually.

nilearn/_utils/docs.py Show resolved Hide resolved
nilearn/_utils/docs.py Outdated Show resolved Hide resolved
nilearn/regions/signal_extraction.py Outdated Show resolved Hide resolved
@mtorabi59
Copy link
Contributor Author

@mtorabi59 nice work! I was wondering if there is any reason some files were reformatted (perhaps with a tool like black)? If they are not formatted by us then we don't expect users to format them because it leads to large diffs making it harder to review relevant changes. If it has to do with flake8 failing for new code it's preferable to fix it manually.

@ymzayek yes it's because I used black since it was failing every time. But you are right. I will discard the changes and reformat only my part.

@Remi-Gau
Copy link
Collaborator

According to the black config on your branch the following should not be touched by black:

  • nilearn/maskers/*

https://github.com/mtorabi59/nilearn/blob/8f2514e3d708657d456b54df5e04921cb5979f1a/pyproject.toml#L105

@Remi-Gau
Copy link
Collaborator

Remi-Gau commented Jun 14, 2023

my suggestion to fix: reset to before you fixed things with black and only run black on the files that need it (according to this last failed CI: https://github.com/nilearn/nilearn/actions/runs/5094502417/jobs/9158397158#step:5:12)

git reset --hard 9bd9e49 
black nilearn/regions/signal_extraction.py
black nilearn/regions/tests/test_signal_extraction.py
git add nilearn/regions
git commit -m 'run black'

you will have to force push after that

@Remi-Gau
Copy link
Collaborator

To make your life easier, you may want to install pre-commit so that you will only commit properly formatted code: https://nilearn.github.io/stable/development.html#pre-commit

pre-commit will only fix files that have been staged so it should avoid fixing files you never touched.

If I have done my job well pre-commit should also ignore files we have not reformated yet even you staged them.

@mtorabi59
Copy link
Contributor Author

Thnx a lot @Remi-Gau !!

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.

@mtorabi59 thanks for making the changes! I think what is left is to add tests to make sure the 2 new warnings are raised. You could add with pytest.warns to the test you already wrote and do another where keep_masked_labels=True

nilearn/regions/tests/test_signal_extraction.py Outdated Show resolved Hide resolved
nilearn/regions/tests/test_signal_extraction.py Outdated Show resolved Hide resolved
nilearn/regions/tests/test_signal_extraction.py Outdated Show resolved Hide resolved
nilearn/regions/tests/test_signal_extraction.py Outdated Show resolved Hide resolved
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 @mtorabi59 !

Copy link
Collaborator

@Remi-Gau Remi-Gau 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 @mtorabi59

@mtorabi59
Copy link
Contributor Author

Thank you for your help @ymzayek @Remi-Gau !!

Copy link
Member

@jeromedockes jeromedockes left a comment

Choose a reason for hiding this comment

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

thanks @mtorabi59 !

@ymzayek ymzayek merged commit 8ee74d3 into nilearn:main Jul 4, 2023
28 checks passed
@mtorabi59 mtorabi59 deleted the labels_masker branch July 4, 2023 17:58
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

5 participants