use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.
the class ClusteringVectorDumper method dumpClusteringOutput.
/**
* Dump a single clustering result.
*
* @param writer Output writer
* @param hierarchy Cluster hierarchy to process
* @param c Clustering result
*/
protected void dumpClusteringOutput(PrintStream writer, ResultHierarchy hierarchy, Clustering<?> c) {
DBIDRange ids = null;
for (It<Relation<?>> iter = hierarchy.iterParents(c).filter(Relation.class); iter.valid(); iter.advance()) {
DBIDs pids = iter.get().getDBIDs();
if (pids instanceof DBIDRange) {
ids = (DBIDRange) pids;
break;
}
LOG.warning("Parent result " + iter.get().getLongName() + " has DBID type " + pids.getClass());
}
// Fallback: try to locate a database.
if (ids == null) {
for (It<Database> iter = hierarchy.iterAll().filter(Database.class); iter.valid(); iter.advance()) {
DBIDs pids = iter.get().getRelation(TypeUtil.ANY).getDBIDs();
if (pids instanceof DBIDRange) {
ids = (DBIDRange) pids;
break;
}
LOG.warning("Parent result " + iter.get().getLongName() + " has DBID type " + pids.getClass());
}
}
if (ids == null) {
LOG.warning("Cannot dump cluster assignment, as I do not have a well-defined DBIDRange to use for a unique column assignment. DBIDs must be a continuous range.");
return;
}
WritableIntegerDataStore map = DataStoreUtil.makeIntegerStorage(ids, DataStoreFactory.HINT_TEMP);
int cnum = 0;
for (Cluster<?> clu : c.getAllClusters()) {
for (DBIDIter iter = clu.getIDs().iter(); iter.valid(); iter.advance()) {
map.putInt(iter, cnum);
}
++cnum;
}
for (DBIDArrayIter iter = ids.iter(); iter.valid(); iter.advance()) {
if (iter.getOffset() > 0) {
writer.append(' ');
}
writer.append(Integer.toString(map.intValue(iter)));
}
if (forceLabel != null) {
if (forceLabel.length() > 0) {
writer.append(' ').append(forceLabel);
}
} else {
writer.append(' ').append(c.getLongName());
}
writer.append('\n');
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.
the class PrecomputedSimilarityMatrix method initialize.
@Override
public void initialize() {
DBIDs rids = relation.getDBIDs();
if (!(rids instanceof DBIDRange)) {
throw new AbortException("Similarity matrixes are currently only supported for DBID ranges (as used by static databases) for performance reasons (Patches welcome).");
}
ids = (DBIDRange) rids;
size = ids.size();
if (size > 65536) {
throw new AbortException("Similarity matrixes currently have a limit of 65536 objects (~16 GB). After this, the array size exceeds the Java integer range, and a different data structure needs to be used.");
}
similarityQuery = similarityFunction.instantiate(relation);
int msize = triangleSize(size);
matrix = new double[msize];
DBIDArrayIter ix = ids.iter(), iy = ids.iter();
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Precomputing similarity matrix", msize, LOG) : null;
int pos = 0;
for (ix.seek(0); ix.valid(); ix.advance()) {
// y < x -- must match {@link #getOffset}!
for (iy.seek(0); iy.getOffset() < ix.getOffset(); iy.advance()) {
matrix[pos] = similarityQuery.similarity(ix, iy);
pos++;
}
if (prog != null) {
prog.setProcessed(prog.getProcessed() + ix.getOffset(), LOG);
}
}
LOG.ensureCompleted(prog);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.
the class PrecomputeDistancesAsciiApplication method run.
@Override
public void run() {
database.initialize();
Relation<O> relation = database.getRelation(distance.getInputTypeRestriction());
DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, distance);
DBIDRange ids = DBIDUtil.assertRange(relation.getDBIDs());
final int size = ids.size();
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Precomputing distances", (int) (((size - 1) * (long) size) >>> 1), LOG) : null;
try (PrintStream fout = openStream(out)) {
DBIDArrayIter id1 = ids.iter(), id2 = ids.iter();
for (; id1.valid(); id1.advance()) {
String idstr1 = Integer.toString(id1.getOffset());
if (debugExtraCheckSymmetry && distanceQuery.distance(id1, id1) != 0.) {
LOG.warning("Distance function doesn't satisfy d(0,0) = 0.");
}
for (id2.seek(id1.getOffset() + 1); id2.valid(); id2.advance()) {
double d = distanceQuery.distance(id1, id2);
if (debugExtraCheckSymmetry) {
double d2 = distanceQuery.distance(id2, id1);
if (Math.abs(d - d2) > 0.0000001) {
LOG.warning("Distance function doesn't appear to be symmetric!");
}
}
//
fout.append(idstr1).append('\t').append(Integer.toString(id2.getOffset())).append(//
'\t').append(Double.toString(d)).append('\n');
}
if (prog != null) {
prog.setProcessed(prog.getProcessed() + (size - id1.getOffset() - 1), LOG);
}
}
} catch (IOException e) {
throw new AbortException("Could not write to output file.", e);
}
LOG.ensureCompleted(prog);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.
the class CacheFloatDistanceInOnDiskMatrix method run.
@Override
public void run() {
database.initialize();
Relation<O> relation = database.getRelation(distance.getInputTypeRestriction());
DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, distance);
DBIDRange ids = DBIDUtil.assertRange(relation.getDBIDs());
int size = ids.size();
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Precomputing distances", (int) (((size + 1) * (long) size) >>> 1), LOG) : null;
try (//
OnDiskUpperTriangleMatrix matrix = new OnDiskUpperTriangleMatrix(out, DiskCacheBasedFloatDistanceFunction.FLOAT_CACHE_MAGIC, 0, ByteArrayUtil.SIZE_FLOAT, size)) {
DBIDArrayIter id1 = ids.iter(), id2 = ids.iter();
for (; id1.valid(); id1.advance()) {
for (id2.seek(id1.getOffset()); id2.valid(); id2.advance()) {
float d = (float) distanceQuery.distance(id1, id2);
if (debugExtraCheckSymmetry) {
float d2 = (float) distanceQuery.distance(id2, id1);
if (Math.abs(d - d2) > 0.0000001) {
LOG.warning("Distance function doesn't appear to be symmetric!");
}
}
try {
matrix.getRecordBuffer(id1.getOffset(), id2.getOffset()).putFloat(d);
} catch (IOException e) {
throw new AbortException("Error writing distance record " + DBIDUtil.toString(id1) + "," + DBIDUtil.toString(id2) + " to matrix.", e);
}
}
if (prog != null) {
prog.setProcessed(prog.getProcessed() + (size - id1.getOffset()), LOG);
}
}
} catch (IOException e) {
throw new AbortException("Error precomputing distance matrix.", e);
}
prog.ensureCompleted(LOG);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.
the class WeightedQuickUnionRangeDBIDsTest method testTree.
@Test
public void testTree() {
DBIDRange range = DBIDUtil.generateStaticDBIDRange(8);
UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
DBIDArrayIter i1 = range.iter(), i2 = range.iter();
assertFalse(uf.isConnected(i1.seek(0), i2.seek(7)));
uf.union(i1.seek(0), i2.seek(1));
assertTrue(uf.isConnected(i1.seek(0), i2.seek(1)));
uf.union(i1.seek(2), i2.seek(3));
assertFalse(uf.isConnected(i1.seek(0), i2.seek(2)));
uf.union(i1.seek(0), i2.seek(2));
assertTrue(uf.isConnected(i1.seek(3), i2.seek(1)));
uf.union(i1.seek(4), i2.seek(5));
uf.union(i1.seek(6), i2.seek(7));
uf.union(i1.seek(4), i2.seek(6));
assertFalse(uf.isConnected(i1.seek(0), i2.seek(4)));
uf.union(i1.seek(0), i2.seek(4));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
assertTrue(uf.isConnected(i1.seek(i), i2.seek(j)));
}
}
}
Aggregations