Esempio n. 1
0
def hd3dn(all_grp,all_card_gt,all_pred,all_gt,all_pnames,metric_axis,pprint=False,do_hd=0):
    list(filter(lambda a: a != "0.0", all_pnames))
    list(filter(lambda a: a != 0.0, all_pnames))
    _,C = all_card_gt.shape
    unique_patients = torch.unique(all_grp)
    list(filter(lambda a: a != 0.0, unique_patients))
    unique_patients = unique_patients[unique_patients != torch.ones_like(unique_patients)*666]
    unique_patients = [u.item() for u in unique_patients] 
    batch_hd = torch.zeros((len(unique_patients), C))
    for i, p in enumerate(unique_patients):
        try:
            bool_p = [int(re.split('_',re.split('slice',x.item())[1])[0])==p for x in all_pnames]
            data = "whs"
        except:
            bool_p = [int(re.split('_',re.split('Subj_',x.item())[1])[0])==p for x in all_pnames]
            data = "ivd"
        slices_p = all_pnames[bool_p]
        if do_hd >0:
            all_gt_p = all_gt[bool_p,:]
            all_pred_p = all_pred[bool_p,:] 
            sn_p = [int(re.split('_',x)[1]) for x in slices_p]
            ord_p = np.argsort(sn_p)
            label_gt = all_gt_p[ord_p,...]
            label_pred = all_pred_p[ord_p,...]
            hd_3d_var_vec= [None] * C
            for j in range(0,C):
                label_pred_c = numpy.copy(label_pred)
                label_pred_c[label_pred_c!=j]=0
                label_pred_c[label_pred_c==j]=1
                label_gt_c = numpy.copy(label_gt)
                label_gt_c[label_gt!=j]=0
                label_gt_c[label_gt==j]=1
                if len(np.unique(label_pred_c))>1 and len(np.unique(label_gt_c))>1:
                    if data=="whs":
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[0.6,0.44,0.44]).item()
                    elif data=="ivd":
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[1.25,1.25,2]).item()
                else:
                    hd_3d_var_vec[j]=np.NaN
            hd_3d_var=torch.from_numpy(np.asarray(hd_3d_var_vec))# np.nanmean(hd_3d_var_vec)
        dice_3d = (2 * inter_card_p + 1e-8) / ((card_pred_p + card_gt_p)+ 1e-8)
        if pprint and do_hd:
            print(p,hd_3d_var) 
        if do_hd>0:
            batch_hd[i,...] = hd_3d_var

    indices = torch.tensor(metric_axis)
    hd_3d = torch.index_select(batch_hd, 1, indices)

    hd_3d_mean = hd_3d.mean(dim=0)
    hd_3d_mean = torch.round(hd_3d_mean * 10**4) / (10**4)

    hd_3d_sd = hd_3d.std(dim=0)
    hd_3d_sd = torch.round(hd_3d_sd * 10**4) / (10**4)

    print('metric_axis hd 3d',np.round(hd_3d_mean,2), 'mean ',np.round(hd_3d_mean.mean(),2),'std ',np.round(hd_3d_sd,2), 'std mean', np.round(hd_3d_sd.mean(),2))
    [hd_3d, hd_3d_sd] = map_(lambda t: t.mean(), [hd_3d_mean, hd_3d_sd])
    return hd_3d.item(), hd_3d_sd.item()
            #Dice distance
            dice = dc(mask_result, mask_reference)

            #Jacard distance
            jac = jc(mask_result, mask_reference)

            #Haudorff distance
            haus = hd(mask_result,
                      mask_reference,
                      voxelspacing=1.0156,
                      connectivity=1)

            #Hausdorff distance 95
            haus2 = hd95(mask_result,
                         mask_reference,
                         voxelspacing=1.0156,
                         connectivity=1)

            fichier_dice.write(str(dice) + ';')
            fichier_jac.write(str(jac) + ';')
            fichier_haus.write(str(haus) + ';')
            fichier_haus95.write(str(haus2) + ';')

        fichier_dice.write("\n")
        fichier_jac.write("\n")
        fichier_haus.write("\n")
        fichier_haus95.write("\n")

fichier_dice.close()
fichier_jac.close()
fichier_haus.close()
                mdice.append(dice_value)
                print(mpath, ': ', dice_value)

                #other metrics
                A = np.logical_and(img_pred[dice_idx], labels[dice_idx])
                tp = float(A[A > 0].shape[0])
                tn = float(A[A == 0].shape[0])
                B = img_pred[dice_idx] - labels[dice_idx]
                fp = float(B[B > 0].shape[0])
                fn = float(B[B < 0].shape[0])
                mspecificity.append(specificity(tp, tn, fp, fn))
                msensitivity.append(sensitivity(tp, tn, fp, fn))
                #mhausdorff.append(HausdorffDist(img_pred[dice_idx], labels[dice_idx]))
                #mhausdorff.append(directed_hausdorff(img_pred[dice_idx], labels[dice_idx])[0])
                mhausdorff.append(hd95(img_pred[dice_idx], labels[dice_idx]))

                img_pred[dice_idx][img_pred[dice_idx] == 1] = 255
                pred_dir = 'preds_' + ckpt_path[
                    7:] + '_reducedTest/'  #'linknet_spp8/'

                seg_path = pred_dir + os.path.basename(mpath[dice_idx])

                if not os.path.exists(pred_dir):
                    os.mkdir(pred_dir)
                for x in [30, 40, 50, 60, 70, 75, 80, 90, 100]:
                    pred_dir_sub = pred_dir + str(x)
                    if not os.path.exists(pred_dir_sub):
                        pass
                        os.mkdir(pred_dir_sub)
                        print('created', pred_dir_sub)
