# Strange errors with calculating source estimates in	a frequency band

**URL:** <https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [July 31, 2014, 6:14am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767 "2014-07-31T06:14:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 31, 2014, 6:14am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/1 "2014-07-31T06:14:38Z")

</div>

Hi everyone!

I have been trying to code a program that would enable to plot user selectable time windows of averaged source estimates in a certain frequency band in real time with FieldTrip Buffer. I'm now trying to simulate that in offline mode with looping through the epochs.get\_data() and then building an Epoch from a single epoch with EpochsArray (because that's the way the real time sytem works). I end up with a couple of odd errors using compute\_source\_psd\_epochs depending on how long my time window is. If my time window is shorter than 10 seconds, my error is this:

/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, \*where)  
&nbsp;&nbsp;&nbsp;&nbsp;173 else:  
&nbsp;&nbsp;&nbsp;&nbsp;174 filename = fname  
--\> 175 \_\_builtin\_\_.execfile(filename, \*where)

/home/silfves1/mne-python/examples/omatfunktiot/jatk\_off.py in \<module\>()  
&nbsp;&nbsp;&nbsp;&nbsp;380  
&nbsp;&nbsp;&nbsp;&nbsp;381  
--\> 382 stc = compute\_source\_psd\_epochs(epochs=ep, inverse\_operator=inverse\_operator, lambda2=lambda2, method=method, fmin=15.0, fmax=25.0, pick\_ori=None, pca=True, bandwidth=bandwidth, n\_jobs=10, return\_generator=False)  
&nbsp;&nbsp;&nbsp;&nbsp;383  
&nbsp;&nbsp;&nbsp;&nbsp;384

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/utils.pyc in verbose(function, \*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;549 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;550 else:  
--\> 551 ret = function(\*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;552 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;553

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)  
&nbsp;&nbsp;&nbsp;&nbsp;669 # return a list

&nbsp;&nbsp;&nbsp;&nbsp;670 stcs = list()  
--\> 671 for stc in stcs\_gen:  
&nbsp;&nbsp;&nbsp;&nbsp;672 stcs.append(stc)  
&nbsp;&nbsp;&nbsp;&nbsp;673

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in \_compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, n\_jobs, verbose)  
&nbsp;&nbsp;&nbsp;&nbsp;519  
&nbsp;&nbsp;&nbsp;&nbsp;520 dpss, eigvals = dpss\_windows(n\_times, half\_nbw, n\_tapers\_max,  
--\> 521 low\_bias=low\_bias)  
&nbsp;&nbsp;&nbsp;&nbsp;522 n\_tapers = len(dpss)  
&nbsp;&nbsp;&nbsp;&nbsp;523

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/time\_frequency/multitaper.pyc in dpss\_windows(N, half\_nbw, Kmax, low\_bias, interp\_from, interp\_kind)  
&nbsp;&nbsp;&nbsp;&nbsp;202 # only calculate the highest Kmax eigenvalues

&nbsp;&nbsp;&nbsp;&nbsp;203 w = linalg.eigvals\_banded(ab, select='i',  
--\> 204 select\_range=(N - Kmax, N - 1))  
&nbsp;&nbsp;&nbsp;&nbsp;205 w = w[::-1]  
&nbsp;&nbsp;&nbsp;&nbsp;206

/usr/lib/python2.7/dist-packages/scipy/linalg/decomp.pyc in eigvals\_banded(a\_band, lower, overwrite\_a\_band, select, select\_range)  
&nbsp;&nbsp;&nbsp;&nbsp;698 return eig\_banded(a\_band, lower=lower, eigvals\_only=1,  
&nbsp;&nbsp;&nbsp;&nbsp;699 overwrite\_a\_band=overwrite\_a\_band, select=select,  
--\> 700 select\_range=select\_range)  
&nbsp;&nbsp;&nbsp;&nbsp;701  
&nbsp;&nbsp;&nbsp;&nbsp;702 \_double\_precision = ['i','l','d']

/usr/lib/python2.7/dist-packages/scipy/linalg/decomp.pyc in eig\_banded(a\_band, lower, eigvals\_only, overwrite\_a\_band, select, select\_range, max\_ev)  
&nbsp;&nbsp;&nbsp;&nbsp;490 vl, vu, il, iu = 0.0, 0.0, min(select\_range), max(select\_range)  
&nbsp;&nbsp;&nbsp;&nbsp;491 if min(il, iu) \< 0 or max(il, iu) \>= a1.shape[1]:  
--\> 492 raise ValueError('select\_range out of bounds')  
&nbsp;&nbsp;&nbsp;&nbsp;493 max\_ev = iu - il + 1  
&nbsp;&nbsp;&nbsp;&nbsp;494 else: # 1, 'v', 'value'

ValueError: select\_range out of bounds

However, if my time window is 10 seconds, here's the error then:

error Traceback (most recent call last)  
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, \*where)  
&nbsp;&nbsp;&nbsp;&nbsp;173 else:  
&nbsp;&nbsp;&nbsp;&nbsp;174 filename = fname  
--\> 175 \_\_builtin\_\_.execfile(filename, \*where)

