Repository navigation
Conversation
larsoner
left a comment
There was a problem hiding this comment.
Otherwise LGTM. Can you update some example to show the new functionality?
|
|
||
| plt.close('all') | ||
|
|
||
| def test_plot_joint(): |
There was a problem hiding this comment.
PEP8 two newlines before function def
| dB=False, colorbar=True, show=True, title=None, axes=None, | ||
| layout=None, yscale='auto', verbose=None): | ||
| layout=None, yscale='auto', verbose=None, | ||
| aggregate='mean'): |
There was a problem hiding this comment.
by convention we always put verbose last
| 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' |
There was a problem hiding this comment.
to avoid breaking argument order, exclude should probably go just before verbose at the end
| 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. |
There was a problem hiding this comment.
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.
| tf_ax = fig.add_subplot(212) | ||
|
|
||
| _, tf_data, tf_times, tf_freqs = \ | ||
| self._plot(picks=picks, exclude=exclude, baseline=baseline, |
There was a problem hiding this comment.
To save lines you can do:
_, tf_data, tf_times, tf_freqs = self.plot(
...)
| 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. |
There was a problem hiding this comment.
might as well put this after head_pos
There was a problem hiding this comment.
great, thanks for the feedback @Eric89GXL!
There was a problem hiding this comment.
ouch. This was missing? Thanks!
|
Looks great! |
| if aggregate is None or len(info['ch_names']) == 1: | ||
| title = info['ch_names'][0] | ||
| else: | ||
| ch_title = ", ".join(info['ch_names'][:6]) |
There was a problem hiding this comment.
Can we refactor the plot_compare_evoked code that does the same thing and only use one private helper function? (_format_ch_names)
There was a problem hiding this comment.
@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?
| timefreqs = [(peakmax_time, peakmax_freq)] | ||
|
|
||
| # prepare axes for topomap | ||
| # slightly convoluted due to colorbar placement and for vertical alignment |
There was a problem hiding this comment.
similarly - does it make sense to create a helper to use by both plot_joint functions?
| 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) |
There was a problem hiding this comment.
Interactive mode already works?
There was a problem hiding this comment.
Good point! No, it doesn't work yet. Maybe for the moment I'll just remove it from the docstring, what do you think?
There was a problem hiding this comment.
Yes. @jaeilepp is the expert on interactive stuff, he's basically a wizard, I have no idea how he does it.
There was a problem hiding this comment.
Yeah. I don't think it'll work with multiple mappables out of the box.
There was a problem hiding this comment.
Although, if it's only for the topomaps, it should be possible to call DraggableColorbar for each mappable with the same cbar.
There was a problem hiding this comment.
I guess it should apply to both the TFR and the topomaps, that probably makes it more complicated, right?
|
|
||
| for idx in range(len(map_ax)): | ||
| time = timefreqs[idx][0] | ||
| freq = timefreqs[idx][1] |
There was a problem hiding this comment.
Can't you do for (time, freq), ax in zip(timefreqs, map_ax)?
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
That's why I'd make timefreqs_average a dict :)
| 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 |
There was a problem hiding this comment.
Can't you do smth like
time_range, freq_range = [t / 2 for t in list(timefreqs_average)]
| 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) |
There was a problem hiding this comment.
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)], |
There was a problem hiding this comment.
Hm. How about using dicts instead of lists?
(I like dicts. They're more directly interpretable, albeit here more verbose.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
what concerns the name, forget about it. I just saw that you inherited this from existing code. Let's keep it for now.
There was a problem hiding this comment.
"you are ad-dict-ed @jona-sassenhagen :)", Denis dixit.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
That last one is pretty cool I think!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| return Line2D((xt, xs), (yt, ys), transform=tf, color='grey', | ||
| linestyle='-', linewidth=1.5, alpha=.66, zorder=1, | ||
| clip_on=False) | ||
|
|
| if y is None: | ||
| (_, ys) = transFigure.transform(sourceax.transAxes.transform([0, 1])) | ||
| else: | ||
| (_, ys) = transFigure.transform(sourceax.transData.transform([0, y])) |
There was a problem hiding this comment.
maybe
(_, ys) = transFigure.transform(
sourceax.transData.transform([0, 1 if y is None else y]))
There was a problem hiding this comment.
Or
y = 1 if y is None else y
(_, ys) = transFigure.transform(sourceax.transData.transform([0, y]))
There was a problem hiding this comment.
Actually, isn't this better:
def _connection_line(x, fig, sourceax, targetax, y=1):
...
_, ys = transFigure.transform(sourceax.transData.transform([0, y]))
| if y is None: | ||
| (_, ys) = transFigure.transform(sourceax.transAxes.transform([0, 1])) | ||
| else: | ||
| (_, ys) = transFigure.transform(sourceax.transData.transform([0, y])) |
There was a problem hiding this comment.
using the brackets here is neither necessary nor desirable. It should be like _, ys = ...
There was a problem hiding this comment.
That's my fault, I wrote the original code like that ...
| (source) and topolots (target). | ||
| """ | ||
| from matplotlib.lines import Line2D | ||
| transFigure = fig.transFigure.inverted() |
| """ | ||
| from matplotlib.lines import Line2D | ||
| transFigure = fig.transFigure.inverted() | ||
| tf = fig.transFigure |
There was a problem hiding this comment.
maybe some more explicit variable name would be better. It's funny that one is transFigure and one is tf.
|
|
||
| # TFR - Topomap joint plot | ||
| timefreqs = [(tfr.times[2], tfr.freqs[1]), | ||
| (tfr.times[1], tfr.freqs[1])] |
There was a problem hiding this comment.
Why 1 and 2? Any reason for not taking the first and the second (index 0 and 1)?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
pick a random number to keep the CIs on their toes :D
| 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) |
There was a problem hiding this comment.
whitespace missing
| 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']) |
There was a problem hiding this comment.
I think it is now safe to write {k: v for k, v in topomap ...}
There was a problem hiding this comment.
Minor thing: do membership tests with sets.
k not in {'axes', 'show', 'colorbar'}
There was a problem hiding this comment.
@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')
There was a problem hiding this comment.
Well a tuple is better than a list.
| 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,): |
|
@hubertjb great work! It seems we're close to finishing this :) |
|
@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 |
|
Can't units be inferred? 'units' seems like something that 1. should be supplied as a default during the call to |
| 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)) |
There was a problem hiding this comment.
... 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 ...
I think this'll save a lot. Becomes even simpler with dicts: ... ok. Maybe this is golfing it. |
| 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) |
There was a problem hiding this comment.
I think you should use show=False, to prevent showing the figure prematurely and call plt_show at the end (like you're doing).
| 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. |
| 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) |
There was a problem hiding this comment.
Although, if it's only for the topomaps, it should be possible to call DraggableColorbar for each mappable with the same cbar.
|
Circle CI crashes into some case where for some reason no data is selected: |
|
@jona-sassenhagen concerning units, it looks like 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? |
|
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 Report
@@ 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 |
|
I'm not sure why Travis failed here... Could that come from the rebase? |
|
Seems unrelated. I restarted the job. |
|
Green. |
|
Cool, thanks @jaeilepp! |
| _, 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, |
There was a problem hiding this comment.
Good catch, I'll change that to False.
|
There's something weird with the yticks. Check this: Not catastrophic, but could be better. Also, is the colorbar scaled correctly? Annnnnnd lastly, for some plots I'm getting truncated colorbar vlims: See what I mean? |
|
@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. |
|
@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. |
|
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. |
|
@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. |
| 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') |
There was a problem hiding this comment.
topomap_args_pass['outlines'] = topomap_args.get("outlines", "skirt")
?
|
I don't get why they're different ... is this maybe because |
|
@jona-sassenhagen |
|
I guess it doesn't make much sense to share the vlims.
|
|
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. |
|
Hm. Can't we have one colorbar, just different vlims? Maybe the tfr will look pale, so be it.
Unless we actually have different unit, eg when the tfr isn't the mean, but the gfp. I'm not sure.
|
|
Ok, so if I understand well:
|
|
I'm not sure :( |
|
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? |
|
@jona-sassenhagen Sorry for the delay! OK, just double-checking before I implement. Here's what I'll do:
Also, I was thinking the arguments |
|
@mmagnuski what do you think of this solution?
Looks sensible to me.
Maybe, but I'd do the other stuff first. |
|
@mmagnuski do you perhaps have time to finish this one up? Would be nice to finally have it. |
|
@jona-sassenhagen I should have time next week (starting tomorrow) for some mne stuff, so I can try :) |
|
I miss this function every day. Would like to take over and finish this great code, if that's ok with everyone. |
|
@jona-sassenhagen From my side: sure, would be great! |
|
@jona-sassenhagen it would be great, thanks! |





See issue #3801
Adding aggregate to plot method in averageTFR class
Added aggregate (mean or rms) and exclude arguments to averageTFR.plot
Set
picksargument to default NoneCreated 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
excludeandaggregatearguments in plot_jointAdded contour argument in plot_tfr_topomap
Moved connection_line method to viz/utils from viz/evoked and added y
argument.
Adding test_joint_plot