Search in sources :

Example 11 with DefaultDataArray

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

the class Rule2DPlotter method updatePaintModel.

/**
 * {@inheritDoc}
 */
@Override
protected void updatePaintModel() {
    super.updatePaintModel();
    if (m_rules != null) {
        Rule2DDrawingPane drawingPane = getDrawingPane();
        drawingPane.setOriginalRuleTable(m_rules);
        String xName = getXColName();
        String yName = getYColName();
        int xIdx = -1;
        int yIdx = -1;
        if (xName != null && yName != null) {
            xIdx = m_rules.getDataTableSpec().findColumnIndex(xName);
            yIdx = m_rules.getDataTableSpec().findColumnIndex(yName);
        }
        if (xIdx >= 0 && yIdx >= 0) {
            Coordinate x = getColHeader().getCoordinate();
            Coordinate y = getRowHeader().getCoordinate();
            // check if the coordinates are valid
            if (x == null || y == null) {
                return;
            }
            // calculate the coordinates of the rules here
            // List<DataRow> rows = new ArrayList<DataRow>();
            DataColumnSpecCreator creator = new DataColumnSpecCreator("xValues", FuzzyIntervalCell.TYPE);
            DataColumnSpec col1 = creator.createSpec();
            creator = new DataColumnSpecCreator("yValues", FuzzyIntervalCell.TYPE);
            DataColumnSpec col2 = creator.createSpec();
            DataTableSpec spec = new DataTableSpec(new DataColumnSpec[] { col1, col2 });
            DataContainer rows = new DataContainer(spec);
            for (RowIterator itr = m_rules.iterator(); itr.hasNext(); ) {
                DataRow currRow = itr.next();
                DataCell[] newCells = new DataCell[2];
                for (int cell = 0; cell < currRow.getNumCells(); cell++) {
                    // if (!m_rules.getDataTableSpec().getColumnSpec(cell)
                    // .getType().isCompatible(
                    // FuzzyIntervalValue.class)) {
                    // continue;
                    // }
                    Rectangle rect = calculateDrawingRectangle();
                    double a;
                    double b;
                    double c;
                    double d;
                    if (cell == xIdx) {
                        if (currRow.getCell(cell).isMissing()) {
                            // normalize xValues
                            a = getXmin();
                            b = getXmin();
                            c = getXmax();
                            d = getXmax();
                        } else {
                            // normalize xValues
                            a = ((FuzzyIntervalValue) currRow.getCell(cell)).getMinSupport();
                            b = ((FuzzyIntervalValue) currRow.getCell(cell)).getMinCore();
                            c = ((FuzzyIntervalValue) currRow.getCell(cell)).getMaxCore();
                            d = ((FuzzyIntervalValue) currRow.getCell(cell)).getMaxSupport();
                        }
                        double newA = x.calculateMappedValue(new DoubleCell(a), rect.width, true);
                        double newB = x.calculateMappedValue(new DoubleCell(b), rect.width, true);
                        double newC = x.calculateMappedValue(new DoubleCell(c), rect.width, true);
                        double newD = x.calculateMappedValue(new DoubleCell(d), rect.width, true);
                        DataCell newInterval = new FuzzyIntervalCell(rect.x + newA, rect.x + newB, rect.x + newC, rect.x + newD);
                        newCells[0] = newInterval;
                    }
                    if (cell == yIdx) {
                        if (currRow.getCell(cell).isMissing()) {
                            a = getYmin();
                            b = getYmin();
                            c = getYmax();
                            d = getYmax();
                        } else {
                            // normalize yValues
                            a = ((FuzzyIntervalValue) currRow.getCell(cell)).getMinSupport();
                            b = ((FuzzyIntervalValue) currRow.getCell(cell)).getMinCore();
                            c = ((FuzzyIntervalValue) currRow.getCell(cell)).getMaxCore();
                            d = ((FuzzyIntervalValue) currRow.getCell(cell)).getMaxSupport();
                        }
                        double newA = y.calculateMappedValue(new DoubleCell(a), rect.height, true);
                        double newB = y.calculateMappedValue(new DoubleCell(b), rect.height, true);
                        double newC = y.calculateMappedValue(new DoubleCell(c), rect.height, true);
                        double newD = y.calculateMappedValue(new DoubleCell(d), rect.height, true);
                        DataCell newInterval = new FuzzyIntervalCell(rect.y + rect.height - newD, rect.y + rect.height - newC, rect.y + rect.height - newB, rect.y + rect.height - newA);
                        newCells[1] = newInterval;
                    }
                }
                // create new row out of the normalized cells
                rows.addRowToTable(new DefaultRow(currRow.getKey(), newCells));
            }
            rows.close();
            drawingPane.setNormalizedRules(new DefaultDataArray(rows.getTable(), 1, m_rules.size()));
        }
        super.updatePaintModel();
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) Rectangle(java.awt.Rectangle) DataRow(org.knime.core.data.DataRow) FuzzyIntervalCell(org.knime.core.data.def.FuzzyIntervalCell) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) Coordinate(org.knime.base.util.coordinate.Coordinate) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 12 with DefaultDataArray

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

the class ConditionalBoxPlotNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    try {
        File f = new File(nodeInternDir, "conditionalBoxPlotInternals");
        FileInputStream fis = new FileInputStream(f);
        NodeSettingsRO settings = NodeSettings.loadFromXML(fis);
        fis.close();
        m_statistics = new LinkedHashMap<DataColumnSpec, double[]>();
        m_mildOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
        m_extremeOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
        /* Load the numerical column spec if available.*/
        if (settings.containsKey("numColSpec")) {
            m_numColSpec = DataColumnSpec.load(settings.getConfig("numColSpec"));
        }
        int nrOfCols = settings.getInt("nrOfCols");
        for (int i = 0; i < nrOfCols; i++) {
            DataColumnSpec spec = DataColumnSpec.load(settings.getConfig("col" + i));
            String colName = spec.getName();
            double[] stats = settings.getDoubleArray("stats" + colName);
            m_statistics.put(spec, stats);
            double[] mild = settings.getDoubleArray("mild" + colName);
            Map<Double, Set<RowKey>> mildmap = new LinkedHashMap<Double, Set<RowKey>>();
            for (int j = 0; j < mild.length; j++) {
                Set<RowKey> set = new HashSet<RowKey>();
                String[] mildKeys = settings.getStringArray("mildKeys" + colName + j);
                for (int k = 0; k < mildKeys.length; k++) {
                    set.add(new RowKey(mildKeys[k]));
                }
                mildmap.put(mild[j], set);
            }
            m_mildOutliers.put(colName, mildmap);
            double[] extr = settings.getDoubleArray("extreme" + colName);
            Map<Double, Set<RowKey>> extrmap = new LinkedHashMap<Double, Set<RowKey>>();
            for (int j = 0; j < extr.length; j++) {
                Set<RowKey> set = new HashSet<RowKey>();
                String[] extrKeys = settings.getStringArray("extremeKeys" + colName + j);
                for (int k = 0; k < extrKeys.length; k++) {
                    set.add(new RowKey(extrKeys[k]));
                }
                extrmap.put(extr[j], set);
            }
            m_extremeOutliers.put(colName, extrmap);
        }
        File dataFile = new File(nodeInternDir, "conditionalBoxPlotDataFile");
        ContainerTable table = DataContainer.readFromZip(dataFile);
        m_dataArray = new DefaultDataArray(table, 1, 2, exec);
    } catch (Exception e) {
        throw new IOException("Unable to load internals: " + e.getMessage(), e);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RowKey(org.knime.core.data.RowKey) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 13 with DefaultDataArray

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

the class InteractiveHistogramDataModel method loadFromFile.

/**
 * @param dataDir the data directory to read from
 * @param exec the {@link ExecutionMonitor}
 * @return the {@link InteractiveHistogramDataModel}
 * @throws IOException if the file is invalid
 * @throws InvalidSettingsException if a setting is invalid
 * @throws CanceledExecutionException if the process was canceled
 */
public static InteractiveHistogramDataModel loadFromFile(final File dataDir, final ExecutionMonitor exec) throws IOException, InvalidSettingsException, CanceledExecutionException {
    final File settingFile = new File(dataDir, CFG_SETTING_FILE);
    final FileInputStream is = new FileInputStream(settingFile);
    final GZIPInputStream inData = new GZIPInputStream(is);
    final ConfigRO config = NodeSettings.loadFromXML(inData);
    final ConfigRO colorColsConf = config.getConfig(CFG_COLOR_COLS);
    final int counter = colorColsConf.getInt(CFG_ROW_COLOR_COUNTER);
    final List<Color> rowColors = new ArrayList<Color>();
    for (int i = 0; i < counter; i++) {
        rowColors.add(new Color(colorColsConf.getInt(CFG_ROW_COLOR + i)));
    }
    exec.checkCanceled();
    final File dataFile = new File(dataDir, CFG_DATA_FILE);
    final ContainerTable table = DataContainer.readFromZip(dataFile);
    final int rowCount = table.getRowCount();
    final DefaultDataArray dataArray = new DefaultDataArray(table, 1, rowCount, exec);
    return new InteractiveHistogramDataModel(dataArray, rowColors);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) Color(java.awt.Color) ArrayList(java.util.ArrayList) ConfigRO(org.knime.core.node.config.ConfigRO) File(java.io.File) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable)

Example 14 with DefaultDataArray

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

the class InteractivePieDataModel method loadFromFile.

/**
 * @param dataDir the data directory to read from
 * @param exec {@link ExecutionMonitor}
 * @return the {@link InteractivePieDataModel}
 * @throws IOException if the file could not be read
 * @throws InvalidSettingsException if a setting wasn't present
 * @throws CanceledExecutionException if the operation was canceled
 */
public static InteractivePieDataModel loadFromFile(final File dataDir, final ExecutionMonitor exec) throws IOException, InvalidSettingsException, CanceledExecutionException {
    final File settingFile = new File(dataDir, CFG_SETTING_FILE);
    final FileInputStream is = new FileInputStream(settingFile);
    final GZIPInputStream inData = new GZIPInputStream(is);
    final ConfigRO config = NodeSettings.loadFromXML(inData);
    final boolean supportHiliting = config.getBoolean(CFG_HILITING);
    final boolean detailsAvailable = config.getBoolean(CFG_DETAILS);
    exec.checkCanceled();
    final File dataFile = new File(dataDir, CFG_DATA_FILE);
    final ContainerTable table = DataContainer.readFromZip(dataFile);
    final int rowCount = table.getRowCount();
    final DefaultDataArray dataArray = new DefaultDataArray(table, 1, rowCount, exec);
    return new InteractivePieDataModel(dataArray, detailsAvailable, supportHiliting);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ConfigRO(org.knime.core.node.config.ConfigRO) File(java.io.File) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable)

Example 15 with DefaultDataArray

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

