Search in sources :

Example 1 with ListDataValue

use of org.knime.core.data.collection.ListDataValue 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 2 with ListDataValue

use of org.knime.core.data.collection.ListDataValue 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

DataCell (org.knime.core.data.DataCell)2 ListDataValue (org.knime.core.data.collection.ListDataValue)2 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)2 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)2 DataRow (org.knime.core.data.DataRow)1 DoubleValue (org.knime.core.data.DoubleValue)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1