Search in sources :

Example 66 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class XValidatePartitionModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    boolean inLoop = (m_partNumbers != null);
    if (!inLoop) {
        if (m_settings.leaveOneOut()) {
            m_nrIterations = inData[0].getRowCount();
            m_currIteration = 0;
            m_partNumbers = new short[0];
        } else {
            m_partNumbers = new short[inData[0].getRowCount()];
            final double partSize = m_partNumbers.length / (double) m_settings.validations();
            if (m_settings.stratifiedSampling()) {
                ExecutionMonitor subExec = exec.createSubProgress(0.0);
                subExec.setMessage("Preparing stratified sampling");
                Map<DataCell, List<Integer>> valueCounts = countValues(inData[0], subExec, m_settings.classColumn());
                int part = 0;
                for (Map.Entry<DataCell, List<Integer>> e : valueCounts.entrySet()) {
                    List<Integer> l = e.getValue();
                    for (Integer i : l) {
                        m_partNumbers[i] = (short) part++;
                        part %= m_settings.validations();
                    }
                }
            } else {
                for (int i = 0; i < m_partNumbers.length; i++) {
                    m_partNumbers[i] = (short) Math.min(i / partSize, m_partNumbers.length);
                }
                if (m_settings.randomSampling()) {
                    long seed = m_settings.useRandomSeed() ? m_settings.randomSeed() : System.currentTimeMillis();
                    Random rand = new Random(seed);
                    for (int i = 0; i < m_partNumbers.length; i++) {
                        int pos = rand.nextInt(m_partNumbers.length);
                        short x = m_partNumbers[pos];
                        m_partNumbers[pos] = m_partNumbers[i];
                        m_partNumbers[i] = x;
                    }
                }
            }
            m_nrIterations = m_settings.validations();
            m_currIteration = 0;
        }
    }
    BufferedDataContainer test = exec.createDataContainer(inData[0].getDataTableSpec());
    BufferedDataContainer train = exec.createDataContainer(inData[0].getDataTableSpec());
    int count = 0;
    final double max = inData[0].getRowCount();
    for (DataRow row : inData[0]) {
        exec.checkCanceled();
        exec.setProgress(count / max);
        if (m_settings.leaveOneOut() && (count == m_currIteration)) {
            test.addRowToTable(row);
        } else if (!m_settings.leaveOneOut() && (m_partNumbers[count] == m_currIteration)) {
            test.addRowToTable(row);
        } else {
            train.addRowToTable(row);
        }
        count++;
    }
    test.close();
    train.close();
    // we need to put the counts on the stack for the loop's tail to see:
    pushFlowVariableInt("currentIteration", m_currIteration);
    pushFlowVariableInt("maxIterations", m_nrIterations);
    m_currIteration++;
    return new BufferedDataTable[] { train.getTable(), test.getTable() };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DataRow(org.knime.core.data.DataRow) Random(java.util.Random) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 67 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class WorkflowManager method saveAs.

/**
 * Saves the workflow to a new location, setting the argument directory as the new NC dir. It will first copy the
 * "old" directory, point the NC dir to the new location and then do an incremental save.
 *
 * @param newContext the new workflow context, including the changed path
 * @param exec The execution monitor
 * @throws IOException If an IO error occured
 * @throws CanceledExecutionException If the execution was canceled
 * @throws LockFailedException If locking failed
 * @since 3.3
 */
