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

[BUG] NiftiLabelsMasker missing signal #4113

Open
3 of 9 tasks
andreifoldes opened this issue Nov 17, 2023 · 9 comments
Open
3 of 9 tasks

[BUG] NiftiLabelsMasker missing signal #4113

andreifoldes opened this issue Nov 17, 2023 · 9 comments
Labels
Bug for bug reports Masker

Comments

@andreifoldes
Copy link

andreifoldes commented Nov 17, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Operating system

  • Linux
  • Mac
  • Windows

Operating system version

  • Windows 10

Python version

  • 3.12
  • 3.11
  • 3.10
  • 3.9
  • 3.8

nilearn version

0.10.2

Expected behavior

I have a mask with certified 8 non-zero values and would expect 8 numbers from NiLabelsMasker, that is indeed the case for all but one of my participants:

 right_subfield_mask = f"xxx/hippunfold/sub-{subject_label}/anat/sub-{subject_label}_hemi-R_space-T1w_desc-subfields_atlas-bigbrain_dseg.nii.gz"
    
    tmp = nib.load(right_subfield_mask)
    tmp = tmp.get_fdata()
    # get unique values nonzero values
    unique_values = np.unique(tmp[tmp != 0])
    print(unique_values)
    # print unique values    
    if len(unique_values) != 8:
        # raise an actual warning using warnings.warn
        # print unique values
        print(unique_values)
        warnings.warn("WARNING: unique values in right_subfield_mask is not equal to 8")
    
    right_subfield_labels = [ label + "_right" for label in subfield_labels]
    right_subfield_labels = [right_subfield_labels[i-1] for i in unique_values.astype(int)]

    # Instantiate the masker with label image and label values
    masker = NiftiLabelsMasker(
        right_subfield_mask,
        labels=right_subfield_labels,
        standardize="zscore_sample",
    )

    right_subfield_signals = masker.fit_transform(betamap)
    print(right_subfield_signals)

Current behavior & error messages

This is what I got:

8 non-zero values go in, 7 values come out. No warning or error message...

>> right_subfield_mask = f"/xxx/hippunfold_v2/hippunfold/sub-{subject_label}/anat/sub-{subject_label}_hemi-R_space-T1w_desc-subfields_atlas-bigbrain_dseg.nii.gz"
>>> tmp = nib.load(right_subfield_mask)
p.get_fdata()
# get unique values nonzero values

>>> tmp = tmp.get_fdata()
unique_values = np.unique(tmp[tmp != 0])
print(unique_values)
# print unique values

if len(unique_values) != 8:
    # raise an actual warning using warnings.warn
    # print unique values
    print(unique_values)
    warnings.warn("WARNING: unique values in right_subfield_mask is not equal to 8")

right_subfield_labels = [ label + "_right" for label in subfield_labels]
right_subfield_labels = [right_subfield_labels[i-1] for i in unique_values.astype(int)]
# Instantiate the masker with label image and label values

masker = NiftiLabelsMasker(
    right_subfield_mask,
    labels=right_subfield_labels,
    standardize="zscore_sample",
)

right_subfield_signals = masker.fit_transform(betamap)
print(right_subfield_signals)
>>> # get unique values nonzero values
>>>
>>> unique_values = np.unique(tmp[tmp != 0])
>>> print(unique_values)
**[1. 2. 3. 4. 5. 6. 7. 8.]**
>>> # print unique values
>>>
>>> if len(unique_values) != 8:
...     # raise an actual warning using warnings.warn
...     # print unique values
...     print(unique_values)
...     warnings.warn("WARNING: unique values in right_subfield_mask is not equal to 8")
...
>>> right_subfield_labels = [ label + "_right" for label in subfield_labels]
>>> right_subfield_labels = [right_subfield_labels[i-1] for i in unique_values.astype(int)]
>>> # Instantiate the masker with label image and label values
>>>
>>> masker = NiftiLabelsMasker(
...     right_subfield_mask,
...     labels=right_subfield_labels,
...     standardize="zscore_sample",
... )
>>>
>>> right_subfield_signals = masker.fit_transform(betamap)
>>> print(right_subfield_signals)
**[[-0.52435863 -0.40006825 -0.34511584 -0.5896911  -0.44941693 -0.23059142
  -0.57147044]]**

Steps and code to reproduce bug

No response

@andreifoldes andreifoldes added the Bug for bug reports label Nov 17, 2023
@bthirion
Copy link
Member

Thx for reporting.
Can you provide the mask and betamap in some way ?

@ymzayek
Copy link
Member

ymzayek commented Nov 17, 2023

@andreifoldes I've seen this happen before due to resampling. The labels masker by default resamples the labels_img to the data and this could result in small regions getting wiped out. See resampling_target paramter of NiftiLabelsMasker. We could add a warning when this happens. WDYT @bthirion ?

@bthirion
Copy link
Member

Isn't it addressed by #3761

@ymzayek
Copy link
Member

ymzayek commented Nov 17, 2023

No explicitly but yes. These new returns will help the user map the output back to the input

@andreifoldes
Copy link
Author

Yes, nice catch, that's probably it! A warning raised would be nice!

@andreifoldes
Copy link
Author

andreifoldes commented Nov 17, 2023

Is sending the files still necessary?

Actually upon second thought maybe its something else because but files are in the native space so resampling shouldn't be doing much of anything :)

@bthirion
Copy link
Member

If you still don't understand what's going on, it's a goo idea to share the files. Best,

@ymzayek
Copy link
Member

ymzayek commented Nov 21, 2023

@andreifoldes actually a warning should be raised, see

warnings.warn("After resampling the label image to the "
"data image, the following labels were "
f"removed: {labels_diff}. "
"Label image only contains "
f"{len(labels_after_resampling)} labels "
"(including background).")

Can you double check if that is raised?

If that's not the case then sending the files or providing a minimal reproducible example to raise the same error might be necessary

@andreifoldes
Copy link
Author

andreifoldes commented Nov 27, 2023

Yes, thank you, will check this week and also send data!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug for bug reports Masker
Projects
None yet
Development

No branches or pull requests

4 participants