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() };
}
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);
}
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);
}
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);
}
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);
}
Aggregations