import click
# from mne import read_ctf
import contextlib
import sys
from mne import find_layout
from  os.path import commonprefix as cprfx
from os.path import split, splitext, exists, join
from os import makedirs
from mne.io import Raw as Raw_fif
from mne.io import read_raw_ctf as Raw_ctf
from scipy.io import savemat
import numpy as np

@contextlib.contextmanager
def nostdout():
    # -- Works both in python2 and python3 -- #
    try:
        from cStringIO import StringIO
    except ImportError:
        from io import StringIO
    # --------------------------------------- #
    save_stdout = sys.stdout 
    sys.stdout = StringIO()
    yield
    sys.stdout = save_stdout


@click.command()
@click.argument('save_path', type=click.Path())
@click.argument('meg_files', nargs=-1)
@click.option('--flat/--no-flat', default=False)
def cli(meg_files, save_path, flat):
    '''Convert fif or ds to .mat format'''
    common_prefix = split(cprfx(meg_files))[0] + '/'
    for meg_file in meg_files:
        # click.echo(meg_file)
        base, ext = splitext(meg_file)
        new_base = base.replace(common_prefix, '')

        if flat:
            new_base = new_base.replace('/','_')

        # click.echo(new_base)
        new_base = join(save_path, new_base) 

        if ext == '.fif':
            with nostdout(): 
                raw = Raw_fif(meg_file, preload=True, add_eeg_ref=False)
        elif ext == '.ds':
            with nostdout(): 
                raw = Raw_ctf(meg_file, preload=True)
        else:
            click.echo('ERROR: UNKNOWN FORMAT FOR {}'.format(meg_file)) 

        meg_raw = raw.pick_types(meg=True, ref_meg=False)
        data, times = meg_raw[:,:]
        ch_names = meg_raw.info['ch_names']
        la = find_layout(meg_raw.info)

        pos = la.pos[:,:2]

        pos_filt = np.array([pos[i,:] for i in range(len(pos)) if la.names[i] in ''.join(raw.info['ch_names'])])
        # click.echo(pos)
        new_path,_ = split(new_base) 

        if not exists(new_path):
            makedirs(new_path)

        savemat(new_base + '.mat', {'data': data, 'times': times, 'chnames': ch_names, 'chxy': pos_filt})

2c6c4d1133ea6c4f95f96e3c699ff6f50b4bed27
<mask token>

<mask token>


@contextlib.contextmanager
def nostdout():
    try:
        from cStringIO import StringIO
    except ImportError:
        from io import StringIO
    save_stdout = sys.stdout
    sys.stdout = StringIO()
    yield
    sys.stdout = save_stdout


<mask token>

<mask token>


@contextlib.contextmanager
def nostdout():
    try:
        from cStringIO import StringIO
    except ImportError:
        from io import StringIO
    save_stdout = sys.stdout
    sys.stdout = StringIO()
    yield
    sys.stdout = save_stdout


@click.command()
@click.argument('save_path', type=click.Path())
@click.argument('meg_files', nargs=-1)
@click.option('--flat/--no-flat', default=False)
def cli(meg_files, save_path, flat):
    """Convert fif or ds to .mat format"""
    common_prefix = split(cprfx(meg_files))[0] + '/'
    for meg_file in meg_files:
        base, ext = splitext(meg_file)
        new_base = base.replace(common_prefix, '')
        if flat:
            new_base = new_base.replace('/', '_')
        new_base = join(save_path, new_base)
        if ext == '.fif':
            with nostdout():
                raw = Raw_fif(meg_file, preload=True, add_eeg_ref=False)
        elif ext == '.ds':
            with nostdout():
                raw = Raw_ctf(meg_file, preload=True)
        else:
            click.echo('ERROR: UNKNOWN FORMAT FOR {}'.format(meg_file))
        meg_raw = raw.pick_types(meg=True, ref_meg=False)
        data, times = meg_raw[:, :]
        ch_names = meg_raw.info['ch_names']
        la = find_layout(meg_raw.info)
        pos = la.pos[:, :2]
        pos_filt = np.array([pos[i, :] for i in range(len(pos)) if la.names
            [i] in ''.join(raw.info['ch_names'])])
        new_path, _ = split(new_base)
        if not exists(new_path):
            makedirs(new_path)
        savemat(new_base + '.mat', {'data': data, 'times': times, 'chnames':
            ch_names, 'chxy': pos_filt})

