Skip to content

meas_date is guessed during I/O when it is None #7067

Closed

Description

@jasmainak

It seems that the meas_date is not saved / read in correctly. There seems to be a regression of issue #4949 with latest master. Here is the code to reproduce:

import os.path as op

import mne
from mne.datasets import testing

data_path = testing.data_path()
raw_fname = op.join(data_path, 'MEG', 'sample',
                    'sample_audvis_trunc_raw.fif')

raw = mne.io.read_raw_fif(raw_fname)
raw.info['meas_date'] = None
raw.save('temp.fif')

raw2 = mne.io.read_raw_fif('temp.fif')
assert raw2.info['meas_date'] is None

I discovered this while working on mne-tools/mne-bids#290

Activity

  1. agramfort commented on Nov 16, 2019

    @agramfort
    SponsorMember

    it was working with 0.19 ?

  2. jasmainak commented on Nov 16, 2019

    @jasmainak
    MemberAuthor

    Looks like @bloyl is the culprit :)

    Git bisect tells me it stopped working after #5255:

    1aea53a5e7b67ed82fa78c3fcf80d954bdf99a23 is the first bad commit
    commit 1aea53a5e7b67ed82fa78c3fcf80d954bdf99a23
    Author: Luke Bloy <[email protected]>
    Date:   Fri Jul 20 04:20:45 2018 -0400
    
        [MRG+1] CTF support for ds bad channels and bad segments (#5255)
        
        * CTF support for ds bad channels and bad segments
  3. bloyl commented on Nov 17, 2019

    @bloyl
    Contributor

    To be fair the guessing

    mne-python/mne/io/meas_info.py

    Lines 1229 to 1233 in ef7d7c1

    if meas_date is None:
    meas_date = (info['meas_id']['secs'], info['meas_id']['usecs'])
    if np.array_equal(meas_date, DATE_NONE):
    meas_date = None
    info['meas_date'] = meas_date

    is from 2010. :)

    All #5255 does is to ensure the info['meas_id'] round trips through io.

    mne-python/mne/io/meas_info.py

    Lines 1300 to 1302 in ef7d7c1

    # Add measurement id
    if info['meas_id'] is not None:
    write_id(fid, FIFF.FIFF_PARENT_BLOCK_ID, info['meas_id'])

    So for a non anonymized info object (meas_date==None, but meas_id != DATE_NONE) the info doesn't round trip.

    I think there are two reasonable fixes.

    1. Always write a meas_date tag during IO. If info['meas_date'] is None then write DATE_NONE
    2. During Reading don't guessmeas_date from meas_id
  4. jasmainak commented on Nov 17, 2019

    @jasmainak
    MemberAuthor

    I agree with no guessing. Note that while it was broken before, didn't we fix it in #4990? Now looking at the diff I'm not so sure if we added a test to check that or prevent a future regression. Thanks for making the PR to fix it!

  5. bloyl commented on Nov 17, 2019

    @bloyl
    Contributor

    Potential Fixes

    1. During Reading don't guess meas_date from meas_id. This is here.

      mne-python/mne/io/meas_info.py

      Lines 1229 to 1230 in ef7d7c1

      if meas_date is None:
      meas_date = (info['meas_id']['secs'], info['meas_id']['usecs'])

      I don't really know why this behavior was desirable but I'm sure it was/is for something.

    2. Stop writing the meas_id directly in the meas_info tree of the fiff. This would revert to pre [MRG+1] CTF support for ds bad channels and bad segments #5255 behavior. we'd also have to remove some of the testing that was added to [MRG+1] CTF support for ds bad channels and bad segments #5255. This would work because somewhere in here:

      mne-python/mne/io/meas_info.py

      Lines 1211 to 1224 in ef7d7c1

      # Make the most appropriate selection for the measurement id
      if meas_info['parent_id'] is None:
      if meas_info['id'] is None:
      if meas['id'] is None:
      if meas['parent_id'] is None:
      info['meas_id'] = info['file_id']
      else:
      info['meas_id'] = meas['parent_id']
      else:
      info['meas_id'] = meas['id']
      else:
      info['meas_id'] = meas_info['id']
      else:
      info['meas_id'] = meas_info['parent_id']

      the meas_id gets found and set to to DATE_NONE

    3. When writing the meas_id to the meas_info fiff tree, I could set the meas_id = DATA_NONE if meas_date is None.

    4. Do nothing. This issue indicates that meas_date and meas_id are out of sync (ie anonymized not through anonymise_info) and maybe its not surprising to get strange results?

    Finally, I think that only case 1 would preserve round trip IO of both meas_date and meas_id in a situation where meas_date = NONE and meas_id != DATE_NONE. The other options will all guess one from the other in some way.

    ;----
    I should note that removing

    mne-python/mne/io/meas_info.py

    Lines 1229 to 1230 in ef7d7c1

    if meas_date is None:
    meas_date = (info['meas_id']['secs'], info['meas_id']['usecs'])

    pass IO tests.
    So whatever it's original need was it isn't explicitly tested for :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions