Search in sources :

Example 21 with ArrayModifiableDBIDs

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

the class DBSCAN method expandCluster.

/**
 * DBSCAN-function expandCluster.
 *
 * Border-Objects become members of the first possible cluster.
 *
 * @param relation Database relation to run on
 * @param rangeQuery Range query to use
 * @param startObjectID potential seed of a new potential cluster
 * @param seeds Array to store the current seeds
 * @param objprog Number of objects processed (may be {@code null})
 * @param clusprog Number of clusters found (may be {@code null})
 */
protected void expandCluster(Relation<O> relation, RangeQuery<O> rangeQuery, DBIDRef startObjectID, ArrayModifiableDBIDs seeds, FiniteProgress objprog, IndefiniteProgress clusprog) {
    DoubleDBIDList neighbors = rangeQuery.getRangeForDBID(startObjectID, epsilon);
    ncounter += neighbors.size();
    // startObject is no core-object
    if (neighbors.size() < minpts) {
        noise.add(startObjectID);
        processedIDs.add(startObjectID);
        if (objprog != null) {
            objprog.incrementProcessed(LOG);
        }
        return;
    }
    ModifiableDBIDs currentCluster = DBIDUtil.newArray();
    currentCluster.add(startObjectID);
    processedIDs.add(startObjectID);
    // try to expand the cluster
    assert (seeds.size() == 0);
    seeds.clear();
    processNeighbors(neighbors.iter(), currentCluster, seeds);
    DBIDVar o = DBIDUtil.newVar();
    while (!seeds.isEmpty()) {
        neighbors = rangeQuery.getRangeForDBID(seeds.pop(o), epsilon);
        ncounter += neighbors.size();
        if (neighbors.size() >= minpts) {
            processNeighbors(neighbors.iter(), currentCluster, seeds);
        }
        if (objprog != null) {
            objprog.incrementProcessed(LOG);
        }
    }
    resultList.add(currentCluster);
    if (clusprog != null) {
        clusprog.setProcessed(resultList.size(), LOG);
    }
}
Also used : DBIDVar(de.lmu.ifi.dbs.elki.database.ids.DBIDVar) DoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDList) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)

Example 22 with ArrayModifiableDBIDs

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

the class RelationSortingTest method testSorting.

@Test
public void testSorting() {
    Database db = AbstractSimpleAlgorithmTest.makeSimpleDatabase(filename, -1);
    Relation<? extends NumberVector> rel = db.getRelation(TypeUtil.NUMBER_VECTOR_FIELD);
    ArrayModifiableDBIDs ids = DBIDUtil.newArray(rel.getDBIDs());
    final int size = rel.size();
    int dims = RelationUtil.dimensionality(rel);
    SortDBIDsBySingleDimension sorter = new VectorUtil.SortDBIDsBySingleDimension(rel);
    for (int d = 0; d < dims; d++) {
        sorter.setDimension(d);
        ids.sort(sorter);
        assertEquals("Lost some DBID during sorting?!?", size, DBIDUtil.newHashSet(ids).size());
        DBIDArrayIter it = ids.iter();
        double prev = rel.get(it).doubleValue(d);
        for (it.advance(); it.valid(); it.advance()) {
            double next = rel.get(it).doubleValue(d);
            assertTrue("Not correctly sorted: " + prev + " > " + next + " at pos " + it.getOffset(), prev <= next);
            prev = next;
        }
    }
}
Also used : SortDBIDsBySingleDimension(de.lmu.ifi.dbs.elki.data.VectorUtil.SortDBIDsBySingleDimension) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test) AbstractSimpleAlgorithmTest(de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest)

Example 23 with ArrayModifiableDBIDs

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

the class P3C method partitionData.

/**
 * Partition the data set into {@code bins} bins in each dimension
 * <i>independently</i>.
 *
 * This can be used to construct a grid approximation of the data using O(d n)
 * memory.
 *
 * When a dimension is found to be constant, it will not be partitioned, but
 * instead the corresponding array will be set to {@code null}.
 *
 * @param relation Data relation to partition
 * @param bins Number of bins
 * @return Partitions of each dimension.
 */
