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; 018 019import org.tribuo.MutableOutputInfo; 020 021/** 022 * An {@link MutableOutputInfo} object for {@link Event}s. 023 * <p> 024 * Counts the number of {@link Event.EventType#ANOMALOUS}, {@link Event.EventType#EXPECTED} 025 * and {@link Event.EventType#UNKNOWN} outputs observed. The unknown output is invalid 026 * at training time, and used as a prediction time sentinel (similarly to other Tribuo 027 * prediction tasks). 028 * <p> 029 * Anomaly detection has a fixed domain, so it will throw {@link IllegalArgumentException} 030 * if you somehow modify the {@link Event.EventType} enum to add a new value. 031 */ 032public final class MutableAnomalyInfo extends AnomalyInfo implements MutableOutputInfo<Event> { 033 private static final long serialVersionUID = 1L; 034 035 MutableAnomalyInfo() { 036 super(); 037 } 038 039 MutableAnomalyInfo(AnomalyInfo info) { 040 super(info); 041 } 042 043 @Override 044 public void observe(Event output) { 045 if (output == AnomalyFactory.UNKNOWN_EVENT) { 046 unknownCount++; 047 } else { 048 switch (output.getType()) { 049 case ANOMALOUS: 050 anomalyCount++; 051 break; 052 case EXPECTED: 053 expectedCount++; 054 break; 055 default: 056 throw new IllegalArgumentException("Unexpected EventType, found " + output.getType()); 057 } 058 } 059 } 060 061 @Override 062 public void clear() { 063 unknownCount = 0; 064 anomalyCount = 0; 065 expectedCount = 0; 066 } 067 068 @Override 069 public MutableAnomalyInfo copy() { 070 return new MutableAnomalyInfo(this); 071 } 072}