prob_data[..., j + 1] += temp prob_data = prob_data / num prob_data[prob_data < thres] = 0 mpm = np.argmax(prob_data, axis=3) for j in range(len(cls_list)): mpm[mpm == j + 1] = cls_list[j] ## save MPM file #mybase.save2nifti(mpm, header, os.path.join(data_dir, 'mpm.nii.gz')) pred_label = mpm * mask_data * test_data # compute Dice for label_idx in cls_list: P = pred_label == label_idx T = test_label == label_idx dice_val = mymath.dice_coef(T, P) print 'Dice for label %s: %f' % (label_idx, dice_val) if label_idx == 4: temp_ffa_dice.append(dice_val) else: temp_ofa_dice.append(dice_val) # compute consistency of peak location for label_idx in cls_list: P = pred_label == label_idx T = test_label == label_idx pred_zstat = P * zstat_data[..., i] true_zstat = T * zstat_data[..., i] if P.sum() and T.sum(): if np.argmax(pred_zstat) == np.argmax(true_zstat): c = 1
prob_data[..., j+1] += temp prob_data = prob_data / num prob_data[prob_data<thres] = 0 mpm = np.argmax(prob_data, axis=3) for j in range(len(cls_list)): mpm[mpm==j+1] = cls_list[j] ## save MPM file #mybase.save2nifti(mpm, header, os.path.join(data_dir, 'mpm.nii.gz')) pred_label = mpm * mask_data * test_data # compute Dice for label_idx in cls_list: P = pred_label == label_idx T = test_label == label_idx dice_val = mymath.dice_coef(T, P) print 'Dice for label %s: %f'%(label_idx, dice_val) if label_idx == 4: temp_ffa_dice.append(dice_val) else: temp_ofa_dice.append(dice_val) # compute consistency of peak location for label_idx in cls_list: P = pred_label == label_idx T = test_label == label_idx pred_zstat = P * zstat_data[..., i] true_zstat = T * zstat_data[..., i] if P.sum() and T.sum(): if np.argmax(pred_zstat) == np.argmax(true_zstat): c = 1
pred_data = np.around(pred_data) roi_label = [8, 10, 12] for roi in roi_label: out_file = os.path.join(pred_dir, 'perf_' + str(roi) + '.txt') f = open(out_file, 'w') f.write('SID, peak, dice\n') for i in range(len(sessid)): tmp_pred = pred_data[..., i].copy() tmp_pred[tmp_pred!=roi] = 0 tmp_pred[tmp_pred==roi] = 1 tmp_label = label_data[..., i].copy() tmp_label[tmp_label!=roi] = 0 tmp_label[tmp_label==roi] = 1 pred_zstat = tmp_pred * zstat_data[..., i] true_zstat = tmp_label * zstat_data[..., i] if tmp_pred.sum() and tmp_label.sum(): if np.argmax(pred_zstat) == np.argmax(true_zstat): c = 1 else: c = 0 elif tmp_pred.sum() or tmp_label.sum(): c = 0 else: c = 1 dice = mymath.dice_coef(tmp_pred, tmp_label) f.write(','.join([sessid[i], str(c), str(dice)]) + '\n')
pred_data = nib.load(pred_file).get_data() pred_data = np.around(pred_data) roi_label = [8, 10, 12] for roi in roi_label: out_file = os.path.join(pred_dir, 'perf_' + str(roi) + '.txt') f = open(out_file, 'w') f.write('SID, peak, dice\n') for i in range(len(sessid)): tmp_pred = pred_data[..., i].copy() tmp_pred[tmp_pred != roi] = 0 tmp_pred[tmp_pred == roi] = 1 tmp_label = label_data[..., i].copy() tmp_label[tmp_label != roi] = 0 tmp_label[tmp_label == roi] = 1 pred_zstat = tmp_pred * zstat_data[..., i] true_zstat = tmp_label * zstat_data[..., i] if tmp_pred.sum() and tmp_label.sum(): if np.argmax(pred_zstat) == np.argmax(true_zstat): c = 1 else: c = 0 elif tmp_pred.sum() or tmp_label.sum(): c = 0 else: c = 1 dice = mymath.dice_coef(tmp_pred, tmp_label) f.write(','.join([sessid[i], str(c), str(dice)]) + '\n')
'lffa': 4, 'rpcsts': 7, 'lpcsts': 8, 'rpsts': 9, 'lpsts': 10, 'rasts': 11, 'lasts': 12} for roi in roi_dict: roi_idx = roi_dict[roi] roi_dice = [] roi_mask = parcel_data.copy() roi_mask[roi_mask!=roi_idx] = 0 roi_mask[roi_mask==roi_idx] = 1 label = label_data.copy() label[label!=roi_idx] = 0 label[label==roi_idx] = 1 for i in range(act_data.shape[3]): pred_roi = act_data[..., i] * roi_mask dice = mymath.dice_coef(pred_roi, label[..., i]) roi_dice.append(dice) print 'Mean Dice - %s: %s'%(roi, np.array(roi_dice).mean()) out_file = os.path.join(roi + '_dice_output.txt') f = open(out_file, 'w') for line in roi_dice: f.write(str(line) + '\n')
def leave_one_out_test(sid_list, atlas_num, data_dir, class_label, forest_list, classes_list, spatial_ptn, save_nifti=False, sorted=True, single_atlas=False): """ Evaluate classifier performance with leave-one-out scheme. """ # initial output dice value dice = {} for idx in class_label: dice[idx] = [] for i in range(len(sid_list)): print 'Test subject %s' % (sid_list[i]) test_data = get_subj_sample(sid_list[i], data_dir) # mask out voxels which are not activated significantly smp_mask = test_data[..., 0] >= 2.3 #test_x = test_data[smp_mask, 0:4] test_x = test_data[smp_mask, 1:4] test_y = test_data[smp_mask, -1] # define similarity index similarity = [] atlas_idx = [] for j in range(len(sid_list)): if i == j: continue atlas_idx.append(j) #r = normalized_mutual_info_score(spatial_ptn[..., i], # spatial_ptn[..., j]) #r = mymath.mutual_information(spatial_ptn[..., i], # spatial_ptn[..., j]) r = np.corrcoef(spatial_ptn[..., i], spatial_ptn[..., j])[0, 1] similarity.append(r) # sort the similarity sorted_sim_idx = np.argsort(similarity)[::-1] #print similarity #print sorted_sim_idx # label the activation voxels with atlas forests (AFs) tmp_dice = {} for idx in class_label: tmp_dice[idx] = [] for num in atlas_num: print 'atlas number %s' % (num) if sorted: if not single_atlas: selected_atlas = sorted_sim_idx[0:num] else: selected_atlas = sorted_sim_idx[(num - 1):num] else: selected_atlas = np.random.choice(len(sorted_sim_idx), num, replace=False) pred_prob = None for idx in selected_atlas: clf = forest_list[atlas_idx[idx]] prob = clf.predict_proba(test_x) std_prob = np.zeros((prob.shape[0], len(class_label) + 1)) # TODO: construct a std prob table for cls_idx in range(prob.shape[1]): if classes_list[atlas_idx[idx]][cls_idx] == 0: std_prob[..., 0] = prob[..., cls_idx] else: tmp_idx = class_label.index( classes_list[atlas_idx[idx]][cls_idx]) std_prob[..., tmp_idx + 1] = prob[..., cls_idx] new_prob = np.zeros(std_prob.shape) new_prob[range(new_prob.shape[0]), np.argmax(std_prob, axis=1)] = 1 # optional: modulate prediction probability with similarity #new_prob = new_prob * similarity[clf_idx] if not isinstance(pred_prob, np.ndarray): pred_prob = new_prob else: pred_prob += new_prob #print pred_prob.shape tmp_pred_y = np.argmax(pred_prob, axis=1) pred_y = np.zeros(tmp_pred_y.shape) for k in range(1, pred_prob.shape[1]): pred_y[tmp_pred_y == k] = class_label[k - 1] for label_idx in class_label: P = pred_y == label_idx T = test_y == label_idx dice_val = mymath.dice_coef(T, P) print 'Dice for label %s: %f' % (label_idx, dice_val) tmp_dice[label_idx].append(dice_val) # save predicted label as a nifti file if save_nifti: # predicted nifti directory pred_dir = os.path.join(data_dir, str(num) + '_atlas_pred') if not os.path.exists(pred_dir): os.system('mkdir ' + pred_dir) fsl_dir = os.getenv('FSL_DIR') img = nib.load( os.path.join(fsl_dir, 'data', 'standard', 'MNI152_T1_2mm_brain.nii.gz')) # save predicted label header = img.get_header() #coords = test_x[..., 1:4] coords = test_x pred_data = arlib.write2array(coords, pred_y) pred_data = np.around(pred_data) out_file = os.path.join(pred_dir, sid_list[i] + '_pred.nii.gz') mybase.save2nifti(pred_data, header, out_file) for idx in class_label: dice[idx].append(tmp_dice[idx]) for idx in class_label: print 'Mean Dice for label %s: %s' % (idx, np.mean(dice[idx], axis=0)[0]) return dice