use of de.lmu.ifi.dbs.elki.datasource.filter.FixedDBIDsFilter in project elki by elki-project.
the class AbstractSimpleAlgorithmTest method makeSimpleDatabase.
/**
* Generate a simple DoubleVector database from a file.
*
* @param filename File to load
* @param expectedSize Expected size in records
* @return Database
*/
public static Database makeSimpleDatabase(String filename, int expectedSize) {
ListParameterization params = new ListParameterization();
// Use a fixed DBID - historically, we used 1 indexed - to reduce random
// variation in results due to different hash codes everywhere.
params.addParameter(AbstractDatabaseConnection.Parameterizer.FILTERS_ID, new FixedDBIDsFilter(1));
return makeSimpleDatabase(filename, expectedSize, params);
}
use of de.lmu.ifi.dbs.elki.datasource.filter.FixedDBIDsFilter in project elki by elki-project.
the class AbstractIndexStructureTest method testExactCosine.
/**
* Actual test routine, for cosine distance
*
* @param inputparams
*/
protected void testExactCosine(ListParameterization inputparams, Class<?> expectKNNQuery, Class<?> expectRangeQuery) {
// Use a fixed DBID - historically, we used 1 indexed - to reduce random
// variation in results due to different hash codes everywhere.
inputparams.addParameter(AbstractDatabaseConnection.Parameterizer.FILTERS_ID, new FixedDBIDsFilter(1));
Database db = AbstractSimpleAlgorithmTest.makeSimpleDatabase(dataset, shoulds, inputparams);
Relation<DoubleVector> rep = db.getRelation(TypeUtil.DOUBLE_VECTOR_FIELD);
DistanceQuery<DoubleVector> dist = db.getDistanceQuery(rep, CosineDistanceFunction.STATIC);
if (expectKNNQuery != null) {
// get the 10 next neighbors
DoubleVector dv = DoubleVector.wrap(querypoint);
KNNQuery<DoubleVector> knnq = db.getKNNQuery(dist, k);
assertTrue("Returned knn query is not of expected class: expected " + expectKNNQuery + " got " + knnq.getClass(), expectKNNQuery.isAssignableFrom(knnq.getClass()));
KNNList ids = knnq.getKNNForObject(dv, k);
assertEquals("Result size does not match expectation!", cosshouldd.length, ids.size());
// verify that the neighbors match.
int i = 0;
for (DoubleDBIDListIter res = ids.iter(); res.valid(); res.advance(), i++) {
// Verify distance
assertEquals("Expected distance doesn't match.", cosshouldd[i], res.doubleValue(), 1e-15);
// verify vector
DoubleVector c = rep.get(res);
DoubleVector c2 = DoubleVector.wrap(cosshouldc[i]);
assertEquals("Expected vector doesn't match: " + c.toString(), 0.0, dist.distance(c, c2), 1e-15);
}
}
if (expectRangeQuery != null) {
// Do a range query
DoubleVector dv = DoubleVector.wrap(querypoint);
RangeQuery<DoubleVector> rangeq = db.getRangeQuery(dist, coseps);
assertTrue("Returned range query is not of expected class: expected " + expectRangeQuery + " got " + rangeq.getClass(), expectRangeQuery.isAssignableFrom(rangeq.getClass()));
DoubleDBIDList ids = rangeq.getRangeForObject(dv, coseps);
assertEquals("Result size does not match expectation!", cosshouldd.length, ids.size());
// verify that the neighbors match.
int i = 0;
for (DoubleDBIDListIter res = ids.iter(); res.valid(); res.advance(), i++) {
// Verify distance
assertEquals("Expected distance doesn't match.", cosshouldd[i], res.doubleValue(), 1e-15);
// verify vector
DoubleVector c = rep.get(res);
DoubleVector c2 = DoubleVector.wrap(cosshouldc[i]);
assertEquals("Expected vector doesn't match: " + c.toString(), 0.0, dist.distance(c, c2), 1e-15);
}
}
}
use of de.lmu.ifi.dbs.elki.datasource.filter.FixedDBIDsFilter in project elki by elki-project.
the class AbstractIndexStructureTest method testExactEuclidean.
/**
* Actual test routine.
*
* @param inputparams
*/
protected void testExactEuclidean(ListParameterization inputparams, Class<?> expectKNNQuery, Class<?> expectRangeQuery) {
// Use a fixed DBID - historically, we used 1 indexed - to reduce random
// variation in results due to different hash codes everywhere.
inputparams.addParameter(AbstractDatabaseConnection.Parameterizer.FILTERS_ID, new FixedDBIDsFilter(1));
Database db = AbstractSimpleAlgorithmTest.makeSimpleDatabase(dataset, shoulds, inputparams);
Relation<DoubleVector> rep = db.getRelation(TypeUtil.DOUBLE_VECTOR_FIELD);
DistanceQuery<DoubleVector> dist = db.getDistanceQuery(rep, EuclideanDistanceFunction.STATIC);
if (expectKNNQuery != null) {
// get the 10 next neighbors
DoubleVector dv = DoubleVector.wrap(querypoint);
KNNQuery<DoubleVector> knnq = db.getKNNQuery(dist, k);
assertTrue("Returned knn query is not of expected class: expected " + expectKNNQuery + " got " + knnq.getClass(), expectKNNQuery.isAssignableFrom(knnq.getClass()));
KNNList ids = knnq.getKNNForObject(dv, k);
assertEquals("Result size does not match expectation!", shouldd.length, ids.size(), 1e-15);
// verify that the neighbors match.
int i = 0;
for (DoubleDBIDListIter res = ids.iter(); res.valid(); res.advance(), i++) {
// Verify distance
assertEquals("Expected distance doesn't match.", shouldd[i], res.doubleValue(), 1e-6);
// verify vector
DoubleVector c = rep.get(res);
DoubleVector c2 = DoubleVector.wrap(shouldc[i]);
assertEquals("Expected vector doesn't match: " + c.toString(), 0.0, dist.distance(c, c2), 1e-15);
}
}
if (expectRangeQuery != null) {
// Do a range query
DoubleVector dv = DoubleVector.wrap(querypoint);
RangeQuery<DoubleVector> rangeq = db.getRangeQuery(dist, eps);
assertTrue("Returned range query is not of expected class: expected " + expectRangeQuery + " got " + rangeq.getClass(), expectRangeQuery.isAssignableFrom(rangeq.getClass()));
DoubleDBIDList ids = rangeq.getRangeForObject(dv, eps);
assertEquals("Result size does not match expectation!", shouldd.length, ids.size(), 1e-15);
// verify that the neighbors match.
int i = 0;
for (DoubleDBIDListIter res = ids.iter(); res.valid(); res.advance(), i++) {
// Verify distance
assertEquals("Expected distance doesn't match.", shouldd[i], res.doubleValue(), 1e-6);
// verify vector
DoubleVector c = rep.get(res);
DoubleVector c2 = DoubleVector.wrap(shouldc[i]);
assertEquals("Expected vector doesn't match: " + c.toString(), 0.0, dist.distance(c, c2), 1e-15);
}
}
}
use of de.lmu.ifi.dbs.elki.datasource.filter.FixedDBIDsFilter in project elki by elki-project.
the class AbstractFrequentItemsetAlgorithmTest method loadTransactions.
/**
* Load a transaction database.
*
* @param filename Filename
* @param expectedSize Expected size
* @return Database
*/
public static <T> Database loadTransactions(String filename, int expectedSize) {
// Allow loading test data from resources.
try (InputStream is = open(filename)) {
// Instantiate filters manually. TODO: redesign
List<ObjectFilter> filterlist = new ArrayList<>();
filterlist.add(new FixedDBIDsFilter(1));
// Setup parser and data loading
SimpleTransactionParser parser = new SimpleTransactionParser(CSVReaderFormat.DEFAULT_FORMAT);
InputStreamDatabaseConnection dbc = new InputStreamDatabaseConnection(is, filterlist, parser);
// We want to allow the use of indexes via "params"
Database db = //
new ELKIBuilder<>(StaticArrayDatabase.class).with(AbstractDatabase.Parameterizer.DATABASE_CONNECTION_ID, dbc).build();
db.initialize();
Relation<?> rel = db.getRelation(TypeUtil.ANY);
if (expectedSize > 0) {
assertEquals("Database size does not match.", expectedSize, rel.size());
}
return db;
} catch (IOException e) {
fail("Test data " + filename + " not found.");
// Not reached.
return null;
}
}
Aggregations