Search in sources :

Example 61 with IntCell

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

the class CategoryToNumberNodeDialogPane method saveSettingsTo.

/**
 * {@inheritDoc}
 */
@Override
protected void saveSettingsTo(final NodeSettingsWO settings) throws InvalidSettingsException {
    DataColumnSpecFilterConfiguration config = CategoryToNumberNodeModel.createDCSFilterConfiguration();
    m_filterPanel.saveConfiguration(config);
    m_settings.setFilterConfiguration(config);
    m_settings.setAppendColumns(m_appendColums.isSelected());
    m_settings.setColumnSuffix(m_columnSuffix.getText());
    m_settings.setStartIndex((Integer) m_startIndex.getValue());
    m_settings.setIncrement((Integer) m_increment.getValue());
    m_settings.setMaxCategories((Integer) m_maxCategories.getValue());
    if (!m_defaultValue.getText().trim().isEmpty()) {
        int value = Integer.valueOf(m_defaultValue.getText());
        m_settings.setDefaultValue(new IntCell(value));
    } else {
        m_settings.setDefaultValue(DataType.getMissingCell());
    }
    if (!m_mapMissingTo.getText().trim().isEmpty()) {
        int value = Integer.valueOf(m_mapMissingTo.getText());
        m_settings.setMapMissingTo(new IntCell(value));
    } else {
        m_settings.setMapMissingTo(DataType.getMissingCell());
    }
    m_settings.saveSettings(settings);
}
Also used : DataColumnSpecFilterConfiguration(org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration) IntCell(org.knime.core.data.def.IntCell)

Example 62 with IntCell

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

the class TreeEnsembleRegressionPredictorCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    TreeEnsembleModelPortObject modelObject = m_predictor.getModelObject();
    TreeEnsemblePredictorConfiguration cfg = m_predictor.getConfiguration();
    final TreeEnsembleModel ensembleModel = modelObject.getEnsembleModel();
    int size = 1;
    final boolean appendConfidence = cfg.isAppendPredictionConfidence();
    final boolean appendModelCount = cfg.isAppendModelCount();
    if (appendConfidence) {
        size += 1;
    }
    if (appendModelCount) {
        size += 1;
    }
    final boolean hasOutOfBagFilter = m_predictor.hasOutOfBagFilter();
    DataCell[] result = new DataCell[size];
    DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
    PredictorRecord record = ensembleModel.createPredictorRecord(filterRow, m_learnSpec);
    if (record == null) {
        // missing value
        Arrays.fill(result, DataType.getMissingCell());
        return result;
    }
    Mean mean = new Mean();
    Variance variance = new Variance();
    final int nrModels = ensembleModel.getNrModels();
    for (int i = 0; i < nrModels; i++) {
        if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
        // ignore, row was used to train the model
        } else {
            TreeModelRegression m = ensembleModel.getTreeModelRegression(i);
            TreeNodeRegression match = m.findMatchingNode(record);
            double nodeMean = match.getMean();
            mean.increment(nodeMean);
            variance.increment(nodeMean);
        }
    }
    int nrValidModels = (int) mean.getN();
    int index = 0;
    result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(mean.getResult());
    if (appendConfidence) {
        result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(variance.getResult());
    }
    if (appendModelCount) {
        result[index++] = new IntCell(nrValidModels);
    }
    return result;
}
Also used : Mean(org.apache.commons.math.stat.descriptive.moment.Mean) TreeEnsembleModel(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel) DoubleCell(org.knime.core.data.def.DoubleCell) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble.node.predictor.TreeEnsemblePredictorConfiguration) DataRow(org.knime.core.data.DataRow) TreeNodeRegression(org.knime.base.node.mine.treeensemble.model.TreeNodeRegression) Variance(org.apache.commons.math.stat.descriptive.moment.Variance) TreeModelRegression(org.knime.base.node.mine.treeensemble.model.TreeModelRegression) IntCell(org.knime.core.data.def.IntCell) TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModelPortObject) PredictorRecord(org.knime.base.node.mine.treeensemble.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) FilterColumnRow(org.knime.base.data.filter.column.FilterColumnRow)

Example 63 with IntCell

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

the class TreeEnsembleRegressionPredictorCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    TreeEnsembleModelPortObject modelObject = m_predictor.getModelObject();
    TreeEnsemblePredictorConfiguration cfg = m_predictor.getConfiguration();
    final TreeEnsembleModel ensembleModel = modelObject.getEnsembleModel();
    int size = 1;
    final boolean appendConfidence = cfg.isAppendPredictionConfidence();
    final boolean appendModelCount = cfg.isAppendModelCount();
    if (appendConfidence) {
        size += 1;
    }
    if (appendModelCount) {
        size += 1;
    }
    final boolean hasOutOfBagFilter = m_predictor.hasOutOfBagFilter();
    DataCell[] result = new DataCell[size];
    DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
    PredictorRecord record = ensembleModel.createPredictorRecord(filterRow, m_learnSpec);
    if (record == null) {
        // missing value
        Arrays.fill(result, DataType.getMissingCell());
        return result;
    }
    Mean mean = new Mean();
    Variance variance = new Variance();
    final int nrModels = ensembleModel.getNrModels();
    for (int i = 0; i < nrModels; i++) {
        if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
        // ignore, row was used to train the model
        } else {
            TreeModelRegression m = ensembleModel.getTreeModelRegression(i);
            TreeNodeRegression match = m.findMatchingNode(record);
            double nodeMean = match.getMean();
            mean.increment(nodeMean);
            variance.increment(nodeMean);
        }
    }
    int nrValidModels = (int) mean.getN();
    int index = 0;
    result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(mean.getResult());
    if (appendConfidence) {
        result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(variance.getResult());
    }
    if (appendModelCount) {
        result[index++] = new IntCell(nrValidModels);
    }
    return result;
}
Also used : Mean(org.apache.commons.math.stat.descriptive.moment.Mean) TreeEnsembleModel(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModel) DoubleCell(org.knime.core.data.def.DoubleCell) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble2.node.predictor.TreeEnsemblePredictorConfiguration) DataRow(org.knime.core.data.DataRow) TreeNodeRegression(org.knime.base.node.mine.treeensemble2.model.TreeNodeRegression) Variance(org.apache.commons.math.stat.descriptive.moment.Variance) TreeModelRegression(org.knime.base.node.mine.treeensemble2.model.TreeModelRegression) IntCell(org.knime.core.data.def.IntCell) TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObject) PredictorRecord(org.knime.base.node.mine.treeensemble2.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) FilterColumnRow(org.knime.base.data.filter.column.FilterColumnRow)

Example 64 with IntCell

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

the class VariableToTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    DataTableSpec spec = createOutSpec();
    BufferedDataContainer cont = exec.createDataContainer(spec);
    List<Pair<String, FlowVariable.Type>> vars;
    if (m_settings.getIncludeAll()) {
        vars = getAllVariables();
    } else {
        vars = m_settings.getVariablesOfInterest();
    }
    DataCell[] specs = new DataCell[vars.size()];
    List<String> lostVariables = new ArrayList<String>();
    for (int i = 0; i < vars.size(); i++) {
        Pair<String, FlowVariable.Type> c = vars.get(i);
        String name = c.getFirst();
        // fallback
        DataCell cell = DataType.getMissingCell();
        switch(c.getSecond()) {
            case DOUBLE:
                try {
                    double dValue = peekFlowVariableDouble(c.getFirst());
                    cell = new DoubleCell(dValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Double)");
                }
                break;
            case INTEGER:
                try {
                    int iValue = peekFlowVariableInt(c.getFirst());
                    cell = new IntCell(iValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Integer)");
                }
                break;
            case STRING:
                try {
                    String sValue = peekFlowVariableString(c.getFirst());
                    sValue = sValue == null ? "" : sValue;
                    cell = new StringCell(sValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (String)");
                }
                break;
        }
        specs[i] = cell;
    }
    cont.addRowToTable(new DefaultRow("values", specs));
    cont.close();
    return new BufferedDataTable[] { cont.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) IntCell(org.knime.core.data.def.IntCell) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) Pair(org.knime.core.util.Pair) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 65 with IntCell

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

