mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.66k stars 1.31k forks source link

TypeError: ch_names must be a list, tuple, or int #5594

Closed Rockingsnow closed 5 years ago

Rockingsnow commented 5 years ago

When I run the sample code in Python 2.7,the error happened as follow:

import numpy as np
import matplotlib.pyplot as plt

from sklearn.pipeline import Pipeline
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import ShuffleSplit, cross_val_score

from mne import Epochs, pick_types, find_events
from mne.channels import read_layout
from mne.io import concatenate_raws, read_raw_edf
from mne.datasets import eegbci
from mne.decoding import CSP

print(__doc__)

tmin, tmax = -1., 4.
event_id = dict(hands=2, feet=3)
subject = 1
runs = [6, 10, 14]  # motor imagery: hands vs feet

raw_fnames = eegbci.load_data(subject, runs)
raw_files = [read_raw_edf(f, preload=True, stim_channel='auto') for f in
             raw_fnames]
raw = concatenate_raws(raw_files)

# strip channel names of "." characters
raw.rename_channels(lambda x: x.strip('.'))

# Apply band-pass filter
raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')

events = find_events(raw, shortest_event=0, stim_channel='STI 014')

picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                   exclude='bads')

epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                baseline=None, preload=True)
epochs_train = epochs.copy().crop(tmin=1., tmax=2.)
labels = epochs.events[:, -1] - 2

scores = []
epochs_data = epochs.get_data()
epochs_data_train = epochs_train.get_data()
cv = ShuffleSplit(10, test_size=0.2, random_state=42)
cv_split = cv.split(epochs_data_train)

lda = LinearDiscriminantAnalysis()
csp = CSP(n_components=4, reg=None, log=True, norm_trace=False)

clf = Pipeline([('CSP', csp), ('LDA', lda)])
scores = cross_val_score(clf, epochs_data_train, labels, cv=cv, n_jobs=1)

class_balance = np.mean(labels == labels[0])
class_balance = max(class_balance, 1. - class_balance)
print("Classification accuracy: %f / Chance level: %f" % (np.mean(scores),
                                                          class_balance))
Automatically created module for IPython interactive environment
Extracting EDF parameters from C:\Users\user\mne_data\MNE-eegbci-data\physiobank\database\eegmmidb\S001\S001R06.edf...
EDF file detected
EDF annotations detected (consider using raw.find_edf_events() to extract them)
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Extracting EDF parameters from C:\Users\user\mne_data\MNE-eegbci-data\physiobank\database\eegmmidb\S001\S001R10.edf...
EDF file detected
EDF annotations detected (consider using raw.find_edf_events() to extract them)
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Extracting EDF parameters from C:\Users\user\mne_data\MNE-eegbci-data\physiobank\database\eegmmidb\S001\S001R14.edf...
EDF file detected
EDF annotations detected (consider using raw.find_edf_events() to extract them)
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Setting up band-pass filter from 7 - 30 Hz
l_trans_bandwidth chosen to be 2.0 Hz
h_trans_bandwidth chosen to be 7.5 Hz
Filter length of 265 samples (1.656 sec) selected
Setting up band-pass filter from 7 - 30 Hz
l_trans_bandwidth chosen to be 2.0 Hz
h_trans_bandwidth chosen to be 7.5 Hz
Filter length of 265 samples (1.656 sec) selected
Setting up band-pass filter from 7 - 30 Hz
l_trans_bandwidth chosen to be 2.0 Hz
h_trans_bandwidth chosen to be 7.5 Hz
Filter length of 265 samples (1.656 sec) selected
Trigger channel has a non-zero initial value of 1 (consider using initial_event=True to detect this event)
Removing orphaned offset at the beginning of the file.
89 events found
Event IDs: [1 2 3]
45 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
Loading data for 45 events and 801 original time points ...
0 bad epochs dropped
Traceback (most recent call last):

  File "<ipython-input-1-212a350e05c6>", line 65, in <module>
    scores = cross_val_score(clf, epochs_data_train, labels, cv=cv, n_jobs=1)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\model_selection\_validation.py", line 321, in cross_val_score
    pre_dispatch=pre_dispatch)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\model_selection\_validation.py", line 195, in cross_validate
    for train, test in cv.split(X, y, groups))

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 625, in dispatch_one_batch
    self._dispatch(tasks)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 588, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 111, in apply_async
    result = ImmediateResult(func)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 332, in __init__
    self.results = batch()

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\model_selection\_validation.py", line 437, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\pipeline.py", line 257, in fit
    Xt, fit_params = self._fit(X, y, **fit_params)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\pipeline.py", line 222, in _fit
    **fit_params_steps[name])

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\externals\joblib\memory.py", line 362, in __call__
    return self.func(*args, **kwargs)

  File "C:\Users\user\Anaconda2\lib\site-packages\sklearn\pipeline.py", line 589, in _fit_transform_one
    res = transformer.fit_transform(X, y, **fit_params)

  File "C:\Users\user\Anaconda2\lib\site-packages\mne\decoding\mixin.py", line 33, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)

  File "C:\Users\user\Anaconda2\lib\site-packages\mne\decoding\csp.py", line 172, in fit
    class_, reg=self.reg, method_params=self.cov_method_params)

  File "C:\Users\user\Anaconda2\lib\site-packages\mne\cov.py", line 1586, in _regularized_covariance
    info = create_info(data.shape[-2], 1000., 'eeg') if info is None else info

  File "<string>", line 2, in create_info

  File "C:\Users\user\Anaconda2\lib\site-packages\mne\utils.py", line 729, in verbose
    return function(*args, **kwargs)

  File "C:\Users\user\Anaconda2\lib\site-packages\mne\io\meas_info.py", line 1745, in create_info
    raise TypeError('ch_names must be a list, tuple, or int')

TypeError: ch_names must be a list, tuple, or int
agramfort commented 5 years ago

@Rockingsnow I cannot replicate with current master branch:


alex@:mne-python(master)$ ipython
Python 2.7.12 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:43:17)
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: %run debug5
Module created for script run in IPython
Classification accuracy: 0.933333 / Chance level: 0.533333```
Rockingsnow commented 5 years ago

@agramfort Why can you run successfully? Is my development environment not configured right or what code is missing?

Rockingsnow commented 5 years ago

This is my version information: import sys print sys.version print sys.version_info 2.7.13 |Anaconda, Inc.| (default, Sep 19 2017, 08:25:59) [MSC v.1500 64 bit (AMD64)] sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)

Should I update my python to version 2.7.12 or 3.0 ?

agramfort commented 5 years ago

what MNE version are you using?

what does :

mne.sys_info()

gives you?

Rockingsnow commented 5 years ago

Recently I have updated my MNE version to the latest one —version 0.16.2 :

NAME mne - MNE software for MEG and EEG data analysis.

FILE c:\users\user\anaconda2\lib\site-packages\mne__init__.py

PACKAGE CONTENTS annotations baseline beamformer (package) bem channels (package) chpi commands (package) connectivity (package) coreg cov cuda data (package) datasets (package) decoding (package) defaults dipole epochs event evoked externals (package) filter fixes forward (package) gui (package) inverse_sparse (package) io (package) label minimum_norm (package) misc parallel preprocessing (package) proj realtime (package) report selection simulation (package) source_estimate source_space stats (package) surface tests (package) time_frequency (package) transforms utils viz (package)

DATA version = '0.16.2'

VERSION 0.16.2

agramfort commented 5 years ago

you need to use the latest master version (unreleased)

See https://mne-tools.github.io/dev/advanced_setup.html#advanced-setup

use something like this:

pip install --upgrade --no-deps https://api.github.com/repos/mne-tools/mne-python/zipball/master

Rockingsnow commented 5 years ago

I don't know there is also a newer version of MNE! All right, I will try soon.

larsoner commented 5 years ago

Also the output of:

>>> import mne
>>> mne.sys_info()

Would be helpful. This is what @agramfort was asking for. It looks like you pasted something different above.

Rockingsnow commented 5 years ago

After updating my MNE to the latest version——mne-0.17.dev0 : Platform: Windows-10-10.0.15063 Python: 2.7.13 |Anaconda, Inc.| (default, Sep 19 2017, 08:25:59) [MSC v.1500 64 bit (AMD64)] Executable: C:\Users\user\Anaconda2\pythonw.exe CPU: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel: 28 cores Memory: 31.9 GB

mne: 0.17.dev0 numpy: 1.13.1 {lapack=mkl_rt, blas=mkl_rt} scipy: 0.19.1 matplotlib: 2.0.2 {backend=TkAgg}

sklearn: 0.19.0 nibabel: 2.2.1 mayavi: Not found cupy: Not found pandas: 0.20.3 dipy: Not found

The same error still occurred as follow:

File "C:\Users\user\Anaconda2\lib\site-packages\mne\utils.py", line 797, in verbose return function(*args, **kwargs)

File "C:\Users\user\Anaconda2\lib\site-packages\mne\io\meas_info.py", line 1783, in create_info ("list, tuple, or int"))

File "C:\Users\user\Anaconda2\lib\site-packages\mne\utils.py", line 2915, in _validate_type ', got %s instead.' % (type(item),))

TypeError: ('ch_names', ' must be an instance of ', 'list, tuple, or int', ", got <type 'long'> instead.")

agramfort commented 5 years ago

can you open your meas_info.py file (in your mne install) and see if it works when you replace this line:

https://github.com/mne-tools/mne-python/blob/master/mne/io/meas_info.py#L1780

by

if isinstance(ch_names, integer_types):

making sure you import integer_types from six like this:

from ..externals.six import string_types, integer_types

Rockingsnow commented 5 years ago

This process is effective and my code have run successfully as well, I think that this problem may be caused by the incompatible of integral type between python 2.7 ( support int and long type) and the code of version 3.5(only support int type). your advice does solve my problem eventually,thanks!

agramfort commented 5 years ago

great

@Rockingsnow do you want to give a try to contribute the fix to MNE with a pull request? or shall we do it?

Rockingsnow commented 5 years ago

I hope so, but I don't know the specific operation process or you can help me deal with it.

agramfort commented 5 years ago

this should help https://mne-tools.github.io/dev/contributing.html?highlight=contributing

and more generally for learning github : https://help.github.com/articles/creating-a-pull-request-from-a-fork/

massich commented 5 years ago

@Rockingsnow if you need help when attempting you swing by https://gitter.im/mne-tools/mne-python

JanCBrammer commented 5 years ago

I have the same issue. However, @agramfort 's fix

if isinstance(ch_names, integer_types):

doesn't solve the issue.

mne.sys_info():

Platform: Windows-7-6.1.7601-SP1 Python: 2.7.15 |Anaconda custom (64-bit)| (default, May 1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)] Executable: C:\Users\Public\Anaconda2\pythonw.exe CPU: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel: 4 cores Memory: 7.9 GB

mne: 0.16.2 numpy: 1.12.1 {lapack=mkl_rt, blas=mkl_rt} scipy: 1.1.0 matplotlib: 2.1.0 {backend=Qt5Agg}

sklearn: 0.19.1 nibabel: 2.0.2 mayavi: Not found pycuda: Not found skcuda: Not found pandas: 0.23.1

When I run this...

import mne
from mne.decoding import CSP

import numpy as np
from numpy import random

import pandas as pd
import matplotlib.pyplot as plt
import os

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import ShuffleSplit
from sklearn.pipeline import Pipeline
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

def train_test_split(data, test_ratio):
  random.seed(42)   
  shuf_ind = random.permutation(len(data))
  test_size = int(len(data) * test_ratio)
  test_idx = shuf_ind[:test_size]
  train_idx = shuf_ind[test_size:]
  return train_idx, test_idx

##############
# setting-up #
##############

# get the data
os.chdir(r'C:\Users\John Doe\surfdrive\Alpha\data\ML_format\features')
all_trials_raw = mne.read_epochs('all_trials_raw-epo.fif', preload=False)

# get the labels
os.chdir(r'C:\Users\John Doe\surfdrive\Alpha\data\ML_format\labels')
labels = pd.read_csv('response.txt',
                     delim_whitespace=True,
                     header=None,
                     names=['subjectID', 'condition', 'ISI', 'response'])
#labels.info()

# split training and test set 
train_idx, test_idx = train_test_split(all_trials_raw, 0.2)
xtrain = all_trials_raw[train_idx]
ytrain = labels.response.iloc[train_idx]

## first, to test CSP, filter data in alpha range (no filter bank yet)
#xtrain.load_data().filter(l_freq=8.0, h_freq=13.0)

## after filtering, discard everything but 1 sec pre-stimulus (i.e. dump the buffer zones that were only there to protect against filter artifacts)
#xtrain.load_data().crop(tmin=-1.0, tmax=0)

# now let's apply CSP to the data and plot the filters
csp = CSP()
csp.fit(xtrain.get_data(), ytrain)

... the following error occurs:

runfile('C:/Users/John Doe/surfdrive/Alpha/code/pre_processing/FBCSP.py', wdir='C:/Users/John Doe/surfdrive/Alpha/code/pre_processing') Reading all_trials_raw-epo.fif ... Found the data of interest: t = -3000.00 ... 2996.09 ms 0 CTF compensation matrices available 3520 matching events found Applying baseline correction (mode: mean) 3520 matching events found Applying baseline correction (mode: mean) Not setting metadata 0 projection items activated Loading data for 2816 events and 1536 original time points ... Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/John Doe/surfdrive/Alpha/code/pre_processing/FBCSP.py', wdir='C:/Users/John Doe/surfdrive/Alpha/code/pre_processing')

File "C:\Users\Public\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Public\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/John Doe/surfdrive/Alpha/code/pre_processing/FBCSP.py", line 65, in csp.fit(xtrain.get_data(), ytrain)

File "C:\Users\Public\Anaconda2\lib\site-packages\mne\decoding\csp.py", line 172, in fit class_, reg=self.reg, method_params=self.cov_method_params)

File "C:\Users\Public\Anaconda2\lib\site-packages\mne\cov.py", line 1586, in _regularized_covariance info = create_info(data.shape[-2], 1000., 'eeg') if info is None else info

File "", line 2, in create_info

File "C:\Users\Public\Anaconda2\lib\site-packages\mne\utils.py", line 729, in verbose return function(*args, **kwargs)

File "C:\Users\Public\Anaconda2\lib\site-packages\mne\io\meas_info.py", line 1745, in create_info if not isinstance(ch_names, (list, tuple)):

TypeError: ch_names must be a list, tuple, or int

larsoner commented 5 years ago

The integer_types is probably not as robust as:

import operator
try:  # integer
    ch_names = [operator.index(ch_names)]
except TypeError:
    pass

can you try that fix?

Also updating to latest mne master you will get a more informative message saying what the errant type is

JanCBrammer commented 5 years ago

Still throws the same error when I

import operator

and insert

try:  # integer
    ch_names = [operator.index(ch_names)]
except TypeError:
    pass

immediatly above https://github.com/mne-tools/mne-python/blob/master/mne/io/meas_info.py#L1780

agramfort commented 5 years ago

@JohnDoenut can you tell us what is ch_names (type and value) before the try?

JanCBrammer commented 5 years ago

the data.shape[-2] argument taken by create_info in meas_info.py pertains to

xtrain.get_data()

xtrain.get_data().shape[-2]

gives 62L

larsoner commented 5 years ago

What is type(np.long(10)) and type(operator.index(np.long(10)))? I'm surprised that the operator.index fix does not take care of this.

larsoner commented 5 years ago

Actually instead could you try #5603 to see if it fixes your issue?

JanCBrammer commented 5 years ago

I applied the fix again, this time correctly as in https://github.com/mne-tools/mne-python/pull/5603. I didn't remove

if isinstance(ch_names, int):

before.

This solves the ch_names issue.