Search in sources :

Example 1 with Algorithm

use of de.lmu.ifi.dbs.elki.algorithm.Algorithm in project elki by elki-project.

the class SimpleOutlierEnsemble method run.

@Override
public OutlierResult run(Database database) throws IllegalStateException {
    int num = algorithms.size();
    // Run inner outlier algorithms
    ModifiableDBIDs ids = DBIDUtil.newHashSet();
    ArrayList<OutlierResult> results = new ArrayList<>(num);
    {
        FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Inner outlier algorithms", num, LOG) : null;
        for (Algorithm alg : algorithms) {
            Result res = alg.run(database);
            List<OutlierResult> ors = OutlierResult.getOutlierResults(res);
            for (OutlierResult or : ors) {
                results.add(or);
                ids.addDBIDs(or.getScores().getDBIDs());
            }
            LOG.incrementProcessed(prog);
        }
        LOG.ensureCompleted(prog);
    }
    // Combine
    WritableDoubleDataStore sumscore = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_STATIC);
    DoubleMinMax minmax = new DoubleMinMax();
    {
        FiniteProgress cprog = LOG.isVerbose() ? new FiniteProgress("Combining results", ids.size(), LOG) : null;
        for (DBIDIter id = ids.iter(); id.valid(); id.advance()) {
            double[] scores = new double[num];
            int i = 0;
            for (OutlierResult r : results) {
                double score = r.getScores().doubleValue(id);
                if (!Double.isNaN(score)) {
                    scores[i] = score;
                    i++;
                } else {
                    LOG.warning("DBID " + id + " was not given a score by result " + r);
                }
            }
            if (i > 0) {
                // Shrink array if necessary.
                if (i < scores.length) {
                    scores = Arrays.copyOf(scores, i);
                }
                double combined = voting.combine(scores);
                sumscore.putDouble(id, combined);
                minmax.put(combined);
            } else {
                LOG.warning("DBID " + id + " was not given any score at all.");
            }
            LOG.incrementProcessed(cprog);
        }
        LOG.ensureCompleted(cprog);
    }
    OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax());
    DoubleRelation scores = new MaterializedDoubleRelation("Simple Outlier Ensemble", "ensemble-outlier", sumscore, ids);
    return new OutlierResult(meta, scores);
}
Also used : WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) ArrayList(java.util.ArrayList) Algorithm(de.lmu.ifi.dbs.elki.algorithm.Algorithm) OutlierAlgorithm(de.lmu.ifi.dbs.elki.algorithm.outlier.OutlierAlgorithm) AbstractAlgorithm(de.lmu.ifi.dbs.elki.algorithm.AbstractAlgorithm) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation) BasicOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta) OutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta) BasicOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta) Result(de.lmu.ifi.dbs.elki.result.Result) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) ArrayList(java.util.ArrayList) List(java.util.List) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)

Example 2 with Algorithm

use of de.lmu.ifi.dbs.elki.algorithm.Algorithm in project elki by elki-project.

the class AlgorithmStep method runAlgorithms.

/**
 * Run algorithms.
 *
 * @param database Database
 * @return Algorithm result
 */
public Result runAlgorithms(Database database) {
    ResultHierarchy hier = database.getHierarchy();
    if (LOG.isStatistics()) {
        boolean first = true;
        for (It<Index> it = hier.iterDescendants(database).filter(Index.class); it.valid(); it.advance()) {
            if (first) {
                LOG.statistics("Index statistics before running algorithms:");
                first = false;
            }
            it.get().logStatistics();
        }
    }
    stepresult = new BasicResult("Algorithm Step", "algorithm-step");
    for (Algorithm algorithm : algorithms) {
        Thread.currentThread().setName(algorithm.toString());
        Duration duration = LOG.isStatistics() ? LOG.newDuration(algorithm.getClass().getName() + ".runtime").begin() : null;
        Result res = algorithm.run(database);
        if (duration != null) {
            LOG.statistics(duration.end());
        }
        if (LOG.isStatistics()) {
            boolean first = true;
            for (It<Index> it = hier.iterDescendants(database).filter(Index.class); it.valid(); it.advance()) {
                if (first) {
                    LOG.statistics("Index statistics after running algorithm " + algorithm.toString() + ":");
                    first = false;
                }
                it.get().logStatistics();
            }
        }
        if (res != null) {
            // Make sure the result is attached, but usually this is a noop:
            hier.add(database, res);
        }
    }
    return stepresult;
}
Also used : ResultHierarchy(de.lmu.ifi.dbs.elki.result.ResultHierarchy) BasicResult(de.lmu.ifi.dbs.elki.result.BasicResult) Index(de.lmu.ifi.dbs.elki.index.Index) Duration(de.lmu.ifi.dbs.elki.logging.statistics.Duration) Algorithm(de.lmu.ifi.dbs.elki.algorithm.Algorithm) AbstractAlgorithm(de.lmu.ifi.dbs.elki.algorithm.AbstractAlgorithm) Result(de.lmu.ifi.dbs.elki.result.Result) BasicResult(de.lmu.ifi.dbs.elki.result.BasicResult)

Aggregations

AbstractAlgorithm (de.lmu.ifi.dbs.elki.algorithm.AbstractAlgorithm)2 Algorithm (de.lmu.ifi.dbs.elki.algorithm.Algorithm)2 Result (de.lmu.ifi.dbs.elki.result.Result)2 OutlierAlgorithm (de.lmu.ifi.dbs.elki.algorithm.outlier.OutlierAlgorithm)1 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)1 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)1 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)1 MaterializedDoubleRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)1 Index (de.lmu.ifi.dbs.elki.index.Index)1 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)1 Duration (de.lmu.ifi.dbs.elki.logging.statistics.Duration)1 DoubleMinMax (de.lmu.ifi.dbs.elki.math.DoubleMinMax)1 BasicResult (de.lmu.ifi.dbs.elki.result.BasicResult)1 ResultHierarchy (de.lmu.ifi.dbs.elki.result.ResultHierarchy)1 BasicOutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta)1 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)1 OutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1