use of org.knime.core.data.DataType in project knime-core by knime.
the class TreeEnsembleModelPortObjectSpec method calculateFilterIndices.
/**
* Calculates the filter indices that can be used by a {@link FilterColumnRow} to create a row that matches the spec
* of the learn table (except the target).<br>
* Since this throws a {@link InvalidSettingsException} it is also used in the configure method of the
* {@link NodeModel} to ensure that the test table {@link DataTableSpec} matches the learn table spec.
*
* @param testTableInput {@link DataTableSpec} of the test table
* @return the indices of the learn columns in the <b>testTableInput</b>
* @throws InvalidSettingsException thrown if the {@link DataTableSpec} of the test table does not contain all
* columns of the learn table (except the target column).
*/
public int[] calculateFilterIndices(final DataTableSpec testTableInput) throws InvalidSettingsException {
DataTableSpec learnSpec = getLearnTableSpec();
// check existence and types of columns, create reordering
int[] result = new int[learnSpec.getNumColumns()];
for (int i = 0; i < learnSpec.getNumColumns(); i++) {
DataColumnSpec learnCol = learnSpec.getColumnSpec(i);
final String colName = learnCol.getName();
int dataColIndex = testTableInput.findColumnIndex(colName);
if (dataColIndex < 0) {
throw new InvalidSettingsException("Required data column \"" + colName + "\" does not exist in table");
}
DataColumnSpec dataCol = testTableInput.getColumnSpec(dataColIndex);
// expected type
DataType eType = learnCol.getType();
// actual type
DataType aType = dataCol.getType();
String errorType = null;
if (eType.isCompatible(NominalValue.class) && !aType.isCompatible(NominalValue.class)) {
errorType = "nominal";
}
if (eType.isCompatible(DoubleValue.class) && !aType.isCompatible(DoubleValue.class)) {
errorType = "numeric";
}
if (eType.isCompatible(BitVectorValue.class) && !aType.isCompatible(BitVectorValue.class)) {
errorType = "fingerprint/bitvector";
}
if (eType.isCompatible(ByteVectorValue.class) && !aType.isCompatible(ByteVectorValue.class)) {
errorType = "fingerprint/bytevector";
}
if (errorType != null) {
throw new InvalidSettingsException("Column \"" + colName + "\" does exist in the data but" + "is not of the expected " + errorType + " type");
}
result[i] = dataColIndex;
}
return result;
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class AbstractMetaDataMapper method checkForVectorColumn.
/**
* Checks if <b>colSpec</b> could originate from a vector column.
* If it does, this method throws an exception.
* @param colSpec {@link DataColumnSpec} to check
* @throws IllegalArgumentException if <b>colSpec</b> could originate from a vector column
*/
private static void checkForVectorColumn(final DataColumnSpec colSpec) {
final boolean possibleVectorName = TranslationUtil.isVectorFieldName(colSpec.getName());
DataType type = colSpec.getType();
DataColumnDomain domain = colSpec.getDomain();
boolean domainInformationIsMissing = false;
if (type.isCompatible(StringValue.class)) {
if (domain.hasValues()) {
domainInformationIsMissing = domain.getValues().isEmpty();
} else {
domainInformationIsMissing = true;
}
} else if (type.isCompatible(DoubleValue.class)) {
domainInformationIsMissing = !domain.hasBounds();
}
CheckUtils.checkArgument(!(possibleVectorName && domainInformationIsMissing), "The column %s seems to " + "originate from a vector column. A model learned on a vector can currently not be imported.", colSpec);
}
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();
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class ImagePortObject method toDataCell.
/**
* Produces a single data cell containing the image. This method also
* verifies the requirements stated in the
* {@link #ImagePortObject(ImageContent, ImagePortObjectSpec) constructor}.
*
* @return A new cell representing the image.
* @see ImageContent#toImageCell()
*/
public DataCell toDataCell() {
DataType typeInSpec = m_spec.getDataType();
DataCell result = m_content.toImageCell();
if (!typeInSpec.isASuperTypeOf(result.getType())) {
LOGGER.coding("Inconsistent data types for image cell \"" + result.getClass().getName() + "\" -- expected type \"" + typeInSpec + "\"");
return DataType.getMissingCell();
} else {
return result;
}
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class PMMLDataDictionaryTranslator method addColSpecsForDataFields.
/**
* @param pmmlDoc the PMML document to analyze
* @param colSpecs the list to add the data column specs to
*/
private void addColSpecsForDataFields(final PMMLDocument pmmlDoc, final List<DataColumnSpec> colSpecs) {
DataDictionary dict = pmmlDoc.getPMML().getDataDictionary();
for (DataField dataField : dict.getDataFieldArray()) {
String name = dataField.getName();
DataType dataType = getKNIMEDataType(dataField.getDataType());
DataColumnSpecCreator specCreator = new DataColumnSpecCreator(name, dataType);
DataColumnDomain domain = null;
if (dataType.isCompatible(NominalValue.class)) {
Value[] valueArray = dataField.getValueArray();
DataCell[] cells;
if (DataType.getType(StringCell.class).equals(dataType)) {
if (dataField.getIntervalArray().length > 0) {
throw new IllegalArgumentException("Intervals cannot be defined for Strings.");
}
cells = new StringCell[valueArray.length];
if (valueArray.length > 0) {
for (int j = 0; j < cells.length; j++) {
cells[j] = new StringCell(valueArray[j].getValue());
}
}
domain = new DataColumnDomainCreator(cells).createDomain();
}
} else if (dataType.isCompatible(DoubleValue.class)) {
Double leftMargin = null;
Double rightMargin = null;
Interval[] intervalArray = dataField.getIntervalArray();
if (intervalArray != null && intervalArray.length > 0) {
Interval interval = dataField.getIntervalArray(0);
leftMargin = interval.getLeftMargin();
rightMargin = interval.getRightMargin();
} else if (dataField.getValueArray() != null && dataField.getValueArray().length > 0) {
// try to derive the bounds from the values
Value[] valueArray = dataField.getValueArray();
List<Double> values = new ArrayList<Double>();
for (int j = 0; j < valueArray.length; j++) {
String value = "";
try {
value = valueArray[j].getValue();
values.add(Double.parseDouble(value));
} catch (Exception e) {
throw new IllegalArgumentException("Skipping domain calculation. " + "Value \"" + value + "\" cannot be cast to double.");
}
}
leftMargin = Collections.min(values);
rightMargin = Collections.max(values);
}
if (leftMargin != null && rightMargin != null) {
// set the bounds of the domain if available
DataCell lowerBound = null;
DataCell upperBound = null;
if (DataType.getType(IntCell.class).equals(dataType)) {
lowerBound = new IntCell(leftMargin.intValue());
upperBound = new IntCell(rightMargin.intValue());
} else if (DataType.getType(DoubleCell.class).equals(dataType)) {
lowerBound = new DoubleCell(leftMargin);
upperBound = new DoubleCell(rightMargin);
}
domain = new DataColumnDomainCreator(lowerBound, upperBound).createDomain();
} else {
domain = new DataColumnDomainCreator().createDomain();
}
}
specCreator.setDomain(domain);
colSpecs.add(specCreator.createSpec());
m_dictFields.add(name);
}
}
Aggregations