Beispiel #1
0
def do_R_Crossval(y,yname,files,X,gr = 0):
    from LassoPCR_test import load_data
    from LassoPCR_test import do_PCA
    subject_num = y.shape[0]
    output = Outputsclass(subject_num,y,yname)
    data, idx, img  = load_data(files)
    for train, test in cv.LeaveOneOut(subject_num):
        pca, data_red = do_PCA(data[train])
        output.pca.append(pca)
        data_red_test = pca.transform(data[test])
        desmat_cv = np.hstack((data_red,X[train]))
        desmat_cv = np.array(desmat_cv)
        y_cv = y[train]
        if type(gr) == type(y):
            gr_cv = gr[train]
        else:
            gr_cv = 0
        
        test_vec = np.hstack((data_red_test,X[test]))
    
        test_vec = np.array(test_vec)
        mult_lm = do_Regression(y_cv,desmat_cv,gr_cv)
        output.mult_lm.append(mult_lm)
        output.rsq[test] = np.array(base.summary(mult_lm).rx("r.squared")[0])
        output.adjrsq[test] = np.array(base.summary(mult_lm).rx("adj.r.squared")[0])
        
        # Prediction
        predd = dict()
        for i, vec in enumerate(test_vec[0]):  
            name = "roi%02d"%i
            predd[name] = vec
        if type(gr) == type(y):
            predd["group"] = FV([gr[test]])
        
        preddataf = rob.DataFrame(predd)
        output.prediction[test] = stats.predict(mult_lm, preddataf)[0]
        output.pred_errors[test] = y[test] - output.prediction[test]
        #print "did prediction, error = ", output.pred_errors[test]
    return output
Beispiel #2
0
    X[:, 1] = pdata.classtype - 2
    X[:, 0] = pdata.lsas_pre
    X[:, 2] = pdata.age
    # run this for all the contrasts specified when calling the script

    for con in contrasts:
        condir = os.path.join(outdir, "con%d" % con)
        if not os.path.exists(condir):
            os.mkdir(condir)
        for i, s in enumerate(pdata.subject):
            # the original nii file for each subject
            fname = os.path.join(
                "/mindhive/gablab/sad/PY_STUDY_DIR/Block/volsurf/l1output/social/norm_contrasts/_subject_id_%s/_fwhm_6/wcon_%04d_out_warped.nii"
                % (s, con)
            )
            # in our conX folder in the output directory we want a symbolic link to the original nii files for each subject
            newname = os.path.join(condir, "%s_con%d.nii" % (s, con))
            if not os.path.islink(newname):
                os.symlink(fname, newname)

        confiles = sorted(glob(os.path.join(condir, "*_con%d.nii" % (con))))

        X, idx, img = load_data(confiles)

        S1 = np.cov(X)

        plt.figure()
        plt.pcolor(S1)
        plt.colorbar()
        plt.show()