Search in sources :

Example 46 with Pair

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

the class VariableToTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    DataTableSpec spec = createOutSpec();
    BufferedDataContainer cont = exec.createDataContainer(spec);
    List<Pair<String, FlowVariable.Type>> vars;
    if (m_settings.getIncludeAll()) {
        vars = getAllVariables();
    } else {
        vars = m_settings.getVariablesOfInterest();
    }
    DataCell[] specs = new DataCell[vars.size()];
    List<String> lostVariables = new ArrayList<String>();
    for (int i = 0; i < vars.size(); i++) {
        Pair<String, FlowVariable.Type> c = vars.get(i);
        String name = c.getFirst();
        // fallback
        DataCell cell = DataType.getMissingCell();
        switch(c.getSecond()) {
            case DOUBLE:
                try {
                    double dValue = peekFlowVariableDouble(c.getFirst());
                    cell = new DoubleCell(dValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Double)");
                }
                break;
            case INTEGER:
                try {
                    int iValue = peekFlowVariableInt(c.getFirst());
                    cell = new IntCell(iValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (Integer)");
                }
                break;
            case STRING:
                try {
                    String sValue = peekFlowVariableString(c.getFirst());
                    sValue = sValue == null ? "" : sValue;
                    cell = new StringCell(sValue);
                } catch (NoSuchElementException e) {
                    lostVariables.add(name + " (String)");
                }
                break;
        }
        specs[i] = cell;
    }
    cont.addRowToTable(new DefaultRow("values", specs));
    cont.close();
    return new BufferedDataTable[] { cont.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) IntCell(org.knime.core.data.def.IntCell) PortType(org.knime.core.node.port.PortType) DataType(org.knime.core.data.DataType) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) Pair(org.knime.core.util.Pair) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 47 with Pair

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

the class RearrangeColumnsTable method calcNewCellsForRow.

/**
 * Calls for an input row the list of cell factories to produce the output row (contains only the new cells, merged
 * later).
 *
 * @param unconvertedRow The input row to be processed
 * @param producerMap For each new (or replaced) column the factory.
 * @return The output row.
 */
static DataRow calcNewCellsForRow(final DataRow unconvertedRow, final NewColumnsProducerMapping producerMap) {
    final int newColCount = producerMap.getAllNewColumnsList().size();
    DataCell[] newCells = new DataCell[newColCount];
    DataRow row = applyDataTypeConverters(unconvertedRow, producerMap, newCells);
    IdentityHashMap<CellFactory, List<Pair<Integer, Integer>>> uniqueCellFactoryMap = producerMap.getUniqueCellFactoryMap();
    for (Map.Entry<CellFactory, List<Pair<Integer, Integer>>> e : uniqueCellFactoryMap.entrySet()) {
        CellFactory factory = e.getKey();
        List<Pair<Integer, Integer>> list = e.getValue();
        DataCell[] fromFac = factory.getCells(row);
        if (fromFac.length != list.size()) {
            String error = String.format("New cells array length conflict: expected %d, actual %d (class %s)", list.size(), fromFac.length, factory.getClass().getName());
            if (fromFac.length < list.size()) {
                throw new IndexOutOfBoundsException(error);
            } else {
                // such problems were ignored until 2.6 -- print warning only
                if (codingProblemsCellFactoryClasses == null) {
                    codingProblemsCellFactoryClasses = new HashSet<Class<? extends CellFactory>>();
                }
                if (codingProblemsCellFactoryClasses.add(factory.getClass())) {
                    LOGGER.coding(error);
                }
            }
        }
        final int length = list.size();
        for (int i = 0; i < length; i++) {
            Pair<Integer, Integer> indexPair = list.get(i);
            int indexInNewCellsArray = indexPair.getFirst();
            int indexInFactory = indexPair.getSecond();
            assert newCells[indexInNewCellsArray] == null : "New cells array at index expected to be null";
            newCells[indexInNewCellsArray] = fromFac[indexInFactory];
        }
    }
    DataRow appendix = new DefaultRow(row.getKey(), newCells);
    return appendix;
}
Also used : DataRow(org.knime.core.data.DataRow) DataCell(org.knime.core.data.DataCell) ArrayList(java.util.ArrayList) List(java.util.List) DefaultRow(org.knime.core.data.def.DefaultRow) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Pair(org.knime.core.util.Pair)

Example 48 with Pair

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

the class RearrangeColumnsTable method applyDataTypeConverters.

/**
 * Used when {@link ColumnRearranger#ensureColumnIsConverted(DataCellTypeConverter, int)} is called. It
 * preproccesses the row and replaces the column to be converted by the the result of the given converter.
 *
 * @param row The original input row.
 * @param producerMap The object having the converter list (or not)
 * @param newCells
 * @return The input row if no converter applied or a modified copy of the input row.
 */
