{% if pipeline.adaptation_metric == macros.Metric.AUC.value %}
        from sklearn.metrics import roc_auc_score
{% if pipeline.task.is_multiclass == True %}
        score = roc_auc_score(target_test.values.ravel(), y_pred, multi_class="ovr")
{% else %}
        score = roc_auc_score(target_test, y_pred)
{% endif %}
{% elif pipeline.adaptation_metric == macros.Metric.Accuracy.value %}
        from sklearn.metrics import accuracy_score
        score = accuracy_score(target_test, y_pred)
{% elif pipeline.adaptation_metric == macros.Metric.F1.value %}
        from sklearn import metrics
        score = metrics.f1_score(target_test, y_pred, average='macro')
{% elif pipeline.adaptation_metric == macros.Metric.R2.value %}
        from sklearn import metrics
        score = metrics.r2_score(target_test, y_pred)
{% elif pipeline.adaptation_metric == macros.Metric.RMSE.value %}
        from sklearn.metrics import mean_squared_error
        score = mean_squared_error(target_test, y_pred, squared=False)
{% elif pipeline.adaptation_metric == macros.Metric.RMSLE.value %}
        import numpy as np
        from sklearn.metrics import mean_squared_log_error
        target_test = np.clip(target_test, 0, None)
        y_pred = np.clip(y_pred, 0, None)
        score = np.sqrt(mean_squared_log_error(target_test, y_pred))
{% elif pipeline.adaptation_metric == macros.Metric.Gini.value %}
        from sklearn.metrics import roc_auc_score
{% if pipeline.task.is_multiclass == True %}
        score = 2 * roc_auc_score(target_test.values.ravel(), y_pred, multi_class="ovr") - 1
{% else %}
        score = 2 * roc_auc_score(target_test, y_pred) - 1
{% endif %}
{% elif pipeline.adaptation_metric == macros.Metric.MAE.value %}
        from sklearn.metrics import mean_absolute_error
        score = mean_absolute_error(target_test, y_pred)
{% elif pipeline.adaptation_metric == macros.Metric.LogLoss.value %}
        from sklearn.metrics import log_loss
        score = log_loss(target_test, y_pred)
{% elif pipeline.adaptation_metric == macros.Metric.ROC_AUC.value %}
        from sklearn.metrics import roc_auc_score
{% if pipeline.task.is_multiclass == True %}
        score = roc_auc_score(target_test.values.ravel(), y_pred, multi_class="ovr")
{% else %}
        score = roc_auc_score(target_test, y_pred)
{% endif %}
{% elif pipeline.adaptation_metric.startswith("MAP_") %}
{% set k = pipeline.adaptation_metric.split("_")[1] %}
        def apk(actual, predicted, k):
            if len(predicted)>k:
                predicted = predicted[:k]
        
            score = 0.0
            num_hits = 0.0
    
            for i,p in enumerate(predicted):
                if p in actual and p not in predicted[:i]:
                    num_hits += 1.0
                    score += num_hits / (i+1.0)
    
            return score / min(len(actual), k)
    
        def mapk(actual, predicted, k):
            """ Computes the mean average precision at k.

            Args:
                actual (list[list[str] or ndarray): A list of lists of elements that are to be predicted
                predicted (list[list[str] or ndarray): A list of lists of predicted elements
                    (In each list, arrange in the order you predicted.)
                k (int): The maximum number of predicted elements

            Returns:
                double: The mean average precision at k over the input lists
            """
            return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])
    
        score = mapk(target_test.to_numpy(), y_pred, k={{ k }})
{% elif pipeline.adaptation_metric == macros.Metric.MAPE.value %}
        from sklearn.metrics import mean_absolute_percentage_error
        score = mean_absolute_percentage_error(target_test, y_pred)        
{% elif pipeline.task.task_type == macros.TASK_REGRESSION %}
        from sklearn import metrics
        score = metrics.r2_score(target_test, y_pred)
{% else %}
        from sklearn import metrics
        score = metrics.f1_score(target_test, y_pred, average='macro')
{% endif %}