use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class VisualizerParameterizer method newContext.
/**
* Make a new visualization context
*
* @param hier Result hierarchy
* @param start Starting result
* @return New context
*/
public VisualizerContext newContext(ResultHierarchy hier, Result start) {
Collection<Relation<?>> rels = ResultUtil.filterResults(hier, Relation.class);
for (Relation<?> rel : rels) {
if (samplesize == 0) {
continue;
}
if (!ResultUtil.filterResults(hier, rel, SamplingResult.class).isEmpty()) {
continue;
}
if (rel.size() > samplesize) {
SamplingResult sample = new SamplingResult(rel);
sample.setSample(DBIDUtil.randomSample(sample.getSample(), samplesize, rnd));
ResultUtil.addChildResult(rel, sample);
}
}
return new VisualizerContext(hier, start, stylelib, factories);
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class PolygonVisualization method processNewResult.
@SuppressWarnings("unchecked")
@Override
public void processNewResult(VisualizerContext context, Object result) {
VisualizationTree.findNewResultVis(context, result, Relation.class, ScatterPlotProjector.class, (rel, p) -> {
if (!TypeUtil.POLYGON_TYPE.isAssignableFromType(rel.getDataTypeInformation())) {
return;
}
if (RelationUtil.dimensionality((Relation<? extends PolygonsObject>) rel) != 2) {
return;
}
// Assume that a 2d projector is using the same coordinates as the
// polygons.
final VisualizationTask task = //
new VisualizationTask(this, NAME, rel, rel).level(//
VisualizationTask.LEVEL_DATA - 10).with(UpdateFlag.ON_DATA);
context.addVis(rel, task);
context.addVis(p, task);
});
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class SampleKMeansInitialization method chooseInitialMeans.
@Override
public <T extends V> double[][] chooseInitialMeans(Database database, Relation<T> relation, int k, NumberVectorDistanceFunction<? super T> distanceFunction) {
final DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), rate, rnd);
// Ugly cast, sorry
@SuppressWarnings("unchecked") Relation<V> rel = (Relation<V>) relation;
// FIXME: This does not necessarily hold. Check and fail!
if (!distanceFunction.getInputTypeRestriction().isAssignableFromType(TypeUtil.NUMBER_VECTOR_FIELD)) {
LoggingUtil.warning("Initializing k-means with k-means using specialized distance functions MAY fail, if the initialization method does require a distance defined on arbitrary number vectors.");
}
@SuppressWarnings("unchecked") NumberVectorDistanceFunction<? super V> pdf = (NumberVectorDistanceFunction<? super V>) distanceFunction;
ProxyView<V> proxyv = new ProxyView<>(sample, rel);
ProxyDatabase proxydb = new ProxyDatabase(sample, proxyv);
innerkMeans.setK(k);
innerkMeans.setDistanceFunction(pdf);
Clustering<?> clusters = innerkMeans.run(proxydb, proxyv);
double[][] means = new double[clusters.getAllClusters().size()][];
int i = 0;
for (Cluster<?> cluster : clusters.getAllClusters()) {
means[i++] = ModelUtil.getPrototype(cluster.getModel(), relation).toArray();
}
return means;
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class FarthestSumPointsInitialMeans 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, 0.);
ArrayModifiableDBIDs means = DBIDUtil.newArray(k);
DBIDRef first = DBIDUtil.randomSample(ids, rnd);
means.add(first);
DBIDVar prevmean = DBIDUtil.newVar(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 dsum = prev + distQ.distance(prevmean, it);
// Don't store distance to first mean, when it will be dropped below.
if (i > 0) {
store.putDouble(it, dsum);
}
if (dsum > maxdist) {
maxdist = dsum;
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);
}
store.destroy();
return means;
}
use of de.lmu.ifi.dbs.elki.database.relation.Relation in project elki by elki-project.
the class ExternalClustering method run.
/**
* Run the algorithm.
*
* @param database Database to use
* @return Result
*/
@Override
public Clustering<? extends Model> run(Database database) {
Clustering<? extends Model> m = null;
try (//
InputStream in = FileUtil.tryGzipInput(new FileInputStream(file));
TokenizedReader reader = CSVReaderFormat.DEFAULT_FORMAT.makeReader()) {
Tokenizer tokenizer = reader.getTokenizer();
reader.reset(in);
IntArrayList assignment = new IntArrayList(database.getRelation(TypeUtil.DBID).size());
ArrayList<String> name = new ArrayList<>();
line: while (reader.nextLineExceptComments()) {
for (; /* initialized by nextLineExceptComments */
tokenizer.valid(); tokenizer.advance()) {
try {
assignment.add(tokenizer.getIntBase10());
} catch (NumberFormatException e) {
name.add(tokenizer.getSubstring());
}
}
if (LOG.isDebuggingFinest()) {
LOG.debugFinest("Read " + assignment.size() + " assignments and " + name.size() + " labels.");
}
for (Relation<?> r : database.getRelations()) {
if (r.size() == assignment.size()) {
attachToRelation(database, r, assignment, name);
assignment.clear();
name.clear();
continue line;
}
}
throw new AbortException("No relation found to match with clustering of size " + assignment.size());
}
} catch (IOException e) {
throw new AbortException("Could not load outlier scores: " + e.getMessage() + " when loading " + file, e);
}
return m;
}
Aggregations