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