the class DataTableSpecExtractor method extract.

/**
 * Creates and returns a data table containing the meta information of the given spec. The meta information is
 * referred to as the table data specification and contains information such as column names, types, domain values
 * (list of possible values for categorical columns) and lower and upper bounds. It also contains the information
 * which of the columns have a view handler associated, as well the possible values, if specified. Each column in
 * the given table spec is represented as a row in the returned table.
 *
 * @param spec The spec to extract the meta information from.
 * @return The data table containing the meta information of the given spec.
 */
public DataTable extract(final DataTableSpec spec) {
    List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
    if (m_extractColumnNameAsColumn) {
        colSpecs.add(new DataColumnSpecCreator("Column Name", StringCell.TYPE).createSpec());
    }
    colSpecs.add(new DataColumnSpecCreator("Column Type", StringCell.TYPE).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Column Index", IntCell.TYPE).createSpec());
    switch(m_propertyHandlerOutputFormat) {
        case Hide:
            break;
        case Boolean:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", BooleanCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", BooleanCell.TYPE).createSpec());
            break;
        default:
            colSpecs.add(new DataColumnSpecCreator("Color Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Size Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Shape Handler", StringCell.TYPE).createSpec());
            colSpecs.add(new DataColumnSpecCreator("Filter Handler", StringCell.TYPE).createSpec());
    }
    // likely number (important for sorting)
    DataType lowerBoundColType = null;
    DataType upperBoundColType = null;
    for (DataColumnSpec c : spec) {
        DataColumnDomain domain = c.getDomain();
        if (domain.hasLowerBound()) {
            DataType curLowerType = domain.getLowerBound().getType();
            if (lowerBoundColType == null) {
                lowerBoundColType = curLowerType;
            } else {
                lowerBoundColType = DataType.getCommonSuperType(lowerBoundColType, curLowerType);
            }
        }
        if (domain.hasUpperBound()) {
            DataType curUpperType = domain.getUpperBound().getType();
            if (upperBoundColType == null) {
                upperBoundColType = curUpperType;
            } else {
                upperBoundColType = DataType.getCommonSuperType(upperBoundColType, curUpperType);
            }
        }
    }
    lowerBoundColType = lowerBoundColType == null ? GENERIC_DATA_TYPE : lowerBoundColType;
    upperBoundColType = upperBoundColType == null ? GENERIC_DATA_TYPE : upperBoundColType;
    colSpecs.add(new DataColumnSpecCreator("Lower Bound", lowerBoundColType).createSpec());
    colSpecs.add(new DataColumnSpecCreator("Upper Bound", upperBoundColType).createSpec());
    int maxPossValues = 0;
    switch(m_possibleValueOutputFormat) {
        case Hide:
            break;
        case Collection:
            colSpecs.add(new DataColumnSpecCreator("Possible Values", SetCell.getCollectionType(GENERIC_DATA_TYPE)).createSpec());
            break;
        default:
            for (DataColumnSpec c : spec) {
                DataColumnDomain domain = c.getDomain();
                if (domain.hasValues()) {
                    maxPossValues = Math.max(domain.getValues().size(), maxPossValues);
                }
            }
            for (int i = 0; i < maxPossValues; i++) {
                colSpecs.add(new DataColumnSpecCreator("Value " + i, GENERIC_DATA_TYPE).createSpec());
            }
    }
    /* fill it */
    DataContainer dc = new DataContainer(new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()])));
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec colSpec = spec.getColumnSpec(i);
        List<DataCell> cells = new ArrayList<DataCell>();
        if (m_extractColumnNameAsColumn) {
            cells.add(new StringCell(colSpec.getName()));
        }
        cells.add(new StringCell(colSpec.getType().toString()));
        cells.add(new IntCell(i));
        ColorHandler colorHandler = colSpec.getColorHandler();
        SizeHandler sizeHandler = colSpec.getSizeHandler();
        ShapeHandler shapeHandler = colSpec.getShapeHandler();
        Optional<FilterHandler> filterHandler = colSpec.getFilterHandler();
        switch(m_propertyHandlerOutputFormat) {
            case Hide:
                break;
            case Boolean:
                cells.add(BooleanCellFactory.create(colorHandler != null));
                cells.add(BooleanCellFactory.create(sizeHandler != null));
                cells.add(BooleanCellFactory.create(shapeHandler != null));
                cells.add(BooleanCellFactory.create(filterHandler.isPresent()));
                break;
            default:
                cells.add(new StringCell(colorHandler == null ? "" : colorHandler.toString()));
                cells.add(new StringCell(sizeHandler == null ? "" : sizeHandler.toString()));
                cells.add(new StringCell(shapeHandler == null ? "" : shapeHandler.toString()));
                cells.add(new StringCell(filterHandler.map(f -> f.toString()).orElse("")));
        }
        DataColumnDomain domain = colSpec.getDomain();
        DataCell lb = domain.getLowerBound();
        if (lb != null) {
            cells.add(lb);
        } else {
            cells.add(DataType.getMissingCell());
        }
        DataCell ub = domain.getUpperBound();
        if (ub != null) {
            cells.add(ub);
        } else {
            cells.add(DataType.getMissingCell());
        }
        switch(m_possibleValueOutputFormat) {
            case Hide:
                break;
            case Collection:
                if (domain.hasValues()) {
                    cells.add(CollectionCellFactory.createSetCell(domain.getValues()));
                } else {
                    cells.add(DataType.getMissingCell());
                }
                break;
            default:
                Set<DataCell> set = domain.hasValues() ? domain.getValues() : Collections.EMPTY_SET;
                int nrColsToWrite = maxPossValues;
                for (DataCell c : set) {
                    cells.add(c);
                    nrColsToWrite -= 1;
                }
                while (nrColsToWrite > 0) {
                    cells.add(DataType.getMissingCell());
                    nrColsToWrite -= 1;
                }
        }
        dc.addRowToTable(new DefaultRow(new RowKey(colSpec.getName()), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : FilterHandler(org.knime.core.data.property.filter.FilterHandler) RowKey(org.knime.core.data.RowKey) DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) ColorHandler(org.knime.core.data.property.ColorHandler) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) ShapeHandler(org.knime.core.data.property.ShapeHandler) SetCell(org.knime.core.data.collection.SetCell) Set(java.util.Set) DataTable(org.knime.core.data.DataTable) SizeHandler(org.knime.core.data.property.SizeHandler) CollectionCellFactory(org.knime.core.data.collection.CollectionCellFactory) DataColumnDomain(org.knime.core.data.DataColumnDomain) List(java.util.List) BooleanCell(org.knime.core.data.def.BooleanCell) BooleanCellFactory(org.knime.core.data.def.BooleanCell.BooleanCellFactory) Optional(java.util.Optional) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) Collections(java.util.Collections) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) ArrayList(java.util.ArrayList) ShapeHandler(org.knime.core.data.property.ShapeHandler) FilterHandler(org.knime.core.data.property.filter.FilterHandler) IntCell(org.knime.core.data.def.IntCell) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) StringCell(org.knime.core.data.def.StringCell) SizeHandler(org.knime.core.data.property.SizeHandler) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) ColorHandler(org.knime.core.data.property.ColorHandler)

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