/home/silfves1/mne-python/examples/omatfunktiot/jatk\_off.py in \<module\>()  
&nbsp;&nbsp;&nbsp;&nbsp;380  
&nbsp;&nbsp;&nbsp;&nbsp;381  
--\> 382 stc = compute\_source\_psd\_epochs(epochs=ep, inverse\_operator=inverse\_operator, lambda2=lambda2, method=method, fmin=15.0, fmax=25.0, pick\_ori=None, pca=True, bandwidth=bandwidth, n\_jobs=10, return\_generator=False)  
&nbsp;&nbsp;&nbsp;&nbsp;383  
&nbsp;&nbsp;&nbsp;&nbsp;384

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/utils.pyc in verbose(function, \*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;549 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;550 else:  
--\> 551 ret = function(\*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;552 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;553

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)  
&nbsp;&nbsp;&nbsp;&nbsp;669 # return a list

&nbsp;&nbsp;&nbsp;&nbsp;670 stcs = list()  
--\> 671 for stc in stcs\_gen:  
&nbsp;&nbsp;&nbsp;&nbsp;672 stcs.append(stc)  
&nbsp;&nbsp;&nbsp;&nbsp;673

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in \_compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, n\_jobs, verbose)  
&nbsp;&nbsp;&nbsp;&nbsp;545  
&nbsp;&nbsp;&nbsp;&nbsp;546 # compute tapered spectra in sensor space

--\> 547 x\_mt, freqs = \_mt\_spectra(data, dpss, sfreq)  
&nbsp;&nbsp;&nbsp;&nbsp;548  
&nbsp;&nbsp;&nbsp;&nbsp;549 if k == 0:

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/time\_frequency/multitaper.pyc in \_mt\_spectra(x, dpss, sfreq, n\_fft)  
&nbsp;&nbsp;&nbsp;&nbsp;439 # remove mean (do not use in-place subtraction as it may modify input x)

&nbsp;&nbsp;&nbsp;&nbsp;440 x = x - np.mean(x, axis=-1)[:, np.newaxis]  
--\> 441 x\_mt = fftpack.fft(x[:, np.newaxis, :] \* dpss, n=n\_fft)  
&nbsp;&nbsp;&nbsp;&nbsp;442  
&nbsp;&nbsp;&nbsp;&nbsp;443 # only keep positive frequencies

/usr/lib/python2.7/dist-packages/scipy/fftpack/basic.pyc in fft(x, n, axis, overwrite\_x)  
&nbsp;&nbsp;&nbsp;&nbsp;213  
&nbsp;&nbsp;&nbsp;&nbsp;214 if axis == -1 or axis == len(tmp.shape) - 1:  
--\> 215 return work\_function(tmp,n,1,0,overwrite\_x)  
&nbsp;&nbsp;&nbsp;&nbsp;216  
&nbsp;&nbsp;&nbsp;&nbsp;217 tmp = swapaxes(tmp, axis, -1)

