Beispiel #1
0
def get_mvp_group_roi(root_dir):
    """Get multivoxel activity pattern for each srimulus from each ROI."""
    # directory config
    nii_dir = os.path.join(root_dir, 'nii')
    ppi_dir = os.path.join(root_dir, 'ppi')
    # load rois
    #mask_data = nib.load(os.path.join(ppi_dir, 'cube_rois.nii.gz')).get_data()
    mask_data = nib.load(
        os.path.join(root_dir, 'group-level', 'rois', 'neurosynth',
                     'cube_rois.nii.gz')).get_data()
    roi_num = int(mask_data.max())
    # get scan info from scanlist
    scanlist_file = os.path.join(root_dir, 'doc', 'scanlist.csv')
    [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file)

    for subj in subj_list:
        # get run infor for emo task
        sid = subj.sess_ID
        subj_dir = os.path.join(nii_dir, sid, 'emo')
        # get run index
        if not 'emo' in subj.run_info:
            continue
        [run_idx, par_idx] = subj.getruninfo('emo')
        # var for MVP
        mvp_dict = {}
        for r in range(roi_num):
            mvp_dict['roi_%s' % (r + 1)] = []
        for i in range(10):
            if str(i + 1) in par_idx:
                print 'Run %s' % (i + 1)
                # load cope data
                ipar = par_idx.index(str(i + 1))
                run_dir = os.path.join(subj_dir, '00' + run_idx[ipar])
                print run_dir
                trn_file = os.path.join(run_dir, 'train_merged_cope.nii.gz')
                test_file = os.path.join(run_dir, 'test_merged_cope.nii.gz')
                trn_cope = nib.load(trn_file).get_data()
                test_cope = nib.load(test_file).get_data()
                run_cope = np.concatenate((trn_cope, test_cope), axis=3)
                # XXX: remove mean cope from each trial
                mean_cope = np.mean(run_cope, axis=3, keepdims=True)
                run_cope = run_cope - mean_cope
                # get MVP for each ROI
                for r in range(roi_num):
                    roi_mask = mask_data.copy()
                    roi_mask[roi_mask != (r + 1)] = 0
                    roi_mask[roi_mask == (r + 1)] = 1
                    roi_coord = niroi.get_roi_coord(roi_mask)
                    for j in range(run_cope.shape[3]):
                        vtr = niroi.get_voxel_value(roi_coord, run_cope[...,
                                                                        j])
                        mvp_dict['roi_%s' % (r + 1)].append(vtr.tolist())
        for roi in mvp_dict:
            mvp_dict[roi] = np.array(mvp_dict[roi])
        outfile = r'%s_roi_mvp.mat' % (sid)
        sio.savemat(outfile, mvp_dict)
Beispiel #2
0
def get_roi_mvps(nii_list, trial_seq_list, roi_coord):
    """Get MVPs from each nii file based on trial info."""
    train_x = []
    train_y = []
    test_x = []
    test_y = []
    for i in range(len(nii_list)):
        nii_data = nii_list[i]
        trial_seq = trial_seq_list[i]
        for t in trial_seq:
            if len(trial_seq[t][0])>1:
                tmp = (nii_data[..., trial_seq[t][0][0]] + \
                       nii_data[..., trial_seq[t][0][1]]) / 2
                vtr = niroi.get_voxel_value(roi_coord, tmp)
                test_x.append(vtr.tolist())
                test_y.append(trial_seq[t][1])

            else:
                tmp = nii_data[..., trial_seq[t][0][0]]
                vtr = niroi.get_voxel_value(roi_coord, tmp)
                train_x.append(vtr.tolist())
                train_y.append(trial_seq[t][1])
    return np.array(train_x), np.array(train_y), np.array(test_x), np.array(test_y)
