Search in sources :

Example 51 with DefaultRow

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

the class LiftChartNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    ConvenienceMethods.checkTableSize(inData[0]);
    int predColIndex = inData[0].getDataTableSpec().findColumnIndex(m_responseColumn.getStringValue());
    List<String> inclList = new LinkedList<String>();
    inclList.add(m_probabilityColumn.getStringValue());
    boolean[] order = new boolean[] { false };
    SortedTable st = new SortedTable(inData[0], inclList, order, exec);
    long totalResponses = 0;
    double partWidth = Double.parseDouble(m_intervalWidth.getStringValue());
    int nrParts = (int) Math.ceil(100.0 / partWidth);
    List<Integer> positiveResponses = new LinkedList<Integer>();
    int rowIndex = 0;
    for (DataRow row : st) {
        if (row.getCell(predColIndex).isMissing()) {
            setWarningMessage("There are missing values." + " Please check your data.");
            continue;
        }
        String response = ((StringValue) row.getCell(predColIndex)).getStringValue().trim();
        if (response.equalsIgnoreCase(m_responseLabel.getStringValue())) {
            totalResponses++;
            positiveResponses.add(rowIndex);
        }
        rowIndex++;
    }
    int[] counter = new int[nrParts];
    int partWidthAbsolute = (int) Math.ceil(rowIndex / (double) nrParts);
    double avgResponse = (double) positiveResponses.size() / rowIndex;
    for (int rIndex : positiveResponses) {
        int index = rIndex / partWidthAbsolute;
        counter[index]++;
    }
    DataColumnSpec[] colSpec = new DataColumnSpec[3];
    colSpec[0] = new DataColumnSpecCreator("Lift", DoubleCell.TYPE).createSpec();
    colSpec[1] = new DataColumnSpecCreator("Baseline", DoubleCell.TYPE).createSpec();
    colSpec[2] = new DataColumnSpecCreator("Cumulative Lift", DoubleCell.TYPE).createSpec();
    DataTableSpec tableSpec = new DataTableSpec(colSpec);
    DataContainer cont = exec.createDataContainer(tableSpec);
    colSpec = new DataColumnSpec[2];
    colSpec[0] = new DataColumnSpecCreator("Actual", DoubleCell.TYPE).createSpec();
    colSpec[1] = new DataColumnSpecCreator("Baseline", DoubleCell.TYPE).createSpec();
    tableSpec = new DataTableSpec(colSpec);
    DataContainer responseCont = exec.createDataContainer(tableSpec);
    long cumulativeCounter = 0;
    responseCont.addRowToTable(new DefaultRow(new RowKey("0"), 0.0, 0.0));
    for (int i = 0; i < counter.length; i++) {
        cumulativeCounter += counter[i];
        double responseRate = (double) counter[i] / partWidthAbsolute;
        double lift = responseRate / avgResponse;
        double cumResponseRate = (double) cumulativeCounter / totalResponses;
        long number = partWidthAbsolute * (i + 1);
        // well.. rounding problems
        if (number > rowIndex) {
            number = rowIndex;
        }
        double cumulativeLift = // (double)cumulativeCounter / (partWidthAbsolute * (i + 1));
        (double) cumulativeCounter / number;
        cumulativeLift /= avgResponse;
        // cumulativeLift = lifts / (i+1);
        double rowKey = ((i + 1) * partWidth);
        if (rowKey > 100) {
            rowKey = 100;
        }
        cont.addRowToTable(new DefaultRow(new RowKey("" + rowKey), lift, 1.0, cumulativeLift));
        double cumBaseline = (i + 1) * partWidth;
        if (cumBaseline > 100) {
            cumBaseline = 100;
        }
        responseCont.addRowToTable(new DefaultRow(new RowKey("" + rowKey), cumResponseRate * 100, cumBaseline));
    }
    cont.close();
    responseCont.close();
    m_dataArray[0] = new DefaultDataArray(cont.getTable(), 1, (int) cont.size());
    m_dataArray[1] = new DefaultDataArray(responseCont.getTable(), 1, (int) responseCont.size());
    return new BufferedDataTable[] { st.getBufferedDataTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) SortedTable(org.knime.base.data.sort.SortedTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 52 with DefaultRow

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

the class DataTableDomainCreatorTest method testBoundsDouble.

/**
 * Check whether upper and lower bounds are computed correctly for double column (including infinity and NaN).
 */
@Test
public void testBoundsDouble() {
    DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("Double col", DoubleCell.TYPE);
    DataTableSpec tableSpec = new DataTableSpec(colSpecCrea.createSpec());
    RowKey rowKey = new RowKey("Row0");
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, false);
    // initially bounds are null
    DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is(nullValue()));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is(nullValue()));
    // NaN values are ignored completely
    domainCreator.updateDomain(new DefaultRow(rowKey, Double.NaN));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is(nullValue()));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is(nullValue()));
    // missing cells are also ignored
    domainCreator.updateDomain(new DefaultRow(rowKey, DataType.getMissingCell()));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is(nullValue()));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is(nullValue()));
    // change lower and upper bound
    domainCreator.updateDomain(new DefaultRow(rowKey, 0.0));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(0)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(0)));
    // change upper bound
    domainCreator.updateDomain(new DefaultRow(rowKey, 1.0));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(0)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(1)));
    // change lower bound
    domainCreator.updateDomain(new DefaultRow(rowKey, -1.0));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(-1)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(1)));
    // ignore NaN (again)
    domainCreator.updateDomain(new DefaultRow(rowKey, Double.NaN));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(-1)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(1)));
    // ignore missing values (again)
    domainCreator.updateDomain(new DefaultRow(rowKey, DataType.getMissingCell()));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(-1)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(1)));
    // change lower bound to -Inf
    domainCreator.updateDomain(new DefaultRow(rowKey, Double.NEGATIVE_INFINITY));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(Double.NEGATIVE_INFINITY)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(1)));
    // change upper bound to +Inf
    domainCreator.updateDomain(new DefaultRow(rowKey, Double.POSITIVE_INFINITY));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new DoubleCell(Double.NEGATIVE_INFINITY)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new DoubleCell(Double.POSITIVE_INFINITY)));
}
Also used : DoubleCell(org.knime.core.data.def.DoubleCell) DefaultRow(org.knime.core.data.def.DefaultRow) Test(org.junit.Test)

