Search in sources :

Example 1 with UpdatableDatabase

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

the class MaterializedKNNAndRKNNPreprocessorTest method testPreprocessor.

@Test
public void testPreprocessor() {
    UpdatableDatabase db;
    // get database
    try (InputStream is = AbstractSimpleAlgorithmTest.open(dataset)) {
        ListParameterization params = new ListParameterization();
        // Setup parser and data loading
        NumberVectorLabelParser<DoubleVector> parser = new NumberVectorLabelParser<>(DoubleVector.FACTORY);
        InputStreamDatabaseConnection dbc = new InputStreamDatabaseConnection(is, new ArrayList<>(), parser);
        // We want to allow the use of indexes via "params"
        params.addParameter(AbstractDatabase.Parameterizer.DATABASE_CONNECTION_ID, dbc);
        db = ClassGenericsUtil.parameterizeOrAbort(HashmapDatabase.class, params);
        db.initialize();
    } catch (IOException e) {
        fail("Test data " + dataset + " not found.");
        return;
    }
    Relation<DoubleVector> rep = db.getRelation(TypeUtil.DOUBLE_VECTOR_FIELD);
    DistanceQuery<DoubleVector> distanceQuery = db.getDistanceQuery(rep, EuclideanDistanceFunction.STATIC);
    // verify data set size.
    assertEquals("Data set size doesn't match parameters.", shoulds, rep.size());
    // get linear queries
    LinearScanDistanceKNNQuery<DoubleVector> lin_knn_query = new LinearScanDistanceKNNQuery<>(distanceQuery);
    LinearScanRKNNQuery<DoubleVector> lin_rknn_query = new LinearScanRKNNQuery<>(distanceQuery, lin_knn_query, k);
    // get preprocessed queries
    ListParameterization config = new ListParameterization();
    config.addParameter(MaterializeKNNPreprocessor.Factory.DISTANCE_FUNCTION_ID, distanceQuery.getDistanceFunction());
    config.addParameter(MaterializeKNNPreprocessor.Factory.K_ID, k);
    MaterializeKNNAndRKNNPreprocessor<DoubleVector> preproc = new MaterializeKNNAndRKNNPreprocessor<>(rep, distanceQuery.getDistanceFunction(), k);
    KNNQuery<DoubleVector> preproc_knn_query = preproc.getKNNQuery(distanceQuery, k);
    RKNNQuery<DoubleVector> preproc_rknn_query = preproc.getRKNNQuery(distanceQuery);
    // add as index
    db.getHierarchy().add(rep, preproc);
    assertFalse("Preprocessor knn query class incorrect.", preproc_knn_query instanceof LinearScanDistanceKNNQuery);
    assertFalse("Preprocessor rknn query class incorrect.", preproc_rknn_query instanceof LinearScanDistanceKNNQuery);
    // test queries
    testKNNQueries(rep, lin_knn_query, preproc_knn_query, k);
    testRKNNQueries(rep, lin_rknn_query, preproc_rknn_query, k);
    // also test partial queries, forward only
    testKNNQueries(rep, lin_knn_query, preproc_knn_query, k / 2);
    // insert new objects
    List<DoubleVector> insertions = new ArrayList<>();
    NumberVector.Factory<DoubleVector> o = RelationUtil.getNumberVectorFactory(rep);
    int dim = RelationUtil.dimensionality(rep);
    Random random = new Random(seed);
    for (int i = 0; i < updatesize; i++) {
        DoubleVector obj = VectorUtil.randomVector(o, dim, random);
        insertions.add(obj);
    }
    // System.out.println("Insert " + insertions);
    DBIDs deletions = db.insert(MultipleObjectsBundle.makeSimple(rep.getDataTypeInformation(), insertions));
    // test queries
    testKNNQueries(rep, lin_knn_query, preproc_knn_query, k);
    testRKNNQueries(rep, lin_rknn_query, preproc_rknn_query, k);
    // delete objects
    // System.out.println("Delete " + deletions);
    db.delete(deletions);
    // test queries
    testKNNQueries(rep, lin_knn_query, preproc_knn_query, k);
    testRKNNQueries(rep, lin_rknn_query, preproc_rknn_query, k);
}
Also used : UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) ArrayList(java.util.ArrayList) NumberVectorLabelParser(de.lmu.ifi.dbs.elki.datasource.parser.NumberVectorLabelParser) Random(java.util.Random) ListParameterization(de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization) LinearScanDistanceKNNQuery(de.lmu.ifi.dbs.elki.database.query.knn.LinearScanDistanceKNNQuery) InputStream(java.io.InputStream) HashmapDatabase(de.lmu.ifi.dbs.elki.database.HashmapDatabase) ArrayDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) InputStreamDatabaseConnection(de.lmu.ifi.dbs.elki.datasource.InputStreamDatabaseConnection) IOException(java.io.IOException) MaterializeKNNAndRKNNPreprocessor(de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNAndRKNNPreprocessor) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) LinearScanRKNNQuery(de.lmu.ifi.dbs.elki.database.query.rknn.LinearScanRKNNQuery) AbstractSimpleAlgorithmTest(de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest) Test(org.junit.Test)

Example 2 with UpdatableDatabase

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

the class OnlineLOFTest method testOnlineLOF.

/**
 * First, run the {@link LOF} algorithm on the database. Second, run the
 * {@link OnlineLOF} algorithm on the database, insert new objects and
 * afterwards delete them. Then, compare the two results for equality.
 */
@SuppressWarnings("unchecked")
@Test
public void testOnlineLOF() {
    UpdatableDatabase db = getDatabase();
    // 1. Run LOF
    FlexibleLOF<DoubleVector> lof = new FlexibleLOF<>(k, k, neighborhoodDistanceFunction, reachabilityDistanceFunction);
    DoubleRelation scores1 = lof.run(db).getScores();
    // 2. Run OnlineLOF (with insertions and removals) on database
    DoubleRelation scores2 = runOnlineLOF(db).getScores();
    // 3. Compare results
    for (DBIDIter id = scores1.getDBIDs().iter(); id.valid(); id.advance()) {
        double lof1 = scores1.doubleValue(id);
        double lof2 = scores2.doubleValue(id);
        assertEquals("lof(" + DBIDUtil.toString(id) + ") != lof(" + DBIDUtil.toString(id) + "): " + lof1 + " != " + lof2, lof1, lof2, 1e-10);
    }
}
Also used : UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) Test(org.junit.Test)

Example 3 with UpdatableDatabase

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

the class DiSHPreferenceVectorIndex method determinePreferenceVectorByApriori.

/**
 * Determines the preference vector with the apriori strategy.
 *
 * @param relation the database storing the objects
 * @param neighborIDs the list of ids of the neighbors in each dimension
 * @param msg a string buffer for debug messages
 * @return the preference vector
 */
private long[] determinePreferenceVectorByApriori(Relation<V> relation, ModifiableDBIDs[] neighborIDs, StringBuilder msg) {
    int dimensionality = neighborIDs.length;
    // database for apriori
    UpdatableDatabase apriori_db = new HashmapDatabase();
    SimpleTypeInformation<?> bitmeta = VectorFieldTypeInformation.typeRequest(BitVector.class, dimensionality, dimensionality);
    for (DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
        long[] bits = BitsUtil.zero(dimensionality);
        boolean allFalse = true;
        for (int d = 0; d < dimensionality; d++) {
            if (neighborIDs[d].contains(it)) {
                BitsUtil.setI(bits, d);
                allFalse = false;
            }
        }
        if (!allFalse) {
            SingleObjectBundle oaa = new SingleObjectBundle();
            oaa.append(bitmeta, new BitVector(bits, dimensionality));
            apriori_db.insert(oaa);
        }
    }
    APRIORI apriori = new APRIORI(minpts);
    FrequentItemsetsResult aprioriResult = apriori.run(apriori_db);
    // result of apriori
    List<Itemset> frequentItemsets = aprioriResult.getItemsets();
    if (msg != null) {
        msg.append("\n Frequent itemsets: ").append(frequentItemsets);
    }
    int maxSupport = 0;
    int maxCardinality = 0;
    long[] preferenceVector = BitsUtil.zero(dimensionality);
    for (Itemset itemset : frequentItemsets) {
        if ((maxCardinality < itemset.length()) || (maxCardinality == itemset.length() && maxSupport == itemset.getSupport())) {
            preferenceVector = Itemset.toBitset(itemset, BitsUtil.zero(dimensionality));
            maxCardinality = itemset.length();
            maxSupport = itemset.getSupport();
        }
    }
    if (msg != null) {
        // 
        msg.append("\n preference ").append(// 
        BitsUtil.toStringLow(preferenceVector, dimensionality)).append('\n');
        LOG.debugFine(msg.toString());
    }
    return preferenceVector;
}
Also used : UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) BitVector(de.lmu.ifi.dbs.elki.data.BitVector) HashmapDatabase(de.lmu.ifi.dbs.elki.database.HashmapDatabase) SingleObjectBundle(de.lmu.ifi.dbs.elki.datasource.bundle.SingleObjectBundle) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) FrequentItemsetsResult(de.lmu.ifi.dbs.elki.result.FrequentItemsetsResult) Itemset(de.lmu.ifi.dbs.elki.algorithm.itemsetmining.Itemset) APRIORI(de.lmu.ifi.dbs.elki.algorithm.itemsetmining.APRIORI)

