Example usage for weka.core Attribute RELATIONAL

List of usage examples for weka.core Attribute RELATIONAL

Introduction

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

Prototype

int RELATIONAL

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

Click Source Link

Document

Constant set for relation-valued attributes.

Usage

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/*  ww w  .  j av a2 s .  c  o  m*/
 */
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.//  ww 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;
    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./*  w ww  .  j a  va  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

/**
 * Merges the datasets based on the collected IDs.
 *
 * @param orig   the original datasets/*w w  w .  j a v  a  2s  . c  o  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  ww  .j a  v  a 2  s .com*/
 *
 * @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.//  w  w w.  java 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();
    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  www. ja  va 2s .c o 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//ww  w . jav  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 "???";
        }
    }
}

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

License:Open Source License

/**
 * Returns the class for the column.//from w  ww.j a  v a2  s.  c  om
 *
 * @param column   the column to retrieve the class for
 * @return      the class
 */
public Class getColumnClass(int column) {
    Attribute att;

    att = getAttribute(column);
    if (column == 0) {
        return Integer.class;
    } else if (att == null) {
        return String.class;
    } else if (att.name().equals(ArffUtils.getDBIDName())) { // special case
        return Integer.class;
    } else {
        switch (att.type()) {
        case Attribute.NUMERIC:
            return Double.class;

        case Attribute.DATE:
        case Attribute.NOMINAL:
        case Attribute.STRING:
        case Attribute.RELATIONAL:
            return String.class;

        default:
            return String.class;
        }
    }
}

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

License:Open Source License

/**
 * Sets the default for the specified type of attribute.
 *
 * @param attType   the attribute type/*from w w w  .  j  a v  a 2s.  c om*/
 * @param value   true if to restrict to attribute type by default
 * @see      Attribute
 */
public void setDefaultIncludeAttributes(int attType, boolean value) {
    switch (attType) {
    case Attribute.NUMERIC:
        m_DefaultIncludeNumericAttributes = value;
        break;
    case Attribute.DATE:
        m_DefaultIncludeDateAttributes = value;
        break;
    case Attribute.NOMINAL:
        m_DefaultIncludeNominalAttributes = value;
        break;
    case Attribute.STRING:
        m_DefaultIncludeStringAttributes = value;
        break;
    case Attribute.RELATIONAL:
        m_DefaultIncludeDateAttributes = value;
        break;
    default:
        System.err.println(getClass().getName() + ": unhandled attribute type " + attType);
    }
}