Search in sources :

Example 81 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class ARFFWriterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    checkFileAccess(m_location, false);
    URL url = FileUtil.toURL(m_location);
    Path localPath = FileUtil.resolveToPath(url);
    DataTableSpec inSpec = inData[0].getDataTableSpec();
    int numOfCols = inSpec.getNumColumns();
    for (int c = 0; c < numOfCols; c++) {
        DataType colType = inSpec.getColumnSpec(c).getType();
        if (!colType.isCompatible(IntValue.class) && !colType.isCompatible(DoubleValue.class) && !colType.isCompatible(StringValue.class)) {
            throw new IllegalStateException("Can only write Double, Int," + " and String columns to ARFF file.");
        }
    }
    LOGGER.info("ARFF Writer: ARFFing into '" + m_location + "'.");
    try (BufferedWriter writer = openWriter(localPath, url)) {
        // Write ARFF header
        writer.write("%\n");
        writer.write("% ARFF data file, generated by KNIME\n");
        writer.write("%\n");
        writer.write("% Date: " + new Date(System.currentTimeMillis()) + "\n");
        try {
            writer.write("% User: " + System.getProperty("user.name") + "\n");
        } catch (SecurityException se) {
        // okay - we don't add the user name.
        }
        writer.write("%\n");
        writer.write("\n@RELATION " + m_relationName + "\n");
        // write the attribute part, i.e. the columns' name and type
        for (int c = 0; c < numOfCols; c++) {
            DataColumnSpec cSpec = inSpec.getColumnSpec(c);
            writer.write("@ATTRIBUTE ");
            if (needsQuotes(cSpec.getName().toString())) {
                writer.write("'" + cSpec.getName().toString() + "'");
            } else {
                writer.write(cSpec.getName().toString());
            }
            writer.write("\t");
            writer.write(colspecToARFFType(cSpec));
            writer.write("\n");
        }
        // finally add the data
        writer.write("\n@DATA\n");
        long rowCnt = inData[0].size();
        long rowNr = 0;
        for (DataRow row : inData[0]) {
            rowNr++;
            exec.setProgress(rowNr / (double) rowCnt, "Writing row " + rowNr + " ('" + row.getKey() + "') of " + rowCnt);
            if (m_sparse) {
                writer.write("{");
            }
            // flag to skip comma in first column
            boolean first = true;
            for (int c = 0; c < row.getNumCells(); c++) {
                DataCell cell = row.getCell(c);
                if (m_sparse && !cell.isMissing()) {
                    // we write only non-zero values in a sparse file
                    if ((cell instanceof IntValue) && (((IntValue) cell).getIntValue() == 0)) {
                        continue;
                    }
                    if ((cell instanceof DoubleValue) && (Math.abs(((DoubleValue) cell).getDoubleValue()) < 1e-29)) {
                        continue;
                    }
                }
                String data = "?";
                if (!cell.isMissing()) {
                    data = cell.toString();
                }
                // trigger quotes.
                if (needsQuotes(data)) {
                    data = "'" + data + "'";
                }
                // now spit it out
                if (!first) {
                    // print column separator
                    writer.write(",");
                } else {
                    first = false;
                }
                // data in sparse file must be proceeded by the column number
                if (m_sparse) {
                    writer.write("" + c + " ");
                }
                writer.write(data);
            }
            if (m_sparse) {
                writer.write("}");
            }
            writer.write("\n");
            // see if user told us to stop.
            // Check if execution was canceled !
            exec.checkCanceled();
        }
    // while (!rIter.atEnd())
    } catch (CanceledExecutionException ex) {
        if (localPath != null) {
            Files.deleteIfExists(localPath);
            LOGGER.debug("File '" + localPath + "' deleted.");
        }
        throw ex;
    }
    // execution successful return empty array
    return new BufferedDataTable[0];
}
Also used : Path(java.nio.file.Path) DataTableSpec(org.knime.core.data.DataTableSpec) DataRow(org.knime.core.data.DataRow) URL(java.net.URL) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) IntValue(org.knime.core.data.IntValue)

Example 82 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class TableColumnToVariableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    if (inData[0] instanceof BufferedDataTable) {
        final BufferedDataTable table = (BufferedDataTable) inData[0];
        final int colIndex = table.getSpec().findColumnIndex(m_column.getStringValue());
        assert colIndex >= 0 : colIndex;
        for (DataRow dataRow : table) {
            DataCell cell = dataRow.getCell(colIndex);
            if (cell.isMissing()) {
                if (m_ignoreMissing.getBooleanValue()) {
                    continue;
                }
                throw new Exception("Missing value in column (" + m_column.getColumnName() + ") in row: " + dataRow.getKey());
            }
            if (cell instanceof IntValue) {
                final IntValue iv = (IntValue) cell;
                pushFlowVariableInt(dataRow.getKey().getString(), iv.getIntValue());
            } else if (cell instanceof DoubleValue) {
                final DoubleValue dv = (DoubleValue) cell;
                pushFlowVariableDouble(dataRow.getKey().getString(), dv.getDoubleValue());
            } else if (cell instanceof StringValue) {
                final StringValue sv = (StringValue) cell;
                pushFlowVariableString(dataRow.getKey().getString(), sv.getStringValue());
            }
        }
    }
    return new FlowVariablePortObject[] { FlowVariablePortObject.INSTANCE };
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DataRow(org.knime.core.data.DataRow) IntValue(org.knime.core.data.IntValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 83 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class MissingValueHandling2Panel method getFixTextField.

/*
     * Helper in constructor, generates the text field to enter the replacement
     * value.
     */
private static JComponent getFixTextField(final MissingValueHandling2ColSetting setting, final DataColumnSpec... specs) {
    JComponent fixText;
    // FIX text field
    DataCell fixCell = setting.getFixCell();
    switch(setting.getType()) {
        case MissingValueHandling2ColSetting.TYPE_DOUBLE:
            fixText = new JFormattedTextField();
            ((JFormattedTextField) fixText).setColumns(11);
            Double doubel;
            if (fixCell == null) {
                doubel = new Double(0.0);
            } else {
                double d = ((DoubleValue) fixCell).getDoubleValue();
                doubel = new Double(d);
            }
            ((JFormattedTextField) fixText).setValue(doubel);
            break;
        case MissingValueHandling2ColSetting.TYPE_INT:
            fixText = new JFormattedTextField();
            ((JFormattedTextField) fixText).setColumns(11);
            Integer integer;
            if (fixCell == null) {
                integer = 0;
            } else {
                integer = ((IntValue) fixCell).getIntValue();
            }
            ((JFormattedTextField) fixText).setValue(integer);
            break;
        case MissingValueHandling2ColSetting.TYPE_STRING:
            final ArrayList<DataCell> vals = new ArrayList<DataCell>();
            if (specs != null) {
                for (DataColumnSpec spec : specs) {
                    if (spec != null && spec.getDomain().hasValues()) {
                        vals.addAll(spec.getDomain().getValues());
                    }
                }
            }
            DefaultComboBoxModel model = new DefaultComboBoxModel(vals.toArray(new DataCell[0]));
            fixText = new JComboBox(model);
            ((JComboBox) fixText).setPrototypeDisplayValue("#########");
            ((JComboBox) fixText).setEditable(true);
            ((JComboBox) fixText).setRenderer(new DefaultListCellRenderer() {

                /**
                 * Overridden to set tooltip text properly.
                 *
                 * @see DefaultListCellRenderer#getListCellRendererComponent(JList, Object, int, boolean, boolean)
                 */
                @Override
                public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
                    Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    if (c instanceof JComponent) {
                        ((JComponent) c).setToolTipText(value.toString());
                    }
                    return c;
                }
            });
            String string;
            if (fixCell == null) {
                string = "";
            } else {
                string = ((StringValue) fixCell).getStringValue();
            }
            model.setSelectedItem(string);
            break;
        default:
            throw new InternalError("No such type");
    }
    return fixText;
}
Also used : JComboBox(javax.swing.JComboBox) JComponent(javax.swing.JComponent) JFormattedTextField(javax.swing.JFormattedTextField) ArrayList(java.util.ArrayList) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) DataCell(org.knime.core.data.DataCell) JComponent(javax.swing.JComponent) Component(java.awt.Component) JList(javax.swing.JList)

Example 84 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class AutoBinner method execute.