import click
import contextlib
import sys
from mne import find_layout
from os.path import commonprefix as cprfx
from os.path import split, splitext, exists, join
from os import makedirs
from mne.io import Raw as Raw_fif
from mne.io import read_raw_ctf as Raw_ctf
from scipy.io import savemat
import numpy as np


@contextlib.contextmanager
def nostdout():
    try:
        from cStringIO import StringIO
    except ImportError:
        from io import StringIO
    save_stdout = sys.stdout
    sys.stdout = StringIO()
    yield
    sys.stdout = save_stdout


@click.command()
@click.argument('save_path', type=click.Path())
@click.argument('meg_files', nargs=-1)
@click.option('--flat/--no-flat', default=False)
def cli(meg_files, save_path, flat):
    """Convert fif or ds to .mat format"""
    common_prefix = split(cprfx(meg_files))[0] + '/'
    for meg_file in meg_files:
        base, ext = splitext(meg_file)
        new_base = base.replace(common_prefix, '')
        if flat:
            new_base = new_base.replace('/', '_')
        new_base = join(save_path, new_base)
        if ext == '.fif':
            with nostdout():
                raw = Raw_fif(meg_file, preload=True, add_eeg_ref=False)
        elif ext == '.ds':
            with nostdout():
                raw = Raw_ctf(meg_file, preload=True)
        else:
            click.echo('ERROR: UNKNOWN FORMAT FOR {}'.format(meg_file))
        meg_raw = raw.pick_types(meg=True, ref_meg=False)
        data, times = meg_raw[:, :]
        ch_names = meg_raw.info['ch_names']
        la = find_layout(meg_raw.info)
        pos = la.pos[:, :2]
        pos_filt = np.array([pos[i, :] for i in range(len(pos)) if la.names
            [i] in ''.join(raw.info['ch_names'])])
        new_path, _ = split(new_base)
        if not exists(new_path):
            makedirs(new_path)
        savemat(new_base + '.mat', {'data': data, 'times': times, 'chnames':
            ch_names, 'chxy': pos_filt})

import click
# from mne import read_ctf
import contextlib
import sys
from mne import find_layout
from  os.path import commonprefix as cprfx
from os.path import split, splitext, exists, join
from os import makedirs
from mne.io import Raw as Raw_fif
from mne.io import read_raw_ctf as Raw_ctf
from scipy.io import savemat
import numpy as np

@contextlib.contextmanager
def nostdout():
    # -- Works both in python2 and python3 -- #
    try:
        from cStringIO import StringIO
    except ImportError:
        from io import StringIO
    # --------------------------------------- #
    save_stdout = sys.stdout 
    sys.stdout = StringIO()
    yield
    sys.stdout = save_stdout


@click.command()
@click.argument('save_path', type=click.Path())
@click.argument('meg_files', nargs=-1)
@click.option('--flat/--no-flat', default=False)
def cli(meg_files, save_path, flat):
    '''Convert fif or ds to .mat format'''
    common_prefix = split(cprfx(meg_files))[0] + '/'
    for meg_file in meg_files:
        # click.echo(meg_file)
        base, ext = splitext(meg_file)
        new_base = base.replace(common_prefix, '')

        if flat:
            new_base = new_base.replace('/','_')

        # click.echo(new_base)
        new_base = join(save_path, new_base) 

        if ext == '.fif':
            with nostdout(): 
                raw = Raw_fif(meg_file, preload=True, add_eeg_ref=False)
        elif ext == '.ds':
            with nostdout(): 
                raw = Raw_ctf(meg_file, preload=True)
        else:
            click.echo('ERROR: UNKNOWN FORMAT FOR {}'.format(meg_file)) 

        meg_raw = raw.pick_types(meg=True, ref_meg=False)
        data, times = meg_raw[:,:]
        ch_names = meg_raw.info['ch_names']
        la = find_layout(meg_raw.info)

        pos = la.pos[:,:2]

        pos_filt = np.array([pos[i,:] for i in range(len(pos)) if la.names[i] in ''.join(raw.info['ch_names'])])
        # click.echo(pos)
        new_path,_ = split(new_base) 

        if not exists(new_path):
            makedirs(new_path)

        savemat(new_base + '.mat', {'data': data, 'times': times, 'chnames': ch_names, 'chxy': pos_filt})