Esempio n. 4
0
def dice3dn2(all_grp,all_inter_card,all_card_gt,all_card_pred,all_pred,all_gt,all_pnames,metric_axis,pprint=False,do_hd=0,best_epoch_val=0):
    list(filter(lambda a: a != "0.0", all_pnames))
    list(filter(lambda a: a != 0.0, all_pnames))
    _,C = all_card_gt.shape
    unique_patients = torch.unique(all_grp)
    list(filter(lambda a: a != 0.0, unique_patients))
    unique_patients = unique_patients[unique_patients != torch.ones_like(unique_patients)*666]
    unique_patients = [u.item() for u in unique_patients] 
    batch_dice = torch.zeros((len(unique_patients), C))
    batch_hd = torch.zeros((len(unique_patients), C))
    batch_asd = torch.zeros((len(unique_patients), C))
    if do_hd>0:
        all_pred = all_pred.cpu().numpy()
        all_gt = all_gt.cpu().numpy()

    # do DICE   
    for i, p in enumerate(unique_patients):
        inter_card_p = torch.einsum("bc->c", [torch.masked_select(all_inter_card, all_grp == p).reshape((-1, C))])
        card_gt_p= torch.einsum("bc->c", [torch.masked_select(all_card_gt, all_grp == p).reshape((-1, C))])
        card_pred_p= torch.einsum("bc->c", [torch.masked_select(all_card_pred, all_grp == p).reshape((-1, C))])
        dice_3d = (2 * inter_card_p + 1e-8) / ((card_pred_p + card_gt_p)+ 1e-8)
        batch_dice[i,...] = dice_3d
        if pprint:
            print(p,dice_3d.cpu())
    indices = torch.tensor(metric_axis)
    dice_3d = torch.index_select(batch_dice, 1, indices)
    dice_3d_mean = dice_3d.mean(dim=0)
    dice_3d_mean = torch.round(dice_3d_mean * 10**4) / (10**4)
    dice_3d_sd = dice_3d.std(dim=0)
    dice_3d_sd = torch.round(dice_3d_sd * 10**4) / (10**4)

    # do HD
    #if dice_3d_mean.mean()>best_epoch_val:
    if dice_3d_mean.mean()>0:
        for i, p in enumerate(unique_patients):
            try:
                #bool_p = [int(re.split('_',re.split('Case',x.item())[1])[0])==p for x in all_pnames]
                bool_p = [int(re.split('_',re.split('slice',x.item())[1])[0])==p for x in all_pnames]
                #bool_p = [int(re.split('_',re.split('Subj_',x.item())[1])[0])==p for x in all_pnames]
                data = "saml"
            except:
                bool_p = [int(re.split('_',re.split('Case',x.item())[1])[0])==p for x in all_pnames]
                #bool_p = [int(re.split('_',re.split('Subj_',x.item())[1])[0])==p for x in all_pnames]
                data = "ivd"
            slices_p = all_pnames[bool_p]
            #if do_hd >0 or dice_3d_mean.mean()>best_epoch_val:
            if do_hd >0 :
                all_gt_p = all_gt[bool_p,:]
                all_pred_p = all_pred[bool_p,:]
                sn_p = [int(re.split('_',x)[1]) for x in slices_p]
                ord_p = np.argsort(sn_p)
                label_gt = all_gt_p[ord_p,...]
                label_pred = all_pred_p[ord_p,...]
                asd_3d_var_vec = [None] * C
                hd_3d_var_vec= [None] * C
                for j in range(0,C):
                    label_pred_c = numpy.copy(label_pred)
                    label_pred_c[label_pred_c!=j]=0
                    label_pred_c[label_pred_c==j]=1
                    label_gt_c = numpy.copy(label_gt)
                    label_gt_c[label_gt!=j]=0
                    label_gt_c[label_gt==j]=1
                    if len(np.unique(label_pred_c))>1 and len(np.unique(label_gt_c))>1:
                        if data=="saml":
                            asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item() #ASD IN VOXEL TO COMPARE
                            hd_3d_var_vec[j] = asd_3d_var_vec[j]
                        elif data=="ivd":
                            hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[1.25,1.25,2]).item()
                            asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item() #ASD IN VOXEL TO COMPARE TO PNP PAPER
                    else:
                        hd_3d_var_vec[j]=np.NaN
                        asd_3d_var_vec[j] = np.NaN
                hd_3d_var=torch.from_numpy(np.asarray(hd_3d_var_vec))# np.nanmean(hd_3d_var_vec)
                asd_3d_var=torch.from_numpy(np.asarray(asd_3d_var_vec))# np.nanmean(hd_3d_var_vec)
            if pprint and do_hd:
                print(p,asd_3d_var)
            if do_hd>0:
                batch_hd[i,...] = hd_3d_var
                batch_asd[i,...] = asd_3d_var

    hd_3d = torch.index_select(batch_hd, 1, indices)
    asd_3d = torch.index_select(batch_asd, 1, indices)
    hd_3d_mean = hd_3d.mean(dim=0)
    asd_3d_mean = torch.from_numpy(np.nanmean(asd_3d, axis=0))
    hd_3d_mean = torch.round(hd_3d_mean * 10**4) / (10**4)
    asd_3d_mean = torch.round(asd_3d_mean * 10**4) / (10**4)
    hd_3d_sd = hd_3d.std(dim=0)
    asd_3d_sd = asd_3d.std(dim=0)
    hd_3d_sd = torch.round(hd_3d_sd * 10**4) / (10**4)
    asd_3d_sd = torch.round(asd_3d_sd * 10**4) / (10**4)
    asd_3d_mean = asd_3d_mean.cpu().numpy()

    #if dice_3d_mean.mean()<best_epoch_val:
    #if dice_3d_mean.mean()<0:
    #    print('dice 3d',np.round(dice_3d_mean*100,2), 'mean ',np.round(100*dice_3d_mean.mean(),2),'std ',np.round(100*dice_3d_sd,2), 'std mean', np.round(100*dice_3d_sd.mean(),2))
    #else:
    print('dice 3d',np.round(dice_3d_mean*100,2), 'mean ',np.round(100*dice_3d_mean.mean(),2),'std ',np.round(100*dice_3d_sd,2), 'std mean', np.round(100*dice_3d_sd.mean(),2),'hd_3d',hd_3d_mean, 'hd std',hd_3d_sd,'asd',asd_3d_mean,'mean ',asd_3d_mean.mean())

    [dice_3d, dice_3d_sd] = map_(lambda t: t.mean(), [dice_3d_mean, dice_3d_sd])
    [hd_3d, hd_3d_sd] = map_(lambda t: t.mean(), [hd_3d_mean, hd_3d_sd])
    [asd_3d, asd_3d_sd] = map_(lambda t: t.mean(), [asd_3d_mean, asd_3d_sd])
    if pprint:
        print('asd_3d_mean',asd_3d_mean.mean(), "asd_3d_sd", asd_3d_sd, "hd_3d_mean", hd_3d_mean, "hd_3d_sd", hd_3d_sd)
        print("hd_3d_mean", hd_3d_mean, "hd_3d_sd", hd_3d_sd)
        #print("in AA,LA,LV,Myo order:",asd_3d_sd.tolist()[3,1,2,0])
    return dice_3d.item(), dice_3d_sd.item(), asd_3d.item(), asd_3d_sd.item()
