Search in sources :

Example 6 with DataArray

use of org.knime.base.node.util.DataArray in project knime-core by knime.

the class PolyRegLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File f = new File(nodeInternDir, "data.zip");
    final DataArray rowContainer;
    if (f.exists()) {
        ContainerTable t = DataContainer.readFromZip(f);
        int rowCount = t.getRowCount();
        rowContainer = new DefaultDataArray(t, 1, rowCount, exec);
    } else {
        throw new FileNotFoundException("Internals do not exist");
    }
    f = new File(nodeInternDir, "internals.xml");
    if (f.exists()) {
        NodeSettingsRO internals = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(f)));
        try {
            double[] betas = internals.getDoubleArray("betas");
            String[] columnNames = internals.getStringArray("columnNames");
            double squaredError = internals.getDouble("squaredError");
            double adjustedR2 = internals.getDouble("adjustedSquaredError", Double.NaN);
            double[] meanValues = internals.getDoubleArray("meanValues");
            double[] emptyArray = new double[betas.length];
            Arrays.fill(emptyArray, Double.NaN);
            double[] stdErrs = internals.getDoubleArray("stdErrors", emptyArray);
            double[] tValues = internals.getDoubleArray("tValues", emptyArray);
            double[] pValues = internals.getDoubleArray("pValues", emptyArray);
            m_viewData = new PolyRegViewData(meanValues, betas, stdErrs, tValues, pValues, squaredError, adjustedR2, columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        } catch (InvalidSettingsException ex) {
            throw new IOException("Old or corrupt internals", ex);
        }
    } else {
        throw new FileNotFoundException("Internals do not exist");
    }
}
Also used : DefaultDataArray(org.knime.base.node.util.DefaultDataArray) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataArray(org.knime.base.node.util.DataArray) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ContainerTable(org.knime.core.data.container.ContainerTable) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File)

Example 7 with DataArray

use of org.knime.base.node.util.DataArray in project knime-core by knime.

