Skip to content

[MRG] Adding AverageTFR.plot_joint() method - #4134

Closed
hubertjb wants to merge 1 commit into
mne-tools:masterfrom
hubertjb:tfr_plot_joint
Closed

hubertjb wants to merge 1 commit into
mne-tools:masterfrom
hubertjb:tfr_plot_joint

Conversation

@hubertjb

Copy link
Copy Markdown
Contributor

See issue #3801

Adding aggregate to plot method in averageTFR class

  • Added aggregate (mean or rms) and exclude arguments to averageTFR.plot

  • Set picks argument to default None

  • Created internal _plot method called by averageTFR.plot

  • Added automatic title option in _plot method

Implementing plot_joint method in AverageTFR class

  • Timefreqs and timefreqs_average arguments to specify values to be used

  • for topomaps.

  • Timefreqs can be iterable of tuples or 'peaks', where topomap is

  • plotted for maximum peak in tfr

  • exclude and aggregate arguments in plot_joint

  • Added contour argument in plot_tfr_topomap

  • Moved connection_line method to viz/utils from viz/evoked and added y

  • argument.

  • Adding test_joint_plot

@hubertjb

Copy link
Copy Markdown
Contributor Author

tfr_plot_join_example

@larsoner larsoner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM. Can you update some example to show the new functionality?


plt.close('all')

def test_plot_joint():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 two newlines before function def

Comment thread mne/time_frequency/tfr.py Outdated
dB=False, colorbar=True, show=True, title=None, axes=None,
layout=None, yscale='auto', verbose=None):
layout=None, yscale='auto', verbose=None,
aggregate='mean'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by convention we always put verbose last

Comment thread mne/time_frequency/tfr.py Outdated
picks : None | array-like of int
The indices of the channels to plot, one figure per channel. If
None, plot the across-channel average.
exclude : None | list of str | 'bads'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid breaking argument order, exclude should probably go just before verbose at the end

Comment thread mne/time_frequency/tfr.py Outdated
layout=None, yscale='auto', verbose=None, aggregate='mean',
timefreqs=None, timefreqs_average=None, topomap_args=None):
"""Plot TFRs as a two-dimensional image(s) with topomaps at
specific times and frequencies.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function / methods / classes should have single-line first line, so you can just stop after "topomaps here". If you want to elaborate, add a new paragraph below.

Comment thread mne/time_frequency/tfr.py Outdated
tf_ax = fig.add_subplot(212)

_, tf_data, tf_times, tf_freqs = \
self._plot(picks=picks, exclude=exclude, baseline=baseline,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save lines you can do:

_, tf_data, tf_times, tf_freqs = self.plot(
    ...)

Comment thread mne/viz/topomap.py Outdated
If an array, the values represent the levels for the contours. The
values are in uV for EEG, fT for magnetometers and fT/m for
gradiometers. If colorbar=True, the ticks in colorbar correspond to the
contour levels.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well put this after head_pos

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks for the feedback @Eric89GXL!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch. This was missing? Thanks!

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Looks great!
What about the units for the cbar?

Comment thread mne/time_frequency/tfr.py Outdated
if aggregate is None or len(info['ch_names']) == 1:
title = info['ch_names'][0]
else:
ch_title = ", ".join(info['ch_names'][:6])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor the plot_compare_evoked code that does the same thing and only use one private helper function? (_format_ch_names)

@hubertjb hubertjb Mar 31, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen in plot_compare_evokeds, the '...' is only added if the gfp parameter is False:

    if len(ch_names) > 6 and gfp is False:
        warn("More than 6 channels, truncating title ...")
        title += ", ..."

What's the reason for that?

Comment thread mne/time_frequency/tfr.py Outdated
timefreqs = [(peakmax_time, peakmax_freq)]

# prepare axes for topomap
# slightly convoluted due to colorbar placement and for vertical alignment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly - does it make sense to create a helper to use by both plot_joint functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea.

Comment thread mne/time_frequency/tfr.py Outdated
vmax : float | None
The maxinum value of the color scale. If vmax is None, the data
maximum value is used.
cmap : matplotlib colormap | 'interactive' | (colormap, bool)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interactive mode already works?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! No, it doesn't work yet. Maybe for the moment I'll just remove it from the docstring, what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. @jaeilepp is the expert on interactive stuff, he's basically a wizard, I have no idea how he does it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I don't think it'll work with multiple mappables out of the box.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, if it's only for the topomaps, it should be possible to call DraggableColorbar for each mappable with the same cbar.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should apply to both the TFR and the topomaps, that probably makes it more complicated, right?

Comment thread mne/time_frequency/tfr.py Outdated

for idx in range(len(map_ax)):
time = timefreqs[idx][0]
freq = timefreqs[idx][1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you do for (time, freq), ax in zip(timefreqs, map_ax)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen we also need idx for getting the average parameters (timefreqs_average) in the case where it is a list. That would give something like

for idx, ((time, freq), ax) in enumerate(zip(timefreqs, map_ax)):
    ...

I switched to a simple for idx in range(len(map_ax)): because it was getting a bit cramped up. Do you think it's still better to follow the pythonic way?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I'd make timefreqs_average a dict :)

Comment thread mne/time_frequency/tfr.py Outdated
freq_half_range = timefreqs_average[1]/2
elif isinstance(timefreqs_average, list):
time_half_range = timefreqs_average[idx][0]/2
freq_half_range = timefreqs_average[idx][1]/2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you do smth like
time_range, freq_range = [t / 2 for t in list(timefreqs_average)]

Comment thread mne/time_frequency/tfr.py Outdated
lines = [_connection_line(timepoint, fig, tf_ax, map_ax_, y=freq)
for timepoint, freq, map_ax_ in zip(tf_times, tf_freqs, map_ax)]
for line in lines:
fig.lines.append(line)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fig.lines.extend(lines) ?

# across channels and topomaps at specific times and frequencies.

power.plot_joint(picks=range(82, 84), baseline=(-0.5, 0), mode='logratio',
title='auto', timefreqs=[(1, 10), (1, 19)],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. How about using dicts instead of lists?

(I like dicts. They're more directly interpretable, albeit here more verbose.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good moment to understand this API :)
Also timefreqs is neither CamalCase nor_underscore_style. The latter is the good style. Playing the naïve, how do I read these parameters? Maybe a dict could make it clearer indeed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what concerns the name, forget about it. I just saw that you inherited this from existing code. Let's keep it for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are ad-dict-ed @jona-sassenhagen :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"you are ad-dict-ed @jona-sassenhagen :)", Denis dixit.

@jona-sassenhagen jona-sassenhagen Mar 31, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean:

timefreqs=[(1, 10), (1, 19)],
timefreqs_average={(1, 10):(1, 4), (1, 19):(1, 6)}

Could also just go
timefreqs=[(1, 10), (1, 19)] (no averaging)
vs
timefreqs={(1, 10):(1, 4), (1, 19):(1, 6)} (averaging by dict keys)

@dengemann what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That last one is pretty cool I think!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the implementation, I suggest something like

if not isinstance(timefreqs, dict):
    timefreqs = {k:(1, 1) for k in timefreqs}

So you always have a dict.
At least that's how I do it in plot_compare_evokeds and I think it works fine.

@jona-sassenhagen jona-sassenhagen Apr 1, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh ...

timefreqs = {k:np.asarray(timefreqs[k]) if isinstance(k, dict) else np.array([1, 1]) for k in timefreqs}

Cause then you can later do

for map_ax, ((time, freq), avg) in zip(map_ax, timefreqs.items()):
    time_half_range, freq_half_range = avg / 2

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen nice, thanks! :)

Comment thread mne/viz/utils.py
return Line2D((xt, xs), (yt, ys), transform=tf, color='grey',
linestyle='-', linewidth=1.5, alpha=.66, zorder=1,
clip_on=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread mne/viz/utils.py Outdated
if y is None:
(_, ys) = transFigure.transform(sourceax.transAxes.transform([0, 1]))
else:
(_, ys) = transFigure.transform(sourceax.transData.transform([0, y]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

(_, ys) = transFigure.transform(
    sourceax.transData.transform([0, 1 if y is None else y]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

y = 1 if y is None else y
(_, ys) = transFigure.transform(sourceax.transData.transform([0, y]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, isn't this better:

def _connection_line(x, fig, sourceax, targetax, y=1):
...
    _, ys = transFigure.transform(sourceax.transData.transform([0, y]))

Comment thread mne/viz/utils.py Outdated
if y is None:
(_, ys) = transFigure.transform(sourceax.transAxes.transform([0, 1]))
else:
(_, ys) = transFigure.transform(sourceax.transData.transform([0, y]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the brackets here is neither necessary nor desirable. It should be like _, ys = ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my fault, I wrote the original code like that ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen just say it was the dwarf :)

Comment thread mne/viz/utils.py Outdated
(source) and topolots (target).
"""
from matplotlib.lines import Line2D
transFigure = fig.transFigure.inverted()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no_camel_case_here :)

