Search in sources :

Example 96 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class AffineTransRowIterator method next.

/**
 * {@inheritDoc}
 */
@Override
public DataRow next() {
    AffineTransConfiguration config = m_transtable.getConfiguration();
    int[] indices = m_transtable.getIndicesInConfiguration();
    double[] scales = config.getScales();
    double[] translations = config.getTranslations();
    double[] min = config.getMin();
    double[] max = config.getMax();
    final DataRow in = m_it.next();
    final DataCell[] cells = new DataCell[in.getNumCells()];
    for (int i = 0; i < cells.length; i++) {
        final DataCell oldCell = in.getCell(i);
        if (oldCell.isMissing() || indices[i] == -1) {
            cells[i] = oldCell;
        } else {
            int index = indices[i];
            double interval = max[index] - min[index];
            double oldDouble = ((DoubleValue) oldCell).getDoubleValue();
            double newDouble = scales[index] * oldDouble + translations[index];
            if (!Double.isNaN(min[index])) {
                if (newDouble < min[index]) {
                    if ((min[index] - newDouble) / interval < AffineTransTable.VERY_SMALL) {
                        newDouble = min[index];
                    } else {
                        m_transtable.setErrorMessage("Normalized value is out of bounds." + " Original value: " + oldDouble + " Transformed value: " + newDouble + " Lower Bound: " + min[index]);
                    }
                }
            }
            if (!Double.isNaN(max[index])) {
                if (newDouble > max[index]) {
                    if ((newDouble - max[index]) / interval < AffineTransTable.VERY_SMALL) {
                        newDouble = max[index];
                    } else {
                        m_transtable.setErrorMessage("Normalized value is out of bounds." + " Original value: " + oldDouble + " Transformed value: " + newDouble + " Upper Bound: " + max[index]);
                    }
                }
            }
            cells[i] = new DoubleCell(newDouble);
        }
    }
    return new DefaultRow(in.getKey(), cells);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow)

Example 97 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class VariableToTable2NodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    DataTableSpec spec = createOutSpec();
    BufferedDataContainer cont = exec.createDataContainer(spec);
    List<Pair<String, FlowVariable.Type>> vars = getVariablesOfInterest();
    DataCell[] specs = new DataCell[vars.size()];
    List<String> lostVariables = new ArrayList<String>();
    for (int i = 0; i < vars.size(); i++) {
        Pair<String, FlowVariable.Type> c = vars.get(i);
        String name = c.getFirst();
        // fallback
        DataCell cell = DataType.getMissingCell();
        switch(c.getSecond()) {
            case DOUBLE:
                try {
                    double dValue = peekFlowVariableDouble(c.getFirst());
                    cell = new DoubleCell(dValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Double)");
                }
                break;
            case INTEGER:
                try {
                    int iValue = peekFlowVariableInt(c.getFirst());
                    cell = new IntCell(iValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Integer)");
                }
                break;
            case STRING:
                try {
                    String sValue = peekFlowVariableString(c.getFirst());
                    sValue = sValue == null ? "" : sValue;
                    cell = new StringCell(sValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (String)");
                }
                break;
        }
        specs[i] = cell;
    }
    cont.addRowToTable(new DefaultRow(m_rowID.getStringValue(), specs));
    cont.close();
    return new BufferedDataTable[] { cont.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) IntCell(org.knime.core.data.def.IntCell) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) Pair(org.knime.core.util.Pair) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 98 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class ExpressionFactoryTest method setUp.

/**
 * Sets up the mock objects.
 *
 * @throws java.lang.Exception Should not happen.
 */
@Before
public void setUp() throws Exception {
    final Map<String, Map<String, String>> emptyMap = Collections.emptyMap();
    m_mockBoolExpression = new Expression.Base() {

        @Override
        public List<DataType> getInputArgs() {
            return Collections.emptyList();
        }

        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            final DataCell cell;
            switch(Integer.parseInt(row.getKey().getString())) {
                case 0:
                    cell = BooleanCell.TRUE;
                    break;
                case 1:
                    cell = BooleanCell.FALSE;
                    break;
                case 2:
                    cell = BooleanCell.TRUE;
                    break;
                case 3:
                    cell = BooleanCell.FALSE;
                    break;
                case 4:
                    cell = DataType.getMissingCell();
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
            return new ExpressionValue(cell, emptyMap);
        }

        @Override
        public boolean isConstant() {
            return false;
        }

        @Override
        public ASTType getTreeType() {
            return ASTType.ColRef;
        }
    };
    m_mockBoolExpressionOther = new Expression.Base() {

        @Override
        public boolean isConstant() {
            return false;
        }

        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

        @Override
        public List<DataType> getInputArgs() {
            return Collections.emptyList();
        }

        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            final DataCell cell;
            switch(Integer.parseInt(row.getKey().getString())) {
                case 0:
                    cell = BooleanCell.TRUE;
                    break;
                case 1:
                    cell = BooleanCell.TRUE;
                    break;
                case 2:
                    cell = BooleanCell.FALSE;
                    break;
                case 3:
                    cell = BooleanCell.FALSE;
                    break;
                case 4:
                    cell = BooleanCell.TRUE;
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
            return new ExpressionValue(cell, emptyMap);
        }

        @Override
        public ASTType getTreeType() {
            return ASTType.ColRef;
        }
    };
    final DefaultRow row = new DefaultRow(new RowKey("x"), new DataCell[0]);
    m_rows = new DataRow[] { new DefaultRow(new RowKey("0"), row), new DefaultRow(new RowKey("1"), row), new DefaultRow(new RowKey("2"), row), new DefaultRow(new RowKey("3"), row), new DefaultRow(new RowKey("4"), new DataCell[] { DataType.getMissingCell() }) };
}
Also used : RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) List(java.util.List) DefaultRow(org.knime.core.data.def.DefaultRow) Map(java.util.Map) Before(org.junit.Before)

