Example usage for weka.core Instance dataset

List of usage examples for weka.core Instance dataset

Introduction

In this page you can find the example usage for weka.core Instance dataset.

Prototype

public Instances dataset();

Source Link

Document

Returns the dataset this instance has access to.

Usage

From source file:PrincipalComponents.java

License:Open Source License

/**
 * Transform an instance in original (unormalized) format. Convert back to
 * the original space if requested./*w w w.j  a v  a2s  .co m*/
 *
 * @param instance an instance in the original (unormalized) format
 * @return a transformed instance
 * @throws Exception if instance cant be transformed
 */
@Override
public Instance convertInstance(Instance instance) throws Exception {

    if (m_eigenvalues == null) {
        throw new Exception("convertInstance: Principal components not " + "built yet");
    }

    double[] newVals = new double[m_outputNumAtts];
    Instance tempInst = (Instance) instance.copy();
    if (!instance.dataset().equalHeaders(m_trainHeader)) {
        throw new Exception("Can't convert instance: header's don't match: " + "PrincipalComponents\n"
                + instance.dataset().equalHeadersMsg(m_trainHeader));
    }

    m_replaceMissingFilter.input(tempInst);
    m_replaceMissingFilter.batchFinished();
    tempInst = m_replaceMissingFilter.output();

    /*
     * if (m_normalize) { m_normalizeFilter.input(tempInst);
     * m_normalizeFilter.batchFinished(); tempInst =
     * m_normalizeFilter.output(); }
     */

    m_nominalToBinFilter.input(tempInst);
    m_nominalToBinFilter.batchFinished();
    tempInst = m_nominalToBinFilter.output();

    if (m_attributeFilter != null) {
        m_attributeFilter.input(tempInst);
        m_attributeFilter.batchFinished();
        tempInst = m_attributeFilter.output();
    }

    if (!m_center) {
        m_standardizeFilter.input(tempInst);
        m_standardizeFilter.batchFinished();
        tempInst = m_standardizeFilter.output();
    } else {
        m_centerFilter.input(tempInst);
        m_centerFilter.batchFinished();
        tempInst = m_centerFilter.output();
    }

    if (m_hasClass) {
        newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex());
    }

    double cumulative = 0;
    int numAttAdded = 0;
    for (int i = m_numAttribs - 1; i >= 0; i--) {
        double tempval = 0.0;
        for (int j = 0; j < m_numAttribs; j++) {
            tempval += (m_eigenvectors[j][m_sortedEigens[i]] * tempInst.value(j));
        }
        newVals[m_numAttribs - i - 1] = tempval;
        cumulative += m_eigenvalues[m_sortedEigens[i]];
        if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) {
            break;
        }
        if (numAttAdded > m_maxNumAttr) {
            break;
        }
        numAttAdded++;
    }

    if (!m_transBackToOriginal) {
        if (instance instanceof SparseInstance) {
            return new SparseInstance(instance.weight(), newVals);
        } else {
            return new DenseInstance(instance.weight(), newVals);
        }
    } else {
        if (instance instanceof SparseInstance) {
            return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals));
        } else {
            return convertInstanceToOriginal(new DenseInstance(instance.weight(), newVals));
        }
    }
}

From source file:adams.data.instances.InstanceComparator.java

License:Open Source License

/**
 * Compares its two arguments for order.  Returns a negative integer,
 * zero, or a positive integer as the first argument is less than, equal
 * to, or greater than the second.//www.java2  s  .c o  m
 *
 * @param o1 the first object to be compared.
 * @param o2 the second object to be compared.
 * @return a negative integer, zero, or a positive integer as the
 *           first argument is less than, equal to, or greater than the
 *          second.
 */
@Override
public int compare(Instance o1, Instance o2) {
    int result;
    Instances header;
    int i;
    int weight;
    double d1;
    double d2;

    result = 0;
    header = o1.dataset();
    i = 0;
    while ((result == 0) && (i < m_Indices.length)) {
        if (o1.isMissing(m_Indices[i]) && o2.isMissing(m_Indices[i]))
            result = 0;
        else if (o1.isMissing(m_Indices[i]))
            result = -1;
        else if (o2.isMissing(m_Indices[i]))
            result = +1;
        else if (header.attribute(m_Indices[i]).isNumeric()) {
            d1 = o1.value(m_Indices[i]);
            d2 = o2.value(m_Indices[i]);
            if (d1 < d2)
                result = -1;
            else if (d1 == d2)
                result = 0;
            else
                result = +1;
        } else {
            result = o1.stringValue(m_Indices[i]).compareTo(o2.stringValue(m_Indices[i]));
        }

        if (!m_Ascending[i])
            result = -result;

        // add weight to index
        weight = (int) Math.pow(10, (m_Indices.length - i));
        result *= weight;

        i++;
    }

    return result;
}

