示例#1
0
def load_data(filename):
    ds = mv.gifti_dataset(filename)
    ds.sa.pop('intents')
    ds.sa['subjects'] = [participant] * ds.shape[0]
    ds.fa['node_indices'] = range(n_vertices)
    # z-score features across samples
    mv.zscore(ds, chunks_attr=None)

    return ds
def get_ha_common_data(test_p, mappers, fold_shifted, included, hemi):
    train_p = [x for x in participants if x != test_p]
    print(
        '\nLoading fMRI GIFTI data for HA in test subj space and using {0} as test participant...'.format(test_p))
    train_resp = []
    for run in included:
        avg = []
        for participant in train_p:
            if run == 4:
                resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
                    participant, tr_fmri[run], run, hemi))).samples[4:-5, :]
            else:
                resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
                    participant, tr_fmri[run], run, hemi))).samples[4:-4, :]

            mv.zscore(resp, chunks_attr=None)
            resp = mappers[participant].forward(resp)
            resp = resp[:, cortical_vertices[hemi] == 1]
            mv.zscore(resp, chunks_attr=None)
            avg.append(resp)

        avg = np.mean(avg, axis=0)
        mv.zscore(avg, chunks_attr=None)
        print('train', run, avg.shape)
        train_resp.append(avg)

    if fold_shifted == 4:
        test_resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
            test_p, tr_fmri[fold_shifted], fold_shifted, hemi))).samples[4:-5, :]
    else:
        test_resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
            test_p, tr_fmri[fold_shifted], fold_shifted, hemi))).samples[4:-4, :]

    mv.zscore(test_resp, chunks_attr=None)
    test_resp = mappers[participant].forward(test_resp)
    test_resp = test_resp[:, cortical_vertices[hemi] == 1]
    mv.zscore(test_resp, chunks_attr=None)

    print('test', fold_shifted, test_resp.shape)

    return train_resp, test_resp
示例#3
0
def create_dataset(sub_name, main_dir, task_list, hemi):
    data_set = []
    for task_name in task_list:
        for run_num in range(1,6):
            ds=[]
            gifti_fname = os.path.join(main_dir,'analysis', sub_name,'func', sub_name + '_task-' + task_name + '_run-' + str(run_num) +'_rw-glm.' +hemi+'.coefs.gii')
            ds = mv.gifti_dataset(gifti_fname)

            # order in sub-rid000001_task-beh_run-5_rw-glm.lh.X.xmat.1D
            ds.sa['beh_tax'] = ["bird_eating", "bird_fighting", "bird_running", "bird_swimming",
                               "insect_eating", "insect_fighting", "insect_running", "insect_swimming",
                               "primate_eating","primate_fighting","primate_running","primate_swimming",
                               "reptile_eating","reptile_fighting", "reptile_running", "reptile_swimming",
                               "ungulate_eating","ungulate_fighting","ungulate_running","ungulate_swimming"]
            ds.sa['beh'] = np.tile(['eating','fighting','running','swimming'],5)
            ds.sa['tax'] = np.repeat(['bird','insect','primate', 'reptile', 'ungulate'],4)
            ds.fa['node_indices'] = range(0,ds.shape[1]) # 0 ~ 400000
            data_set.append(ds)

    # http://www.pymvpa.org/tutorial_mappers.html
    within_ds = mv.vstack(data_set)
    return within_ds
示例#4
0
def get_ha_testsubj_data(test_p, mappers, fold_shifted, included, hemi):
    train_p = [x for x in participants if x != test_p]
    print("\n4. hyperalignment test subject data")
    print(
        'Loading fMRI GIFTI data for HA in test subj space and using {0} as test participant...'
        .format(test_p))
    train_resp = []
    for run in included:
        avg = []
        for participant in train_p:
            # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
            # UNCOMMENT LATER -
            # if run == 4:
            #     resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
            #         participant, tr_fmri[run], run, hemi))).samples[4:-5, :]
            # else:
            #     resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
            #         participant, tr_fmri[run], run, hemi))).samples[4:-4, :]
            # # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
            # DELETE LATER -
            if run == 4:
                resp = mv.gifti_dataset(
                    os.path.join(
                        sam_data_dir,
                        '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.
                        format(participant, tr_fmri[run], run,
                               hemi))).samples[4:-5, :]
            else:
                resp = mv.gifti_dataset(
                    os.path.join(
                        sam_data_dir,
                        '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.
                        format(participant, tr_fmri[run], run,
                               hemi))).samples[4:-4, :]
            # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
            mv.zscore(resp, chunks_attr=None)
            resp = mappers[participant].forward(resp)
            mv.zscore(resp, chunks_attr=None)
            resp = mappers[test_p].reverse(resp)
            # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
            # DELETE LATER -
            # resp = resp[:, cortical_vertices[hemi] == 1]

            resp = resp[:, selected_node]
            # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
            mv.zscore(resp, chunks_attr=None)

            avg.append(resp)

        avg = np.mean(avg, axis=0)
        mv.zscore(avg, chunks_attr=None)
        print('train', run, avg.shape)
        train_resp.append(avg)

    # if fold_shifted == 4:
    #     test_resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
    #         test_p, tr_fmri[fold_shifted], fold_shifted, hemi))).samples[4:-5, cortical_vertices[hemi] == 1]
    # else:
    #     test_resp = mv.gifti_dataset(os.path.join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
    #         test_p, tr_fmri[fold_shifted], fold_shifted, hemi))).samples[4:-4, cortical_vertices[hemi] == 1]

    if fold_shifted == 4:
        test_resp = mv.gifti_dataset(
            os.path.join(
                sam_data_dir,
                '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
                    test_p, tr_fmri[fold_shifted], fold_shifted,
                    hemi))).samples[4:-5, selected_node]
    else:
        test_resp = mv.gifti_dataset(
            os.path.join(
                sam_data_dir,
                '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(
                    test_p, tr_fmri[fold_shifted], fold_shifted,
                    hemi))).samples[4:-4, selected_node]

    mv.zscore(test_resp, chunks_attr=None)

    print('test', fold_shifted, test_resp.shape)

    return train_resp, test_resp
