Search in sources :

Example 61 with KNNList

use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.

the class LoOP method computePLOFs.

/**
 * Compute the LOF values, using the pdist distances.
 *
 * @param relation Data relation
 * @param knn kNN query
 * @param pdists Precomputed distances
 * @param plofs Storage for PLOFs.
 * @return Normalization factor.
 */
protected double computePLOFs(Relation<O> relation, KNNQuery<O> knn, WritableDoubleDataStore pdists, WritableDoubleDataStore plofs) {
    FiniteProgress progressPLOFs = LOG.isVerbose() ? new FiniteProgress("PLOFs for objects", relation.size(), LOG) : null;
    double nplof = 0.;
    for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
        // + query
        final KNNList neighbors = knn.getKNNForDBID(iditer, kcomp + 1);
        // point
        // use first kref neighbors as comparison set.
        int ks = 0;
        double sum = 0.;
        for (DBIDIter neighbor = neighbors.iter(); neighbor.valid() && ks < kcomp; neighbor.advance()) {
            if (DBIDUtil.equal(neighbor, iditer)) {
                continue;
            }
            sum += pdists.doubleValue(neighbor);
            ks++;
        }
        double plof = MathUtil.max(pdists.doubleValue(iditer) * ks / sum, 1.0);
        if (Double.isNaN(plof) || Double.isInfinite(plof)) {
            plof = 1.0;
        }
        plofs.putDouble(iditer, plof);
        nplof += (plof - 1.0) * (plof - 1.0);
        LOG.incrementProcessed(progressPLOFs);
    }
    LOG.ensureCompleted(progressPLOFs);
    nplof = lambda * FastMath.sqrt(nplof / relation.size());
    if (LOG.isDebuggingFine()) {
        LOG.debugFine("nplof normalization factor is " + nplof);
    }
    return nplof > 0. ? nplof : 1.;
}
Also used : KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 62 with KNNList

use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.

the class ParallelLOF method run.

public OutlierResult run(Database database, Relation<O> relation) {
    DBIDs ids = relation.getDBIDs();
    DistanceQuery<O> distq = database.getDistanceQuery(relation, getDistanceFunction());
    KNNQuery<O> knnq = database.getKNNQuery(distq, k + 1);
    // Phase one: KNN and k-dist
    WritableDoubleDataStore kdists = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
    WritableDataStore<KNNList> knns = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, KNNList.class);
    {
        // Compute kNN
        KNNProcessor<O> knnm = new KNNProcessor<>(k + 1, knnq);
        SharedObject<KNNList> knnv = new SharedObject<>();
        WriteDataStoreProcessor<KNNList> storek = new WriteDataStoreProcessor<>(knns);
        knnm.connectKNNOutput(knnv);
        storek.connectInput(knnv);
        // Compute k-dist
        KDistanceProcessor kdistm = new KDistanceProcessor(k + 1);
        SharedDouble kdistv = new SharedDouble();
        WriteDoubleDataStoreProcessor storem = new WriteDoubleDataStoreProcessor(kdists);
        kdistm.connectKNNInput(knnv);
        kdistm.connectOutput(kdistv);
        storem.connectInput(kdistv);
        ParallelExecutor.run(ids, knnm, storek, kdistm, storem);
    }
    // Phase two: lrd
    WritableDoubleDataStore lrds = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
    {
        LRDProcessor lrdm = new LRDProcessor(knns, kdists);
        SharedDouble lrdv = new SharedDouble();
        WriteDoubleDataStoreProcessor storelrd = new WriteDoubleDataStoreProcessor(lrds);
        lrdm.connectOutput(lrdv);
        storelrd.connectInput(lrdv);
        ParallelExecutor.run(ids, lrdm, storelrd);
    }
    // No longer needed.
    kdists.destroy();
    kdists = null;
    // Phase three: LOF
    WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
    DoubleMinMax minmax;
    {
        LOFProcessor lofm = new LOFProcessor(knns, lrds, true);
        SharedDouble lofv = new SharedDouble();
        DoubleMinMaxProcessor mmm = new DoubleMinMaxProcessor();
        WriteDoubleDataStoreProcessor storelof = new WriteDoubleDataStoreProcessor(lofs);
        lofm.connectOutput(lofv);
        mmm.connectInput(lofv);
        storelof.connectInput(lofv);
        ParallelExecutor.run(ids, lofm, storelof, mmm);
        minmax = mmm.getMinMax();
    }
    DoubleRelation scoreres = new MaterializedDoubleRelation("Local Outlier Factor", "lof-outlier", lofs, ids);
    OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
    return new OutlierResult(meta, scoreres);
}
Also used : KDistanceProcessor(de.lmu.ifi.dbs.elki.parallel.processor.KDistanceProcessor) WriteDataStoreProcessor(de.lmu.ifi.dbs.elki.parallel.processor.WriteDataStoreProcessor) SharedDouble(de.lmu.ifi.dbs.elki.parallel.variables.SharedDouble) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) KNNProcessor(de.lmu.ifi.dbs.elki.parallel.processor.KNNProcessor) 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) WriteDoubleDataStoreProcessor(de.lmu.ifi.dbs.elki.parallel.processor.WriteDoubleDataStoreProcessor) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) SharedObject(de.lmu.ifi.dbs.elki.parallel.variables.SharedObject) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation) WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) DoubleMinMaxProcessor(de.lmu.ifi.dbs.elki.parallel.processor.DoubleMinMaxProcessor)

Example 63 with KNNList

use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.

the class SimplifiedLOF method computeSimplifiedLOFs.

/**
 * Compute the simplified LOF factors.
 *
 * @param ids IDs to compute for
 * @param knnq kNN query class
 * @param slrds Object densities
 * @param lofs SLOF output storage
 * @param lofminmax Minimum and maximum scores
 */
private void computeSimplifiedLOFs(DBIDs ids, KNNQuery<O> knnq, WritableDoubleDataStore slrds, WritableDoubleDataStore lofs, DoubleMinMax lofminmax) {
    FiniteProgress progressLOFs = LOG.isVerbose() ? new FiniteProgress("Simplified LOF scores", ids.size(), LOG) : null;
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        final double lof;
        final double lrdp = slrds.doubleValue(iter);
        final KNNList neighbors = knnq.getKNNForDBID(iter, k);
        if (!Double.isInfinite(lrdp)) {
            double sum = 0.;
            int count = 0;
            for (DBIDIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
                // skip the point itself
                if (DBIDUtil.equal(neighbor, iter)) {
                    continue;
                }
                final double val = slrds.doubleValue(neighbor);
                sum += val;
                count++;
                if (Double.isInfinite(val)) {
                    break;
                }
            }
            lof = sum / (lrdp * count);
        } else {
            lof = 1.0;
        }
        lofs.putDouble(iter, lof);
        // update minimum and maximum
        lofminmax.put(lof);
        LOG.incrementProcessed(progressLOFs);
    }
    LOG.ensureCompleted(progressLOFs);
}
Also used : KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 64 with KNNList

use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.

the class ParallelSimplifiedLOF method run.

public OutlierResult run(Database database, Relation<O> relation) {
    DBIDs ids = relation.getDBIDs();
    DistanceQuery<O> distq = database.getDistanceQuery(relation, getDistanceFunction());
    KNNQuery<O> knnq = database.getKNNQuery(distq, k + 1);
    // Phase one: KNN and k-dist
    WritableDataStore<KNNList> knns = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, KNNList.class);
    {
        // Compute kNN
        KNNProcessor<O> knnm = new KNNProcessor<>(k + 1, knnq);
        SharedObject<KNNList> knnv = new SharedObject<>();
        WriteDataStoreProcessor<KNNList> storek = new WriteDataStoreProcessor<>(knns);
        knnm.connectKNNOutput(knnv);
        storek.connectInput(knnv);
        ParallelExecutor.run(ids, knnm, storek);
    }
    // Phase two: simplified-lrd
    WritableDoubleDataStore lrds = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
    {
        SimplifiedLRDProcessor lrdm = new SimplifiedLRDProcessor(knns);
        SharedDouble lrdv = new SharedDouble();
        WriteDoubleDataStoreProcessor storelrd = new WriteDoubleDataStoreProcessor(lrds);
        lrdm.connectOutput(lrdv);
        storelrd.connectInput(lrdv);
        ParallelExecutor.run(ids, lrdm, storelrd);
    }
    // Phase three: Simplified-LOF
    WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB);
    DoubleMinMax minmax;
    {
        LOFProcessor lofm = new LOFProcessor(knns, lrds, true);
        SharedDouble lofv = new SharedDouble();
        DoubleMinMaxProcessor mmm = new DoubleMinMaxProcessor();
        WriteDoubleDataStoreProcessor storelof = new WriteDoubleDataStoreProcessor(lofs);
        lofm.connectOutput(lofv);
        mmm.connectInput(lofv);
        storelof.connectInput(lofv);
        ParallelExecutor.run(ids, lofm, storelof, mmm);
        minmax = mmm.getMinMax();
    }
    DoubleRelation scoreres = new MaterializedDoubleRelation("Simplified Local Outlier Factor", "simplified-lof-outlier", lofs, ids);
    OutlierScoreMeta meta = new BasicOutlierScoreMeta(minmax.getMin(), minmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
    return new OutlierResult(meta, scoreres);
}
Also used : WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) WriteDataStoreProcessor(de.lmu.ifi.dbs.elki.parallel.processor.WriteDataStoreProcessor) SharedDouble(de.lmu.ifi.dbs.elki.parallel.variables.SharedDouble) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) KNNProcessor(de.lmu.ifi.dbs.elki.parallel.processor.KNNProcessor) 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) WriteDoubleDataStoreProcessor(de.lmu.ifi.dbs.elki.parallel.processor.WriteDoubleDataStoreProcessor) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) SharedObject(de.lmu.ifi.dbs.elki.parallel.variables.SharedObject) DoubleMinMaxProcessor(de.lmu.ifi.dbs.elki.parallel.processor.DoubleMinMaxProcessor) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)

