Example usage for weka.core Attribute STRING

List of usage examples for weka.core Attribute STRING

Introduction

In this page you can find the example usage for weka.core Attribute STRING.

Prototype

int STRING

To view the source code for weka.core Attribute STRING.

Click Source Link

Document

Constant set for attributes with string values.

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// w w  w . j a va 2s  .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.data.conversion.AbstractMatchWekaInstanceAgainstHeader.java

License:Open Source License

/**
 * Matches the input instance against the header.
 *
 * @param input   the Instance to align to the header
 * @return      the aligned Instance/*from  w w  w.j  a  v a2s  . c om*/
 */
protected Instance match(Instance input) {
    Instance result;
    double[] values;
    int i;

    values = new double[m_Dataset.numAttributes()];
    for (i = 0; i < m_Dataset.numAttributes(); i++) {
        values[i] = Utils.missingValue();
        switch (m_Dataset.attribute(i).type()) {
        case Attribute.NUMERIC:
        case Attribute.DATE:
            values[i] = input.value(i);
            break;
        case Attribute.NOMINAL:
            if (m_Dataset.attribute(i).indexOfValue(input.stringValue(i)) != -1)
                values[i] = m_Dataset.attribute(i).indexOfValue(input.stringValue(i));
            break;
        case Attribute.STRING:
            values[i] = m_Dataset.attribute(i).addStringValue(input.stringValue(i));
            break;
        case Attribute.RELATIONAL:
            values[i] = m_Dataset.attribute(i).addRelation(input.relationalValue(i));
            break;
        default:
            throw new IllegalStateException(
                    "Unhandled attribute type: " + Attribute.typeToString(m_Dataset.attribute(i).type()));
        }
    }

    if (input instanceof SparseInstance)
        result = new SparseInstance(input.weight(), values);
    else
        result = new DenseInstance(input.weight(), values);
    result.setDataset(m_Dataset);

    // fix class index, if necessary
    if ((input.classIndex() != m_Dataset.classIndex()) && (m_Dataset.classIndex() < 0))
        m_Dataset.setClassIndex(input.classIndex());

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from ww w . ja  va2  s.  com
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances inst;
    int index;
    int row;

    result = null;

    inst = (Instances) m_InputToken.getPayload();
    m_Column.setData(inst);
    m_Row.setMax(inst.numInstances());
    index = m_Column.getIntIndex();
    row = m_Row.getIntIndex();

    if (row == -1)
        result = "Failed to retrieve row: " + m_Row.getIndex();
    else if (index == -1)
        result = "Failed to retrieve column: " + m_Column.getIndex();

    if (result == null) {
        try {
            if (inst.instance(row).isMissing(index)) {
                m_OutputToken = new Token("?");
            } else {
                switch (inst.attribute(index).type()) {
                case Attribute.NUMERIC:
                    m_OutputToken = new Token(inst.instance(row).value(index));
                    break;

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

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

    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  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.WekaInstancesMerge.java

License:Open Source License

/**
 * Updates the IDs in the hashset with the ones stored in the ID attribute
 * of the provided dataset./*from   w  ww .  jav a 2 s  . c o  m*/
 *
 * @param instIndex    the dataset index
 * @param inst   the dataset to obtain the IDs from
 * @param ids      the hashset to store the IDs in
 */
protected void updateIDs(int instIndex, Instances inst, HashSet ids) {
    Attribute att;
    int i;
    boolean numeric;
    HashSet current;
    Object id;

    att = inst.attribute(m_UniqueID);
    if (att == null)
        throw new IllegalStateException("Attribute '" + m_UniqueID + "' not found in relation '"
                + inst.relationName() + "' (#" + (instIndex + 1) + ")!");

    // determine/check type
    if (m_AttType == -1) {
        if ((att.type() == Attribute.NUMERIC) || (att.type() == Attribute.STRING))
            m_AttType = att.type();
        else
            throw new IllegalStateException("Attribute '" + m_UniqueID + "' must be either NUMERIC or STRING (#"
                    + (instIndex + 1) + ")!");
    } else {
        if (m_AttType != att.type())
            throw new IllegalStateException("Attribute '" + m_UniqueID
                    + "' must have same attribute type in all the datasets (#" + (instIndex + 1) + ")!");
    }

    // get IDs
    numeric = m_AttType == Attribute.NUMERIC;
    current = new HashSet();
    for (i = 0; i < inst.numInstances(); i++) {
        if (numeric)
            id = inst.instance(i).value(att);
        else
            id = inst.instance(i).stringValue(att);
        if (m_Strict && current.contains(id))
            throw new IllegalStateException(
                    "ID '" + id + "' is not unique in dataset #" + (instIndex + 1) + "!");
        current.add(id);
    }
    ids.addAll(current);
}

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

License:Open Source License

/**
 * Merges the datasets based on the collected IDs.
 *
 * @param orig   the original datasets/*from ww  w. j  ava  2 s.co m*/
 * @param inst   the processed datasets to merge into one
 * @param ids      the IDs for identifying the rows
 * @return      the merged dataset
 */
protected Instances merge(Instances[] orig, Instances[] inst, HashSet ids) {
    Instances result;
    ArrayList<Attribute> atts;
    int i;
    int n;
    int m;
    int index;
    String relation;
    List sortedIDs;
    Attribute att;
    int[] indexStart;
    double value;
    double[] values;
    HashMap<Integer, Integer> hashmap;
    HashSet<Instance> hs;

    // create header
    if (isLoggingEnabled())
        getLogger().info("Creating merged header...");
    atts = new ArrayList<>();
    relation = "";
    indexStart = new int[inst.length];
    for (i = 0; i < inst.length; i++) {
        indexStart[i] = atts.size();
        for (n = 0; n < inst[i].numAttributes(); n++)
            atts.add((Attribute) inst[i].attribute(n).copy());
        // assemble relation name
        if (i > 0)
            relation += "_";
        relation += inst[i].relationName();
    }
    result = new Instances(relation, atts, ids.size());

    // fill with missing values
    if (isLoggingEnabled())
        getLogger().info("Filling with missing values...");
    for (i = 0; i < ids.size(); i++) {
        if (isStopped())
            return null;
        // progress
        if (isLoggingEnabled() && ((i + 1) % 1000 == 0))
            getLogger().info("" + (i + 1));
        result.add(new DenseInstance(result.numAttributes()));
    }

    // sort IDs
    if (isLoggingEnabled())
        getLogger().info("Sorting indices...");
    sortedIDs = new ArrayList(ids);
    Collections.sort(sortedIDs);

    // generate rows
    hashmap = new HashMap<>();
    for (i = 0; i < inst.length; i++) {
        if (isStopped())
            return null;
        if (isLoggingEnabled())
            getLogger().info("Adding file #" + (i + 1));
        att = orig[i].attribute(m_UniqueID);
        for (n = 0; n < inst[i].numInstances(); n++) {
            // progress
            if (isLoggingEnabled() && ((n + 1) % 1000 == 0))
                getLogger().info("" + (n + 1));

            // determine index of row
            if (m_AttType == Attribute.NUMERIC)
                index = Collections.binarySearch(sortedIDs, inst[i].instance(n).value(att));
            else
                index = Collections.binarySearch(sortedIDs, inst[i].instance(n).stringValue(att));
            if (index < 0)
                throw new IllegalStateException(
                        "Failed to determine index for row #" + (n + 1) + " of dataset #" + (i + 1) + "!");

            if (!hashmap.containsKey(index))
                hashmap.put(index, 0);
            hashmap.put(index, hashmap.get(index) + 1);

            // use internal representation for faster access
            values = result.instance(index).toDoubleArray();

            // add attribute values
            for (m = 0; m < inst[i].numAttributes(); m++) {
                // missing value?
                if (inst[i].instance(n).isMissing(m))
                    continue;

                switch (inst[i].attribute(m).type()) {
                case Attribute.NUMERIC:
                case Attribute.DATE:
                case Attribute.NOMINAL:
                    values[indexStart[i] + m] = inst[i].instance(n).value(m);
                    break;

                case Attribute.STRING:
                    value = result.attribute(indexStart[i] + m)
                            .addStringValue(inst[i].instance(n).stringValue(m));
                    values[indexStart[i] + m] = value;
                    break;

                case Attribute.RELATIONAL:
                    value = result.attribute(indexStart[i] + m)
                            .addRelation(inst[i].instance(n).relationalValue(m));
                    values[indexStart[i] + m] = value;
                    break;

                default:
                    throw new IllegalStateException("Unhandled attribute type: " + inst[i].attribute(m).type());
                }
            }

            // update row
            result.set(index, new DenseInstance(1.0, values));
        }
    }

    if (getRemove()) {
        hs = new HashSet<>();
        for (Integer x : hashmap.keySet()) {
            if (hashmap.get(x) != inst.length)
                hs.add(result.get(x));
        }
        result.removeAll(hs);
    }

    return result;
}

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

License:Open Source License

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

    result = null;

    inst = (Instances) m_InputToken.getPayload();
    inst = new Instances(inst);
    m_Row.setMax(inst.numInstances());
    m_Column.setData(inst);
    row = m_Row.getIntIndex();
    index = m_Column.getIntIndex();

    if (row == -1)
        result = "Failed to retrieve row: " + m_Row.getIndex();
    else if (index == -1)
        result = "Failed to retrieve column: " + m_Column.getIndex();

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

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

                case Attribute.NOMINAL:
                case Attribute.STRING:
                    inst.instance(row).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_Column.getIndex() + " -> " + m_Value, e);
        }
    }

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

    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 2s.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();
    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.visualization.instance.InstanceExplorer.java

License:Open Source License

/**
 * pops up file dialog for loading dataset form disk.
 *
 * @param file   an optional file, use null to ignore
 *///from  ww  w  .j a v  a2s . co m
public void loadDataFromDisk(File file) {
    if (m_LoadFromDiskDialog == null) {
        if (getParentDialog() != null)
            m_LoadFromDiskDialog = new LoadDatasetDialog(getParentDialog());
        else
            m_LoadFromDiskDialog = new LoadDatasetDialog(getParentFrame());
        m_LoadFromDiskDialog.setCurrent(new File(getProperties().getPath("InitialDir", "%h")));
        m_LoadFromDiskDialog.setDefaultAttributeRange(getProperties().getPath("AttributeRange", "first-last"));
        m_LoadFromDiskDialog.setDefaultClassIndex(getProperties().getPath("ClassIndex", ""));
        m_LoadFromDiskDialog.setDefaultSortIndex(getProperties().getPath("SortIndex", ""));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NUMERIC,
                getProperties().getBoolean("IncludeNumericAttributes", true));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.DATE,
                getProperties().getBoolean("IncludeDateAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NOMINAL,
                getProperties().getBoolean("IncludeNominalAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.STRING,
                getProperties().getBoolean("IncludeStringAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.RELATIONAL,
                getProperties().getBoolean("IncludeRelationalAttributes", false));
        m_LoadFromDiskDialog.setAcceptListener((ChangeEvent e) -> {
            int[] indices = m_LoadFromDiskDialog.getIndices();
            if (indices == null)
                return;
            if (m_RecentFilesHandler != null)
                m_RecentFilesHandler.addRecentItem(m_LoadFromDiskDialog.getCurrent());

            HashSet<Integer> attTypes = new HashSet<>();
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NUMERIC))
                attTypes.add(Attribute.NUMERIC);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.DATE))
                attTypes.add(Attribute.DATE);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NOMINAL))
                attTypes.add(Attribute.NOMINAL);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.STRING))
                attTypes.add(Attribute.STRING);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.RELATIONAL))
                attTypes.add(Attribute.RELATIONAL);

            showStatus("Loading data...");
            List<InstanceContainer> data = new ArrayList<>();
            Instances dataset = m_LoadFromDiskDialog.getDataset();
            int[] additional = m_LoadFromDiskDialog.getAdditionalAttributes();
            Range range = m_LoadFromDiskDialog.getCurrentAttributeRange();
            int id = m_LoadFromDiskDialog.getCurrentIDIndex();
            for (int i = 0; i < indices.length; i++) {
                weka.core.Instance winst = dataset.instance(indices[i]);
                Instance inst = new Instance();
                inst.set(winst, i, additional, range, attTypes);
                if (id == -1) {
                    inst.setID((indices[i] + 1) + "." + dataset.relationName());
                } else {
                    if (winst.attribute(id).isNumeric())
                        inst.setID("" + winst.value(id));
                    else
                        inst.setID(winst.stringValue(id));
                }
                data.add(getContainerManager().newContainer(inst));
                showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances());
            }
            loadData(dataset, data);

            showStatus("");
        });
    }

    if (file != null)
        m_LoadFromDiskDialog.setCurrent(file);
    m_LoadFromDiskDialog.setVisible(true);
}

From source file:adams.gui.visualization.instance.InstanceTableModel.java

License:Open Source License

/**
 * Returns the value at the given position.
 *
 * @param row   the row in the table/*from   ww w .  j av  a 2  s  . c o m*/
 * @param column   the column in the table
 * @return      the value
 */
public Object getValueAt(int row, int column) {
    Attribute att;

    att = getAttribute(column);
    if (column == 0) {
        return row + 1;
    } else if (att == null) {
        return "";
    } else if (m_Data.instance(row).isMissing(att)) {
        return null;
    } else if (att.name().equals(ArffUtils.getDBIDName())) {
        return (int) m_Data.instance(row).value(att);
    } else if (att.name().equals(ArffUtils.getIDName())) {
        return m_Data.instance(row).stringValue(att).replaceAll("\'", "");
    } else {
        switch (att.type()) {
        case Attribute.NUMERIC:
            return m_Data.instance(row).value(att);

        case Attribute.DATE:
        case Attribute.NOMINAL:
        case Attribute.STRING:
        case Attribute.RELATIONAL:
            return m_Data.instance(row).stringValue(att);

        default:
            return "???";
        }
    }
}