diff options
Diffstat (limited to 'test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof')
6 files changed, 478 insertions, 0 deletions
diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestINFLO.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestINFLO.java new file mode 100644 index 00000000..99b97d92 --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestINFLO.java @@ -0,0 +1,62 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.INFLO; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.database.Database; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the INFLO algorithm. + * + * @author Lucia Cichella + */ +public class TestINFLO extends AbstractSimpleAlgorithmTest implements JUnit4Test { + @Test + public void testINFLO() { + Database db = makeSimpleDatabase(UNITTEST + "outlier-3d-3clusters.ascii", 960); + + // Parameterization + ListParameterization params = new ListParameterization(); + params.addParameter(INFLO.K_ID, 29); + + // setup Algorithm + INFLO<DoubleVector, DoubleDistance> inflo = ClassGenericsUtil.parameterizeOrAbort(INFLO.class, params); + testParameterizationOk(params); + + // run INFLO on database + OutlierResult result = inflo.run(db); + + testSingleScore(result, 945, 1.215459716); + testAUC(db, "Noise", result, 0.9389259259259); + } +}
\ No newline at end of file diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLDOF.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLDOF.java new file mode 100644 index 00000000..370054ea --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLDOF.java @@ -0,0 +1,62 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LDOF; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.database.Database; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the LDOF algorithm. + * + * @author Lucia Cichella + */ +public class TestLDOF extends AbstractSimpleAlgorithmTest implements JUnit4Test { + @Test + public void testLDOF() { + Database db = makeSimpleDatabase(UNITTEST + "outlier-fire.ascii", 1025); + + // Parameterization + ListParameterization params = new ListParameterization(); + params.addParameter(LDOF.K_ID, 25); + + // setup Algorithm + LDOF<DoubleVector, DoubleDistance> ldof = ClassGenericsUtil.parameterizeOrAbort(LDOF.class, params); + testParameterizationOk(params); + + // run LDOF on database + OutlierResult result = ldof.run(db); + + testAUC(db, "Noise", result, 0.9637948717948718); + testSingleScore(result, 1025, 0.8976268846182947); + } +}
\ No newline at end of file diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOCI.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOCI.java new file mode 100644 index 00000000..05206f55 --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOCI.java @@ -0,0 +1,62 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOCI; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.database.Database; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the LOCI algorithm. + * + * @author Lucia Cichella + */ +public class TestLOCI extends AbstractSimpleAlgorithmTest implements JUnit4Test { + @Test + public void testLOCI() { + Database db = makeSimpleDatabase(UNITTEST + "3clusters-and-noise-2d.csv", 330); + + // Parameterization + ListParameterization params = new ListParameterization(); + params.addParameter(LOCI.RMAX_ID, 0.5); + + // setup Algorithm + LOCI<DoubleVector, DoubleDistance> loci = ClassGenericsUtil.parameterizeOrAbort(LOCI.class, params); + testParameterizationOk(params); + + // run LOCI on database + OutlierResult result = loci.run(db); + + testAUC(db, "Noise", result, 0.96222222); + testSingleScore(result, 146, 3.8054382); + } +}
\ No newline at end of file diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOF.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOF.java new file mode 100644 index 00000000..2c7bbd1d --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLOF.java @@ -0,0 +1,62 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOF; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.database.Database; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the LOF algorithm. + * + * @author Lucia Cichella + */ +public class TestLOF extends AbstractSimpleAlgorithmTest implements JUnit4Test { + @Test + public void testLOF() { + Database db = makeSimpleDatabase(UNITTEST + "outlier-axis-subspaces-6d.ascii", 1345); + + // Parameterization + ListParameterization params = new ListParameterization(); + params.addParameter(LOF.Parameterizer.K_ID, 10); + + // setup Algorithm + LOF<DoubleVector, DoubleDistance> lof = ClassGenericsUtil.parameterizeOrAbort(LOF.class, params); + testParameterizationOk(params); + + // run LOF on database + OutlierResult result = lof.run(db); + + testSingleScore(result, 1293, 1.1945314199156365); + testAUC(db, "Noise", result, 0.8921680672268908); + } +}
\ No newline at end of file diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLoOP.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLoOP.java new file mode 100644 index 00000000..e6670eb7 --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestLoOP.java @@ -0,0 +1,62 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.database.Database; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the LoOP algorithm. + * + * @author Lucia Cichella + */ +public class TestLoOP extends AbstractSimpleAlgorithmTest implements JUnit4Test { + @Test + public void testLoOP() { + Database db = makeSimpleDatabase(UNITTEST + "outlier-3d-3clusters.ascii", 960); + + // Parameterization + ListParameterization params = new ListParameterization(); + params.addParameter(LoOP.KCOMP_ID, 15); + + // setup Algorithm + LoOP<DoubleVector, DoubleDistance> loop = ClassGenericsUtil.parameterizeOrAbort(LoOP.class, params); + testParameterizationOk(params); + + // run LoOP on database + OutlierResult result = loop.run(db); + + testAUC(db, "Noise", result, 0.9443796296296296); + testSingleScore(result, 945, 0.39805457858293325); + } +}
\ No newline at end of file diff --git a/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestOnlineLOF.java b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestOnlineLOF.java new file mode 100644 index 00000000..cd60a58f --- /dev/null +++ b/test/de/lmu/ifi/dbs/elki/algorithm/outlier/lof/TestOnlineLOF.java @@ -0,0 +1,168 @@ +package de.lmu.ifi.dbs.elki.algorithm.outlier.lof; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Random; + +import org.junit.Test; + +import de.lmu.ifi.dbs.elki.JUnit4Test; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.FlexibleLOF; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LOF; +import de.lmu.ifi.dbs.elki.algorithm.outlier.lof.OnlineLOF; +import de.lmu.ifi.dbs.elki.data.DoubleVector; +import de.lmu.ifi.dbs.elki.data.NumberVector; +import de.lmu.ifi.dbs.elki.data.VectorUtil; +import de.lmu.ifi.dbs.elki.data.type.TypeUtil; +import de.lmu.ifi.dbs.elki.database.HashmapDatabase; +import de.lmu.ifi.dbs.elki.database.UpdatableDatabase; +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.DBIDs; +import de.lmu.ifi.dbs.elki.database.relation.Relation; +import de.lmu.ifi.dbs.elki.database.relation.RelationUtil; +import de.lmu.ifi.dbs.elki.datasource.FileBasedDatabaseConnection; +import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle; +import de.lmu.ifi.dbs.elki.distance.distancefunction.CosineDistanceFunction; +import de.lmu.ifi.dbs.elki.distance.distancefunction.DistanceFunction; +import de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.EuclideanDistanceFunction; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.result.outlier.OutlierResult; +import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil; +import de.lmu.ifi.dbs.elki.utilities.exceptions.UnableToComplyException; +import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization; + +/** + * Tests the OnlineLOF algorithm. Compares the result of the static LOF + * algorithm to the result of the OnlineLOF algorithm, where some insertions and + * deletions (of the previously inserted objects) have been applied to the + * database. + * + * @author Elke Achtert + * + */ +public class TestOnlineLOF implements JUnit4Test { + // the following values depend on the data set used! + static String dataset = "data/testdata/unittests/3clusters-and-noise-2d.csv"; + + // parameter k for LOF and OnlineLOF + static int k = 5; + + // neighborhood distance function for LOF and OnlineLOF + @SuppressWarnings("rawtypes") + static DistanceFunction neighborhoodDistanceFunction = EuclideanDistanceFunction.STATIC; + + // reachability distance function for LOF and OnlineLOF + @SuppressWarnings("rawtypes") + static DistanceFunction reachabilityDistanceFunction = CosineDistanceFunction.STATIC; + + // seed for the generator + static int seed = 5; + + // size of the data set + static int size = 50; + + /** + * First, run the {@link LOF} algorithm on the database. Second, run the + * {@link OnlineLOF} algorithm on the database, insert new objects and + * afterwards delete them. Then, compare the two results for equality. + * + * @throws UnableToComplyException + */ + @SuppressWarnings("unchecked") + @Test + public void testOnlineLOF() throws UnableToComplyException { + UpdatableDatabase db = getDatabase(); + + // 1. Run LOF + FlexibleLOF<DoubleVector, DoubleDistance> lof = new FlexibleLOF<>(k, k, neighborhoodDistanceFunction, reachabilityDistanceFunction); + OutlierResult result1 = lof.run(db); + + // 2. Run OnlineLOF (with insertions and removals) on database + OutlierResult result2 = runOnlineLOF(db); + + // 3. Compare results + Relation<Double> scores1 = result1.getScores(); + Relation<Double> scores2 = result2.getScores(); + + for(DBIDIter id = scores1.getDBIDs().iter(); id.valid(); id.advance()) { + Double lof1 = scores1.get(id); + Double lof2 = scores2.get(id); + assertTrue("lof(" + DBIDUtil.toString(id) + ") != lof(" + DBIDUtil.toString(id) + "): " + lof1 + " != " + lof2, lof1.equals(lof2)); + } + } + + /** + * Run OnlineLOF (with insertions and removals) on database. + */ + @SuppressWarnings("unchecked") + private static OutlierResult runOnlineLOF(UpdatableDatabase db) throws UnableToComplyException { + Relation<DoubleVector> rep = db.getRelation(TypeUtil.DOUBLE_VECTOR_FIELD); + + // setup algorithm + OnlineLOF<DoubleVector, DoubleDistance> lof = new OnlineLOF<>(k, k, neighborhoodDistanceFunction, reachabilityDistanceFunction); + + // run OnlineLOF on database + OutlierResult result = lof.run(db); + + // insert new objects + ArrayList<DoubleVector> insertions = new ArrayList<>(); + NumberVector.Factory<DoubleVector, ?> o = RelationUtil.getNumberVectorFactory(rep); + int dim = RelationUtil.dimensionality(rep); + Random random = new Random(seed); + for(int i = 0; i < size; i++) { + DoubleVector obj = VectorUtil.randomVector(o, dim, random); + insertions.add(obj); + } + DBIDs deletions = db.insert(MultipleObjectsBundle.makeSimple(rep.getDataTypeInformation(), insertions)); + + // delete objects + db.delete(deletions); + + return result; + } + + /** + * Returns the database. + */ + private static UpdatableDatabase getDatabase() { + ListParameterization params = new ListParameterization(); + params.addParameter(FileBasedDatabaseConnection.INPUT_ID, dataset); + + UpdatableDatabase db = ClassGenericsUtil.parameterizeOrAbort(HashmapDatabase.class, params); + params.failOnErrors(); + if(params.hasUnusedParameters()) { + fail("Unused parameters: " + params.getRemainingParameters()); + } + + // get database + db.initialize(); + return db; + } + +} |