private SetDBIDs[][] partitionData(final Relation<V> relation, final int bins) {
    final int dim = RelationUtil.dimensionality(relation);
    SetDBIDs[][] partitions = new SetDBIDs[dim][bins];
    ArrayModifiableDBIDs ids = DBIDUtil.newArray(relation.getDBIDs());
    // will be reused.
    DBIDArrayIter iter = ids.iter();
    SortDBIDsBySingleDimension sorter = new VectorUtil.SortDBIDsBySingleDimension(relation, 0);
    for (int d = 0; d < dim; d++) {
        sorter.setDimension(d);
        ids.sort(sorter);
        // Minimum:
        iter.seek(0);
        double min = relation.get(iter).doubleValue(d);
        // Extend:
        iter.seek(ids.size() - 1);
        double delta = (relation.get(iter).doubleValue(d) - min) / bins;
        if (delta > 0.) {
            SetDBIDs[] dimparts = partitions[d];
            double split = min + delta;
            HashSetModifiableDBIDs pids = DBIDUtil.newHashSet();
            dimparts[0] = pids;
            int i = 0;
            for (iter.seek(0); iter.valid(); iter.advance()) {
                final double v = relation.get(iter).doubleValue(d);
                if (v <= split || i == dimparts.length - 1) {
                    pids.add(iter);
                } else {
                    i++;
                    split += delta;
                    pids = DBIDUtil.newHashSet();
                    dimparts[i] = pids;
                }
            }
            for (++i; i < dimparts.length; ++i) {
                dimparts[i] = pids;
            }
        } else {
            // Flag whole dimension as bad
            partitions[d] = null;
        }
    }
    return partitions;
}
Also used : HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) SortDBIDsBySingleDimension(de.lmu.ifi.dbs.elki.data.VectorUtil.SortDBIDsBySingleDimension) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) SetDBIDs(de.lmu.ifi.dbs.elki.database.ids.SetDBIDs)

Example 24 with ArrayModifiableDBIDs

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

the class PROCLUS method computeM_current.

/**
 * Computes the set of medoids in current iteration.
 *
 * @param m the medoids
 * @param m_best the best set of medoids found so far
 * @param m_bad the bad medoids
 * @param random random number generator
 * @return m_current, the set of medoids in current iteration
 */
private ArrayDBIDs computeM_current(DBIDs m, DBIDs m_best, DBIDs m_bad, Random random) {
    ArrayModifiableDBIDs m_list = DBIDUtil.newArray(m);
    m_list.removeDBIDs(m_best);
    DBIDArrayMIter it = m_list.iter();
    ArrayModifiableDBIDs m_current = DBIDUtil.newArray();
    for (DBIDIter iter = m_best.iter(); iter.valid(); iter.advance()) {
        if (m_bad.contains(iter)) {
            int currentSize = m_current.size();
            while (m_current.size() == currentSize) {
                m_current.add(it.seek(random.nextInt(m_list.size())));
                it.remove();
            }
        } else {
            m_current.add(iter);
        }
    }
    return m_current;
}
Also used : DBIDArrayMIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayMIter) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 25 with ArrayModifiableDBIDs

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

the class SOD method getNearestNeighbors.

/**
 * Get the k nearest neighbors in terms of the shared nearest neighbor
 * distance.
 *
 * The query object is excluded from the knn list.
 *
 * FIXME: move this to the database layer.
 *
 * @param relation the database holding the objects
 * @param simQ similarity function
 * @param queryObject the query object for which the kNNs should be determined
 * @return the k nearest neighbors in terms of the shared nearest neighbor
 *         distance without the query object
 */
private DBIDs getNearestNeighbors(Relation<V> relation, SimilarityQuery<V> simQ, DBIDRef queryObject) {
    Heap<DoubleDBIDPair> nearestNeighbors = new TiedTopBoundedHeap<>(knn);
    for (DBIDIter iter = relation.iterDBIDs(); iter.valid(); iter.advance()) {
        if (DBIDUtil.equal(iter, queryObject)) {
            continue;
        }
        double sim = simQ.similarity(queryObject, iter);
        if (sim > 0.) {
            nearestNeighbors.add(DBIDUtil.newPair(sim, iter));
        }
    }
    // Collect DBIDs
    ArrayModifiableDBIDs dbids = DBIDUtil.newArray(nearestNeighbors.size());
    while (nearestNeighbors.size() > 0) {
        dbids.add(nearestNeighbors.poll());
    }
    return dbids;
}
Also used : TiedTopBoundedHeap(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DoubleDBIDPair(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)49 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)23 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)12 ArrayList (java.util.ArrayList)11 DBIDVar (de.lmu.ifi.dbs.elki.database.ids.DBIDVar)10 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)9 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)8 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)7 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)6 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)6 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)5 DBIDRef (de.lmu.ifi.dbs.elki.database.ids.DBIDRef)5 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)5 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)4 SortDBIDsBySingleDimension (de.lmu.ifi.dbs.elki.data.VectorUtil.SortDBIDsBySingleDimension)4 DoubleDBIDListIter (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter)4 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)4 ModifiableDoubleDBIDList (de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList)4 SetDBIDs (de.lmu.ifi.dbs.elki.database.ids.SetDBIDs)4 Pair (de.lmu.ifi.dbs.elki.utilities.pairs.Pair)4