def getaveragedicescore2d(moving,fixed,parameter_file):
    #Calculates the average dice score per slice of registration
    #between all combinations of moving and fixed images    

    #moving = array with patientnumbers of moving images
    #fixed = array with patientnumbers of fixed images

    DCscores = [0]*nrslices
    n = 0
    
    for i in fixed:
        for j in moving:
            if i == j: continue
            register3d(i,j,parameter_file,runnr=runnr,verbose=False)
            transform3d(j,runnr,transformmask=True)

            transformed = findtransformedmask(runnr)
            fixed = findfixedmask3d(i)

            for k in range(nrslices):
                if sum(sum(x) for x in transformed[k])+sum(sum(x) for x in fixed[k]) == 0:
                    DCscores[k] += 1
                else:
                    DCscores[k] += medpyDC(transformed[k],fixed[k])
            n += 1
            
    for i in range(nrslices):
        DCscores[i] = DCscores[i]/n
    
    output_dir = f'results{runnr}'
    shutil.rmtree(output_dir)
    
    return DCscores
def getaveragedicescore3d(moving,fixed,parameter_file):
    #Calculates the average dice score of registration
    #between all combinations of moving and fixed images    

    #moving = array with patientnumbers of moving images
    #fixed = array with patientnumbers of fixed images

    DCscore = 0
    n = 0
    
    for i in fixed:
        for j in moving:
            if i == j: continue

            register3d(i,j,parameter_file,runnr=runnr,verbose=False)
            transform3d(j,runnr,transformmask=True)

            transformed = findtransformedmask(runnr)
            fixed = findfixedmask3d(i)

            DCscore += medpyDC(transformed,fixed)
            n += 1
            
    DCscore /= n
    
    output_dir = f'results{runnr}'
    shutil.rmtree(output_dir)

    return DCscore
def validationscores(validationset,finalmasks):
    #This function performs step 9 of the plan.
    scores = []
    for i in range(len(validationset)):
        reference = findfixedmask3d(validationset[i])
        result = finalmasks[i]
        
        if sum(sum(sum(x) for x in y) for y in finalmasks[i]) == 0:
            scores.append([0,0,-100])
            continue
        DCscore = medpyDC(result,reference)
        HDscore = medpyHD(result,reference)
        RVDscore = medpyRVD(result,reference)
        scores.append([DCscore,HDscore,RVDscore])
    return scores
Example #4
0
#Before this step: change offset in parameterfile from fixed to moving image!
#runnr = 20

#transform command
#transform2d(movingnr,slicenr,runnr,transformmask=True)
transform3d(movingnr, runnr, transformmask=True)

#visualisation commands
#visualize2d(fixednr,movingnr,slicenr,runnr)
#visualize3d(fixednr,movingnr,slicenr,runnr)

fixedmask = findfixedmask(107)
transformedmask = findtransformedmask(runnr)

print(medpyDC(transformedmask, fixedmask))
print(medpyHD(transformedmask, fixedmask))

fixed_image_path = os.path.join(f"results{runnr}", "result.mhd")
moving_image_path = os.path.join(data_path, f"p{movingnr}\mr_bffe.mhd")

output_dir = f'results{runnr}'

el = elastix.ElastixInterface(elastix_path=elastix_path)

#run model
el.register(fixed_image=fixed_image_path,
            moving_image=moving_image_path,
            parameters=[parameter_file2],
            output_dir=output_dir)
listofDCs = []
listofHDs = []
fixed_mask_array = findfixedmask2d(fixednr, slicenr)

for x in listofvalues:
    replace(parameter_file, keyword, x)
    runnr = register2d(fixednr, movingnr, slicenr, parameter_file)
    transform2d(movingnr, slicenr, runnr, transformmask=True)

    transformed_mask_path = os.path.join(f"results{runnr}",
                                         r"transformedmask\result.mhd")
    transformed_mask = sitk.ReadImage(transformed_mask_path)
    transformed_mask_array = sitk.GetArrayFromImage(transformed_mask)

    DCscore = medpyDC(transformed_mask_array, fixed_mask_array)
    HDscore = medpyHD(transformed_mask_array, fixed_mask_array)

    listofDCs.append(DCscore)
    listofHDs.append(HDscore)

print(keyword)
print(listofvalues)
print('Dice scores')
print(listofDCs)
print('Hausendorff distances')
print(listofHDs)

fig, ax = plt.subplots(1, 2)
ax[0].plot(listofvalues, listofDCs)
ax[0].set_title('Dice coefficient scores')