Search in sources :

Example 56 with IntCell

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

the class MissingValuePanel method getSettings.

/**
 * Get the settings currently entered in the dialog.
 *
 * @return the current settings
 */
public ColSetting getSettings() {
    int method;
    if (m_nothingButton.isSelected()) {
        method = ColSetting.METHOD_NO_HANDLING;
    } else if (m_removeButton.isSelected()) {
        method = ColSetting.METHOD_IGNORE_ROWS;
    } else if (m_fixButton != null && m_fixButton.isSelected()) {
        method = ColSetting.METHOD_FIX_VAL;
        DataCell cell;
        switch(m_setting.getType()) {
            case ColSetting.TYPE_INT:
                Object value = ((JFormattedTextField) m_fixText).getValue();
                cell = new IntCell(((Number) value).intValue());
                break;
            case ColSetting.TYPE_DOUBLE:
                value = ((JFormattedTextField) m_fixText).getValue();
                cell = new DoubleCell(((Number) value).doubleValue());
                break;
            case ColSetting.TYPE_STRING:
                value = ((JComboBox) m_fixText).getEditor().getItem();
                cell = new StringCell(value.toString());
                break;
            default:
                throw new RuntimeException("You shouldn't have come here.");
        }
        m_setting.setFixCell(cell);
    } else if (m_maxButton != null && m_maxButton.isSelected()) {
        method = ColSetting.METHOD_MAX;
    } else if (m_minButton != null && m_minButton.isSelected()) {
        method = ColSetting.METHOD_MIN;
    } else if (m_meanButton != null && m_meanButton.isSelected()) {
        method = ColSetting.METHOD_MEAN;
    } else if (m_mostFrequentButton != null && m_mostFrequentButton.isSelected()) {
        method = ColSetting.METHOD_MOST_FREQUENT;
    } else {
        assert false : "One button must be selected.";
        method = ColSetting.METHOD_NO_HANDLING;
    }
    m_setting.setMethod(method);
    return m_setting;
}
Also used : JComboBox(javax.swing.JComboBox) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) JFormattedTextField(javax.swing.JFormattedTextField) DataCell(org.knime.core.data.DataCell) IntCell(org.knime.core.data.def.IntCell)

Example 57 with IntCell

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

the class BatchExecutor method setNodeOptions.

private static void setNodeOptions(final Collection<Option> options, final WorkflowManager wfm) throws InvalidSettingsException, IllegalOptionException {
    for (Option o : options) {
        int[] idPath = o.m_nodeIDs;
        NodeID subID = new NodeID(wfm.getID(), idPath[0]);
        NodeContainer cont = null;
        try {
            cont = wfm.getNodeContainer(subID);
            for (int i = 1; i < idPath.length; i++) {
                if (cont instanceof WorkflowManager) {
                    WorkflowManager subWM = (WorkflowManager) cont;
                    subID = new NodeID(subID, idPath[i]);
                    cont = subWM.getNodeContainer(subID);
                } else {
                    cont = null;
                }
            }
        } catch (IllegalArgumentException ex) {
            // throw by getNodeContainer if no node with the id exists
            cont = null;
        }
        if (cont == null) {
            LOGGER.warn("No node with id " + Arrays.toString(idPath) + " found.");
        } else {
            WorkflowManager parent = cont.getParent();
            NodeSettings settings = new NodeSettings("something");
            parent.saveNodeSettings(cont.getID(), settings);
            NodeSettings model = settings.getNodeSettings(Node.CFG_MODEL);
            String[] splitName = o.m_name.split("/");
            String name = splitName[splitName.length - 1];
            String[] pathElements = new String[splitName.length - 1];
            System.arraycopy(splitName, 0, pathElements, 0, pathElements.length);
            for (String s : pathElements) {
                model = model.getNodeSettings(s);
            }
            if ("int".equals(o.m_type)) {
                model.addInt(name, Integer.parseInt(o.m_value));
            } else if ("long".equals(o.m_type)) {
                model.addLong(name, Long.parseLong(o.m_value));
            } else if ("short".equals(o.m_type)) {
                model.addShort(name, Short.parseShort(o.m_value));
            } else if ("byte".equals(o.m_type)) {
                model.addByte(name, Byte.parseByte(o.m_value));
            } else if ("boolean".equals(o.m_type)) {
                model.addBoolean(name, Boolean.parseBoolean(o.m_value));
            } else if ("char".equals(o.m_type)) {
                model.addChar(name, o.m_value.charAt(0));
            } else if ("float".equals(o.m_type) || ("double".equals(o.m_type))) {
                model.addDouble(name, Double.parseDouble(o.m_value));
            } else if ("String".equals(o.m_type)) {
                model.addString(name, o.m_value);
            } else if ("StringCell".equals(o.m_type)) {
                model.addDataCell(name, new StringCell(o.m_value));
            } else if ("DoubleCell".equals(o.m_type)) {
                double d = Double.parseDouble(o.m_value);
                model.addDataCell(name, new DoubleCell(d));
            } else if ("IntCell".equals(o.m_type)) {
                int i = Integer.parseInt(o.m_value);
                model.addDataCell(name, new IntCell(i));
            } else if ("LongCell".equals(o.m_type)) {
                long i = Long.parseLong(o.m_value);
                model.addDataCell(name, new LongCell(i));
            } else {
                throw new IllegalOptionException("Unknown option type for " + o.m_name + ": " + o.m_type);
            }
            parent.loadNodeSettings(cont.getID(), settings);
        }
    }
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) NodeSettings(org.knime.core.node.NodeSettings) LongCell(org.knime.core.data.def.LongCell) StringCell(org.knime.core.data.def.StringCell)

Example 58 with IntCell

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

the class BootstrapNodeModel method sampleChunk.

/**
 * Puts samples, for the chunk of rows, in the bootstrap container and unused ones in the holdout container.
 *
 * @param iterator Iterator over a set of rows. Must at least have chunkSize rows left
 * @param chunkSize The number of rows that will be processed
 * @param numberOfSamples The number of samples that should be generated
 * @param bootstrap The container for the bootstrap samples
 * @param holdout The container for the unused samples
 * @param random A random for selection of rows
 * @param progress A progress object that will be incremented for each processed row
 * @throws CanceledExecutionException If execution has been canceled
 */
private void sampleChunk(final CloseableRowIterator iterator, final int chunkSize, final int numberOfSamples, final BufferedDataContainer bootstrap, final BufferedDataContainer holdout, final Random random, final Progress progress) throws CanceledExecutionException {
    // Array holding the amount of copies that will go into the bootstrap samples for each row
    int[] sampled = new int[chunkSize];
    // Init all with 0
    for (int i = 0; i < sampled.length; i++) {
        sampled[i] = 0;
    }
    // Increment randomly until amount of samples has been selected
    for (int i = 0; i < numberOfSamples; i++) {
        sampled[random.nextInt(sampled.length)]++;
    }
    // Iterate through as many rows as this chunk is big
    for (int i = 0; i < chunkSize; i++) {
        DataRow row = iterator.next();
        // Check if row will go into the bootstrap or holdout table
        if (sampled[i] == 0) {
            // Add the unchanged row to the holdout table
            holdout.addRowToTable(row);
        } else {
            // Add the row to the bootstrap table as many times as it has been selected
            for (int j = 0; j < sampled[i]; j++) {
                DataRow newRow;
                if (m_configuration.getAppendOccurrences() || m_configuration.getAppendOriginalRowId()) {
                    int appendedColumns = 0;
                    if (m_configuration.getAppendOccurrences()) {
                        appendedColumns++;
                    }
                    if (m_configuration.getAppendOriginalRowId()) {
                        appendedColumns++;
                    }
                    // Add occurrences column to the row
                    DataCell[] cells = new DataCell[row.getNumCells() + appendedColumns];
                    int k;
                    for (k = 0; k < row.getNumCells(); k++) {
                        cells[k] = row.getCell(k);
                    }
                    if (m_configuration.getAppendOccurrences()) {
                        cells[k++] = new IntCell(sampled[i]);
                    }
                    if (m_configuration.getAppendOriginalRowId()) {
                        cells[k++] = new StringCell(row.getKey().toString());
                    }
                    newRow = new DefaultRow(row.getKey() + m_configuration.getRowIdSeparator() + j, cells);
                } else {
                    // Only change the row ID of the original row
                    newRow = new DefaultRow(row.getKey() + m_configuration.getRowIdSeparator() + j, row);
                }
                // Add row to the bootstrap table
                bootstrap.addRowToTable(newRow);
            }
        }
        // Update progress
        progress.increment();
    }
}
Also used : StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell)

Example 59 with IntCell

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

