Search in sources :

Example 1 with LinearScale

use of de.lmu.ifi.dbs.elki.math.scales.LinearScale in project elki by elki-project.

the class HistogramVisualization method makeVisualization.

@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
    HistogramResult curve = task.getResult();
    final StyleLibrary style = context.getStyleLibrary();
    final double sizex = StyleLibrary.SCALE;
    final double sizey = StyleLibrary.SCALE * height / width;
    final double margin = style.getSize(StyleLibrary.MARGIN);
    Element layer = SVGUtil.svgElement(plot.getDocument(), SVGConstants.SVG_G_TAG);
    final String transform = SVGUtil.makeMarginTransform(width, height, sizex, sizey, margin);
    SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
    // find maximum, determine step size
    int dim = -1;
    DoubleMinMax xminmax = new DoubleMinMax();
    DoubleMinMax yminmax = new DoubleMinMax();
    for (double[] point : curve) {
        xminmax.put(point[0]);
        dim = dim < point.length ? point.length : dim;
        for (int i = 1; i < point.length; i++) {
            yminmax.put(point[i]);
        }
    }
    // Minimum should always start at 0 for histograms
    yminmax.put(0.0);
    // remove one dimension which are the x values.
    dim = dim - 1;
    int size = curve.size();
    double range = xminmax.getMax() - xminmax.getMin();
    double binwidth = range / (size - 1);
    LinearScale xscale = new LinearScale(xminmax.getMin() - binwidth * .49999, xminmax.getMax() + binwidth * .49999);
    LinearScale yscale = new LinearScale(yminmax.getMin(), yminmax.getMax());
    SVGPath[] path = new SVGPath[dim];
    for (int i = 0; i < dim; i++) {
        path[i] = new SVGPath(sizex * xscale.getScaled(xminmax.getMin() - binwidth * .5), sizey);
    }
    // draw curves.
    for (double[] point : curve) {
        for (int d = 0; d < dim; d++) {
            path[d].lineTo(sizex * (xscale.getScaled(point[0] - binwidth * .5)), sizey * (1 - yscale.getScaled(point[d + 1])));
            path[d].lineTo(sizex * (xscale.getScaled(point[0] + binwidth * .5)), sizey * (1 - yscale.getScaled(point[d + 1])));
        }
    }
    // close all histograms
    for (int i = 0; i < dim; i++) {
        path[i].lineTo(sizex * xscale.getScaled(xminmax.getMax() + binwidth * .5), sizey);
    }
    // add axes
    try {
        SVGSimpleLinearAxis.drawAxis(plot, layer, yscale, 0, sizey, 0, 0, SVGSimpleLinearAxis.LabelStyle.LEFTHAND, style);
        SVGSimpleLinearAxis.drawAxis(plot, layer, xscale, 0, sizey, sizex, sizey, SVGSimpleLinearAxis.LabelStyle.RIGHTHAND, style);
    } catch (CSSNamingConflict e) {
        LoggingUtil.exception(e);
    }
    // Setup line styles and insert lines.
    ColorLibrary cl = style.getColorSet(StyleLibrary.PLOT);
    for (int d = 0; d < dim; d++) {
        CSSClass csscls = new CSSClass(this, SERIESID + "_" + d);
        csscls.setStatement(SVGConstants.SVG_FILL_ATTRIBUTE, SVGConstants.SVG_NONE_VALUE);
        csscls.setStatement(SVGConstants.SVG_STROKE_ATTRIBUTE, cl.getColor(d));
        csscls.setStatement(SVGConstants.SVG_STROKE_WIDTH_ATTRIBUTE, style.getLineWidth(StyleLibrary.PLOT));
        plot.addCSSClassOrLogError(csscls);
        Element line = path[d].makeElement(plot);
        line.setAttribute(SVGConstants.SVG_CLASS_ATTRIBUTE, csscls.getName());
        layer.appendChild(line);
    }
    return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
Also used : HistogramResult(de.lmu.ifi.dbs.elki.result.HistogramResult) LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale) Element(org.w3c.dom.Element) StyleLibrary(de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary) CSSClass(de.lmu.ifi.dbs.elki.visualization.css.CSSClass) StaticVisualizationInstance(de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance) ColorLibrary(de.lmu.ifi.dbs.elki.visualization.colors.ColorLibrary) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) CSSNamingConflict(de.lmu.ifi.dbs.elki.visualization.css.CSSClassManager.CSSNamingConflict) SVGPath(de.lmu.ifi.dbs.elki.visualization.svg.SVGPath)

Example 2 with LinearScale

use of de.lmu.ifi.dbs.elki.math.scales.LinearScale in project elki by elki-project.

the class XYPlotVisualization method makeVisualization.

@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
    XYPlot xyplot = task.getResult();
    setupCSS(context, plot, xyplot);
    final StyleLibrary style = context.getStyleLibrary();
    final double sizex = StyleLibrary.SCALE;
    final double sizey = StyleLibrary.SCALE * height / width;
    final double margin = style.getSize(StyleLibrary.MARGIN);
    Element layer = SVGUtil.svgElement(plot.getDocument(), SVGConstants.SVG_G_TAG);
    final String transform = SVGUtil.makeMarginTransform(width, height, sizex, sizey, margin);
    SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
    // determine scaling
    LinearScale scalex = new LinearScale(xyplot.getMinx(), xyplot.getMaxx());
    LinearScale scaley = new LinearScale(xyplot.getMiny(), xyplot.getMaxy());
    for (XYPlot.Curve curve : xyplot) {
        // plot the line
        SVGPath path = new SVGPath();
        for (XYPlot.Curve.Itr iterator = curve.iterator(); iterator.valid(); iterator.advance()) {
            final double x = scalex.getScaled(iterator.getX());
            final double y = 1 - scaley.getScaled(iterator.getY());
            path.drawTo(sizex * x, sizey * y);
        }
        Element line = path.makeElement(plot);
        line.setAttribute(SVGConstants.SVG_CLASS_ATTRIBUTE, SERIESID + curve.getColor());
        layer.appendChild(line);
    }
    // add axes
    try {
        SVGSimpleLinearAxis.drawAxis(plot, layer, scaley, 0, sizey, 0, 0, SVGSimpleLinearAxis.LabelStyle.LEFTHAND, style);
        SVGSimpleLinearAxis.drawAxis(plot, layer, scalex, 0, sizey, sizex, sizey, SVGSimpleLinearAxis.LabelStyle.RIGHTHAND, style);
    } catch (CSSNamingConflict e) {
        LoggingUtil.exception(e);
    }
    // Add axis labels
    {
        Element labelx = plot.svgText(sizex * .5, sizey + margin * .9, xyplot.getLabelx());
        SVGUtil.setCSSClass(labelx, CSS_AXIS_LABEL);
        layer.appendChild(labelx);
        Element labely = plot.svgText(margin * -.8, sizey * .5, xyplot.getLabely());
        SVGUtil.setCSSClass(labely, CSS_AXIS_LABEL);
        SVGUtil.setAtt(labely, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, "rotate(-90," + FormatUtil.NF6.format(margin * -.8) + "," + FormatUtil.NF6.format(sizey * .5) + ")");
        layer.appendChild(labely);
    }
    return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
Also used : LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale) XYPlot(de.lmu.ifi.dbs.elki.math.geometry.XYPlot) CSSNamingConflict(de.lmu.ifi.dbs.elki.visualization.css.CSSClassManager.CSSNamingConflict) Element(org.w3c.dom.Element) StyleLibrary(de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary) SVGPath(de.lmu.ifi.dbs.elki.visualization.svg.SVGPath) StaticVisualizationInstance(de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)

Example 3 with LinearScale

use of de.lmu.ifi.dbs.elki.math.scales.LinearScale in project elki by elki-project.

the class ScatterData method initializeData.

public void initializeData(GL2 gl) {
    length = ids.size();
    dim = 0;
    vecOffset = -1;
    classOffset = -1;
    // Scan relations for dimensionalities:
    int[] dims = new int[relations.size()];
    LinearScale[][] scales = new LinearScale[relations.size()][];
    ArrayList<Relation<? extends NumberVector>> vrels = new ArrayList<>(relations.size());
    for (int r = 0; r < relations.size(); r++) {
        Relation<?> rel = relations.get(r);
        final SimpleTypeInformation<?> type = rel.getDataTypeInformation();
        if (type instanceof VectorFieldTypeInformation) {
            @SuppressWarnings("unchecked") final Relation<? extends NumberVector> vrel = (Relation<? extends NumberVector>) rel;
            final int d = ((VectorFieldTypeInformation<?>) type).getDimensionality();
            dims[r] = d;
            LinearScale[] rscales = new LinearScale[d];
            double[][] minmax = RelationUtil.computeMinMax(vrel);
            for (int i = 0; i < d; i++) {
                rscales[i] = new LinearScale(minmax[0][i], minmax[1][i]);
            }
            scales[r] = rscales;
            vrels.add(vrel);
            if (vecOffset < 0) {
                vecOffset = dim;
            }
            dim += d;
        } else {
            // FIXME: handle other relation types!
            dims[r] = 0;
            vrels.add(null);
        }
    }
    if (classOffset < 0) {
        ++dim;
    }
    LOG.warning("Dimensionalities: " + FormatUtil.format(dims));
    // Initialize vertex buffer handles:
    assert (vbos[0] == -1);
    gl.glGenBuffers(1, vbos, 0);
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbos[0]);
    gl.glBufferData(GL.GL_ARRAY_BUFFER, // 
    length * dim * SIZE_FLOAT + // safety padding
    3 * SIZE_FLOAT, null, GL2.GL_STATIC_DRAW);
    ByteBuffer vbytebuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_WRITE_ONLY);
    FloatBuffer vertices = vbytebuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
    Random rnd = new Random();
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        for (int r = 0; r < dims.length; r++) {
            if (dims[r] <= 0) {
                continue;
            }
            final Relation<? extends NumberVector> vrel = vrels.get(r);
            LinearScale[] rscales = scales[r];
            if (vrel != null) {
                NumberVector vec = vrel.get(iter);
                for (int d = 0; d < dims[r]; d++) {
                    // vertices.put( rnd.nextFloat());
                    vertices.put((float) rscales[d].getScaled(vec.doubleValue(d)) * 2.f - 1.f);
                }
            }
        }
        if (classOffset < 0) {
            vertices.put(rnd.nextInt(30));
        }
    }
    stride = dim * SIZE_FLOAT;
    if (classOffset < 0) {
        classOffset = (dim - 1) * SIZE_FLOAT;
    }
    if (vertices.position() != length * dim) {
        LOG.warning("Size mismatch: " + vertices.position() + " expected: " + length * dim, new Throwable());
    }
    vertices.flip();
    gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
    LOG.warning("Size: " + length + " dim: " + dim + " " + vecOffset + " " + classOffset);
}
Also used : LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) Random(java.util.Random) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) VectorFieldTypeInformation(de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)