Beispiel #3
0
def get_vxl_trial_rsp(root_dir):
    """Get multivoxel activity pattern for each srimulus
    from whole brain mask.
    """
    # directory config
    nii_dir = os.path.join(root_dir, 'prepro')
    rsp_dir = os.path.join(root_dir, 'workshop', 'trial_rsp', 'whole_brain')
    # load rois
    mask_data = nib.load(
        os.path.join(root_dir, 'group-level', 'rois', 'neurosynth',
                     'cube_rois_r2.nii.gz')).get_data()
    mask_data = mask_data > 0
    # get scan info from scanlist
    scanlist_file = os.path.join(root_dir, 'doc', 'scanlist.csv')
    [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file)

    for subj in subj_list:
        # get run infor for emo task
        sid = subj.sess_ID
        print sid
        subj_dir = os.path.join(nii_dir, sid)
        # get run index
        if not 'emo' in subj.run_info:
            continue
        [run_idx, par_idx] = subj.getruninfo('emo')
        # var for MVP
        for i in range(10):
            if not str(i + 1) in par_idx:
                continue
            print 'Run %s' % (i + 1)
            mvp_data = []
            # load cope data
            ipar = par_idx.index(str(i + 1))
            run_dir = os.path.join(subj_dir, '00' + run_idx[ipar])
            print run_dir
            rsp_file = os.path.join(run_dir, 'mni_sfunc_data_mcf_hp.nii.gz')
            rsp = nib.load(rsp_file).get_data()
            # derive trial-wise response
            trsp = np.zeros((91, 109, 91, 88))
            for t in range(88):
                trsp[..., t] = (rsp[..., 4 * t + 5] + rsp[..., 4 * t + 6]) / 2
            # get MVP of mask
            vxl_coord = niroi.get_roi_coord(mask_data)
            for j in range(trsp.shape[3]):
                vtr = niroi.get_voxel_value(vxl_coord, trsp[..., j])
                mvp_data.append(vtr.tolist())
            outfile = os.path.join(rsp_dir,
                                   '%s_r%s_mvp.npy' % (sid[:2], i + 1))
            np.save(outfile, np.array(mvp_data))
Beispiel #4
0
def get_mvp_group_roi(scanlist_file, mask_file):
    """Get multivoxel activity pattern for each srimulus from each ROI."""
    # get scan info from scanlist
    [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file)
    # directory config
    nii_dir = scan_info['sessdir']
    
    # load rois
    mask_data = nib.load(mask_file).get_data()
    roi_dict = {'rOFA': 1, 'lOFA': 2, 'rFFA': 3, 'lFFA': 4}
 
    #output_file = os.path.join(roi_dir, 'neo_group_roi_mvpa.csv')
    #f = open(output_file, 'wb')
    #f.write('SID,rOFA,lOFA,rFFA,lFFA,rpcSTS,lpcSTS\n')

    for subj in subj_list:
        # get run infor for emo task
        sid = subj.sess_ID
        subj_dir = os.path.join(nii_dir, sid, 'emo')
        # get par index for each emo run
        if not 'emo' in subj.run_info:
            continue
        [run_idx, par_idx] = subj.getruninfo('emo')
        # var for MVP
        mvp_dict = {}
        for roi in roi_dict:
            mvp_dict[roi] = []
        for i in range(len(run_idx)):
            run_dir = os.path.join(subj_dir, '00'+run_idx[i])
            trn_file = os.path.join(run_dir, 'train_merged_cope.nii.gz')
            test_file = os.path.join(run_dir, 'test_merged_cope.nii.gz')
            trn_cope = nib.load(trn_file).get_data()
            test_cope = nib.load(test_file).get_data()
            run_cope = np.concatenate((trn_cope, test_cope), axis=3)
            # XXX: remove mean cope from each trial
            #mean_cope = np.mean(run_cope, axis=3, keepdims=True)
            #run_cope = run_cope - mean_cope
            # get MVP for each ROI
            for roi in roi_dict:
                roi_mask = mask_data.copy()
                roi_mask[roi_mask!=roi_dict[roi]] = 0
                roi_mask[roi_mask==roi_dict[roi]] = 1
                roi_coord = niroi.get_roi_coord(roi_mask)
                for j in range(run_cope.shape[3]):
                    trl_vtr = niroi.get_voxel_value(roi_coord, run_cope[..., j])

        f.write(','.join(temp)+'\n')
        print 'cost %s s'%(time.time() - start_time)
Beispiel #5
0
def get_roi_bold_ts(root_dir, subj, roi):
    """Get one ROI's BOLD time course."""
    # load nii data list
    print 'load nii files ...'
    nii_list = get_subj_nii_list(root_dir, subj)
    # output data shape: #time x #voxel
    bold_ts = np.zeros((3550, roi.sum()))
    c = 0
    roi_coord = niroi.get_roi_coord(roi)
    for i in range(10):
        nii = nii_list[i]
        for j in range(nii.shape[3]):
            vtr = niroi.get_voxel_value(roi_coord, nii[..., j])
            bold_ts[c] = vtr
            c += 1
    return bold_ts
Beispiel #6
0
def get_roi_cope_mvps(cope_list, trial_tag_list, roi_coord):
    """Get MVPs from each nii file based on trial info."""
    train_x = []
    train_y = []
    test_x = []
    test_y = []
    for i in range(len(cope_list)):
        cope_data = cope_list[i]
        trial_tag = trial_tag_list[i]
        for t in range(cope_data.shape[3]):
            vtr = niroi.get_voxel_value(roi_coord, cope_data[..., t])
            if t < 72:
                train_x.append(vtr.tolist())
                train_y.append(trial_tag[t])
            else:
                test_x.append(vtr.tolist())
                test_y.append(trial_tag[t])
    return np.array(train_x), np.array(train_y), np.array(test_x), np.array(
        test_y)