Skip to content

Improving EDF reading in line with specification #9704

Closed

Description

@adam2392

Describe the new feature or enhancement

In #9694 (comment), I realized that the EDF specification can "encode" the channel type. One can have a label string in EDF be:

EEG Fz, implying that it is the Fz channel of eeg type. However, it's "optional".

For exporting EDF data, I would like to augment the export to encode the channel type, such that if you have channels:

EOG1, ECG1, A1, A2, A3, Fz, Cz, then they would be written to EDF like:

EOG EOG1
ECG ECG1
sEEG A1
sEEG A2
sEEG A3
EEG Fz
EEG Cz

Describe your proposed implementation

First, I would modify these lines:

for idx, ch_name in enumerate(ch_names):
chan_info = {}
chan_info['cal'] = 1.
chan_info['logno'] = idx + 1
chan_info['scanno'] = idx + 1
chan_info['range'] = 1.
chan_info['unit_mul'] = FIFF.FIFF_UNITM_NONE
chan_info['ch_name'] = ch_name
chan_info['unit'] = FIFF.FIFF_UNIT_V
chan_info['coord_frame'] = FIFF.FIFFV_COORD_HEAD
chan_info['coil_type'] = FIFF.FIFFV_COIL_EEG
chan_info['kind'] = FIFF.FIFFV_EEG_CH
chan_info['loc'] = np.zeros(12)
if ch_name in eog or idx in eog or idx - nchan in eog:
chan_info['coil_type'] = FIFF.FIFFV_COIL_NONE
chan_info['kind'] = FIFF.FIFFV_EOG_CH
pick_mask[idx] = False
elif ch_name in misc or idx in misc or idx - nchan in misc:
chan_info['coil_type'] = FIFF.FIFFV_COIL_NONE
chan_info['kind'] = FIFF.FIFFV_MISC_CH
pick_mask[idx] = False
elif idx in stim_channel_idxs:
chan_info['coil_type'] = FIFF.FIFFV_COIL_NONE
chan_info['unit'] = FIFF.FIFF_UNIT_NONE
chan_info['kind'] = FIFF.FIFFV_STIM_CH
pick_mask[idx] = False
chan_info['ch_name'] = ch_name
ch_names[idx] = chan_info['ch_name']
edf_info['units'][idx] = 1
chs.append(chan_info)

to allow for backwards-compatible behavior that also then sees if there is the "channel type" encoded in the label string.

Additional comments

This addition would not "hurt" our codebase, since it really just adds backwards-compatible behavior to allow for additional parsing of the channel type upon reading the EDF file. In addition, it would add more robust exporting. Overall, it is an improvement in our roundtrip of compliant EDF files.

That way I can modify the string of PRs:

See:

Activity

  1. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    If core-devs agree this is desired behavior, then I can add a PR.

  2. larsoner commented on Aug 30, 2021

    @larsoner
    Member

    Has anyone ever encountered such files in the wild? If not, then I wouldn't add support for it until we hit a use case where it's actually needed

  3. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    All my files actually have the EEG (i.e. channel type) prefix in the channel label. I can add a dummy file to misc/testing, but I think maybe one of the EDF files have it too. I can double check.

    Screen Shot 2021-08-30 at 11 54 36 AM

  4. larsoner commented on Aug 30, 2021

    @larsoner
    Member

    Okay sounds justified enough to me! Bonus points if some testing data are aleady formatted that way but even if not I'm happy with you having some files.

    For my edification, what system/conversion routine produced those files with the channel type encoded?

  5. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    I believe the recording system at this center used Nihon Kohden. However, it might've done the conversion through Persyst instead.

    My suspicion is that not "all files" have this behavior as the "string channel label" in EDF is not strictly required to have the channel type. It's just a pattern in the string that is available if need be. I.e. I've seen some files from Nihon Kohden (or Natus) converted that don't have it. :(

    Therefore, I don't think the change will be that bad since it's really just adding an if/else clause capturing the "space" in the string label if it detects there are.

  6. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    Added PR to mne-testing-data: mne-tools/mne-testing-data#88

  7. larsoner commented on Aug 30, 2021

    @larsoner
    Member

    +1 for adding a ch_type mapping for this during reading (and exporting) in MNE-Python from me then. Hopefully we support all channel types EDF does, but any that we don't we should probably map to EEG (?) and issue a warning.

  8. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    Yeah it seems like they map to EEG by default currently, so I'm okay with just leaving that as is.

    Do you think a warning is necessary if that's current behavior? I suppose for a newbie to MNE, having channel "DC01" map to EEG because it doesn't have a channel type wouldn't be expected.

  9. larsoner commented on Aug 30, 2021

    @larsoner
    Member

    Indeed I would make a list of all the channel names that had a type that we didn't know and say we mapped them to EEG at the end, something like:

    bad_map = dict()
    for ci in range(channels):
        if this_ch_type not in ch_type_dict:
            bad_map[ch_name] = this_ch_type
            this_ch_type = 'eeg'
    if len(bad_map):
        bad_map = '\n'.join(f'{ch_name}: {ch_type}' for ch_name, ch_type in bad_map.items()'
        warn(f'Found the following unknown channel type mapping(s), setting the channel type to EEG:\n{bad_map})
    
  10. cbrnr commented on Aug 30, 2021

    @cbrnr
    Contributor

    I have never used EDF files with this naming convention. Can you check the names in https://physionet.org/content/eegmmidb/1.0.0/? I'm OK with deducing the channel type if the names follow this scheme, but I'm -1 on modifying existing labels on export.

  11. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    Re reading it in: Not all files will have it. I showed a screenshot of a file that does. Many files though may just have the channel name. This is just a consequence of different manufacturing systems exporting EDF files differently.

    Re exporting: The standard has this built in, so it wouldn't hurt to have it export if the channel type information is there. One would lose roundtrip ability with 'misc', and 'bio' channels perhaps, but everything else is preserved as intended i.e. the Voltage channels EEG, ECOG, SEEG, etc.

    We wouldn't break anything because by our construction, the EDF file will still be EDF-compliant, and so any downstream software can still read the file.

    wdyt?

  12. cbrnr commented on Aug 30, 2021

    @cbrnr
    Contributor

    Re exporting: The standard has this built in, so it wouldn't hurt to have it export if the channel type information is there. One would lose roundtrip ability with 'misc', and 'bio' channels perhaps, but everything else is preserved as intended i.e. the Voltage channels EEG, ECOG, SEEG, etc.

    Then at least let's have a parameter that controls this behavior.

  13. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    So if you have the following channels:

    {
       A1 = 'eeg',
       A2 = 'eeg',
       ECG1 = 'ecg',
       EOG1 = 'eog',
       LMT1 = 'seeg'
    }
    

    Then a parameter would either let their labels be written as the following in the EDF file?

    A1, A2, ECG1, EOG1, LMT1
    

    or

    EEG A1, EEG A2, ECG ECG1, EOG EOG1, SEEG LMT1
    

    Is there any reason to want the labels be the first way, rather than the second? Mistakes by user?

  14. adam2392 commented on Aug 30, 2021

    @adam2392
    MemberAuthor

    Well I suppose one would want to toggle it yes when exporting from a FIFF file where the original names are:

    EEG A1, EEG A2, ECG ECG1, EOG EOG1, SEEG LMT1
    

    One wouldn't want to accidentally add an extra channel type string in front of each of those.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions