Search in sources :

Example 66 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator 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 67 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class AbstractPlotter method createNominalYCoordinate.

/**
 * Creates a nominal y axis.
 *
 * @param values the possible values.
 */
public void createNominalYCoordinate(final Set<DataCell> values) {
    DataColumnDomainCreator domainCreator = new DataColumnDomainCreator();
    domainCreator.setValues(values);
    DataColumnSpecCreator specCreator = new DataColumnSpecCreator("X", StringCell.TYPE);
    specCreator.setDomain(domainCreator.createDomain());
    Coordinate nominalCoordinate = Coordinate.createCoordinate(specCreator.createSpec());
    if (getYAxis() == null) {
        Axis yAxis = new Axis(Axis.VERTICAL, getDrawingPaneDimension().width);
        setYAxis(yAxis);
    }
    getYAxis().setCoordinate(nominalCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 68 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class AbstractPlotter method createNominalXCoordinate.

/**
 * Creates a nominal x axis.
 *
 * @param values the possible values.
 */
public void createNominalXCoordinate(final Set<DataCell> values) {
    DataColumnDomainCreator domainCreator = new DataColumnDomainCreator();
    domainCreator.setValues(values);
    DataColumnSpecCreator specCreator = new DataColumnSpecCreator("X", StringCell.TYPE);
    specCreator.setDomain(domainCreator.createDomain());
    Coordinate nominalCoordinate = Coordinate.createCoordinate(specCreator.createSpec());
    if (getXAxis() == null) {
        Axis xAxis = new Axis(Axis.HORIZONTAL, getDrawingPaneDimension().width);
        setXAxis(xAxis);
    }
    getXAxis().setCoordinate(nominalCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 69 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class AbstractPlotter method createXCoordinate.

/**
 * Recalculates the domain of the x axis. If preserve is set to false the
 * passed values are taken as min and max no matter was was set before. If
 * preserve is set to true (default) the possibly already available min and
 * max values are preserved.
 *
 * @param min the min value
 * @param max the max value {@link AbstractPlotter#setPreserve(boolean)}
 */
public void createXCoordinate(final double min, final double max) {
    DataColumnDomainCreator xDomainCreator = new DataColumnDomainCreator();
    double actualMin = min;
    double actualMax = max;
    if (getXAxis() != null && getXAxis().getCoordinate() != null && m_preserve) {
        if (!(getXAxis().getCoordinate() instanceof NumericCoordinate)) {
            return;
        }
        actualMin = Math.min(min, ((NumericCoordinate) getXAxis().getCoordinate()).getMinDomainValue());
        actualMax = Math.max(max, ((NumericCoordinate) getXAxis().getCoordinate()).getMaxDomainValue());
    }
    xDomainCreator.setLowerBound(new DoubleCell(actualMin));
    xDomainCreator.setUpperBound(new DoubleCell(actualMax));
    DataColumnSpecCreator xSpecCreator = new DataColumnSpecCreator("X", DoubleCell.TYPE);
    xSpecCreator.setDomain(xDomainCreator.createDomain());
    Coordinate xCoordinate = Coordinate.createCoordinate(xSpecCreator.createSpec());
    if (getXAxis() == null) {
        Axis xAxis = new Axis(Axis.HORIZONTAL, getDrawingPaneDimension().width);
        setXAxis(xAxis);
    }
    getXAxis().setCoordinate(xCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DoubleCell(org.knime.core.data.def.DoubleCell) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 70 with DataColumnSpecCreator

use of org.knime.core.data.DataColumnSpecCreator in project knime-core by knime.

the class AbstractPlotter method createYCoordinate.

/**
 * Recalculates the domain of the y axis. If preserve is set to false the
 * passed values are taken as min and max no matter was was set before. If
 * preserve is set to true (default) the possibly already available min and
 * max values are preserved.
 *
 * @param min the min value
 * @param max the max value {@link AbstractPlotter#setPreserve(boolean)}
 */
public void createYCoordinate(final double min, final double max) {
    DataColumnDomainCreator yDomainCreator = new DataColumnDomainCreator();
    double actualMin = min;
    double actualMax = max;
    if (getYAxis() != null && getYAxis().getCoordinate() != null && m_preserve) {
        if (!(getYAxis().getCoordinate() instanceof NumericCoordinate)) {
            return;
        }
        actualMin = Math.min(min, ((NumericCoordinate) getYAxis().getCoordinate()).getMinDomainValue());
        actualMax = Math.max(max, ((NumericCoordinate) getYAxis().getCoordinate()).getMaxDomainValue());
    }
    yDomainCreator.setLowerBound(new DoubleCell(actualMin));
    yDomainCreator.setUpperBound(new DoubleCell(actualMax));
    DataColumnSpecCreator ySpecCreator = new DataColumnSpecCreator("Y", DoubleCell.TYPE);
    ySpecCreator.setDomain(yDomainCreator.createDomain());
    Coordinate yCoordinate = Coordinate.createCoordinate(ySpecCreator.createSpec());
    if (getYAxis() == null) {
        Axis yAxis = new Axis(Axis.VERTICAL, getDrawingPaneDimension().height);
        setYAxis(yAxis);
    }
    getYAxis().setCoordinate(yCoordinate);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Coordinate(org.knime.base.util.coordinate.Coordinate) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DoubleCell(org.knime.core.data.def.DoubleCell) NumericCoordinate(org.knime.base.util.coordinate.NumericCoordinate) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Aggregations

DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)267 DataColumnSpec (org.knime.core.data.DataColumnSpec)210 DataTableSpec (org.knime.core.data.DataTableSpec)132 DataCell (org.knime.core.data.DataCell)92 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)77 DataType (org.knime.core.data.DataType)74 DataRow (org.knime.core.data.DataRow)73 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)57 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)51 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)48 ArrayList (java.util.ArrayList)46 DoubleCell (org.knime.core.data.def.DoubleCell)45 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)44 StringCell (org.knime.core.data.def.StringCell)29 BufferedDataTable (org.knime.core.node.BufferedDataTable)23 DoubleValue (org.knime.core.data.DoubleValue)22 HashSet (java.util.HashSet)19 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)17 DataColumnDomain (org.knime.core.data.DataColumnDomain)16 DefaultRow (org.knime.core.data.def.DefaultRow)16