use of de.lmu.ifi.dbs.elki.database.ids.DBIDIter in project elki by elki-project.
the class OPTICSPlot method replot.
/**
* Trigger a redraw of the OPTICS plot
*/
public void replot() {
width = co.size();
height = (int) Math.ceil(width * .2);
ratio = width / (double) height;
height = height < MIN_HEIGHT ? MIN_HEIGHT : height > MAX_HEIGHT ? MAX_HEIGHT : height;
if (scale == null) {
scale = computeScale(co);
}
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int x = 0;
for (DBIDIter it = co.iter(); it.valid(); it.advance()) {
double reach = co.getReachability(it);
final int y = scaleToPixel(reach);
try {
int col = colors.getColorForDBID(it);
for (int y2 = height - 1; y2 >= y; y2--) {
img.setRGB(x, y2, col);
}
} catch (ArrayIndexOutOfBoundsException e) {
LOG.error("Plotting out of range: " + x + "," + y + " >= " + width + "x" + height);
}
x++;
}
plot = img;
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDIter in project elki by elki-project.
the class ODIN method run.
/**
* Run the ODIN algorithm
*
* Tutorial note: the <em>signature</em> of this method depends on the types
* that we requested in the {@link #getInputTypeRestriction} method. Here we
* requested a single relation of type {@code O} , the data type of our
* distance function.
*
* @param database Database to run on.
* @param relation Relation to process.
* @return ODIN outlier result.
*/
public OutlierResult run(Database database, Relation<O> relation) {
// Get the query functions:
DistanceQuery<O> dq = database.getDistanceQuery(relation, getDistanceFunction());
KNNQuery<O> knnq = database.getKNNQuery(dq, k);
// Get the objects to process, and a data storage for counting and output:
DBIDs ids = relation.getDBIDs();
WritableDoubleDataStore scores = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_DB, 0.);
// Process all objects
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
// Find the nearest neighbors (using an index, if available!)
KNNList neighbors = knnq.getKNNForDBID(iter, k);
// For each neighbor, except ourselves, increase the in-degree:
for (DBIDIter nei = neighbors.iter(); nei.valid(); nei.advance()) {
if (DBIDUtil.equal(iter, nei)) {
continue;
}
scores.put(nei, scores.doubleValue(nei) + 1);
}
}
// Compute maximum
double min = Double.POSITIVE_INFINITY, max = 0.0;
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
min = Math.min(min, scores.doubleValue(iter));
max = Math.max(max, scores.doubleValue(iter));
}
// Wrap the result and add metadata.
// By actually specifying theoretical min, max and baseline, we get a better
// visualization (try it out - or see the screenshots in the tutorial)!
OutlierScoreMeta meta = new InvertedOutlierScoreMeta(min, max, 0., ids.size() - 1, k);
DoubleRelation rel = new MaterializedDoubleRelation("ODIN In-Degree", "odin", scores, ids);
return new OutlierResult(meta, rel);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDIter in project elki by elki-project.
the class CenterOfMassMetaClustering method run.
/**
* This run method will do the wrapping.
*
* Its called from {@link AbstractAlgorithm#run(Database)} and performs the
* call to the algorithms particular run method as well as the storing and
* comparison of the resulting Clusterings.
*
* @param database Database
* @param relation Data relation of uncertain objects
* @return Clustering result
*/
public C run(Database database, Relation<? extends UncertainObject> relation) {
final int dim = RelationUtil.dimensionality(relation);
DBIDs ids = relation.getDBIDs();
// Build a relation storing the center of mass:
WritableDataStore<DoubleVector> store1 = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, DoubleVector.class);
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
store1.put(iter, relation.get(iter).getCenterOfMass());
}
return runClusteringAlgorithm(database.getHierarchy(), relation, ids, store1, dim, "Uncertain Model: Center of Mass");
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDIter in project elki by elki-project.
the class ScatterData method initializeData.
public void initializeData(GL2 gl) {
length = ids.size();
dim = 0;
vecOffset = -1;
classOffset = -1;
// Scan relations for dimensionalities:
int[] dims = new int[relations.size()];
LinearScale[][] scales = new LinearScale[relations.size()][];
ArrayList<Relation<? extends NumberVector>> vrels = new ArrayList<>(relations.size());
for (int r = 0; r < relations.size(); r++) {
Relation<?> rel = relations.get(r);
final SimpleTypeInformation<?> type = rel.getDataTypeInformation();
if (type instanceof VectorFieldTypeInformation) {
@SuppressWarnings("unchecked") final Relation<? extends NumberVector> vrel = (Relation<? extends NumberVector>) rel;
final int d = ((VectorFieldTypeInformation<?>) type).getDimensionality();
dims[r] = d;
LinearScale[] rscales = new LinearScale[d];
double[][] minmax = RelationUtil.computeMinMax(vrel);
for (int i = 0; i < d; i++) {
rscales[i] = new LinearScale(minmax[0][i], minmax[1][i]);
}
scales[r] = rscales;
vrels.add(vrel);
if (vecOffset < 0) {
vecOffset = dim;
}
dim += d;
} else {
// FIXME: handle other relation types!
dims[r] = 0;
vrels.add(null);
}
}
if (classOffset < 0) {
++dim;
}
LOG.warning("Dimensionalities: " + FormatUtil.format(dims));
// Initialize vertex buffer handles:
assert (vbos[0] == -1);
gl.glGenBuffers(1, vbos, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbos[0]);
gl.glBufferData(GL.GL_ARRAY_BUFFER, //
length * dim * SIZE_FLOAT + // safety padding
3 * SIZE_FLOAT, null, GL2.GL_STATIC_DRAW);
ByteBuffer vbytebuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_WRITE_ONLY);
FloatBuffer vertices = vbytebuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
Random rnd = new Random();
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
for (int r = 0; r < dims.length; r++) {
if (dims[r] <= 0) {
continue;
}
final Relation<? extends NumberVector> vrel = vrels.get(r);
LinearScale[] rscales = scales[r];
if (vrel != null) {
NumberVector vec = vrel.get(iter);
for (int d = 0; d < dims[r]; d++) {
// vertices.put( rnd.nextFloat());
vertices.put((float) rscales[d].getScaled(vec.doubleValue(d)) * 2.f - 1.f);
}
}
}
if (classOffset < 0) {
vertices.put(rnd.nextInt(30));
}
}
stride = dim * SIZE_FLOAT;
if (classOffset < 0) {
classOffset = (dim - 1) * SIZE_FLOAT;
}
if (vertices.position() != length * dim) {
LOG.warning("Size mismatch: " + vertices.position() + " expected: " + length * dim, new Throwable());
}
vertices.flip();
gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
LOG.warning("Size: " + length + " dim: " + dim + " " + vecOffset + " " + classOffset);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBIDIter in project elki by elki-project.
the class FarthestPointsInitialMeans method chooseInitialMedoids.
@Override
public DBIDs chooseInitialMedoids(int k, DBIDs ids, DistanceQuery<? super O> distQ) {
@SuppressWarnings("unchecked") final Relation<O> relation = (Relation<O>) distQ.getRelation();
WritableDoubleDataStore store = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP, Double.POSITIVE_INFINITY);
ArrayModifiableDBIDs means = DBIDUtil.newArray(k);
DBIDRef first = DBIDUtil.randomSample(ids, rnd);
DBIDVar prevmean = DBIDUtil.newVar(first);
means.add(first);
DBIDVar best = DBIDUtil.newVar(first);
for (int i = (dropfirst ? 0 : 1); i < k; i++) {
// Find farthest object:
double maxdist = Double.NEGATIVE_INFINITY;
for (DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
final double prev = store.doubleValue(it);
if (prev != prev) {
// NaN: already chosen!
continue;
}
double val = Math.min(prev, distQ.distance(prevmean, it));
// Don't store distance to first mean, when it will be dropped below.
if (i > 0) {
store.putDouble(it, val);
}
if (val > maxdist) {
maxdist = val;
best.set(it);
}
}
// Add new mean:
if (i == 0) {
// Remove temporary first element.
means.clear();
}
// So it won't be chosen twice.
store.putDouble(best, Double.NaN);
prevmean.set(best);
means.add(best);
}
return means;
}
Aggregations