import numpy as np

# Definition of errors

def absolute_error(value1,value2):
    return np.abs(value1-value2)

def squared_error(value1,value2):
    return np.abs(value1-value2)**2


# Tool for model comparaison

class Models_Comparator(object):
    
    def __init__(self,model_ref,error="absolute"):
        self.model_ref=model_ref
        self.error=error
    
    def compare(self,model2,attributes=None):
        
        try:
            if attributes == None:
                ref_dict=self.model_ref.__dict__
                attributes=sorted(list(ref_dict.keys()))
            else:
                if isinstance(attributes, list) == False:
                    attributes=[attributes]
        
            for attribute in attributes:
                
                if attribute not in ["name","N"]:
                    value1=getattr(self.model_ref,attribute)
                    value2=getattr(model2,attribute)
                    if self.error=="absolute":
                        error=absolute_error(value1,value2)
                    if self.error=="squared":
                        error=squared_error(value1,value2)
                    
                    print("%s:\t" %attribute, end='')
                    print(error)
        except:
            raise(ValueError("The two models do not match"))
