Repository: mne-tools/mne-python

Issue:
[BUG] sources_as_raw leads to error when calling __repr__ of raw object
After creating an `ICA object` and calling `get_sources`. The created `Raw object` hast an empty filenames list [mne.preprocessing.ica:_sources_as_raw](https://github.com/mne-tools/mne-python/blob/master/mne/preprocessing/ica.py#L729).
This causes an `IndexError` when calling [mne.io.Base:repr](https://github.com/mne-tools/mne-python/blob/master/mne/io/base.py#L1884) because the list is not `None`, but empty.


Discussion:
Looks like it should set `_filenames = [None]` instead of `_filenames = []`. Can you make a PR to fix it, and add a test? The test can just be a line in `mne/preprocessing/tests/test_ica.py` that does `print(repr(ica_raw))`
Wow, this seems like a time-warp, a regression?
Fix welcomed.


On Tue, Nov 29, 2016 at 5:04 PM ppasler <notifications@github.com> wrote:

> After creating an ICA object and calling get_sources. The created Raw
> object hast an empty filenames list mne.preprocessing.ica:_sources_as_raw
> <https://github.com/mne-tools/mne-python/blob/master/mne/preprocessing/ica.py#L729>
> .
> This causes an IndexError when calling repr
> <https://github.com/mne-tools/mne-python/blob/master/mne/io/base.py#L1884>
> because the list is not None, but empty.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/mne-tools/mne-python/issues/3804>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AB0fiq_w9_7ryyAXTrVpVhgI85GOWQiAks5rDE0KgaJpZM4K_JoE>
> .
>

@Eric89GXL @dengemann sure.
@Eric89GXL @dengemann this is first time I contribute to an open source project. 

1. fork the repo
2. create a branch from master
3. add a PullRequest with my changes

Is this correct?
Yep that's the idea. How you go about creating branches and the like is up to you. It's possible to do it all graphically on GitHub to start. For frequent development it's easier to learn the `git` command-line interface.

Resolving patch:
diff --git a/mne/preprocessing/ica.py b/mne/preprocessing/ica.py
--- a/mne/preprocessing/ica.py
+++ b/mne/preprocessing/ica.py
@@ -726,7 +726,7 @@ def _sources_as_raw(self, raw, add_channels, start, stop):
             _, times_ = raw[0, start:stop]
         out._data = data_
         out._times = times_
-        out._filenames = list()
+        out._filenames = [None]
         out.preload = True
 
         # update first and last samples


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
@@ -216,7 +216,12 @@ def test_ica_core():
             ica.fit(raw, picks=pcks, start=start, stop=stop)
         assert_array_almost_equal(unmixing1, ica.unmixing_matrix_)
 
-        sources = ica.get_sources(raw)[:, :][0]
+        raw_sources = ica.get_sources(raw)
+        # test for #3804
+        assert_equal(raw_sources._filenames, [None])
+        print(raw_sources)
+
+        sources = raw_sources[:, :][0]
         assert_true(sources.shape[0] == ica.n_components_)
 
         # test preload filter
@@ -526,7 +531,7 @@ def f(x, y):
     # Test ica fiff export
     ica_raw = ica.get_sources(raw, start=0, stop=100)
     assert_true(ica_raw.last_samp - ica_raw.first_samp == 100)
-    assert_true(len(ica_raw._filenames) == 0)  # API consistency
+    assert_equal(len(ica_raw._filenames), 1)  # API consistency
     ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch]
     assert_true(ica.n_components_ == len(ica_chans))
     test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif')