Search in sources :

Example 6 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class RepresentativeUncertainClustering 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 Clustering<?> run(Database database, Relation<? extends UncertainObject> relation) {
    ResultHierarchy hierarchy = database.getHierarchy();
    ArrayList<Clustering<?>> clusterings = new ArrayList<>();
    final int dim = RelationUtil.dimensionality(relation);
    DBIDs ids = relation.getDBIDs();
    // To collect samples
    Result samples = new BasicResult("Samples", "samples");
    // Step 1: Cluster sampled possible worlds:
    Random rand = random.getSingleThreadedRandom();
    FiniteProgress sampleP = LOG.isVerbose() ? new FiniteProgress("Clustering samples", numsamples, LOG) : null;
    for (int i = 0; i < numsamples; i++) {
        WritableDataStore<DoubleVector> store = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, DoubleVector.class);
        for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
            store.put(iter, relation.get(iter).drawSample(rand));
        }
        clusterings.add(runClusteringAlgorithm(hierarchy, samples, ids, store, dim, "Sample " + i));
        LOG.incrementProcessed(sampleP);
    }
    LOG.ensureCompleted(sampleP);
    // Step 2: perform the meta clustering (on samples only).
    DBIDRange rids = DBIDFactory.FACTORY.generateStaticDBIDRange(clusterings.size());
    WritableDataStore<Clustering<?>> datastore = DataStoreUtil.makeStorage(rids, DataStoreFactory.HINT_DB, Clustering.class);
    {
        Iterator<Clustering<?>> it2 = clusterings.iterator();
        for (DBIDIter iter = rids.iter(); iter.valid(); iter.advance()) {
            datastore.put(iter, it2.next());
        }
    }
    assert (rids.size() == clusterings.size());
    // Build a relation, and a distance matrix.
    Relation<Clustering<?>> crel = new MaterializedRelation<Clustering<?>>(Clustering.TYPE, rids, "Clusterings", datastore);
    PrecomputedDistanceMatrix<Clustering<?>> mat = new PrecomputedDistanceMatrix<>(crel, rids, distance);
    mat.initialize();
    ProxyDatabase d = new ProxyDatabase(rids, crel);
    d.getHierarchy().add(crel, mat);
    Clustering<?> c = metaAlgorithm.run(d);
    // Detach from database
    d.getHierarchy().remove(d, c);
    // Evaluation
    Result reps = new BasicResult("Representants", "representative");
    hierarchy.add(relation, reps);
    DistanceQuery<Clustering<?>> dq = mat.getDistanceQuery(distance);
    List<? extends Cluster<?>> cl = c.getAllClusters();
    List<DoubleObjPair<Clustering<?>>> evaluated = new ArrayList<>(cl.size());
    for (Cluster<?> clus : cl) {
        double besttau = Double.POSITIVE_INFINITY;
        Clustering<?> bestc = null;
        for (DBIDIter it1 = clus.getIDs().iter(); it1.valid(); it1.advance()) {
            double tau = 0.;
            Clustering<?> curc = crel.get(it1);
            for (DBIDIter it2 = clus.getIDs().iter(); it2.valid(); it2.advance()) {
                if (DBIDUtil.equal(it1, it2)) {
                    continue;
                }
                double di = dq.distance(curc, it2);
                tau = di > tau ? di : tau;
            }
            // Cluster member with the least maximum distance.
            if (tau < besttau) {
                besttau = tau;
                bestc = curc;
            }
        }
        if (bestc == null) {
            // E.g. degenerate empty clusters
            continue;
        }
        // Global tau:
        double gtau = 0.;
        for (DBIDIter it2 = crel.iterDBIDs(); it2.valid(); it2.advance()) {
            double di = dq.distance(bestc, it2);
            gtau = di > gtau ? di : gtau;
        }
        final double cprob = computeConfidence(clus.size(), crel.size());
        // Build an evaluation result
        hierarchy.add(bestc, new RepresentativenessEvaluation(gtau, besttau, cprob));
        evaluated.add(new DoubleObjPair<Clustering<?>>(cprob, bestc));
    }
    // Sort evaluated results by confidence:
    Collections.sort(evaluated, Collections.reverseOrder());
    for (DoubleObjPair<Clustering<?>> pair : evaluated) {
        // Attach parent relation (= sample) to the representative samples.
        for (It<Relation<?>> it = hierarchy.iterParents(pair.second).filter(Relation.class); it.valid(); it.advance()) {
            hierarchy.add(reps, it.get());
        }
    }
    // Add the random samples below the representative results only:
    if (keep) {
        hierarchy.add(relation, samples);
    } else {
        hierarchy.removeSubtree(samples);
    }
    return c;
}
Also used : ArrayList(java.util.ArrayList) Result(de.lmu.ifi.dbs.elki.result.Result) EvaluationResult(de.lmu.ifi.dbs.elki.result.EvaluationResult) BasicResult(de.lmu.ifi.dbs.elki.result.BasicResult) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) Random(java.util.Random) BasicResult(de.lmu.ifi.dbs.elki.result.BasicResult) Iterator(java.util.Iterator) ResultHierarchy(de.lmu.ifi.dbs.elki.result.ResultHierarchy) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) ProxyDatabase(de.lmu.ifi.dbs.elki.database.ProxyDatabase) PrecomputedDistanceMatrix(de.lmu.ifi.dbs.elki.index.distancematrix.PrecomputedDistanceMatrix) Clustering(de.lmu.ifi.dbs.elki.data.Clustering) DoubleObjPair(de.lmu.ifi.dbs.elki.utilities.pairs.DoubleObjPair) DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector)

