Search in sources :

Example 66 with NodeSettingsWO

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

the class Sampler method createRandomNumberRowFilter.

/*
     * Creates random number row filter that samples count rows from a table
     * with overall allCount rows.
     */
private static final IRowFilter createRandomNumberRowFilter(final long count, final long allCount, final Random rand) {
    Random random = rand != null ? rand : new Random();
    if (allCount <= count) {
        return new TrueRowFilter();
    }
    if (allCount <= Integer.MAX_VALUE) {
        BitSet bitset = new BitSet((int) allCount);
        // hm, I'm sure there is a better way to draw arbitrary bits
        int[] vals = new int[(int) allCount];
        for (int i = 0; i < vals.length; i++) {
            vals[i] = i;
        }
        for (int i = vals.length; --i >= 0; ) {
            int swapIndex = random.nextInt(i + 1);
            int swap = vals[swapIndex];
            vals[swapIndex] = vals[i];
            vals[i] = swap;
        }
        for (int i = 0; i < count; i++) {
            bitset.set(vals[i]);
        }
        return new RandomNumberRowFilter(bitset);
    } else {
        // Sampling based on Fan's selection rejection algorithm (1962)
        return new AbstractRowFilter() {

            private long m_i;

            @Override
            public boolean matches(final DataRow row, final long rowIndex) throws EndOfTableException, IncludeFromNowOn {
                double p = (count - m_i) / (double) (allCount - rowIndex + 1);
                if (random.nextDouble() <= p) {
                    m_i++;
                    return true;
                }
                return false;
            }

            @Override
            protected void saveSettings(final NodeSettingsWO cfg) {
                throw new UnsupportedOperationException();
            }

            @Override
            public void loadSettingsFrom(final NodeSettingsRO cfg) throws InvalidSettingsException {
                throw new UnsupportedOperationException();
            }

            @Override
            public DataTableSpec configure(final DataTableSpec inSpec) throws InvalidSettingsException {
                throw new UnsupportedOperationException();
            }
        };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) BitSet(java.util.BitSet) DataRow(org.knime.core.data.DataRow) Random(java.util.Random) AbstractRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.AbstractRowFilter) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) TrueRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.TrueRowFilter)

Example 67 with NodeSettingsWO

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

the class AutoHiLiteNodeFactory method createNodeModel.

/**
 * {@inheritDoc}
 */
@Override
public NodeModel createNodeModel() {
    return new NodeModel(1, 1) {

        private SettingsModelBoolean m_smClearHiLites = createClearHilitesModel();

        /**
         * {@inheritDoc}
         */
        @Override
        protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
            return inSpecs;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
            if (m_smClearHiLites.getBooleanValue()) {
                getInHiLiteHandler(0).fireClearHiLiteEvent();
            }
            final Set<RowKey> keys = new HashSet<RowKey>();
            final HiLiteHandler hlh = getInHiLiteHandler(0);
            long counter = 0;
            long numOfRows = inData[0].size();
            for (final DataRow row : inData[0]) {
                keys.add(row.getKey());
                if (keys.size() == NUMBER_OF_ROWS_HILITED_AT_ONCE) {
                    exec.setProgress(++counter * NUMBER_OF_ROWS_HILITED_AT_ONCE / (double) numOfRows, "HiLiting all rows...");
                    hlh.fireHiLiteEvent(keys);
                    keys.clear();
                }
            }
            hlh.fireHiLiteEvent(keys);
            // wait for hilite to propagate
            ViewUtils.invokeAndWaitInEDT(() -> {
            });
            return inData;
        }

        @Override
        protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
        }

        @Override
        protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
        }

        @Override
        protected void saveSettingsTo(final NodeSettingsWO settings) {
            m_smClearHiLites.saveSettingsTo(settings);
        }

        @Override
        protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
            m_smClearHiLites.validateSettings(settings);
        }

        @Override
        protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException {
            m_smClearHiLites.loadSettingsFrom(settings);
        }

        @Override
        protected void reset() {
        // no op
        }
    };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) RowKey(org.knime.core.data.RowKey) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DataRow(org.knime.core.data.DataRow) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) NodeModel(org.knime.core.node.NodeModel) ExecutionContext(org.knime.core.node.ExecutionContext) BufferedDataTable(org.knime.core.node.BufferedDataTable) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) File(java.io.File) HashSet(java.util.HashSet)

Example 68 with NodeSettingsWO

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