prefix = data_path + 'sub-rid' + sub
suffix = hemi + '.coefs.gii'
fn = prefix + '*' + suffix
files = sorted(glob.glob(fn))

# labels for our gifti_dataset
taxonomy = np.repeat(['bird', 'insect', 'primate', 'reptile', 'unduate'], 4)
behavior = np.tile(['eating', 'fighting', 'running', 'swimming'], 5)
conditions = [' '.join((beh, tax)) for beh, tax in zip(behavior, taxonomy)]

# load in all of the data into the dataframe
targets = range(1, 21)
ds = None
for x in range(len(files)):
    chunks = [x + 1] * 20
    d = mv.gifti_dataset(files[x], chunks=chunks, targets=targets)
    d.sa['conditions'] = conditions
    d.sa['taxonomy'] = taxonomy
    d.sa['behavior'] = behavior
    if ds is None:
        ds = d
    else:
        ds = mv.vstack((ds, d))
ds.fa['node_indices'] = range(ds.shape[1])
# zscore all of our samples
mv.zscore(ds, chunks_attr='chunks', dtype='float32')
# load in surgace and get searchlight query
radius = 10
surface = mv.surf.read(join(data_path, '{0}.pial.gii'.format(hemi)))
# this is an arbitrary radius and distance metric!
query = mv.SurfaceQueryEngine(surface, radius, distance_metric='dijkstra')
示例#6
0
print('made our sls')

all_slres = []
for sub in subjs:
    # get all our data files for this subj
    ds = None
    prefix = data_path + 'sub-rid' + sub
    suffix = hemi + '.coefs.gii'
    fn = prefix + '*' + suffix
    files = sorted(glob.glob(fn))
    for x in range(len(files)):
        if x < 5:
            chunks = [x + 1] * 20
        else:
            chunks = [x - 5 + 1] * 20
        d = mv.gifti_dataset(files[x], chunks=chunks, targets=conditions)
        d.sa['conditions'] = conditions
        if ds is None:
            ds = d
        else:
            ds = mv.vstack((ds, d))
    ds.fa['node_indices'] = range(ds.shape[1])
    ds.samples = zscore(ds.samples, axis=1)
    mtgs = mean_group_sample(['conditions'])
    mtds = mtgs(ds)
    slres = sl(mtds)
    slres.samples = np.nan_to_num(slres.samples)
    all_slres.append(slres.samples)

# all_slres has all (190, 40962) RDMs for each subject
# now we need ISCs
示例#7
0
    qe = mv.SurfaceQueryEngine(surf, 20.0, distance_metric='dijkstra')
    print("Finished creating surface-based searchlight query engine")

    # Optional load hyperalignment mappers
    if hyperalign:
        mappers1 = mv.h5load(join(mvpa_dir, 'search_hyper_mappers_life_mask_nofsel_{0}_leftout_1.hdf5'.format(hemi)))
        mappers2 = mv.h5load(join(mvpa_dir, 'search_hyper_mappers_life_mask_nofsel_{0}_leftout_2.hdf5'.format(hemi)))
        mappers3 = mv.h5load(join(mvpa_dir, 'search_hyper_mappers_life_mask_nofsel_{0}_leftout_3.hdf5'.format(hemi)))
        mappers4 = mv.h5load(join(mvpa_dir, 'search_hyper_mappers_life_mask_nofsel_{0}_leftout_4.hdf5'.format(hemi)))
        mappers = [mappers1, mappers2, mappers3, mappers4]
        print("Loaded hyperalignment mappers")

