Search in sources :

Example 46 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class SampleDataNodeModel method run.

private void run(final DataTableSpec spec, final RowOutput dataOutput, final DataTableSpec clusterSpec, final RowOutput clusterOutput, final ExecutionContext exec) throws Exception {
    Random rand = new Random(m_randomSeed);
    NodeLogger.getLogger(getClass()).info("Using '" + m_randomSeed + "' as seed for random data generation.");
    int dimensions = spec.getNumColumns() - 1;
    SizeSequence uniSizes = new SizeSequence(m_uniSize);
    SizeSequence clusters = new SizeSequence(m_clusterCount);
    int l = m_clusterCount.length - 1;
    final int overallClusterCount = clusters.getPosition(l) + clusters.getSize(l);
    final double noiseFrac = Math.min(Math.max(0.0, m_noiseFrac), 1.0);
    /*
         * the cluster centers. If a cluster doesn't restrict a dimension, the
         * value is NaN
         */
    double[][] optimalClusters = new double[Math.max(overallClusterCount, 1)][dimensions];
    if (overallClusterCount == 0) {
        Arrays.fill(optimalClusters[0], Double.NaN);
    }
    for (int c = 0; c < overallClusterCount; c++) {
        int uniToClusterIn = clusters.getIndex(c);
        int startPos = uniSizes.getPosition(uniToClusterIn);
        int endPos = startPos + uniSizes.getSize(uniToClusterIn);
        // assert (universeSize == uniSizes.getSize(uniToClusterIn));
        for (int d = 0; d < dimensions; d++) {
            if (d < startPos || d >= endPos) {
                optimalClusters[c][d] = Double.NaN;
            } else {
                double min = m_minValues[d];
                double max = m_maxValues[d];
                double range = max - min;
                double min2 = min + m_dev * range;
                double max2 = max - m_dev * range;
                double range2 = max2 - min2;
                double center = min2 + rand.nextDouble() * range2;
                optimalClusters[c][d] = center;
            }
        }
    }
    DataRow[] centerRows = new DataRow[overallClusterCount];
    int colNameLength = overallClusterCount + (noiseFrac > 0.0 ? 1 : 0);
    StringCell[] colNames = new StringCell[colNameLength];
    for (int i = 0; i < overallClusterCount; i++) {
        double[] cs = optimalClusters[i];
        DataCell[] cells = new DataCell[dimensions];
        for (int c = 0; c < dimensions; c++) {
            if (Double.isNaN(cs[c])) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new DoubleCell(cs[c]);
            }
        }
        colNames[i] = new StringCell("Cluster_" + i);
        centerRows[i] = new DefaultRow(colNames[i].toString(), cells);
    }
    if (noiseFrac > 0.0) {
        colNames[overallClusterCount] = new StringCell("Noise");
    }
    for (DataRow r : centerRows) {
        clusterOutput.push(r);
    }
    clusterOutput.close();
    /* first output (data) comes here */
    // assign attributes to patterns
    int noise = (int) (m_patCount * noiseFrac);
    int patternsPerCluster = (m_patCount - noise) / optimalClusters.length;
    int patternCount = patternsPerCluster * optimalClusters.length;
    noise = noiseFrac > 0.0 ? m_patCount - patternCount : 0;
    int pattern = 0;
    double totalCount = m_patCount;
    for (int c = 0; c < optimalClusters.length; c++) {
        // all clusters
        double[] centers = optimalClusters[c];
        // patterns in cluster
        for (int p = 0; p < patternsPerCluster; p++) {
            double[] d = fill(rand, centers);
            DataCell cl = (overallClusterCount > 0 ? colNames[c] : DataType.getMissingCell());
            DataRow r = createRow(RowKey.createRowKey(pattern), d, cl);
            dataOutput.push(r);
            final int patternTempFinal = pattern;
            exec.setProgress(pattern / totalCount, () -> ("Added row " + patternTempFinal));
            exec.checkCanceled();
            pattern++;
        }
    }
    assert (pattern == patternCount);
    double[] noiseCenter = new double[dimensions];
    Arrays.fill(noiseCenter, Double.NaN);
    // draw noise patterns
    for (int i = 0; i < noise; i++) {
        int index = i + pattern;
        double[] d = fill(rand, noiseCenter);
        DataCell cl = colNames[colNames.length - 1];
        DataRow r = createRow(RowKey.createRowKey(index), d, cl);
        dataOutput.push(r);
        exec.setProgress(index / totalCount, () -> ("Added row " + index));
        exec.checkCanceled();
    }
    dataOutput.close();
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) SizeSequence(javax.swing.SizeSequence) Random(java.util.Random) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 47 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class AbstractPlotter method createXCoordinate.

/**
 * Recalculates the domain of the x axis. If preserve is set to false the
 * passed values are taken as min and max no matter was was set before. If
 * preserve is set to true (default) the possibly already available min and
 * max values are preserved.
 *
 * @param min the min value
 * @param max the max value {@link AbstractPlotter#setPreserve(boolean)}
 */
public void createXCoordinate(final double min, final double max) {
    DataColumnDomainCreator xDomainCreator = new DataColumnDomainCreator();
    double actualMin = min;
    double actualMax = max;
    if (getXAxis() != null && getXAxis().getCoordinate() != null && m_preserve) {
        if (!(getXAxis().getCoordinate() instanceof NumericCoordinate)) {
            return;
        }
        actualMin = Math.min(min, ((NumericCoordinate) getXAxis().getCoordinate()).getMinDomainValue());
        actualMax = Math.max(max, ((NumericCoordinate) getXAxis().getCoordinate()).getMaxDomainValue());
    }
    xDomainCreator.setLowerBound(new DoubleCell(actualMin));
    xDomainCreator.setUpperBound(new DoubleCell(actualMax));
    DataColumnSpecCreator xSpecCreator = new DataColumnSpecCreator("X", DoubleCell.TYPE);
    xSpecCreator.setDomain(xDomainCreator.createDomain());
    Coordinate xCoordinate = Coordinate.createCoordinate(xSpecCreator.createSpec());
    if (getXAxis() == null) {
        Axis xAxis = new Axis(Axis.HORIZONTAL, getDrawingPaneDimension().width);
        setXAxis(xAxis);
    }
    getXAxis().setCoordinate(xCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DoubleCell(org.knime.core.data.def.DoubleCell) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 48 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class AbstractPlotter method createYCoordinate.

/**
 * Recalculates the domain of the y axis. If preserve is set to false the
 * passed values are taken as min and max no matter was was set before. If
 * preserve is set to true (default) the possibly already available min and
 * max values are preserved.
 *
 * @param min the min value
 * @param max the max value {@link AbstractPlotter#setPreserve(boolean)}
 */
public void createYCoordinate(final double min, final double max) {
    DataColumnDomainCreator yDomainCreator = new DataColumnDomainCreator();
    double actualMin = min;
    double actualMax = max;
    if (getYAxis() != null && getYAxis().getCoordinate() != null && m_preserve) {
        if (!(getYAxis().getCoordinate() instanceof NumericCoordinate)) {
            return;
        }
        actualMin = Math.min(min, ((NumericCoordinate) getYAxis().getCoordinate()).getMinDomainValue());
        actualMax = Math.max(max, ((NumericCoordinate) getYAxis().getCoordinate()).getMaxDomainValue());
    }
    yDomainCreator.setLowerBound(new DoubleCell(actualMin));
    yDomainCreator.setUpperBound(new DoubleCell(actualMax));
    DataColumnSpecCreator ySpecCreator = new DataColumnSpecCreator("Y", DoubleCell.TYPE);
    ySpecCreator.setDomain(yDomainCreator.createDomain());
    Coordinate yCoordinate = Coordinate.createCoordinate(ySpecCreator.createSpec());
    if (getYAxis() == null) {
        Axis yAxis = new Axis(Axis.VERTICAL, getDrawingPaneDimension().height);
        setYAxis(yAxis);
    }
    getYAxis().setCoordinate(yCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DoubleCell(org.knime.core.data.def.DoubleCell) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 49 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class BasicPlotter method addRectangle.

/**
 * Adds a rectangle defined by the upper-left corner and the width and
 * height.
 *
 * @param x x
 * @param y y
 * @param width width
 * @param height height
 * @param color color
 * @param stroke stroke
 * @param filled true if the rectangle should be filled.
 */
public void addRectangle(final double x, final double y, final double width, final double height, final Color color, final Stroke stroke, final boolean filled) {
    if (!(getDrawingPane() instanceof BasicDrawingPane)) {
        return;
    }
    createXCoordinate(x, x + width);
    createYCoordinate(y, y + height);
    int mappedX1 = (int) getXAxis().getCoordinate().calculateMappedValue(new DoubleCell(x), getDrawingPaneDimension().width);
    int mappedX2 = (int) getXAxis().getCoordinate().calculateMappedValue(new DoubleCell(x + width), getDrawingPaneDimension().width);
    int mappedY1 = (int) getYAxis().getCoordinate().calculateMappedValue(new DoubleCell(y), getDrawingPaneDimension().height);
    int mappedY2 = (int) getYAxis().getCoordinate().calculateMappedValue(new DoubleCell(y + height), getDrawingPaneDimension().height);
    BasicRectangle rectangle = new BasicRectangle(filled);
    rectangle.setPoints(new Point(mappedX1, (int) getScreenYCoordinate(mappedY1)), new Point(mappedX2, (int) getScreenYCoordinate(mappedY2)));
    rectangle.setDomainValues(new DataCellPoint(new DoubleCell(x), new DoubleCell(y)), new DataCellPoint(new DoubleCell(x + width), new DoubleCell(y + height)));
    if (color != null) {
        rectangle.setColor(color);
    }
    if (stroke != null) {
        rectangle.setStroke(stroke);
    }
    ((BasicDrawingPane) getDrawingPane()).addDrawingElement(rectangle);
    fitToScreen();
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) Point(java.awt.Point) Point(java.awt.Point)

Example 50 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class BasicPlotter method interpolateLine.

private List<Point> interpolateLine(final BasicDrawingElement line, final boolean x, final boolean y) {
    // add points between every two points
    List<Point> newPoints = new ArrayList<Point>();
    List<DataCellPoint> domainValues = line.getDomainValues();
    for (int i = 0; i < domainValues.size() - 1; i++) {
        DoubleValue x1 = (DoubleValue) domainValues.get(i).getX();
        DoubleValue y1 = (DoubleValue) domainValues.get(i).getY();
        DoubleValue x2 = (DoubleValue) domainValues.get(i + 1).getX();
        DoubleValue y2 = (DoubleValue) domainValues.get(i + 1).getY();
        // computed unmapped deltas and distance for calculating the
        // interpolation points below
        double unmappedDeltaX = x2.getDoubleValue() - x1.getDoubleValue();
        double unmappedDeltaY = y2.getDoubleValue() - y1.getDoubleValue();
        double unmappedDistance = Math.sqrt((unmappedDeltaX * unmappedDeltaX) + (unmappedDeltaY * unmappedDeltaY));
        // computed mapped distance for determining the number of
        // interpolation points
        double mappedDeltaX = getMappedXValue((DataCell) x2) - getMappedXValue((DataCell) x1);
        double mappedDeltaY = getMappedYValue((DataCell) y2) - getMappedYValue((DataCell) y1);
        double mappedDistance = Math.sqrt((mappedDeltaX * mappedDeltaX) + (mappedDeltaY * mappedDeltaY));
        // compute at least 10 interpolation points, just to be on the safe
        // side
        mappedDistance = Math.max(10, mappedDistance);
        for (int j = 0; j <= mappedDistance; j += 1) {
            double newX = x1.getDoubleValue() + (unmappedDeltaX / unmappedDistance) * j;
            double newY = y1.getDoubleValue() + (unmappedDeltaY / unmappedDistance) * j;
            // either x or Y
            if (x && newX < 1.0) {
                continue;
            }
            if (y && newY < 1.0) {
                continue;
            }
            newX = getMappedXValue(new DoubleCell(newX));
            newY = getMappedYValue(new DoubleCell(newY));
            newPoints.add(new Point((int) newX, (int) newY));
        }
    }
    return newPoints;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) Point(java.awt.Point) Point(java.awt.Point)

Aggregations

DoubleCell (org.knime.core.data.def.DoubleCell)189 DataCell (org.knime.core.data.DataCell)129 IntCell (org.knime.core.data.def.IntCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)66 StringCell (org.knime.core.data.def.StringCell)65 DataRow (org.knime.core.data.DataRow)57 DataTableSpec (org.knime.core.data.DataTableSpec)55 ArrayList (java.util.ArrayList)42 DataColumnSpec (org.knime.core.data.DataColumnSpec)42 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)41 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)39 RowKey (org.knime.core.data.RowKey)37 DoubleValue (org.knime.core.data.DoubleValue)35 BufferedDataTable (org.knime.core.node.BufferedDataTable)28 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)22 DataType (org.knime.core.data.DataType)20 LinkedHashMap (java.util.LinkedHashMap)17 HashMap (java.util.HashMap)13 Point (java.awt.Point)12