Search in sources :

Example 71 with DoubleCell

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

the class KnnNodeModel method classify.

// returns a list where the first value if the winner class, and the
// following values are the class probabilities (if enabled)
private List<DataCell> classify(final DataRow row, final KDTree<DataCell> tree, final List<Integer> featureColumns, final Map<Integer, Integer> firstToSecond, final DataCell[] allClassValues) {
    double[] features = createQueryVector(row, featureColumns, firstToSecond);
    List<DataCell> output = new ArrayList<DataCell>();
    if (features == null) {
        for (int i = 0; i < 1 + allClassValues.length; i++) {
            output.add(DataType.getMissingCell());
        }
        return output;
    }
    HashMap<DataCell, MutableDouble> classWeights = new LinkedHashMap<DataCell, MutableDouble>();
    List<NearestNeighbour<DataCell>> nearestN = tree.getKNearestNeighbours(features, Math.min(m_settings.k(), tree.size()));
    for (NearestNeighbour<DataCell> n : nearestN) {
        MutableDouble count = classWeights.get(n.getData());
        if (count == null) {
            count = new MutableDouble(0);
            classWeights.put(n.getData(), count);
        }
        if (m_settings.weightByDistance()) {
            count.add(1 / n.getDistance());
        } else {
            count.inc();
        }
    }
    double winnerWeight = 0;
    double weightSum = 0;
    DataCell winnerCell = DataType.getMissingCell();
    for (Map.Entry<DataCell, MutableDouble> e : classWeights.entrySet()) {
        double weight = e.getValue().doubleValue();
        if (weight > winnerWeight) {
            winnerWeight = weight;
            winnerCell = e.getKey();
        }
        weightSum += weight;
    }
    // check if there are other classes with the same weight
    for (Map.Entry<DataCell, MutableDouble> e : classWeights.entrySet()) {
        double weight = e.getValue().doubleValue();
        if (weight == winnerWeight) {
            if (m_classDistribution.get(winnerCell).intValue() < m_classDistribution.get(e.getKey()).intValue()) {
                winnerCell = e.getKey();
            }
        }
    }
    output.add(winnerCell);
    if (m_settings.outputClassProbabilities()) {
        for (DataCell classVal : allClassValues) {
            MutableDouble v = classWeights.get(classVal);
            if (v == null) {
                output.add(new DoubleCell(0));
            // } else if (Double.isInfinite(v.doubleValue())) { // if distance to prototype is 0
            // output.add(new DoubleCell(1));
            } else {
                output.add(new DoubleCell(v.doubleValue() / weightSum));
            }
        }
    }
    return output;
}
Also used : MutableDouble(org.knime.core.util.MutableDouble) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NearestNeighbour(org.knime.base.util.kdtree.NearestNeighbour) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 72 with DoubleCell

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

the class PCANodeModel method createCovarianceTable.

/**
 * create data table from covariance matrix.
 *
 * @param exec
 *            execution context
 * @param m
 *            covariance matrix
 * @param inputColumnNames
 *            names of input columns the matrix was created from
 * @return table
 */