the class BoxPlotNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    if (inData[0] == null) {
        return new BufferedDataTable[] {};
    }
    BufferedDataTable table = inData[0];
    m_statistics = new LinkedHashMap<DataColumnSpec, double[]>();
    m_mildOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
    m_extremeOutliers = new LinkedHashMap<String, Map<Double, Set<RowKey>>>();
    int colIdx = 0;
    List<DataColumnSpec> outputColSpecs = new ArrayList<DataColumnSpec>();
    double subProgress = 1.0 / getNumNumericColumns(table.getDataTableSpec());
    for (DataColumnSpec colSpec : table.getDataTableSpec()) {
        ExecutionContext colExec = exec.createSubExecutionContext(subProgress);
        exec.checkCanceled();
        if (colSpec.getType().isCompatible(DoubleValue.class)) {
            double[] statistic = new double[SIZE];
            outputColSpecs.add(colSpec);
            List<String> col = new ArrayList<String>();
            col.add(colSpec.getName());
            ExecutionContext sortExec = colExec.createSubExecutionContext(0.75);
            ExecutionContext findExec = colExec.createSubExecutionContext(0.25);
            SortedTable sorted = new SortedTable(table, col, new boolean[] { true }, sortExec);
            long currRowAbsolute = 0;
            int currCountingRow = 1;
            double lastValue = 1;
            long nrOfRows = table.size();
            boolean first = true;
            for (DataRow row : sorted) {
                exec.checkCanceled();
                double rowProgress = currRowAbsolute / (double) table.size();
                findExec.setProgress(rowProgress, "determining statistics for: " + table.getDataTableSpec().getColumnSpec(colIdx).getName());
                if (row.getCell(colIdx).isMissing()) {
                    // asserts that the missing values are sorted at
                    // the top of the table
                    currRowAbsolute++;
                    nrOfRows--;
                    continue;
                }
                // get the first value = actually observed minimum
                if (first) {
                    statistic[MIN] = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                    // initialize the statistics with first value
                    // if the table is large enough it will be overriden
                    // this is just for the case of tables with < 5 rows
                    statistic[MEDIAN] = statistic[MIN];
                    statistic[LOWER_QUARTILE] = statistic[MIN];
                    statistic[UPPER_QUARTILE] = statistic[MIN];
                    first = false;
                }
                // get the last value = actually observed maximum
                if (currRowAbsolute == table.size() - 1) {
                    statistic[MAX] = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                }
                float medianPos = nrOfRows * 0.5f;
                float lowerQuartilePos = nrOfRows * 0.25f;
                float upperQuartilePos = nrOfRows * 0.75f;
                if (currCountingRow == (int) Math.floor(lowerQuartilePos) + 1) {
                    if (lowerQuartilePos % 1 != 0) {
                        // get the row's value
                        statistic[LOWER_QUARTILE] = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                    } else {
                        // calculate the mean between row and last row
                        double value = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                        statistic[LOWER_QUARTILE] = (value + lastValue) / 2;
                    }
                }
                if (currCountingRow == (int) Math.floor(medianPos) + 1) {
                    if (medianPos % 1 != 0) {
                        // get the row's value
                        statistic[MEDIAN] = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                    } else {
                        // calculate the mean between row and last row
                        double value = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                        statistic[MEDIAN] = (value + lastValue) / 2;
                    }
                }
                if (currCountingRow == (int) Math.floor(upperQuartilePos) + 1) {
                    if (upperQuartilePos % 1 != 0) {
                        // get the row's value
                        statistic[UPPER_QUARTILE] = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                    } else {
                        // calculate the mean between row and last row
                        double value = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                        statistic[UPPER_QUARTILE] = (value + lastValue) / 2;
                    }
                }
                lastValue = ((DoubleValue) row.getCell(colIdx)).getDoubleValue();
                currRowAbsolute++;
                currCountingRow++;
            }
            double iqr = statistic[UPPER_QUARTILE] - statistic[LOWER_QUARTILE];
            Map<Double, Set<RowKey>> mild = new LinkedHashMap<Double, Set<RowKey>>();
            Map<Double, Set<RowKey>> extreme = new LinkedHashMap<Double, Set<RowKey>>();
            // per default the whiskers are at min and max
            double[] whiskers = new double[] { statistic[MIN], statistic[MAX] };
            if (statistic[MIN] < (statistic[LOWER_QUARTILE] - (1.5 * iqr)) || statistic[MAX] > statistic[UPPER_QUARTILE] + (1.5 * iqr)) {
                detectOutliers(sorted, iqr, new double[] { statistic[LOWER_QUARTILE], statistic[UPPER_QUARTILE] }, mild, extreme, whiskers, colIdx);
            }
            statistic[LOWER_WHISKER] = whiskers[0];
            statistic[UPPER_WHISKER] = whiskers[1];
            m_mildOutliers.put(colSpec.getName(), mild);
            m_extremeOutliers.put(colSpec.getName(), extreme);
            m_statistics.put(colSpec, statistic);
        }
        colIdx++;
    }
    DataContainer container = createOutputTable(exec, outputColSpecs);
    // return a data array with just one row but with the data table spec
    // for the column selection panel
    m_array = new DefaultDataArray(table, 1, 2);
    return new BufferedDataTable[] { exec.createBufferedDataTable(container.getTable(), exec) };
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RowKey(org.knime.core.data.RowKey) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) DataContainer(org.knime.core.data.container.DataContainer) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) ExecutionContext(org.knime.core.node.ExecutionContext) DoubleValue(org.knime.core.data.DoubleValue) SortedTable(org.knime.base.data.sort.SortedTable) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DefaultDataArray (org.knime.base.node.util.DefaultDataArray)32 BufferedDataTable (org.knime.core.node.BufferedDataTable)16 File (java.io.File)14 ContainerTable (org.knime.core.data.container.ContainerTable)13 DataTableSpec (org.knime.core.data.DataTableSpec)12 FileInputStream (java.io.FileInputStream)10 IOException (java.io.IOException)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 DataRow (org.knime.core.data.DataRow)8 DataColumnSpec (org.knime.core.data.DataColumnSpec)7 HashSet (java.util.HashSet)6 DataArray (org.knime.base.node.util.DataArray)6 DataTable (org.knime.core.data.DataTable)6 Map (java.util.Map)5 RowKey (org.knime.core.data.RowKey)5 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)5 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)5 PortObject (org.knime.core.node.port.PortObject)5 BufferedInputStream (java.io.BufferedInputStream)4 ArrayList (java.util.ArrayList)4