Search in sources :

Example 76 with ModifiableDBIDs

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

the class ByLabelHierarchicalClustering method assign.

/**
 * Assigns the specified id to the labelMap according to its label
 *
 * @param labelMap the mapping of label to ids
 * @param label the label of the object to be assigned
 * @param id the id of the object to be assigned
 */
private void assign(HashMap<String, DBIDs> labelMap, String label, DBIDRef id) {
    if (labelMap.containsKey(label)) {
        DBIDs exist = labelMap.get(label);
        if (exist instanceof DBID) {
            ModifiableDBIDs n = DBIDUtil.newHashSet();
            n.add((DBID) exist);
            n.add(id);
            labelMap.put(label, n);
        } else {
            assert (exist instanceof HashSetModifiableDBIDs);
            assert (exist.size() > 1);
            ((ModifiableDBIDs) exist).add(id);
        }
    } else {
        labelMap.put(label, DBIDUtil.deref(id));
    }
}
Also used : HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)

Example 77 with ModifiableDBIDs

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

the class ByLabelHierarchicalClustering method run.

/**
 * Run the actual clustering algorithm.
 *
 * @param relation The data input to use
 */
public Clustering<Model> run(Relation<?> relation) {
    HashMap<String, DBIDs> labelmap = new HashMap<>();
    ModifiableDBIDs noiseids = DBIDUtil.newArray();
    Clustering<Model> clustering = new Clustering<>("By Label Hierarchical Clustering", "bylabel-clustering");
    for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
        final Object val = relation.get(iditer);
        if (val == null) {
            noiseids.add(iditer);
            continue;
        }
        String label = val.toString();
        assign(labelmap, label, iditer);
    }
    ArrayList<Cluster<Model>> clusters = new ArrayList<>(labelmap.size());
    for (Entry<String, DBIDs> entry : labelmap.entrySet()) {
        DBIDs ids = entry.getValue();
        if (ids instanceof DBID) {
            noiseids.add((DBID) ids);
            continue;
        }
        Cluster<Model> clus = new Cluster<Model>(entry.getKey(), ids, ClusterModel.CLUSTER);
        clusters.add(clus);
    }
    for (Cluster<Model> cur : clusters) {
        boolean isrootcluster = true;
        for (Cluster<Model> oth : clusters) {
            if (oth != cur && oth.getName().startsWith(cur.getName())) {
                clustering.addChildCluster(oth, cur);
                if (LOG.isDebuggingFiner()) {
                    LOG.debugFiner(oth.getName() + " is a child of " + cur.getName());
                }
                isrootcluster = false;
            }
        }
        if (isrootcluster) {
            clustering.addToplevelCluster(cur);
        }
    }
    // Collected noise IDs.
    if (noiseids.size() > 0) {
        Cluster<Model> c = new Cluster<Model>("Noise", noiseids, ClusterModel.CLUSTER);
        c.setNoise(true);
        clustering.addToplevelCluster(c);
    }
    return clustering;
}
Also used : HashMap(java.util.HashMap) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) ArrayList(java.util.ArrayList) Cluster(de.lmu.ifi.dbs.elki.data.Cluster) Clustering(de.lmu.ifi.dbs.elki.data.Clustering) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) ClusterModel(de.lmu.ifi.dbs.elki.data.model.ClusterModel) Model(de.lmu.ifi.dbs.elki.data.model.Model) HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)

Example 78 with ModifiableDBIDs

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

the class RdKNNTree method doBulkReverseKNN.

/**
 * Performs a bulk reverse knn query in the specified subtree.
 *
 * @param node the root node of the current subtree
 * @param ids the object ids for which the rknn query is performed
 * @param result the map containing the query results for each object
 */
private void doBulkReverseKNN(RdKNNNode node, DBIDs ids, Map<DBID, ModifiableDoubleDBIDList> result) {
    if (node.isLeaf()) {
        for (int i = 0; i < node.getNumEntries(); i++) {
            RdKNNLeafEntry entry = (RdKNNLeafEntry) node.getEntry(i);
            for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
                DBID id = DBIDUtil.deref(iter);
                double distance = distanceQuery.distance(entry.getDBID(), id);
                if (distance <= entry.getKnnDistance()) {
                    result.get(id).add(distance, entry.getDBID());
                }
            }
        }
    } else // node is a inner node
    {
        for (int i = 0; i < node.getNumEntries(); i++) {
            RdKNNDirectoryEntry entry = (RdKNNDirectoryEntry) node.getEntry(i);
            ModifiableDBIDs candidates = DBIDUtil.newArray();
            for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
                DBID id = DBIDUtil.deref(iter);
                double minDist = distanceQuery.minDist(entry, id);
                if (minDist <= entry.getKnnDistance()) {
                    candidates.add(id);
                }
                if (!candidates.isEmpty()) {
                    doBulkReverseKNN(getNode(entry), candidates, result);
                }
            }
        }
    }
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) 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 79 with ModifiableDBIDs

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

the class DWOF method clusterData.

/**
 * This method applies a density based clustering algorithm.
 *
 * It looks for an unclustered object and builds a new cluster for it, then
 * adds all the points within its radius to that cluster.
 *
 * nChain represents the points to be added to the cluster and its
 * radius-neighbors
 *
 * @param ids Database IDs to process
 * @param rnnQuery Data to process
 * @param radii Radii to cluster accordingly
 * @param labels Label storage.
 */
