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()]));
}
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;
}
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 };
}
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;
}
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();
}
Aggregations