Skip to content

MRG ENH/ BUG Update how ylim parameter is calculated in plot_evoked_topo - #9285

Merged
larsoner merged 8 commits into
mne-tools:mainfrom
ramkpari:ylim-calc
Apr 26, 2021
Merged

larsoner merged 8 commits into
mne-tools:mainfrom
ramkpari:ylim-calc

Conversation

@ramkpari

Copy link
Copy Markdown
Contributor

Reference issue

Fixes #8714.

What does this implement/fix?

Changes the way plot_evoked_topo calculates ylims when the input param for ylim=None. The current way uses an absolute maxima method to calculate the ylim, while the proposed PR uses a maximum/minimum approach to calculate ylim. The MinMax approach was chosen as it seemed to be consistent with the rest of the codebase while still improving the plot.

Other approaches tried were 99.5 percentile and MAD approach which is detailed further along.

Additional information

Using the following code snippet for the plots below.

import numpy as np

import mne
from mne.viz import (plot_evoked_topo)
from mne.datasets import sample
import matplotlib.pyplot as plt
data_path = sample.data_path()

fname = data_path + '/MEG/sample/sample_audvis-ave.fif'

# Reading evoked data
condition = 'Left Auditory'
evoked = mne.read_evokeds(fname, condition=condition, baseline=(None, 0),
                          proj=False)

evoked_good = evoked.copy().pick_types(meg=True, eeg=False,exclude='bads')

# Plot all channels. Except parameter hasn't been merged yet
evoked.info['bads'] = list()
evoked.plot_topo()

# Plot only good channels
evoked_good.plot_topo()

Current PR approach (MinMax), All Channels
MinMax_all

Current PR approach (MinMax), Good Channels
MinMax_good

Current Main branch(AbsMax),All Channels
Current_all

Current Main branch(AbsMax), Good Channels
Current_good

Raw data for the ylims being used in the above plots

# All ylims are in tuple form ([negtyp1,negtyp2],[postyp1,postyp2])
# Default/Current approach , All Channels
([-548.98971293, -302.86675199]), array([548.98971293, 302.86675199])

# Default/Current approach, Good Channels
([-548.98971293, -199.91518048]), array([548.98971293, 199.91518048])

# MixMax approach , All Channels
([-459.40572859, -302.86675199]), array([548.98971293, 265.07541099])

#MinMax approach , Good Channels
([-459.40572859, -163.54697654]), array([548.98971293, 199.91518048])

@ramkpari
ramkpari marked this pull request as ready for review April 12, 2021 18:31
@ramkpari

ramkpari commented Apr 12, 2021 •

Copy link
Copy Markdown
Contributor Author

Data from more Additional approaches that we're considered!

MAD approach, All channels
mad_all

MAD approach, Good channels
mad_good

99.5 percentile approach, All channels
99_5_all

99.5 percentile approach, All channels
99_5_good

Raw data for the ylims used in the above plots


# 99.5 approach All Channels
([-528.93600073, -197.41929987]) ( [528.93600073, 197.41929987])