for participant in participants:

    # Load in functional data
    ds1 = mv.gifti_dataset(join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(participant, tr[1], 1, hemi)))
    ds2 = mv.gifti_dataset(join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(participant, tr[2], 2, hemi)))
    ds3 = mv.gifti_dataset(join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(participant, tr[3], 3, hemi)))
    ds4 = mv.gifti_dataset(join(sam_data_dir, '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.format(participant, tr[4], 4, hemi)))

    # Exclude medial wall
    medial_wall = np.where(np.sum(ds1.samples == 0, axis=0) == tr[1])[0].tolist()
    cortical_vertices = np.where(np.sum(ds1.samples == 0, axis=0) < tr[1])[0].tolist()
    assert len(medial_wall) == n_medial[hemi]
    assert len(medial_wall) + len(cortical_vertices) == n_vertices

    for i, ds in enumerate([ds1, ds2, ds3, ds4]):
        print(i)
        ds.samples = ds.samples[]
        if i < 2:
            ses = 0
示例#8
0
                join(
                    mvpa_dir,
                    'search_hyper_mappers_life_mask_nofsel_{0}_leftout_{1}.hdf5'
                    .format(hemi, run)))
        for participant in participants:

            # print(ds.sa['intents'])
            # to_avg = mean_group_feature()
            # averaged_response = to_avg(ds)

            print(
                "loading data for run {0}, hemisphere {1}, participant {2}...".
                format(run, hemi, participant))
            ds = mv.gifti_dataset(
                join(
                    sam_data_dir,
                    '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.
                    format(participant, tr[run], run, hemi)))
            mv.zscore(ds, chunks_attr=None)

            if hyperalign:
                ds = mappers[participant].forward(ds)
                mv.zscore(ds, chunks_attr=None)
            ds.fa['node_indices'] = range(ds.shape[1])

            # n_samples = ds.samples.shape[0]
            #
            # # Exclude medial wall
            # print(np.where(np.sum(ds.samples == 0, axis=0) == n_samples))
            n_samples = ds.samples.shape[0]
            medial_wall = np.where(
                data_dir,
                '{0}-leftout{1}_singlealpha/{2}/{3}/weights.npy'.format(
                    t, run, p, h)))

        # put medial wall back in
        med_wall_ind = np.where(cortical_vertices[h] == 0)[0]
        wt = np.zeros((wt_missing_medial.shape[0],
                       wt_missing_medial.shape[1] + med_wall_ind.shape[0]),
                      dtype=wt_missing_medial.dtype)
        wt[:, cortical_vertices[h] == 1] = wt_missing_medial

        Pstim = get_stim_for_test_fold(run)
        if run == 4:
            Presp = mv.gifti_dataset(
                os.path.join(
                    sam_data_dir,
                    '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.
                    format(p, tr[run], run, h))).samples[4:-14, :]
        else:
            Presp = mv.gifti_dataset(
                os.path.join(
                    sam_data_dir,
                    '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'.
                    format(p, tr[run], run, h))).samples[4:-7, :]

        mv.zscore(Presp, chunks_attr=None)
        forward_resp = mappers[p].forward(Presp)
        mv.zscore(forward_resp, chunks_attr=None)

        print("Loaded stim and resp data. Doing prediction...")
        pred = np.dot(Pstim, wt)
        for hemi in hemispheres:

            runlist = []
            for run in runs:
                avg_stack = np.empty((4, 40962))
                triu_corrs = []
                plist = []
                for participant in participants:
                    #     filenames = []
                    #     filenames = glob.glob(os.path.join(result_dir, align, 'visual', 'bg_actions_agents', 'leftout_run_' + str(run), '*', h,
                    # 'kernel-weights_' + '*' + '_model-visual_align-' + align + '_feature-' + model + '_foldshifted-' + str(run) + '_hemi_' + h + '.npy'))
                    if align == 'aa':
                        if run == 4:
                            resp = mv.gifti_dataset(
                                os.path.join(
                                    sam_data_dir,
                                    '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'
                                    .format(participant, tr_fmri[run], run,
                                            hemi))).samples[4:-5, :]
                        else:
                            resp = mv.gifti_dataset(
                                os.path.join(
                                    sam_data_dir,
                                    '{0}_task-life_acq-{1}vol_run-0{2}.{3}.tproject.gii'
                                    .format(participant, tr_fmri[run], run,
                                            hemi))).samples[4:-4, :]

                    resp = resp[:, cortical_vertices[hemi] == 1]
                    mv.zscore(resp, chunks_attr=None)
                    plist.append(resp)
                    # np.shape(plist) (18, 403, 37471)