def _electrodes_tsv(raw, fname, datatype, overwrite=False, verbose=True): """Create an electrodes.tsv file and save it. Parameters ---------- raw : instance of Raw The data as MNE-Python Raw object. fname : str Filename to save the electrodes.tsv to. datatype : str Type of the data recording. Can be ``meg``, ``eeg``, or ``ieeg``. overwrite : bool Defaults to False. Whether to overwrite the existing data in the file. If there is already data for the given `fname` and overwrite is False, an error will be raised. verbose : bool Set verbose output to true or false. """ # create list of channel coordinates and names x, y, z, names = list(), list(), list(), list() for ch in raw.info['chs']: if _check_ch_locs([ch]): x.append(ch['loc'][0]) y.append(ch['loc'][1]) z.append(ch['loc'][2]) else: x.append('n/a') y.append('n/a') z.append('n/a') names.append(ch['ch_name']) # create OrderedDict to write to tsv file if datatype == "ieeg": # XXX: size should be included in the future sizes = ['n/a'] * len(names) data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ('size', sizes), ]) elif datatype == 'eeg': data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ]) else: # pragma: no cover raise RuntimeError("datatype {} not supported.".format(datatype)) # Add impedance values if available, currently only BrainVision: # https://github.com/mne-tools/mne-python/pull/7974 if hasattr(raw, 'impedances'): data['impedance'] = _get_impedances(raw, names) _write_tsv(fname, data, overwrite=overwrite, verbose=verbose)
def _electrodes_tsv(raw, fname, kind, overwrite=False, verbose=True): """Create an electrodes.tsv file and save it. Parameters ---------- raw : instance of Raw The data as MNE-Python Raw object. fname : str Filename to save the electrodes.tsv to. kind : str Type of the data as in ALLOWED_KINDS. For iEEG, requires size. overwrite : bool Defaults to False. Whether to overwrite the existing data in the file. If there is already data for the given `fname` and overwrite is False, an error will be raised. verbose : bool Set verbose output to true or false. """ # create list of channel coordinates and names x, y, z, names = list(), list(), list(), list() for ch in raw.info['chs']: if _check_ch_locs([ch]): x.append(ch['loc'][0]) y.append(ch['loc'][1]) z.append(ch['loc'][2]) else: x.append('n/a') y.append('n/a') z.append('n/a') names.append(ch['ch_name']) # create OrderedDict to write to tsv file if kind == "ieeg": # XXX: size should be included in the future sizes = ['n/a'] * len(names) data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ('size', sizes), ]) elif kind == 'eeg': data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ]) else: # pragma: no cover raise RuntimeError("kind {} not supported.".format(kind)) _write_tsv(fname, data, overwrite=overwrite, verbose=verbose)
def test_check_ch_locs(): """Test _check_ch_locs behavior.""" info = mne.io.read_info(fname_raw) assert _check_ch_locs(info=info) for picks in ([0], [0, 1], None): assert _check_ch_locs(info=info, picks=picks) for ch_type in ('meg', 'mag', 'grad', 'eeg'): assert _check_ch_locs(info=info, ch_type=ch_type) # drop locations for EEG picks_eeg = _picks_to_idx(info=info, picks='eeg') for idx in picks_eeg: info['chs'][idx]['loc'][:3] = np.nan # EEG tests should fail now assert _check_ch_locs(info=info, picks=picks_eeg) is False assert _check_ch_locs(info=info, ch_type='eeg') is False # tests for other (and "all") channels should still pass assert _check_ch_locs(info=info) assert _check_ch_locs(info=info, ch_type='mag')
def _write_electrodes_tsv(raw, fname, datatype, overwrite=False, verbose=True): """Create an electrodes.tsv file and save it. Parameters ---------- raw : mne.io.Raw The data as MNE-Python Raw object. fname : str Filename to save the electrodes.tsv to. datatype : str Type of the data recording. Can be ``meg``, ``eeg``, or ``ieeg``. overwrite : bool Defaults to False. Whether to overwrite the existing data in the file. If there is already data for the given `fname` and overwrite is False, an error will be raised. verbose : bool Set verbose output to true or false. """ # create list of channel coordinates and names x, y, z, names = list(), list(), list(), list() for ch in raw.info['chs']: if _check_ch_locs([ch]): x.append(ch['loc'][0]) y.append(ch['loc'][1]) z.append(ch['loc'][2]) else: x.append('n/a') y.append('n/a') z.append('n/a') names.append(ch['ch_name']) # create OrderedDict to write to tsv file if datatype == "ieeg": # XXX: size should be included in the future sizes = ['n/a'] * len(names) data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ('size', sizes), ]) elif datatype == 'eeg': data = OrderedDict([ ('name', names), ('x', x), ('y', y), ('z', z), ]) else: # pragma: no cover raise RuntimeError("datatype {} not supported.".format(datatype)) # Add impedance values if available, currently only BrainVision: # https://github.com/mne-tools/mne-python/pull/7974 if hasattr(raw, 'impedances'): data['impedance'] = _get_impedances(raw, names) # note that any coordsystem.json file shared within sessions # will be the same across all runs (currently). So # overwrite is set to True always # XXX: improve later when BIDS is updated # check that there already exists a coordsystem.json if Path(fname).exists() and not overwrite: electrodes_tsv = _from_tsv(fname) # cast values to str to make equality check work if any([ list(map(str, vals1)) != list(vals2) for vals1, vals2 in zip(data.values(), electrodes_tsv.values()) ]): raise RuntimeError( f'Trying to write electrodes.tsv, but it already ' f'exists at {fname} and the contents do not match. ' f'You must differentiate this electrodes.tsv file ' f'from the existing one, or set "overwrite" to True.') _write_tsv(fname, data, overwrite=True, verbose=verbose)