Search in sources :

Example 11 with ByteVectorValue

use of org.knime.core.data.vector.bytevector.ByteVectorValue in project knime-core by knime.

the class TreeEnsembleModel method createByteVectorPredictorRecord.

private PredictorRecord createByteVectorPredictorRecord(final DataRow filterRow) {
    assert filterRow.getNumCells() == 1 : "Expected one cell as byte vector data";
    DataCell c = filterRow.getCell(0);
    if (c.isMissing()) {
        return null;
    }
    ByteVectorValue bv = (ByteVectorValue) c;
    final long length = bv.length();
    if (length != getMetaData().getNrAttributes()) {
        throw new IllegalArgumentException("The byte-vector in " + filterRow.getKey().getString() + " has the wrong length. (" + length + " instead of " + getMetaData().getNrAttributes() + ")");
    }
    Map<String, Object> valueMap = new LinkedHashMap<String, Object>((int) (length / 0.75 + 1.0));
    for (int i = 0; i < length; i++) {
        valueMap.put(TreeNumericColumnMetaData.getAttributeName(i), Integer.valueOf(bv.get(i)));
    }
    return new PredictorRecord(valueMap);
}
Also used : PredictorRecord(org.knime.base.node.mine.treeensemble.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with ByteVectorValue

use of org.knime.core.data.vector.bytevector.ByteVectorValue in project knime-core by knime.

the class RegressionTrainingData method maxLength.

/**
 * @param data
 * @param i
 * @param missingValueHandling
 * @param lengthConstraint
 * @return
 * @throws InvalidSettingsException
 */
private long maxLength(final BufferedDataTable data, final int i, final SameLengthVectors lengthConstraint, final AllowMissing missingValueHandling) throws InvalidSettingsException {
    long maxLength = 0;
    for (final DataRow dataRow : data) {
        final DataCell cell = dataRow.getCell(i);
        if (cell.isMissing()) {
            switch(missingValueHandling) {
                case NoMissing:
                    throw new InvalidSettingsException("There are missing values in the column with index (from 1): " + (i + 1));
                case SkipMissing:
                    // the loop
                    continue;
                default:
                    throw new UnsupportedOperationException("Not supported missing value handling: " + missingValueHandling);
            }
        }
        long vectorLength;
        if (cell instanceof BitVectorValue) {
            BitVectorValue bvv = (BitVectorValue) cell;
            vectorLength = bvv.length();
        } else if (cell instanceof ByteVectorValue) {
            ByteVectorValue bvv = (ByteVectorValue) cell;
            vectorLength = bvv.length();
        } else if (cell instanceof ListDataValue) {
            ListDataValue ldv = (ListDataValue) cell;
            vectorLength = ldv.size();
        } else {
            throw new IllegalStateException("Wrong type of value in the column with index (from 1): " + (i + 1) + " : " + cell.getType());
        }
        switch(lengthConstraint) {
            case BoundedLength:
                maxLength = Math.max(maxLength, vectorLength);
                break;
            case SameLength:
                if (maxLength == 0) {
                    maxLength = vectorLength;
                } else if (maxLength != vectorLength) {
                    throw new InvalidSettingsException("There are different length vectors in the column with index (from 1): " + (i + 1) + " which are: " + maxLength + " and " + vectorLength);
                }
                break;
            default:
                throw new UnsupportedOperationException("Not supported length constraint: " + lengthConstraint);
        }
    }
    return maxLength;
}
Also used : ListDataValue(org.knime.core.data.collection.ListDataValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DataRow(org.knime.core.data.DataRow)

Example 13 with ByteVectorValue

use of org.knime.core.data.vector.bytevector.ByteVectorValue in project knime-core by knime.

the class RegressionTrainingRow method getValue.

/**
 * Gets the value from the {@code cell} with the {@code 0}-based {@code index} in case it was a collection.
 *
 * @param cell A {@link DataCell}, probably a collection of numbers/bytes/bits.
 * @param index The index to select from a collection.
 * @param missingHandling How to handle missing values.
 * @return The number at the specified position.
 * @since 3.1
 */
public static double getValue(final DataCell cell, final int index, final MissingHandling missingHandling) {
    if (cell instanceof BitVectorValue) {
        final BitVectorValue bvv = (BitVectorValue) cell;
        if (bvv.length() > index) {
            return bvv.get(index) ? 1d : 0d;
        }
        // TODO NaN, or 0?
        return Double.NaN;
    } else if (cell instanceof ByteVectorValue) {
        final ByteVectorValue bvv = (ByteVectorValue) cell;
        if (bvv.length() > index) {
            return bvv.get(index);
        }
        // Maybe some other value?
        return Double.NaN;
    } else if (cell instanceof ListDataValue) {
        final ListDataValue ldv = (ListDataValue) cell;
        if (ldv.size() > index) {
            DataCell dataCell = ldv.get(index);
            if (dataCell instanceof DoubleValue) {
                final DoubleValue dv = (DoubleValue) dataCell;
                return dv.getDoubleValue();
            }
            missingHandling.isMissing(dataCell);
            return Double.NaN;
        }
    }
    throw new IllegalStateException("Not a missing, nor a vector value: " + cell);
}
Also used : ListDataValue(org.knime.core.data.collection.ListDataValue) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Aggregations

ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)13 DataCell (org.knime.core.data.DataCell)10 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)7 LinkedHashMap (java.util.LinkedHashMap)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 IntCell (org.knime.core.data.def.IntCell)3 StringCell (org.knime.core.data.def.StringCell)3 PredictorRecord (org.knime.base.node.mine.treeensemble.data.PredictorRecord)2 DataRow (org.knime.core.data.DataRow)2 DataType (org.knime.core.data.DataType)2 DoubleValue (org.knime.core.data.DoubleValue)2 ListDataValue (org.knime.core.data.collection.ListDataValue)2 TreeType (org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel.TreeType)1 PredictorRecord (org.knime.base.node.mine.treeensemble2.data.PredictorRecord)1 AbstractCellFactory (org.knime.core.data.container.AbstractCellFactory)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 DoubleVectorValue (org.knime.core.data.vector.doublevector.DoubleVectorValue)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1