Search in sources :

Example 11 with Relation

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

the class TextWriter method printObject.

private void printObject(TextWriterStream out, Database db, final DBIDRef objID, List<Relation<?>> ra) throws IOException {
    SingleObjectBundle bundle = db.getBundle(objID);
    // Write database element itself.
    for (int i = 0; i < bundle.metaLength(); i++) {
        Object obj = bundle.data(i);
        if (obj != null) {
            TextWriterWriterInterface<?> owriter = out.getWriterFor(obj);
            if (owriter == null) {
                throw new IOException("No handler for database object itself: " + obj.getClass().getSimpleName());
            }
            String lbl = null;
            // TODO: ugly compatibility hack...
            if (TypeUtil.DBID.isAssignableFromType(bundle.meta(i))) {
                lbl = "ID";
            }
            owriter.writeObject(out, lbl, obj);
        }
    }
    Collection<Relation<?>> dbrels = db.getRelations();
    // print the annotations
    if (ra != null) {
        for (Relation<?> a : ra) {
            // Avoid duplicated output.
            if (dbrels.contains(a)) {
                continue;
            }
            String label = a.getShortName();
            Object value = a.get(objID);
            if (value == null) {
                continue;
            }
            TextWriterWriterInterface<?> writer = out.getWriterFor(value);
            if (writer == null) {
                // Ignore
                continue;
            }
            writer.writeObject(out, label, value);
        }
    }
    out.flush();
}
Also used : Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) SingleObjectBundle(de.lmu.ifi.dbs.elki.datasource.bundle.SingleObjectBundle) IOException(java.io.IOException)

Example 12 with Relation

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

the class StaticArrayDatabase method initialize.

/**
 * Initialize the database by getting the initial data from the database
 * connection.
 */
@Override
public void initialize() {
    if (databaseConnection != null) {
        if (LOG.isDebugging()) {
            LOG.debugFine("Loading data from database connection.");
        }
        MultipleObjectsBundle bundle = databaseConnection.loadData();
        // Run at most once.
        databaseConnection = null;
        // Find DBIDs for bundle
        {
            DBIDs bids = bundle.getDBIDs();
            if (bids instanceof ArrayStaticDBIDs) {
                this.ids = (ArrayStaticDBIDs) bids;
            } else if (bids == null) {
                this.ids = DBIDUtil.generateStaticDBIDRange(bundle.dataLength());
            } else {
                this.ids = (ArrayStaticDBIDs) DBIDUtil.makeUnmodifiable(bids);
            }
        }
        // Replace id representation (it would be nicer if we would not need
        // DBIDView at all)
        this.idrep = new DBIDView(this.ids);
        relations.add(this.idrep);
        getHierarchy().add(this, idrep);
        DBIDArrayIter it = this.ids.iter();
        int numrel = bundle.metaLength();
        for (int i = 0; i < numrel; i++) {
            SimpleTypeInformation<?> meta = bundle.meta(i);
            @SuppressWarnings("unchecked") SimpleTypeInformation<Object> ometa = (SimpleTypeInformation<Object>) meta;
            WritableDataStore<Object> store = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_DB, ometa.getRestrictionClass());
            for (it.seek(0); it.valid(); it.advance()) {
                store.put(it, bundle.data(it.getOffset(), i));
            }
            Relation<?> relation = new MaterializedRelation<>(ometa, ids, null, store);
            relations.add(relation);
            getHierarchy().add(this, relation);
            // Try to add indexes where appropriate
            for (IndexFactory<?, ?> factory : indexFactories) {
                if (factory.getInputTypeRestriction().isAssignableFromType(ometa)) {
                    @SuppressWarnings("unchecked") final IndexFactory<Object, ?> ofact = (IndexFactory<Object, ?>) factory;
                    @SuppressWarnings("unchecked") final Relation<Object> orep = (Relation<Object>) relation;
                    final Index index = ofact.instantiate(orep);
                    Duration duration = LOG.isStatistics() ? LOG.newDuration(index.getClass().getName() + ".construction").begin() : null;
                    index.initialize();
                    if (duration != null) {
                        LOG.statistics(duration.end());
                    }
                    getHierarchy().add(relation, index);
                }
            }
        }
        // fire insertion event
        eventManager.fireObjectsInserted(ids);
    }
}
Also used : DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) ArrayStaticDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs) MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) SimpleTypeInformation(de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Index(de.lmu.ifi.dbs.elki.index.Index) Duration(de.lmu.ifi.dbs.elki.logging.statistics.Duration) ArrayStaticDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) IndexFactory(de.lmu.ifi.dbs.elki.index.IndexFactory) DBIDView(de.lmu.ifi.dbs.elki.database.relation.DBIDView)

Example 13 with Relation

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

the class KMLOutputHandler method writeOutlierResult.

private void writeOutlierResult(XMLStreamWriter xmlw, OutlierResult outlierResult, Database database) throws XMLStreamException {
    Relation<PolygonsObject> polys = database.getRelation(TypeUtil.POLYGON_TYPE);
    Relation<String> labels = DatabaseUtil.guessObjectLabelRepresentation(database);
    xmlw.writeStartDocument();
    xmlw.writeCharacters("\n");
    xmlw.writeStartElement("kml");
    xmlw.writeDefaultNamespace("http://earth.google.com/kml/2.2");
    xmlw.writeStartElement("Document");
    {
        // TODO: can we automatically generate more helpful data here?
        xmlw.writeStartElement("name");
        xmlw.writeCharacters("ELKI KML output for " + outlierResult.getLongName());
        // name
        xmlw.writeEndElement();
        writeNewlineOnDebug(xmlw);
        // TODO: e.g. list the settings in the description?
        xmlw.writeStartElement("description");
        xmlw.writeCharacters("ELKI KML output for " + outlierResult.getLongName());
        // description
        xmlw.writeEndElement();
        writeNewlineOnDebug(xmlw);
    }
    {
        // TODO: generate styles from color scheme
        for (int i = 0; i < NUMSTYLES; i++) {
            Color col = getColorForValue(i / (NUMSTYLES - 1.0));
            xmlw.writeStartElement("Style");
            xmlw.writeAttribute("id", "s" + i);
            writeNewlineOnDebug(xmlw);
            {
                xmlw.writeStartElement("LineStyle");
                xmlw.writeStartElement("width");
                xmlw.writeCharacters("0");
                // width
                xmlw.writeEndElement();
                // LineStyle
                xmlw.writeEndElement();
            }
            writeNewlineOnDebug(xmlw);
            {
                xmlw.writeStartElement("PolyStyle");
                xmlw.writeStartElement("color");
                // KML uses AABBGGRR format!
                xmlw.writeCharacters(String.format("%02x%02x%02x%02x", col.getAlpha(), col.getBlue(), col.getGreen(), col.getRed()));
                // color
                xmlw.writeEndElement();
                // out.writeStartElement("fill");
                // out.writeCharacters("1"); // Default 1
                // out.writeEndElement(); // fill
                xmlw.writeStartElement("outline");
                xmlw.writeCharacters("0");
                // outline
                xmlw.writeEndElement();
                // PolyStyle
                xmlw.writeEndElement();
            }
            writeNewlineOnDebug(xmlw);
            // Style
            xmlw.writeEndElement();
            writeNewlineOnDebug(xmlw);
        }
    }
    DoubleRelation scores = outlierResult.getScores();
    Collection<Relation<?>> otherrel = new LinkedList<>(database.getRelations());
    otherrel.remove(scores);
    otherrel.remove(polys);
    otherrel.remove(labels);
    otherrel.remove(database.getRelation(TypeUtil.DBID));
    ArrayModifiableDBIDs ids = DBIDUtil.newArray(scores.getDBIDs());
    scaling.prepare(outlierResult);
    for (DBIDIter iter = outlierResult.getOrdering().order(ids).iter(); iter.valid(); iter.advance()) {
        double score = scores.doubleValue(iter);
        PolygonsObject poly = polys.get(iter);
        String label = labels.get(iter);
        if (Double.isNaN(score)) {
            LOG.warning("No score for object " + DBIDUtil.toString(iter));
        }
        if (poly == null) {
            LOG.warning("No polygon for object " + DBIDUtil.toString(iter) + " - skipping.");
            continue;
        }
        xmlw.writeStartElement("Placemark");
        {
            xmlw.writeStartElement("name");
            xmlw.writeCharacters(score + " " + label);
            // name
            xmlw.writeEndElement();
            StringBuilder buf = makeDescription(otherrel, iter);
            xmlw.writeStartElement("description");
            xmlw.writeCData("<div>" + buf.toString() + "</div>");
            // description
            xmlw.writeEndElement();
            xmlw.writeStartElement("styleUrl");
            int style = (int) (scaling.getScaled(score) * NUMSTYLES);
            style = Math.max(0, Math.min(style, NUMSTYLES - 1));
            xmlw.writeCharacters("#s" + style);
            // styleUrl
            xmlw.writeEndElement();
        }
        {
            xmlw.writeStartElement("Polygon");
            writeNewlineOnDebug(xmlw);
            if (compat) {
                xmlw.writeStartElement("altitudeMode");
                xmlw.writeCharacters("relativeToGround");
                // close altitude mode
                xmlw.writeEndElement();
                writeNewlineOnDebug(xmlw);
            }
            // First polygon clockwise?
            boolean first = true;
            for (Polygon p : poly.getPolygons()) {
                if (first) {
                    xmlw.writeStartElement("outerBoundaryIs");
                } else {
                    xmlw.writeStartElement("innerBoundaryIs");
                }
                xmlw.writeStartElement("LinearRing");
                xmlw.writeStartElement("coordinates");
                // Reverse anti-clockwise polygons.
                boolean reverse = (p.testClockwise() >= 0);
                ArrayListIter<double[]> it = p.iter();
                if (reverse) {
                    it.seek(p.size() - 1);
                }
                while (it.valid()) {
                    double[] v = it.get();
                    xmlw.writeCharacters(FormatUtil.format(v, ","));
                    if (compat && (v.length == 2)) {
                        xmlw.writeCharacters(",50");
                    }
                    xmlw.writeCharacters(" ");
                    if (!reverse) {
                        it.advance();
                    } else {
                        it.retract();
                    }
                }
                // close coordinates
                xmlw.writeEndElement();
                // close LinearRing
                xmlw.writeEndElement();
                // close *BoundaryIs
                xmlw.writeEndElement();
                first = false;
            }
            writeNewlineOnDebug(xmlw);
            // Polygon
            xmlw.writeEndElement();
        }
        // Placemark
        xmlw.writeEndElement();
        writeNewlineOnDebug(xmlw);
    }
    // Document
    xmlw.writeEndElement();
    // kml
    xmlw.writeEndElement();
    xmlw.writeEndDocument();
}
Also used : ArrayListIter(de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.ArrayListIter) Color(java.awt.Color) PolygonsObject(de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) LinkedList(java.util.LinkedList) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) Polygon(de.lmu.ifi.dbs.elki.data.spatial.Polygon)

