Repository navigation
[DOC] Improve argument documentation for TFR multitaper - #11265
Conversation
| def tfr_array_multitaper(epoch_data, sfreq, freqs, n_cycles=7.0, | ||
| zero_mean=True, time_bandwidth=None, use_fft=True, | ||
| decim=1, output='complex', n_jobs=None, | ||
| zero_mean=True, time_bandwidth=4.0, use_fft=True, |
There was a problem hiding this comment.
tfr_multitaper default is 4.0.
tfr_array_multitaper default was None, which sets it to 4.0.
I changed it to have the same default and skip this None step.
|
I haven't been able to return to TFR until today, I need some clarification on the following bullet point before I try to integrate it into the documentation:
Also, the relationship between frequency of interest |
I was recently reading about this, and found this passage from https://en.wikipedia.org/wiki/Wavelet to be clear/helpful:
In other words, time x bandwidth determines the cell "area" on the time x frequency plane (also called an "atom" sometimes). What we lose in favor of frequency resolution is temporal resolution. IDK why it's supposed to be >=2 though. This figure may also be helpful: https://en.wikipedia.org/wiki/Wavelet#/media/File:Time_frequency_atom_resolution.png |
|
I didn't look at this PR in detail, but some time ago I discussed a similar confusion in psd multitaper arguments on the discourse list, maybe it will be helpful: https://mne.discourse.group/t/bandwidth-of-psd/4529/2 |
|
Adding to @drammock description, the "Time x (Full) Bandwidth" product referred to in the docs (later just |
|
Thanks for the details! There is one part I do not understand, how do we lose temporal resolution in favor of frequency resolution when changing the surface of the time-frequency window? The
|
|
@mmagnuski In this PR, I'll just improve the docstrings of the existing functions. Replacing the existing arguments with more intuitive ones could be done as part of @drammock PR to refactor the TFR-API, where the low-level function would retain the current set of arguments. |
|
@mscheltienne Multitapering is often used when you want to increase frequency smoothing without changing window length (for example when studying gamma oscillations), so you usually want more tapers if you are doing multitapering. It also reduces the variance (noise) in your psd (in a similar way welch does, but by averaging different realisations of the same time-window signal via different tapets, not different time windows). |
|
My earlier statement:
is I think incorrect when talking about multitaper (DPSS) analysis, so it's no wonder you're feeling confused. I was thinking of multiresolution (i.e., wavelet) analysis. In DPSS all the tapers have the same fixed length so temporal resolution shouldn't change when you change the time x (full or half) bandwidth parameter. |
| floor ``time_bandwidth - 1``. | ||
| For example, with ``freq = 20 Hz`` and ``n_cycles = 10``, we get | ||
| ``time = 0.5 s``. If ``time_bandwidth = 4.``, then frequency smoothing | ||
| is ``(4 / time) = 8 Hz``.""" |
There was a problem hiding this comment.
I wouldn't refer to frequency bandwidth as frequency smoothing as some people will understand it as +- smoothing, not the total smoothing width.
| Time x (Full) Bandwidth product. Should be ``≥ 2.0``. | ||
| Choose this along with ``n_cycles`` to set the desired frequency | ||
| resolution. The number of good tapers (least leakage from far away | ||
| frequencies) is chosen automatically based on this to | ||
| floor ``time_bandwidth - 1``. | ||
| For example, with ``freq = 20 Hz`` and ``n_cycles = 10``, we get | ||
| ``time = 0.5 s``. If ``time_bandwidth = 4.``, then frequency smoothing | ||
| is ``(4 / time) = 8 Hz``.""" |
There was a problem hiding this comment.
I suggest changing the sentence order for slightly better clarity, and make sure frequency resolution / frequency bandwidth and ± frequency smoothing is not confused:
| Time x (Full) Bandwidth product. Should be ``≥ 2.0``. | |
| Choose this along with ``n_cycles`` to set the desired frequency | |
| resolution. The number of good tapers (least leakage from far away | |
| frequencies) is chosen automatically based on this to | |
| floor ``time_bandwidth - 1``. | |
| For example, with ``freq = 20 Hz`` and ``n_cycles = 10``, we get | |
| ``time = 0.5 s``. If ``time_bandwidth = 4.``, then frequency smoothing | |
| is ``(4 / time) = 8 Hz``.""" | |
| Time x (Full) Bandwidth product. The number of good tapers (least leakage | |
| from far away frequencies) is chosen automatically as | |
| ``floor(time_bandwidth - 1)``. ``time_bandwidth`` should be ``≥ 2.0`` to | |
| guarantee that at least one taper is used. | |
| Choose the value of ``time_bandwidth`` along with ``n_cycles`` to set the | |
| desired frequency resolution. For example, with ``freq = 20 Hz`` and | |
| ``n_cycles = 10``, we get ``time = 0.5 s``. If ``time_bandwidth = 4.``, | |
| then frequency resolution is ``(4 / time) = 8 Hz``. This is equivalent to | |
| ± 4 Hz frequency smoothing.""" |
There was a problem hiding this comment.
Yes, this is still the original docstring, simply moved to the docdict. I first wanted to make sure I understood the trade-off made by this argument before writing about it ;)
That's what I was remembering too! Ok, now it make sense, and this is what you lose by decreasing the |
…keys [skip azp] [skip actions]
…bandwitdh [skip azp] [skip actions]
…tradeoff [skip azp] [skip actions]
|
I changed the sentence order a bit in the note, I think it's better this way. Here are the updated links to the rendered doc: Updated documentation for PSD:
Updated documentation for TFR:
|
drammock
left a comment
There was a problem hiding this comment.
This is looking really good. I've made a few suggestions of minor wording improvements, and some LaTeX tweaks. Thanks for taking this on, it's a big improvement.
| Either the temporal window has a fixed length independent of frequency, or the | ||
| temporal window decreases in length with increased frequency. | ||
|
|
||
| .. image:: https://www.fieldtriptoolbox.org/assets/img/tutorial/timefrequencyanalysis/figure1.png |
There was a problem hiding this comment.
someday (not in this PR) I think we should make our own figure; this one is good but not excellent, and it would be nice to have dark/light versions of the graphic. Also we risk it breaking if FT rearranges their website and the URL changes.
| """ | ||
|
|
||
| docdict['time_bandwidth_tfr'] = """ | ||
| time_bandwidth : float ``≥ 2.0`` |
There was a problem hiding this comment.
I'm surprised CIs don't complain about this. In general we don't put the constraints on valid values on this line, but I'll admit it is concise to have it here instead of an extra sentence in the param description.
There was a problem hiding this comment.
I was surprised as well that it did not require an exclusion in numpydoc_xref_ignore!
|
Thanks for working on this @mscheltienne, I'll take a look later today! |
Co-authored-by: Daniel McCloy <[email protected]>
|
I think there is a bracket typo in some of the LaTex syntax, I'll check if the doc renders correctly. |
I wouldn't be surprised :) I didn't do a local test sphinx build 🙈 |
|
The LaTex part renders correctly now ;) |
|
I'm happy 👍🏻 @mmagnuski I'll let you merge if you're satisfied. Here's a quick link to the CircleCI docs build for tfr_multitaper and tfr_morlet note the CIs won't go green because of |
|
Great work @mscheltienne! The only remaining thing for me is changing the focus in the multitaper-specific part of the notes a bit: more detailsThe additional frequency smoothing in multitapering may be desirable for at least two reasons: decreasing the noise in the spectrum and increasing the frequency overlap between oscillatory events (across trials for example when performing averaging) when you are interested in phenomena that occur in a relatively wide frequency window (like gamma oscillations). |
|
@mmagnuski You mentioned several times that multitapering is interesting when applied to a wide frequency window, and especially to high frequencies. Is it however a "bad" method to apply on lower frequency bands, delta, theta, alpha? Also, the increased smoothing when studying high-frequencies comes from the bandwidth. Thus, if we leave a bit TFRs and return to PSDs, how would you compute a power estimate on short recordings in the lower frequencies? I would assume a multitaper-psd with only a few tapers (e.g. 3) and a finer frequency resolution would still provide a better estimate than a welch-psd with only a few windows. |
|
Great, thanks for the changes @mscheltienne! I'll merge the PR in a few minutes.
No, often there is just no need to use multitapering for these lower frequencies (the signal to noise i usually high, peaks easily discernible in the spectrum). But I wouldn't explicitly discourge using multitapering for lower frequencies. I can image it might be beneficial when studying single-trial TFR/spectra.
Yes, for short segments, multitapering is a good alternative to Welch. |
|
I don't think I can merge this without all required tests being green. All I was able to do was enable auto-merge. |
|
There you go -- thanks @mscheltienne @mmagnuski ! |
|
BTW @larsoner how were you able to merge this? |
|
I think we configured this repo so that people with repo admin-level permissions can override required CI checks, basically for situations like this or when CIs are misbehaving |
I've been giving a shot at TFR representation for the first time today, and it took me a while to grasp how it works. And it's still a bit of a headache for now 🤯
With that in mind, I'd like to improve the documentation. I've been starting with multitapers, the goal of this PR is to describe better the relation between
freqs,n_cycles,time-window size, andtime_bandwith.tfr_array_multitapertfr_multitaperUpdate the tutorialTime-frequency on simulated data (Multitaper vs. Morlet vs. Stockwell vs. Hilbert)That said I could really use an advanced course on signal processing one of those days, so please let me know if anything I wrote is wrong!
PR Status: for starters, I'll only update
tfr_array_multitaper, and once the documentation is consolidated there, I will updatetfr_multitaper, deduplicate docstrings and update the tutorial.Covered: