""" 
Copyright (c) 2016, Jose Dolz .All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

Jose Dolz. Dec, 2016.
email: jose.dolz.upv@gmail.com
LIVIA Department, ETS, Montreal.
"""

import pdb
import numpy as np

def calculateDiceCoefficient(predictedBinaryLabels, groundTruthBinaryLabels) :
    unionCorrectlyPredicted = predictedBinaryLabels * groundTruthBinaryLabels
    numberOfTruePositives = np.sum(unionCorrectlyPredicted)
    numberOfGtPositives = np.sum(groundTruthBinaryLabels)
    diceCoeff = (2.0 * numberOfTruePositives) / (np.sum(predictedBinaryLabels) + numberOfGtPositives) if numberOfGtPositives<>0 else -1 
    return diceCoeff
    
# ----- Dice Score -----
def computeDice(autoSeg, groundTruth):
    """ Returns
    -------
    DiceArray : floats array
          
          Dice coefficient as a float on range [0,1].
          Maximum similarity = 1
          No similarity = 0 """
          
    n_classes = int( np.max(groundTruth) + 1)
   
    DiceArray = []
    
    
    for c_i in xrange(1,n_classes):
        idx_Auto = np.where(autoSeg.flatten() == c_i)[0]
        idx_GT   = np.where(groundTruth.flatten() == c_i)[0]
        
        autoArray = np.zeros(autoSeg.size,dtype=np.bool)
        autoArray[idx_Auto] = 1
        
        gtArray = np.zeros(autoSeg.size,dtype=np.bool)
        gtArray[idx_GT] = 1

        
        dsc = dice(autoArray, gtArray)
        dsc2 = calculateDiceCoefficient(autoArray, gtArray)

        print(" dsc1: {}").format(dsc)
        print(" dsc2: {}").format(dsc2)
        
        #dice = np.sum(autoSeg[groundTruth==c_i])*2.0 / (np.sum(autoSeg) + np.sum(groundTruth))
        DiceArray.append(dsc)
        
    return DiceArray


def dice(im1, im2):
    """
    Computes the Dice coefficient
    ----------
    im1 : boolean array
    im2 : boolean array
    
    If they are not boolean, they will be converted.
    
    -------
    It returns the Dice coefficient as a float on the range [0,1].
        1: Perfect overlapping 
        0: Not overlapping 
    """
    im1 = np.asarray(im1).astype(np.bool)
    im2 = np.asarray(im2).astype(np.bool)

    if im1.size != im2.size:
        raise ValueError("Size mismatch between input arrays!!!")

    im_sum = im1.sum() + im2.sum()
    if im_sum == 0:
        return 1.0

    # Compute Dice 
    intersection = np.logical_and(im1, im2)

    return 2. * intersection.sum() / im_sum

@@ -1177,442 +1177,8 @@
 s np
-%0A%0Adef calculateDiceCoefficient(predictedBinaryLabels, groundTruthBinaryLabels) :%0A    unionCorrectlyPredicted = predictedBinaryLabels * groundTruthBinaryLabels%0A    numberOfTruePositives = np.sum(unionCorrectlyPredicted)%0A    numberOfGtPositives = np.sum(groundTruthBinaryLabels)%0A    diceCoeff = (2.0 * numberOfTruePositives) / (np.sum(predictedBinaryLabels) + numberOfGtPositives) if numberOfGtPositives%3C%3E0 else -1 %0A    return diceCoeff
 %0A   
@@ -1864,17 +1864,16 @@
 GT%5D = 1%0A
-%0A
         
@@ -1916,148 +1916,8 @@
 ay)%0A
-        dsc2 = calculateDiceCoefficient(autoArray, gtArray)%0A%0A        print(%22 dsc1: %7B%7D%22).format(dsc)%0A        print(%22 dsc2: %7B%7D%22).format(dsc2)%0A
     

Update Evaluation.py