public static BufferedDataTable createCovarianceTable(final ExecutionContext exec, final double[][] m, final String[] inputColumnNames) {
    final BufferedDataContainer bdt = exec.createDataContainer(createCovarianceMatrixSpec(inputColumnNames));
    for (int i = 0; i < m.length; i++) {
        final DataCell[] cells = new DataCell[inputColumnNames.length];
        for (int j = 0; j < m[i].length; j++) {
            cells[j] = new DoubleCell(m[i][j]);
        }
        bdt.addRowToTable(new DefaultRow(inputColumnNames[i], cells));
    }
    bdt.close();
    final BufferedDataTable covarianceTable = bdt.getTable();
    return covarianceTable;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 73 with DoubleCell

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

the class BinModelPlotter method updatePaintModel.

/**
 * {@inheritDoc}
 */
@Override
public synchronized void updatePaintModel() {
    if (m_discretizationModel == null) {
        return;
    }
    // clear the drawing pane
    ((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(null);
    // get the first columns
    if (m_selectedColumns == null) {
        m_selectedColumns = new LinkedHashSet<String>();
        String[] binnedColumnNames = m_discretizationModel.getIncludedColumnNames();
        for (int i = 0; i < binnedColumnNames.length; i++) {
            // add them to the selected columns
            m_selectedColumns.add(binnedColumnNames[i]);
        }
        ((MultiColumnPlotterProperties) getProperties()).updateColumnSelection(m_binnedColumnsSpec, m_selectedColumns);
    }
    if (m_selectedColumns.size() == 0) {
        getDrawingPane().repaint();
        return;
    }
    Set<DataCell> selectedColumnCells = new LinkedHashSet<DataCell>();
    m_coordinates = new ArrayList<Coordinate>();
    List<Integer> columnIndices = new ArrayList<Integer>();
    for (String name : m_selectedColumns) {
        int idx = m_binnedColumnsSpec.findColumnIndex(name);
        if (idx >= 0) {
            selectedColumnCells.add(new StringCell(name));
            DataColumnSpec colSpec = m_binnedColumnsSpec.getColumnSpec(idx);
            columnIndices.add(idx);
            Coordinate coordinate = Coordinate.createCoordinate(colSpec);
            m_coordinates.add(coordinate);
        }
    }
    // get the binning schemes for the selected columns
    DiscretizationScheme[] selectedSchemes = getSelectedSchemes();
    String[] selectedColumnNames = getSelectedColumnNames();
    // calculate the display coordinates for the drawing pane
    BinRuler[] binRulers = new BinRuler[selectedSchemes.length];
    // determine the width available for a bin ruler
    int rulerWidth = getDrawingPaneDimension().width - 2 * m_hMargin;
    for (int i = 0; i < selectedSchemes.length; i++) {
        double[] bounds = selectedSchemes[i].getBounds();
        double min = bounds[0];
        double max = bounds[bounds.length - 1];
        // first create a colum spec from the schemes
        DataColumnSpecCreator columnSpecCreator = new DataColumnSpecCreator("", DoubleCell.TYPE);
        columnSpecCreator.setDomain(new DataColumnDomainCreator(new DoubleCell(min), new DoubleCell(max)).createDomain());
        DoubleCoordinate coordinate = (DoubleCoordinate) Coordinate.createCoordinate(columnSpecCreator.createSpec());
        Point leftStart = new Point(m_hMargin, m_vMargin + (i + 1) * m_columnDisplayHeight);
        int[] binPositions = new int[bounds.length];
        String[] binLabels = new String[bounds.length];
        int count = 0;
        for (double bound : bounds) {
            binPositions[count] = (int) coordinate.calculateMappedValue(new DoubleCell(bound), rulerWidth, true);
            binLabels[count] = coordinate.formatNumber(bounds[count]);
            count++;
        }
        binRulers[i] = new BinRuler(leftStart, rulerWidth, binPositions, binLabels, selectedColumnNames[i]);
    }
    ((BinModelDrawingPane) getDrawingPane()).setBinningSchemes(binRulers);
    m_hMargin = 10;
    m_vMargin = 10;
    ((BinModelDrawingPane) getDrawingPane()).setHorizontalMargin(m_hMargin);
    setHeight(binRulers[binRulers.length - 1].getLeftStartPoint().y + 40);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DiscretizationScheme(org.knime.base.node.preproc.discretization.caim2.DiscretizationScheme) ArrayList(java.util.ArrayList) DoubleCoordinate(org.knime.base.util.coordinate.DoubleCoordinate) DataColumnSpec(org.knime.core.data.DataColumnSpec) MultiColumnPlotterProperties(org.knime.base.node.viz.plotter.columns.MultiColumnPlotterProperties) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) Point(java.awt.Point) Point(java.awt.Point) DoubleCoordinate(org.knime.base.util.coordinate.DoubleCoordinate) Coordinate(org.knime.base.util.coordinate.Coordinate) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell)

Example 74 with DoubleCell

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

the class ColumnRowFilterPanel method getBoundCell.

/* method used from the above */
private DataCell getBoundCell(final JTextField editField, final String name) throws InvalidSettingsException {
    if (editField.getText().length() <= 0) {
        return null;
    }
    String colName = getSelectedColumnName();
    if ((colName == null) || (colName.length() == 0)) {
        throw new InvalidSettingsException("Invalid columns selection");
    }
    if (m_tSpec != null) {
        final DataType origType = m_tSpec.getColumnSpec(colName).getType();
        final DataType cType;
        if (m_deepFiltering.isSelected() && origType.isCollectionType()) {
            cType = origType.getCollectionElementType();
        } else {
            cType = origType;
        }
        if (cType.isCompatible(IntValue.class)) {
            // first try making of an IntCell
            try {
                int lb = Integer.parseInt(editField.getText());
                return new IntCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid integer.");
            }
        } else if (cType.isCompatible(LongValue.class)) {
            try {
                long lb = Long.parseLong(editField.getText());
                return new LongCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: Enter a valid number.");
            }
        } else if (cType.isCompatible(DoubleValue.class)) {
            try {
                double lb = Double.parseDouble(editField.getText());
                return new DoubleCell(lb);
            } catch (NumberFormatException nfe) {
                throw new InvalidSettingsException("Number format error in " + name + " bound number: enter a valid " + "float number");
            }
        } else {
            return new StringCell(editField.getText());
        }
    } else {
        // if we got no column type
        return new StringCell(editField.getText());
    }
}
Also used : LongCell(org.knime.core.data.def.LongCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) LongValue(org.knime.core.data.LongValue) DataType(org.knime.core.data.DataType) IntCell(org.knime.core.data.def.IntCell)

Example 75 with DoubleCell

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

the class ConditionalBoxPlotNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    m_statistics = new LinkedHashMap<DataColumnSpec, double[]>();
    m_mildOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
    m_extremeOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
    double nrRows = inData[0].size();
    int rowCount = 0;
    int numericIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.numericColumn());
    int nominalIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.nominalColumn());
    Map<String, Map<Double, Set<RowKey>>> data = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
    // some default values .. if one column only has missing values.
    for (DataCell d : inData[0].getDataTableSpec().getColumnSpec(nominalIndex).getDomain().getValues()) {
        String name = ((StringValue) d).getStringValue();
        m_mildOutliers.put(name, new HashMap<Double, Set<RowKey>>());
        m_extremeOutliers.put(name, new HashMap<Double, Set<RowKey>>());
    }
    for (DataRow r : inData[0]) {
        exec.checkCanceled();
        exec.setProgress(rowCount++ / nrRows, "Separating...");
        if (!m_settings.showMissingValues()) {
            if (r.getCell(nominalIndex).isMissing()) {
                // missing cell in nominal values is unwanted?
                continue;
            }
        }
        String nominal = replaceSpaces(r.getCell(nominalIndex).toString());
        if (r.getCell(numericIndex).isMissing()) {
            // ignore missing cells in numeric column
            continue;
        }
        DoubleValue numeric = (DoubleValue) r.getCell(numericIndex);
        Map<Double, Set<RowKey>> map = data.get(nominal);
        if (map == null) {
            map = new LinkedHashMap<Double, Set<RowKey>>();
        }
        Set<RowKey> set = map.get(numeric.getDoubleValue());
        if (set == null) {
            set = new HashSet<RowKey>();
        }
        set.add(r.getKey());
        map.put(numeric.getDoubleValue(), set);
        data.put(nominal, map);
    }
    List<String> keys = new ArrayList<String>(data.keySet());
    boolean ignoreMissingValues = false;
    if (m_settings.showMissingValues() && !keys.contains(DataType.getMissingCell().toString())) {
        // we promised to create data for missing values..
        // if there aren't any.. we have to create them ourselves
        setWarningMessage("No missing values found.");
        ignoreMissingValues = true;
    }
    Collections.sort(keys);
    DataColumnSpec[] colSpecs = createColumnSpec(inData[0].getDataTableSpec().getColumnSpec(nominalIndex), ignoreMissingValues);
    if (keys.size() == 0) {
        setWarningMessage("All classes are empty.");
    }
    int dataSetNr = 0;
    // for (String d : keys) {
    for (DataColumnSpec dcs : colSpecs) {
        String d = dcs.getName();
        if (data.get(d) == null || keys.size() == 0) {
            dataSetNr++;
            continue;
        }
        exec.checkCanceled();
        exec.setProgress(dataSetNr / (double) keys.size(), "Creating statistics");
        Map<Double, Set<RowKey>> extremeOutliers = new LinkedHashMap<Double, Set<RowKey>>();
        Map<Double, Set<RowKey>> mildOutliers = new LinkedHashMap<Double, Set<RowKey>>();
        double[] stats = calculateStatistic(data.get(d), mildOutliers, extremeOutliers);
        double minimum = stats[BoxPlotNodeModel.MIN];
        double maximum = stats[BoxPlotNodeModel.MAX];
        DataColumnSpecCreator creator = new DataColumnSpecCreator(colSpecs[dataSetNr]);
        creator.setDomain(new DataColumnDomainCreator(new DoubleCell(minimum), new DoubleCell(maximum)).createDomain());
        colSpecs[dataSetNr] = creator.createSpec();
        m_statistics.put(colSpecs[dataSetNr], stats);
        m_mildOutliers.put(d, mildOutliers);
        m_extremeOutliers.put(d, extremeOutliers);
        dataSetNr++;
    }
    DataTableSpec dts = new DataTableSpec("MyTempTable", colSpecs);
    DataContainer cont = new DataContainer(dts);
    cont.close();
    m_dataArray = new DefaultDataArray(cont.getTable(), 1, 2);
    cont.dispose();
    if (ignoreMissingValues) {
        DataColumnSpec[] temp = new DataColumnSpec[colSpecs.length + 1];
        DataColumnSpec missing = new DataColumnSpecCreator(DataType.getMissingCell().toString(), DataType.getMissingCell().getType()).createSpec();
        int i = 0;
        while (missing.getName().compareTo(colSpecs[i].getName()) > 0) {
            temp[i] = colSpecs[i];
            i++;
        }
        temp[i++] = missing;
        while (i < temp.length) {
            temp[i] = colSpecs[i - 1];
            i++;
        }
        colSpecs = temp;
    }
    /* Save inSpec of the numeric column to provide the view a way to
         * consider the input domain for normalization. */
    m_numColSpec = inData[0].getDataTableSpec().getColumnSpec(numericIndex);
    return new BufferedDataTable[] { createOutputTable(inData[0].getDataTableSpec(), colSpecs, exec).getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashSet(java.util.HashSet) Set(java.util.Set) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) DataContainer(org.knime.core.data.container.DataContainer) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) StringValue(org.knime.core.data.StringValue) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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