From source file:adams.flow.sink.WekaDatabaseWriter.java

License:Open Source License

/**
 * Executes the flow item./*  w ww  .j  a v  a 2  s .c o  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    Instance inst;

    result = null;

    if (m_InputToken.getPayload() instanceof Instance) {
        inst = (Instance) m_InputToken.getPayload();
        data = inst.dataset();
    } else {
        data = (Instances) m_InputToken.getPayload();
        inst = null;
    }

    try {
        if (m_Saver == null) {
            m_Saver = new DatabaseSaver();
            m_Saver.setUrl(m_URL);
            m_Saver.setUser(m_User);
            m_Saver.setPassword(m_Password.getValue());
            m_Saver.setTableName(m_TableName);
            m_Saver.setRelationForTableName(m_UseRelationNameAsTable);
            m_Saver.setAutoKeyGeneration(m_AutoKeyGeneration);
            if (!m_CustomPropsFile.isDirectory())
                m_Saver.setCustomPropsFile(m_CustomPropsFile.getAbsoluteFile());
        }
        if (inst == null) {
            m_Saver.setInstances(data);
            m_Saver.writeBatch();
        } else {
            m_Saver.writeIncremental(inst);
        }
    } catch (Exception e) {
        result = handleException("Failed to write to database: " + m_URL, e);
    }

    return result;
}

From source file:adams.flow.sink.WekaInstanceViewer.java

License:Open Source License

/**
 * Displays the token (the panel and dialog have already been created at
 * this stage).//from w w  w. j a va 2  s.  c o  m
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    InstanceContainerManager manager;
    InstanceContainer cont;
    weka.core.Instance winst;
    weka.core.Attribute att;
    String id;
    adams.data.instance.Instance inst;

    if (token.getPayload() instanceof weka.core.Instance) {
        winst = (weka.core.Instance) token.getPayload();
        inst = new adams.data.instance.Instance();
        inst.set(winst);
        if (!m_ID.isEmpty()) {
            att = winst.dataset().attribute(m_ID);
            if (att != null) {
                if (att.isNominal() || att.isString())
                    id = winst.stringValue(att.index());
                else
                    id = "" + winst.value(att.index());
                inst.setID(id);
            }
        }
    } else {
        inst = (adams.data.instance.Instance) token.getPayload();
        if (inst.hasReport() && inst.getReport().hasValue(m_ID))
            inst.setID("" + inst.getReport().getValue(new Field(m_ID, DataType.UNKNOWN)));
    }

    manager = m_InstancePanel.getContainerManager();
    cont = manager.newContainer(inst);
    manager.startUpdate();
    manager.add(cont);

    m_Updater.update(m_InstancePanel, cont);
}

From source file:adams.flow.transformer.WekaDatasetsMerge.java

License:Open Source License

/**
 * Creates an Instances dataset, containing a copy of the single instance
 * provided./*from  w w  w .  java2s  . c o m*/
 *
 * @param instance The instance to create a dataset for.
 * @return The created dataset.
 */
protected Instances datasetForSingleInstance(Instance instance) {
    // Create a copy of the instance's original dataset
    Instances dataset = new Instances(instance.dataset(), 1);

    // Add a copy of the provided instance
    dataset.add((Instance) instance.copy());

    // Return the dataset
    return dataset;
}

From source file:adams.flow.transformer.WekaFilter.java

License:Open Source License