public void saveAs(final WorkflowContext newContext, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    if (this == ROOT) {
        throw new IOException("Can't save root workflow");
    }
    try (WorkflowLock lock = lock()) {
        ReferencedFile ncDirRef = getNodeContainerDirectory();
        if (!isProject()) {
            throw new IOException("Cannot call save-as on a non-project workflow");
        }
        File directory = newContext.getCurrentLocation();
        directory.mkdirs();
        if (!directory.isDirectory() || !directory.canWrite()) {
            throw new IOException("Cannot write to " + directory);
        }
        boolean isNCDirNullOrRootReferenceFolder = ncDirRef == null || ncDirRef.getParent() == null;
        if (!isNCDirNullOrRootReferenceFolder) {
            throw new IOException("Referenced directory pointer is not hierarchical: " + ncDirRef);
        }
        m_workflowContext = newContext;
        ReferencedFile autoSaveDirRef = getAutoSaveDirectory();
        ExecutionMonitor saveExec;
        File ncDir = ncDirRef != null ? ncDirRef.getFile() : null;
        if (!ConvenienceMethods.areEqual(ncDir, directory)) {
            // new location
            // cannot be null
            ncDirRef.writeLock();
            try {
                ExecutionMonitor copyExec = exec.createSubProgress(0.5);
                final String copymsg = String.format("Copying existing workflow to new location " + "(from \"%s\" to \"%s\")", ncDir, directory);
                exec.setMessage(copymsg);
                LOGGER.debug(copymsg);
                copyExec.setProgress(1.0);
                FileUtils.copyDirectory(ncDir, directory, /* all but .knimeLock */
                FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(VMFileLocker.LOCK_FILE, IOCase.SENSITIVE)));
                exec.setMessage("Incremental save");
                ncDirRef.changeRoot(directory);
                if (autoSaveDirRef != null) {
                    File newLoc = WorkflowSaveHelper.getAutoSaveDirectory(ncDirRef);
                    final File autoSaveDir = autoSaveDirRef.getFile();
                    if (autoSaveDir.exists()) {
                        try {
                            FileUtils.moveDirectory(autoSaveDir, newLoc);
                            autoSaveDirRef.changeRoot(newLoc);
                            LOGGER.debugWithFormat("Moved auto-save directory from \"%s\" to \"%s\"", autoSaveDir.getAbsolutePath(), newLoc.getAbsolutePath());
                        } catch (IOException ioe) {
                            LOGGER.error(String.format("Couldn't move auto save directory \"%s\" to \"%s\": %s", autoSaveDir.getAbsolutePath(), newLoc.getAbsolutePath(), ioe.getMessage()), ioe);
                        }
                    } else {
                        autoSaveDirRef.changeRoot(newLoc);
                    }
                }
                m_isWorkflowDirectoryReadonly = false;
            } finally {
                ncDirRef.writeUnlock();
            }
            saveExec = exec.createSubProgress(0.5);
        } else {
            saveExec = exec;
        }
        save(directory, saveExec, true);
    }
}
Also used : IOException(java.io.IOException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 68 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class WorkflowManager method loadExecutionResult.

/**
 * {@inheritDoc}
 */
@Override
public void loadExecutionResult(final NodeContainerExecutionResult result, final ExecutionMonitor exec, final LoadResult loadResult) {
    CheckUtils.checkArgument(result instanceof WorkflowExecutionResult, "Argument must be instance of \"%s\": %s", WorkflowExecutionResult.class.getSimpleName(), result == null ? "null" : result.getClass().getSimpleName());
    WorkflowExecutionResult r = (WorkflowExecutionResult) result;
    try (WorkflowLock lock = lock()) {
        super.loadExecutionResult(result, exec, loadResult);
        Map<NodeID, NodeContainerExecutionResult> map = r.getExecutionResultMap();
        final int count = map.size();
        // contains the corrected NodeID in this workflow (the node ids in
        // the execution result refer to the base id of the remote workflow)
        Map<NodeID, NodeID> transMap = new HashMap<NodeID, NodeID>();
        NodeID otherIDPrefix = r.getBaseID();
        for (NodeID otherID : map.keySet()) {
            assert otherID.hasSamePrefix(otherIDPrefix);
            transMap.put(new NodeID(getID(), otherID.getIndex()), otherID);
        }
        for (NodeID id : m_workflow.createBreadthFirstSortedList(transMap.keySet(), true).keySet()) {
            NodeID otherID = transMap.get(id);
            NodeContainer nc = m_workflow.getNode(id);
            NodeContainerExecutionResult exResult = map.get(otherID);
            if (exResult == null) {
                loadResult.addError("No execution result for node " + nc.getNameWithID());
                continue;
            }
            exec.setMessage(nc.getNameWithID());
            ExecutionMonitor subExec = exec.createSubProgress(1.0 / count);
            // Propagagte the flow variables
            if (nc instanceof SingleNodeContainer) {
                NodeOutPort[] predecessorOutPorts = assemblePredecessorOutPorts(id);
                FlowObjectStack[] sos = Arrays.stream(predecessorOutPorts).map(p -> p != null ? p.getFlowObjectStack() : null).toArray(FlowObjectStack[]::new);
                createAndSetFlowObjectStackFor((SingleNodeContainer) nc, sos);
            }
            nc.loadExecutionResult(exResult, subExec, loadResult);
            subExec.setProgress(1.0);
        }
    }
}
Also used : Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) EXECUTING(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTING) ReferencedFile(org.knime.core.internal.ReferencedFile) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IWriteFileStoreHandler(org.knime.core.data.filestore.internal.IWriteFileStoreHandler) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) AbstractQuickFormValueInConfiguration(org.knime.core.quickform.AbstractQuickFormValueInConfiguration) NodeView(org.knime.core.node.NodeView) NodeContainerExecutionStatus(org.knime.core.node.workflow.execresult.NodeContainerExecutionStatus) Node(org.knime.core.node.Node) Vector(java.util.Vector) Matcher(java.util.regex.Matcher) WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) Map(java.util.Map) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) LockFailedException(org.knime.core.util.LockFailedException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) EXECUTINGREMOTELY(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTINGREMOTELY) Set(java.util.Set) ResolverUtil(org.knime.core.util.pathresolve.ResolverUtil) Executors(java.util.concurrent.Executors) POSTEXECUTE(org.knime.core.node.workflow.InternalNodeContainerState.POSTEXECUTE) InputNode(org.knime.core.node.dialog.InputNode) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Stream(java.util.stream.Stream) CONFIGURED(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ThreadNodeExecutionJobManager(org.knime.core.node.exec.ThreadNodeExecutionJobManager) CollapseIntoMetaNodeResult(org.knime.core.node.workflow.action.CollapseIntoMetaNodeResult) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) VirtualParallelizedChunkPortObjectInNodeModel(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeModel) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) VMFileLocker(org.knime.core.util.VMFileLocker) RestoredFlowLoopContext(org.knime.core.node.workflow.FlowLoopContext.RestoredFlowLoopContext) SplitType(org.knime.core.node.workflow.NodeContainer.NodeContainerSettings.SplitType) AbstractQuickFormConfiguration(org.knime.core.quickform.AbstractQuickFormConfiguration) EXECUTED_QUEUED(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED_QUEUED) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) NotConfigurableException(org.knime.core.node.NotConfigurableException) UpdateStatus(org.knime.core.node.workflow.MetaNodeTemplateInformation.UpdateStatus) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) DialogNode(org.knime.core.node.dialog.DialogNode) LinkedHashSet(java.util.LinkedHashSet) VirtualParallelizedChunkNodeInput(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkNodeInput) KNIMEConstants(org.knime.core.node.KNIMEConstants) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) QuickFormInputNode(org.knime.core.quickform.in.QuickFormInputNode) Executor(java.util.concurrent.Executor) PREEXECUTE(org.knime.core.node.workflow.InternalNodeContainerState.PREEXECUTE) InteractiveView(org.knime.core.node.interactive.InteractiveView) FileOutputStream(java.io.FileOutputStream) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) ViewContent(org.knime.core.node.interactive.ViewContent) UNCONFIGURED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.UNCONFIGURED_MARKEDFOREXEC) OutputNode(org.knime.core.node.dialog.OutputNode) Platform(org.eclipse.core.runtime.Platform) LoopStatus(org.knime.core.node.workflow.NativeNodeContainer.LoopStatus) FileUtil(org.knime.core.util.FileUtil) CONFIGURED_QUEUED(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED_QUEUED) NodeType(org.knime.core.node.NodeFactory.NodeType) ExpandSubnodeResult(org.knime.core.node.workflow.action.ExpandSubnodeResult) ListIterator(java.util.ListIterator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult) ReexecutionCallback(org.knime.core.node.interactive.ReexecutionCallback) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult) FileFilterUtils(org.apache.commons.io.filefilter.FileFilterUtils) NodeDialogPane(org.knime.core.node.NodeDialogPane) VirtualParallelizedChunkPortObjectInNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectInNodeFactory) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) AbstractNodeView(org.knime.core.node.AbstractNodeView) ConvenienceMethods(org.knime.core.node.util.ConvenienceMethods) URI(java.net.URI) ThreadFactory(java.util.concurrent.ThreadFactory) ContainerTable(org.knime.core.data.container.ContainerTable) MetaNodeToSubNodeResult(org.knime.core.node.workflow.action.MetaNodeToSubNodeResult) PortType(org.knime.core.node.port.PortType) NodeAndInports(org.knime.core.node.workflow.Workflow.NodeAndInports) CONFIGURED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.CONFIGURED_MARKEDFOREXEC) Collection(java.util.Collection) CredentialsNode(org.knime.core.node.workflow.CredentialsStore.CredentialsNode) NodeModel(org.knime.core.node.NodeModel) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) WorkflowPortTemplate(org.knime.core.node.workflow.WorkflowPersistor.WorkflowPortTemplate) LoadResultEntryType(org.knime.core.node.workflow.WorkflowPersistor.LoadResultEntry.LoadResultEntryType) NodeContainerTemplateLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.NodeContainerTemplateLinkUpdateResult) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) Entry(java.util.Map.Entry) Optional(java.util.Optional) CheckUtils(org.knime.core.node.util.CheckUtils) CheckUtils.checkState(org.knime.core.node.util.CheckUtils.checkState) PortObject(org.knime.core.node.port.PortObject) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) IntStream(java.util.stream.IntStream) IOCase(org.apache.commons.io.IOCase) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NodeExecutionJobManagerPool(org.knime.core.node.util.NodeExecutionJobManagerPool) SingleNodeContainerSettings(org.knime.core.node.workflow.SingleNodeContainer.SingleNodeContainerSettings) EXECUTED_MARKEDFOREXEC(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED_MARKEDFOREXEC) HashMap(java.util.HashMap) NodeSettings(org.knime.core.node.NodeSettings) ConnectionType(org.knime.core.node.workflow.ConnectionContainer.ConnectionType) Function(java.util.function.Function) Pair(org.knime.core.util.Pair) HashSet(java.util.HashSet) IDLE(org.knime.core.node.workflow.InternalNodeContainerState.IDLE) ExternalNodeData(org.knime.core.node.dialog.ExternalNodeData) ConnectionContainerTemplate(org.knime.core.node.workflow.WorkflowPersistor.ConnectionContainerTemplate) NodeLogger(org.knime.core.node.NodeLogger) NodeCreationContext(org.knime.core.node.NodeCreationContext) EXECUTED(org.knime.core.node.workflow.InternalNodeContainerState.EXECUTED) LoadResult(org.knime.core.node.workflow.WorkflowPersistor.LoadResult) LinkedList(java.util.LinkedList) Role(org.knime.core.node.workflow.MetaNodeTemplateInformation.Role) InteractiveWebViewsResult(org.knime.core.node.workflow.action.InteractiveWebViewsResult) SubNodeToMetaNodeResult(org.knime.core.node.workflow.action.SubNodeToMetaNodeResult) OutputStream(java.io.OutputStream) NodeFactory(org.knime.core.node.NodeFactory) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) TemplateType(org.knime.core.node.workflow.MetaNodeTemplateInformation.TemplateType) MetaPortInfo(org.knime.core.node.port.MetaPortInfo) VirtualParallelizedChunkPortObjectOutNodeFactory(org.knime.core.node.workflow.virtual.parchunk.VirtualParallelizedChunkPortObjectOutNodeFactory) MetaNodeDialogNode(org.knime.core.node.dialog.MetaNodeDialogNode) ParallelizedChunkContent(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContent) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) Type(org.knime.core.node.workflow.NodeMessage.Type) TimeUnit(java.util.concurrent.TimeUnit) Condition(java.util.concurrent.locks.Condition) IEarlyStartup(org.knime.core.util.IEarlyStartup) ParallelizedChunkContentMaster(org.knime.core.node.workflow.virtual.parchunk.ParallelizedChunkContentMaster) IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) NodeProperty(org.knime.core.node.workflow.NodePropertyChangedEvent.NodeProperty) Collections(java.util.Collections) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Example 69 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class WorkflowManager method loadMetaNodeTemplate.

/**
 * Reads the template info from the metanode argument and then resolves that URI and returns a workflow manager that
 * lives as child of {@link #templateWorkflowRoot}. Used to avoid duplicate loading from a remote location. The
 * returned instance is then copied to the final destination.
 */
private NodeContainerTemplate loadMetaNodeTemplate(final NodeContainerTemplate meta, final WorkflowLoadHelper loadHelper, final LoadResult loadResult) throws IOException, UnsupportedWorkflowVersionException, CanceledExecutionException {
    MetaNodeTemplateInformation linkInfo = meta.getTemplateInformation();
    URI sourceURI = linkInfo.getSourceURI();
    WorkflowManager tempParent = lazyInitTemplateWorkflowRoot();
    MetaNodeLinkUpdateResult loadResultChild;
    NodeContext.pushContext((NodeContainer) meta);
    try {
        if (m_workflowContext != null && m_workflowContext.getMountpointURI().isPresent() && sourceURI.getHost().startsWith("knime.") && (ResolverUtil.resolveURItoLocalFile(m_workflowContext.getMountpointURI().get()) == null)) {
            // a workflow relative template URI but the workflow itself is not local
            // => the template is also not local and must be resolved using the workflow's original location
            URI origWfUri = m_workflowContext.getMountpointURI().get();
            String combinedPath = origWfUri.getPath() + sourceURI.getPath();
            sourceURI = new URI(origWfUri.getScheme(), origWfUri.getUserInfo(), origWfUri.getHost(), origWfUri.getPort(), combinedPath, origWfUri.getQuery(), origWfUri.getFragment()).normalize();
        }
        File localDir = ResolverUtil.resolveURItoLocalOrTempFile(sourceURI);
        if (localDir.isFile()) {
            // looks like a zipped metanode downloaded from a 4.4+ server
            File unzipped = FileUtil.createTempDir("metanode-template");
            FileUtil.unzip(localDir, unzipped);
            localDir = unzipped.listFiles()[0];
        }
        TemplateNodeContainerPersistor loadPersistor = loadHelper.createTemplateLoadPersistor(localDir, sourceURI);
        loadResultChild = new MetaNodeLinkUpdateResult("Template from " + sourceURI.toString());
        tempParent.load(loadPersistor, loadResultChild, new ExecutionMonitor(), false);
    } catch (InvalidSettingsException | URISyntaxException e) {
        throw new IOException("Unable to read template metanode: " + e.getMessage(), e);
    } finally {
        NodeContext.removeLastContext();
    }
    NodeContainerTemplate linkResult = loadResultChild.getLoadedInstance();
    MetaNodeTemplateInformation templInfo = linkResult.getTemplateInformation();
    Role sourceRole = templInfo.getRole();
    switch(sourceRole) {
        case Link:
            // (this is due to the template information link uri set above)
            break;
        default:
            throw new IOException("The source of the linked instance does " + "not represent a template but is of role " + sourceRole);
    }
    loadResult.addChildError(loadResultChild);
    return linkResult;
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) Role(org.knime.core.node.workflow.MetaNodeTemplateInformation.Role) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 70 with ExecutionMonitor