Example 14 with Relation

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

the class HashmapDatabase method insert.

@Override
public DBIDs insert(ObjectBundle objpackages) {
    if (objpackages.dataLength() == 0) {
        return DBIDUtil.EMPTYDBIDS;
    }
    // insert into db
    ArrayModifiableDBIDs newids = DBIDUtil.newArray(objpackages.dataLength());
    Relation<?>[] targets = alignColumns(objpackages);
    DBIDVar var = DBIDUtil.newVar();
    for (int j = 0; j < objpackages.dataLength(); j++) {
        // insert object
        if (!objpackages.assignDBID(j, var)) {
            var.set(DBIDUtil.generateSingleDBID());
        }
        if (ids.contains(var)) {
            throw new AbortException("Duplicate DBID conflict.");
        }
        ids.add(var);
        for (int i = 0; i < targets.length; i++) {
            if (!(targets[i] instanceof ModifiableRelation)) {
                throw new AbortException("Non-modifiable relations have been added to the database.");
            }
            @SuppressWarnings("unchecked") final ModifiableRelation<Object> relation = (ModifiableRelation<Object>) targets[i];
            relation.insert(var, objpackages.data(j, i));
        }
        newids.add(var);
    }
    // fire insertion event
    eventManager.fireObjectsInserted(newids);
    return newids;
}
Also used : ModifiableRelation(de.lmu.ifi.dbs.elki.database.relation.ModifiableRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) DBIDVar(de.lmu.ifi.dbs.elki.database.ids.DBIDVar) ModifiableRelation(de.lmu.ifi.dbs.elki.database.relation.ModifiableRelation) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 15 with Relation

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

the class HashmapDatabase method addNewRelation.

/**
 * Add a new representation for the given meta.
 *
 * @param meta meta data
 * @return new representation
 */
private Relation<?> addNewRelation(SimpleTypeInformation<?> meta) {
    @SuppressWarnings("unchecked") SimpleTypeInformation<Object> ometa = (SimpleTypeInformation<Object>) meta;
    Relation<?> relation = new MaterializedRelation<>(ometa, ids);
    relations.add(relation);
    getHierarchy().add(this, relation);
    // Try to add indexes where appropriate
    for (IndexFactory<?, ?> factory : indexFactories) {
        if (factory.getInputTypeRestriction().isAssignableFromType(meta)) {
            @SuppressWarnings("unchecked") final IndexFactory<Object, ?> ofact = (IndexFactory<Object, ?>) factory;
            @SuppressWarnings("unchecked") final Relation<Object> orep = (Relation<Object>) relation;
            Index index = ofact.instantiate(orep);
            index.initialize();
            getHierarchy().add(relation, index);
        }
    }
    return relation;
}
Also used : ModifiableRelation(de.lmu.ifi.dbs.elki.database.relation.ModifiableRelation) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation) IndexFactory(de.lmu.ifi.dbs.elki.index.IndexFactory) SimpleTypeInformation(de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation) Index(de.lmu.ifi.dbs.elki.index.Index) MaterializedRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation)

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