/**
 * Determine bins.
 *
 * @param data the input data
 * @param exec the execution context
 * @return the operation with the discretisation information
 * @throws Exception
 */
public PMMLPreprocDiscretize execute(final BufferedDataTable data, final ExecutionContext exec) throws Exception {
    // Auto configuration when target is not set
    final DataTableSpec spec = data.getDataTableSpec();
    if (null == m_settings.getTargetColumn() || m_settings.getIncludeAll()) {
        addAllNumericCols(spec);
    }
    // determine intervals
    if (m_settings.getMethod().equals(Method.fixedNumber)) {
        BufferedDataTable inData = calcDomainBoundsIfNeccessary(data, exec.createSubExecutionContext(0.9), Arrays.asList(m_settings.getTargetColumn()));
        init(inData.getDataTableSpec());
        Map<String, double[]> edgesMap = new HashMap<String, double[]>();
        for (String target : m_settings.getTargetColumn()) {
            DataTableSpec inSpec = inData.getDataTableSpec();
            DataColumnSpec targetCol = inSpec.getColumnSpec(target);
            // bounds of the domain
            double min = ((DoubleValue) targetCol.getDomain().getLowerBound()).getDoubleValue();
            double max = ((DoubleValue) targetCol.getDomain().getUpperBound()).getDoubleValue();
            // the edges of the bins
            double[] edges = new double[m_settings.getBinCount() + 1];
            edges[0] = min;
            edges[edges.length - 1] = max;
            for (int i = 1; i < edges.length - 1; i++) {
                edges[i] = min + i / (double) m_settings.getBinCount() * (max - min);
            }
            edgesMap.put(target, edges);
        }
        return createDisretizeOp(edgesMap);
    } else if (m_settings.getMethod().equals(Method.sampleQuantiles)) {
        init(spec);
        Map<String, double[]> edgesMap = new LinkedHashMap<String, double[]>();
        final int colCount = m_settings.getTargetColumn().length;
        // contains all numeric columns if include all is set!
        for (String target : m_settings.getTargetColumn()) {
            exec.setMessage("Calculating quantiles (column \"" + target + "\")");
            ExecutionContext colSortContext = exec.createSubExecutionContext(0.7 / colCount);
            ExecutionContext colCalcContext = exec.createSubExecutionContext(0.3 / colCount);
            ColumnRearranger singleRearranger = new ColumnRearranger(spec);
            singleRearranger.keepOnly(target);
            BufferedDataTable singleColSorted = colSortContext.createColumnRearrangeTable(data, singleRearranger, colSortContext);
            SortedTable sorted = new SortedTable(singleColSorted, Collections.singletonList(target), new boolean[] { true }, colSortContext);
            colSortContext.setProgress(1.0);
            double[] edges = createEdgesFromQuantiles(sorted.getBufferedDataTable(), colCalcContext, m_settings.getSampleQuantiles());
            colCalcContext.setProgress(1.0);
            exec.clearTable(singleColSorted);
            edgesMap.put(target, edges);
        }
        return createDisretizeOp(edgesMap);
    } else {
        throw new IllegalStateException("Unknown binning method.");
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DoubleValue(org.knime.core.data.DoubleValue) SortedTable(org.knime.base.data.sort.SortedTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 85 with DoubleValue

use of org.knime.core.data.DoubleValue in project knime-core by knime.

the class AutoBinner method execute.

/**
 * Determine bins.
 *
 * @param data the input data
 * @param exec the execution context
 * @return the operation with the discretisation information
 * @throws Exception ...
 */
public PMMLPreprocDiscretize execute(final BufferedDataTable data, final ExecutionContext exec) throws Exception {
    // Auto configuration when target is not set
    final DataTableSpec spec = data.getDataTableSpec();
    if (null == m_settings.getTargetColumn() || m_settings.getIncludeAll()) {
        addAllNumericCols(spec);
    }
    // determine intervals
    if (m_settings.getMethod().equals(Method.fixedNumber)) {
        if (m_settings.getEqualityMethod().equals(EqualityMethod.width)) {
            BufferedDataTable inData = calcDomainBoundsIfNeccessary(data, exec.createSubExecutionContext(0.9), Arrays.asList(m_settings.getTargetColumn()));
            init(inData.getDataTableSpec());
            Map<String, double[]> edgesMap = new HashMap<String, double[]>();
            for (String target : m_settings.getTargetColumn()) {
                DataTableSpec inSpec = inData.getDataTableSpec();
                DataColumnSpec targetCol = inSpec.getColumnSpec(target);
                // bounds of the domain
                double min = ((DoubleValue) targetCol.getDomain().getLowerBound()).getDoubleValue();
                double max = ((DoubleValue) targetCol.getDomain().getUpperBound()).getDoubleValue();
                // the edges of the bins
                double[] edges = new double[m_settings.getBinCount() + 1];
                edges[0] = min;
                edges[edges.length - 1] = max;
                for (int i = 1; i < edges.length - 1; i++) {
                    edges[i] = min + i / (double) m_settings.getBinCount() * (max - min);
                }
                if (m_settings.getIntegerBounds()) {
                    edges = toIntegerBounds(edges);
                }
                edgesMap.put(target, edges);
            }
            return createDisretizeOp(edgesMap);
        } else {
            // EqualityMethod.equalCount
            Map<String, double[]> edgesMap = new HashMap<String, double[]>();
            for (String target : m_settings.getTargetColumn()) {
                int colIndex = data.getDataTableSpec().findColumnIndex(target);
                List<Double> values = new ArrayList<Double>();
                for (DataRow row : data) {
                    if (!row.getCell(colIndex).isMissing()) {
                        values.add(((DoubleValue) row.getCell(colIndex)).getDoubleValue());
                    }
                }
                edgesMap.put(target, findEdgesForEqualCount(values, m_settings.getBinCount()));
            }
            return createDisretizeOp(edgesMap);
        }
    } else if (m_settings.getMethod().equals(Method.sampleQuantiles)) {
        init(spec);
        Map<String, double[]> edgesMap = new LinkedHashMap<String, double[]>();
        final int colCount = m_settings.getTargetColumn().length;
        // contains all numeric columns if include all is set!
        for (String target : m_settings.getTargetColumn()) {
            exec.setMessage("Calculating quantiles (column \"" + target + "\")");
            ExecutionContext colSortContext = exec.createSubExecutionContext(0.7 / colCount);
            ExecutionContext colCalcContext = exec.createSubExecutionContext(0.3 / colCount);
            ColumnRearranger singleRearranger = new ColumnRearranger(spec);
            singleRearranger.keepOnly(target);
            BufferedDataTable singleColSorted = colSortContext.createColumnRearrangeTable(data, singleRearranger, colSortContext);
            SortedTable sorted = new SortedTable(singleColSorted, Collections.singletonList(target), new boolean[] { true }, colSortContext);
            colSortContext.setProgress(1.0);
            double[] edges = createEdgesFromQuantiles(sorted.getBufferedDataTable(), colCalcContext, m_settings.getSampleQuantiles());
            colCalcContext.setProgress(1.0);
            exec.clearTable(singleColSorted);
            if (m_settings.getIntegerBounds()) {
                edges = toIntegerBounds(edges);
            }
            edgesMap.put(target, edges);
        }
        return createDisretizeOp(edgesMap);
    } else {
        throw new IllegalStateException("Unknown binning method.");
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DoubleValue(org.knime.core.data.DoubleValue) SortedTable(org.knime.base.data.sort.SortedTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DoubleValue (org.knime.core.data.DoubleValue)154 DataCell (org.knime.core.data.DataCell)103 DataRow (org.knime.core.data.DataRow)71 DataColumnSpec (org.knime.core.data.DataColumnSpec)38 DataTableSpec (org.knime.core.data.DataTableSpec)38 DoubleCell (org.knime.core.data.def.DoubleCell)32 ArrayList (java.util.ArrayList)26 BufferedDataTable (org.knime.core.node.BufferedDataTable)26 DataType (org.knime.core.data.DataType)23 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)21 LinkedHashMap (java.util.LinkedHashMap)18 IntValue (org.knime.core.data.IntValue)15 HashMap (java.util.HashMap)14 RowIterator (org.knime.core.data.RowIterator)14 RowKey (org.knime.core.data.RowKey)13 DefaultRow (org.knime.core.data.def.DefaultRow)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 LongValue (org.knime.core.data.LongValue)10 StringValue (org.knime.core.data.StringValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10