Search in sources :

Example 31 with Pair

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

the class DecisionTreeNode method getWinnerAndClasscounts.

/**
 * Classify a new pattern given as a row of values. Returns the winning
 * class and the class counts of all classes.
 *
 * @param row input pattern
 * @param spec the corresponding table spec
 * @return class of pattern the decision tree predicts
 * @throws Exception if something went wrong (unknown attribute for example)
 */
public final Pair<DataCell, LinkedHashMap<DataCell, Double>> getWinnerAndClasscounts(final DataRow row, final DataTableSpec spec) throws Exception {
    DecisionTreeNode winnerNode = getWinnerNode(row, spec);
    LinkedHashMap<DataCell, Double> classCounts;
    DataCell winner = null;
    if (winnerNode == null) {
        // Missing value encountered, return null and empty map
        classCounts = new LinkedHashMap<DataCell, Double>();
        winner = new MissingCell("Error in decision tree prediction");
    } else if (winnerNode instanceof DecisionTreeNodeSplit) {
        // We stopped before reaching a leaf (eg due to a missing value)
        classCounts = ((DecisionTreeNodeSplit) winnerNode).getNodeClassWeights();
        double max = 0;
        for (DataCell key : classCounts.keySet()) {
            Double val = classCounts.get(key);
            if (val != null && val > max) {
                max = val;
                winner = key;
            }
        }
    } else {
        // We reached a leaf node, return its score and class counts
        DecisionTreeNodeLeaf leaf = (DecisionTreeNodeLeaf) winnerNode;
        winner = leaf.getMajorityClass();
        classCounts = leaf.getClassCounts();
    }
    return new Pair<DataCell, LinkedHashMap<DataCell, Double>>(winner, classCounts);
}
Also used : MissingCell(org.knime.core.data.MissingCell) DataCell(org.knime.core.data.DataCell) Pair(org.knime.core.util.Pair)

Example 32 with Pair

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

the class WorkflowManager method changeMetaNodeInputPorts.

/**
 * @param subFlowID ID of the subflow
 * @param newPorts The new ports
 * @since 2.6
 */
public void changeMetaNodeInputPorts(final NodeID subFlowID, final MetaPortInfo[] newPorts) {
    try (WorkflowLock lock = lock()) {
        WorkflowManager subFlowMgr = getNodeContainer(subFlowID, WorkflowManager.class, true);
        if (!haveMetaPortsChanged(newPorts, true, subFlowMgr)) {
            return;
        }
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsThisFlow = m_workflow.changeDestinationPortsForMetaNode(subFlowID, newPorts, false);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer old = p.getFirst();
            m_workflow.removeConnection(old);
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        Workflow subFlow = subFlowMgr.m_workflow;
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsSubFlow = subFlow.changeSourcePortsForMetaNode(subFlowID, newPorts, false);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer old = p.getFirst();
            subFlow.removeConnection(old);
            subFlowMgr.notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        WorkflowInPort[] newMNPorts = new WorkflowInPort[newPorts.length];
        for (int i = 0; i < newPorts.length; i++) {
            final int oldIndex = newPorts[i].getOldIndex();
            if (oldIndex >= 0) {
                newMNPorts[i] = subFlowMgr.getInPort(oldIndex);
                newMNPorts[i].setPortIndex(i);
            } else {
                newMNPorts[i] = new WorkflowInPort(i, newPorts[i].getType());
            }
        }
        subFlowMgr.m_inPorts = newMNPorts;
        subFlowMgr.notifyNodePropertyChangedListener(NodeProperty.MetaNodePorts);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer newConn = p.getSecond();
            m_workflow.addConnection(newConn);
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer newConn = p.getSecond();
            subFlow.addConnection(newConn);
            subFlowMgr.notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        subFlowMgr.setDirty();
        setDirty();
    }
}
Also used : IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) Pair(org.knime.core.util.Pair)

Example 33 with Pair

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

the class WorkflowManager method parallelizeLoop.

/* Parallelize this "loop": create appropriate number of parallel
     * branches executing the matching chunks.
     */
