Search in sources :

Example 51 with DataType

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;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) DataType(org.knime.core.data.DataType) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Example 52 with DataType

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);
}
Also used : DataColumnDomain(org.knime.core.data.DataColumnDomain) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType)

Example 53 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)

Example 54 with DataType

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;
    }
}
Also used : DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell)

Example 55 with DataType

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);
    }
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) IntCell(org.knime.core.data.def.IntCell) DataColumnDomain(org.knime.core.data.DataColumnDomain) DataField(org.dmg.pmml.DataFieldDocument.DataField) StringCell(org.knime.core.data.def.StringCell) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) BooleanValue(org.knime.core.data.BooleanValue) IntValue(org.knime.core.data.IntValue) Value(org.dmg.pmml.ValueDocument.Value) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) Interval(org.dmg.pmml.IntervalDocument.Interval)

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