Search in sources :

Example 26 with Pair

use of org.knime.core.util.Pair in project knime-core by knime.

the class CollectionSplitNodeModel method createColumnRearranger.

/**
 * Create rearrange object, setup the table.
 */
private Pair<ColumnRearranger, SplitCellFactory> createColumnRearranger(final DataTableSpec spec, final DataColumnSpec[] newColSpecs) throws InvalidSettingsException {
    int colIndex = getTargetColIndex(spec);
    SplitCellFactory fac = new SplitCellFactory(colIndex, newColSpecs);
    ColumnRearranger arranger = new ColumnRearranger(spec);
    if (m_settings.isReplaceInputColumn()) {
        arranger.remove(colIndex);
        arranger.insertAt(colIndex, fac);
    } else {
        arranger.append(fac);
    }
    return new Pair<ColumnRearranger, SplitCellFactory>(arranger, fac);
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) Pair(org.knime.core.util.Pair)

Example 27 with Pair

use of org.knime.core.util.Pair in project knime-core by knime.

the class AppendVariableToTable2NodeModel method createColumnRearranger.

@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    ColumnRearranger arranger = new ColumnRearranger(spec);
    Set<String> nameHash = new HashSet<String>();
    for (DataColumnSpec c : spec) {
        nameHash.add(c.getName());
    }
    List<Pair<String, FlowVariable.Type>> vars = getVariablesOfInterest();
    if (vars.isEmpty()) {
        throw new InvalidSettingsException("No variables selected");
    }
    DataColumnSpec[] specs = new DataColumnSpec[vars.size()];
    final DataCell[] values = new DataCell[vars.size()];
    for (int i = 0; i < vars.size(); i++) {
        Pair<String, FlowVariable.Type> c = vars.get(i);
        String name = c.getFirst();
        final DataType type;
        switch(c.getSecond()) {
            case DOUBLE:
                type = DoubleCell.TYPE;
                try {
                    double dValue = peekFlowVariableDouble(name);
                    values[i] = new DoubleCell(dValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type double): " + name);
                }
                break;
            case INTEGER:
                type = IntCell.TYPE;
                try {
                    int iValue = peekFlowVariableInt(name);
                    values[i] = new IntCell(iValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type int): " + name);
                }
                break;
            case STRING:
                type = StringCell.TYPE;
                try {
                    String sValue = peekFlowVariableString(name);
                    sValue = sValue == null ? "" : sValue;
                    values[i] = new StringCell(sValue);
                } catch (NoSuchElementException e) {
                    throw new InvalidSettingsException("No such flow variable (of type String): " + name);
                }
                break;
            default:
                throw new InvalidSettingsException("Unsupported variable type: " + c.getSecond());
        }
        if (nameHash.contains(name) && !name.toLowerCase().endsWith("(variable)")) {
            name = name.concat(" (variable)");
        }
        String newName = name;
        int uniquifier = 1;
        while (!nameHash.add(newName)) {
            newName = name + " (#" + (uniquifier++) + ")";
        }
        specs[i] = new DataColumnSpecCreator(newName, type).createSpec();
    }
    arranger.append(new AbstractCellFactory(specs) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell[] getCells(final DataRow row) {
            return values;
        }
    });
    return arranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) HashSet(java.util.HashSet) Pair(org.knime.core.util.Pair) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 28 with Pair

use of org.knime.core.util.Pair in project knime-core by knime.

the class RegressionContent method getValues.

/**
 * Returns the parameters mapped to the values of the given matrix.
 *
 * @param matrix the matrix with the raw data
 * @return the variables and the exponents mapped to values of the given matrix
 * @since 3.4
 */
protected Map<Pair<String, Integer>, Double> getValues(final RealMatrix matrix) {
    Map<Pair<String, Integer>, Double> coefficients = new HashMap<Pair<String, Integer>, Double>();
    int p = m_includeConstant ? 1 : 0;
    for (String colName : m_outSpec.getLearningFields()) {
        if (m_factorList.contains(colName)) {
            Iterator<DataCell> designIter = m_factorDomainValues.get(colName).iterator();
            if (!designIter.hasNext()) {
                continue;
            }
            // Omit first
            designIter.next();
            while (designIter.hasNext()) {
                DataCell dvValue = designIter.next();
                String variable = colName + "=" + dvValue;
                double coeff = matrix.getEntry(0, p);
                coefficients.put(Pair.create(variable, 1), coeff);
                p++;
            }
        } else {
            String variable = colName;
            double coeff = matrix.getEntry(0, p);
            coefficients.put(Pair.create(variable, 1), coeff);
            p++;
        }
    }
    return coefficients;
}
Also used : HashMap(java.util.HashMap) DataCell(org.knime.core.data.DataCell) Pair(org.knime.core.util.Pair)