Example 4 with UpdatableDatabase

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

the class MoveObjectsToolVisualization method processNewResult.

@Override
public void processNewResult(VisualizerContext context, Object start) {
    Database db = ResultUtil.findDatabase(context.getHierarchy());
    if (!(db instanceof UpdatableDatabase)) {
        return;
    }
    VisualizationTree.findVis(context, start).filter(ScatterPlotProjector.class).forEach(p -> {
        final Relation<?> rel = p.getRelation();
        if (!TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(rel.getDataTypeInformation())) {
            return;
        }
        context.addVis(p, // 
        new VisualizationTask(this, NAME, p.getRelation(), rel).level(VisualizationTask.LEVEL_INTERACTIVE).tool(true).visibility(// 
        false).with(RenderFlag.NO_THUMBNAIL).with(// 
        RenderFlag.NO_EXPORT).with(UpdateFlag.ON_DATA).with(UpdateFlag.ON_SAMPLE).with(UpdateFlag.ON_SELECTION));
    });
}
Also used : UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) VisualizationTask(de.lmu.ifi.dbs.elki.visualization.VisualizationTask) ScatterPlotProjector(de.lmu.ifi.dbs.elki.visualization.projector.ScatterPlotProjector) UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) Database(de.lmu.ifi.dbs.elki.database.Database)

Example 5 with UpdatableDatabase

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

the class OnlineLOFTest method getDatabase.

/**
 * Returns the database.
 */
private static UpdatableDatabase getDatabase() {
    UpdatableDatabase db = // 
    new ELKIBuilder<>(HashmapDatabase.class).with(FileBasedDatabaseConnection.Parameterizer.INPUT_ID, // 
    dataset).build();
    db.initialize();
    return db;
}
Also used : UpdatableDatabase(de.lmu.ifi.dbs.elki.database.UpdatableDatabase) ELKIBuilder(de.lmu.ifi.dbs.elki.utilities.ELKIBuilder)

Aggregations

UpdatableDatabase (de.lmu.ifi.dbs.elki.database.UpdatableDatabase)5 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)2 HashmapDatabase (de.lmu.ifi.dbs.elki.database.HashmapDatabase)2 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)2 Test (org.junit.Test)2 AbstractSimpleAlgorithmTest (de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest)1 APRIORI (de.lmu.ifi.dbs.elki.algorithm.itemsetmining.APRIORI)1 Itemset (de.lmu.ifi.dbs.elki.algorithm.itemsetmining.Itemset)1 BitVector (de.lmu.ifi.dbs.elki.data.BitVector)1 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)1 Database (de.lmu.ifi.dbs.elki.database.Database)1 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 LinearScanDistanceKNNQuery (de.lmu.ifi.dbs.elki.database.query.knn.LinearScanDistanceKNNQuery)1 LinearScanRKNNQuery (de.lmu.ifi.dbs.elki.database.query.rknn.LinearScanRKNNQuery)1 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)1 InputStreamDatabaseConnection (de.lmu.ifi.dbs.elki.datasource.InputStreamDatabaseConnection)1 SingleObjectBundle (de.lmu.ifi.dbs.elki.datasource.bundle.SingleObjectBundle)1 NumberVectorLabelParser (de.lmu.ifi.dbs.elki.datasource.parser.NumberVectorLabelParser)1 MaterializeKNNAndRKNNPreprocessor (de.lmu.ifi.dbs.elki.index.preprocessed.knn.MaterializeKNNAndRKNNPreprocessor)1