private void parallelizeLoop(final NodeID startID) throws IllegalLoopException {
    try (WorkflowLock lock = lock()) {
        final NodeID endID = m_workflow.getMatchingLoopEnd(startID);
        LoopEndParallelizeNode endNode;
        LoopStartParallelizeNode startNode;
        try {
            // just for validation
            startNode = castNodeModel(startID, LoopStartParallelizeNode.class);
            endNode = castNodeModel(endID, LoopEndParallelizeNode.class);
        } catch (IllegalArgumentException iae) {
            throw new IllegalLoopException("Parallel Chunk Start Node not connected to matching end node!", iae);
        }
        final ArrayList<NodeAndInports> loopBody = m_workflow.findAllNodesConnectedToLoopBody(startID, endID);
        NodeID[] loopNodes = new NodeID[loopBody.size()];
        loopNodes[0] = startID;
        for (int i = 0; i < loopBody.size(); i++) {
            loopNodes[i] = loopBody.get(i).getID();
        }
        // creating matching sub workflow node holding all chunks
        Set<Pair<NodeID, Integer>> exposedInports = findNodesWithExternalSources(startID, loopNodes);
        HashMap<Pair<NodeID, Integer>, Integer> extInConnections = new HashMap<Pair<NodeID, Integer>, Integer>();
        PortType[] exposedInportTypes = new PortType[exposedInports.size() + 1];
        // the first port is the variable port
        exposedInportTypes[0] = FlowVariablePortObject.TYPE;
        // the remaining ports cover the exposed inports of the loop body
        int index = 1;
        for (Pair<NodeID, Integer> npi : exposedInports) {
            NodeContainer nc = getNodeContainer(npi.getFirst());
            int portIndex = npi.getSecond();
            exposedInportTypes[index] = nc.getInPort(portIndex).getPortType();
            extInConnections.put(npi, index);
            index++;
        }
        WorkflowManager subwfm = null;
        if (startNode.getNrRemoteChunks() > 0) {
            subwfm = createAndAddSubWorkflow(exposedInportTypes, new PortType[0], "Parallel Chunks");
            NodeUIInformation startUIPlain = getNodeContainer(startID).getUIInformation();
            if (startUIPlain != null) {
                NodeUIInformation startUI = NodeUIInformation.builder(startUIPlain).translate(new int[] { 60, -60, 0, 0 }).build();
                subwfm.setUIInformation(startUI);
            }
            // connect outside(!) nodes to new sub metanode
            for (Map.Entry<Pair<NodeID, Integer>, Integer> entry : extInConnections.entrySet()) {
                final Pair<NodeID, Integer> npi = entry.getKey();
                int metanodeindex = entry.getValue();
                if (metanodeindex >= 0) {
                    // ignore variable port!
                    // we need to find the source again (since our list
                    // only holds the destination...)
                    ConnectionContainer cc = this.getIncomingConnectionFor(npi.getFirst(), npi.getSecond());
                    this.addConnection(cc.getSource(), cc.getSourcePort(), subwfm.getID(), metanodeindex);
                }
            }
        }
        ParallelizedChunkContentMaster pccm = new ParallelizedChunkContentMaster(subwfm, endNode, startNode.getNrRemoteChunks());
        for (int i = 0; i < startNode.getNrRemoteChunks(); i++) {
            ParallelizedChunkContent copiedNodes = duplicateLoopBodyInSubWFMandAttach(subwfm, extInConnections, startID, endID, loopNodes, i);
            copiedNodes.executeChunk();
            pccm.addParallelChunk(i, copiedNodes);
        }
        // make sure head knows his chunk master (for potential cleanup)
        startNode.setChunkMaster(pccm);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) NodeAndInports(org.knime.core.node.workflow.Workflow.NodeAndInports) ParallelizedChunkContent(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContent) Pair(org.knime.core.util.Pair) ParallelizedChunkContentMaster(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContentMaster) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) PortType(org.knime.core.node.port.PortType)

Example 34 with Pair

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

the class WorkflowManager method changeSubNodeOutputPorts.

/**
 * @param subFlowID ID of the subflow
 * @param newPorts The new ports
 * @since 2.10
 */