Example 29 with Pair

use of org.knime.core.util.Pair in project knime-core by knime.

the class RegressionContent method createTablePortObject.

/**
 * Creates a BufferedDataTable with the estimated parameters and statistics.
 *
 * @param exec The execution context
 * @return a port object
 */
public BufferedDataTable createTablePortObject(final ExecutionContext exec) {
    DataTableSpec tableOutSpec = outputTableSpec();
    BufferedDataContainer dc = exec.createDataContainer(tableOutSpec);
    List<String> parameters = this.getParameters();
    int c = 0;
    Map<Pair<String, Integer>, Double> coefficients = this.getCoefficients();
    Map<Pair<String, Integer>, Double> stdErrs = this.getStandardErrors();
    Map<Pair<String, Integer>, Double> zScores = this.getTValues();
    Map<Pair<String, Integer>, Double> pValues = this.getPValues();
    for (int degree = 1; degree <= m_maxExponent; ++degree) {
        for (String parameter : parameters) {
            Pair<String, Integer> key = Pair.create(parameter, degree);
            List<DataCell> cells = new ArrayList<DataCell>();
            cells.add(new StringCell(parameter));
            addDegree(degree, cells);
            cells.add(value(coefficients.get(key)));
            cells.add(value(stdErrs.get(key)));
            cells.add(value(zScores.get(key)));
            cells.add(value(pValues.get(key)));
            c++;
            dc.addRowToTable(new DefaultRow("Row" + c, cells));
        }
    }
    if (m_includeConstant) {
        List<DataCell> cells = new ArrayList<DataCell>();
        cells.add(new StringCell("Intercept"));
        addDegree(0, cells);
        cells.add(value(this.getIntercept()));
        cells.add(value(this.getInterceptStdErr()));
        cells.add(value(this.getInterceptTValue()));
        cells.add(value(this.getInterceptPValue()));
        c++;
        dc.addRowToTable(new DefaultRow("Row" + c, cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) Pair(org.knime.core.util.Pair)

Example 30 with Pair

use of org.knime.core.util.Pair in project knime-core by knime.

the class PolyRegContent method getValues.

/**
 * Returns the parameters mapped to the values of the given matrix.
 *
 * @param matrix the matrix with the raw data
 * @return the variables mapped to values of the given matrix
 */
@Override
protected Map<Pair<String, Integer>, Double> getValues(final RealMatrix matrix) {
    Map<Pair<String, Integer>, Double> coefficients = new HashMap<Pair<String, Integer>, Double>();
    int p = m_includeConstant ? 1 : 0;
    for (int degree = 1; degree <= m_maxExponent; ++degree) {
        for (String colName : m_outSpec.getLearningFields()) {
            if (m_factorList.contains(colName)) {
                Iterator<DataCell> designIter = m_factorDomainValues.get(colName).iterator();
                // Omit first
                designIter.next();
                while (designIter.hasNext()) {
                    DataCell dvValue = designIter.next();
                    String variable = colName + "=" + dvValue;
                    double coeff = matrix.getEntry(0, p);
                    coefficients.put(Pair.create(variable, degree), coeff);
                    p++;
                }
            } else {
                String variable = colName;
                double coeff = matrix.getEntry(0, p);
                coefficients.put(Pair.create(variable, degree), coeff);
                p++;
            }
        }
    }
    return coefficients;
}
Also used : HashMap(java.util.HashMap) DataCell(org.knime.core.data.DataCell) Pair(org.knime.core.util.Pair)

Aggregations

Pair (org.knime.core.util.Pair)54 ArrayList (java.util.ArrayList)17 DataCell (org.knime.core.data.DataCell)14 DataType (org.knime.core.data.DataType)13 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)13 PortType (org.knime.core.node.port.PortType)13 LinkedHashMap (java.util.LinkedHashMap)11 Map (java.util.Map)10 DataColumnSpec (org.knime.core.data.DataColumnSpec)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 DataTableSpec (org.knime.core.data.DataTableSpec)9 FlowVariable (org.knime.core.node.workflow.FlowVariable)9 DataRow (org.knime.core.data.DataRow)8 StringCell (org.knime.core.data.def.StringCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)7 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)6 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)6 DefaultRow (org.knime.core.data.def.DefaultRow)6 DoubleCell (org.knime.core.data.def.DoubleCell)6