use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class COPVectorVisualization method processNewResult.
@Override
public void processNewResult(VisualizerContext context, Object start) {
VisualizationTree.findNewSiblings(context, start, OutlierResult.class, ScatterPlotProjector.class, (o, p) -> {
final Relation<?> rel2 = p.getRelation();
if (!TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(rel2.getDataTypeInformation())) {
return;
}
VisualizationTree.findNewResults(context, o).filter(Relation.class).forEach(rel -> {
if (!rel.getShortName().equals(COP.COP_ERRORVEC)) {
return;
}
final VisualizationTask task = //
new VisualizationTask(this, NAME, rel, rel2).level(//
VisualizationTask.LEVEL_DATA).with(UpdateFlag.ON_DATA).with(UpdateFlag.ON_SAMPLE);
context.addVis(o, task);
context.addVis(p, task);
});
});
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation 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.relation.Relation 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;
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class KMeansPlusPlusInitialMeans method chooseInitialMedoids.
@Override
public DBIDs chooseInitialMedoids(int k, DBIDs ids, DistanceQuery<? super O> distQ) {
@SuppressWarnings("unchecked") final Relation<O> rel = (Relation<O>) distQ.getRelation();
ArrayModifiableDBIDs means = DBIDUtil.newArray(k);
WritableDoubleDataStore weights = DataStoreUtil.makeDoubleStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP, 0.);
Random random = rnd.getSingleThreadedRandom();
DBIDRef first = DBIDUtil.randomSample(ids, random);
means.add(first);
// Initialize weights
double weightsum = initialWeights(weights, ids, rel.get(first), distQ);
while (true) {
if (weightsum > Double.MAX_VALUE) {
LoggingUtil.warning("Could not choose a reasonable mean for k-means++ - too many data points, too large squared distances?");
}
if (weightsum < Double.MIN_NORMAL) {
LoggingUtil.warning("Could not choose a reasonable mean for k-means++ - to few unique data points?");
}
double r = random.nextDouble() * weightsum;
while (r <= 0 && weightsum > Double.MIN_NORMAL) {
// Try harder to not choose 0.
r = random.nextDouble() * weightsum;
}
DBIDIter it = ids.iter();
for (; r > 0. && it.valid(); it.advance()) {
r -= weights.doubleValue(it);
}
// Add new mean:
means.add(it);
if (means.size() >= k) {
break;
}
// Update weights:
weights.putDouble(it, 0.);
weightsum = updateWeights(weights, ids, rel.get(it), distQ);
}
return means;
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class PAMInitialMeans method chooseInitialMeans.
@Override
public <T extends NumberVector> double[][] chooseInitialMeans(Database database, Relation<T> relation, int k, NumberVectorDistanceFunction<? super T> distanceFunction) {
if (relation.size() < k) {
throw new AbortException("Database has less than k objects.");
}
// Ugly cast; but better than code duplication.
@SuppressWarnings("unchecked") Relation<O> rel = (Relation<O>) relation;
// Get a distance query
@SuppressWarnings("unchecked") final PrimitiveDistanceFunction<? super O> distF = (PrimitiveDistanceFunction<? super O>) distanceFunction;
final DistanceQuery<O> distQ = database.getDistanceQuery(rel, distF);
DBIDs medids = chooseInitialMedoids(k, rel.getDBIDs(), distQ);
double[][] medoids = new double[k][];
DBIDIter iter = medids.iter();
for (int i = 0; i < k; i++, iter.advance()) {
medoids[i] = relation.get(iter).toArray();
}
return medoids;
}
Aggregations