the class PolyRegLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    final int colCount = inSpec.getNumColumns();
    String[] selectedCols = computeSelectedColumns(inSpec);
    Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
    m_colSelected = new boolean[colCount];
    for (int i = 0; i < colCount; i++) {
        m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
    }
    final int rowCount = inTable.getRowCount();
    String[] temp = new String[m_columnNames.length + 1];
    System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
    temp[temp.length - 1] = m_settings.getTargetColumn();
    FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
    final DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
    PortObjectSpec[] outputSpec = configure((inPMMLPort == null) ? new PortObjectSpec[] { inData[0].getSpec(), null } : new PortObjectSpec[] { inData[0].getSpec(), inPMMLPort.getSpec() });
    Learner learner = new Learner((PMMLPortObjectSpec) outputSpec[0], 0d, m_settings.getMissingValueHandling() == MissingValueHandling.fail, m_settings.getDegree());
    try {
        PolyRegContent polyRegContent = learner.perform(inTable, exec);
        m_betas = fillBeta(polyRegContent);
        m_meanValues = polyRegContent.getMeans();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        PortObject[] bdt = new PortObject[] { createPMMLModel(inPMMLPort, inSpec), exec.createColumnRearrangeTable(inTable, crea, exec.createSilentSubExecutionContext(.2)), polyRegContent.createTablePortObject(exec.createSubExecutionContext(0.2)) };
        m_squaredError /= rowCount;
        if (polyRegContent.getWarningMessage() != null) {
            setWarningMessage(polyRegContent.getWarningMessage());
        }
        double[] stdErrors = PolyRegViewData.mapToArray(polyRegContent.getStandardErrors(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptStdErr());
        double[] tValues = PolyRegViewData.mapToArray(polyRegContent.getTValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptTValue());
        double[] pValues = PolyRegViewData.mapToArray(polyRegContent.getPValues(), m_columnNames, m_settings.getDegree(), polyRegContent.getInterceptPValue());
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, stdErrors, tValues, pValues, m_squaredError, polyRegContent.getAdjustedRSquared(), m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        return bdt;
    } catch (ModelSpecificationException e) {
        final String origWarning = getWarningMessage();
        final String warning = (origWarning != null && !origWarning.isEmpty()) ? (origWarning + "\n") : "" + e.getMessage();
        setWarningMessage(warning);
        final ExecutionContext subExec = exec.createSubExecutionContext(.1);
        final BufferedDataContainer empty = subExec.createDataContainer(STATS_SPEC);
        int rowIdx = 1;
        for (final String column : m_columnNames) {
            for (int d = 1; d <= m_settings.getDegree(); ++d) {
                empty.addRowToTable(new DefaultRow("Row" + rowIdx++, new StringCell(column), new IntCell(d), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
            }
        }
        empty.addRowToTable(new DefaultRow("Row" + rowIdx, new StringCell("Intercept"), new IntCell(0), new DoubleCell(0.0d), DataType.getMissingCell(), DataType.getMissingCell(), DataType.getMissingCell()));
        double[] nans = new double[m_columnNames.length * m_settings.getDegree() + 1];
        Arrays.fill(nans, Double.NaN);
        m_betas = new double[nans.length];
        // Mean only for the linear tags
        m_meanValues = new double[nans.length / m_settings.getDegree()];
        m_viewData = new PolyRegViewData(m_meanValues, m_betas, nans, nans, nans, m_squaredError, Double.NaN, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn(), rowContainer);
        empty.close();
        ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
        crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
        BufferedDataTable rearrangerTable = exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6));
        PMMLPortObject model = createPMMLModel(inPMMLPort, inTable.getDataTableSpec());
        PortObject[] bdt = new PortObject[] { model, rearrangerTable, empty.getTable() };
        return bdt;
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) DoubleCell(org.knime.core.data.def.DoubleCell) FilterColumnTable(org.knime.base.data.filter.column.FilterColumnTable) DataArray(org.knime.base.node.util.DataArray) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ModelSpecificationException(org.apache.commons.math3.stat.regression.ModelSpecificationException) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) HashSet(java.util.HashSet) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StringCell(org.knime.core.data.def.StringCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 8 with DataArray

use of org.knime.base.node.util.DataArray in project knime-core by knime.

the class HierarchicalClusterNodeView method modelChanged.

/**
 * {@inheritDoc}
 */
@Override
public void modelChanged() {
    if (getNodeModel() == null || ((DataProvider) getNodeModel()).getDataArray(0) == null || ((DataProvider) getNodeModel()).getDataArray(0).size() == 0) {
        return;
    }
    NodeModel model = getNodeModel();
    m_dendroPlotter.reset();
    m_distancePlotter.reset();
    m_dendroPlotter.setHiLiteHandler(model.getInHiLiteHandler(0));
    m_dendroPlotter.setAntialiasing(false);
    m_dendroPlotter.setDataProvider((DataProvider) model);
    m_distancePlotter.setDataProvider((DataProvider) model);
    m_distancePlotter.setHiLiteHandler(model.getInHiLiteHandler(0));
    DendrogramNode rootNode = getNodeModel().getRootNode();
    DataArray distanceTable = ((DataProvider) getNodeModel()).getDataArray(0);
    m_dendroPlotter.setRootNode(rootNode);
    m_distancePlotter.createXCoordinate(((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(0).getDomain().getLowerBound()).getDoubleValue(), ((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(0).getDomain().getUpperBound()).getDoubleValue());
    m_distancePlotter.createYCoordinate(((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(1).getDomain().getLowerBound()).getDoubleValue(), ((DoubleValue) distanceTable.getDataTableSpec().getColumnSpec(1).getDomain().getUpperBound()).getDoubleValue());
    ((BasicDrawingPane) m_distancePlotter.getDrawingPane()).clearPlot();
    m_distancePlotter.addLine(distanceTable, 0, 1, Color.BLACK, new BasicStroke(m_thickness));
    // m_distancePlotter.getXAxis().getCoordinate().setPolicy(
    // DescendingNumericTickPolicyStrategy.getInstance());
    m_distancePlotter.updatePaintModel();
    m_dendroPlotter.updatePaintModel();
}
Also used : DataProvider(org.knime.base.node.viz.plotter.DataProvider) BasicStroke(java.awt.BasicStroke) NodeModel(org.knime.core.node.NodeModel) BasicDrawingPane(org.knime.base.node.viz.plotter.basic.BasicDrawingPane) DendrogramNode(org.knime.base.node.viz.plotter.dendrogram.DendrogramNode) DataArray(org.knime.base.node.util.DataArray)

Example 9 with DataArray

use of org.knime.base.node.util.DataArray in project knime-core by knime.

the class LinePlotter method updatePaintModel.

/**
 * Creates the color mapping by dividing the hue circle of the HSB color
 * model by the number of selected columns, calculates the coordinates by
 * determining the overall minimum and maximum values of the selected
 * columns and maps the data points to the resulting screen coordinates.
 *
 * @see org.knime.base.node.viz.plotter.AbstractPlotter#updatePaintModel()
 */
@Override
public void updatePaintModel() {
    if (getDataProvider() != null && getDataProvider().getDataArray(getDataArrayIdx()) != null) {
        // draw the points and add the lines
        DataArray array = getDataProvider().getDataArray(getDataArrayIdx());
        DataTableSpec spec = array.getDataTableSpec();
        if (m_columnNames == null) {
            initColumnNames(array);
            initializeColors(spec);
        }
        // set only displayed columns for the color mapping in the
        // color legend and drawing pane....
        Map<String, Color> displayedColors = new LinkedHashMap<String, Color>();
        // color mapping is initially defined for all columns
        for (String colName : m_columnNames) {
            Color c = m_colorMapping.get(colName);
            displayedColors.put(colName, c);
        }
        displayedColors.keySet().retainAll(m_columnNames);
        calculateCoordinates(array);
        calculateDots();
        // if we have line plotter properties update them
        if (getProperties() instanceof LinePlotterProperties) {
            ((LinePlotterProperties) getProperties()).updateColumnSelection(array.getDataTableSpec(), m_columnNames);
            ((LinePlotterProperties) getProperties()).updateColorLegend(displayedColors);
        }
        getDrawingPane().repaint();
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Color(java.awt.Color) DataArray(org.knime.base.node.util.DataArray) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with DataArray

use of org.knime.base.node.util.DataArray in project knime-core by knime.

the class LinePlotter method calculateDots.

/**
 * Calculates the screen coordinates (dots) for the lines and puts them in a
 * large {@link org.knime.base.node.viz.plotter.scatter.DotInfoArray}, which
 * is passed to the
 * {@link org.knime.base.node.viz.plotter.line.LinePlotterDrawingPane}.
 */
protected void calculateDots() {
    if (!(getDrawingPane() instanceof ScatterPlotterDrawingPane)) {
        return;
    }
    if (m_columnNames == null) {
        return;
    }
    if (getDataProvider() != null && getDataProvider().getDataArray(getDataArrayIdx()) != null) {
        DataArray array = getDataProvider().getDataArray(getDataArrayIdx());
        int nrOfRows = array.size();
        // set the empty dots to delete the old ones
        // if we have no columns to display
        ((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(new DotInfo[0]));
        // first store them in a list to avoid keep tracking of indices
        List<DotInfo> dotList = new ArrayList<DotInfo>();
        for (String col : m_columnNames) {
            int colIdx = array.getDataTableSpec().findColumnIndex(col);
            Color c = m_colorMapping.get(col);
            if (c == null) {
                c = Color.black;
            }
            ColorAttr color = ColorAttr.getInstance(c);
            // store the last point with valid value for interpolation
            Point p1 = new Point(-1, -1);
            Point p2;
            List<DotInfo> missingValues = new ArrayList<DotInfo>();
            // create the dots
            for (int row = 0; row < nrOfRows; row++) {
                DataCell cell = array.getRow(row).getCell(colIdx);
                int y = -1;
                DotInfo dot;
                int x = getMappedXValue(new StringCell(array.getRow(row).getKey().getString()));
                if (!cell.isMissing()) {
                    y = getMappedYValue(cell);
                    if (missingValues.size() > 0) {
                        // we have some missing values in between,
                        // thus we have to interpolate
                        p2 = new Point(x, y);
                        DotInfo[] interpolated = interpolate(p1, p2, missingValues);
                        // and add them
                        for (DotInfo p : interpolated) {
                            dotList.add(p);
                        }
                        // and clear the list again
                        missingValues.clear();
                    }
                    p1 = new Point(x, y);
                    dot = new DotInfo(x, y, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    dot.setXDomainValue(new StringCell(array.getRow(row).getKey().getString()));
                    dot.setYDomainValue(cell);
                    dotList.add(dot);
                } else if (!m_interpolate) {
                    // LOGGER.debug("missing value");
                    dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    dotList.add(dot);
                } else {
                    // interpolate
                    dot = new DotInfo(x, -1, array.getRow(row).getKey(), delegateIsHiLit(array.getRow(row).getKey()), color, 1, row);
                    missingValues.add(dot);
                }
            }
            // un-interpolated at the end, we add them anyway
            if (!missingValues.isEmpty()) {
                DotInfo[] interpolated = interpolate(p1, null, missingValues);
                // and add them
                for (DotInfo p : interpolated) {
                    dotList.add(p);
                }
                // and clear the list again
                missingValues.clear();
            }
        }
        DotInfo[] dots = new DotInfo[dotList.size()];
        dotList.toArray(dots);
        ((LinePlotterDrawingPane) getDrawingPane()).setNumberOfLines(nrOfRows);
        ((ScatterPlotterDrawingPane) getDrawingPane()).setDotInfoArray(new DotInfoArray(dots));
    }
}
Also used : DotInfo(org.knime.base.node.viz.plotter.scatter.DotInfo) Color(java.awt.Color) ArrayList(java.util.ArrayList) ColorAttr(org.knime.core.data.property.ColorAttr) Point(java.awt.Point) ScatterPlotterDrawingPane(org.knime.base.node.viz.plotter.scatter.ScatterPlotterDrawingPane) DataArray(org.knime.base.node.util.DataArray) Point(java.awt.Point) StringCell(org.knime.core.data.def.StringCell) DotInfoArray(org.knime.base.node.viz.plotter.scatter.DotInfoArray) DataCell(org.knime.core.data.DataCell)

Aggregations

DataArray (org.knime.base.node.util.DataArray)23 DataTableSpec (org.knime.core.data.DataTableSpec)8 DataRow (org.knime.core.data.DataRow)7 DefaultDataArray (org.knime.base.node.util.DefaultDataArray)6 DataCell (org.knime.core.data.DataCell)6 Point (java.awt.Point)5 ArrayList (java.util.ArrayList)5 DataProvider (org.knime.base.node.viz.plotter.DataProvider)5 StringCell (org.knime.core.data.def.StringCell)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 Color (java.awt.Color)3 ColorAttr (org.knime.core.data.property.ColorAttr)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 FilterColumnTable (org.knime.base.data.filter.column.FilterColumnTable)2 LinearRegressionContent (org.knime.base.node.mine.regression.linear.LinearRegressionContent)2 DotInfo (org.knime.base.node.viz.plotter.scatter.DotInfo)2