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