read_dig_dat() does not work

  • MNE-Python version: 0.23.0
  • operating system: Windows 10

montage = mne.channels.read_dig_dat(os.path.join(dataDir,'SynAmps2_Quik_Cap64.DAT'))

When i used the read_dig_dat() to read the SynAmps2_Quik_Cap64.DAT, this function did not work. I looked over the source code and found that the code handles five columns of data, but Compumedics only provides four columns for Cap64. Could anyone give me some advice on how to solve this problem?

Hello @leosunpsy and welcome to the forum!

Could you share the problematic file so the developers can have a look? Thanks!

Hi, @richard. The SynAmps2_Quik_Cap64.DAT file can be download at here. I do not know how to directly upload files in the forum, please forgive me!
Thanks for your reply.

Thank you! The format of this file is currently not supported by MNE-Python. It would be possible (and not too difficult) to add support for it, but somebody would have to do it. I’d be willing to give it a shot personally, but I’ll be busy for the next couple of days. If you can wait that long, I’d be glad to help.

In the meantime, perhaps you could simply use our built-in 10-20 standard montage? It won’t give you the precise locations, but it might be good enough to move on with your sensor-level analysis? To do that, you can call

raw.set_montage(montage='standard_1020')

Best wishes,

Richard

@richard OK Thanks for your advice.

Hi @richard, I have solved the problem. The modified code of the read_dig_dat function is as follows:

def read_dig_dat(fname):
    r"""Read electrode positions from a ``*.dat`` file.

    .. Warning::
        This function was implemented based on ``*.dat`` files available from
        `Compumedics <https://compumedicsneuroscan.com/scan-acquire-
        configuration-files/>`__ and might not work as expected with novel
        files. If it does not read your files correctly please contact the
        mne-python developers.

    Parameters
    ----------
    fname : path-like
        File from which to read electrode locations.

    Returns
    -------
    montage : DigMontage
        The montage.

    See Also
    --------
    read_dig_captrak
    read_dig_dat
    read_dig_egi
    read_dig_fif
    read_dig_hpts
    read_dig_polhemus_isotrak
    make_dig_montage

    Notes
    -----
    ``*.dat`` files are plain text files and can be inspected and amended with
    a plain text editor.
    """
    from ._standard_montage_utils import _check_dupes_odict
    fname = _check_fname(fname, overwrite='read', must_exist=True)

    with open(fname, 'r') as fid:
        lines = fid.readlines()

    ch_names, poss = list(), list()
    nasion = lpa = rpa = None
    for i, line in enumerate(lines):
        items = line.split()
        if not items:
            continue
        elif not (len(items) == 4 or len(items) == 5):
            raise ValueError(
                "Error reading %s, line %s has unexpected number of entries:\n"
                "%s" % (fname, i, line.rstrip()))
        
        if len(items) == 5:
            num = items[1]
            if num == '67':
                continue  # centroid
            pos = np.array([float(item) for item in items[2:]])
            if num == '78':
                nasion = pos
            elif num == '76':
                lpa = pos
            elif num == '82':
                rpa = pos
            else:
                ch_names.append(items[0])
                poss.append(pos)
        elif len(items) == 4:
            label = items[0]
            if label == 'Centroid':
                continue  # centroid
            pos = np.array([float(item) for item in items[1:]])
            if label == 'Nasion':
                nasion = pos
            elif label == 'Left':
                lpa = pos
            elif label == 'Right':
                rpa = pos
            else:
                ch_names.append(items[0])
                poss.append(pos)            
    
    electrodes = _check_dupes_odict(ch_names, poss)
    return make_dig_montage(electrodes, nasion, lpa, rpa)

Nice! Would you like to try and make a pull request so your improvement can be officially included in the next release of MNE-Python?

@richard I have made a pull request. :v:

1 Like

Thank you! We will review the PR soon. It may take a few small adjustments until it can be merged.

For reference, the PR can be found here: