Skip to content

Commit b259e59

Browse files
authored
Fix a bug for ica.get_sources() and add a corresponding test (mne-tools#13068)
1 parent f66a254 commit b259e59

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

doc/changes/dev/13068.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ICA getting sources for concatenated raw instances, by :newcontrib:`Beige Jin`.

doc/changes/names.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
.. _Ashley Drew: https://github.com/ashdrew
3434
.. _Asish Panda: https://github.com/kaichogami
3535
.. _Austin Hurst: https://github.com/a-hurst
36+
.. _Beige Jin: https://github.com/BeiGeJin
3637
.. _Ben Beasley: https://github.com/musicinmybrain
3738
.. _Bradley Voytek: https://github.com/voytek
3839
.. _Britta Westner: https://britta-wstnr.github.io

mne/preprocessing/ica.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,8 +1295,10 @@ def _sources_as_raw(self, raw, add_channels, start, stop):
12951295
picks = pick_channels(raw.ch_names, add_channels)
12961296
data_ = np.concatenate([data_, raw.get_data(picks, start=start, stop=stop)])
12971297
out._data = data_
1298-
out._first_samps = [out.first_samp]
1299-
out._last_samps = [out.last_samp]
1298+
out_first_samp = out.first_samp
1299+
out_last_samp = out.last_samp
1300+
out._first_samps = [out_first_samp]
1301+
out._last_samps = [out_last_samp]
13001302
out.filenames = [None]
13011303
out.preload = True
13021304
out._projector = None

mne/preprocessing/tests/test_ica.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
EpochsArray,
2626
EvokedArray,
2727
Info,
28+
concatenate_raws,
2829
create_info,
2930
make_ad_hoc_cov,
3031
pick_channels_regexp,
@@ -1727,3 +1728,21 @@ def test_ica_ch_types(ch_type):
17271728
for inst in [raw, epochs, evoked]:
17281729
ica.apply(inst)
17291730
ica.get_sources(inst)
1731+
1732+
1733+
@testing.requires_testing_data
1734+
def test_ica_get_sources_concatenated():
1735+
"""Test ICA get_sources method with concatenated raws."""
1736+
# load data
1737+
raw = read_raw_fif(raw_fname).crop(0, 3).load_data() # raw has 3 seconds of data
1738+
# create concatenated raw instances
1739+
raw_concat = concatenate_raws(
1740+
[raw.copy(), raw.copy()]
1741+
) # raw_concat has 6 seconds of data
1742+
# do ICA
1743+
ica = ICA(n_components=2, max_iter=2)
1744+
with _record_warnings(), pytest.warns(UserWarning, match="did not converge"):
1745+
ica.fit(raw_concat)
1746+
# get sources
1747+
raw_sources = ica.get_sources(raw_concat) # but this only has 3 seconds of data
1748+
assert raw_concat.n_times == raw_sources.n_times # this will fail

0 commit comments

Comments
 (0)