Created
October 28, 2019 11:05
-
-
Save BorisMansencal/35ef825d56abcc57f55128db66490260 to your computer and use it in GitHub Desktop.
dice computation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import nibabel as nii | |
import os | |
import glob | |
import time | |
os.environ["CUDA_VISIBLE_DEVICES"]='0' | |
def mean_dice_withoutbg(y_pred, y_true): | |
acu=0 | |
n=0 | |
list=np.unique(y_true) | |
for i in list: | |
if(i==0): | |
continue #avoid background | |
a=(y_true==i)*1.0 | |
b=(y_pred==i)*1.0 | |
y_int = a[:]*b[:] | |
acu=acu+(2*np.sum(y_int[:]) / (np.sum(a[:]) + np.sum(b[:]))) | |
n=n+1 | |
acu=acu/n | |
return acu | |
def Dice_Native_Space_1mm(img_path, ref_path, result_path): | |
labels_SLANT = [0, 4,11,23,30,31,32,35,36,37,38,39,40,41,44,45,47,48,49,50,51,52,55,56,57,58,59,60,61,62,71,72,73,75,76,100,101,102,103,104,105,106,107,108,109,112,113,114,115,116,117,118,119,120,121,122,123,124,125,128,129,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207] | |
D=[] | |
listT1 = sorted(glob.glob(img_path+"native_*.nii*")) | |
listLAB = sorted(glob.glob(ref_path+"native_*seg.nii*")) | |
listSEG = sorted(glob.glob(result_path+"native_*seg.nii*")) | |
assert(len(listT1) == len(listLAB)) | |
assert(len(listT1) == len(listSEG)) | |
numfiles=len(listLAB) | |
for i in range(0,numfiles): | |
print(" ") | |
print("Image", str(i+1)) | |
T1_filename=listLAB[i].replace("_seg.nii", ".nii") | |
LAB_filename=listLAB[i] | |
SEG_filename=listSEG[i] | |
print(T1_filename) | |
print(LAB_filename) | |
print(SEG_filename) | |
nii.Nifti1Header.quaternion_threshold = -8e-07 | |
LAB_img = nii.load(os.path.join(ref_path, LAB_filename)) | |
LAB=LAB_img.get_data() | |
LAB=LAB.astype('int') | |
#remove partial labels | |
LABb = np.zeros(LAB.shape) | |
for indexlab, lab in enumerate(labels_SLANT): | |
ind=np.where(LAB==lab) | |
LABb[ind] = LAB[ind] | |
print('Removed inconsistent labels : ', list(set(np.unique(LAB)) - set(np.unique(LABb)))) | |
LAB = LABb | |
SEG_img = nii.load(os.path.join(result_path, SEG_filename)) | |
SEG=SEG_img.get_data() | |
SEG=SEG.astype('int') | |
result=mean_dice_withoutbg(SEG, LAB) | |
print("result=",result) | |
D.append(result) | |
# results | |
print("") | |
print("mean_dice=", np.mean(D)) | |
print("median_dice=", np.median(D)) | |
return D | |
img_path = "" #<path to OASIS T1 test images> | |
ref_path = "" #<path ot OASIS test images groundtruth segmentations> | |
result_path = "" #<path to OASIS test images SLANT27 segmentations> | |
natDOASIS = Dice_Native_Space_1mm(img_path, ref_path, result_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment