Repository: mne-tools/mne-python

Issue:
mne.preprocessing.corrmap auto threshold error handling
using the threshold="auto" option in corrmap sets the lower threshold for correlation at 0.6 - if there is no correlation above that threshold, the routine dies with the not terribly informative "ValueError: x or y has zero length". Could either have a lower min threshold that's checked, or just provide feedback that there are no maps correlating above threshold. 



Discussion:
Hi, I am interested in contributing to this awesome project. Could this issue be assigned to me please?
@nugenta I tried to replicate the error using the following script but could not:

```
import os
import mne
from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs,
                               corrmap)
import numpy as np


mapping = {
    'Fc5.': 'FC5', 'Fc3.': 'FC3', 'Fc1.': 'FC1', 'Fcz.': 'FCz', 'Fc2.': 'FC2',
    'Fc4.': 'FC4', 'Fc6.': 'FC6', 'C5..': 'C5', 'C3..': 'C3', 'C1..': 'C1',
    'Cz..': 'Cz', 'C2..': 'C2', 'C4..': 'C4', 'C6..': 'C6', 'Cp5.': 'CP5',
    'Cp3.': 'CP3', 'Cp1.': 'CP1', 'Cpz.': 'CPz', 'Cp2.': 'CP2', 'Cp4.': 'CP4',
    'Cp6.': 'CP6', 'Fp1.': 'Fp1', 'Fpz.': 'Fpz', 'Fp2.': 'Fp2', 'Af7.': 'AF7',
    'Af3.': 'AF3', 'Afz.': 'AFz', 'Af4.': 'AF4', 'Af8.': 'AF8', 'F7..': 'F7',
    'F5..': 'F5', 'F3..': 'F3', 'F1..': 'F1', 'Fz..': 'Fz', 'F2..': 'F2',
    'F4..': 'F4', 'F6..': 'F6', 'F8..': 'F8', 'Ft7.': 'FT7', 'Ft8.': 'FT8',
    'T7..': 'T7', 'T8..': 'T8', 'T9..': 'T9', 'T10.': 'T10', 'Tp7.': 'TP7',
    'Tp8.': 'TP8', 'P7..': 'P7', 'P5..': 'P5', 'P3..': 'P3', 'P1..': 'P1',
    'Pz..': 'Pz', 'P2..': 'P2', 'P4..': 'P4', 'P6..': 'P6', 'P8..': 'P8',
    'Po7.': 'PO7', 'Po3.': 'PO3', 'Poz.': 'POz', 'Po4.': 'PO4', 'Po8.': 'PO8',
    'O1..': 'O1', 'Oz..': 'Oz', 'O2..': 'O2', 'Iz..': 'Iz'
}

raws = list()
icas = list()

for subj in range(4):
    # EEGBCI subjects are 1-indexed; run 3 is a left/right hand movement task
    fname = mne.datasets.eegbci.load_data(subj + 1, runs=[3])[0]
    raw = mne.io.read_raw_edf(fname)
    # remove trailing `.` from channel names so we can set montage
    raw.rename_channels(mapping)
    raw.set_montage('standard_1005')
    # fit ICA
    ica = ICA(n_components=3, random_state=5)
    ica.fit(raw)
    if subj==0:
        ica.pca_components_[:, :] = np.random.rand(64, 64)
        
    raws.append(raw)
    icas.append(ica)


corrmap(icas, template=(0, 0), threshold='auto',plot=False)

```
can you send us a minimal example to regenerate your error?
@PalAditya it looks like @fmamashli was also looking, but feel free to give it a try if you'd like
@PalAditya I could not regenerate the error after several try. I asked the person to send a code which does that. 
I can indeed run your code, but here is the error I get when I try to run this on my data. If I change “auto” to 0.5 in the initial call, corrmap runs.

