001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.anomaly.evaluation;
018
019import org.tribuo.Model;
020import org.tribuo.Prediction;
021import org.tribuo.anomaly.Event;
022import org.tribuo.evaluation.AbstractEvaluator;
023import org.tribuo.evaluation.Evaluator;
024import org.tribuo.evaluation.metrics.MetricID;
025import org.tribuo.provenance.EvaluationProvenance;
026
027import java.util.HashSet;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * An {@link Evaluator} for anomaly detection {@link Event}s.
034 */
035public class AnomalyEvaluator extends AbstractEvaluator<Event, AnomalyMetric.Context, AnomalyEvaluation, AnomalyMetric> {
036
037    @Override
038    protected Set<AnomalyMetric> createMetrics(Model<Event> model) {
039        Set<AnomalyMetric> metrics = new HashSet<>();
040        metrics.add(AnomalyMetrics.TP.asMetric());
041        metrics.add(AnomalyMetrics.FP.asMetric());
042        metrics.add(AnomalyMetrics.TN.asMetric());
043        metrics.add(AnomalyMetrics.FN.asMetric());
044        metrics.add(AnomalyMetrics.PRECISION.asMetric());
045        metrics.add(AnomalyMetrics.RECALL.asMetric());
046        metrics.add(AnomalyMetrics.F1.asMetric());
047        return metrics;
048    }
049
050    @Override
051    protected AnomalyMetric.Context createContext(Model<Event> model, List<Prediction<Event>> predictions) {
052        return AnomalyMetric.buildContext(model, predictions);
053    }
054
055    @Override
056    protected AnomalyEvaluation createEvaluation(AnomalyMetric.Context context,
057                                                 Map<MetricID<Event>, Double> results,
058                                                 EvaluationProvenance provenance) {
059        return new AnomalyEvaluationImpl(results, context, provenance);
060    }
061
062}