def create_gif(BASE_PATH): cv = glob.glob(BASE_PATH) for i in range(len(cv)): print("CV : %s" % cv[i]) PATH = cv[i] params = glob.glob(os.path.join(PATH, "0*")) for p in params: p = os.path.basename(p) print(p) snap_path = os.path.join(PATH, p, "conesta_ite_snapshots") os.makedirs(snap_path, exist_ok=True) beta_path = os.path.join(PATH, p, "conesta_ite_beta") os.makedirs(beta_path, exist_ok=True) conesta_ite = sorted(os.listdir(snap_path)) nb_conesta = len(conesta_ite) os.makedirs(os.path.join(PATH, p, "images"), exist_ok=True) i = 1 for ite in conesta_ite: conesta_ite_number = ite[-10:-4] print("........Iterations: " + str(conesta_ite_number) + "........") if not os.path.exists( os.path.join(PATH, p, "images", conesta_ite_number + ".png")): path = os.path.join(snap_path, ite) ite = np.load(path) fista_ite_nb = ite['continuation_ite_nb'][-1] beta = ite["beta"][penalty_start:, :] beta_t, t = array_utils.arr_threshold_from_norm2_ratio( beta[penalty_start:], 0.99) prop_non_zero = float(np.count_nonzero(beta_t)) / float( np.prod(beta.shape)) print("Proportion of non-zeros voxels " + str(prop_non_zero)) arr = np.zeros(mask_bool.shape) arr[mask_bool] = beta.ravel() out_im = nibabel.Nifti1Image(arr, affine=babel_mask.affine) filename = os.path.join( beta_path, "beta_" + conesta_ite_number + ".nii.gz") out_im.to_filename(filename) beta = nibabel.load(filename).get_data() beta_t, t = array_utils.arr_threshold_from_norm2_ratio( beta, .99) title = "CONESTA iterations: " + str( i) + " - FISTA iterations : " + str(fista_ite_nb) nilearn.plotting.plot_stat_map(filename,display_mode='ortho',\ threshold = t,title = title,cut_coords = [0,-15,4],draw_cross = False) plt.text( -43, 0.023, "proportion of non-zero voxels:%.4f" % round(prop_non_zero, 4)) plt.savefig( os.path.join(PATH, p, "images", conesta_ite_number + ".png")) i = i + 1 os.chdir(os.path.join(PATH, p, "images")) cmd_gif = "convert -delay 100 *.png %s.gif" % p os.system(cmd_gif)
def reducer(key, values): values = [values[item].load() for item in values] #FOLD O is a refit on all samples. DOn't take into account in test. values = values[1:] recall_mean_std = np.std([np.mean(precision_recall_fscore_support( item["y_true"].ravel(), item["y_pred"])[1]) for item in values]) \ / np.sqrt(len(values)) recall = [precision_recall_fscore_support(item["y_true"].ravel(), item["y_pred"].ravel(), average=None)[1] \ for item in values] support = [precision_recall_fscore_support(item["y_true"].ravel(), item["y_pred"].ravel(), average=None)[3] \ for item in values] accuracy_std = np.std([((recall[i][0] * support[i][0] + \ recall[i][1] * support[i][1]) \ / (float(support[i][0] + support[i][1]))) \ for i in xrange(len(values))]) \ / np.sqrt(len(values)) y_true = [item["y_true"].ravel() for item in values] y_pred = [item["y_pred"].ravel() for item in values] y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) auc = roc_auc_score(y_true, y_pred) print auc p, r, f, s = precision_recall_fscore_support(y_true, y_pred, average=None) betas = np.hstack([item["beta"] for item in values]).T R = np.corrcoef(betas) beta_cor_mean = np.mean(R[np.triu_indices_from(R, 1)]) success = r * s success = success.astype('int') accuracy = (r[0] * s[0] + r[1] * s[1]) accuracy = accuracy.astype('int') pvalue_accuracy = binom_test(accuracy, s[0] + s[1], p=0.5) import array_utils betas_t = np.vstack([ array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in xrange(betas.shape[0]) ]) scores = OrderedDict() scores['recall_0'] = r[0] scores['recall_1'] = r[1] scores['recall_mean'] = r.mean() scores['recall_mean_std'] = recall_mean_std scores['accuracy'] = accuracy / float(s[0] + s[1]) scores['pvalue_accuracy'] = pvalue_accuracy scores['accuracy_std'] = accuracy_std scores['precision_0'] = p[0] scores['precision_1'] = p[1] scores['precision_mean'] = p.mean() scores['support_0'] = s[0] scores['support_1'] = s[1] scores['beta_cor_mean'] = beta_cor_mean # proportion of non zeros elements in betas matrix over all folds scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas_t)) / \ float(np.prod(betas_t.shape)) print scores['accuracy'] return scores
def scores(key, paths, config): import mapreduce values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] if key == "nestedcv": model = "nestedcv" else: if key.split("_")[0] == "struct" and float(key.split("_")[3]) < 0.001: model = "enet" if key.split("_")[0] == "struct" and float(key.split("_")[3]) > 0.001: model = "enettv" if key.split("_")[0] == "pca": model = "pca" if key.split("_")[0] == "sparse": model = "sparse" if key.split("_")[0] == "graphNet": model = "graphNet" frobenius_train = np.vstack([item["frobenius_train"] for item in values]) frobenius_test = np.vstack([item["frobenius_test"] for item in values]) comp = np.stack([item["components"] for item in values]) #Mean frobenius norm across folds mean_frobenius_train = frobenius_train.mean() mean_frobenius_test = frobenius_test.mean() comp_t = np.zeros(comp.shape) comp_t_non_zero = np.zeros((5,10)) for i in range(comp.shape[0]): for j in range(comp.shape[2]): comp_t[i,:,j] = array_utils.arr_threshold_from_norm2_ratio(comp[i, :,j], .99)[0] comp_t_non_zero[i,j] = float(np.count_nonzero(comp_t[i,:,j]))/float(comp.shape[1]) prop_non_zero_mean = np.mean(comp_t_non_zero[:,1:])#do not count first comp # print(prop_non_zero_mean) print(key) scores = OrderedDict(( ('param_key',key), ('model', model), ('frobenius_test', mean_frobenius_test), ('frob_test_fold0',float(frobenius_test[0])), ('frob_test_fold1',float(frobenius_test[1])), ('frob_test_fold2',float(frobenius_test[2])), ('frob_test_fold3',float(frobenius_test[3])), ('frob_test_fold4',float(frobenius_test[4])), ('frobenius_train', mean_frobenius_train), ('frob_train_fold0',float(frobenius_train[0])), ('frob_train_fold1',float(frobenius_train[1])), ('frob_train_fold2',float(frobenius_train[2])), ('frob_train_fold3',float(frobenius_train[3])), ('frob_train_fold4',float(frobenius_train[4])), ('prop_non_zeros_mean',prop_non_zero_mean))) return scores
def scores(key, paths, config, ret_y=False): import mapreduce print(key) values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] y_true = [item["y_true"].ravel() for item in values] y_pred = [item["y_pred"].ravel() for item in values] y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) prob_pred = [item["proba_pred"].ravel() for item in values] prob_pred = np.concatenate(prob_pred) p, r, f, s = precision_recall_fscore_support(y_true, y_pred, average=None) auc = roc_auc_score(y_true, prob_pred) #area under curve score. betas = np.hstack([item["beta"] for item in values]).T # threshold betas to compute fleiss_kappa and DICE import array_utils betas_t = np.vstack([ array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in range(betas.shape[0]) ]) success = r * s success = success.astype('int') accuracy = (r[0] * s[0] + r[1] * s[1]) accuracy = accuracy.astype('int') prob_class1 = np.count_nonzero(y_true) / float(len(y_true)) pvalue_recall0 = binom_test(success[0], s[0], 1 - prob_class1) pvalue_recall1 = binom_test(success[1], s[1], prob_class1) pvalue_accuracy = binom_test(accuracy, s[0] + s[1], p=0.5) scores = OrderedDict() try: a, l1, l2, tv = [float(par) for par in key.split("_")] scores['a'] = a scores['l1'] = l1 scores['l2'] = l2 scores['tv'] = tv left = float(1 - tv) if left == 0: left = 1. scores['l1_ratio'] = float(l1) / left except: pass scores['recall_mean'] = r.mean() scores['recall_0'] = r[0] scores['recall_1'] = r[1] scores['accuracy'] = accuracy / float(len(y_true)) scores['pvalue_recall_0'] = pvalue_recall0 scores['pvalue_recall_1'] = pvalue_recall1 scores['pvalue_accuracy'] = pvalue_accuracy scores['max_pvalue_recall'] = np.maximum(pvalue_recall0, pvalue_recall1) scores['recall_mean'] = r.mean() scores['auc'] = auc scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas_t)) / \ float(np.prod(betas_t.shape)) scores['param_key'] = key return scores
def scores(key, paths, config): import mapreduce values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] model = "Jenatton" frobenius_train = np.vstack([item["frob_train"] for item in values]) frobenius_test = np.vstack([item["frob_test"] for item in values]) comp = np.dstack([item["V"] for item in values]) print(comp.shape) comp_t = np.zeros(comp.shape) comp_t_non_zero = np.zeros((10,5)) for i in range(comp.shape[1]): for j in range(comp.shape[2]): comp_t[:,i,j] = array_utils.arr_threshold_from_norm2_ratio(comp[:,i,j], .99)[0] #comp_t_non_zero[i,j] = float(np.count_nonzero(comp_t[:,i,j]))/float(comp.shape[0]) #print(np.count_nonzero(comp_t[:,i,j])) comp_t_non_zero[i,j] = float(np.count_nonzero(comp_t[:,i,j]))/float(63966) prop_non_zero_mean = np.mean(comp_t_non_zero[:,:]) print(prop_non_zero_mean) #save mean pariwise across folds for further analysis # dices_mean_path = os.path.join(WD,'dices_mean_%s.npy' %key.split("_")[0]) # if key.split("_")[0] == 'struct_pca' and key.split("_")[2]<1e-3: # dices_mean_path = os.path.join(WD,'dices_mean_%s.npy' %"enet_pca") # # np.save(dices_mean_path,dice_pairwise_values.mean(axis=0)) #Mean frobenius norm across folds mean_frobenius_train = frobenius_train.mean() mean_frobenius_test = frobenius_test.mean() print(key) scores = OrderedDict(( ('param_key',key), ('model', model), ('frobenius_train', mean_frobenius_train), ('frobenius_test', mean_frobenius_test), ('prop_non_zeros_mean',prop_non_zero_mean))) return scores
def scores(key, paths, config, ret_y=False): import mapreduce print(key) values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] y_true = [item["y_true"].ravel() for item in values] y_pred = [item["y_pred"].ravel() for item in values] y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) p, r, f, s = precision_recall_fscore_support(y_true, y_pred, average=None) auc = roc_auc_score(y_true, y_pred) #area under curve score. betas = np.hstack([item["beta"] for item in values]).T # threshold betas to compute fleiss_kappa and DICE import array_utils betas_t = np.vstack([array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in range(betas.shape[0])]) #Compute pvalue success = r * s success = success.astype('int') prob_class1 = np.count_nonzero(y_true) / float(len(y_true)) pvalue_recall0_true_prob = binom_test(success[0], s[0], 1 - prob_class1,alternative = 'greater') pvalue_recall1_true_prob = binom_test(success[1], s[1], prob_class1,alternative = 'greater') pvalue_recall0_unknwon_prob = binom_test(success[0], s[0], 0.5,alternative = 'greater') pvalue_recall1_unknown_prob = binom_test(success[1], s[1], 0.5,alternative = 'greater') pvalue_recall_mean = binom_test(success[0]+success[1], s[0] + s[1], p=0.5,alternative = 'greater') scores = OrderedDict() try: c = float(key[0]) scores['c'] = c except: pass scores['recall_0'] = r[0] scores['recall_1'] = r[1] scores['recall_mean'] = r.mean() scores["auc"] = auc scores['pvalue_recall0_true_prob_one_sided'] = pvalue_recall0_true_prob scores['pvalue_recall1_true_prob_one_sided'] = pvalue_recall1_true_prob scores['pvalue_recall0_unknwon_prob_one_sided'] = pvalue_recall0_unknwon_prob scores['pvalue_recall1_unknown_prob_one_sided'] = pvalue_recall1_unknown_prob scores['pvalue_recall_mean'] = pvalue_recall_mean scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas)) / \ float(np.prod(betas.shape)) scores['param_key'] = key return scores
def scores(key, paths, config): import mapreduce print(key) values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] y_true = [item["y_true"].ravel() for item in values] y_pred = [item["y_pred"].ravel() for item in values] y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( y_true, y_pred) betas = np.hstack([item["beta"] for item in values]).T # threshold betas to compute fleiss_kappa and DICE betas_t = np.vstack([ array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in range(betas.shape[0]) ]) #Compute pvalue scores = OrderedDict() try: a, l1, l2, tv = [float(par) for par in key.split("_")] scores['a'] = a scores['l1'] = l1 scores['l2'] = l2 scores['tv'] = tv left = float(1 - tv) if left == 0: left = 1. scores['l1_ratio'] = float(l1) / left except: pass scores['slope'] = slope scores['intercept'] = intercept scores['r_value'] = r_value scores['p_value'] = p_value scores['std_err'] = std_err scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas_t)) / \ float(np.prod(betas.shape)) scores['param_key'] = key return scores
penalty_start = 3 ################################################################################## #SVM WD = "/neurospin/brainomics/2016_AUSZ/results/VBM/linear_regression/model_selectionCV/\ refit/refit/0.1_0.35_0.35_0.3" beta = np.load(os.path.join(WD, "beta.npz"))['arr_0'][penalty_start:, 0] arr = np.zeros(mask_bool.shape) arr[mask_bool] = beta.ravel() out_im = nibabel.Nifti1Image(arr, affine=babel_mask.get_affine()) filename = os.path.join(WD, "weight_map.nii.gz") out_im.to_filename(filename) beta = nibabel.load(filename).get_data() beta_t, t = array_utils.arr_threshold_from_norm2_ratio(beta, .99) nilearn.plotting.plot_glass_brain(filename, colorbar=True, plot_abs=False, threshold=t) #CONTROLS VS SCZ dir_path = os.path.join(BASE_PATH, "scz_vs_controls") scores_all_svm = pd.read_excel(os.path.join(dir_path, "svm", "results_dCV.xlsx"), sheetname=0) scores_all_enettv = pd.read_excel(os.path.join(dir_path, "enettv_scz_vs_controls", "results_dCV.xlsx"), sheetname=0)
WD = "/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/\ VBM/all_subjects/results/pcatv_scz/vbm_pcatv_all+VIP_scz/results/0/struct_pca_0.1_0.1_0.1" comp = np.load(os.path.join(WD, "components.npz"))['arr_0'] N_COMP = 10 for i in range(comp.shape[1]): arr = np.zeros(mask_bool.shape) arr[mask_bool] = comp[:, i] out_im = nibabel.Nifti1Image(arr, affine=babel_mask.get_affine()) filename = os.path.join(WD, "comp_%s.nii.gz") % (i) out_im.to_filename(filename) comp_data = nibabel.load(filename).get_data() comp_t, t = array_utils.arr_threshold_from_norm2_ratio(comp_data, .99) nilearn.plotting.plot_glass_brain(filename, colorbar=True, plot_abs=False, threshold=t) print(i) print(t) ############################################################################# #CONTROLS only ############################################################################# WD = "/neurospin/brainomics/2016_schizConnect/analysis/NUSDAST/VBM/results/\ pcatv_controls/5_folds_NUDAST_controls/results/0/struct_pca_0.1_0.1_0.1" comp = np.load(os.path.join(WD, "components.npz"))['arr_0']
def scores(key, paths, config, ret_y=False): import mapreduce print(key) values = [mapreduce.OutputCollector(p) for p in paths] values = [item.load() for item in values] recall_mean_std = np.std([ np.mean( precision_recall_fscore_support( item["y_true"].ravel(), item["y_pred"])[1]) for item in values ]) / np.sqrt(len(values)) y_true = [item["y_true"].ravel() for item in values] y_pred = [item["y_pred"].ravel() for item in values] #prob_pred = [item["proba_pred"].ravel() for item in values] y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) #prob_pred = np.concatenate(prob_pred) p, r, f, s = precision_recall_fscore_support(y_true, y_pred, average=None) #auc = roc_auc_score(y_true, prob_pred) #area under curve score. n_ite = None betas = np.hstack([item["beta"] for item in values]).T ## Compute beta similarity measures # Correlation R = np.corrcoef(betas) #print R R = R[np.triu_indices_from(R, 1)] #print R # Fisher z-transformation / average z_bar = np.mean(1. / 2. * np.log((1 + R) / (1 - R))) # bracktransform r_bar = (np.exp(2 * z_bar) - 1) / (np.exp(2 * z_bar) + 1) # threshold betas to compute fleiss_kappa and DICE import array_utils betas_t = np.vstack([ array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in range(betas.shape[0]) ]) #print "--", np.sqrt(np.sum(betas_t ** 2, 1)) / np.sqrt(np.sum(betas ** 2, 1)) # print np.allclose(np.sqrt(np.sum(betas_t ** 2, 1)) / np.sqrt(np.sum(betas ** 2, 1)), [0.99]*5, # rtol=0, atol=1e-02) # # # Compute fleiss kappa statistics # beta_signed = np.sign(betas_t) # # # Paire-wise Dice coeficient # ij = [[i, j] for i in xrange(5) for j in xrange(i+1, 5)] # dices = list() # for idx in ij: # A, B = beta_signed[idx[0], :], beta_signed[idx[1], :] # dices.append(float(np.sum((A == B)[(A != 0) & (B != 0)])) / (np.sum(A != 0) + np.sum(B != 0))) # dice_bar = np.mean(dices) # # dice_bar = 0. scores = OrderedDict() try: a, l1, l2, tv = [float(par) for par in key.split("_")] scores['a'] = a scores['l1'] = l1 scores['l2'] = l2 scores['tv'] = tv left = float(1 - tv) if left == 0: left = 1. scores['l1_ratio'] = float(l1) / left except: pass scores['recall_0'] = r[0] scores['recall_1'] = r[1] scores['recall_mean'] = r.mean() scores['recall_mean_std'] = recall_mean_std scores['precision_0'] = p[0] scores['precision_1'] = p[1] scores['precision_mean'] = p.mean() scores['support_0'] = s[0] scores['support_1'] = s[1] # scores['corr']= corr scores['beta_r'] = str(R) scores['beta_r_bar'] = r_bar #scores['beta_fleiss_kappa'] = fleiss_kappa_stat #scores['beta_dice'] = str(dices) #scores['beta_dice_bar'] = dice_bar scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas_t)) / \ float(np.prod(betas_t.shape)) scores['n_ite'] = n_ite scores['param_key'] = key if ret_y: scores["y_true"], scores["y_pred"] = y_true, y_pred return scores
beta_path = os.path.join(BASE_PATH,p,"conesta_ite_beta") os.makedirs(beta_path, exist_ok=True) conesta_ite = sorted(os.listdir(snap_path)) nb_conesta = len(conesta_ite) i=1 pdf_path = os.path.join(BASE_PATH,p,"weight_map_across_iterations.pdf") pdf = PdfPages(pdf_path) fig = plt.figure(figsize=(11.69, 8.27)) for ite in conesta_ite: path = os.path.join(snap_path,ite) conesta_ite_number = ite[-11:-4] print ("........Iterations: " + str(conesta_ite_number)+"........") ite = np.load(path) fista_ite_nb =ite['continuation_ite_nb'][-1] beta = ite["beta"][penalty_start:,:] beta_t, t = array_utils.arr_threshold_from_norm2_ratio(beta[penalty_start:], 0.99) prop_non_zero = float(np.count_nonzero(beta_t)) / float(np.prod(beta.shape)) print ("Proportion of non-zeros voxels " + str(prop_non_zero)) arr = np.zeros(mask_bool.shape); arr[mask_bool] = beta.ravel() out_im = nibabel.Nifti1Image(arr, affine=babel_mask.get_affine()) filename = os.path.join(beta_path,"beta_"+conesta_ite_number+".nii.gz") out_im.to_filename(filename) beta = nibabel.load(filename).get_data() beta_t,t = array_utils.arr_threshold_from_norm2_ratio(beta, .99) fig.add_subplot(nb_conesta,1,i) title = "CONESTA iterations: " + str(i) + " - FISTA iterations : " + str(fista_ite_nb) nilearn.plotting.plot_glass_brain(filename,colorbar=True,plot_abs=False,threshold = t,title = title) plt.text(-43,0.023,"proportion of non-zero voxels:%.4f" % round(prop_non_zero,4)) pdf.savefig() plt.close(fig)
je_param_path = os.path.join(INPUT_RESULTS_DIR_JE, "results/cv01/all/%s/V.npy") % (best_je_param) components_je[:, :, i] = np.load(je_param_path) enettv_param_path = os.path.join( INPUT_RESULTS_DIR, "results/cv01/all/%s/components.npz") % (best_enettv_param) components_tv[:, :, i] = np.load(enettv_param_path)['arr_0'] print(i) for i in range(10): for j in range(50): components_sparse[:, i, j], t = array_utils.arr_threshold_from_norm2_ratio( components_sparse[:, i, j], .99) components_enet[:, i, j], t = array_utils.arr_threshold_from_norm2_ratio( components_enet[:, i, j], .99) components_tv[:, i, j], t = array_utils.arr_threshold_from_norm2_ratio( components_tv[:, i, j], .99) components_je[:, i, j], t = array_utils.arr_threshold_from_norm2_ratio( components_je[:, i, j], .99) components_gn[:, i, j], t = array_utils.arr_threshold_from_norm2_ratio( components_gn[:, i, j], .99) ################################################################################## #Pairing of components components_tv = identify_comp(components_tv)
def reducer(key, values): output_collectors = values global N_COMP import mapreduce as GLOBAL N_FOLDS = GLOBAL.N_FOLDS components = np.zeros((63966, N_COMP, 5)) frobenius_train = np.zeros((N_COMP, 5)) frobenius_test = np.zeros((1, 5)) evr_train = np.zeros((N_COMP, 5)) evr_test = np.zeros((N_COMP, 5)) l0 = np.zeros((N_COMP, 5)) l1 = np.zeros((N_COMP, 5)) l2 = np.zeros((N_COMP, 5)) tv = np.zeros((N_COMP, 5)) times = np.zeros((1, 5)) # N_FOLDS is the number of true folds (not the number of resamplings) # key : string of intermediary key # load return dict corresponding to mapper ouput. they need to be loaded.] # Avoid taking into account the fold 0 for item in output_collectors: if item != N_FOLDS: values = output_collectors[item + 1].load() components[:, :, item] = values["components"] frobenius_train[:, item] = values["frobenius_train"] frobenius_test[:, item] = values["frobenius_test"] # l0[:,item] = values["l0"] # l1[:,item] = values["l1"] # l2[:,item] = values["l2"] # tv[:,item] = values["tv"] # evr_train[:,item] = values["evr_train"] # evr_test[:,item] = values["evr_test"] # times[:,item] = values["time"] #Solve non-identifiability problem (baseline = first fold) for i in range(1, 5): if np.abs(np.corrcoef( components[:, 0, 0], components[:, 0, i])[0, 1]) < np.abs( np.corrcoef(components[:, 0, 0], components[:, 1, i])[0, 1]): print("components inverted") print(i) temp_comp1 = np.copy(components[:, 1, i]) components[:, 1, i] = components[:, 0, i] components[:, 0, i] = temp_comp1 if np.abs(np.corrcoef( components[:, 1, 0], components[:, 1, i])[0, 1]) < np.abs( np.corrcoef(components[:, 1, 0], components[:, 2, i])[0, 1]): print("components inverted") print(i) temp_comp2 = np.copy(components[:, 2, i]) components[:, 2, i] = components[:, 1, i] components[:, 1, i] = temp_comp2 # Thesholded components (list of tuples (comp, threshold)) thresh_components = np.empty(components.shape) thresholds = np.empty((N_COMP, N_FOLDS)) for l in range(N_FOLDS): for k in range(N_COMP): thresh_comp, t = array_utils.arr_threshold_from_norm2_ratio( components[:, k, l], .99) thresh_components[:, k, l] = thresh_comp thresholds[k, l] = t # Average precision/recall across folds for each component av_frobenius_train = frobenius_train.mean(axis=1) av_frobenius_test = frobenius_test.mean(axis=1) # Align sign of loading vectors to the first fold for each component aligned_thresh_comp = np.copy(thresh_components) REF_FOLD_NUMBER = 0 for k in range(N_COMP): for i in range(N_FOLDS): ref = thresh_components[:, k, REF_FOLD_NUMBER].T if i != REF_FOLD_NUMBER: r = np.corrcoef(thresh_components[:, k, i].T, ref) if r[0, 1] < 0: #print "Reverting comp {k} of fold {i} for model {key}".format(i=i+1, k=k, key=key) aligned_thresh_comp[:, k, i] *= -1 # Compute fleiss_kappa and DICE on thresholded components fleiss_kappas = np.empty(N_COMP) dice_bars = np.empty(N_COMP) dices = np.zeros((10, N_COMP)) for k in range(N_COMP): # One component accross folds thresh_comp = aligned_thresh_comp[:, k, :] fleiss_kappas[k] = metrics.fleiss_kappa(thresh_comp) dice_bars[k], dices[:, k] = metrics.dice_bar(thresh_comp) print(dices.mean(axis=1)) dices_mean_path = os.path.join(OUTPUT_DIR, 'fmri_5folds/results', 'dices_mean_%s.npy' % key[0]) if key[0] == 'struct_pca' and key[2] == 1e-6: dices_mean_path = os.path.join(OUTPUT_DIR, 'fmri_5folds/results', 'dices_mean_%s.npy' % 'enet_pca') print(dices_mean_path) np.save(dices_mean_path, dices.mean(axis=1)) print(key) scores = OrderedDict( (('model', key[0]), ('global_pen', key[1]), ('tv_ratio', key[2]), ('l1_ratio', key[3]), ('frobenius_train', av_frobenius_train[0]), ('frobenius_test', av_frobenius_test[0]), ('kappa_0', fleiss_kappas[0]), ('kappa_1', fleiss_kappas[1]), ('kappa_2', fleiss_kappas[2]), ('kappa_mean', np.mean(fleiss_kappas)), ('dice_bar_0', dice_bars[0]), ('dice_bar_1', dice_bars[1]), ('dice_bar_2', dice_bars[2]), ('dices_mean', dice_bars.mean()), ('time', np.mean(times)), ('evr_test_0', evr_test.mean(axis=1)[0]), ('evr_test_1', evr_test.mean(axis=1)[1]), ('evr_test_2', evr_test.mean(axis=1)[2]), ('evr_test_sum', evr_test.mean(axis=1)[0] + evr_test.mean(axis=1)[1] + evr_test.mean(axis=1)[2]), ('frob_test_fold1', frobenius_test[0][0]), ('frob_test_fold2', frobenius_test[0][1]), ('frob_test_fold3', frobenius_test[0][2]), ('frob_test_fold4', frobenius_test[0][3]), ('frob_test_fold5', frobenius_test[0][4]))) return scores
WD = "/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/NUSDAST/enetall/5cv/refit/refit/enettv_0.01_0.1_0.8" WD = "/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/VIP/enetall/5cv/refit/refit/enettv_0.01_0.1_0.8" beta = np.load(os.path.join(WD,"beta.npz"))['arr_0'][penalty_start:,0] arr = np.zeros(mask_bool.shape); arr[mask_bool] = beta.ravel() out_im = nibabel.Nifti1Image(arr, affine=babel_mask.get_affine()) filename = os.path.join(WD,"weight_map.nii.gz") out_im.to_filename(filename) beta = nibabel.load(filename).get_data() beta_t,t = array_utils.arr_threshold_from_norm2_ratio(beta, .99) nilearn.plotting.plot_stat_map(filename,colorbar=True,draw_cross=False,threshold = "auto",cut_coords=(-1,-13,14),vmax=0.1) beta1= np.load("/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/COBRE/enetall/5cv/refit/refit/enetgn_0.01_0.5_0.8/beta.npz")['arr_0'][penalty_start:] beta2= np.load("/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/NMORPH/enetall/5cv/refit/refit/enettv_0.01_0.1_0.8/beta.npz")['arr_0'][penalty_start:] beta3= np.load("/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/NUSDAST/enetall/5cv/refit/refit/enettv_0.01_0.1_0.8/beta.npz")['arr_0'][penalty_start:] beta4= np.load("/neurospin/brainomics/2016_schizConnect/analysis/all_studies+VIP/VBM/\ all_subjects/results/by_site/VIP/enetall/5cv/refit/refit/enettv_0.01_0.1_0.8/beta.npz")['arr_0'][penalty_start:]
#With all subjects comp1 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis/components_extracted/struct_pca_0.1_0.1_0.1/vol0000.nii.gz' comp2 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis/components_extracted/struct_pca_0.1_0.1_0.1/vol0001.nii.gz' comp3 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis/components_extracted/struct_pca_0.1_0.1_0.1/vol0002.nii.gz' comp4 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis/components_extracted/struct_pca_0.1_0.1_0.1/vol0003.nii.gz' comp5 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis/components_extracted/struct_pca_0.1_0.1_0.1/vol0004.nii.gz' #Without subject 19 comp1 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis_wto_s20/components_extracted/0.1_0.1_0.1/vol0000.nii.gz' comp2 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis_wto_s20/components_extracted/0.1_0.1_0.1/vol0001.nii.gz' comp3 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis_wto_s20/components_extracted/0.1_0.1_0.1/vol0002.nii.gz' comp4 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis_wto_s20/components_extracted/0.1_0.1_0.1/vol0003.nii.gz' comp5 = '/neurospin/brainomics/2016_classif_hallu_fmri_bis/results26/multivariate_analysis/PCA_analysis_wto_s20/components_extracted/0.1_0.1_0.1/vol0004.nii.gz' import array_utils comp_t1, t1 = array_utils.arr_threshold_from_norm2_ratio( nib.load(comp1).get_data(), .99) comp_t2, t2 = array_utils.arr_threshold_from_norm2_ratio( nib.load(comp2).get_data(), .99) comp_t3, t3 = array_utils.arr_threshold_from_norm2_ratio( nib.load(comp3).get_data(), .99) comp_t4, t4 = array_utils.arr_threshold_from_norm2_ratio( nib.load(comp4).get_data(), .99) comp_t5, t5 = array_utils.arr_threshold_from_norm2_ratio( nib.load(comp5).get_data(), .99) import nilearn from nilearn import plotting from nilearn import image nilearn.plotting.plot_glass_brain(comp1, colorbar=True, plot_abs=False,