Example 65 with KNNList

use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.

the class SimpleKernelDensityLOF method run.

/**
 * Run the naive kernel density LOF algorithm.
 *
 * @param database Database to query
 * @param relation Data to process
 * @return LOF outlier result
 */
public OutlierResult run(Database database, Relation<O> relation) {
    StepProgress stepprog = LOG.isVerbose() ? new StepProgress("KernelDensityLOF", 3) : null;
    final int dim = RelationUtil.dimensionality(relation);
    DBIDs ids = relation.getDBIDs();
    LOG.beginStep(stepprog, 1, "Materializing neighborhoods w.r.t. distance function.");
    KNNQuery<O> knnq = DatabaseUtil.precomputedKNNQuery(database, relation, getDistanceFunction(), k);
    // Compute LRDs
    LOG.beginStep(stepprog, 2, "Computing densities.");
    WritableDoubleDataStore dens = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP);
    FiniteProgress densProgress = LOG.isVerbose() ? new FiniteProgress("Densities", ids.size(), LOG) : null;
    for (DBIDIter it = ids.iter(); it.valid(); it.advance()) {
        final KNNList neighbors = knnq.getKNNForDBID(it, k);
        int count = 0;
        double sum = 0.0;
        // Fast version for double distances
        for (DoubleDBIDListIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
            if (DBIDUtil.equal(neighbor, it)) {
                continue;
            }
            double max = knnq.getKNNForDBID(neighbor, k).getKNNDistance();
            if (max == 0) {
                sum = Double.POSITIVE_INFINITY;
                break;
            }
            final double v = neighbor.doubleValue() / max;
            sum += kernel.density(v) / MathUtil.powi(max, dim);
            count++;
        }
        final double density = count > 0 ? sum / count : 0.;
        dens.putDouble(it, density);
        LOG.incrementProcessed(densProgress);
    }
    LOG.ensureCompleted(densProgress);
    // compute LOF_SCORE of each db object
    LOG.beginStep(stepprog, 3, "Computing KLOFs.");
    WritableDoubleDataStore lofs = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_STATIC);
    // track the maximum value for normalization.
    DoubleMinMax lofminmax = new DoubleMinMax();
    FiniteProgress progressLOFs = LOG.isVerbose() ? new FiniteProgress("KLOF_SCORE for objects", ids.size(), LOG) : null;
    for (DBIDIter it = ids.iter(); it.valid(); it.advance()) {
        final double lrdp = dens.doubleValue(it);
        final double lof;
        if (lrdp > 0) {
            final KNNList neighbors = knnq.getKNNForDBID(it, k);
            double sum = 0.0;
            int count = 0;
            for (DBIDIter neighbor = neighbors.iter(); neighbor.valid(); neighbor.advance()) {
                // skip the point itself
                if (DBIDUtil.equal(neighbor, it)) {
                    continue;
                }
                sum += dens.doubleValue(neighbor);
                count++;
            }
            lof = (lrdp == Double.POSITIVE_INFINITY) ? (sum == Double.POSITIVE_INFINITY ? 1 : 0.) : sum / (count * lrdp);
        } else {
            lof = 1.0;
        }
        lofs.putDouble(it, lof);
        // update minimum and maximum
        lofminmax.put(lof);
        LOG.incrementProcessed(progressLOFs);
    }
    LOG.ensureCompleted(progressLOFs);
    LOG.setCompleted(stepprog);
    // Build result representation.
    DoubleRelation scoreResult = new MaterializedDoubleRelation("Kernel Density Local Outlier Factor", "kernel-density-slof-outlier", lofs, ids);
    OutlierScoreMeta scoreMeta = new QuotientOutlierScoreMeta(lofminmax.getMin(), lofminmax.getMax(), 0.0, Double.POSITIVE_INFINITY, 1.0);
    OutlierResult result = new OutlierResult(scoreMeta, scoreResult);
    return result;
}
Also used : DoubleDBIDListIter(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter) WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) StepProgress(de.lmu.ifi.dbs.elki.logging.progress.StepProgress) QuotientOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.QuotientOutlierScoreMeta) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation) QuotientOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.QuotientOutlierScoreMeta) OutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)

Aggregations

KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)80 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)53 DoubleDBIDListIter (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter)38 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)32 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)21 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)20 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)18 MaterializedDoubleRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)18 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)18 OutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta)18 DoubleMinMax (de.lmu.ifi.dbs.elki.math.DoubleMinMax)15 ArrayList (java.util.ArrayList)11 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)10 ModifiableDoubleDBIDList (de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList)9 BasicOutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta)9 DBID (de.lmu.ifi.dbs.elki.database.ids.DBID)8 KNNHeap (de.lmu.ifi.dbs.elki.database.ids.KNNHeap)8 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)8 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)8 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)6