private static DataRow applyDataTypeConverters(final DataRow row, final NewColumnsProducerMapping producerMap, final DataCell[] newCells) {
    List<Pair<SpecAndFactoryObject, Integer>> converterToIndexMap = producerMap.getConverterToIndexMap();
    if (!converterToIndexMap.isEmpty()) {
        DataCell[] inputRowCells = new DataCell[row.getNumCells()];
        for (int i = 0; i < inputRowCells.length; i++) {
            inputRowCells[i] = row instanceof BlobSupportDataRow ? ((BlobSupportDataRow) row).getRawCell(i) : row.getCell(i);
        }
        for (Pair<SpecAndFactoryObject, Integer> entry : converterToIndexMap) {
            SpecAndFactoryObject specAndObject = entry.getFirst();
            DataCellTypeConverter converter = specAndObject.getConverter();
            int converterIndex = specAndObject.getConverterIndex();
            Integer index = entry.getSecond();
            DataCell convertedCell = converter.callConvert(row.getCell(converterIndex));
            newCells[index] = convertedCell;
            inputRowCells[converterIndex] = convertedCell;
        }
        return new BlobSupportDataRow(row.getKey(), inputRowCells);
    }
    return row;
}
Also used : DataCellTypeConverter(org.knime.core.data.DataCellTypeConverter) SpecAndFactoryObject(org.knime.core.data.container.ColumnRearranger.SpecAndFactoryObject) DataCell(org.knime.core.data.DataCell) Pair(org.knime.core.util.Pair)

Example 49 with Pair

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

the class ClassUtil method getMethodsWithAnnotation.

/**
 * Get a Collection of methods which are annotated with the given {@link Annotation} paired with the Annotation
 * itself.
 *
 * @param cls Class to get annotated methods from
 * @param annotationClass the annotation
 * @return Collection of pairs of methods together with the instance of <code>annotationClass</code> they are
 *         annotated with
 */
public static <A extends Annotation> Collection<Pair<Method, A>> getMethodsWithAnnotation(final Class<?> cls, final Class<A> annotationClass) {
    // result list of annotated methods
    final ArrayList<Pair<Method, A>> annotatedMethods = new ArrayList<>();
    // all methods declared in exactly this class (not in superclasses)
    final Method[] declaredMethods = cls.getDeclaredMethods();
    for (final Method m : declaredMethods) {
        // get an instance of the given annotation
        final A a = m.getAnnotation(annotationClass);
        if (a != null) {
            // present, therefore add to the list
            annotatedMethods.add(new Pair<>(m, a));
        }
    }
    return annotatedMethods;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Pair(org.knime.core.util.Pair)

Example 50 with Pair

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

the class ExtendedStatisticsNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec) throws IOException {
    NodeSettingsRO sett = NodeSettings.loadFromXML(new FileInputStream(new File(internDir, "statistic.xml.gz")));
    try {
        m_statTable = Statistics3Table.load(sett);
    } catch (InvalidSettingsException ise) {
        throw new IOException(ise);
    }
    double[] means = getStatTable().getMean();
    if (m_enableHiLite.getBooleanValue()) {
        File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
        File dataArrayGz = new File(internDir, DATA_ARRAY_GZ);
        try {
            Set<String> nominalColumnNames = new LinkedHashSet<String>();
            String[] columnNames = getStatTable().getColumnNames();
            for (int i = 0; i < columnNames.length; ++i) {
                if (getStatTable().getNominalValues(i) != null) {
                    nominalColumnNames.add(columnNames[i]);
                }
            }
            Pair<Pair<Map<Integer, ? extends HistogramModel<?>>, Map<Integer, Map<Integer, Set<RowKey>>>>, Map<Integer, Map<DataValue, Set<RowKey>>>> ppair = HistogramColumn.loadHistograms(histogramsGz, dataArrayGz, nominalColumnNames, BIN_SELECTION_STRATEGY, means);
            m_histograms = ppair.getFirst().getFirst();
            m_buckets = ppair.getFirst().getSecond();
            m_nominalKeys = ppair.getSecond();
        // m_nominalTypes = ppair.getSecond().getSecond();
        } catch (InvalidSettingsException e) {
            getLogger().error("Failed to load settings for the HiLite, please rerun.", e);
            m_histograms = Collections.emptyMap();
            m_buckets = Collections.emptyMap();
            m_nominalKeys = Collections.emptyMap();
        // m_nominalTypes = Collections.emptyMap();
        }
    } else {
        File histogramsGz = new File(internDir, HISTOGRAMS_GZ);
        m_nominalKeys = Collections.emptyMap();
        try {
            m_buckets = new HashMap<Integer, Map<Integer, Set<RowKey>>>();
            m_histograms = HistogramColumn.loadHistograms(histogramsGz, m_buckets, BIN_SELECTION_STRATEGY, means);
            m_buckets.clear();
        } catch (InvalidSettingsException e) {
            m_histograms = Collections.emptyMap();
            m_buckets = Collections.emptyMap();
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) RowKey(org.knime.core.data.RowKey) DataValue(org.knime.core.data.DataValue) IOException(java.io.IOException) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) FileInputStream(java.io.FileInputStream) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) 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