Example 99 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class ExpressionFactoryTest method testColumnRef.

/**
 * Test method for {@link ExpressionFactory#columnRef(DataTableSpec, java.lang.String)} .
 */
@Test
public void testColumnRef() {
    final DefaultRow row0 = new DefaultRow(new RowKey("0"), new IntCell(3));
    final DefaultRow row1 = new DefaultRow(new RowKey("1"), new IntCell(3), new DoubleCell(3.14159));
    final DefaultRow row2 = new DefaultRow(new RowKey("1"), new DoubleCell(3.14159), new StringCell("Hi"));
    final ExpressionValue firstVal = m_factory.columnRef(new DataTableSpec(new String[] { "Num" }, new DataType[] { IntCell.TYPE }), "Num").evaluate(row0, null);
    assertEquals(row0.getCell(0), firstVal.getValue());
    assertEquals(row1.getCell(0), m_factory.columnRef(new DataTableSpec(new String[] { "Num", "Pi" }, new DataType[] { IntCell.TYPE, DoubleCell.TYPE }), "Num").evaluate(row0, null).getValue());
    final ExpressionValue secondVal = m_factory.columnRef(new DataTableSpec(new String[] { "Num", "Pi" }, new DataType[] { IntCell.TYPE, DoubleCell.TYPE }), "Pi").evaluate(row1, null);
    assertEquals(row1.getCell(1), secondVal.getValue());
    final ExpressionValue thirdVal = m_factory.columnRef(new DataTableSpec(new String[] { "Pi", "Greeting" }, new DataType[] { DoubleCell.TYPE, StringCell.TYPE }), "Greeting").evaluate(row2, null);
    assertEquals(row2.getCell(1), thirdVal.getValue());
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 100 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class ExpressionFactoryTest method testColumnRefForMissing.

/**
 * Test method for {@link ExpressionFactory#columnRefForMissing(DataTableSpec, String)} .
 */
@Test
public void testColumnRefForMissing() {
    final DefaultRow row0 = new DefaultRow(new RowKey("0"), DataType.getMissingCell());
    final ExpressionValue firstVal = m_factory.columnRef(new DataTableSpec(new String[] { "Num" }, new DataType[] { IntCell.TYPE }), "Num").evaluate(row0, null);
    assertEquals(row0.getCell(0), firstVal.getValue());
    assertEquals(BooleanCell.FALSE, m_factory.columnRef(new DataTableSpec(new String[] { "Bool" }, new DataType[] { BooleanCell.TYPE }), "Bool").evaluate(row0, null).getValue());
    final ExpressionValue secondVal = m_factory.columnRefForMissing(new DataTableSpec(new String[] { "Bool" }, new DataType[] { BooleanCell.TYPE }), "Bool").evaluate(row0, null);
    assertEquals(row0.getCell(0), secondVal.getValue());
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DefaultRow(org.knime.core.data.def.DefaultRow) Test(org.junit.Test)

Aggregations

DefaultRow (org.knime.core.data.def.DefaultRow)207 DataCell (org.knime.core.data.DataCell)165 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)94 DataTableSpec (org.knime.core.data.DataTableSpec)92 DataRow (org.knime.core.data.DataRow)88 RowKey (org.knime.core.data.RowKey)80 DoubleCell (org.knime.core.data.def.DoubleCell)66 StringCell (org.knime.core.data.def.StringCell)65 BufferedDataTable (org.knime.core.node.BufferedDataTable)56 IntCell (org.knime.core.data.def.IntCell)46 ArrayList (java.util.ArrayList)26 DataType (org.knime.core.data.DataType)26 DataColumnSpec (org.knime.core.data.DataColumnSpec)22 DataContainer (org.knime.core.data.container.DataContainer)21 HashSet (java.util.HashSet)18 LinkedHashMap (java.util.LinkedHashMap)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 LinkedHashSet (java.util.LinkedHashSet)14 DoubleValue (org.knime.core.data.DoubleValue)14 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)14