/**
 * Executes the flow item./*from ww  w . ja  va 2  s. c  om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    weka.core.Instances data;
    weka.core.Instances filteredData;
    weka.core.Instance inst;
    adams.data.instance.Instance instA;
    weka.core.Instance filteredInst;
    String relation;

    result = null;

    data = null;
    inst = null;
    if (m_InputToken.hasPayload(weka.core.Instance.class))
        inst = m_InputToken.getPayload(weka.core.Instance.class);
    else if (m_InputToken.hasPayload(adams.data.instance.Instance.class))
        inst = m_InputToken.getPayload(adams.data.instance.Instance.class).toInstance();
    else if (m_InputToken.hasPayload(weka.core.Instances.class))
        data = m_InputToken.getPayload(weka.core.Instances.class);
    else
        result = m_InputToken.unhandledData();

    if (result == null) {
        try {
            // initialize filter?
            if (!m_Initialized || !m_InitializeOnce) {
                if (data == null) {
                    data = new weka.core.Instances(inst.dataset(), 0);
                    data.add(inst);
                }
                initActualFilter(data);
            }

            synchronized (m_ActualFilter) {
                if (!m_FlowContextUpdated) {
                    m_FlowContextUpdated = true;
                    if (m_ActualFilter instanceof FlowContextHandler)
                        ((FlowContextHandler) m_ActualFilter).setFlowContext(this);
                }

                // filter data
                filteredData = null;
                filteredInst = null;
                if (data != null) {
                    relation = data.relationName();
                    filteredData = weka.filters.Filter.useFilter(data, m_ActualFilter);
                    if (m_KeepRelationName) {
                        filteredData.setRelationName(relation);
                        if (isLoggingEnabled())
                            getLogger().info("Setting relation name: " + relation);
                    }
                    m_Initialized = true;
                } else {
                    relation = inst.dataset().relationName();
                    m_ActualFilter.input(inst);
                    m_ActualFilter.batchFinished();
                    filteredInst = m_ActualFilter.output();
                    if (m_KeepRelationName) {
                        filteredInst.dataset().setRelationName(relation);
                        if (isLoggingEnabled())
                            getLogger().info("Setting relation name: " + relation);
                    }
                }
            }

            // build output token
            if (inst != null) {
                if (filteredInst != null) {
                    if (m_InputToken.getPayload() instanceof weka.core.Instance) {
                        m_OutputToken = new Token(filteredInst);
                    } else {
                        instA = new adams.data.instance.Instance();
                        instA.set(filteredInst);
                        m_OutputToken = createToken(m_InputToken.getPayload(), instA);
                    }
                } else if ((filteredData != null) && (filteredData.numInstances() > 0)) {
                    m_OutputToken = createToken(m_InputToken.getPayload(), filteredData.instance(0));
                }
            } else {
                m_OutputToken = createToken(m_InputToken.getPayload(), filteredData);
            }
        } catch (Exception e) {
            result = handleException("Failed to filter data: ", e);
        }
    }

    if (m_OutputToken != null)
        updateProvenance(m_OutputToken);

    return result;
}

From source file:adams.flow.transformer.WekaGetInstanceValue.java

License:Open Source License

/**
 * Executes the flow item./*from w  w  w.  j  a  v a2  s . c  o m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    int index;

    result = null;

    inst = (Instance) m_InputToken.getPayload();

    try {
        if (m_AttributeName.length() > 0) {
            index = inst.dataset().attribute(m_AttributeName).index();
        } else {
            m_Index.setMax(inst.numAttributes());
            index = m_Index.getIntIndex();
        }
        if (inst.isMissing(index)) {
            m_OutputToken = new Token("?");
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                m_OutputToken = new Token(inst.value(index));
                break;

            case Attribute.DATE:
            case Attribute.NOMINAL:
            case Attribute.STRING:
            case Attribute.RELATIONAL:
                m_OutputToken = new Token(inst.stringValue(index));
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to obtain value from instance:\n" + inst, e);
    }

    return result;
}

From source file:adams.flow.transformer.WekaInstanceBuffer.java

License:Open Source License

/**
 * Executes the flow item./*from  w  w  w.ja v a2 s.co  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance[] insts;
    Instance inst;
    double[] values;
    int i;
    int n;
    boolean updated;

    result = null;

    if (m_Operation == Operation.INSTANCE_TO_INSTANCES) {
        if (m_InputToken.getPayload() instanceof Instance) {
            insts = new Instance[] { (Instance) m_InputToken.getPayload() };
        } else {
            insts = (Instance[]) m_InputToken.getPayload();
        }

        for (n = 0; n < insts.length; n++) {
            inst = insts[n];

            if ((m_Buffer != null) && m_CheckHeader) {
                if (!m_Buffer.equalHeaders(inst.dataset())) {
                    getLogger().info("Header changed, resetting buffer");
                    m_Buffer = null;
                }
            }

            // buffer instance
            if (m_Buffer == null)
                m_Buffer = new Instances(inst.dataset(), 0);

            // we need to make sure that string and relational values are in our
            // buffer header and update the current Instance accordingly before
            // buffering it
            values = inst.toDoubleArray();
            updated = false;
            for (i = 0; i < values.length; i++) {
                if (inst.isMissing(i))
                    continue;
                if (inst.attribute(i).isString()) {
                    values[i] = m_Buffer.attribute(i).addStringValue(inst.stringValue(i));
                    updated = true;
                } else if (inst.attribute(i).isRelationValued()) {
                    values[i] = m_Buffer.attribute(i).addRelation(inst.relationalValue(i));
                    updated = true;
                }
            }

            if (updated) {
                if (inst instanceof SparseInstance) {
                    inst = new SparseInstance(inst.weight(), values);
                } else if (inst instanceof BinarySparseInstance) {
                    inst = new BinarySparseInstance(inst.weight(), values);
                } else {
                    if (!(inst instanceof DenseInstance)) {
                        getLogger().severe("Unhandled instance class (" + inst.getClass().getName() + "), "
                                + "defaulting to " + DenseInstance.class.getName());
                    }
                    inst = new DenseInstance(inst.weight(), values);
                }
            } else {
                inst = (Instance) inst.copy();
            }

            m_Buffer.add(inst);
        }

        if (m_Buffer.numInstances() % m_Interval == 0) {
            m_OutputToken = new Token(m_Buffer);
            if (m_ClearBuffer)
                m_Buffer = null;
        }
    } else if (m_Operation == Operation.INSTANCES_TO_INSTANCE) {
        m_Buffer = (Instances) m_InputToken.getPayload();
        m_Iterator = m_Buffer.iterator();
    } else {
        throw new IllegalStateException("Unhandled operation: " + m_Operation);
    }

    return result;
}

From source file:adams.flow.transformer.WekaInstanceDumper.java

License:Open Source License

/**
 * Executes the flow item./* ww w .j a va  2s  .c  om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    Instances newHeader;
    double[] values;
    boolean append;
    ArrayList<Attribute> atts;
    int i;

    result = null;

    if (m_InputToken.getPayload() instanceof Instance) {
        inst = (Instance) m_InputToken.getPayload();
        // get header and optionally compare it to previous (to start a new
        // output file)
        newHeader = inst.dataset();
    } else {
        values = (double[]) m_InputToken.getPayload();
        // create artificial dataset header
        atts = new ArrayList<>();
        for (i = 0; i < values.length; i++)
            atts.add(new Attribute("att_" + (i + 1)));
        newHeader = new Instances(getName(), atts, 0);
        inst = new DenseInstance(1.0, values);
        inst.setDataset(newHeader);
    }

    append = true;
    if (m_Header == null) {
        m_Header = new Instances(newHeader, 0);
        if (!m_KeepExisting)
            append = false;
    } else {
        if (m_CheckHeader) {
            if (!m_Header.equalHeaders(newHeader)) {
                m_Counter++;
                m_Header = new Instances(newHeader, 0);
                append = false;
            }
        }
    }

    if (!append)
        FileUtils.delete(createFilename(inst.dataset()).getAbsolutePath());

    // buffer data and write to disk if necessary
    m_Buffer.add(inst);
    if (m_Buffer.size() >= m_BufferSize)
        result = writeToDisk(append);

    // broadcast name
    if (result == null)
        m_OutputToken = new Token(createFilename(inst.dataset()).getAbsolutePath());

    return result;
}

From source file:adams.flow.transformer.WekaInstanceEvaluator.java

License:Open Source License

/**
 * Generates the new header for the data.
 *
 * @param inst   the instance to get the original data format from
 * @return      null if everything is fine, otherwise error message
 * @see      #m_Header// ww w . j ava  2s  .  co m
 * @see      #m_Filter
 */
protected String generateHeader(Instance inst) {
    String result;

    result = null;

    m_Filter = new Add();
    m_Filter.setAttributeName(determineAttributeName(inst.dataset()));
    m_Filter.setAttributeType(new SelectedTag(Attribute.NUMERIC, Add.TAGS_TYPE));
    if (inst.dataset().classIndex() == inst.dataset().numAttributes() - 1)
        m_Filter.setAttributeIndex("" + inst.dataset().numAttributes());
    else
        m_Filter.setAttributeIndex("" + (inst.dataset().numAttributes() + 1));
    try {
        m_Filter.setInputFormat(inst.dataset());
        m_Header = weka.filters.Filter.useFilter(inst.dataset(), m_Filter);
    } catch (Exception e) {
        result = handleException("Failed to generate header:", e);
    }

    return result;
}