Example 7 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class MemoryDataStoreFactory method makeStorage.

@SuppressWarnings("unchecked")
@Override
public <T> WritableDataStore<T> makeStorage(DBIDs ids, int hints, Class<? super T> dataclass) {
    if (Double.class.equals(dataclass)) {
        return (WritableDataStore<T>) makeDoubleStorage(ids, hints);
    }
    if (Integer.class.equals(dataclass)) {
        return (WritableDataStore<T>) makeIntegerStorage(ids, hints);
    }
    if (ids instanceof DBIDRange) {
        DBIDRange range = (DBIDRange) ids;
        Object[] data = new Object[range.size()];
        return new ArrayStore<>(data, range);
    } else {
        return new MapIntegerDBIDStore<>(ids.size());
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) WritableDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDataStore)

Example 8 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class FixedDBIDsFilter method filter.

@Override
public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
    DBIDRange ids = DBIDFactory.FACTORY.generateStaticDBIDRange(curid, objects.dataLength());
    objects.setDBIDs(ids);
    curid += objects.dataLength();
    return objects;
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange)

Example 9 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class WeightedQuickUnionRangeDBIDsTest method testWorstCase.

/**
 * Worst-case with 10 nodes, from Sedgewick.
 *
 * We don't test runtime, but this is an interesting case nevertheless.
 */
@Test
public void testWorstCase() {
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(10);
    UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(1)));
    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(5), i2.seek(4));
    uf.union(i1.seek(7), i2.seek(6));
    uf.union(i1.seek(8), i2.seek(9));
    uf.union(i1.seek(1), i2.seek(3));
    assertTrue(uf.isConnected(i1.seek(0), i2.seek(2)));
    uf.union(i1.seek(4), i2.seek(6));
    assertTrue(uf.isConnected(i1.seek(5), i2.seek(7)));
    uf.union(i1.seek(3), i2.seek(7));
    assertTrue(uf.isConnected(i1.seek(0), i2.seek(4)));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(9)));
    uf.union(i1.seek(0), i2.seek(9));
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            assertTrue(uf.isConnected(i1.seek(i), i2.seek(j)));
        }
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Example 10 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class WeightedQuickUnionRangeDBIDsTest method testRoots.

@Test
public void testRoots() {
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(8);
    UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    uf.union(i1.seek(0), i2.seek(1));
    uf.union(i1.seek(2), i2.seek(3));
    assertEquals(6, uf.getRoots().size());
    uf.union(i1.seek(0), i2.seek(2));
    assertEquals(5, uf.getRoots().size());
    uf.union(i1.seek(4), i2.seek(5));
    uf.union(i1.seek(6), i2.seek(7));
    uf.union(i1.seek(4), i2.seek(6));
    assertEquals(2, uf.getRoots().size());
    uf.union(i1.seek(0), i2.seek(4));
    assertEquals(1, uf.getRoots().size());
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Aggregations

DBIDRange (de.lmu.ifi.dbs.elki.database.ids.DBIDRange)24 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)13 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)8 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)8 Test (org.junit.Test)8 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)6 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)6 Random (java.util.Random)4 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)3 TypeInformation (de.lmu.ifi.dbs.elki.data.type.TypeInformation)3 Database (de.lmu.ifi.dbs.elki.database.Database)3 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)3 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)3 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)3 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)2 StaticArrayDatabase (de.lmu.ifi.dbs.elki.database.StaticArrayDatabase)2 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)2 ArrayAdapterDatabaseConnection (de.lmu.ifi.dbs.elki.datasource.ArrayAdapterDatabaseConnection)2 DatabaseConnection (de.lmu.ifi.dbs.elki.datasource.DatabaseConnection)2 OnDiskUpperTriangleMatrix (de.lmu.ifi.dbs.elki.persistent.OnDiskUpperTriangleMatrix)2