Here’s the error I get:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-f0a981905c23> in <module>
      4 mne.preprocessing.corrmap([ica], ecg_temp2_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
      5 mne.preprocessing.corrmap([ica], ecg_temp3_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
----> 6 mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold="auto",label='eog',plot=False)
      7 mne.preprocessing.corrmap([ica], eog_temp2_short, ch_type="mag",threshold="auto",label='eog',plot=False)

</anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/externals/decorator.py:decorator-gen-387> in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/_logging.py in wrapper(*args, **kwargs)
     87             with use_log_level(verbose_level):
     88                 return function(*args, **kwargs)
---> 89         return function(*args, **kwargs)
     90     return FunctionMaker.create(
     91         function, 'return decfunc(%(signature)s)',

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in _find_max_corrs(all_maps, target, threshold)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/numerics.py in compute_corr(x, y)
    245     """Compute pearson correlations between a vector and a matrix."""
    246     if len(x) == 0 or len(y) == 0:
--> 247         raise ValueError('x or y has zero length')
    248     X = np.array(x, float)
    249     Y = np.array(y, float)

ValueError: x or y has zero length



Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: fmamashli <notifications@github.com>
Reply-To: mne-tools/mne-python <reply@reply.github.com>
Date: Wednesday, October 2, 2019 at 10:25 AM
To: mne-tools/mne-python <mne-python@noreply.github.com>
Cc: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>, Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)


@nugenta<https://github.com/nugenta> I tried to replicate the error using the following script but could not:

import os

import mne

from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs,

                               corrmap)

import numpy as np





mapping = {

    'Fc5.': 'FC5', 'Fc3.': 'FC3', 'Fc1.': 'FC1', 'Fcz.': 'FCz', 'Fc2.': 'FC2',

    'Fc4.': 'FC4', 'Fc6.': 'FC6', 'C5..': 'C5', 'C3..': 'C3', 'C1..': 'C1',

    'Cz..': 'Cz', 'C2..': 'C2', 'C4..': 'C4', 'C6..': 'C6', 'Cp5.': 'CP5',

    'Cp3.': 'CP3', 'Cp1.': 'CP1', 'Cpz.': 'CPz', 'Cp2.': 'CP2', 'Cp4.': 'CP4',

    'Cp6.': 'CP6', 'Fp1.': 'Fp1', 'Fpz.': 'Fpz', 'Fp2.': 'Fp2', 'Af7.': 'AF7',

    'Af3.': 'AF3', 'Afz.': 'AFz', 'Af4.': 'AF4', 'Af8.': 'AF8', 'F7..': 'F7',

    'F5..': 'F5', 'F3..': 'F3', 'F1..': 'F1', 'Fz..': 'Fz', 'F2..': 'F2',

    'F4..': 'F4', 'F6..': 'F6', 'F8..': 'F8', 'Ft7.': 'FT7', 'Ft8.': 'FT8',

    'T7..': 'T7', 'T8..': 'T8', 'T9..': 'T9', 'T10.': 'T10', 'Tp7.': 'TP7',

    'Tp8.': 'TP8', 'P7..': 'P7', 'P5..': 'P5', 'P3..': 'P3', 'P1..': 'P1',

    'Pz..': 'Pz', 'P2..': 'P2', 'P4..': 'P4', 'P6..': 'P6', 'P8..': 'P8',

    'Po7.': 'PO7', 'Po3.': 'PO3', 'Poz.': 'POz', 'Po4.': 'PO4', 'Po8.': 'PO8',

    'O1..': 'O1', 'Oz..': 'Oz', 'O2..': 'O2', 'Iz..': 'Iz'

}



raws = list()

icas = list()



for subj in range(4):

    # EEGBCI subjects are 1-indexed; run 3 is a left/right hand movement task

    fname = mne.datasets.eegbci.load_data(subj + 1, runs=[3])[0]

    raw = mne.io.read_raw_edf(fname)

    # remove trailing `.` from channel names so we can set montage

    raw.rename_channels(mapping)

    raw.set_montage('standard_1005')

    # fit ICA

    ica = ICA(n_components=3, random_state=5)

    ica.fit(raw)

    if subj==0:

        ica.pca_components_[:, :] = np.random.rand(64, 64)



    raws.append(raw)

    icas.append(ica)





corrmap(icas, template=(0, 0), threshold='auto',plot=False)



