Search in sources :

Example 16 with ArrayModifiableDBIDs

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

the class AbstractBiclustering method rowsBitsetToIDs.

/**
 * Convert a bitset into integer row ids.
 *
 * @param rows
 * @return integer row ids
 */
protected ArrayDBIDs rowsBitsetToIDs(BitSet rows) {
    ArrayModifiableDBIDs rowIDs = DBIDUtil.newArray(rows.cardinality());
    DBIDArrayIter iter = this.rowIDs.iter();
    for (int i = rows.nextSetBit(0); i >= 0; i = rows.nextSetBit(i + 1)) {
        iter.seek(i);
        rowIDs.add(iter);
    }
    return rowIDs;
}
Also used : ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)

Example 17 with ArrayModifiableDBIDs

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

the class ClusteringAlgorithmUtil method partitionsFromIntegerLabels.

/**
 * Collect clusters from their [0;k-1] integer labels.
 *
 * @param ids Objects
 * @param assignment Cluster assignment
 * @param k Number of labels (must be labeled 0 to k-1)
 * @return Partitions
 */
public static ArrayModifiableDBIDs[] partitionsFromIntegerLabels(DBIDs ids, IntegerDataStore assignment, int k) {
    int[] sizes = new int[k];
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        sizes[assignment.intValue(iter)] += 1;
    }
    ArrayModifiableDBIDs[] clusters = new ArrayModifiableDBIDs[k];
    for (int i = 0; i < k; i++) {
        clusters[i] = DBIDUtil.newArray(sizes[i]);
    }
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        clusters[assignment.intValue(iter)].add(iter);
    }
    return clusters;
}
Also used : ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 18 with ArrayModifiableDBIDs

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

the class SameSizeKMeansAlgorithm method initialAssignment.

protected ArrayModifiableDBIDs initialAssignment(List<ModifiableDBIDs> clusters, final WritableDataStore<Meta> metas, DBIDs ids) {
    // Build a sorted list of objects, by descending distance delta
    ArrayModifiableDBIDs tids = DBIDUtil.newArray(ids);
    // Our desired cluster size:
    // rounded up
    final int maxsize = (tids.size() + k - 1) / k;
    // Comparator: sort by largest benefit of assigning to preferred cluster.
    final Comparator<DBIDRef> comp = new Comparator<DBIDRef>() {

        @Override
        public int compare(DBIDRef o1, DBIDRef o2) {
            Meta c1 = metas.get(o1), c2 = metas.get(o2);
            return -Double.compare(c1.priority(), c2.priority());
        }
    };
    // We will use this iterator below. It allows seeking!
    DBIDArrayIter id = tids.iter();
    // Initialization phase:
    for (int start = 0; start < tids.size(); ) {
        tids.sort(start, tids.size(), comp);
        for (id.seek(start); id.valid(); id.advance()) {
            Meta c = metas.get(id);
            // Assigning to best cluster - which cannot be full yet!
            ModifiableDBIDs cluster = clusters.get(c.primary);
            assert (cluster.size() <= maxsize);
            cluster.add(id);
            start++;
            // Now the cluster may have become completely filled:
            if (cluster.size() == maxsize) {
                final int full = c.primary;
                // Refresh the not yet assigned objects where necessary:
                for (id.advance(); id.valid(); id.advance()) {
                    Meta ca = metas.get(id);
                    if (ca.primary == full) {
                        // Update the best index:
                        for (int i = 0; i < k; i++) {
                            if (i == full || clusters.get(i).size() >= maxsize) {
                                continue;
                            }
                            if (ca.primary == full || ca.dists[i] < ca.dists[ca.primary]) {
                                ca.primary = i;
                            }
                        }
                        // Changed.
                        metas.put(id, ca);
                    }
                }
                // not really necessary - iterator is at end anyway.
                break;
            }
        }
    // Note: we expect Candidate.a == cluster the object is assigned to!
    }
    return tids;
}
Also used : ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DBIDRef(de.lmu.ifi.dbs.elki.database.ids.DBIDRef) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) IntegerComparator(de.lmu.ifi.dbs.elki.utilities.datastructures.arrays.IntegerComparator) Comparator(java.util.Comparator)

Example 19 with ArrayModifiableDBIDs

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

