Search in sources :

Example 16 with Relation

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);
}
Also used : Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) SamplingResult(de.lmu.ifi.dbs.elki.result.SamplingResult)

Example 17 with Relation

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);
    });
}
Also used : Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) VisualizationTask(de.lmu.ifi.dbs.elki.visualization.VisualizationTask) PolygonsObject(de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject)

Example 18 with Relation

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;
}
Also used : ProxyView(de.lmu.ifi.dbs.elki.database.relation.ProxyView) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) ProxyDatabase(de.lmu.ifi.dbs.elki.database.ProxyDatabase) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) NumberVectorDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction)

Example 19 with Relation

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;
}
Also used : Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) DBIDVar(de.lmu.ifi.dbs.elki.database.ids.DBIDVar) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) DBIDRef(de.lmu.ifi.dbs.elki.database.ids.DBIDRef) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 20 with Relation

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;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) TokenizedReader(de.lmu.ifi.dbs.elki.utilities.io.TokenizedReader) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) Tokenizer(de.lmu.ifi.dbs.elki.utilities.io.Tokenizer) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Aggregations

Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)24 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)11 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)6 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)6 Database (de.lmu.ifi.dbs.elki.database.Database)5 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)4 MaterializedRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation)4 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)4 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)3 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)3 DBIDRef (de.lmu.ifi.dbs.elki.database.ids.DBIDRef)3 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)3 Random (java.util.Random)3 PolygonsObject (de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject)2 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)2 DBIDVar (de.lmu.ifi.dbs.elki.database.ids.DBIDVar)2 ModifiableRelation (de.lmu.ifi.dbs.elki.database.relation.ModifiableRelation)2 Duration (de.lmu.ifi.dbs.elki.logging.statistics.Duration)2 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)2 ArrayList (java.util.ArrayList)2