Example usage for weka.core Instance attribute

List of usage examples for weka.core Instance attribute

Introduction

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

Prototype

public Attribute attribute(int index);

Source Link

Document

Returns the attribute with the given index.

Usage

From source file:adams.data.conversion.AbstractMatchWekaInstanceAgainstHeader.java

License:Open Source License

/**
 * Checks the instance against the header, whether they are compatible.
 *
 * @param input   the input instance//from w  w  w. j  a v  a2s. c o m
 * @return      null if compatible, otherwise error message
 */
protected String isCompatible(Instance input) {
    String result;
    int i;
    int typeInput;
    int typeHeader;

    result = null;
    if (input.numAttributes() != m_Dataset.numAttributes())
        result = "Number of attributes differ";

    if (result == null) {
        for (i = 0; i < m_Dataset.numAttributes(); i++) {
            typeInput = input.attribute(i).type();
            typeHeader = m_Dataset.attribute(i).type();
            if (typeInput == typeHeader)
                continue;
            if ((typeInput == Attribute.NOMINAL) && (typeHeader == Attribute.STRING))
                continue;
            if ((typeInput == Attribute.STRING) && (typeHeader == Attribute.NOMINAL))
                continue;
            result = "Attribute types at #" + (i + 1) + "  are not ";
            break;
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item./*from  w w  w.ja v  a 2 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.// w  w w. java  2s. c om
 *
 * @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.WekaInstancesAppend.java

License:Open Source License

/**
 * Executes the flow item./*  www.  j ava  2  s  . c o  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    String[] filesStr;
    File[] files;
    int i;
    int n;
    Instances[] inst;
    Instances full;
    String msg;
    StringBuilder relation;
    double[] values;

    result = null;

    // get filenames
    files = null;
    inst = null;
    if (m_InputToken.getPayload() instanceof String[]) {
        filesStr = (String[]) m_InputToken.getPayload();
        files = new File[filesStr.length];
        for (i = 0; i < filesStr.length; i++)
            files[i] = new PlaceholderFile(filesStr[i]);
    } else if (m_InputToken.getPayload() instanceof File[]) {
        files = (File[]) m_InputToken.getPayload();
    } else if (m_InputToken.getPayload() instanceof Instances[]) {
        inst = (Instances[]) m_InputToken.getPayload();
    } else {
        throw new IllegalStateException("Unhandled input type: " + m_InputToken.getPayload().getClass());
    }

    // load data?
    if (files != null) {
        inst = new Instances[files.length];
        for (i = 0; i < files.length; i++) {
            try {
                inst[i] = DataSource.read(files[i].getAbsolutePath());
            } catch (Exception e) {
                result = handleException("Failed to load dataset: " + files[i], e);
                break;
            }
        }
    }

    // test compatibility
    if (result == null) {
        for (i = 0; i < inst.length - 1; i++) {
            for (n = i + 1; n < inst.length; n++) {
                if ((msg = inst[i].equalHeadersMsg(inst[n])) != null) {
                    result = "Dataset #" + (i + 1) + " and #" + (n + 1) + " are not compatible:\n" + msg;
                    break;
                }
            }
            if (result != null)
                break;
        }
    }

    // append
    if (result == null) {
        full = new Instances(inst[0]);
        relation = new StringBuilder(inst[0].relationName());
        for (i = 1; i < inst.length; i++) {
            relation.append("+" + inst[i].relationName());
            for (Instance row : inst[i]) {
                values = row.toDoubleArray();
                for (n = 0; n < values.length; n++) {
                    if (row.attribute(n).isString())
                        values[n] = full.attribute(n).addStringValue(row.stringValue(n));
                    else if (row.attribute(n).isRelationValued())
                        values[n] = full.attribute(n).addRelation(row.relationalValue(n));
                }
                if (row instanceof SparseInstance)
                    row = new SparseInstance(row.weight(), values);
                else
                    row = new DenseInstance(row.weight(), values);
                full.add(row);
            }
        }
        full.setRelationName(relation.toString());
        m_OutputToken = new Token(full);
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//  w  w  w .j a va2  s .  c o  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    SequencePlotterContainer cont;
    int[] indices;
    int i;

    result = null;

    inst = (Instance) m_InputToken.getPayload();

    m_Counter++;
    m_Containers.clear();
    m_Attributes.setMax(inst.numAttributes());
    indices = m_Attributes.getIntIndices();

    for (i = 0; i < indices.length; i++) {
        if (inst.attribute(indices[i]).isNominal())
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.stringValue(indices[i]));
        else
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.value(indices[i]));
        m_Containers.add(cont);
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from w w w  .  ja  v a  2  s.c  om
 *
 * @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();
    inst = (Instance) inst.copy();
    m_Index.setData(inst.dataset());
    index = m_Index.getIntIndex();

    try {
        if (m_Value.equals("?")) {
            inst.setMissing(index);
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                inst.setValue(index, Utils.toDouble(m_Value));
                break;

            case Attribute.DATE:
                inst.setValue(index, inst.attribute(index).parseDate(m_Value));
                break;

            case Attribute.NOMINAL:
            case Attribute.STRING:
                inst.setValue(index, m_Value);
                break;

            case Attribute.RELATIONAL:
                result = "Relational attributes cannot be set!";
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to set value: " + m_Index.getIndex() + " -> " + m_Value, e);
    }

    // broadcast data
    if (result == null)
        m_OutputToken = new Token(inst);

    return result;
}

From source file:adams.gui.menu.AppendDatasets.java

License:Open Source License

/**
 * Performs the append.//from ww  w  .  ja  v  a  2s .  com
 *
 * @param frame       the frame to close
 * @param input       the files to merge
 * @param output      the output file
 */
protected void doAppend(ChildFrame frame, File[] input, File output) {
    Instances[] data;
    Instances full;
    int i;
    int n;
    AbstractFileLoader loader;
    DataSink sink;
    int count;
    TIntArrayList transferAtt;
    int index;

    if (input.length < 2) {
        GUIHelper.showErrorMessage(getOwner(), "At least two files are required!");
        return;
    }

    // load and check compatibility
    loader = ConverterUtils.getLoaderForFile(input[0]);
    data = new Instances[input.length];
    count = 0;
    transferAtt = new TIntArrayList();
    for (i = 0; i < input.length; i++) {
        try {
            loader.setFile(input[i]);
            data[i] = DataSource.read(loader);
            if (i > 0) {
                if (!data[0].equalHeaders(data[i])) {
                    GUIHelper.showErrorMessage(getOwner(), "Datasets '" + input[0] + "' and '" + input[i]
                            + "' are not compatible!\n" + data[0].equalHeadersMsg(data[i]));
                    return;
                }
            } else {
                for (n = 0; n < data[0].numAttributes(); n++) {
                    if (data[0].attribute(n).isString() || data[0].attribute(n).isRelationValued())
                        transferAtt.add(n);
                }
            }
            count += data[i].numInstances();
        } catch (Exception e) {
            GUIHelper.showErrorMessage(getOwner(),
                    "Failed to read '" + input[i] + "'!\n" + Utils.throwableToString(e));
            return;
        }
    }

    // combine
    full = new Instances(data[0], count);
    for (i = 0; i < data.length; i++) {
        for (Instance inst : data[i]) {
            if (transferAtt.size() > 0) {
                for (n = 0; n < transferAtt.size(); n++) {
                    index = transferAtt.get(n);
                    if (inst.attribute(index).isString())
                        full.attribute(index).addStringValue(inst.stringValue(index));
                    else if (inst.attribute(n).isRelationValued())
                        full.attribute(index).addRelation(inst.relationalValue(index));
                    else
                        throw new IllegalStateException(
                                "Unhandled attribute type: " + Attribute.typeToString(inst.attribute(index)));
                }
            }
            full.add(inst);
        }
    }

    // save
    try {
        sink = new DataSink(output.getAbsolutePath());
        sink.write(full);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(getOwner(),
                "Failed to save data to '" + output + "'!\n" + Utils.throwableToString(e));
        return;
    }

    GUIHelper.showInformationMessage(null, "Successfully appended!\n" + output);
    frame.dispose();
}

From source file:adams.gui.visualization.instances.InstancesTableModel.java

License:Open Source License

/**
 * Sets the value in the cell at columnIndex and rowIndex to aValue. but only
 * the value and the value can be changed. Ignores operation if value hasn't
 * changed.//from   ww w .  ja v a2 s  .  c  o  m
 *
 * @param aValue the new value
 * @param rowIndex the row index
 * @param columnIndex the column index
 * @param notify whether to notify the listeners
 */
public void setValueAt(Object aValue, int rowIndex, int columnIndex, boolean notify) {
    int type;
    int index;
    String tmp;
    Instance inst;
    Attribute att;
    Object oldValue;
    boolean different;
    int offset;

    offset = 1;
    if (m_ShowWeightsColumn)
        offset++;

    oldValue = getValueAt(rowIndex, columnIndex);
    different = !("" + oldValue).equals("" + aValue);
    if (!different)
        return;

    if (!m_IgnoreChanges)
        addUndoPoint();

    type = getType(rowIndex, columnIndex);
    index = columnIndex - offset;
    inst = m_Data.instance(rowIndex);
    att = inst.attribute(index);

    // missing?
    if (aValue == null) {
        inst.setValue(index, Utils.missingValue());
    } else {
        tmp = aValue.toString();

        switch (type) {
        case Attribute.DATE:
            try {
                att.parseDate(tmp);
                inst.setValue(index, att.parseDate(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.NOMINAL:
            if (att.indexOfValue(tmp) > -1)
                inst.setValue(index, att.indexOfValue(tmp));
            break;

        case Attribute.STRING:
            inst.setValue(index, tmp);
            break;

        case Attribute.NUMERIC:
            try {
                inst.setValue(index, Double.parseDouble(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.RELATIONAL:
            try {
                inst.setValue(index, inst.attribute(index).addRelation((Instances) aValue));
            } catch (Exception e) {
                // ignore
            }
            break;

        default:
            throw new IllegalArgumentException("Unsupported Attribute type: " + type + "!");
        }
    }

    // notify only if the value has changed!
    if (notify)
        notifyListener(new TableModelEvent(this, rowIndex, columnIndex));
}

From source file:adams.tools.CompareDatasets.java

License:Open Source License

/**
 * Returns the correlation between the two rows.
 *
 * @param first   the first row//from  www .j  av  a 2  s.c  o m
 * @param second   the second row
 * @return      the correlation
 */
protected double getCorrelation(Instance first, Instance second) {
    double[] val1;
    double[] val2;
    int i;

    val1 = new double[m_Indices1.length];
    val2 = new double[m_Indices2.length];

    for (i = 0; i < val1.length; i++) {
        if (first.attribute(m_Indices1[i]).isNumeric())
            val1[i] = first.value(m_Indices1[i]);
        if (second.attribute(m_Indices2[i]).isNumeric())
            val2[i] = second.value(m_Indices2[i]);
    }

    return StatUtils.correlationCoefficient(val1, val2);
}

From source file:affective.core.ArffLexiconEvaluator.java

License:Open Source License

/**
 * Processes  all the dictionary files./*from  w ww.j a va  2 s  .  c  o  m*/
 * @throws IOException  an IOException will be raised if an invalid file is supplied
 */
public void processDict() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(this.m_lexiconFile));
    Instances lexInstances = new Instances(reader);

    // set upper value for word index
    lexiconWordIndex.setUpper(lexInstances.numAttributes() - 1);

    List<Attribute> numericAttributes = new ArrayList<Attribute>();
    List<Attribute> nominalAttributes = new ArrayList<Attribute>();

    // checks all numeric and nominal attributes and discards the word attribute
    for (int i = 0; i < lexInstances.numAttributes(); i++) {

        if (i != this.lexiconWordIndex.getIndex()) {
            if (lexInstances.attribute(i).isNumeric()) {
                numericAttributes.add(lexInstances.attribute(i));
                // adds the attribute name to the message-level features to be calculated
                this.featureNames.add(this.lexiconName + "-" + lexInstances.attribute(i).name());
            }

            else if (lexInstances.attribute(i).isNominal()) {
                nominalAttributes.add(lexInstances.attribute(i));
                // adds the attribute name together with the nominal value to the message-level features to be calculated
                int numValues = lexInstances.attribute(i).numValues();
                for (int j = 0; j < numValues; j++)
                    this.featureNames.add(this.lexiconName + "-" + lexInstances.attribute(i).name() + "-"
                            + lexInstances.attribute(i).value(j));

            }

        }

    }

    // Maps all words with their affective scores discarding missing values
    for (Instance inst : lexInstances) {
        if (inst.attribute(this.lexiconWordIndex.getIndex()).isString()) {
            String word = inst.stringValue(this.lexiconWordIndex.getIndex());
            // stems the word
            word = this.m_stemmer.stem(word);

            // map numeric scores
            if (!numericAttributes.isEmpty()) {
                Map<String, Double> wordVals = new HashMap<String, Double>();
                for (Attribute na : numericAttributes) {
                    if (!weka.core.Utils.isMissingValue(inst.value(na)))
                        wordVals.put(na.name(), inst.value(na));
                }
                this.numDict.put(word, wordVals);
            }

            // map nominal associations
            if (!nominalAttributes.isEmpty()) {
                Map<String, String> wordCounts = new HashMap<String, String>();
                for (Attribute no : nominalAttributes) {
                    if (!weka.core.Utils.isMissingValue(inst.value(no))) {
                        wordCounts.put(no.name(), no.value((int) inst.value(no)));
                    }

                    this.nomDict.put(word, wordCounts);

                }

            }

        }

    }

}