the class SNNClustering method expandCluster.

/**
 * DBSCAN-function expandCluster adapted to SNN criterion.
 * <p/>
 * <p/>
 * Border-Objects become members of the first possible cluster.
 *
 * @param snnInstance shared nearest neighbors
 * @param startObjectID potential seed of a new potential cluster
 * @param objprog the progress object to report about the progress of
 *        clustering
 */
protected void expandCluster(SimilarityQuery<O> snnInstance, DBIDRef startObjectID, FiniteProgress objprog, IndefiniteProgress clusprog) {
    ArrayModifiableDBIDs seeds = findSNNNeighbors(snnInstance, startObjectID);
    // startObject is no core-object
    if (seeds.size() < minpts) {
        noise.add(startObjectID);
        processedIDs.add(startObjectID);
        if (objprog != null && clusprog != null) {
            objprog.setProcessed(processedIDs.size(), LOG);
            clusprog.setProcessed(resultList.size(), LOG);
        }
        return;
    }
    // try to expand the cluster
    ModifiableDBIDs currentCluster = DBIDUtil.newArray();
    for (DBIDIter seed = seeds.iter(); seed.valid(); seed.advance()) {
        if (!processedIDs.contains(seed)) {
            currentCluster.add(seed);
            processedIDs.add(seed);
        } else if (noise.contains(seed)) {
            currentCluster.add(seed);
            noise.remove(seed);
        }
    }
    DBIDVar o = DBIDUtil.newVar();
    while (seeds.size() > 0) {
        seeds.pop(o);
        ArrayModifiableDBIDs neighborhood = findSNNNeighbors(snnInstance, o);
        if (neighborhood.size() >= minpts) {
            for (DBIDIter iter = neighborhood.iter(); iter.valid(); iter.advance()) {
                boolean inNoise = noise.contains(iter);
                boolean unclassified = !processedIDs.contains(iter);
                if (inNoise || unclassified) {
                    if (unclassified) {
                        seeds.add(iter);
                    }
                    currentCluster.add(iter);
                    processedIDs.add(iter);
                    if (inNoise) {
                        noise.remove(iter);
                    }
                }
            }
        }
        if (objprog != null && clusprog != null) {
            objprog.setProcessed(processedIDs.size(), LOG);
            int numClusters = currentCluster.size() > minpts ? resultList.size() + 1 : resultList.size();
            clusprog.setProcessed(numClusters, LOG);
        }
        if (processedIDs.size() == snnInstance.getRelation().size() && noise.size() == 0) {
            break;
        }
    }
    if (currentCluster.size() >= minpts) {
        resultList.add(currentCluster);
    } else {
        noise.addDBIDs(currentCluster);
        noise.add(startObjectID);
        processedIDs.add(startObjectID);
    }
}
Also used : DBIDVar(de.lmu.ifi.dbs.elki.database.ids.DBIDVar) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 20 with ArrayModifiableDBIDs

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

the class DBSCAN method runDBSCAN.

/**
 * Run the DBSCAN algorithm
 *
 * @param relation Data relation
 * @param rangeQuery Range query class
 */
protected void runDBSCAN(Relation<O> relation, RangeQuery<O> rangeQuery) {
    final int size = relation.size();
    FiniteProgress objprog = LOG.isVerbose() ? new FiniteProgress("Processing objects", size, LOG) : null;
    IndefiniteProgress clusprog = LOG.isVerbose() ? new IndefiniteProgress("Number of clusters", LOG) : null;
    processedIDs = DBIDUtil.newHashSet(size);
    ArrayModifiableDBIDs seeds = DBIDUtil.newArray();
    for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
        if (!processedIDs.contains(iditer)) {
            expandCluster(relation, rangeQuery, iditer, seeds, objprog, clusprog);
        }
        if (objprog != null && clusprog != null) {
            objprog.setProcessed(processedIDs.size(), LOG);
            clusprog.setProcessed(resultList.size(), LOG);
        }
        if (processedIDs.size() == size) {
            break;
        }
    }
    // Finish progress logging
    LOG.ensureCompleted(objprog);
    LOG.setCompleted(clusprog);
}
Also used : ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) IndefiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.IndefiniteProgress) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) 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