def build_auc_design_matrix(subject,
                            cluster,
                            latency,
                            hemi="Lateralized",
                            freq_bands=None,
                            ogl=False):
    from glob import glob
    from pymeg import aggregate_sr as asr
    if not ogl:
        filenames = glob(
            join(inpath, "S%i_*_%s_agg.hdf" % (subject, "stimulus")))
        data = asr.delayed_agg(filenames, hemi=hemi, cluster=cluster)()
    else:
        filenames = glob(
            join(inpath,
                 "ogl/slimdown/ogl_S%i_*_%s_agg.hdf" % (subject, "stimulus")))
        data = asr.delayed_agg(filenames,
                               hemi="Pair",
                               cluster=["%s_LH" % cluster,
                                        "%s_RH" % cluster])().copy()

        data = data.groupby(["hemi", "trial", "freq"]).mean()
        data.head()
        hemi = np.asarray(data.index.get_level_values("hemi")).astype("str")
        trial = np.asarray(data.index.get_level_values("trial")).astype(int)
        freq = np.asarray(data.index.get_level_values("freq")).astype(int)
        # cl = hemi.copy()
        cl = [cluster] * len(hemi)
        index = pd.MultiIndex.from_arrays(
            [hemi, cl, trial, freq],
            names=["hemi", "cluster", "trial", "freq"])
        data.index = index
        data.head()

    data = pd.pivot_table(data, values=latency, index='trial', columns='freq')
    if freq_bands is not None:
        data = data.loc[:, freq_bands[0]:freq_bands[1]].mean(1)
    trial_index = data.index.get_level_values("trial").unique()

    meta = preprocessing.get_meta_for_subject(subject, "stimulus")
    meta.set_index("hash", inplace=True)
    meta = meta.loc[trial_index]
    return data, meta
def apply_across_areas(
    decoder,
    meta,
    filenames,
    clusters,
    decoder,
    hemis=["Pair", "Lateralized", "Averaged"],
    ntasks=-1,
):
    """
    Run a decoder across a set of clusters, time points and hemis.

    Args:
        meta: pd.DataFrame
            Contains the target colum, indexed by trial.
        filenames: list
            Filenames containing source reconstructed aggregates.
        decoder: function
            Function that takes meta data, aggregate data, areas
            and latency as input and returns decoding scores
            as data frame.
        latencies: None or list
            List of latencies to decode across. If None
            all time points in agg will be used.

    """
    set_n_threads(1)
    from multiprocessing import Pool
    from pymeg import aggregate_sr as asr

    areas = clusters.keys()
    args = []
    for area, hemi in product(areas, hemis):
        if hemi == "Pair":
            area = [area + "_RH", area + "_LH"]
        a = (meta, asr.delayed_agg(filenames, hemi=hemi,
                                   cluster=area), decoder, hemi)
        args.append(a)
    print("Processing %i tasks" % (len(args)))
    from contextlib import closing

    scores = []

    with closing(Pool(ntasks, maxtasksperchild=1)) as p:
        sc = p.starmap(_par_apply_decode, args)
        sc = [s for s in sc if s is not None]
    scores.extend(sc)
    scores = pd.concat(scores)

    p.terminate()
    return scores
def get_ortho_midc(subject):
    """
    Compute orthogonalized decoding values for IPS/PostCeS.
    """
    from pymeg import aggregate_sr as asr
    from glob import glob
    inpath = "/home/nwilming/conf_meg/sr_labeled/aggs"
    cluster = "JWG_M1"
    # First load low level averaged stimulus data
    filenames = glob(join(inpath, "S%i_*_%s_agg.hdf" % (subject, "stimulus")))
    data = asr.delayed_agg(filenames,
                           hemi="Lateralized",
                           cluster=[cluster] + ["JWG_IPS_PCeS"])()
    meta = augment_meta(preprocessing.get_meta_for_subject(
        subject, "stimulus"))
    scores = ortho_midc_decoder(meta,
                                data,
                                "JWG_IPS_PCeS",
                                "JWG_M1",
                                latency=1.1)
    scores.loc[:, "subject"] = subject
    return scores