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