Comment thread mne/viz/utils.py Outdated
"""
from matplotlib.lines import Line2D
transFigure = fig.transFigure.inverted()
tf = fig.transFigure

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some more explicit variable name would be better. It's funny that one is transFigure and one is tf.

Comment thread mne/time_frequency/tests/test_tfr.py Outdated

# TFR - Topomap joint plot
timefreqs = [(tfr.times[2], tfr.freqs[1]),
(tfr.times[1], tfr.freqs[1])]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 1 and 2? Any reason for not taking the first and the second (index 0 and 1)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, it's pretty arbitrary. I'll make it the first one and the last one (index 0 and -1) instead to test the range instead, what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pick a random number to keep the CIs on their toes :D

Comment thread mne/time_frequency/tfr.py Outdated
order = 1
peaks_idx = argrelmax(tf_data, order=order, axis=2)
if peaks_idx[0].size == 0:
_,p_t,p_f = np.unravel_index(tf_data.argmax(), tf_data.shape)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace missing

Comment thread mne/time_frequency/tfr.py Outdated
locator, contours = _set_contour_locator(vmin, vmax, contours)

topomap_args_pass = dict((k, v) for k, v in topomap_args.items() if
k not in ['axes', 'show', 'colorbar'])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is now safe to write {k: v for k, v in topomap ...}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing: do membership tests with sets.
k not in {'axes', 'show', 'colorbar'}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jona-sassenhagen I disagree. The set is good whenever we want to deal with duplicates/uniques/and ... set logic. A tuple would be more canonical and is preferable.
('axes', 'show', 'colorbar')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well a tuple is better than a list.

Comment thread mne/viz/topomap.py Outdated
cbar_fmt='%1.1e', show_names=False, title=None,
axes=None, show=True, outlines='head', head_pos=None):
axes=None, show=True, outlines='head', head_pos=None,
contours=False,):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the trailing comma.

@dengemann

Copy link
Copy Markdown
Member

@hubertjb great work! It seems we're close to finishing this :)

@nicproulx

Copy link
Copy Markdown
Contributor

@jona-sassenhagen should we add a unit argument in plot_joint? This argument is used in average_tfr.plot_topomap to set the colorbar title for individual topomaps

@hubertjb hubertjb changed the title Adding AverageTFR.plot_joint() method [WIP] Adding AverageTFR.plot_joint() method Mar 31, 2017
@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Can't units be inferred?

'units' seems like something that 1. should be supplied as a default during the call to plot_topomap, 2. with user overried via topomap_args.

Comment thread mne/viz/utils.py
main_ax = fig.add_subplot(212)
ts = n_maps + 2
map_ax = [plt.subplot(4, ts, x + 2 + ts) for x in range(ts - 2)]
cbar_ax = plt.subplot(4, 3 * (ts + 1), 6 * (ts + 1))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... maybe we should also document what the magic numbers in these numbers are for. Which I guess I should have done in the first place ...

@jona-sassenhagen

Copy link
Copy Markdown
Contributor
from itertools import cycle

timefreqs_average = (1, 1) if timefreqs_average is None else timefreqs_average
for map_ax_, (time, freq),  av in zip(
    map_ax, timefreqs, cycle(timefreqs_average)):
    time_half_range, freq_half_range = (x / 2 for x in av)

I think this'll save a lot. Becomes even simpler with dicts:

for map_ax, (time, freq) in zip(map_ax, timefreqs):
    time_half_range, freq_half_range = (x / 2 for x in timefreqs_average.get((time, freq), (1, 1))

... ok. Maybe this is golfing it.

Comment thread mne/time_frequency/tfr.py Outdated
self.plot_topomap(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax,
axes=map_ax[idx], baseline=baseline, mode=mode,
layout=layout, vmin=vmin, vmax=vmax, cmap=cmap,
colorbar=False, show=True, **topomap_args_pass)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use show=False, to prevent showing the figure prematurely and call plt_show at the end (like you're doing).

Comment thread mne/time_frequency/tfr.py Outdated
If an array, the values represent the levels for the contours. The
values are in uV for EEG, fT for magnetometers and fT/m for
gradiometers. If colorbar=True, the ticks in colorbar correspond to the
contour levels.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If None?

Comment thread mne/time_frequency/tfr.py Outdated
vmax : float | None
The maxinum value of the color scale. If vmax is None, the data
maximum value is used.
cmap : matplotlib colormap | 'interactive' | (colormap, bool)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, if it's only for the topomaps, it should be possible to call DraggableColorbar for each mappable with the same cbar.

@dengemann

Copy link
Copy Markdown
Member

Circle CI crashes into some case where for some reason no data is selected:

/home/ubuntu/mne-python/tutorials/plot_sensors_time_frequency.py failed to execute correctly:Traceback (most recent call last):
  File "/home/ubuntu/mne-python/sphinx-gallery/sphinx_gallery/gen_rst.py", line 475, in execute_code_block
    exec(code_block, example_globals)
  File "<string>", line 4, in <module>
  File "<string>", line 2, in plot_joint
  File "/home/ubuntu/mne-python/mne/utils.py", line 707, in verbose
    return function(*args, **kwargs)
  File "/home/ubuntu/mne-python/mne/time_frequency/tfr.py", line 1378, in plot_joint
    colorbar=False, show=True, **topomap_args_pass)
  File "/home/ubuntu/mne-python/mne/time_frequency/tfr.py", line 1700, in plot_topomap
    contours=contours)
  File "/home/ubuntu/mne-python/mne/viz/topomap.py", line 1098, in plot_tfr_topomap
    idx = np.where(_time_mask(tfr.times, tmin, tmax))[0]
  File "/home/ubuntu/mne-python/mne/utils.py", line 2360, in _time_mask
    % (orig_tmin, orig_tmax, times[0], times[-1]))
ValueError: No samples remain when using tmin=1 and tmax=1 (original time bounds are [-0.998976065792, 2.98102393421])
________________________________________________________________________________

@hubertjb

hubertjb commented Apr 17, 2017 •

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen concerning units, it looks like AverageTFR.plot() doesn't yet have an argument for units either.

I guess the units might differ based on how the TFR function was applied, as well as on the original unit of the time series. Would we want to cover all possible cases?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Units: personally I'd chose the easiest possible path for now and go about fixing it for both as soon as this one is merged.

@codecov-io

codecov-io commented Apr 18, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #4134 into master will increase coverage by 0.01%.
The diff coverage is 90.76%.

@@            Coverage Diff             @@
##           master    #4134      +/-   ##
==========================================
+ Coverage   86.24%   86.26%   +0.01%     
==========================================
  Files         357      357              
  Lines       64576    64677     +101     
  Branches     9832     9853      +21     
==========================================
+ Hits        55695    55792      +97     
  Misses       6180     6180              
- Partials     2701     2705       +4

@hubertjb

Copy link
Copy Markdown
Contributor Author

I'm not sure why Travis failed here... Could that come from the rebase?

@jaeilepp

Copy link
Copy Markdown
Contributor

Seems unrelated. I restarted the job.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Green.

@hubertjb

Copy link
Copy Markdown
Contributor Author

Cool, thanks @jaeilepp!

Comment thread mne/time_frequency/tfr.py
_, tf_data, tf_times, tf_freqs = self._plot(
picks=picks, baseline=baseline, mode=mode, tmin=tmin, tmax=tmax,
fmin=fmin, fmax=fmax, vmin=vmin, vmax=vmax, cmap=cmap, dB=dB,
colorbar=False, show=show, title=title, axes=tf_ax,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show=False

Otherwise you get this in the notebook:
screen shot 2017-04-28 at 11 30 03

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll change that to False.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

There's something weird with the yticks. Check this:

%matplotlib inline
import mne
from mne.datasets import testing
from mne.time_frequency import tfr_morlet

data_path = testing.data_path()
fname = data_path + "/EEGLAB/test_raw.set"
montage = data_path + "/EEGLAB/test_chans.locs"

event_id = {"rt":1, "square":2}
eog = {"FPz", "EOG1", "EOG2"}
raw = mne.io.eeglab.read_raw_eeglab(fname, preload=True, eog=eog,
                                    montage=montage, event_id=event_id)
events = mne.find_events(raw)
picks = [idx for idx, ch in enumerate(raw.ch_names) if ch not in eog and ch != 'STI 014']

epochs = mne.Epochs(raw, events=events, event_id=event_id, picks=picks, tmin=-1, tmax=2)
tfr = tfr_morlet(epochs, np.arange(3, 20), n_cycles=3, return_itc=False, n_jobs=-2)
tfr.plot_joint(baseline=(None, 0), show=False);

unknown-246

Not catastrophic, but could be better.

Also, is the colorbar scaled correctly?

Annnnnnd lastly, for some plots I'm getting truncated colorbar vlims:

unknown-247

See what I mean?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

All in all I think it looks great, and I'm happy with the API.

timefreqs = {(.4, 3): (.1, .2),
            (.6, 11): (.1, .2)}
tfr.plot_joint(baseline=(None, 0), show=False, timefreqs=timefreqs, title="EEGLAB Squares");

unknown-249

@mmagnuski

Copy link
Copy Markdown
Member

@jona-sassenhagen the figures look good, indeed. The yticks placement is a known problem, I wanted to correct this for some time, I'll try to make a PR tonight.

@hubertjb

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen I'm not sure I understand what you mean with the colorbar scaling and truncated vlims. The topoplots' color range depends on the vmin and vmax of the TFR plot, and so does the colorbar indirectly, so they should all have the same color range.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

What I mean is, it looks like the topomap vlims aren't the data limits, but narrower, so you have the big red surface. Particularly here.

@hubertjb

hubertjb commented May 2, 2017

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen I see. A way to fix this would be to look at the vmin and vmax of each topomap and of the TFR plot, and then decide on the colorbar boundaries by aggregating them somehow. Currently only the vmin and vmax of the TFR plot are used.

Comment thread mne/time_frequency/tfr.py
topomap_args_pass = {k: v for k, v in topomap_args.items() if
k not in ('axes', 'show', 'colorbar')}
topomap_args_pass['outlines'] = (topomap_args['outlines'] if 'outlines'
in topomap_args else 'skirt')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topomap_args_pass['outlines'] = topomap_args.get("outlines", "skirt")
?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

I don't get why they're different ... is this maybe because TFR.plot in this case aggregates and passes the aggregated data (which will have smaller max and min)?

@mmagnuski

Copy link
Copy Markdown
Member

@jona-sassenhagen
The default is to aggregate across all picks with mean - tf_data, the reduced data, is used for setting vmin and vmax. In most cases this will make the limis different (especially for the frequencies with most pronounced changes wrt the baseline). Maybe the topos should have a separate colorbar? Because setting the limits of tfr to match the limits in the topo may make the tfr look pale (or - in other cases - the topo would go pale).

@jona-sassenhagen

jona-sassenhagen commented May 4, 2017 via email

Copy link
Copy Markdown
Contributor

@hubertjb

hubertjb commented May 6, 2017

Copy link
Copy Markdown
Contributor Author

Right, it would be more informative to have separate colorbars. Do we want one colorbar by element (TFR plot, and each topomap) then? If so I'll add that and update the PR.

Also, would we want to keep an option to have a single colorbar and share vlims across the different elements? I guess it could be useful for a quick visual comparison between e.g., the TFR of a single channel and the other channels as seen on the topomaps.

@jona-sassenhagen

jona-sassenhagen commented May 6, 2017 via email

Copy link
Copy Markdown
Contributor

@hubertjb

Copy link
Copy Markdown
Contributor Author

Ok, so if I understand well:

  • If the TFR plot is an average (and not the RMS), have the colorbar scale with the largest vlims of all subplots (including topomaps and the TFR plot). This might make the TFR plot look pale, but the topomaps will be more informative.
  • If the TFR plot is the RMS, we might have to do things differently. However, the units should still be the same, right? I understand the scale could be very different though. Maybe in that case multiple colorbars would be necessary?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

I'm not sure :(

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Currently my intuition is the colorbar should only be valid on the topomaps. E.g., don't have a colormap for the image (which is an average anyways).

Does that simplify things?

@hubertjb

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen Sorry for the delay!

OK, just double-checking before I implement. Here's what I'll do:

  1. For each specified timefreqs interval, find vmin and vmax. I'll make a new utils function, similar to the first few lines of topomap.py:plot_tfr_topomap(), that extracts and aggregates the data in the right time and frequency range, and returns the min and max of the array.
  2. Aggregate all topomaps' vmins and vmax. In the simplest case, the final vmin and vmax are the smallest and biggest of all vmins and vmax, respectively.
  3. For each specified timefreqs interval, plot a topomap with previously found vmin and vmax. This is identical to what we already have, except that we wouldn't use the vmin and vmax of the TFR plot anymore, but the ones that come from the topomaps instead.
  4. Draw the colorbar using the previously found vmin and vmax.

Also, I was thinking the arguments vmin and vmax could also accept a string, letting the user choose the source of the vmin and vmax. For example, it could take value 'tfr' if the user wants everything to use the vmin and vmax of the TFR plot, or value ''topomap' if the user wants the colorbar to reflect the aggregated vmin and vmax of the topomaps. Would that be worth it?

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

@mmagnuski what do you think of this solution?

For each specified timefreqs interval, find vmin and vmax. I'll make a new utils function, similar to the first few lines of topomap.py:plot_tfr_topomap(), that extracts and aggregates the data in the right time and frequency range, and returns the min and max of the array.
Aggregate all topomaps' vmins and vmax. In the simplest case, the final vmin and vmax are the smallest and biggest of all vmins and vmax, respectively.
For each specified timefreqs interval, plot a topomap with previously found vmin and vmax. This is identical to what we already have, except that we wouldn't use the vmin and vmax of the TFR plot anymore, but the ones that come from the topomaps instead.
Draw the colorbar using the previously found vmin and vmax.

Looks sensible to me.

Also, I was thinking the arguments vmin and vmax could also accept a string, letting the user choose the source of the vmin and vmax. For example, it could take value 'tfr' if the user wants everything to use the vmin and vmax of the TFR plot, or value ''topomap' if the user wants the colorbar to reflect the aggregated vmin and vmax of the topomaps. Would that be worth it?

Maybe, but I'd do the other stuff first.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

@mmagnuski do you perhaps have time to finish this one up? Would be nice to finally have it.

@mmagnuski

Copy link
Copy Markdown
Member

@jona-sassenhagen I should have time next week (starting tomorrow) for some mne stuff, so I can try :)

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

I miss this function every day. Would like to take over and finish this great code, if that's ok with everyone.

@mmagnuski

Copy link
Copy Markdown
Member

@jona-sassenhagen From my side: sure, would be great!

@hubertjb

hubertjb commented Mar 2, 2018

Copy link
Copy Markdown
Contributor Author

@jona-sassenhagen it would be great, thanks!

@jona-sassenhagen jona-sassenhagen mentioned this pull request Mar 3, 2018
3 tasks done
@larsoner larsoner closed this Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants