import os
import sys
import numpy as np
from time import time


def dice_coefficient_post(y_true,y_pred):
    smoothing_factor=1e-6
    y_true = np.squeeze(y_true)
    y_pred = np.squeeze(y_pred)
    y_true = y_true>0
    intersection=np.sum(np.multiply(y_true,y_pred))
    sum_true = np.sum(y_true)
    sum_pred = np.sum(y_pred)
    return (2.0*intersection+smoothing_factor)/(sum_true+sum_pred+smoothing_factor)


def calculate_iou(y_true,y_pred):
    smoothint_factor = 1e-6
    y_true = np.squeeze(y_true)
    y_pred = np.squeeze(y_pred)
    y_true = y_true>0
    intersection=np.sum(np.multiply(y_true,y_pred))
    union = np.sum((y_true+y_pred)>0) 
    return (intersection+smoothing_factor)/(union+smoothing_factor)
    
def overlap_ratio(y_true,y_pred):
    smoothing_factor=1e-6
    y_true = np.squeeze(y_true)
    y_pred = np.squeeze(y_pred)
    y_true = y_true>0
    y_pred = y_pred>0
    intersection=np.sum(np.multiply(y_true,y_pred))
    sum_true = np.sum(y_true)
    sum_pred = np.sum(y_pred)
    return (2.0*intersection+smoothing_factor)/2*min(sum_true,sum_pred) + smoothing_factor