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));
}
}
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;
}
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);
}
}
}
}
}
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);
}
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;
}
Aggregations