the class AccuracyScorerNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File internDir, final ExecutionMonitor exec) throws IOException {
    NodeSettings set = new NodeSettings("scorer");
    set.addInt("correctCount", m_viewData.getCorrectCount());
    set.addInt("falseCount", m_viewData.getFalseCount());
    set.addInt("nrRows", m_viewData.getNrRows());
    set.addStringArray("values", m_viewData.getTargetValues());
    final int targetValueCount = m_viewData.getTargetValues().length;
    for (int i = 0; i < targetValueCount; i++) {
        NodeSettingsWO sub = set.addNodeSettings(m_viewData.getTargetValues()[i]);
        sub.addIntArray("scorerCount", m_viewData.getScorerCount()[i]);
        NodeSettingsWO subSub = sub.addNodeSettings("hilightMap");
        for (int j = 0; j < targetValueCount; j++) {
            NodeSettingsWO sub3 = subSub.addNodeSettings(m_viewData.getTargetValues()[j]);
            RowKey[] rowKeys = m_viewData.getKeyStore()[i][j].toArray(new RowKey[m_viewData.getKeyStore()[i][j].size()]);
            sub3.addRowKeyArray("keyStore", rowKeys);
        }
    }
    set.saveToXML(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(new File(internDir, "internals.xml.gz")))));
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) RowKey(org.knime.core.data.RowKey) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 69 with NodeSettingsWO

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

the class EntropyCalculator method save.

/**
 * Saves the structure of this objec to the target directory.
 *
 * @param dir to save to
 * @param exec for progress/cancel
 * @throws IOException if that fails
 * @throws CanceledExecutionException if canceled
 */
public void save(final File dir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File scorerFile = new File(dir, FILE_SCORER_TABLE);
    DataContainer.writeToZip(m_scoreTable, scorerFile, exec);
    File settingsFile = new File(dir, FILE_SETTINGS);
    NodeSettings config = new NodeSettings(CFG_SETTINGS);
    config.addDouble(CFG_ENTROPY, m_entropy);
    config.addDouble(CFG_QUALITY, m_quality);
    config.addInt(CFG_PAT_IN_CLUSTER, m_patternsInClusters);
    config.addInt(CFG_PAT_IN_REFERENCE, m_patternsInReference);
    config.addInt(CFG_NR_CLUSTER, m_nrClusters);
    config.addInt(CFG_NR_REFERENCES, m_nrReference);
    NodeSettingsWO subConfig = config.addNodeSettings(CFG_CLUSTERING_MAP);
    for (Map.Entry<RowKey, Set<RowKey>> entry : m_clusteringMap.entrySet()) {
        exec.checkCanceled();
        RowKey key = entry.getKey();
        Set<RowKey> values = entry.getValue();
        NodeSettingsWO keySettings = subConfig.addNodeSettings(key.toString());
        keySettings.addRowKey(key.toString(), key);
        keySettings.addRowKeyArray(CFG_MAPPED_KEYS, values.toArray(new RowKey[values.size()]));
    }
    config.saveToXML(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(settingsFile))));
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) RowKey(org.knime.core.data.RowKey) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BufferedOutputStream(java.io.BufferedOutputStream)

Example 70 with NodeSettingsWO

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

the class AddEmptyRowsConfig method saveSettingsTo.

/**
 * Saves current configuration.
 *
 * @param settings To save to.
 */
void saveSettingsTo(final NodeSettingsWO settings) {
    settings.addBoolean("atLeastMode", m_atLeastMode);
    settings.addInt("rowCount", m_rowCount);
    settings.addString("newRowKeyPrefix", m_newRowKeyPrefix);
    NodeSettingsWO doubleSet = settings.addNodeSettings("double");
    doubleSet.addBoolean("useMissing", m_useMissingDouble);
    doubleSet.addDouble("fillValue", m_fillValueDouble);
    NodeSettingsWO intSet = settings.addNodeSettings("int");
    intSet.addBoolean("useMissing", m_useMissingInt);
    intSet.addInt("fillValue", m_fillValueInt);
    NodeSettingsWO stringSet = settings.addNodeSettings("String");
    stringSet.addBoolean("useMissing", m_useMissingString);
    stringSet.addString("fillValue", m_fillValueString);
}
Also used : NodeSettingsWO(org.knime.core.node.NodeSettingsWO)

Aggregations

NodeSettingsWO (org.knime.core.node.NodeSettingsWO)111 NodeSettings (org.knime.core.node.NodeSettings)16 Map (java.util.Map)11 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 HashMap (java.util.HashMap)8 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 RowKey (org.knime.core.data.RowKey)6 DataCell (org.knime.core.data.DataCell)5 BufferedOutputStream (java.io.BufferedOutputStream)4 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)4 HashSet (java.util.HashSet)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 DataRow (org.knime.core.data.DataRow)3 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)3 AbstractQuickFormConfiguration (org.knime.core.quickform.AbstractQuickFormConfiguration)3 AbstractQuickFormValueInConfiguration (org.knime.core.quickform.AbstractQuickFormValueInConfiguration)3 QuickFormInputNode (org.knime.core.quickform.in.QuickFormInputNode)3 MutableInteger (org.knime.core.util.MutableInteger)3