# 99.5 approach Good Channels
([-528.93600073, -175.08902701]) ( [528.93600073, 175.08902701]

# MAD approach All Channels
([-5.22838768, -1.10960077] ) ( [5.22838768, 1.10960077]

# MAD approach Good Channels
([-5.22838768, -1.08180841]) ( [5.22838768, 1.08180841]

Code used for MAD and 99.5 percentile approaches

# 99.5 % percentile

    a=np.zeros(len(picks),dtype=np.float64)
        for e in evoked:
            i=0
            for t in picks:
                abc = e.data[t]
                temp=[]
                for cd in abc:
                    ad = np.percentile(np.ravel(cd),99.5)
                    temp = np.append(temp, ad)

                a[i]=np.percentile(np.ravel(temp),99.5)
                i=i+1
        ylim_ = (-a, a)
        
# MAD approach

        a=np.zeros(len(picks),dtype=np.float64)
        for e in evoked:
            i=0
            for t in picks:
                abc = e.data[t]
                temp=[]
                for cd in abc:
                    ad = np.median(np.ravel(cd))
                    dd = np.median(np.abs(np.ravel(cd) - ad))
                    temp = np.append(temp, dd)

                tempy=np.median(np.ravel(temp))
                a[i]=np.median(np.abs(np.ravel(temp)- tempy))
                i=i+1
                
        ylim_ = (-a, a)

@ramkpari

ramkpari commented Apr 12, 2021 •

Copy link
Copy Markdown
Contributor Author

Also, I can work on/modify these approaches for other methods if they are better suited!

@jasmainak

jasmainak commented Apr 13, 2021 •

Copy link
Copy Markdown
Member

Looks fair to me. Can you set the PR to MRG and update whats_new when it's good to go for you?

For MAD approach, I would have tried k.MAD as suggested here. MAD is just so you get a robust estimate of the standard deviation. Then you can apply your 3 sigma rule to get the 99.7 percentile. So MAD * 3 * 0.67449 = 2 * MAD. Otherwise, you get those crazy high values like in your plots. Anyhow we probably don't need that amount of sophistication

@ramkpari ramkpari changed the title Update how ylim parameter is calculated in plot_evoked_topo MRG Update how ylim parameter is calculated in plot_evoked_topo Apr 13, 2021
@ramkpari ramkpari changed the title MRG Update how ylim parameter is calculated in plot_evoked_topo MRG ENH/ BUG Update how ylim parameter is calculated in plot_evoked_topo Apr 21, 2021
@larsoner larsoner added this to the 0.23 milestone Apr 23, 2021
@larsoner

Copy link
Copy Markdown
Member

@jasmainak let me know if you're happy and I'll take a look and merge for 0.23

Comment thread mne/viz/topo.py
# find maxima over all evoked data for each channel pick
ymaxes = np.array([max(np.abs(e.data[t]).max() for e in evoked)
# find minima and maxima over all evoked data for each channel pick
ymaxes = np.array([max((e.data[t]).max() for e in evoked)

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.

do you need this list comprehension now? Can't you simply do:

ymaxes = evoked.data.max(axis=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.

I think we still need the list comprehension in the case of two-channel types being plotted (like mag and grad). Otherwise, this would be better.

@jasmainak

Copy link
Copy Markdown
Member

tiny nitpick, otherwise good by me

@ramkpari

Copy link
Copy Markdown
Contributor Author

Also that CI fail seems to be unrelated.

@jasmainak

Copy link
Copy Markdown
Member

+1 for MRG

Comment thread mne/viz/evoked.py Resolved
@larsoner

Copy link
Copy Markdown
Member

I think that by default ylim=None should have a uniform ylim across all channels. It's what most users will expect and how all of our other plotting functions work. I understand that for some datasets (e.g., sEEG) it might make sense to do it on a channel-by-channel basis, but it shouldn't be the default.

Maybe have ylim='individual' or 'independent' for this case, and leave ylim=None to mean "automatically chosen to be the same for all channels" as it was before?

@ramkpari

ramkpari commented Apr 26, 2021 •

Copy link
Copy Markdown
Contributor Author

I think that by default ylim=None should have a uniform ylim across all channels. It's what most users will expect and how all of our other plotting functions work. I understand that for some datasets (e.g., sEEG) it might make sense to do it on a channel-by-channel basis, but it shouldn't be the default.

Maybe have ylim='individual' or 'independent' for this case, and leave ylim=None to mean "automatically chosen to be the same for all channels" as it was before?

The default before this PR was to calculate ylims for each channel type based on the absolute maxima of the maxima of each channel. The current PR only changes it calculate ylims for each channel type based on the maximum and minimum across each channel in the specific type. So plot_evoked_topo has already had this behaviour since almost 0.14 release. The calculated ylim is still the same for all channels in that channel type in a plot.

The only 2-channel type plots being currently allowed are (MAG|GRAD) and (HBO|HBR) and in those two cases, I think it might still be useful to set calculate ylim individually for each channel type. From my testing, we don't allow mixes of 2-channel type plots such as EEG and MAG or HBO and GRAD.

@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.

Oh sorry I misunderstood, I thought it was ylims for each channel independently rather than each channel type independently. LGTM!

@larsoner
larsoner merged commit 36f5dcd into mne-tools:main Apr 26, 2021
@larsoner

Copy link
Copy Markdown
Member

Thanks @ramkpari !

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.

ylim in plot_evoked_topo

4 participants