Repository: mne-tools/mne-python

Issue:
make RawArray fail gracefully
If you run this piece of code:

``` py
import mne
import os.path as op
from mne.io import RawArray

raw = mne.io.RawFIF(
    op.join(mne.datasets.sample.data_path(), 'MEG', 'sample',
            'sample_audvis_raw.fif'))

raw_array = RawArray(raw.info, raw[:][0])
```

you get this error:

``` py
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/mainak/Desktop/fail_gracefull.py in <module>()
      7             'sample_audvis_raw.fif'))
      8 
----> 9 raw_array = RawArray(raw.info, raw[:][0])

/home/mainak/Desktop/projects/mne-python/mne/io/array/array.pyc in __init__(self, data, info, first_samp, verbose)

/home/mainak/Desktop/projects/mne-python/mne/utils.pyc in verbose(function, *args, **kwargs)
    548         with use_log_level(verbose_level):
    549             return function(*args, **kwargs)
--> 550     return function(*args, **kwargs)
    551 
    552 

/home/mainak/Desktop/projects/mne-python/mne/io/array/array.pyc in __init__(self, data, info, first_samp, verbose)
     36     def __init__(self, data, info, first_samp=0, verbose=None):
     37         dtype = np.complex128 if np.any(np.iscomplex(data)) else np.float64
---> 38         data = np.asanyarray(data, dtype=dtype)
     39 
     40         if data.ndim != 2:

/home/mainak/anaconda/lib/python2.7/site-packages/numpy/core/numeric.pyc in asanyarray(a, dtype, order)
    523 
    524     """
--> 525     return array(a, dtype, copy=False, order=order, subok=True)
    526 
    527 def ascontiguousarray(a, dtype=None):

ValueError: could not convert string to float: acq_stim
```

I feel it could fail more gracefully



Discussion:
ok I would just call a check_info function that can reuse the _check_consistency method every time the user is expected to pass a measurement info as input.

wdyt?


Resolving patch:
diff --git a/mne/epochs.py b/mne/epochs.py
--- a/mne/epochs.py
+++ b/mne/epochs.py
@@ -458,7 +458,9 @@ def _reject_setup(self, reject, flat):
                                % (kind, bads))
 
         for key in idx.keys():
-            if len(idx[key]) == 0 and (key in reject or key in flat):
+            # don't throw an error if rejection/flat would do nothing
+            if len(idx[key]) == 0 and (np.isfinite(reject.get(key, np.inf)) or
+                                       flat.get(key, -1) >= 0):
                 # This is where we could eventually add e.g.
                 # self.allow_missing_reject_keys check to allow users to
                 # provide keys that don't exist in data
diff --git a/mne/io/array/array.py b/mne/io/array/array.py
--- a/mne/io/array/array.py
+++ b/mne/io/array/array.py
@@ -7,6 +7,7 @@
 import numpy as np
 
 from ..base import _BaseRaw
+from ..meas_info import Info
 from ...utils import verbose, logger
 
 
@@ -34,6 +35,10 @@ class RawArray(_BaseRaw):
     """
     @verbose
     def __init__(self, data, info, first_samp=0, verbose=None):
+        if not isinstance(info, Info):
+            raise TypeError('info must be an instance of Info, got %s'
+                            % type(info))
+        info._check_consistency()  # make sure it's valid
         dtype = np.complex128 if np.any(np.iscomplex(data)) else np.float64
         data = np.asanyarray(data, dtype=dtype)
 


Test patch:
diff --git a/mne/io/array/tests/test_array.py b/mne/io/array/tests/test_array.py
--- a/mne/io/array/tests/test_array.py
+++ b/mne/io/array/tests/test_array.py
@@ -60,6 +60,7 @@ def test_array_raw():
     assert_allclose(data, data2)
     assert_allclose(times, times2)
     assert_true('RawArray' in repr(raw2))
+    assert_raises(TypeError, RawArray, info, data)
 
     # filtering
     picks = pick_types(raw2.info, misc=True, exclude='bads')[:4]
diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py
--- a/mne/tests/test_epochs.py
+++ b/mne/tests/test_epochs.py
@@ -186,6 +186,9 @@ def test_reject():
                   picks=picks, preload=False, reject='foo')
     assert_raises(ValueError, Epochs, raw, events, event_id, tmin, tmax,
                   picks=picks_meg, preload=False, reject=dict(eeg=1.))
+    # this one is okay because it's not actually requesting rejection
+    Epochs(raw, events, event_id, tmin, tmax, picks=picks_meg,
+           preload=False, reject=dict(eeg=np.inf))
     for val in (None, -1):  # protect against older MNE-C types
         for kwarg in ('reject', 'flat'):
             assert_raises(ValueError, Epochs, raw, events, event_id,