Example 4 with LinearScale

use of de.lmu.ifi.dbs.elki.math.scales.LinearScale in project elki by elki-project.

the class DoubleDynamicHistogram method materialize.

/**
 * Materialize the histogram from the cache.
 */
void materialize() {
    // already materialized?
    if (cachefill < 0) {
        return;
    }
    // Compute minimum and maximum
    double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
    for (int i = 0; i < cachefill; i++) {
        min = Math.min(min, cachec[i]);
        max = Math.max(max, cachec[i]);
    }
    // use the LinearScale magic to round to "likely suiteable" step sizes.
    // TODO: extract into a reusable function?
    LinearScale scale = new LinearScale(min, max);
    min = scale.getMin();
    max = scale.getMax();
    this.base = min;
    this.max = max;
    this.binsize = (max - min) / this.destsize;
    // initialize array
    this.data = new double[this.destsize << 1];
    size = destsize;
    // re-insert data we have
    final int end = cachefill;
    // So reinsert works!
    cachefill = -1;
    for (int i = 0; i < end; i++) {
        increment(cachec[i], cachev[i]);
    }
    // delete cache, signal that we're initialized
    cachec = null;
    cachev = null;
}
Also used : LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale)

Example 5 with LinearScale

use of de.lmu.ifi.dbs.elki.math.scales.LinearScale in project elki by elki-project.

the class IntDynamicHistogram method materialize.

/**
 * Materialize the histogram from the cache.
 */
void materialize() {
    // already materialized?
    if (cachefill < 0) {
        return;
    }
    // Compute minimum and maximum
    double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
    for (int i = 0; i < cachefill; i++) {
        min = Math.min(min, cachec[i]);
        max = Math.max(max, cachec[i]);
    }
    // use the LinearScale magic to round to "likely suiteable" step sizes.
    // TODO: extract into a reusable function?
    LinearScale scale = new LinearScale(min, max);
    min = scale.getMin();
    max = scale.getMax();
    this.base = min;
    this.max = max;
    this.binsize = (max - min) / this.destsize;
    // initialize array
    this.data = new int[this.destsize << 1];
    size = destsize;
    // re-insert data we have
    final int end = cachefill;
    // So reinsert works!
    cachefill = -1;
    for (int i = 0; i < end; i++) {
        increment(cachec[i], cachev[i]);
    }
    // delete cache, signal that we're initialized
    cachec = null;
    cachev = null;
}
Also used : LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale)

Aggregations

LinearScale (de.lmu.ifi.dbs.elki.math.scales.LinearScale)14 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)3 DoubleMinMax (de.lmu.ifi.dbs.elki.math.DoubleMinMax)3 CSSNamingConflict (de.lmu.ifi.dbs.elki.visualization.css.CSSClassManager.CSSNamingConflict)3 StyleLibrary (de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary)3 SVGPath (de.lmu.ifi.dbs.elki.visualization.svg.SVGPath)3 StaticVisualizationInstance (de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)3 Element (org.w3c.dom.Element)3 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)2 ScalesResult (de.lmu.ifi.dbs.elki.result.ScalesResult)2 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 PRCurve (de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierPrecisionRecallCurve.PRCurve)1 ROCResult (de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierROCCurve.ROCResult)1 XYCurve (de.lmu.ifi.dbs.elki.math.geometry.XYCurve)1 XYPlot (de.lmu.ifi.dbs.elki.math.geometry.XYPlot)1 HistogramResult (de.lmu.ifi.dbs.elki.result.HistogramResult)1 ListSizeConstraint (de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ListSizeConstraint)1 IntIntPair (de.lmu.ifi.dbs.elki.utilities.pairs.IntIntPair)1 ColorLibrary (de.lmu.ifi.dbs.elki.visualization.colors.ColorLibrary)1