Skip to content

apply_baseline then crop #8216

Closed
Closed

Description

@virvw

If your issue is a usage question, please consider asking on our Gitter channel or on our mailing list instead of opening an issue.

Describe the problem

apply_baseline on epochs followed by a crop removes the former command apply_baseline
It is counter-intuitive that these two operations, in that specific order, would be dependent on each other
If crop first then baseline ---> obsviously non sense
If baseline first then cropt ---> why not possible currently?

For many statistical contrasts, one may wish to apply a baseline (e.g. [-500.0, 0.0]) and then crop epochs (e.g. to apply statistical tests highly sensitive to the number of samples and decimation - wink)

Currently, the warning is not strong enough to realize this happens: the sequence of operations applied to the data is essentially not respected and unless one looks at the execution online (aka not run some parallel processing) one really easily misses this is ongoing on.

Describe your solution

Could crop have an option apply_baseline = 'on' / 'off' prior to cropping?

Describe possible alternatives

  1. Have an easy way to mask portions of the signal but this won't help lower the computational problem
  2. bypass apply_baseline but then the info history won't be kept

Activity

  1. larsoner commented on Sep 6, 2020

    @larsoner
    Member

    Are you on latest master? It sounds like this was probably fixed in #7629 due to issue #7628

  2. added this to the 0.21 milestone on Sep 6, 2020
  3. virvw commented on Sep 8, 2020

    @virvw
    ContributorAuthor

    This is my current setup

    conda 4.8.3
    Python 3.8.3
    (base) vv221713@is235208:~$ python -c "import mne; mne.sys_info()"
    Platform: Linux-4.4.0-187-generic-x86_64-with-glibc2.10
    Python: 3.8.3 (default, Jul 2 2020, 16:21:59) [GCC 7.3.0]
    Executable: /home/vv221713/anaconda3/bin/python
    CPU: x86_64: 32 cores
    Memory: 62.5 GB

    mne: 0.20.7
    numpy: 1.18.5 {blas=mkl_rt, lapack=mkl_rt}
    scipy: 1.5.0
    matplotlib: 3.2.2 {backend=Qt5Agg}

    sklearn: 0.23.1
    numba: 0.50.1
    pandas: 1.0.5

    2 hours later: updated to master. But spyder is not functional, doesn't see mne anymore and messing around with conda is not working. Seems I am up to a full re-install, again.

  4. virvw commented on Sep 8, 2020

    @virvw
    ContributorAuthor
    1. OK after simple mne reinstall with pip:
      I confirm that version mne: 0.20.8 does not warn about not applying baseline. However, it does say "baseline off"

    2. However:
      fname_epochs = (epo_dir +"TF_" + c + '_' + sid + "-epo.fif")
      epochs = mne.read_epochs(fname_epochs)
      if bsln == 1:
      epochs.apply_baseline(baseline = (bsln_tmin,bsln_tmax))
      epochs.crop(tmin=lin_tmin, tmax=lin_tmax)

    In[22]: epochs
    Out[22]: <EpochsFIF | 21 events (all good), 0 - 2 sec, baseline off, ~21.6 MB, data loaded,
    '2': 21>

    3)Sanity check:
    when plotting the same epochs w/ and w/o baseline preceding the crop I get two distinct outcomes:
    test_bsln

    So perhaps just adjusting the "baseline off" would be helpful?

  5. larsoner commented on Sep 8, 2020

    @larsoner
    Member

    So perhaps just adjusting the "baseline off" would be helpful?

    Maybe to "baseline off (or unknown if cropped out of interval)" or something?

  6. virvw commented on Sep 8, 2020

    @virvw
    ContributorAuthor

    it should be a known unknown for the user so it should better specify what has been done?
    how about
    "[bsln_tmin:bsln_tmax]" (these values will be located outside the current epoch length so it implies to have been done before cropping)
    or
    " on [bsln_tmin:bsln_tmax]"
    or
    "baseline before crop"
    or
    "bsln then crop"
    or
    "on before crop"
    or
    "on and crop"

  7. larsoner commented on Sep 8, 2020

    @larsoner
    Member

    it should be a known unknown for the user so it should better specify what has been done?

    Once .crop is done, the information is removed from the object, so not sure this is an option

  8. agramfort commented on Sep 8, 2020

    @agramfort
    SponsorMember
  9. removed this from the 0.21 milestone on Sep 9, 2020
  10. hoechenberger commented on Sep 14, 2020

    @hoechenberger
    Member

    Couldn't we simply keep the baseline attribute, even if the baseline period falls outside of the remaining time points after cropping? And in the str or repr of the Epochs / Evoked, we could easily handle this case, and display it sth like this:

    <Epochs |  108 events (all good), 0 - 1.5 sec, baseline [-0.5, 0] sec (not applicable: data was cropped), ~206.4 MB, data loaded,
     '1': 108>
    

    Isn't that what @virvw suggested? It would make it much clearer that

    • baseline-correction was applied at one point
    • but the baseline period was (partially) removed bc the data was cropped

    This leaves no room for interpretation – unlike the current "let's set baseline to None on cropping".

  11. larsoner commented on Sep 14, 2020

    @larsoner
    Member

    That would be nice, and it would avoid us having to do a bunch of verbose='error' to ignore the baseline removed bits. @hoechenberger feel free to try changing the code to do this and let's see if it works. My guess is that it's going to be a challenge and we decided it was easier just to discard the information previously, but I'm not sure.

  12. added this to the 0.21 milestone on Sep 14, 2020
  13. larsoner commented on Sep 14, 2020

    @larsoner
    Member

    I consider this idea a block for 0.21 release since it affects some warnings we now emit in this cycle...

  14. hoechenberger commented on Sep 14, 2020

    @hoechenberger
    Member

    @larsoner I can implement this, but I will be busy until including Friday. So no chance I will get this done before the weekend. Let's push the planned release date by a week?

  15. hoechenberger commented on Sep 15, 2020

    @hoechenberger
    Member

    Note to self: To handle cases where the baseline was specified with an implicit boundary tmin=None or tmax=None, we actually need an attribute to keep track of whether we chopped off part of the baseline during cropping. Epochs._baseline_was_cropped or something. But that would imply an amendment of the FIFF implementation. Need to come up with something better, unless @larsoner believes this wouldn't be too painful. </end of note to self>

  16. 6 remaining items

  17. larsoner commented on Sep 15, 2020

    @larsoner
    Member

    I am also getting errors like this on master:

    >>> evoked.copy().crop(tmin, tmax)
    ValueError: Baseline interval (tmin = -0.05000000074505806) is outside of data range (tmin = 0.3)
    

    i.e., instead of a warning I'm getting an error. So I don't think re-matching our errors is the safest route here -- it's also not robust to future changings in wordings of these errors.

    I'm thinking that we should probably revert #8156 for the release -- it seems to risky to include it at this point. Then we can add it back in or put the solutions described here in instead.

    WDYT @hoechenberger ?

  18. hoechenberger commented on Sep 15, 2020

    @hoechenberger
    Member

    WDYT @hoechenberger ?

    How about just reverting the re-matching part? I actually only included it because @agramfort wasn't too happy about the broad exception handling (catching all ValueErrors) which had been there before.

  19. hoechenberger commented on Sep 15, 2020

    @hoechenberger
    Member

    @larsoner It's these two commits:
    def384d
    2de7acc

  20. larsoner commented on Sep 15, 2020

    @larsoner
    Member

    How about just reverting the re-matching part? I actually only included it because @agramfort wasn't too happy about the broad exception handling (catching all ValueErrors) which had been there before.

    It's a bit more than this, though. This catching thing in particular is buggy, but also more generally:

    1. The PR changed a lot of core code, so it's risky to change right before release (as evidenced by bugs I just now found by trying to use it in practice)
    2. In this issue we talk about trying to at least revert some or all of the warnings anyway by properly tracking baseline in all conditions, so it's possible we will annoy people by forcing them to do a bunch of verbose='error's all over the place for one release

    These are two things that we try to avoid.

  21. hoechenberger commented on Sep 15, 2020

    @hoechenberger
    Member

    Fair enough, let's revert #8156 and try to address these changes ASAP after cutting the release? I'm worried about ending up in rebase hell. 😵

    Probably the changes discussed here should then also be added to the #8156 branch, I can imagine things becoming super messy otherwise, WDYT?

  22. larsoner commented on Sep 15, 2020

    @larsoner
    Member

    It's probably worth starting fresh with making Epochs work with a baseline that's outside the window, then adding the appropriate parts of #8156 on top just by copy-pasting code in a new branch

  23. agramfort commented on Sep 15, 2020

    @agramfort
    SponsorMember
  24. hoechenberger commented on Sep 15, 2020

    @hoechenberger
    Member

    😭

    Go ahead, revert my baby! 😵

  25. modified the milestones: 0.21, 0.22 on Sep 15, 2020
  26. virvw commented on Nov 25, 2020

    @virvw
    ContributorAuthor

    @hoechenberger @larsoner I am bit a lost on the status of that issue. Which version of python can I use to make sure baseline followed by crop will work for now? ty

  27. hoechenberger commented on Nov 26, 2020

    @hoechenberger
    Member

    Hello @virvw,

    the status is that it already works in the latest stable version – the baseline correction is not being removed from the data when you crop. However, the epochs do lose the information as to which baseline had been used, and misleadingly state that the baseline has been "removed". So we're mostly talking about a display issue here, if you will. In #8444 I'm reimagining this behavior, which is not too easy to change because there are numerous of potential side-effects to account for. @larsoner has provided input and ideas multiple times, and I will work on this again (and hopefully finally be able to resolve it) within the next few hours.

  28. larsoner commented on Dec 16, 2020

    @larsoner
    Member

    Closing as this is fixed in master now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions