Search in sources :

Example 31 with DataType

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

the class DBGroupByNodeModel method createOutSpec.

/**
 * @param inSpec Spec of the input table
 * @param checkRetrieveMetadata
 * @return Spec of the output table
 * @throws InvalidSettingsException if settings do not match the input specification
 */
private DataTableSpec createOutSpec(final DataTableSpec inSpec, final DatabaseConnectionSettings settings, final String query, final boolean ignoreExceptions) throws InvalidSettingsException {
    // Try get spec from database
    try {
        DatabaseQueryConnectionSettings querySettings = new DatabaseQueryConnectionSettings(settings, query);
        DatabaseReaderConnection conn = new DatabaseReaderConnection(querySettings);
        return conn.getDataTableSpec(getCredentialsProvider());
    } catch (SQLException e) {
        NodeLogger.getLogger(getClass()).info("Could not determine table spec from database, trying to guess now", e);
        if (!ignoreExceptions) {
            throw new InvalidSettingsException("Error in automatically build sql statement: " + e.getMessage());
        }
    // Otherwise guess spec
    }
    List<DataColumnSpec> colSpecs = new ArrayList<>();
    // Add all group by columns
    for (String col : m_groupByCols.getIncludeList()) {
        colSpecs.add(inSpec.getColumnSpec(col));
    }
    // Add aggregated columns
    for (int i = 0; i < m_aggregatedColumns.length; i++) {
        String col = m_aggregatedColumns[i];
        String method = m_aggregationMethods[i];
        if (inSpec.getColumnSpec(col) == null) {
            throw new InvalidSettingsException("Column '" + col + "' in aggregation " + method + " does not exist");
        }
        final DatabaseUtility databaseUtility = settings.getUtility();
        final DBAggregationFunction function = databaseUtility.getAggregationFunction(method);
        // Get type of column after aggregation
        DataType type = function.getType(inSpec.getColumnSpec(col).getType());
        colSpecs.add(new DataColumnSpecCreator(generateColumnName(col, method), type).createSpec());
    }
    return new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()]));
}
Also used : DatabaseUtility(org.knime.core.node.port.database.DatabaseUtility) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DatabaseReaderConnection(org.knime.core.node.port.database.DatabaseReaderConnection) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) DataType(org.knime.core.data.DataType)

Example 32 with DataType

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

the class NaiveBayesModel method createModelMap.

private static Map<String, AttributeModel> createModelMap(final DataTableSpec tableSpec, final String classColName, final int maxNoOfNominalVals, final boolean skipMissingVals) {
    final int numColumns = tableSpec.getNumColumns();
    final Map<String, AttributeModel> modelMap = new HashMap<String, AttributeModel>(numColumns);
    for (int i = 0; i < numColumns; i++) {
        final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
        final String colName = colSpec.getName();
        final DataType colType = colSpec.getType();
        final AttributeModel model;
        if (colName.equals(classColName)) {
            model = new ClassAttributeModel(colName, skipMissingVals, maxNoOfNominalVals);
        } else if (colType.isCompatible(DoubleValue.class)) {
            model = new NumericalAttributeModel(colName, skipMissingVals);
        } else if (colType.isCompatible(NominalValue.class)) {
            model = new NominalAttributeModel(colName, skipMissingVals, maxNoOfNominalVals);
        } else if (colType.isCompatible(BitVectorValue.class)) {
            model = new BitVectorAttributeModel(colName, skipMissingVals);
        } else {
            continue;
        }
        modelMap.put(colName, model);
    }
    return modelMap;
}
Also used : HashMap(java.util.HashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Example 33 with DataType

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

the class ColumnToGridConfiguration method autoGuessIncludeColumns.

/**
 * Auto-guessing: choose first column that is not string, int, double
 * as include; if no such column exists use first string column, otherwise
 * use non (null returned).
 * @param spec Input spec.
 * @return A meaningful default or null.
 */
static String[] autoGuessIncludeColumns(final DataTableSpec spec) {
    String firstStringCol = null;
    String firstUnknownCol = null;
    for (DataColumnSpec col : spec) {
        DataType type = col.getType();
        if (type.equals(StringCell.TYPE)) {
            if (firstStringCol == null) {
                firstStringCol = col.getName();
            }
        } else if (type.equals(DoubleCell.TYPE)) {
        // ignore
        } else if (type.equals(IntCell.TYPE)) {
        // ignore
        } else {
            if (firstUnknownCol == null) {
                firstUnknownCol = col.getName();
            }
        }
    }
    String sel = null;
    if (firstUnknownCol != null) {
        sel = firstUnknownCol;
    } else if (firstStringCol != null) {
        sel = firstStringCol;
    } else {
        return null;
    }
    return new String[] { sel };
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType)

Example 34 with DataType

use of org.knime.core.data.DataType 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 35 with DataType

use of org.knime.core.data.DataType 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)

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