private void clusterData(DBIDs ids, RangeQuery<O> rnnQuery, WritableDoubleDataStore radii, WritableDataStore<ModifiableDBIDs> labels) {
    FiniteProgress clustProg = LOG.isVerbose() ? new FiniteProgress("Density-Based Clustering", ids.size(), LOG) : null;
    // Iterate over all objects
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        if (labels.get(iter) != null) {
            continue;
        }
        ModifiableDBIDs newCluster = DBIDUtil.newArray();
        newCluster.add(iter);
        labels.put(iter, newCluster);
        LOG.incrementProcessed(clustProg);
        // container of the points to be added and their radii neighbors to the
        // cluster
        ModifiableDBIDs nChain = DBIDUtil.newArray();
        nChain.add(iter);
        // iterate over nChain
        for (DBIDIter toGetNeighbors = nChain.iter(); toGetNeighbors.valid(); toGetNeighbors.advance()) {
            double range = radii.doubleValue(toGetNeighbors);
            DoubleDBIDList nNeighbors = rnnQuery.getRangeForDBID(toGetNeighbors, range);
            for (DoubleDBIDListIter iter2 = nNeighbors.iter(); iter2.valid(); iter2.advance()) {
                if (DBIDUtil.equal(toGetNeighbors, iter2)) {
                    continue;
                }
                if (labels.get(iter2) == null) {
                    newCluster.add(iter2);
                    labels.put(iter2, newCluster);
                    nChain.add(iter2);
                    LOG.incrementProcessed(clustProg);
                } else if (labels.get(iter2) != newCluster) {
                    ModifiableDBIDs toBeDeleted = labels.get(iter2);
                    newCluster.addDBIDs(toBeDeleted);
                    for (DBIDIter iter3 = toBeDeleted.iter(); iter3.valid(); iter3.advance()) {
                        labels.put(iter3, newCluster);
                    }
                    toBeDeleted.clear();
                }
            }
        }
    }
    LOG.ensureCompleted(clustProg);
}
Also used : DoubleDBIDListIter(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) DoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDList) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 80 with ModifiableDBIDs

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

the class MkMaxTree method reverseKNNQuery.

/**
 * Performs a reverse k-nearest neighbor query for the given object ID. In the
 * first step the candidates are chosen by performing a reverse k-nearest
 * neighbor query with k = {@link #getKmax()}. Then these candidates are refined
 * in a second step.
 */
@Override
public DoubleDBIDList reverseKNNQuery(DBIDRef id, int k) {
    if (k > this.getKmax()) {
        throw new IllegalArgumentException("Parameter k has to be equal or less than " + "parameter k of the MkMax-Tree!");
    }
    // get the candidates
    ModifiableDoubleDBIDList candidates = DBIDUtil.newDistanceDBIDList();
    doReverseKNNQuery(id, getRoot(), null, candidates);
    if (k == this.getKmax()) {
        candidates.sort();
        // rkNNStatistics.addResults(candidates.size());
        return candidates;
    }
    // refinement of candidates
    ModifiableDBIDs candidateIDs = DBIDUtil.newArray(candidates.size());
    for (DBIDIter candidate = candidates.iter(); candidate.valid(); candidate.advance()) {
        candidateIDs.add(candidate);
    }
    Map<DBID, KNNList> knnLists = batchNN(getRoot(), candidateIDs, k);
    ModifiableDoubleDBIDList result = DBIDUtil.newDistanceDBIDList();
    for (DBIDIter iter = candidateIDs.iter(); iter.valid(); iter.advance()) {
        DBID cid = DBIDUtil.deref(iter);
        KNNList cands = knnLists.get(cid);
        for (DoubleDBIDListIter iter2 = cands.iter(); iter2.valid(); iter2.advance()) {
            if (DBIDUtil.equal(id, iter2)) {
                result.add(iter2.doubleValue(), cid);
                break;
            }
        }
    }
    // FIXME: re-add statistics.
    // rkNNStatistics.addResults(result.size());
    // rkNNStatistics.addCandidates(candidates.size());
    result.sort();
    return result;
}
Also used : DoubleDBIDListIter(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter) ModifiableDoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList) KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)80 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)44 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)30 ArrayList (java.util.ArrayList)30 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)28 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)18 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)15 WritableIntegerDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableIntegerDataStore)14 IndefiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.IndefiniteProgress)14 DoubleStatistic (de.lmu.ifi.dbs.elki.logging.statistics.DoubleStatistic)12 LongStatistic (de.lmu.ifi.dbs.elki.logging.statistics.LongStatistic)12 Model (de.lmu.ifi.dbs.elki.data.model.Model)11 DBID (de.lmu.ifi.dbs.elki.database.ids.DBID)11 KMeansModel (de.lmu.ifi.dbs.elki.data.model.KMeansModel)10 StringStatistic (de.lmu.ifi.dbs.elki.logging.statistics.StringStatistic)10 Cluster (de.lmu.ifi.dbs.elki.data.Cluster)9 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)9 HashSetModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs)8 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)8 ClusterModel (de.lmu.ifi.dbs.elki.data.model.ClusterModel)7