def get_ref_head_pos(subject, session, trans, N=-1):
    from mne.transforms import apply_trans
    data = metadata.get_epoch_filename(subject, session, 0, 'stimulus', 'fif')
    data = pymegprepr.load_epochs([data])[0]
    cc = head_loc(data.decimate(10))
    nasion = np.stack([c[0] for c in cc[:N]]).mean(0)
    lpa = np.stack([c[1] for c in cc[:N]]).mean(0)
    rpa = np.stack([c[2] for c in cc[:N]]).mean(0)
    nasion, lpa, rpa = nasion.mean(-1), lpa.mean(-1), rpa.mean(-1)

    return {
        'nasion': apply_trans(trans['t_ctf_dev_dev'], np.array(nasion)),
        'lpa': apply_trans(trans['t_ctf_dev_dev'], np.array(lpa)),
        'rpa': apply_trans(trans['t_ctf_dev_dev'], np.array(rpa))
    }
def get_epochs_for_subject(snum, epoch, sessions=None):
    from itertools import product

    if sessions is None:
        sessions = list(range(4))
    data = []
    meta = get_meta_for_subject(snum, epoch, sessions=sessions)
    for session, block in product(ensure_iter(sessions), list(range(5))):
        filename = metadata.get_epoch_filename(snum, session, block, epoch,
                                               'fif')
        if os.path.isfile(filename):
            data.append(filename)
    #assert len(data) == len(list(ensure_iter(sessions)))*5
    data = pymegprepr.load_epochs(data)
    event_ids = reduce(lambda x, y: x + y,
                       [list(d.event_id.values()) for d in data])
    meta = meta.reset_index().set_index('hash')
    meta = meta.loc[event_ids, :]
    assert len(meta) == sum([d._data.shape[0] for d in data])
    return pymegprepr.concatenate_epochs(data, [meta])
def get_meta_for_subject(snum, epoch, sessions=None):
    if sessions is None:
        sessions = list(range(5))
    metas = []
    for session, block in product(ensure_iter(sessions), list(range(5))):
        filename = metadata.get_epoch_filename(snum, session, block, epoch,
                                               'meta')
        if os.path.isfile(filename + '.msgpack'):
            df = pd.read_msgpack(filename + '.msgpack')
            metas.append(df)
        elif os.path.isfile(filename):
            df = pd.read_hdf(filename)
            metas.append(df)
        else:
            pass
    #meta = pymegprepr.load_meta(metas)
    meta = pd.concat(metas).reset_index()
    cols = [c.decode('utf-8') if type(c) == bytes else c for c in meta.columns]
    meta.columns = cols
    return meta
def get_leadfield(subject, session, head_model="three_layer"):
    """
    Compute leadfield with presets for this subject

    Parameters
    head_model : str, 'three_layer' or 'single_layer'
    """
    raw_filename = metadata.get_raw_filename(subject, session)
    epoch_filename = metadata.get_epoch_filename(subject, session, 0,
                                                 "stimulus", "fif")
    trans = get_trans(subject, session)

    return pymegsr.get_leadfield(
        "S%02i" % subject,
        raw_filename,
        epoch_filename,
        trans,
        conductivity=(0.3, 0.006, 0.3),
        bem_sub_path="bem_ft",
        njobs=4,
    )
def one_block(snum, session, block_in_raw, block_in_experiment):
    '''
    Preprocess a single block and save results.

    Parameters
    ----------
        snum, session : int
    Subject number and session number
        raw : mne.io.Raw object
    Raw data for an entire session of a subject.
        block_in_raw, block_in_experiment : int

    Each succesfull session consists out of five blocks, yet a sessions MEG
    data file sometimes contains more. This happens, for example, when a block
    is restarted. 'block_in_raw' refers to the actual block in a raw file, and
    block_in_experiment to the block in the metadata that block_in_raw should
    be mapped to. block_in_experiment will be used for saving.
    '''

    try:

        art_fname = metadata.get_epoch_filename(snum, session,
                                                block_in_experiment, None,
                                                'artifacts')

        data = empirical.load_data()
        data = empirical.data_cleanup(data)

        filename = metadata.get_raw_filename(snum, session)
        raw = mne.io.read_raw_ctf(filename, system_clock='ignore')
        trials = blocks(raw)
        if not (block_in_raw in np.unique(trials['block'])):
            err_msg = 'Error when processing %i, %i, %i, %i, data file = %s' % (
                snum, session, block_in_raw, block_in_experiment, filename)
            raise RuntimeError(err_msg)

        # Load data and preprocess it.
        logging.info('Loading block of data: %s; block: %i' %
                     (filename, block_in_experiment))
        r, r_id = load_block(raw, trials, block_in_raw)
        r_id['filname'] = filename
        print(('Working on:', filename, block_in_experiment, block_in_raw))
        logging.info('Starting artifact detection')

        r, ants, artdefs = pymegprepr.preprocess_block(r)
        #r.annotations = r
        print('Notch filtering')
        midx = np.where([x.startswith('M') for x in r.ch_names])[0]
        r.notch_filter(np.arange(50, 251, 50), picks=midx)
        logging.info('Aligning meta data')
        meta, timing = get_meta(data, r, snum, block_in_experiment, filename)
        idx = np.isnan(meta.response.values)
        meta = meta.loc[~idx, :]
        timing = timing.loc[~idx, :]
        artdefs['id'] = r_id
        filenames = []
        for epoch, event, (tmin, tmax), (rmin, rmax) in zip(
            ['stimulus', 'response', 'feedback'],
            ['stim_onset_t', 'button_t', 'meg_feedback_t'],
            [(-.75, 1.5), (-1.5, 1), (-1, 1)], [(0, 1), (-1, 0.5),
                                                (-0.5, 0.5)]):

            logging.info('Processing epoch: %s' % epoch)
            try:
                m, s = pymegprepr.get_epoch(
                    r,
                    meta,
                    timing,
                    event=event,
                    epoch_time=(tmin, tmax),
                    reject_time=(rmin, rmax),
                )
            except RuntimeError as e:
                print(e)
                continue

            if len(s) > 0:
                epo_fname = metadata.get_epoch_filename(
                    snum, session, block_in_experiment, epoch, 'fif')
                epo_metaname = metadata.get_epoch_filename(
                    snum, session, block_in_experiment, epoch, 'meta')
                s = s.resample(600, npad='auto')
                s.save(epo_fname)
                m.to_hdf(epo_metaname, 'meta')
                r_id[epoch] = len(s)
                filenames.append(epo_fname)
        pickle.dump(artdefs, open(art_fname, 'wb'), protocol=2)

    except MemoryError:
        print((snum, session, block_in_raw, block_in_experiment))
        raise RuntimeError('MemoryError caught in one block ' + str(snum) +
                           ' ' + str(session) + ' ' + str(block_in_raw) + ' ' +
                           str(block_in_experiment))
    return 'Finished', snum, session, block_in_experiment, filenames