Skip to content

Commit

Permalink
Initial public release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craigacp committed Jun 22, 2020
0 parents commit 7a00fab
Show file tree
Hide file tree
Showing 891 changed files with 103,279 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .gitignore
@@ -0,0 +1,31 @@
*/target/
target

bin/
.settings/

.classpath
.project

# Intellij files
*.iml
.idea/

# Vim backup files
*~
.*.swp

# Other files
*.jar
*.class
*.er
*.log
*.bck
*.so

# Serialised models
*.ser

# Temporary stuff
junk/*
.DS_Store
58 changes: 58 additions & 0 deletions AnomalyDetection/Core/pom.xml
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.tribuo</groupId>
<artifactId>tribuo-anomaly</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<name>AnomalyDetection-Core</name>
<artifactId>tribuo-anomaly-core</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>tribuo-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>tribuo-data</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>tribuo-math</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.labs.mlrg.olcut</groupId>
<artifactId>olcut-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tribuo.anomaly;

import com.oracle.labs.mlrg.olcut.provenance.Provenance;
import org.tribuo.ImmutableOutputInfo;
import org.tribuo.MutableOutputInfo;
import org.tribuo.OutputFactory;
import org.tribuo.anomaly.Event.EventType;
import org.tribuo.anomaly.evaluation.AnomalyEvaluation;
import org.tribuo.anomaly.evaluation.AnomalyEvaluator;
import org.tribuo.evaluation.Evaluator;
import org.tribuo.provenance.OutputFactoryProvenance;

import java.util.Map;

/**
* A factory for generating events.
*/
public final class AnomalyFactory implements OutputFactory<Event> {
private static final long serialVersionUID = 1L;

public static final Event UNKNOWN_EVENT = new Event(EventType.UNKNOWN);
public static final Event EXPECTED_EVENT = new Event(EventType.EXPECTED);
public static final Event ANOMALOUS_EVENT = new Event(EventType.ANOMALY);

private static final AnomalyEvaluator evaluator = new AnomalyEvaluator();

@Override
public <V> Event generateOutput(V label) {
if (label.toString().equalsIgnoreCase(EventType.ANOMALY.toString())) {
return ANOMALOUS_EVENT;
} else {
return EXPECTED_EVENT;
}
}

@Override
public Event getUnknownOutput() {
return UNKNOWN_EVENT;
}

@Override
public MutableOutputInfo<Event> generateInfo() {
return new MutableAnomalyInfo();
}

@Override
public ImmutableOutputInfo<Event> constructInfoForExternalModel(Map<Event,Integer> mapping) {
// Validate inputs are dense
OutputFactory.validateMapping(mapping);

Integer expectedMapping = mapping.get(EXPECTED_EVENT);
Integer anomalousMapping = mapping.get(ANOMALOUS_EVENT);

if (((expectedMapping != null) && (expectedMapping != EventType.EXPECTED.getID())) ||
((anomalousMapping != null) && anomalousMapping != EventType.ANOMALY.getID())){
throw new IllegalArgumentException("Anomaly detection requires that anomalous events have id " + EventType.ANOMALY.getID() + ", and expected events have id " + EventType.EXPECTED.getID());
}

MutableAnomalyInfo info = new MutableAnomalyInfo();
return info.generateImmutableOutputInfo();
}

@Override
public Evaluator<Event, AnomalyEvaluation> getEvaluator() {
return evaluator;
}

@Override
public OutputFactoryProvenance getProvenance() {
return new AnomalyFactoryProvenance();
}

public final static class AnomalyFactoryProvenance implements OutputFactoryProvenance {
private static final long serialVersionUID = 1L;

AnomalyFactoryProvenance() {}

public AnomalyFactoryProvenance(Map<String, Provenance> map) { }

@Override
public String getClassName() {
return AnomalyFactory.class.getName();
}

@Override
public String toString() {
return generateString("OutputFactory");
}

@Override
public boolean equals(Object other) {
return other instanceof AnomalyFactoryProvenance;
}

@Override
public int hashCode() {
return 31;
}
}
}
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tribuo.anomaly;

import com.oracle.labs.mlrg.olcut.util.Pair;
import org.tribuo.ImmutableOutputInfo;
import org.tribuo.MutableOutputInfo;
import org.tribuo.OutputInfo;
import org.tribuo.anomaly.Event.EventType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* The base class for tracking anomalous events.
*/
public abstract class AnomalyInfo implements OutputInfo<Event> {
private static final long serialVersionUID = 1L;

private static final Set<Event> DOMAIN = makeDomain();

protected long expectedCount = 0;
protected long anomalyCount = 0;
protected int unknownCount = 0;

protected AnomalyInfo() { }

protected AnomalyInfo(AnomalyInfo other) {
this.expectedCount = other.expectedCount;
this.anomalyCount = other.anomalyCount;
this.unknownCount = other.unknownCount;
}

@Override
public int getUnknownCount() {
return unknownCount;
}

public long getAnomalyCount() {
return anomalyCount;
}

public long getExpectedCount() {
return expectedCount;
}

/**
* Returns the set of possible {@link Event}s.
*
* Each event has the default score of Double.NaN.
* @return The set of possible events.
*/
@Override
public Set<Event> getDomain() {
return DOMAIN;
}

/**
* Gets the count of the supplied EventType.
* @param type An EventType.
* @return A non-negative long.
*/
public long getEventCount(EventType type) {
switch (type) {
case ANOMALY:
return anomalyCount;
case EXPECTED:
return expectedCount;
case UNKNOWN:
return unknownCount;
default:
return 0;
}
}

@Override
public Iterable<Pair<String,Long>> outputCountsIterable() {
List<Pair<String,Long>> list = new ArrayList<>();

list.add(new Pair<>(EventType.ANOMALY.toString(),anomalyCount));
list.add(new Pair<>(EventType.EXPECTED.toString(),expectedCount));

return list;
}

/**
* The number of possible event types (i.e. 2).
* @return The number of possible event types.
*/
@Override
public int size() {
return DOMAIN.size();
}

@Override
public ImmutableOutputInfo<Event> generateImmutableOutputInfo() {
return new ImmutableAnomalyInfo(this);
}

@Override
public MutableOutputInfo<Event> generateMutableOutputInfo() {
return new MutableAnomalyInfo(this);
}

@Override
public String toReadableString() {
return "{Anomalies:"+anomalyCount+",expected:"+expectedCount+"}";
}

@Override
public abstract AnomalyInfo copy();

private static Set<Event> makeDomain() {
HashSet<Event> set = new HashSet<>();

set.add(AnomalyFactory.EXPECTED_EVENT);
set.add(AnomalyFactory.ANOMALOUS_EVENT);

return Collections.unmodifiableSet(set);
}
}

0 comments on commit 7a00fab

Please sign in to comment.