def evaluate_threshold_metrics(model, data_set, dataset_name,
    threshold_metric_names=None):
    """
    Generates the train, valid or test performance metrics of a trained model

    :param model: an H2O model object
    :param data_set: A DataSet with H2ODataWrappers or H2OSparklingDataWrappers as "full_data" attribute
    :param dataset_name: "train", "valid" or "test", indicating the data being evaluated on
    :return: Dictionary of the form {"metric_name":"metric_value"}
    """
    if not threshold_metric_names:
        threshold_metric_names = ['f1', 'f2', 'f0point5', 'accuracy',
            'precision', 'recall', 'specificity', 'absolute_mcc',
            'min_per_class_accuracy', 'mean_per_class_accuracy', 'tns',
            'fns', 'fps', 'tps', 'tnr', 'fnr', 'fpr', 'tpr']
    h2o_df = data_set.full_data.underlying
    if dataset_name == 'train':
        performance = model.model_performance(test_data=None, train=True,
            valid=False)
    elif dataset_name == 'valid':
        performance = model.model_performance(test_data=None, train=False,
            valid=True)
    elif dataset_name == 'test':
        performance = model.model_performance(test_data=h2o_df, train=False,
            valid=False)
    else:
        raise ValueError(
            "Invalid dataset type. Expecting 'train', 'valid' or 'test'")
    if performance._metric_json.get('thresholds_and_metric_scores'):
        threshold_metrics = performance._metric_json[
            'thresholds_and_metric_scores'].as_data_frame().to_dict()
    else:
        threshold_metrics = {}
    return threshold_metrics