public void changeSubNodeOutputPorts(final NodeID subFlowID, final MetaPortInfo[] newPorts) {
    try (WorkflowLock lock = lock()) {
        SubNodeContainer snc = getNodeContainer(subFlowID, SubNodeContainer.class, true);
        if (!haveSubPortsChanged(newPorts, false, snc)) {
            return;
        }
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsThisFlow = m_workflow.changeSourcePortsForMetaNode(subFlowID, newPorts, true);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer old = p.getFirst();
            m_workflow.removeConnection(old);
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        Workflow subFlow = snc.getWorkflowManager().m_workflow;
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsSubFlow = subFlow.changeDestinationPortsForMetaNode(snc.getVirtualOutNodeID(), newPorts, true);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer old = p.getFirst();
            subFlow.removeConnection(old);
            snc.getWorkflowManager().notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        PortType[] portTypes = new PortType[newPorts.length - 1];
        for (int i = 0; i < newPorts.length - 1; i++) {
            portTypes[i] = newPorts[i + 1].getType();
        }
        snc.setOutPorts(portTypes);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer newConn = p.getSecond();
            m_workflow.addConnection(newConn);
            resetAndConfigureNode(newConn.getDest());
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer newConn = new ConnectionContainer(p.getSecond().getSource(), p.getSecond().getSourcePort(), snc.getVirtualOutNodeID(), p.getSecond().getDestPort(), p.getSecond().getType(), p.getSecond().isFlowVariablePortConnection());
            subFlow.addConnection(newConn);
            snc.getWorkflowManager().resetAndConfigureNode(newConn.getDest());
            snc.getWorkflowManager().notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        setDirty();
    }
}
Also used : IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) Pair(org.knime.core.util.Pair) PortType(org.knime.core.node.port.PortType)

Example 35 with Pair

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

the class WorkflowManager method changeSubNodeInputPorts.

/**
 * @param subFlowID ID of the subflow
 * @param newPorts The new ports
 * @since 2.10
 */
public void changeSubNodeInputPorts(final NodeID subFlowID, final MetaPortInfo[] newPorts) {
    try (WorkflowLock lock = lock()) {
        SubNodeContainer snc = getNodeContainer(subFlowID, SubNodeContainer.class, true);
        if (!haveSubPortsChanged(newPorts, true, snc)) {
            return;
        }
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsThisFlow = m_workflow.changeDestinationPortsForMetaNode(subFlowID, newPorts, true);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer old = p.getFirst();
            m_workflow.removeConnection(old);
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        Workflow subFlow = snc.getWorkflowManager().m_workflow;
        List<Pair<ConnectionContainer, ConnectionContainer>> changedConnectionsSubFlow = subFlow.changeSourcePortsForMetaNode(snc.getVirtualInNodeID(), newPorts, true);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer old = p.getFirst();
            subFlow.removeConnection(old);
            snc.getWorkflowManager().notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_REMOVED, null, old, null));
        }
        PortType[] portTypes = new PortType[newPorts.length - 1];
        for (int i = 0; i < newPorts.length - 1; i++) {
            portTypes[i] = newPorts[i + 1].getType();
        }
        snc.setInPorts(portTypes);
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsThisFlow) {
            ConnectionContainer newConn = p.getSecond();
            m_workflow.addConnection(newConn);
            resetAndConfigureNode(newConn.getDest());
            notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        for (Pair<ConnectionContainer, ConnectionContainer> p : changedConnectionsSubFlow) {
            ConnectionContainer newConn = new ConnectionContainer(snc.getVirtualInNodeID(), p.getSecond().getSourcePort(), p.getSecond().getDest(), p.getSecond().getDestPort(), p.getSecond().getType(), p.getSecond().isFlowVariablePortConnection());
            subFlow.addConnection(newConn);
            snc.getWorkflowManager().resetAndConfigureNode(newConn.getDest());
            snc.getWorkflowManager().notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.CONNECTION_ADDED, null, null, newConn));
        }
        setDirty();
    }
}
Also used : IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) Pair(org.knime.core.util.Pair) PortType(org.knime.core.node.port.PortType)

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