if l != 0: DesignMat[curr_pos:curr_pos + l, k] = np.arange(1, l + 1) #Off state_X[curr_pos:curr_pos + l] = 0 contrasts[k] = -1 curr_pos = curr_pos + l k = k + 1 l = len(curr[(curr['periods'] == n) & (curr['State'] == 'transi')]) if l != 0: DesignMat[curr_pos:curr_pos + l, k] = np.arange(1, l + 1) #on state_X[curr_pos:curr_pos + l] = 1 contrasts[k] = 1 curr_pos = curr_pos + l k = k + 1 muols = MUOLS(Y=X, X=DesignMat) muols.fit() tvals, pvals, dfs = muols.t_test(contrasts, pval=True, two_tailed=True) T[t, :] = tvals[0] t = t + 1 print t #Store variables ############################################################################# np.save(os.path.join(BASE_PATH, 'toward_on', 'univariate', 'T.npy'), T) #Retreive variables ############################################################################# T = np.load(os.path.join(BASE_PATH, 'toward_on', 'univariate', 'T.npy')) #Second Level analysis
INPUT = os.path.join(BASE_PATH, "results", "Freesurfer", "asd_vs_controls") OUTPUT = os.path.join(INPUT, "univariate_analysis") X = np.load(os.path.join(INPUT, "X.npy")) y = np.load(os.path.join(INPUT, "y.npy")) Z = X[:, :3] Y = X[:, 3:] assert np.sum(mask_arr) == Y.shape[1] DesignMat = np.zeros((Z.shape[0], Z.shape[1] + 1)) # y, intercept, age, sex DesignMat[:, 0] = (y.ravel() - y.ravel().mean()) # y DesignMat[:, 1] = 1 # intercept DesignMat[:, 2] = Z[:, 1] # age DesignMat[:, 3] = Z[:, 2] # sex muols = MUOLS(Y=Y, X=DesignMat) muols.fit() tvals, pvals, dfs = muols.t_test(contrasts=[1, 0, 0, 0], pval=True) np.savez(os.path.join(OUTPUT, "pvals", "pvals.npz"), pvals[0]) np.savez(os.path.join(OUTPUT, "tvals", "tvals.npz"), tvals[0]) np.savez(os.path.join(OUTPUT, "log10pvals", "log10pvals.npz"), -np.log10(pvals[0])) create_texture.create_texture_file(OUTPUT=os.path.join(OUTPUT, "pvals"), TEMPLATE_PATH=TEMPLATE_PATH, MASK_PATH=MASK_PATH, beta_path=os.path.join( OUTPUT, "pvals", "pvals.npz"), penalty_start=3, threshold=False)
X = np.vstack(images) X = X[:, mask_bool.ravel()] X = nilearn.signal.clean(X, detrend=True, standardize=True, confounds=None, low_pass=None, high_pass=None, t_r=3) #mean_X=(X[lab==1].mean(axis=0) + X[0:20,:].mean(axis=0) ) / float(2) #X=X-mean_X X = X - X.mean(axis=0) muols = MUOLS(Y=X, X=DesignMat) muols.fit() contrasts = np.identity((8)) tvals, pvals, dfs = muols.t_test(contrasts, pval=True, two_tailed=True) for m in range(0, 8): T[t, :] = tvals[m] betas[t, :] = muols.coef[m, :] subject[t] = subject_num t = t + 1 print t BASE_PATH = "/neurospin/brainomics/2016_classif_hallu_fmri" y = np.zeros((128)) np.save(os.path.join(BASE_PATH, 'results', 'svm_with_HC', 'T_IMA.npy'), T_IMA)
X = np.hstack([np.random.randn(n, px-1), np.ones((n, 1))]) # X with intercept f = tempfile.mktemp(dir='/volatile') Y = np.random.randn(n, py_info + py_noize) # Causal model: add X on the first py_info variable Y[:, :py_info] += np.dot(X, beta) np.save(f, Y) del Y contrasts = np.identity(X.shape[1]) ## Read data Y as a memory map ############################## Y_memmap = np.load(f + '.npy', mmap_mode='r') # univariate analysis: fit by blocks of 2**26 elements muols = MUOLS(Y=Y_memmap, X=X) time1 = time.time() muols.fit(block=True, max_elements=2 ** 26) time2 = time.time() tvals, pvals, dfs = muols.t_test(contrasts=contrasts, pval=True, two_tailed=True) print time2 - time1 del muols # univariate analysis: fit by blocks of 2**27 elements muols = MUOLS(Y=Y_memmap, X=X) time3 = time.time() muols.fit(block=True, max_elements=2 ** 27) time4 = time.time() tvals, pvals, dfs = muols.t_test(contrasts=contrasts,
""" fsl5.0-randomise -i /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_smallmask/ALLcs.nii.gz -m /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_smallmask/mask.nii.gz -o /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_smallmask/li~1+age+sex -d /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_smallmask/li~1+age+sex_design.mat -t /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_smallmask/li~1+age+sex_contrast.mat -T -n 2000 -C 3 fsl5.0-randomise -i /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres3.5/ALLcs.nii.gz -m /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres3.5/mask.nii.gz -o /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres3.5/li~1+age+sex -d /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres3.5/li~1+age+sex_design.mat -t /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres3.5/li~1+age+sex_contrast.mat -T -n 2000 -C 3.5 fsl5.0-randomise -i /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres4/ALLcs.nii.gz -m /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres4/mask.nii.gz -o /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres4/li~1+age+sex -d /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres4/li~1+age+sex_design.mat -t /neurospin/ciclops/projects/BIPLi7/Clinicaldata/Analysis/Processing_February_2019/Reconstruct_gridding/Processing_quantif/TPI_Analysisoutputs_fsl-thres4/li~1+age+sex_contrast.mat -T -n 2000 -C 4 """ ############################################################################### """ ~/git/scripts/brainomics/image_clusters_analysis_nilearn.py li~1+age+sex_tstat1.nii.gz -o li~1+age+sex_tstat1 --thresh_neg_low 0 --thresh_neg_high 0 --thresh_pos_low 3 --thresh_size 10 """ ############################################################################### ## OLDIES muols = MUOLS(Y=X,X=DesignMat) muols.fit() print('launching the t_test') tvals, pvals_ttest, df1 = muols.t_test(contrasts=[1, 0, 0], pval=True) #%time tvals, pvals_ttest, df1 = muols.t_test(contrasts=[1, 0, 0], pval=True) print('t_test done') print('launching maxT test') #tvals2, pvals_maxT, df2 = muols.t_test_maxT(contrasts=np.array([1, 0, 0]), nperms=1000, two_tailed=False) #print('maxT test done') tvals, pvals_maxT, df3 = muols.t_test_maxT(contrasts=np.array([1, 0, 0]), nperms=1000, two_tailed=True) #♣tvals3, minP, df3 = muols.t_test_minP(contrasts=np.array([1, 0, 0]), nperms=5, two_tailed=True) mhist, bins, patches= plt.hist([pvals_ttest[0,:],pvals_maxT[0,:]], color=['blue','red'], label=['pvals_ttest','pvals_maxT']) mycoefs=np.zeros(mask_arr.shape)
#assert np.sum(mask_arr) == Y.shape[1] print "ok read" ############################################################################# ## BASIC MULM DesignMat = np.zeros((Z.shape[0], Z.shape[1] + 1)) DesignMat[:, 0] = (y.ravel() - y.ravel().mean()) # y DesignMat[:, 1] = 1 # intercept DesignMat[:, 2] = Z[:, 1] # age DesignMat[:, 3] = Z[:, 2] # gender contrasts = np.array([[1, 0, 0, 0]]) from mulm import MUOLS muols = MUOLS(Y=X, X=DesignMat) muols.fit(block=True) print "ok fit" tvals, pvals, dfs = muols.t_test(contrasts=contrasts, pval=True, two_tailed=True) print "ok t_test" del muols arr = np.zeros(mask_arr.shape) arr[mask_arr] = pvals[0] out_im = nib.Nifti1Image(arr, affine=mask_ima.get_affine()) out_im.to_filename(os.path.join(OUTPUT_UNIVARIATE, "pval_adrs.nii.gz")) p_log10 = -np.log10(pvals) arr = np.zeros(mask_arr.shape) arr[mask_arr] = p_log10[0]
f = tempfile.mktemp() Y = np.random.randn(n, py_info + py_noize) # Causal model: add X on the first py_info variable Y[:, :py_info] += np.dot(X, beta) print("Save matrix to disk") np.save(f, Y) del Y contrasts = np.identity(X.shape[1]) ## Read data Y as a memory map ############################## Y_memmap = np.load(f + '.npy', mmap_mode='r') # univariate analysis: fit by blocks of 2**27 elements muols = MUOLS(Y=Y_memmap, X=X) time3 = time.time() muols.fit(block=True, max_elements=2**27) time4 = time.time() tvals, pvals, dfs = muols.t_test(contrasts=contrasts, pval=True, two_tailed=True) print("Mmap by block(max_elements=2 ** 27): ", time4 - time3) del muols # univariate analysis: fit in one go muols = MUOLS(Y=Y_memmap, X=X) time5 = time.time() muols.fit(block=False) time6 = time.time() tvals, pvals, dfs = muols.t_test(contrasts=contrasts,