# computing peak frequency in source space

**URL:** <https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [September 30, 2013, 4:02pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617 "2013-09-30T16:02:36Z")\
**Posts on this page:** 11\
**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:** [September 30, 2013, 4:02pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/1 "2013-09-30T16:02:36Z")

</div>

Hi all,

I am interested in computing the peak frequency within a band for each  
source.

So my first question is does this already exists somewhere?

If not what would people suggest as a jumping off point for developing it.  
I was thinking of following apply\_inverse in inverse.py until I get the  
final inverse operator (K in line 753) and then looping through each row  
(source) in K to compute the time course and peak power and frequency. Any  
other suggestions or downsides to this approach?

Thanks,  
Luke  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20130930/1a0598ea/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20130930/1a0598ea/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:** [September 30, 2013, 4:14pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/2 "2013-09-30T16:14:52Z")

</div>

Hi Luke,

> Hi all,
> 
> I am interested in computing the peak frequency within a band for each  
> source.
> 
> So my first question is does this already exists somewhere?

This example might be of interest.

[http://martinos.org/mne/auto\_examples/time\_frequency/plot\_source\_space\_time\_frequency.html#example-time-frequency-plot-source-space-time-frequency-py](http://martinos.org/mne/auto_examples/time_frequency/plot_source_space_time_frequency.html#example-time-frequency-plot-source-space-time-frequency-py)

Basically it returns source estimates per frequency band each of which can  
be visualized on e.g. a cortical surface.

Another timely alternative is the DICS bearmformer recently added by Roman:

[https://github.com/mne-tools/mne-python/blob/master/examples/inverse/plot\_dics\_source\_power.py](https://github.com/mne-tools/mne-python/blob/master/examples/inverse/plot_dics_source_power.py)

[https://github.com/mne-tools/mne-python/blob/master/examples/inverse/plot\_dics\_beamformer.py](https://github.com/mne-tools/mne-python/blob/master/examples/inverse/plot_dics_beamformer.py)

you can always use numpy.argmax and argsort functions to quickly navigate  
through peaks inside the resulting arrays.

If not what would people suggest as a jumping off point for developing it.

> I was thinking of following apply\_inverse in inverse.py until I get the  
> final inverse operator (K in line 753) and then looping through each row  
> (source) in K to compute the time course and peak power and frequency. Any  
> other suggestions or downsides to this approach?

Maybe let's first see whether what is implemented so far gives you what  
you're looking for.

I hope this helps + cheers,  
Denis

> Thanks,  
> Luke
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mne\_analysis mailing list  
> Mne\_analysis at nmr.mgh.harvard.edu  
> [Mne\_analysis Info Page](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> 
> The information in this e-mail is intended only for the person to whom it  
> is  
> addressed. If you believe this e-mail was sent to you in error and the  
> e-mail  
> contains patient information, please contact the Partners Compliance  
> HelpLine at  
> [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you in  
> error  
> but does not contain patient information, please contact the sender and  
> properly  
> dispose of the e-mail.

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20130930/a363e560/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20130930/a363e560/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:** [September 30, 2013, 4:41pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/3 "2013-09-30T16:41:57Z")

</div>

Thanks for the quick reply Denis.

I'm interested in the peak frequency within a band not the power timecourse  
of the band so source\_band\_induced\_power isn't what I want. I also want to  
stick with MNE, as opposed to DICS or another beamformer, for localization  
since I will want to compare with mne/dspm power estimates.

The other complicating factor is that I'm using resting state data (~~ 5  
minutes), so most of the inverse operator code in python runs into memory  
problems. Otherwise I could just do apply\_inverse and then work on the  
returned timecourses in the stc.

once I have a time course for each source finding the peak frequency will  
be pretty straight forward using numpy.fft and numpy.fft.fftfreq.

Hope this makes sense  
-Luke

---

<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:** [September 30, 2013, 4:55pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/4 "2013-09-30T16:55:23Z")

</div>

Hi Luke,

I think you want to either use compute\_source\_psd or (for raw data) or  
compute\_source\_psd\_epochs (for epoched data). The functions are in  
mne.minimum\_norm.time\_frequency. There is an example here:

[http://martinos.org/mne/auto\_examples/time\_frequency/plot\_source\_power\_spectrum.html](http://martinos.org/mne/auto_examples/time_frequency/plot_source_power_spectrum.html)

I hope this helps.

Martin

---

<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:** [September 30, 2013, 4:54pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/5 "2013-09-30T16:54:58Z")

</div>

That is exactly what I was looking for. Thanks.

Best,  
Luke

---

<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:** [September 30, 2013, 5:10pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/6 "2013-09-30T17:10:22Z")

</div>

Sorry Luke,

I did not get what you were looking for when reading your mail first. I'm  
glad the example Martin posted does what you were looking for.

> so most of the inverse operator code in python runs into  
> memory problems.

By the way when working with resting state data you can also generate epochs  
using `mne.event.make_fixed_length_events` and compute source power on  
epochs using `mne.minimum_norm.compute_source_psd_epochs` with the option  
`return_generator` set to True. This will avoid loading all data into  
memory by not returning a list of source estimates but a generator you can  
unfold in a simple loop, such that only one source estimate is processed at  
a given iteration.  
Or you can pass it to functions that do the unpacking as required.

See this example:  
[http://martinos.org/mne/auto\_examples/connectivity/plot\_mne\_inverse\_coherence\_epochs.html#example-connectivity-plot-mne-inverse-coherence-epochs-py](http://martinos.org/mne/auto_examples/connectivity/plot_mne_inverse_coherence_epochs.html#example-connectivity-plot-mne-inverse-coherence-epochs-py)

Cheers,  
Denis

---

<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:** [September 30, 2013, 7:34pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/7 "2013-09-30T19:34:52Z")

</div>

Ahh I didn't know about make\_fixed\_length\_events, that will come in very  
handy.

---

<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:** [September 30, 2013, 9:24pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/8 "2013-09-30T21:24:46Z")

</div>

Is there a reason that compute\_source\_psd doesn't support MNE or is this  
just a bug?

The error I get comes from line 429 of time\_frequency.py. basically  
noise\_norm is None so noise\_norm.size errors.

AttributeError: 'NoneType' object has no attribute 'size'

and in \_assemble\_kernel (inverse.py line 669) noise\_norm is set to None if  
the method = 'MNE'

it seems like line 429 is just trying to get the number of sources?  
Couldn't we get that from K? or am i missing something?

Thanks,  
Luke

---

<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:** [October 1, 2013, 7:00am UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/9 "2013-10-01T07:00:21Z")

</div>

hi Luke,

> Is there a reason that compute\_source\_psd doesn't support MNE or is this  
> just a bug?

yes it is.

would you be willing to give a try at fixing it?

See the "How to contribute" page.

[http://martinos.org/mne/contributing.html](http://martinos.org/mne/contributing.html)

Best,  
Alex

---

<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:** [October 1, 2013, 3:15pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/10 "2013-10-01T15:15:40Z")

</div>

Is there a way to disable parallel jobs in the testing...

nosetest hangs with the following error.

Test cluster level permutations T-test ... [Parallel(n\_jobs=2)]: Done 1  
out of 2 | elapsed: 0.1s remaining: 0.1s  
[Parallel(n\_jobs=2)]: Done 2 out of 2 | elapsed: 0.1s finished  
[Parallel(n\_jobs=2)]: Done 1 out of 2 | elapsed: 0.1s remaining:  
0.1s  
[Parallel(n\_jobs=2)]: Done 2 out of 2 | elapsed: 0.1s finished  
Process PoolWorker-401:  
Traceback (most recent call last):  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in  
\_bootstrap  
Process PoolWorker-402:  
Traceback (most recent call last):  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in  
\_bootstrap  
&nbsp;&nbsp;&nbsp;&nbsp;self.run()  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run  
&nbsp;&nbsp;&nbsp;&nbsp;self.\_target(\*self.\_args, \*\*self.\_kwargs)  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/pool.py", line 57, in worker  
&nbsp;&nbsp;&nbsp;&nbsp;self.run()  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run  
&nbsp;&nbsp;&nbsp;&nbsp;self.\_target(\*self.\_args, \*\*self.\_kwargs)  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/pool.py", line 57, in worker  
&nbsp;&nbsp;&nbsp;&nbsp;task = get()  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/queues.py", line 352, in get  
&nbsp;&nbsp;&nbsp;&nbsp;return recv()  
TypeError: type 'partial' takes at least one argument  
&nbsp;&nbsp;&nbsp;&nbsp;task = get()  
&nbsp;&nbsp;File "/usr/lib64/python2.6/multiprocessing/queues.py", line 352, in get  
&nbsp;&nbsp;&nbsp;&nbsp;return recv()  
TypeError: type 'partial' takes at least one argument

I'm running on a redhat 6.2 box with python 2.6.

Thanks  
Luke

---

<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:** [October 1, 2013, 3:22pm UTC](https://mne.discourse.group/t/computing-peak-frequency-in-source-space/617/11 "2013-10-01T15:22:34Z")

</div>

Luke,

can you move this conversation to a github issue?

Alex
