Search in sources :

Example 76 with DataColumnSpec

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

the class MissingValueHandling2Table method getColSetting.

/* Does internal mapping of the constructor argument. */
private static MissingValueHandling2ColSetting[] getColSetting(final DataTableSpec spec, final MissingValueHandling2ColSetting[] sets, final boolean throwExeception) throws InvalidSettingsException {
    MissingValueHandling2ColSetting[] results = new MissingValueHandling2ColSetting[spec.getNumColumns()];
    // fill up the default (i.e. meta-settings for String, Double, Int,
    // and Other columns) - if they are available
    Hashtable<Integer, MissingValueHandling2ColSetting> hash = new Hashtable<Integer, MissingValueHandling2ColSetting>();
    // the default one
    MissingValueHandling2ColSetting untouched = new MissingValueHandling2ColSetting(MissingValueHandling2ColSetting.TYPE_UNKNOWN);
    untouched.setMethod(MissingValueHandling2ColSetting.METHOD_NO_HANDLING);
    for (int i = 0; i < sets.length; i++) {
        if (sets[i].isMetaConfig()) {
            int type = sets[i].getType();
            hash.put(type, sets[i]);
        }
    }
    // there are only double, int, string, other
    assert (hash.size() <= 4);
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec colSpec = spec.getColumnSpec(i);
        DataType type = colSpec.getType();
        Integer hashKey;
        if (type.isASuperTypeOf(StringCell.TYPE)) {
            hashKey = MissingValueHandling2ColSetting.TYPE_STRING;
        } else if (type.isASuperTypeOf(DoubleCell.TYPE)) {
            hashKey = MissingValueHandling2ColSetting.TYPE_DOUBLE;
        } else if (type.isASuperTypeOf(IntCell.TYPE)) {
            hashKey = MissingValueHandling2ColSetting.TYPE_INT;
        } else {
            hashKey = MissingValueHandling2ColSetting.TYPE_UNKNOWN;
        }
        MissingValueHandling2ColSetting setting = hash.get(hashKey);
        if (setting == null) {
            setting = untouched;
        }
        // may be replaced by an individual setting below
        results[i] = setting;
    }
    for (int i = 0; i < sets.length; i++) {
        if (sets[i].isMetaConfig()) {
            continue;
        }
        String[] names = sets[i].getNames();
        for (int j = 0; j < names.length; j++) {
            String name = names[j];
            int type = sets[i].getType();
            final int index = spec.findColumnIndex(name);
            if (index < 0) {
                String error = "Unable to do missing value handling for" + " column '" + name + "', no such column in table";
                if (throwExeception) {
                    throw new InvalidSettingsException(error);
                } else {
                    error = error + "; skip it.";
                    LOGGER.warn(error);
                }
                continue;
            }
            DataColumnSpec colSpec = spec.getColumnSpec(index);
            DataType colType = colSpec.getType();
            if (type == MissingValueHandling2ColSetting.TYPE_INT && !colType.isASuperTypeOf(IntCell.TYPE)) {
                String error = "Missing value handling for column '" + name + "' failed, incompatible types: " + colType + " is not super type of int type";
                if (throwExeception) {
                    throw new InvalidSettingsException(error);
                } else {
                    error = error + "; skip it.";
                    LOGGER.warn(error);
                }
            }
            if (type == MissingValueHandling2ColSetting.TYPE_DOUBLE && !colType.isASuperTypeOf(DoubleCell.TYPE)) {
                String error = "Missing value handling for column '" + name + "' failed, incompatible types: " + colType + " is not super type of double type";
                if (throwExeception) {
                    throw new InvalidSettingsException(error);
                } else {
                    error = error + "; skip it.";
                    LOGGER.warn(error);
                }
            }
            if (type == MissingValueHandling2ColSetting.TYPE_STRING && !colType.isASuperTypeOf(StringCell.TYPE)) {
                String error = "Missing value handling for column '" + name + "' failed, incompatible types: " + colType + " is not super type of string type";
                if (throwExeception) {
                    throw new InvalidSettingsException(error);
                } else {
                    error = error + "; skip it.";
                    LOGGER.warn(error);
                }
            }
            results[index] = sets[i];
        }
    }
    return results;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Hashtable(java.util.Hashtable) DataType(org.knime.core.data.DataType)

Example 77 with DataColumnSpec

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

the class ProximityMatrix method createTable.

public BufferedDataTable createTable(final ExecutionContext exec) throws CanceledExecutionException {
    int numCols = getNumCols();
    int numRows = getNumRows();
    DataColumnSpec[] colSpecs = new DataColumnSpec[numCols];
    for (int i = 0; i < colSpecs.length; i++) {
        colSpecs[i] = new DataColumnSpecCreator(getRowKeyForTable(1, i).getString(), DoubleCell.TYPE).createSpec();
    }
    DataTableSpec tableSpec = new DataTableSpec(colSpecs);
    BufferedDataContainer container = exec.createDataContainer(tableSpec);
    for (int i = 0; i < numRows; i++) {
        exec.checkCanceled();
        exec.setProgress(((double) i) / numRows, "Row " + i + "/" + numRows);
        DataCell[] cells = new DataCell[numCols];
        for (int j = 0; j < numCols; j++) {
            cells[j] = new DoubleCell(getEntryAt(i, j));
        }
        container.addRowToTable(new DefaultRow(getRowKeyForTable(0, i), cells));
    }
    container.close();
    return container.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 78 with DataColumnSpec

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

the class OptionsPanel method getMissingColSpecName.

@SuppressWarnings("null")
private static String getMissingColSpecName(final DataTableSpec spec, final String[] includedNames, final String[] excludedNames) {
    ColumnRearranger r = new ColumnRearranger(spec);
    // remove columns we know from the include list
    for (String colName : includedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    // remove columns we know from the exclude list
    for (String colName : excludedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    DataTableSpec tableSpecWithMissing = r.createSpec();
    DataColumnSpec formerTargetSpec = null;
    // were either in the include or exclude list
    for (DataColumnSpec colSpec : tableSpecWithMissing) {
        DataType colType = colSpec.getType();
        if (colType.isCompatible(NominalValue.class) || colType.isCompatible(DoubleValue.class)) {
            formerTargetSpec = colSpec;
            break;
        }
    }
    assert formerTargetSpec != null : "The former target spec is no longer part of the table, please check.";
    return formerTargetSpec.getName();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) DataType(org.knime.core.data.DataType)

Example 79 with DataColumnSpec

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

the class RegressionTreePMMLTranslatorNodeModel method containsVector.

private static boolean containsVector(final DataTableSpec learnFeatureSpec) {
    for (DataColumnSpec colSpec : learnFeatureSpec) {
        DataType type = colSpec.getType();
        boolean isVector = type.isCompatible(BitVectorValue.class) || type.isCompatible(DoubleVectorValue.class) || type.isCompatible(ByteVectorValue.class);
        if (isVector) {
            return true;
        }
    }
    return false;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue)

Example 80 with DataColumnSpec

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

the class RegressionTreePMMLPredictorNodeModel method translateSpec.

private RegressionTreeModelPortObjectSpec translateSpec(final PMMLPortObjectSpec pmmlSpec) {
    DataTableSpec pmmlDataSpec = pmmlSpec.getDataTableSpec();
    ColumnRearranger cr = new ColumnRearranger(pmmlDataSpec);
    List<DataColumnSpec> targets = pmmlSpec.getTargetCols();
    CheckUtils.checkArgument(!targets.isEmpty(), "The provided PMML does not declare a target field.");
    CheckUtils.checkArgument(targets.size() == 1, "The provided PMML declares multiple target. " + "This behavior is currently not supported.");
    cr.move(targets.get(0).getName(), pmmlDataSpec.getNumColumns());
    return new RegressionTreeModelPortObjectSpec(cr.createSpec());
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) RegressionTreeModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.RegressionTreeModelPortObjectSpec)

Aggregations

DataColumnSpec (org.knime.core.data.DataColumnSpec)800 DataTableSpec (org.knime.core.data.DataTableSpec)351 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)239 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)223 DataCell (org.knime.core.data.DataCell)187 ArrayList (java.util.ArrayList)167 DataType (org.knime.core.data.DataType)149 DataRow (org.knime.core.data.DataRow)124 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)123 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)119 DoubleValue (org.knime.core.data.DoubleValue)104 HashSet (java.util.HashSet)92 BufferedDataTable (org.knime.core.node.BufferedDataTable)77 LinkedHashSet (java.util.LinkedHashSet)65 LinkedHashMap (java.util.LinkedHashMap)56 LinkedList (java.util.LinkedList)47 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)46 DoubleCell (org.knime.core.data.def.DoubleCell)46 StringCell (org.knime.core.data.def.StringCell)45 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)43