-
Notifications
You must be signed in to change notification settings - Fork 575
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
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1819e24
adding keep_masked_labels param
mtorabi59 193cde1
add deprecation warning
mtorabi59 99b9867
add stacklevel to Deprecation warning
mtorabi59 8301f74
improve warning message and docstring
mtorabi59 67c7485
raise a warning when labels are removed
mtorabi59 2776eba
minor update in warning message
mtorabi59 d114478
Merge remote-tracking branch 'upstream/main' into labels_masker
mtorabi59 96b35e0
make warning shorter
mtorabi59 8f13aba
minor change
mtorabi59 9bd9e49
update doc
mtorabi59 d42332c
run black
mtorabi59 f2bfa3d
update warning message
mtorabi59 c49d328
Merge branch 'main' into labels_masker
mtorabi59 5acf199
test new warnings
mtorabi59 8eb43c8
Merge branch 'main' into labels_masker
mtorabi59 849687a
add new assertions to test_signal_extraction
mtorabi59 ec827a2
Merge branch 'labels_masker' of github.com:mtorabi59/nilearn into lab…
mtorabi59 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
""" | ||
# Author: Philippe Gervais | ||
# License: simplified BSD | ||
import warnings | ||
|
||
import numpy as np | ||
from scipy import linalg, ndimage | ||
|
@@ -102,7 +103,8 @@ def _check_shape_and_affine_compatibility(img1, img2=None, dim=None): | |
|
||
|
||
def _get_labels_data( | ||
target_img, labels_img, mask_img=None, background_label=0, dim=None | ||
target_img, labels_img, mask_img=None, background_label=0, dim=None, | ||
keep_masked_labels=True | ||
): | ||
"""Get the label data. | ||
|
||
|
@@ -133,6 +135,22 @@ def _get_labels_data( | |
dim : :obj:`int`, optional | ||
Integer slices mask for a specific dimension. | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may want to take advantage of the |
||
If True, the masked atlas with these empty labels will be retained | ||
in the output, resulting in corresponding time series containing | ||
zeros only. If False, the empty labels will be removed from the | ||
output, ensuring no empty time series are present. | ||
Default=True. | ||
|
||
.. deprecated:: 0.9.2 | ||
|
||
The 'True' option for ``keep_masked_labels`` is deprecated. | ||
The default value will change to 'False' in 0.13, | ||
and the ``keep_masked_labels`` parameter will be removed in 0.15. | ||
|
||
Returns | ||
------- | ||
labels : :obj:`list` or :obj:`tuple` | ||
|
@@ -154,17 +172,59 @@ def _get_labels_data( | |
|
||
labels_data = _safe_get_data(labels_img, ensure_finite=True) | ||
|
||
labels = list(np.unique(labels_data)) | ||
if background_label in labels: | ||
labels.remove(background_label) | ||
if keep_masked_labels: | ||
labels = list(np.unique(labels_data)) | ||
ymzayek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
warnings.warn( | ||
'Starting in version 0.15, the behavior of "NiftiLabelsMasker" ' | ||
'will change when a mask is supplied through the "mask_img" ' | ||
'parameter. Applying "mask_img" before ' | ||
'signal extraction may result in empty region signals in the ' | ||
'output. These empty region signals used to be kept. ' | ||
'In the new behavior, they will be removed from the output.' | ||
'\n' | ||
'To explicitly enable/disable the retention of empty ' | ||
'region signals, set the parameter "keep_masked_labels" ' | ||
'to True/False when ' | ||
'initializing the "NiftiLabelsMasker" object. ' | ||
'Starting from version 0.13, the default behavior will be ' | ||
'changed to "keep_masked_labels=False". ' | ||
'"keep_masked_labels" parameter will be removed ' | ||
'in version 0.15.', | ||
DeprecationWarning, | ||
stacklevel=3 | ||
) | ||
|
||
# Consider only data within the mask | ||
use_mask = _check_shape_and_affine_compatibility(target_img, mask_img, dim) | ||
if use_mask: | ||
mask_img = _utils.check_niimg_3d(mask_img) | ||
mask_data = _safe_get_data(mask_img, ensure_finite=True) | ||
labels_data = labels_data.copy() | ||
labels_before_mask = set(np.unique(labels_data)) | ||
# Applying mask on labels_data | ||
labels_data[np.logical_not(mask_data)] = background_label | ||
labels_after_mask = set(np.unique(labels_data)) | ||
labels_diff = labels_before_mask.difference( | ||
labels_after_mask | ||
) | ||
# Raising a warning if any label is removed due to the mask | ||
if len(labels_diff) > 0 and (not keep_masked_labels): | ||
warnings.warn( | ||
"After applying mask to the labels image, " | ||
"the following labels were " | ||
f"removed: {labels_diff}. " | ||
f"Out of {len(labels_before_mask)} labels, the " | ||
"masked labels image only contains " | ||
f"{len(labels_after_mask)} labels " | ||
"(including background).", | ||
stacklevel=3 | ||
) | ||
|
||
if not keep_masked_labels: | ||
labels = list(np.unique(labels_data)) | ||
|
||
if background_label in labels: | ||
labels.remove(background_label) | ||
|
||
return labels, labels_data | ||
|
||
|
@@ -206,6 +266,7 @@ def img_to_signals_labels( | |
background_label=0, | ||
order="F", | ||
strategy="mean", | ||
keep_masked_labels=True | ||
): | ||
"""Extract region signals from image. | ||
|
||
|
@@ -241,6 +302,22 @@ def img_to_signals_labels( | |
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 | ||
masked region, resulting in empty time series for those labels. | ||
If True, the masked atlas with these empty labels will be retained | ||
in the output, resulting in corresponding time series containing | ||
zeros only. If False, the empty labels will be removed from the | ||
output, ensuring no empty time series are present. | ||
Default=True. | ||
|
||
.. deprecated:: 0.9.2 | ||
|
||
The 'True' option for ``keep_masked_labels`` is deprecated. | ||
The default value will change to 'False' in 0.13, | ||
and the ``keep_masked_labels`` parameter will be removed in 0.15. | ||
|
||
Returns | ||
------- | ||
signals : :class:`numpy.ndarray` | ||
|
@@ -269,7 +346,8 @@ def img_to_signals_labels( | |
# (load one image at a time). | ||
imgs = _utils.check_niimg_4d(imgs) | ||
labels, labels_data = _get_labels_data( | ||
imgs, labels_img, mask_img, background_label | ||
imgs, labels_img, mask_img, background_label, | ||
keep_masked_labels=keep_masked_labels | ||
) | ||
|
||
data = _safe_get_data(imgs, ensure_finite=True) | ||
|
@@ -284,10 +362,11 @@ def img_to_signals_labels( | |
reduction_function(img, labels=labels_data, index=labels) | ||
) | ||
# Set to zero signals for missing labels. Workaround for Scipy behaviour | ||
missing_labels = set(labels) - set(np.unique(labels_data)) | ||
labels_index = {l: n for n, l in enumerate(labels)} | ||
for this_label in missing_labels: | ||
signals[:, labels_index[this_label]] = 0 | ||
if keep_masked_labels: | ||
missing_labels = set(labels) - set(np.unique(labels_data)) | ||
labels_index = {l: n for n, l in enumerate(labels)} | ||
for this_label in missing_labels: | ||
signals[:, labels_index[this_label]] = 0 | ||
return signals, labels | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?