error: failed in converting 1st argument `x' of \_fftpack.zrfft to C/Fortran array

I don't know, why these error come up. Do some of you have any ideas to help? Thank you so much!

Best regards

Sebastian  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20140731/55d6286b/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20140731/55d6286b/attachment.html)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 31, 2014, 9:22am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/2 "2014-07-31T09:22:58Z")

</div>

Hi Sebastian,

It seems to me that you are somehow iterating over the epochs and stc  
objects incorrectly. You do not need to use EpochsArray because you can  
iterate over the Epochs object directly (same with RtEpochs I think). For  
stc, if you have a single epoch, can you check if your iterations are  
correct?

A small code snippet / gist that reproduces this issue would be great.

Mainak

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 31, 2014, 10:37am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/3 "2014-07-31T10:37:39Z")

</div>

This next snippet is from the offline version I've been testing:

for ii, data in enumerate(epochs.get\_data()):

&nbsp;&nbsp;&nbsp;&nbsp;# make the 2D epoch to 3D

&nbsp;&nbsp;&nbsp;&nbsp;data = data[newaxis, ...]

&nbsp;&nbsp;&nbsp;&nbsp;ep = EpochsArray(data=data, info=raw.info, events=events, tmin=0,event\_id=event\_id, reject=reject)

&nbsp;&nbsp;&nbsp;&nbsp;stc = compute\_source\_psd\_epochs(epochs=ep, inverse\_operator=inverse\_operator, lambda2=lambda2,method=method, fmin=15.0, fmax=25.0, pick\_ori=None, pca=True, bandwidth=bandwidth, n\_jobs=10, return\_generator=False)

and the error comes in the last command.

What is the best way to iterate over rt\_epochs? rt.epochs.next()? I'm now doing just

for ii, data in enumerate(rt\_epochs):

Sebastian

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 31, 2014, 8:31pm UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/4 "2014-07-31T20:31:54Z")

</div>

What are you using for `events`? It should have the same number of entries  
as the number of epochs (1), I suspect it might not. (We should probably  
add a check in mne-python that this is the case...)

Eric

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 31, 2014, 10:20pm UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/5 "2014-07-31T22:20:35Z")

</div>

Also, can you try something like this?

for ii, ep in enumerate(epochs):  
&nbsp;&nbsp;&nbsp;&nbsp;stc = compute\_source\_psd\_epochs(epochs=ep ...)

which would not require EpochsArray.

Mainak

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 1, 2014, 7:06am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/6 "2014-08-01T07:06:05Z")

</div>

Eric:

I have tried to use the events with which I have determined the epochs which has 99 events. That didn't work so I tried to make another events with just one event with this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# make the 2D epoch to 3D (because the EpochsArray has to be given 3D data

&nbsp;&nbsp;&nbsp;&nbsp;ep = ep[newaxis, ...]

&nbsp;&nbsp;&nbsp;&nbsp;new\_event\_list = np.empty([1, 3])  
&nbsp;&nbsp;&nbsp;&nbsp;new\_event\_list[0] = [event\_list[ii][0], 0, 10] # put here the measurement recording start time.  
&nbsp;&nbsp;&nbsp;&nbsp;# Make your own filepath to which to make the event  
&nbsp;&nbsp;&nbsp;&nbsp;fname\_new\_event = 'FILEPATH'

&nbsp;&nbsp;&nbsp;&nbsp;# Write the event  
&nbsp;&nbsp;&nbsp;&nbsp;write\_events(fname\_new\_event, new\_event\_list)

&nbsp;&nbsp;&nbsp;&nbsp;# Finally make the event  
&nbsp;&nbsp;&nbsp;&nbsp;new\_events = read\_events(fname\_new\_event)

and from here on I use the EpochsArray and compute\_source\_psd\_epochs.

Mainak:

I tried your suggestion and that doesn't work either:

AttributeError Traceback (most recent call last)  
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, \*where)  
&nbsp;&nbsp;&nbsp;&nbsp;173 else:  
&nbsp;&nbsp;&nbsp;&nbsp;174 filename = fname  
--\> 175 \_\_builtin\_\_.execfile(filename, \*where)

/home/silfves1/mne-python/examples/omatfunktiot/jatk\_off.py in \<module\>()  
&nbsp;&nbsp;&nbsp;&nbsp;345 # for ii, ep in enumerate(epochs.get\_data()):

&nbsp;&nbsp;&nbsp;&nbsp;346 for ii, ep in enumerate(epochs):  
--\> 347 stc = compute\_source\_psd\_epochs(epochs=ep, inverse\_operator=inverse\_operator, lambda2=lambda2, method=method, fmin=15.0, fmax=25.0, pick\_ori=None, pca=True, bandwidth=bandwidth, n\_jobs=10, return\_generator=False)  
&nbsp;&nbsp;&nbsp;&nbsp;348  
&nbsp;&nbsp;&nbsp;&nbsp;349 # make the 2D epoch to 3D

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/utils.pyc in verbose(function, \*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;549 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;550 else:  
--\> 551 ret = function(\*args, \*\*kwargs)  
&nbsp;&nbsp;&nbsp;&nbsp;552 return ret  
&nbsp;&nbsp;&nbsp;&nbsp;553

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, return\_generator, n\_jobs, verbose, pick\_normal)  
&nbsp;&nbsp;&nbsp;&nbsp;669 # return a list

&nbsp;&nbsp;&nbsp;&nbsp;670 stcs = list()  
--\> 671 for stc in stcs\_gen:  
&nbsp;&nbsp;&nbsp;&nbsp;672 stcs.append(stc)  
&nbsp;&nbsp;&nbsp;&nbsp;673

/home/silfves1/.local/lib/python2.7/site-packages/mne-0.8.git-py2.7.egg/mne/minimum\_norm/time\_frequency.pyc in \_compute\_source\_psd\_epochs(epochs, inverse\_operator, lambda2, method, fmin, fmax, pick\_ori, label, nave, pca, inv\_split, bandwidth, adaptive, low\_bias, n\_jobs, verbose)  
&nbsp;&nbsp;&nbsp;&nbsp;483 # Pick the correct channels from the data

&nbsp;&nbsp;&nbsp;&nbsp;484 #

--\> 485 sel = \_pick\_channels\_inverse\_operator(epochs.ch\_names, inv)  
&nbsp;&nbsp;&nbsp;&nbsp;486 logger.info('Picked %d channels from the data' % len(sel))  
&nbsp;&nbsp;&nbsp;&nbsp;487 logger.info('Computing inverse...')

AttributeError: 'numpy.ndarray' object has no attribute 'ch\_names'

because then the compute\_source\_psd\_epochs uses a numpy array as an epoch.

Sebastian

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 1, 2014, 11:08am UTC](https://mne.discourse.group/t/strange-errors-with-calculating-source-estimates-in-a-frequency-band/767/7 "2014-08-01T11:08:00Z")

</div>

I had a look at the script together with Sebastian. This works:

for ii, ep in enumerate(epochs):  
&nbsp;&nbsp;&nbsp;&nbsp;stc = compute\_source\_psd\_epochs(epochs=ep ..., return\_generator=False)

but setting return\_generator=True does not work. I suspect it returns an  
empty generator but maybe others have more insight.

Mainak
