Search in sources :

Example 66 with DataType

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

the class OptionsPanel method loadSettingsFrom.

/**
 * Load settings from config <b>cfg</b>
 *
 * @param inSpec
 * @param cfg
 * @throws NotConfigurableException
 */
public void loadSettingsFrom(final DataTableSpec inSpec, final TreeEnsembleLearnerConfiguration cfg) throws NotConfigurableException {
    // disabled automatic propagation of table specs
    m_lastTableSpec = null;
    int nrNominalCols = 0;
    int nrNumericCols = 0;
    for (DataColumnSpec col : inSpec) {
        DataType type = col.getType();
        if (type.isCompatible(NominalValue.class)) {
            nrNominalCols += 1;
        } else if (type.isCompatible(DoubleValue.class)) {
            nrNumericCols += 1;
        }
    }
    boolean hasOrdinaryColumnsInInput = nrNominalCols > 1 || nrNumericCols > 0;
    boolean hasFPColumnInInput = inSpec.containsCompatibleType(BitVectorValue.class) || inSpec.containsCompatibleType(ByteVectorValue.class);
    m_targetColumnBox.update(inSpec, cfg.getTargetColumn());
    DataTableSpec attSpec = removeColumn(inSpec, m_targetColumnBox.getSelectedColumn());
    String fpColumn = cfg.getFingerprintColumn();
    boolean includeAll = cfg.isIncludeAllColumns();
    String[] includeCols = cfg.getIncludeColumns();
    if (includeCols == null) {
        includeCols = new String[0];
    }
    m_useOrdinaryColumnsRadio.setEnabled(true);
    m_useFingerprintColumnRadio.setEnabled(true);
    // m_useByteVectorColumnRadio.setEnabled(true);
    // default, fix later
    m_useOrdinaryColumnsRadio.doClick();
    m_includeColumnsFilterPanel.setKeepAllSelected(includeAll);
    if (hasOrdinaryColumnsInInput) {
        m_includeColumnsFilterPanel.update(attSpec, false, includeCols);
        m_includeColumnsFilterPanel.setKeepAllSelected(includeAll);
    } else {
        m_useOrdinaryColumnsRadio.setEnabled(false);
        m_useFingerprintColumnRadio.doClick();
        m_includeColumnsFilterPanel.update(NO_VALID_INPUT_SPEC, true);
    }
    if (hasFPColumnInInput) {
        m_fingerprintColumnBox.update(inSpec, fpColumn);
    } else {
        m_useOrdinaryColumnsRadio.doClick();
        m_fingerprintColumnBox.update(NO_VALID_INPUT_SPEC, "");
        m_useFingerprintColumnRadio.setEnabled(false);
        fpColumn = null;
    }
    if (fpColumn != null || !hasOrdinaryColumnsInInput) {
        m_useFingerprintColumnRadio.doClick();
    } else {
        m_useOrdinaryColumnsRadio.doClick();
    }
    // boolean ignoreColsNoDomain = cfg.isIgnoreColumnsWithoutDomain();
    // m_ignoreColumnsWithoutDomainChecker.setSelected(ignoreColsNoDomain);
    int hiliteCount = cfg.getNrHilitePatterns();
    if (hiliteCount > 0) {
        m_enableHiliteChecker.setSelected(true);
        m_hiliteCountSpinner.setValue(hiliteCount);
    } else {
        m_enableHiliteChecker.setSelected(false);
        m_hiliteCountSpinner.setValue(2000);
    }
    // m_saveTargetDistributionInNodesChecker.setSelected(cfg.isSaveTargetDistributionInNodes());
    // Tree Options
    // m_splitCriterionsBox.setSelectedItem(cfg.getSplitCriterion());
    // m_useAverageSplitPointsChecker.setSelected(cfg.isUseAverageSplitPoints());
    int maxLevel = cfg.getMaxLevels();
    if ((maxLevel != TreeEnsembleLearnerConfiguration.MAX_LEVEL_INFINITE) != m_maxLevelChecker.isSelected()) {
        m_maxLevelChecker.doClick();
    }
    if (maxLevel == TreeEnsembleLearnerConfiguration.MAX_LEVEL_INFINITE) {
        m_maxLevelSpinner.setValue(10);
    } else {
        m_maxLevelSpinner.setValue(maxLevel);
    }
    int minNodeSize = cfg.getMinNodeSize();
    if ((minNodeSize != TreeEnsembleLearnerConfiguration.MIN_NODE_SIZE_UNDEFINED) != m_minNodeSizeChecker.isSelected()) {
        m_minNodeSizeChecker.doClick();
    }
    if (minNodeSize == TreeEnsembleLearnerConfiguration.MIN_NODE_SIZE_UNDEFINED) {
        m_minNodeSizeSpinner.setValue(10);
    } else {
        m_minNodeSizeSpinner.setValue(minNodeSize);
    }
    int minChildNodeSize = cfg.getMinChildSize();
    if ((minChildNodeSize != TreeEnsembleLearnerConfiguration.MIN_CHILD_SIZE_UNDEFINED) != m_minChildNodeSizeChecker.isSelected()) {
        m_minChildNodeSizeChecker.doClick();
    }
    if (minChildNodeSize == TreeEnsembleLearnerConfiguration.MIN_CHILD_SIZE_UNDEFINED) {
        m_minChildNodeSizeSpinner.setValue(5);
    } else {
        m_minChildNodeSizeSpinner.setValue(minChildNodeSize);
    }
    // String rootCol = cfg.getHardCodedRootColumn();
    // if (hasOrdinaryColumnsInInput) {
    // DataTableSpec attrSpec = getCurrentAttributeSpec();
    // m_hardCodedRootColumnChecker.setEnabled(true);
    // // select root attribute (may be null!), then sync states
    // m_hardCodedRootColumnBox.update(attrSpec, rootCol);
    // String nowSelected = m_hardCodedRootColumnBox.getSelectedColumn();
    // boolean isRootValid = nowSelected.equals(rootCol);
    // if (isRootValid != m_hardCodedRootColumnChecker.isSelected()) {
    // m_hardCodedRootColumnChecker.doClick();
    // }
    // } else {
    // // no appropriate root attribute
    // // --> clear checkbox, disable selection
    // m_hardCodedRootColumnBox.update(NO_VALID_INPUT_SPEC, "");
    // if (m_hardCodedRootColumnChecker.isSelected()) {
    // m_hardCodedRootColumnChecker.doClick();
    // }
    // m_hardCodedRootColumnChecker.setEnabled(false);
    // }
    // Forest Options
    // double colFrac = cfg.getColumnFractionLinearValue();
    // int colAbsolute = cfg.getColumnAbsoluteValue();
    // boolean useDifferentAttributesAtEachNode = cfg.isUseDifferentAttributesAtEachNode();
    // ColumnSamplingMode columnFraction = cfg.getColumnSamplingMode();
    // switch (columnFraction) {
    // case None:
    // m_columnFractionNoneButton.doClick();
    // useDifferentAttributesAtEachNode = false;
    // colFrac = 1.0;
    // break;
    // case Linear:
    // m_columnFractionLinearButton.doClick();
    // break;
    // case Absolute:
    // m_columnFractionAbsoluteButton.doClick();
    // break;
    // case SquareRoot:
    // m_columnFractionSqrtButton.doClick();
    // colFrac = 1.0;
    // break;
    // }
    // m_columnFractionLinearTreeSpinner.setValue(colFrac);
    // m_columnFractionAbsoluteTreeSpinner.setValue(colAbsolute);
    // if (useDifferentAttributesAtEachNode) {
    // m_columnUseDifferentSetOfAttributesForNodes.doClick();
    // } else {
    // m_columnUseSameSetOfAttributesForNodes.doClick();
    // }
    // 
    // 
    // Long seed = cfg.getSeed();
    // if (m_seedChecker.isSelected() != (seed != null)) {
    // m_seedChecker.doClick();
    // }
    // m_seedTextField.setText(Long.toString(seed != null ? seed : System.currentTimeMillis()));
    // Other
    m_lastTableSpec = inSpec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Example 67 with DataType

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

the class BlobsInSetCellWorkflowTest method createBDT.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable createBDT(final ExecutionContext exec) {
    DataType t = ListCell.getCollectionType(DataType.getType(DataCell.class));
    BufferedDataContainer c = exec.createDataContainer(new DataTableSpec(new DataColumnSpecCreator("Sequence", t).createSpec()));
    for (int i = 0; i < ROW_COUNT; i++) {
        String s = "someName_" + i;
        // every other a ordinary string cell
        Collection<DataCell> cells = new ArrayList<DataCell>();
        for (int j = 0; j < LIST_SIZE * 2; j++) {
            String val = "Row_" + i + "; Cell index " + j;
            if (j % 2 == 0) {
                cells.add(new LargeBlobCell(val, LargeBlobCell.SIZE_OF_CELL));
            } else {
                cells.add(new StringCell(val));
            }
        }
        ListCell cell = CollectionCellFactory.createListCell(cells);
        c.addRowToTable(new DefaultRow(s, cell));
    }
    c.close();
    return c.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ListCell(org.knime.core.data.collection.ListCell) ArrayList(java.util.ArrayList) LargeBlobCell(org.knime.testing.data.blob.LargeBlobCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 68 with DataType

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

the class RegressionTreeModel method createNominalNumericPredictorRecord.

private PredictorRecord createNominalNumericPredictorRecord(final DataRow filterRow, final DataTableSpec trainSpec) {
    final int nrCols = trainSpec.getNumColumns();
    Map<String, Object> valueMap = new LinkedHashMap<String, Object>((int) (nrCols / 0.75 + 1.0));
    for (int i = 0; i < nrCols; i++) {
        DataColumnSpec col = trainSpec.getColumnSpec(i);
        String colName = col.getName();
        DataType colType = col.getType();
        DataCell cell = filterRow.getCell(i);
        if (cell.isMissing()) {
            valueMap.put(colName, PredictorRecord.NULL);
        } else if (colType.isCompatible(NominalValue.class)) {
            valueMap.put(colName, cell.toString());
        } else if (colType.isCompatible(DoubleValue.class)) {
            valueMap.put(colName, ((DoubleValue) cell).getDoubleValue());
        } else {
            throw new IllegalStateException("Expected nominal or numeric column type for column \"" + colName + "\" but got \"" + colType + "\"");
        }
    }
    return new PredictorRecord(valueMap);
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) NominalValue(org.knime.core.data.NominalValue) PredictorRecord(org.knime.base.node.mine.treeensemble.data.PredictorRecord) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) LinkedHashMap(java.util.LinkedHashMap)

Example 69 with DataType

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

the class TreeEnsembleLearnerConfiguration method filterLearnColumns.

/**
 * @param spec
 * @return ColumnRearranger that filters out all columns not part of the learning columns.
 * @throws InvalidSettingsException
 */
public FilterLearnColumnRearranger filterLearnColumns(final DataTableSpec spec) throws InvalidSettingsException {
    // (ColumnRearranger is a final class in v2.5)
    if (m_targetColumn == null) {
        throw new InvalidSettingsException("Target column not set");
    }
    DataColumnSpec targetCol = spec.getColumnSpec(m_targetColumn);
    if (targetCol == null || !targetCol.getType().isCompatible(getRequiredTargetClass())) {
        throw new InvalidSettingsException("Target column \"" + m_targetColumn + "\" does not exist or is not of the " + "correct type");
    }
    List<String> noDomainColumns = new ArrayList<String>();
    FilterLearnColumnRearranger rearranger = new FilterLearnColumnRearranger(spec);
    if (m_fingerprintColumn == null) {
        // use ordinary data
        Set<String> incl = m_includeAllColumns ? null : new HashSet<String>(Arrays.asList(m_includeColumns));
        for (DataColumnSpec col : spec) {
            String colName = col.getName();
            if (colName.equals(m_targetColumn)) {
                continue;
            }
            DataType type = col.getType();
            boolean ignoreColumn = false;
            boolean isAppropriateType = type.isCompatible(DoubleValue.class) || type.isCompatible(NominalValue.class);
            if (m_includeAllColumns) {
                if (isAppropriateType) {
                    if (shouldIgnoreLearnColumn(col)) {
                        ignoreColumn = true;
                        noDomainColumns.add(colName);
                    } else {
                    // accept column
                    }
                } else {
                    ignoreColumn = true;
                }
            } else {
                if (incl.remove(colName)) {
                    // accept unless type mismatch
                    if (!isAppropriateType) {
                        throw new InvalidSettingsException("Attribute column \"" + colName + "\" is " + "not of the expected type (must be " + "numeric or nominal).");
                    } else if (shouldIgnoreLearnColumn(col)) {
                        ignoreColumn = true;
                        noDomainColumns.add(colName);
                    } else {
                    // accept
                    }
                } else {
                    ignoreColumn = true;
                }
            }
            if (ignoreColumn) {
                rearranger.remove(colName);
            }
        }
        if (rearranger.getColumnCount() <= 1) {
            StringBuilder b = new StringBuilder("Input table has no valid " + "learning columns (need one additional numeric or " + "nominal column).");
            if (!noDomainColumns.isEmpty()) {
                b.append(" ").append(noDomainColumns.size());
                b.append(" column(s) were ignored due to missing domain ");
                b.append("information -- execute predecessor and/or ");
                b.append(" use Domain Calculator node.");
                throw new InvalidSettingsException(b.toString());
            }
        }
        if (!m_includeAllColumns && !incl.isEmpty()) {
            StringBuilder missings = new StringBuilder();
            int i = 0;
            for (Iterator<String> it = incl.iterator(); it.hasNext() && i < 4; i++) {
                String s = it.next();
                missings.append(i > 0 ? ", " : "").append(s);
                it.remove();
            }
            if (!incl.isEmpty()) {
                missings.append(",...").append(incl.size()).append(" more");
            }
            throw new InvalidSettingsException("Some selected attributes " + "are not present in the input table: " + missings);
        }
    } else {
        // use fingerprint data
        DataColumnSpec fpCol = spec.getColumnSpec(m_fingerprintColumn);
        if (fpCol == null || !(fpCol.getType().isCompatible(BitVectorValue.class) || fpCol.getType().isCompatible(ByteVectorValue.class))) {
            throw new InvalidSettingsException("Fingerprint columnn \"" + m_fingerprintColumn + "\" does not exist or is not " + "of correct type.");
        }
        rearranger.keepOnly(m_targetColumn, m_fingerprintColumn);
    }
    rearranger.move(m_targetColumn, rearranger.getColumnCount());
    String warn = null;
    if (!noDomainColumns.isEmpty()) {
        StringBuilder b = new StringBuilder();
        b.append(noDomainColumns.size());
        b.append(" column(s) were ignored due to missing domain");
        b.append(" information: [");
        int index = 0;
        for (String s : noDomainColumns) {
            if (index > 3) {
                b.append(", ...");
                break;
            }
            if (index > 0) {
                b.append(", ");
            }
            b.append("\"").append(s).append("\"");
            index++;
        }
        b.append("] -- change the node configuration or use a");
        b.append(" Domain Calculator node to fix it");
        warn = b.toString();
    }
    rearranger.setWarning(warn);
    return rearranger;
}
Also used : NominalValue(org.knime.core.data.NominalValue) ArrayList(java.util.ArrayList) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType)

Example 70 with DataType

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

the class TreeEnsembleModelPortObjectSpec method calculateFilterIndices.

public int[] calculateFilterIndices(final DataTableSpec testTableInput) throws InvalidSettingsException {
    DataTableSpec learnSpec = getLearnTableSpec();
    // check existence and types of columns, create reordering
    int[] result = new int[learnSpec.getNumColumns()];
    for (int i = 0; i < learnSpec.getNumColumns(); i++) {
        DataColumnSpec learnCol = learnSpec.getColumnSpec(i);
        final String colName = learnCol.getName();
        int dataColIndex = testTableInput.findColumnIndex(colName);
        if (dataColIndex < 0) {
            throw new InvalidSettingsException("Required data column \"" + colName + "\" does not exist in table");
        }
        DataColumnSpec dataCol = testTableInput.getColumnSpec(dataColIndex);
        // expected type
        DataType eType = learnCol.getType();
        // actual type
        DataType aType = dataCol.getType();
        String errorType = null;
        if (eType.isCompatible(NominalValue.class) && !aType.isCompatible(NominalValue.class)) {
            errorType = "nominal";
        }
        if (eType.isCompatible(DoubleValue.class) && !aType.isCompatible(DoubleValue.class)) {
            errorType = "numeric";
        }
        if (eType.isCompatible(BitVectorValue.class) && !aType.isCompatible(BitVectorValue.class)) {
            errorType = "fingerprint/bitvector";
        }
        if (eType.isCompatible(ByteVectorValue.class) && !aType.isCompatible(ByteVectorValue.class)) {
            errorType = "fingerprint/bytevector";
        }
        if (errorType != null) {
            throw new InvalidSettingsException("Column \"" + colName + "\" does exist in the data but" + "is not of the expected " + errorType + " type");
        }
        result[i] = dataColIndex;
    }
    return result;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) DataType(org.knime.core.data.DataType) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18