the class AverageInterpolationStatisticMB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(m_colIdx);
    if (cell.isMissing()) {
        m_numMissing++;
    } else {
        DataCell res;
        if (m_previous.isMissing()) {
            res = cell;
        } else {
            if (m_isDateColumn) {
                DateAndTimeValue val = (DateAndTimeValue) cell;
                DateAndTimeValue prevVal = (DateAndTimeValue) m_previous;
                boolean hasDate = val.hasDate() | prevVal.hasDate();
                boolean hasTime = val.hasTime() | prevVal.hasTime();
                boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                long prev = prevVal.getUTCTimeInMillis();
                long next = val.getUTCTimeInMillis();
                long lin = Math.round((prev + next) / 2);
                res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
            } else {
                DoubleValue val = (DoubleValue) cell;
                double prev = ((DoubleValue) m_previous).getDoubleValue();
                double next = val.getDoubleValue();
                double lin = (prev + next) / 2;
                if (m_previous instanceof IntValue) {
                    // get an int, create an int
                    res = new IntCell((int) Math.round(lin));
                } else if (m_previous instanceof LongValue) {
                    // get an long, create an long
                    res = new LongCell(Math.round(lin));
                } else {
                    res = new DoubleCell(lin);
                }
            }
        }
        for (int i = 0; i < m_numMissing; i++) {
            m_values.add(res);
        }
        m_previous = cell;
        m_numMissing = 0;
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Example 60 with IntCell

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

the class SubsetMatcherNodeModel method createRunnable.

private Runnable createRunnable(final ExecutionMonitor exec, final long noOfSets, final DataCell setIDCell, final CollectionDataValue setCell, final boolean appendSetCol, final Comparator<DataCell> comparator, final SubsetMatcher[] sortedMatcher, final int maxMismatches) {
    return new Runnable() {

        @Override
        public void run() {
            try {
                // check cancel prior updating the global counter!!!
                exec.checkCanceled();
                final long transCounter = m_setCounter.incrementAndGet();
                exec.setMessage(setIDCell.toString() + " (" + transCounter + " of " + noOfSets + ")");
                if (setCell.size() < 1) {
                    // skip empty collections
                    exec.setProgress(transCounter / (double) noOfSets);
                    m_skipCounter.incrementAndGet();
                    return;
                }
                final DataCell[] sortedItems = collectionCell2SortedArray(setCell, comparator);
                // try to match the sorted transaction items and the sorted
                // matcher until all items or all matchers are processed
                int matcherStartIdx = 0;
                int itemIdx = 0;
                final Collection<SetMissmatches> matchingSets = new LinkedList<>();
                while (itemIdx < sortedItems.length && matcherStartIdx < sortedMatcher.length) {
                    final DataCell subItem = sortedItems[itemIdx];
                    // match the current item with all remaining matchers
                    for (int i = matcherStartIdx; i < sortedMatcher.length; i++) {
                        final SubsetMatcher matcher = sortedMatcher[i];
                        final int result = matcher.compare(subItem);
                        if (result > 0 && maxMismatches == 0) {
                            // the next item
                            break;
                        }
                        // this matcher matches the item (result == 0)
                        // or
                        // the item is bigger than the matcher
                        // since we may allow for mismatches we have to
                        // check if subsequent matchers of the matcher
                        // match the current item
                        matcher.match(sortedItems, itemIdx, matchingSets, new LinkedList<DataCell>(), new MismatchCounter(maxMismatches));
                        matcherStartIdx++;
                    }
                    // go to the next index
                    itemIdx++;
                }
                if (matchingSets.size() < 1) {
                    exec.setProgress(transCounter / (double) noOfSets);
                    m_skipCounter.incrementAndGet();
                    return;
                }
                for (final SetMissmatches matchingSet : matchingSets) {
                    exec.checkCanceled();
                    // create for each matching subset a result row
                    final List<DataCell> cells = new LinkedList<>();
                    cells.add(setIDCell);
                    if (appendSetCol) {
                        cells.add((DataCell) setCell);
                    }
                    // the subset column
                    cells.add(matchingSet.getSet());
                    if (maxMismatches > 0) {
                        // append the mismatch counter if mismatches allowed
                        cells.add(new IntCell(matchingSet.getMismatchCounter()));
                    }
                    final RowKey rowKey = RowKey.createRowKey(m_rowId.getAndIncrement());
                    final DefaultRow row = new DefaultRow(rowKey, cells);
                    synchronized (m_dc) {
                        exec.checkCanceled();
                        m_dc.addRowToTable(row);
                    }
                }
                exec.setProgress(transCounter / (double) noOfSets);
            } catch (final CanceledExecutionException e) {
            // ignore this just exit the run method
            } catch (final Exception e) {
                LOGGER.error("Exception while matching sub sets: " + e.getMessage());
            }
        }
    };
}
Also used : RowKey(org.knime.core.data.RowKey) LinkedList(java.util.LinkedList) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) IntCell(org.knime.core.data.def.IntCell) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

IntCell (org.knime.core.data.def.IntCell)109 DataCell (org.knime.core.data.DataCell)79 DoubleCell (org.knime.core.data.def.DoubleCell)67 StringCell (org.knime.core.data.def.StringCell)55 DefaultRow (org.knime.core.data.def.DefaultRow)46 DataRow (org.knime.core.data.DataRow)33 DataTableSpec (org.knime.core.data.DataTableSpec)21 RowKey (org.knime.core.data.RowKey)21 ArrayList (java.util.ArrayList)20 DataType (org.knime.core.data.DataType)20 LongCell (org.knime.core.data.def.LongCell)14 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)14 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 BufferedDataTable (org.knime.core.node.BufferedDataTable)12 Test (org.junit.Test)11 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 DataContainer (org.knime.core.data.container.DataContainer)8 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)8