Esempio n. 5
0
def dice3dn(all_grp,all_inter_card,all_card_gt,all_card_pred,all_pred,all_gt,all_pnames,metric_axis,pprint=False,do_hd=0):
    list(filter(lambda a: a != "0.0", all_pnames))
    list(filter(lambda a: a != 0.0, all_pnames))
    _,C = all_card_gt.shape
    unique_patients = torch.unique(all_grp)
    list(filter(lambda a: a != 0.0, unique_patients))

    unique_patients = unique_patients[unique_patients != torch.ones_like(unique_patients)*666]
    unique_patients = [u.item() for u in unique_patients] 
    batch_dice = torch.zeros((len(unique_patients), C))
    batch_hd = torch.zeros((len(unique_patients), C))
    batch_asd = torch.zeros((len(unique_patients), C))
    if do_hd>0:
        all_pred = all_pred.cpu().numpy()
        all_gt = all_gt.cpu().numpy()
    for i, p in enumerate(unique_patients):
        try:
            bool_p = [int(re.split('_',re.split('slice',x.item())[1])[0])==p for x in all_pnames]
            data = "whs"
        except:
            bool_p = [int(re.split('_',re.split('Subj_',x.item())[1])[0])==p for x in all_pnames]
            data = "ivd"
        slices_p = all_pnames[bool_p]
        inter_card_p = torch.einsum("bc->c", [torch.masked_select(all_inter_card, all_grp == p).reshape((-1, C))])
        card_gt_p= torch.einsum("bc->c", [torch.masked_select(all_card_gt, all_grp == p).reshape((-1, C))])
        card_pred_p= torch.einsum("bc->c", [torch.masked_select(all_card_pred, all_grp == p).reshape((-1, C))])

        if do_hd >0:
            all_gt_p = all_gt[bool_p,:]
            all_pred_p = all_pred[bool_p,:] 
            sn_p = [int(re.split('_',x)[1]) for x in slices_p]
            ord_p = np.argsort(sn_p)
            label_gt = all_gt_p[ord_p,...]
            label_pred = all_pred_p[ord_p,...]
            hd_3d_var_vec= [None] * C
            hd_3d_var_vec2= [None] * C
            hd_3d_var_vec3= [None] * C
            asd_3d_var_vec = [None] * C
            for j in range(0,C):
                label_pred_c = numpy.copy(label_pred)
                label_pred_c[label_pred_c!=j]=0
                label_pred_c[label_pred_c==j]=1
                label_gt_c = numpy.copy(label_gt)
                label_gt_c[label_gt!=j]=0
                label_gt_c[label_gt==j]=1
                if len(np.unique(label_pred_c))>1 and len(np.unique(label_gt_c))>1:
                    if data=="whs":
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[0.6,0.44,0.44]).item()
                        asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item() #ASD IN VOXEL TO COMPARE
                    elif data=="ivd":
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[1.25,1.25,2]).item()
                        asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item() #ASD IN VOXEL TO COMPARE TO PNP PAPER
                else:
                    hd_3d_var_vec[j]=np.NaN
                    asd_3d_var_vec[j] = np.NaN
            hd_3d_var=torch.from_numpy(np.asarray(hd_3d_var_vec))# np.nanmean(hd_3d_var_vec)
            asd_3d_var=torch.from_numpy(np.asarray(asd_3d_var_vec))# np.nanmean(hd_3d_var_vec)
        dice_3d = (2 * inter_card_p + 1e-8) / ((card_pred_p + card_gt_p)+ 1e-8)
        if pprint and do_hd:
            #dice_3d = torch.round(dice_3d * 10**4) / (10**4)
            print(p,dice_3d.cpu(),hd_3d_var, asd_3d_var) 
        if pprint and not(do_hd):
            print(p,dice_3d.cpu())
        batch_dice[i,...] = dice_3d
        if do_hd>0:
            batch_hd[i,...] = hd_3d_var
            batch_asd[i,...] = asd_3d_var

    indices = torch.tensor(metric_axis)
    dice_3d = torch.index_select(batch_dice, 1, indices)
    hd_3d = torch.index_select(batch_hd, 1, indices)
    asd_3d = torch.index_select(batch_asd, 1, indices)

    dice_3d_mean = dice_3d.mean(dim=0)
    hd_3d_mean = hd_3d.mean(dim=0)
    asd_3d_mean = asd_3d.mean(dim=0)
    dice_3d_mean = torch.round(dice_3d_mean * 10**4) / (10**4)
    hd_3d_mean = torch.round(hd_3d_mean * 10**4) / (10**4)
    asd_3d_mean = torch.round(asd_3d_mean * 10**4) / (10**4)

    dice_3d_sd = dice_3d.std(dim=0)
    hd_3d_sd = hd_3d.std(dim=0)
    asd_3d_sd = asd_3d.std(dim=0)
    dice_3d_sd = torch.round(dice_3d_sd * 10**4) / (10**4)
    hd_3d_sd = torch.round(hd_3d_sd * 10**4) / (10**4)
    asd_3d_sd = torch.round(asd_3d_sd * 10**4) / (10**4)

    print('metric_axis dice 3d',np.round(dice_3d_mean*100,2), 'mean ',np.round(100*dice_3d_mean.mean(),2),'std ',np.round(100*dice_3d_sd,2), 'std mean', np.round(100*dice_3d_sd.mean(),2))
    if pprint:
        print('metric_axis asd 3d',np.round(asd_3d_mean,2), 'mean ',np.round(asd_3d_mean.mean(),2),'std ',np.round(asd_3d_sd,2), 'std mean', np.round(asd_3d_sd.mean(),2))
        print('metric_axis hd 3d',np.round(hd_3d_mean,2), 'mean ',np.round(hd_3d_mean.mean(),2),'std ',np.round(hd_3d_sd,2), 'std mean', np.round(hd_3d_sd.mean(),2))
         
        print("DICE",np.round(dice_3d_mean[0].item()*100,1), " $\pm$ ", np.round(dice_3d_sd[0].item()*100,1),
                "&",np.round(dice_3d_mean[1].item()*100,1), " $\pm$ ", np.round(dice_3d_sd[1].item()*100,1),
                "&",np.round(dice_3d_mean[2].item()*100,1), " $\pm$ ", np.round(dice_3d_sd[2].item()*100,1), 
                "&",np.round(dice_3d_mean[3].item()*100,1), " $\pm$ ", np.round(dice_3d_sd[3].item()*100,1), 
                "&",np.round(dice_3d_mean.mean().item()*100,1), " $\pm$ ", np.round(dice_3d_sd.mean().item()*100,1))
        print("HD", np.round(hd_3d_mean[0].item(),1), " $\pm$ ", np.round(hd_3d_sd[0].item(),1),
                "&",np.round(hd_3d_mean[1].item(),1), " $\pm$ ", np.round(hd_3d_sd[1].item(),1),
                "&",np.round(hd_3d_mean[2].item(),1), " $\pm$ ", np.round(hd_3d_sd[2].item(),1),
                "&",np.round(hd_3d_mean[3].item(),1), " $\pm$ ", np.round(hd_3d_sd[3].item(),1),
                "&",np.round(hd_3d_mean.mean().item(),1), " $\pm$ ", np.round(hd_3d_sd.mean().item(),1))
        print("ASD", np.round(asd_3d_mean[0].item(),1), " $\pm$ ", np.round(asd_3d_sd[0].item(),1),
                "&",np.round(asd_3d_mean[1].item(),1), " $\pm$ ", np.round(asd_3d_sd[1].item(),1),
                "&",np.round(asd_3d_mean[2].item(),1), " $\pm$ ", np.round(asd_3d_sd[2].item(),1),
                "&",np.round(asd_3d_mean[3].item(),1), " $\pm$ ", np.round(asd_3d_sd[3].item(),1),
                "&",np.round(asd_3d_mean.mean().item(),1), " $\pm$ ", np.round(asd_3d_sd.mean().item(),1))
        

    [dice_3d, dice_3d_sd] = map_(lambda t: t.mean(), [dice_3d_mean, dice_3d_sd])
    [hd_3d, hd_3d_sd] = map_(lambda t: t.mean(), [hd_3d_mean, hd_3d_sd])
    [asd_3d, asd_3d_sd] = map_(lambda t: t.mean(), [asd_3d_mean, asd_3d_sd])
    if pprint:
        print('asd_3d_mean',asd_3d_mean, "asd_3d_sd", asd_3d_sd, "hd_3d_mean", hd_3d_mean, "hd_3d_sd", hd_3d_sd)
    return dice_3d.item(), dice_3d_sd.item(), hd_3d.item(), hd_3d_sd.item()
Esempio n. 6
0
def dice3d(all_grp,all_inter_card,all_card_gt,all_card_pred,all_pred,all_gt,all_pnames,metric_axis,pprint=False,do_hd=0,do_asd=0,best_epoch_val=0):
    _,C = all_card_gt.shape
    unique_patients = torch.unique(all_grp)
    list(filter(lambda a: a != 0.0, unique_patients))
    unique_patients = [u.item() for u in unique_patients]
    batch_dice = torch.zeros((len(unique_patients), C))
    batch_hd = torch.zeros((len(unique_patients), C))
    batch_asd = torch.zeros((len(unique_patients), C))
    if do_hd>0 or do_asd>0:
        all_pred = all_pred.cpu().numpy()
        all_gt = all_gt.cpu().numpy()
    # do DICE
    for i, p in enumerate(unique_patients):
        inter_card_p = torch.einsum("bc->c", [torch.masked_select(all_inter_card, all_grp == p).reshape((-1, C))])
        card_gt_p= torch.einsum("bc->c", [torch.masked_select(all_card_gt, all_grp == p).reshape((-1, C))])
        card_pred_p= torch.einsum("bc->c", [torch.masked_select(all_card_pred, all_grp == p).reshape((-1, C))])
        dice_3d = (2 * inter_card_p + 1e-8) / ((card_pred_p + card_gt_p)+ 1e-8)
        batch_dice[i,...] = dice_3d
        if pprint:
            print(p,dice_3d.cpu())
    indices = torch.tensor(metric_axis)
    dice_3d = torch.index_select(batch_dice, 1, indices)
    dice_3d_mean = dice_3d.mean(dim=0)
    dice_3d_mean = torch.round(dice_3d_mean * 10**4) / (10**4)
    dice_3d_sd = dice_3d.std(dim=0)
    dice_3d_sd = torch.round(dice_3d_sd * 10**4) / (10**4)

    # do HD and / or ASD
    #if dice_3d_mean.mean()>best_epoch_val:
    if dice_3d_mean.mean()>0:
        for i, p in enumerate(unique_patients):
            root_name = [re.split('(\d+)', x.item())[0] for x in all_pnames][0]
            bool_p = [int(re.split('_',re.split(root_name,x.item())[1])[0])==p for x in all_pnames]
            slices_p = all_pnames[bool_p]
            #if do_hd >0 or dice_3d_mean.mean()>best_epoch_val:
            if do_hd> 0 or do_asd >0 :
                all_gt_p = all_gt[bool_p,:]
                all_pred_p = all_pred[bool_p,:]
                sn_p = [int(re.split('_',x)[1]) for x in slices_p]
                ord_p = np.argsort(sn_p)
                label_gt = all_gt_p[ord_p,...]
                label_pred = all_pred_p[ord_p,...]
                asd_3d_var_vec = [None] * C
                hd_3d_var_vec= [None] * C
                for j in range(0,C):
                    label_pred_c = numpy.copy(label_pred)
                    label_pred_c[label_pred_c!=j]=0
                    label_pred_c[label_pred_c==j]=1
                    label_gt_c = numpy.copy(label_gt)
                    label_gt_c[label_gt!=j]=0
                    label_gt_c[label_gt==j]=1
                    if len(np.unique(label_pred_c))>1: # len(np.unique(label_gt_c))>1 should always be true...
                        if do_hd>0:
                            if root_name=="Case" or root_name=="slice":
                                hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[0.6,0.44,0.44]).item()
                            elif root_name=="Subj_":
                                hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,[1.25,1.25,2]).item()
                        if do_asd > 0:
                            asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item() #ASD IN VOXEL TO COMPARE TO PNP PAPER
                    else:
                        #print(len(np.unique(label_pred_c)),len(np.unique(label_gt_c)))
                        hd_3d_var_vec[j]=np.NaN
                        asd_3d_var_vec[j] = np.NaN
            if do_hd>0:
                hd_3d_var = torch.from_numpy(np.asarray(hd_3d_var_vec))  # np.nanmean(hd_3d_var_vec)
                batch_hd[i,...] = hd_3d_var
            if do_asd>0:
                asd_3d_var = torch.from_numpy(np.asarray(asd_3d_var_vec))  # np.nanmean(hd_3d_var_vec)
                batch_asd[i,...] = asd_3d_var

    [hd_3d, hd_3d_sd] = get_mean_sd(batch_hd,indices)
    [asd_3d, asd_3d_sd] = get_mean_sd(batch_asd,indices)
    [dice_3d, dice_3d_sd] = map_(lambda t: t.mean(), [dice_3d_mean.cpu().numpy(), dice_3d_sd.cpu().numpy()])
    if pprint:
        print('asd_3d_mean',asd_3d, "asd_3d_sd", asd_3d_sd, "hd_3d_mean", hd_3d, "hd_3d_sd", hd_3d_sd)
    [return_asd,return_asd_sd] = [asd_3d.item(),asd_3d_sd.item()] if do_asd >0 else [0,0]
    [return_hd,return_hd_sd] = [hd_3d.item(),hd_3d_sd.item()] if do_hd >0 else [0,0]
    return dice_3d.item(), dice_3d_sd.item(), return_asd, return_asd_sd,return_hd,return_hd_sd
Esempio n. 7
0
def dice3dn(all_grp,
            all_inter_card,
            all_card_gt,
            all_card_pred,
            all_pred,
            all_gt,
            all_pnames,
            metric_axis,
            pprint=False,
            do_hd=0):
    #print(all_card_gt.shape)
    list(filter(lambda a: a != "0.0", all_pnames))
    list(filter(lambda a: a != 0.0, all_pnames))
    #print(all_pnames,"all_pnames")
    _, C = all_card_gt.shape
    unique_patients = torch.unique(all_grp)
    list(filter(lambda a: a != 0.0, unique_patients))
    #unique_patients = unique_patients[unique_patients!="0.0"]
    #unique_patients = unique_patients[unique_patients!=0.0]
    #print(sum(unique_patients == 0))
    #print(all_pred.sum())
    #print(all_gt.sum())
    #print(np.unique(all_pred),np.unique(all_gt),"np.unique(all_pred),np.unique(all_gt)")

    unique_patients = unique_patients[
        unique_patients != torch.ones_like(unique_patients) * 666]
    unique_patients = [u.item() for u in unique_patients]
    #unique_patients = unique_patients[unique_patients != 666]
    #print(unique_patients)
    batch_dice = torch.zeros((len(unique_patients), C))
    batch_hd = torch.zeros((len(unique_patients), C))
    batch_asd = torch.zeros((len(unique_patients), C))
    #hd_3d = torch.zeros((len(unique_patients)))
    #hd95_3d = torch.zeros((len(unique_patients)))
    #hd95_3d_bis = torch.zeros((len(unique_patients)))
    #hd_3d_var = torch.zeros((len(unique_patients)))
    #asd_3d = torch.zeros((len(unique_patients)))
    #ravd_3d = torch.zeros((len(unique_patients)))
    #print(len(all_pred))
    if do_hd > 0:
        all_pred = all_pred.cpu().numpy()
        all_gt = all_gt.cpu().numpy()
    for i, p in enumerate(unique_patients):
        #print(p)
        try:
            bool_p = [
                int(re.split('_',
                             re.split('slice', x.item())[1])[0]) == p
                for x in all_pnames
            ]
            data = "whs"
        except:
            bool_p = [
                int(re.split('_',
                             re.split('Subj_', x.item())[1])[0]) == p
                for x in all_pnames
            ]
            data = "ivd"
        #print(bool_p,"bool_p")
        #print(np.sum(bool_p))
        #bool_p = bool_p.detach()
        #print(all_pnames[1:10])
        #print(len(np.unique(all_pnames)))
        #print(bool_p[1:10])
        #print(all_grp[:,1])
        slices_p = all_pnames[bool_p]
        #print(all_pred.shape,all_grp.shape, "pred versus all_grp")
        #print((all_grp[:,1] == p).shape,'all_grp[:,1] == p shape')
        #print(all_grp,'all_grp')
        #print(sum(all_grp[:,1] == p),'all_grp[:,1] == p sum')
        #all_gt_p = all_gt[all_grp[:,1] == p,:,:]
        #all_gt_p = all_gt[bool_p,:]
        #all_pred_p = all_pred[all_grp[:,1] == p,:,:].cpu()
        #all_pred_p = all_pred[bool_p,:]
        #print(all_gt,all_pred)
        #print(all_gt_p.shape,len(slices_p)) # should be the same
        #sn_p = [int(re.split('_',x)[2].split('.')[0]) for x in slices_p]
        #ord_p = np.argsort(sn_p)
        #label_gt = all_gt_p[ord_p,...]
        #print(all_gt_p, all_pred_p)
        #label_pred = all_pred_p[ord_p,...]
        #print(label_gt.sum())
        #print(all_gt_p, all_pred_p)
        inter_card_p = torch.einsum("bc->c", [
            torch.masked_select(all_inter_card, all_grp == p).reshape((-1, C))
        ])
        card_gt_p = torch.einsum(
            "bc->c",
            [torch.masked_select(all_card_gt, all_grp == p).reshape((-1, C))])
        card_pred_p = torch.einsum("bc->c", [
            torch.masked_select(all_card_pred, all_grp == p).reshape((-1, C))
        ])
        #all_pred = all_pred.cpu().numpy()
        #all_gt = all_gt.cpu().numpy()
        #hd_3d[i]= hd(label_pred, label_gt,[1.25,1.25,2] ).item()
        if do_hd > 0:
            #print(slices_p)
            #all_pred = all_pred.cpu().numpy()
            #print(all_pred.shape)
            #all_gt = all_gt.cpu().numpy()
            all_gt_p = all_gt[bool_p, :]
            all_pred_p = all_pred[bool_p, :]
            #sn_p = [int(re.split('_',x)[2].split('.')[0]) for x in slices_p]
            #print(slices_p)
            #sn_p = [int(re.split('_',re.split('slice',x)[1])[0]) for x in slices_p]
            sn_p = [int(re.split('_', x)[1]) for x in slices_p]
            #print(sn_p)
            ord_p = np.argsort(sn_p)
            #print(ord_p)
            label_gt = all_gt_p[ord_p, ...]
            label_pred = all_pred_p[ord_p, ...]
            #print(np.unique(label_pred))
            hd_3d_var_vec = [None] * C
            hd_3d_var_vec2 = [None] * C
            hd_3d_var_vec3 = [None] * C
            asd_3d_var_vec = [None] * C
            #hd_3d_var_vec[:] = np.NaN
            #asd_3d_vec[:] = np.NaN
            #print(np.unique(label_pred), np.unique(label_gt))
            for j in range(0, C):
                #print(j)
                #print(np.unique(label_pred),"j",j, np.unique(label_pred))
                label_pred_c = numpy.copy(label_pred)
                label_pred_c[label_pred_c != j] = 0
                label_pred_c[label_pred_c == j] = 1
                label_gt_c = numpy.copy(label_gt)
                label_gt_c[label_gt != j] = 0
                label_gt_c[label_gt == j] = 1
                #print(np.unique(label_pred_c),np.unique(label_gt_c),"np.unique(label_pred_c),np.unique(label_gt_c)")
                #hd_3d_var_vec[j-1] = hd_var(label_pred_c, label_gt_c,[1.25,1.25,2],1,do_hd).item()
                if len(np.unique(label_pred_c)) > 1 and len(
                        np.unique(label_gt_c)) > 1:
                    if data == "whs":
                        #print("data:whs")
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,
                                                [0.6, 0.44, 0.44]).item()
                        #hd_3d_var_vec2[j] = hd(label_pred_c, label_gt_c,[0.6,0.44,0.44]).item()
                        #hd_3d_var_vec3[j] = hd(label_pred_c, label_gt_c,[1,1,1]).item()
                        asd_3d_var_vec[j] = assd(
                            label_pred_c,
                            label_gt_c).item()  #ASD IN VOXEL TO COMPARE
                        #print("hd100:",round(hd(label_pred_c, label_gt_c,[0.6,0.44,0.44]).item()), ", hd100 in vox",round(hd(label_pred_c, label_gt_c,[1,1,1]).item()), ", hd95:",round(hd_3d_var_vec[j]))
                    elif data == "ivd":
                        #print("data:ivd")
                        hd_3d_var_vec[j] = hd95(label_pred_c, label_gt_c,
                                                [1.25, 1.25, 2]).item()
                        asd_3d_var_vec[j] = assd(
                            label_pred_c, label_gt_c).item(
                            )  #ASD IN VOXEL TO COMPARE TO PNP PAPER
                    #print(hd_var(label_pred_c, label_gt_c,[1.25,1.25,2],1,do_hd).item())
                    #asd_3d_var_vec[j] = assd(label_pred_c, label_gt_c).item()
                else:
                    hd_3d_var_vec[j] = np.NaN
                    asd_3d_var_vec[j] = np.NaN
            #print("hd_3d_var_vec", hd_3d_var_vec)
            hd_3d_var = torch.from_numpy(
                np.asarray(hd_3d_var_vec))  # np.nanmean(hd_3d_var_vec)
            #hd_3d_var2=torch.from_numpy(np.asarray(hd_3d_var_vec2))# np.nanmean(hd_3d_var_vec)
            #hd_3d_var3=torch.from_numpy(np.asarray(hd_3d_var_vec3))# np.nanmean(hd_3d_var_vec)
            asd_3d_var = torch.from_numpy(
                np.asarray(asd_3d_var_vec))  # np.nanmean(hd_3d_var_vec)
            #asd_3d[i]=np.nanmean(asd_3d_vec)
        #hd95_3d_bis[i]= hd95(label_pred, label_gt,[1.25,2,1.25]).item()
        #ravd_3d[i]= abs(ravd(label_pred, label_gt))
        #print(hd_3d[i])
        #if p == 0:
        #    print("inter_card_p:",inter_card_p.detach())
        #    print("card_gt_p:", card_gt_p.detach())
        #    print("card_pred_p:",card_pred_p.detach())
        #print(card_gt_p.shape)
        dice_3d = (2 * inter_card_p + 1e-8) / (
            (card_pred_p + card_gt_p) + 1e-8)
        if pprint and do_hd:
            #dice_3d = torch.round(dice_3d * 10**4) / (10**4)
            print(p, dice_3d.cpu(), hd_3d_var, asd_3d_var)
        if pprint and not (do_hd):
            print(p, dice_3d.cpu())
        batch_dice[i, ...] = dice_3d
        if do_hd > 0:
            batch_hd[i, ...] = hd_3d_var
            batch_asd[i, ...] = asd_3d_var

    indices = torch.tensor(metric_axis)
    dice_3d = torch.index_select(batch_dice, 1, indices)
    hd_3d = torch.index_select(batch_hd, 1, indices)
    asd_3d = torch.index_select(batch_asd, 1, indices)

    dice_3d_mean = dice_3d.mean(dim=0)
    hd_3d_mean = hd_3d.mean(dim=0)
    asd_3d_mean = asd_3d.mean(dim=0)
    dice_3d_mean = torch.round(dice_3d_mean * 10**4) / (10**4)
    hd_3d_mean = torch.round(hd_3d_mean * 10**4) / (10**4)
    asd_3d_mean = torch.round(asd_3d_mean * 10**4) / (10**4)

    dice_3d_sd = dice_3d.std(dim=0)
    hd_3d_sd = hd_3d.std(dim=0)
    asd_3d_sd = asd_3d.std(dim=0)
    dice_3d_sd = torch.round(dice_3d_sd * 10**4) / (10**4)
    hd_3d_sd = torch.round(hd_3d_sd * 10**4) / (10**4)
    asd_3d_sd = torch.round(asd_3d_sd * 10**4) / (10**4)

    #hd95_3d_sd = hd95_3d.std()
    #hd95_3d_mean = hd95_3d.mean()
    #hdvar_3d_mean = hd_3d_var.mean()
    #hdvar_3d_sd = hd_3d_var.std()
    #ravd_3d_sd = ravd_3d.std()
    #ravd_3d_mean = ravd_3d.mean()
    print('metric_axis dice 3d', np.round(dice_3d_mean * 100, 2), 'mean ',
          np.round(100 * dice_3d_mean.mean(), 2), 'std ',
          np.round(100 * dice_3d_sd, 2), 'std mean',
          np.round(100 * dice_3d_sd.mean(), 2))
    if pprint:
        print('metric_axis asd 3d', np.round(asd_3d_mean, 2), 'mean ',
              np.round(asd_3d_mean.mean(), 2), 'std ', np.round(asd_3d_sd, 2),
              'std mean', np.round(asd_3d_sd.mean(), 2))
        print('metric_axis hd 3d', np.round(hd_3d_mean, 2), 'mean ',
              np.round(hd_3d_mean.mean(), 2), 'std ', np.round(hd_3d_sd, 2),
              'std mean', np.round(hd_3d_sd.mean(), 2))

        print("DICE", np.round(dice_3d_mean[0].item() * 100, 1), " $\pm$ ",
              np.round(dice_3d_sd[0].item() * 100, 1), "&",
              np.round(dice_3d_mean[1].item() * 100, 1), " $\pm$ ",
              np.round(dice_3d_sd[1].item() * 100, 1), "&",
              np.round(dice_3d_mean[2].item() * 100, 1), " $\pm$ ",
              np.round(dice_3d_sd[2].item() * 100, 1), "&",
              np.round(dice_3d_mean[3].item() * 100, 1), " $\pm$ ",
              np.round(dice_3d_sd[3].item() * 100, 1), "&",
              np.round(dice_3d_mean.mean().item() * 100, 1), " $\pm$ ",
              np.round(dice_3d_sd.mean().item() * 100, 1))
        print("HD", np.round(hd_3d_mean[0].item(), 1), " $\pm$ ",
              np.round(hd_3d_sd[0].item(), 1), "&",
              np.round(hd_3d_mean[1].item(), 1), " $\pm$ ",
              np.round(hd_3d_sd[1].item(), 1), "&",
              np.round(hd_3d_mean[2].item(), 1), " $\pm$ ",
              np.round(hd_3d_sd[2].item(), 1), "&",
              np.round(hd_3d_mean[3].item(), 1), " $\pm$ ",
              np.round(hd_3d_sd[3].item(), 1), "&",
              np.round(hd_3d_mean.mean().item(), 1), " $\pm$ ",
              np.round(hd_3d_sd.mean().item(), 1))
        print("ASD", np.round(asd_3d_mean[0].item(), 1), " $\pm$ ",
              np.round(asd_3d_sd[0].item(), 1), "&",
              np.round(asd_3d_mean[1].item(), 1), " $\pm$ ",
              np.round(asd_3d_sd[1].item(), 1), "&",
              np.round(asd_3d_mean[2].item(), 1), " $\pm$ ",
              np.round(asd_3d_sd[2].item(), 1), "&",
              np.round(asd_3d_mean[3].item(), 1), " $\pm$ ",
              np.round(asd_3d_sd[3].item(), 1), "&",
              np.round(asd_3d_mean.mean().item(), 1), " $\pm$ ",
              np.round(asd_3d_sd.mean().item(), 1))

    [dice_3d, dice_3d_sd] = map_(lambda t: t.mean(),
                                 [dice_3d_mean, dice_3d_sd])
    [hd_3d, hd_3d_sd] = map_(lambda t: t.mean(), [hd_3d_mean, hd_3d_sd])
    [asd_3d, asd_3d_sd] = map_(lambda t: t.mean(), [asd_3d_mean, asd_3d_sd])
    if pprint:
        print('asd_3d_mean', asd_3d_mean, "asd_3d_sd", asd_3d_sd, "hd_3d_mean",
              hd_3d_mean, "hd_3d_sd", hd_3d_sd)
    #return dice_3d.item(), dice_3d_sd.item(), hd_3d_mean.item(), hd_3d_sd.item(), asd_3d_mean.item(),asd_3d_sd.item()
    return dice_3d.item(), dice_3d_sd.item(), hd_3d.item(), hd_3d_sd.item()