Example 53 with DefaultRow

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

the class DataTableDomainCreatorTest method testInitBounds.

/**
 * Checks whether bounds are initialized correctly if requested.
 */
@Test
public void testInitBounds() {
    DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("Int col", IntCell.TYPE);
    DataColumnDomainCreator domainCrea = new DataColumnDomainCreator();
    domainCrea.setLowerBound(new IntCell(-2));
    domainCrea.setUpperBound(new IntCell(2));
    colSpecCrea.setDomain(domainCrea.createDomain());
    DataColumnSpec intColSpec = colSpecCrea.createSpec();
    DataTableSpec tableSpec = new DataTableSpec(intColSpec);
    RowKey rowKey = new RowKey("Row0");
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, true);
    // check initialized bounds
    DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-2)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(2)));
    domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(1)));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-2)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(2)));
    domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(3)));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-2)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(3)));
    domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(-3)));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-3)));
    assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(3)));
}
Also used : DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 54 with DefaultRow

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

the class DataTableDomainCreatorTest method testInitValues.

/**
 * Checks whether possible values are initialized correctly if requested.
 */
@Test
public void testInitValues() {
    DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("String col", StringCell.TYPE);
    DataColumnDomainCreator domainCrea = new DataColumnDomainCreator();
    domainCrea.setValues(Collections.singleton(new StringCell("v99")));
    colSpecCrea.setDomain(domainCrea.createDomain());
    DataColumnSpec stringColSpec = colSpecCrea.createSpec();
    DataTableSpec tableSpec = new DataTableSpec(stringColSpec);
    RowKey rowKey = new RowKey("Row0");
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, true);
    domainCreator.setMaxPossibleValues(2);
    // check initial values
    Set<DataCell> expectedValues = new LinkedHashSet<>();
    expectedValues.add(new StringCell("v99"));
    DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // add two values
    expectedValues.add(new StringCell("v1"));
    domainCreator.updateDomain(new DefaultRow(rowKey, "v1"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // check whether a initial set of more than 60 possible values is retained if no new possible values
    // appear in the data
    domainCrea = new DataColumnDomainCreator();
    Set<DataCell> initialValues = new HashSet<>();
    for (int i = 0; i < 100; i++) {
        initialValues.add(new StringCell(Integer.toString(i)));
    }
    domainCrea.setValues(initialValues);
    colSpecCrea.setDomain(domainCrea.createDomain());
    stringColSpec = colSpecCrea.createSpec();
    tableSpec = new DataTableSpec(stringColSpec);
    domainCreator = new DataTableDomainCreator(tableSpec, true);
    domainCreator.setMaxPossibleValues(60);
    // check initial values
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(initialValues));
    // add already existing value
    domainCreator.updateDomain(new DefaultRow(rowKey, "2"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(initialValues));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringCell(org.knime.core.data.def.StringCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 55 with DefaultRow

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

the class DataTableDomainCreatorTest method testPossibleValues.

/**
 * Checks whether possible values are computed correctly.
 */
@Test
public void testPossibleValues() {
    DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("String col", StringCell.TYPE);
    DataTableSpec tableSpec = new DataTableSpec(colSpecCrea.createSpec());
    RowKey rowKey = new RowKey("Row0");
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, false);
    domainCreator.setMaxPossibleValues(2);
    // initially no values
    Set<DataCell> expectedValues = new LinkedHashSet<>();
    DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // add two values
    expectedValues.add(new StringCell("v1"));
    domainCreator.updateDomain(new DefaultRow(rowKey, "v1"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    expectedValues.add(new StringCell("v2"));
    domainCreator.updateDomain(new DefaultRow(rowKey, "v2"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // add more than the maximum number removes all values
    domainCreator.updateDomain(new DefaultRow(rowKey, "v3"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(nullValue()));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringCell(org.knime.core.data.def.StringCell) 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