can you send us a minimal example to regenerate your error?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/mne-tools/mne-python/issues/6836?email_source=notifications&email_token=ADXW4YHIKDTQXZVJ7Q4WHDDQMSVKRA5CNFSM4I3IV6K2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAE5Z6A#issuecomment-537517304>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ADXW4YB6FUPZLNMWRWQEFV3QMSVKRANCNFSM4I3IV6KQ>.

I have also tested with threshold = 0.5. Can you check the version of your mne: mne.__version__
My guess is that your mne is not updated to the latest version.
0.18.2

Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: fmamashli <notifications@github.com>
Reply-To: mne-tools/mne-python <reply@reply.github.com>
Date: Wednesday, October 2, 2019 at 3:00 PM
To: mne-tools/mne-python <mne-python@noreply.github.com>
Cc: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>, Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)


I have also tested with threshold = 0.5. Can you check the version of your mne: mne.version
My guess is that your mne is not updated to the latest version.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/mne-tools/mne-python/issues/6836?email_source=notifications&email_token=ADXW4YCE5B3Z7F2NYA5W3QDQMTVURA5CNFSM4I3IV6K2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAF2FZY#issuecomment-537633511>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ADXW4YFNXBX4X4GFFFRDRRDQMTVURANCNFSM4I3IV6KQ>.

@nugenta based on the traceback I think that `target` must be empty/length zero, and that comes from the `template` argument. What are you passing in your use case?
The other possibility is that `all_maps = [ica.get_components().T for ica in icas]` is somehow creating empty objects but that would be weird / another error. But it's also possible!
Sorry, I think I sent you the wrong traceback. Let me find a dataset that invokes the correct one…

Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>
Date: Wednesday, October 2, 2019 at 2:49 PM
To: mne-tools/mne-python <reply@reply.github.com>
Cc: Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)

I can indeed run your code, but here is the error I get when I try to run this on my data. If I change “auto” to 0.5 in the initial call, corrmap runs.

Here’s the error I get:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-f0a981905c23> in <module>
      4 mne.preprocessing.corrmap([ica], ecg_temp2_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
      5 mne.preprocessing.corrmap([ica], ecg_temp3_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
----> 6 mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold="auto",label='eog',plot=False)
      7 mne.preprocessing.corrmap([ica], eog_temp2_short, ch_type="mag",threshold="auto",label='eog',plot=False)

</anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/externals/decorator.py:decorator-gen-387> in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/_logging.py in wrapper(*args, **kwargs)
     87             with use_log_level(verbose_level):
     88                 return function(*args, **kwargs)
---> 89         return function(*args, **kwargs)
     90     return FunctionMaker.create(
     91         function, 'return decfunc(%(signature)s)',

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in _find_max_corrs(all_maps, target, threshold)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/numerics.py in compute_corr(x, y)
    245     """Compute pearson correlations between a vector and a matrix."""
    246     if len(x) == 0 or len(y) == 0:
--> 247         raise ValueError('x or y has zero length')
    248     X = np.array(x, float)
    249     Y = np.array(y, float)

ValueError: x or y has zero length



Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: fmamashli <notifications@github.com>
Reply-To: mne-tools/mne-python <reply@reply.github.com>
Date: Wednesday, October 2, 2019 at 10:25 AM
To: mne-tools/mne-python <mne-python@noreply.github.com>
Cc: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>, Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)


@nugenta<https://github.com/nugenta> I tried to replicate the error using the following script but could not:

import os

import mne

from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs,

                               corrmap)

import numpy as np