use of org.knime.core.node.ExecutionMonitor in project knime-core by knime.

the class HiliteScorerNodeModel method execute.

/**
 * Starts the scoring in the scorer.
 *
 * @param data
 *            the input data of length one
 * @param exec
 *            the execution monitor
 * @return the confusion matrix
 * @throws CanceledExecutionException
 *             if user canceled execution
 *
 * @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
 */
@SuppressWarnings("unchecked")
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] data, final ExecutionContext exec) throws CanceledExecutionException {
    // check input data
    assert (data != null && data.length == 1 && data[INPORT] != null);
    // blow away result from last execute (should have been reset anyway)
    // first try to figure out what are the different class values
    // in the two respective columns
    BufferedDataTable in = data[INPORT];
    DataTableSpec inSpec = in.getDataTableSpec();
    final int index1 = inSpec.findColumnIndex(m_firstCompareColumn);
    final int index2 = inSpec.findColumnIndex(m_secondCompareColumn);
    // two elements, first is column names, second row names;
    // these arrays are ordered already, i.e. if both columns have
    // cells in common (e.g. both have Iris-Setosa), they get the same
    // index in the array. thus, the high numbers should appear
    // in the diagonal
    DataCell[] values = determineColValues(in, index1, index2, exec.createSubProgress(0.5));
    List<DataCell> valuesList = Arrays.asList(values);
    Set<DataCell> valuesInCol2 = new HashSet<DataCell>();
    m_correctCount = 0;
    m_falseCount = 0;
    // the key store remembers the row key for later hiliting
    List[][] keyStore = new List[values.length][values.length];
    // the scorerCount counts the confusions
    m_scorerCount = new int[values.length][values.length];
    // init the matrix
    for (int i = 0; i < keyStore.length; i++) {
        for (int j = 0; j < keyStore[i].length; j++) {
            keyStore[i][j] = new ArrayList<RowKey>();
        }
    }
    int rowCnt = in.getRowCount();
    int rowNr = 0;
    ExecutionMonitor subExec = exec.createSubProgress(0.5);
    for (Iterator<DataRow> it = in.iterator(); it.hasNext(); rowNr++) {
        DataRow row = it.next();
        subExec.setProgress((1.0 + rowNr) / rowCnt, "Computing score, row " + rowNr + " (\"" + row.getKey() + "\") of " + in.getRowCount());
        try {
            subExec.checkCanceled();
        } catch (CanceledExecutionException cee) {
            reset();
            throw cee;
        }
        DataCell cell1 = row.getCell(index1);
        DataCell cell2 = row.getCell(index2);
        valuesInCol2.add(cell2);
        if (cell1.isMissing() || cell2.isMissing()) {
            continue;
        }
        boolean areEqual = cell1.equals(cell2);
        int i1 = valuesList.indexOf(cell1);
        int i2 = areEqual ? i1 : valuesList.indexOf(cell2);
        assert i1 >= 0 : "column spec lacks possible value " + cell1;
        assert i2 >= 0 : "column spec lacks possible value " + cell2;
        // i2 must be equal to i1 if cells are equal (implication)
        assert (!areEqual || i1 == valuesList.indexOf(cell2));
        keyStore[i1][i2].add(row.getKey());
        m_scorerCount[i1][i2]++;
        if (areEqual) {
            m_correctCount++;
        } else {
            m_falseCount++;
        }
    }
    m_nrRows = rowNr;
    HashSet<String> valuesAsStringSet = new HashSet<String>();
    HashSet<String> duplicateValuesAsString = new HashSet<String>();
    for (DataCell c : values) {
        valuesAsStringSet.add(c.toString());
    }
    for (DataCell c : values) {
        String cAsString = c.toString();
        if (!valuesAsStringSet.remove(cAsString)) {
            duplicateValuesAsString.add(cAsString);
        }
    }
    boolean hasPrintedWarningOnAmbiguousValues = false;
    m_values = new String[values.length];
    for (int i = 0; i < m_values.length; i++) {
        DataCell c = values[i];
        String s = c.toString();
        if (duplicateValuesAsString.contains(s)) {
            boolean isInSecondColumn = valuesInCol2.contains(c);
            int uniquifier = 1;
            if (isInSecondColumn) {
                s = s.concat(" (" + m_secondCompareColumn + ")");
            } else {
                s = s.concat(" (" + m_firstCompareColumn + ")");
            }
            String newName = s;
            while (!valuesAsStringSet.add(newName)) {
                newName = s + "#" + (uniquifier++);
            }
            m_values[i] = newName;
            if (!hasPrintedWarningOnAmbiguousValues) {
                hasPrintedWarningOnAmbiguousValues = true;
                setWarningMessage("Ambiguous value \"" + c.toString() + "\" encountered. Preserving individual instances;" + " consider to convert input columns to string");
            }
        } else {
            int uniquifier = 1;
            String newName = s;
            while (!valuesAsStringSet.add(newName)) {
                newName = s + "#" + (uniquifier++);
            }
            m_values[i] = newName;
        }
    }
    DataType[] colTypes = new DataType[m_values.length];
    Arrays.fill(colTypes, IntCell.TYPE);
    BufferedDataContainer container = exec.createDataContainer(new DataTableSpec(m_values, colTypes));
    for (int i = 0; i < m_values.length; i++) {
        // need to make a datacell for the row key
        container.addRowToTable(new DefaultRow(m_values[i], m_scorerCount[i]));
    }
    container.close();
    // print info
    int correct = getCorrectCount();
    int incorrect = getFalseCount();
    double error = getError();
    int nrRows = getNrRows();
    int missing = nrRows - correct - incorrect;
    m_keyStore = keyStore;
    LOGGER.info("error=" + error + ", #correct=" + correct + ", #false=" + incorrect + ", #rows=" + nrRows + ", #missing=" + missing);
    // our view displays the table - we must keep a reference in the model.
    BufferedDataTable result = container.getTable();
    return new BufferedDataTable[] { result };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) Point(java.awt.Point) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ExecutionMonitor (org.knime.core.node.ExecutionMonitor)160 BufferedDataTable (org.knime.core.node.BufferedDataTable)50 DataTableSpec (org.knime.core.data.DataTableSpec)43 DataRow (org.knime.core.data.DataRow)39 DataCell (org.knime.core.data.DataCell)35 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)35 Test (org.junit.Test)33 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)33 File (java.io.File)29 IOException (java.io.IOException)25 PortObject (org.knime.core.node.port.PortObject)25 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 RowKey (org.knime.core.data.RowKey)20 ArrayList (java.util.ArrayList)19 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)17 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)16 ExecutionException (java.util.concurrent.ExecutionException)14 ExecutionContext (org.knime.core.node.ExecutionContext)13 FileOutputStream (java.io.FileOutputStream)12