mapping = {

    'Fc5.': 'FC5', 'Fc3.': 'FC3', 'Fc1.': 'FC1', 'Fcz.': 'FCz', 'Fc2.': 'FC2',

    'Fc4.': 'FC4', 'Fc6.': 'FC6', 'C5..': 'C5', 'C3..': 'C3', 'C1..': 'C1',

    'Cz..': 'Cz', 'C2..': 'C2', 'C4..': 'C4', 'C6..': 'C6', 'Cp5.': 'CP5',

    'Cp3.': 'CP3', 'Cp1.': 'CP1', 'Cpz.': 'CPz', 'Cp2.': 'CP2', 'Cp4.': 'CP4',

    'Cp6.': 'CP6', 'Fp1.': 'Fp1', 'Fpz.': 'Fpz', 'Fp2.': 'Fp2', 'Af7.': 'AF7',

    'Af3.': 'AF3', 'Afz.': 'AFz', 'Af4.': 'AF4', 'Af8.': 'AF8', 'F7..': 'F7',

    'F5..': 'F5', 'F3..': 'F3', 'F1..': 'F1', 'Fz..': 'Fz', 'F2..': 'F2',

    'F4..': 'F4', 'F6..': 'F6', 'F8..': 'F8', 'Ft7.': 'FT7', 'Ft8.': 'FT8',

    'T7..': 'T7', 'T8..': 'T8', 'T9..': 'T9', 'T10.': 'T10', 'Tp7.': 'TP7',

    'Tp8.': 'TP8', 'P7..': 'P7', 'P5..': 'P5', 'P3..': 'P3', 'P1..': 'P1',

    'Pz..': 'Pz', 'P2..': 'P2', 'P4..': 'P4', 'P6..': 'P6', 'P8..': 'P8',

    'Po7.': 'PO7', 'Po3.': 'PO3', 'Poz.': 'POz', 'Po4.': 'PO4', 'Po8.': 'PO8',

    'O1..': 'O1', 'Oz..': 'Oz', 'O2..': 'O2', 'Iz..': 'Iz'

}



raws = list()

icas = list()



for subj in range(4):

    # EEGBCI subjects are 1-indexed; run 3 is a left/right hand movement task

    fname = mne.datasets.eegbci.load_data(subj + 1, runs=[3])[0]

    raw = mne.io.read_raw_edf(fname)

    # remove trailing `.` from channel names so we can set montage

    raw.rename_channels(mapping)

    raw.set_montage('standard_1005')

    # fit ICA

    ica = ICA(n_components=3, random_state=5)

    ica.fit(raw)

    if subj==0:

        ica.pca_components_[:, :] = np.random.rand(64, 64)



    raws.append(raw)

    icas.append(ica)





corrmap(icas, template=(0, 0), threshold='auto',plot=False)



can you send us a minimal example to regenerate your error?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/mne-tools/mne-python/issues/6836?email_source=notifications&email_token=ADXW4YHIKDTQXZVJ7Q4WHDDQMSVKRA5CNFSM4I3IV6K2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAE5Z6A#issuecomment-537517304>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ADXW4YB6FUPZLNMWRWQEFV3QMSVKRANCNFSM4I3IV6KQ>.

Sigh. I thought I had found where in the code the issue originated from, but I agree that based on this error, it looks like it’s the template or ica maps at fault.

But this:

mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold=0.5,label='eog',plot=False)

produces

Median correlation with constructed map: 0.606
At least 1 IC detected for each subject.

And this

mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold="auto",label='eog',plot=False)

produces this:

ValueError                                Traceback (most recent call last)
<ipython-input-18-97d74457baab> in <module>
----> 1 mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold="auto",label='eog',plot=False)

</anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/externals/decorator.py:decorator-gen-387> in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/_logging.py in wrapper(*args, **kwargs)
     87             with use_log_level(verbose_level):
     88                 return function(*args, **kwargs)
---> 89         return function(*args, **kwargs)
     90     return FunctionMaker.create(
     91         function, 'return decfunc(%(signature)s)',

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in _find_max_corrs(all_maps, target, threshold)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/numerics.py in compute_corr(x, y)
    245     """Compute pearson correlations between a vector and a matrix."""
    246     if len(x) == 0 or len(y) == 0:
--> 247         raise ValueError('x or y has zero length')
    248     X = np.array(x, float)
    249     Y = np.array(y, float)

ValueError: x or y has zero length


Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>
Date: Wednesday, October 2, 2019 at 3:27 PM
To: mne-tools/mne-python <reply@reply.github.com>
Cc: Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)

Sorry, I think I sent you the wrong traceback. Let me find a dataset that invokes the correct one…

Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>
Date: Wednesday, October 2, 2019 at 2:49 PM
To: mne-tools/mne-python <reply@reply.github.com>
Cc: Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)

I can indeed run your code, but here is the error I get when I try to run this on my data. If I change “auto” to 0.5 in the initial call, corrmap runs.

Here’s the error I get:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-f0a981905c23> in <module>
      4 mne.preprocessing.corrmap([ica], ecg_temp2_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
      5 mne.preprocessing.corrmap([ica], ecg_temp3_short, ch_type="mag",threshold="auto",label='ecg',plot=False)
----> 6 mne.preprocessing.corrmap([ica], eog_temp1_short, ch_type="mag",threshold="auto",label='eog',plot=False)
      7 mne.preprocessing.corrmap([ica], eog_temp2_short, ch_type="mag",threshold="auto",label='eog',plot=False)

</anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/externals/decorator.py:decorator-gen-387> in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/_logging.py in wrapper(*args, **kwargs)
     87             with use_log_level(verbose_level):
     88                 return function(*args, **kwargs)
---> 89         return function(*args, **kwargs)
     90     return FunctionMaker.create(
     91         function, 'return decfunc(%(signature)s)',

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in corrmap(icas, template, threshold, label, ch_type, plot, show, verbose, outlines, layout, sensors, contours, cmap)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2583         nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
   2584     elif len(threshold) > 1:
-> 2585         paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
   2586         # find iteration with highest avg correlation with target
   2587         nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in _find_max_corrs(all_maps, target, threshold)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/preprocessing/ica.py in <listcomp>(.0)
   2387 def _find_max_corrs(all_maps, target, threshold):
   2388     """Compute correlations between template and target components."""
-> 2389     all_corrs = [compute_corr(target, subj.T) for subj in all_maps]
   2390     abs_corrs = [np.abs(a) for a in all_corrs]
   2391     corr_polarities = [np.sign(a) for a in all_corrs]

/anaconda3/envs/mne_current/lib/python3.6/site-packages/mne/utils/numerics.py in compute_corr(x, y)
    245     """Compute pearson correlations between a vector and a matrix."""
    246     if len(x) == 0 or len(y) == 0:
--> 247         raise ValueError('x or y has zero length')
    248     X = np.array(x, float)
    249     Y = np.array(y, float)

ValueError: x or y has zero length



Allison C. Nugent, PhD
Director, MEG Core Facility
NIMH/NIH/DHHS
10 Center Drive, MSC 1059
Building 10 Room 4N242
Bethesda, MD 20892-1059
301-451-8863
nugenta@nih.gov<mailto:nugenta@nih.gov>


From: fmamashli <notifications@github.com>
Reply-To: mne-tools/mne-python <reply@reply.github.com>
Date: Wednesday, October 2, 2019 at 10:25 AM
To: mne-tools/mne-python <mne-python@noreply.github.com>
Cc: "Nugent, Allison C. (NIH/NIMH) [E]" <nugenta@mail.nih.gov>, Mention <mention@noreply.github.com>
Subject: Re: [mne-tools/mne-python] mne.preprocessing.corrmap auto threshold error handling (#6836)


@nugenta<https://github.com/nugenta> I tried to replicate the error using the following script but could not:

import os

import mne

from mne.preprocessing import (ICA, create_eog_epochs, create_ecg_epochs,

                               corrmap)

import numpy as np





mapping = {

    'Fc5.': 'FC5', 'Fc3.': 'FC3', 'Fc1.': 'FC1', 'Fcz.': 'FCz', 'Fc2.': 'FC2',

    'Fc4.': 'FC4', 'Fc6.': 'FC6', 'C5..': 'C5', 'C3..': 'C3', 'C1..': 'C1',

    'Cz..': 'Cz', 'C2..': 'C2', 'C4..': 'C4', 'C6..': 'C6', 'Cp5.': 'CP5',

    'Cp3.': 'CP3', 'Cp1.': 'CP1', 'Cpz.': 'CPz', 'Cp2.': 'CP2', 'Cp4.': 'CP4',

    'Cp6.': 'CP6', 'Fp1.': 'Fp1', 'Fpz.': 'Fpz', 'Fp2.': 'Fp2', 'Af7.': 'AF7',

    'Af3.': 'AF3', 'Afz.': 'AFz', 'Af4.': 'AF4', 'Af8.': 'AF8', 'F7..': 'F7',

    'F5..': 'F5', 'F3..': 'F3', 'F1..': 'F1', 'Fz..': 'Fz', 'F2..': 'F2',

    'F4..': 'F4', 'F6..': 'F6', 'F8..': 'F8', 'Ft7.': 'FT7', 'Ft8.': 'FT8',

    'T7..': 'T7', 'T8..': 'T8', 'T9..': 'T9', 'T10.': 'T10', 'Tp7.': 'TP7',

    'Tp8.': 'TP8', 'P7..': 'P7', 'P5..': 'P5', 'P3..': 'P3', 'P1..': 'P1',

    'Pz..': 'Pz', 'P2..': 'P2', 'P4..': 'P4', 'P6..': 'P6', 'P8..': 'P8',

    'Po7.': 'PO7', 'Po3.': 'PO3', 'Poz.': 'POz', 'Po4.': 'PO4', 'Po8.': 'PO8',

    'O1..': 'O1', 'Oz..': 'Oz', 'O2..': 'O2', 'Iz..': 'Iz'

}



raws = list()

icas = list()



for subj in range(4):

    # EEGBCI subjects are 1-indexed; run 3 is a left/right hand movement task

    fname = mne.datasets.eegbci.load_data(subj + 1, runs=[3])[0]

    raw = mne.io.read_raw_edf(fname)

    # remove trailing `.` from channel names so we can set montage

    raw.rename_channels(mapping)

    raw.set_montage('standard_1005')

    # fit ICA

    ica = ICA(n_components=3, random_state=5)

    ica.fit(raw)

    if subj==0:

        ica.pca_components_[:, :] = np.random.rand(64, 64)



    raws.append(raw)

    icas.append(ica)





corrmap(icas, template=(0, 0), threshold='auto',plot=False)



can you send us a minimal example to regenerate your error?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/mne-tools/mne-python/issues/6836?email_source=notifications&email_token=ADXW4YHIKDTQXZVJ7Q4WHDDQMSVKRA5CNFSM4I3IV6K2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAE5Z6A#issuecomment-537517304>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ADXW4YB6FUPZLNMWRWQEFV3QMSVKRANCNFSM4I3IV6KQ>.


Resolving patch:
diff --git a/mne/preprocessing/ica.py b/mne/preprocessing/ica.py
--- a/mne/preprocessing/ica.py
+++ b/mne/preprocessing/ica.py
@@ -108,10 +108,9 @@ def _check_for_unsupported_ica_channels(picks, info, allow_ref_meg=False):
     chs = list({channel_type(info, j) for j in picks})
     check = all([ch in types for ch in chs])
     if not check:
-        raise ValueError('Invalid channel type(s) passed for ICA.\n'
-                         'Only the following channels are supported {}\n'
-                         'Following types were passed {}\n'
-                         .format(types, chs))
+        raise ValueError('Invalid channel type%s passed for ICA: %s.'
+                         'Only the following types are supported: %s'
+                         .format(_pl(chs), chs, types))
 
 
 @fill_doc
@@ -2530,8 +2529,10 @@ def corrmap(icas, template, threshold="auto", label=None, ch_type="eeg",
                          "montage. Consider interpolating bad channels before "
                          "running ICA.")
 
+    threshold_extra = ''
     if threshold == 'auto':
         threshold = np.arange(60, 95, dtype=np.float64) / 100.
+        threshold_extra = ' ("auto")'
 
     all_maps = [ica.get_components().T for ica in icas]
 
@@ -2564,36 +2565,32 @@ def corrmap(icas, template, threshold="auto", label=None, ch_type="eeg",
         template_fig.canvas.draw()
 
     # first run: use user-selected map
-    if isinstance(threshold, (int, float)):
-        if len(all_maps) == 0:
-            logger.info('No component detected using find_outliers.'
-                        ' Consider using threshold="auto"')
-            return icas
-        nt, mt, s, mx = _find_max_corrs(all_maps, target, threshold)
-    else:
-        paths = [_find_max_corrs(all_maps, target, t) for t in threshold]
-        # find iteration with highest avg correlation with target
-        nt, mt, s, mx = paths[np.argmax([path[2] for path in paths])]
+    threshold = np.atleast_1d(np.array(threshold, float)).ravel()
+    threshold_err = ('No component detected using find_outliers when '
+                     'using threshold%s %s, consider using a more lenient '
+                     'threshold' % (threshold_extra, threshold))
+    if len(all_maps) == 0:
+        raise RuntimeError(threshold_err)
+    paths = [_find_max_corrs(all_maps, target, t) for t in threshold]
+    # find iteration with highest avg correlation with target
+    new_target, _, _, _ = paths[np.argmax([path[2] for path in paths])]
 
     # second run: use output from first run
-    if isinstance(threshold, (int, float)):
-        if len(all_maps) == 0 or len(nt) == 0:
-            if threshold > 1:
-                logger.info('No component detected using find_outliers. '
-                            'Consider using threshold="auto"')
-            return icas
-        nt, mt, s, mx = _find_max_corrs(all_maps, nt, threshold)
-    elif len(threshold) > 1:
-        paths = [_find_max_corrs(all_maps, nt, t) for t in threshold]
-        # find iteration with highest avg correlation with target
-        nt, mt, s, mx = paths[np.argmax([path[1] for path in paths])]
+    if len(all_maps) == 0 or len(new_target) == 0:
+        raise RuntimeError(threshold_err)
+    paths = [_find_max_corrs(all_maps, new_target, t) for t in threshold]
+    del new_target
+    # find iteration with highest avg correlation with target
+    _, median_corr, _, max_corrs = paths[
+        np.argmax([path[1] for path in paths])]
 
     allmaps, indices, subjs, nones = [list() for _ in range(4)]
-    logger.info('Median correlation with constructed map: %0.3f' % mt)
+    logger.info('Median correlation with constructed map: %0.3f' % median_corr)
+    del median_corr
     if plot is True:
         logger.info('Displaying selected ICs per subject.')
 
-    for ii, (ica, max_corr) in enumerate(zip(icas, mx)):
+    for ii, (ica, max_corr) in enumerate(zip(icas, max_corrs)):
         if len(max_corr) > 0:
             if isinstance(max_corr[0], np.ndarray):
                 max_corr = max_corr[0]
@@ -2612,9 +2609,9 @@ def corrmap(icas, template, threshold="auto", label=None, ch_type="eeg",
     if len(nones) == 0:
         logger.info('At least 1 IC detected for each subject.')
     else:
-        logger.info('No maps selected for subject(s) ' +
-                    ', '.join([str(x) for x in nones]) +
-                    ', consider a more liberal threshold.')
+        logger.info('No maps selected for subject%s, '
+                    ', consider a more liberal threshold.'
+                    % ([str(x) for x in nones], _pl(nones)))
 
     if plot is True:
         labelled_ics = _plot_corrmap(allmaps, subjs, indices, ch_type, ica,


Test patch:
diff --git a/mne/preprocessing/tests/test_ica.py b/mne/preprocessing/tests/test_ica.py
--- a/mne/preprocessing/tests/test_ica.py
+++ b/mne/preprocessing/tests/test_ica.py
@@ -386,7 +386,9 @@ def test_ica_additional(method):
     ica3 = ica.copy()
     corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True,
             ch_type="mag")
-    corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False)
+    with pytest.raises(RuntimeError, match='No component detected'):
+        corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False,)
+    corrmap([ica, ica2], (0, 0), threshold=0.5, plot=False, show=False)
     assert (ica.labels_["blinks"] == ica2.labels_["blinks"])
     assert (0 in ica.labels_["blinks"])
     # test retrieval of component maps as arrays