Example usage for weka.core Instances relationName

List of usage examples for weka.core Instances relationName

Introduction

In this page you can find the example usage for weka.core Instances relationName.

Prototype


publicString relationName() 

Source Link

Document

Returns the relation's name.

Usage

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

License:Open Source License

/**
 * Generates the filename for the output.
 *
 * @param header   the current relation/*from   w ww.  j a  v a 2  s. c o  m*/
 * @return      the generated filename
 */
protected File createFilename(Instances header) {
    String result;
    File file;

    if (m_UseRelationNameAsFilename) {
        file = new File(m_OutputPrefix.getAbsolutePath());
        result = file.getParent() + File.separator + FileUtils.createFilename(header.relationName(), "_");
    } else {
        result = m_OutputPrefix.getAbsolutePath();
    }

    if (m_Counter > 0)
        result += "-" + m_Counter;

    switch (m_OutputFormat) {
    case ARFF:
        result += ArffLoader.FILE_EXTENSION;
        break;
    case CSV:
        result += CSVLoader.FILE_EXTENSION;
        break;
    case TAB:
        result += CSVLoader.FILE_EXTENSION;
        break;
    default:
        throw new IllegalStateException("Unhandled output format: " + m_OutputFormat);
    }

    return new File(result);
}

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

License:Open Source License

/**
 * Executes the flow item.//w  w w .  j a va 2  s  .  com
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances inst;
    int index;
    int labelIndex;
    double[] dist;
    Enumeration enm;
    int i;

    result = null;

    if (m_InputToken.getPayload() instanceof Instance)
        inst = ((Instance) m_InputToken.getPayload()).dataset();
    else
        inst = (Instances) m_InputToken.getPayload();
    m_AttributeIndex.setData(inst);
    index = m_AttributeIndex.getIntIndex();

    m_Queue.clear();

    switch (m_Type) {
    case FULL:
        m_Queue.add(inst.toSummaryString());
        break;

    case FULL_ATTRIBUTE:
        m_Queue.add(getAttributeStats(inst, index));
        break;

    case FULL_CLASS:
        if (inst.classIndex() > -1)
            m_Queue.add(getAttributeStats(inst, inst.classIndex()));
        break;

    case HEADER:
        m_Queue.add(new Instances(inst, 0).toString());
        break;

    case RELATION_NAME:
        m_Queue.add(inst.relationName());
        break;

    case ATTRIBUTE_NAME:
        if (index != -1)
            m_Queue.add(inst.attribute(index).name());
        break;

    case ATTRIBUTE_NAMES:
        for (i = 0; i < inst.numAttributes(); i++)
            m_Queue.add(inst.attribute(i).name());
        break;

    case LABELS:
        if (index != -1) {
            enm = inst.attribute(index).enumerateValues();
            while (enm.hasMoreElements())
                m_Queue.add(enm.nextElement());
        }
        break;

    case CLASS_LABELS:
        if (inst.classIndex() > -1) {
            enm = inst.classAttribute().enumerateValues();
            while (enm.hasMoreElements())
                m_Queue.add(enm.nextElement());
        }
        break;

    case LABEL_COUNT:
        if (index > -1) {
            m_LabelIndex.setData(inst.attribute(index));
            labelIndex = m_LabelIndex.getIntIndex();
            m_Queue.add(inst.attributeStats(index).nominalCounts[labelIndex]);
        }
        break;

    case LABEL_COUNTS:
        if (index > -1)
            m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(index).nominalCounts));
        break;

    case LABEL_DISTRIBUTION:
        if (index > -1) {
            dist = new double[inst.attributeStats(index).nominalCounts.length];
            for (i = 0; i < dist.length; i++)
                dist[i] = inst.attributeStats(index).nominalCounts[i];
            Utils.normalize(dist);
            m_Queue.add(StatUtils.toNumberArray(dist));
        }
        break;

    case CLASS_LABEL_COUNT:
        if (inst.classIndex() > -1) {
            m_LabelIndex.setData(inst.classAttribute());
            labelIndex = m_LabelIndex.getIntIndex();
            m_Queue.add(inst.attributeStats(inst.classIndex()).nominalCounts[labelIndex]);
        }
        break;

    case CLASS_LABEL_COUNTS:
        if (inst.classIndex() > -1)
            m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(inst.classIndex()).nominalCounts));
        break;

    case CLASS_LABEL_DISTRIBUTION:
        if (inst.classIndex() > -1) {
            dist = new double[inst.attributeStats(inst.classIndex()).nominalCounts.length];
            for (i = 0; i < dist.length; i++)
                dist[i] = inst.attributeStats(inst.classIndex()).nominalCounts[i];
            Utils.normalize(dist);
            m_Queue.add(StatUtils.toNumberArray(dist));
        }
        break;

    case NUM_ATTRIBUTES:
        m_Queue.add(inst.numAttributes());
        break;

    case NUM_INSTANCES:
        m_Queue.add(inst.numInstances());
        break;

    case NUM_CLASS_LABELS:
        if ((inst.classIndex() != -1) && inst.classAttribute().isNominal())
            m_Queue.add(inst.classAttribute().numValues());
        break;

    case NUM_LABELS:
        if ((index != -1) && inst.attribute(index).isNominal())
            m_Queue.add(inst.attribute(index).numValues());
        break;

    case NUM_DISTINCT_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).distinctCount);
        break;

    case NUM_UNIQUE_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).uniqueCount);
        break;

    case NUM_MISSING_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).missingCount);
        break;

    case MIN:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.min);
        break;

    case MAX:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.max);
        break;

    case MEAN:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.mean);
        break;

    case STDEV:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.stdDev);
        break;

    case ATTRIBUTE_TYPE:
        if (index != -1)
            m_Queue.add(Attribute.typeToString(inst.attribute(index)));
        break;

    case CLASS_TYPE:
        if (inst.classIndex() != -1)
            m_Queue.add(Attribute.typeToString(inst.classAttribute()));
        break;

    default:
        result = "Unhandled info type: " + m_Type;
    }

    return result;
}

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

License:Open Source License

/**
 * Generates the prefix for the dataset/index.
 *
 * @param inst   the current dataset//from w  w w  . j a v  a  2s .c o m
 * @param index   the index
 * @return      the prefix
 */
protected String createPrefix(Instances inst, int index) {
    String result;

    // generate prefix
    if (m_Prefix.equals("@"))
        result = inst.relationName();
    else
        result = m_Prefix;
    if (m_AddIndex)
        result += ((result.isEmpty() || result.endsWith(m_PrefixSeparator)) ? "" : m_PrefixSeparator)
                + (index + 1);
    result += m_PrefixSeparator;

    return result;
}

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

License:Open Source License

/**
 * Prefixes the attributes.//from w  w w. j a  v a 2 s.  c om
 *
 * @param index   the index of the dataset
 * @param inst   the data to process
 * @return      the processed data
 */
protected Instances prefixAttributes(Instances inst, int index) {
    Instances result;
    String prefix;
    ArrayList<Attribute> atts;
    int i;

    prefix = createPrefix(inst, index);

    // header
    atts = new ArrayList<>();
    for (i = 0; i < inst.numAttributes(); i++)
        atts.add(inst.attribute(i).copy(prefix + inst.attribute(i).name()));

    // data
    result = new Instances(inst.relationName(), atts, inst.numInstances());
    result.setClassIndex(inst.classIndex());
    for (i = 0; i < inst.numInstances(); i++)
        result.add((Instance) inst.instance(i).copy());

    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  w  w  .  j a  v  a 2  s  .  c  om
 *
 * @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.WekaRelationName.java

License:Open Source License

/**
 * Executes the flow item./*from  ww  w  .  java 2s. c om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
protected String doExecute() {
    String result;
    Instance inst;
    Instances data;

    result = null;

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

    m_OutputToken = new Token(data.relationName());

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from  w w w  . j  a va  2s .  c  o  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    weka.core.Instance inst;
    weka.core.Instances data;
    adams.data.instance.Instance instA;
    String oldName;
    String newName;

    result = null;

    if (m_InputToken.getPayload() instanceof weka.core.Instance) {
        inst = (weka.core.Instance) m_InputToken.getPayload();
        data = inst.dataset();
    } else if (m_InputToken.getPayload() instanceof adams.data.instance.Instance) {
        inst = ((adams.data.instance.Instance) m_InputToken.getPayload()).toInstance();
        data = inst.dataset();
    } else {
        inst = null;
        data = (weka.core.Instances) m_InputToken.getPayload();
    }
    if (isLoggingEnabled())
        getLogger().info("Renaming: " + m_Find + " -> " + m_Replace);

    // perform rename
    if (data != null) {
        oldName = data.relationName();
        newName = oldName.replaceAll(m_Find, m_Replace);
        data.setRelationName(newName);
        if (isLoggingEnabled())
            getLogger().info("Renamed: " + oldName + " -> " + newName);
    } else {
        if (isLoggingEnabled())
            getLogger().info("weka.core.Instance doesn't have access to dataset?");
    }

    if (inst == null) {
        m_OutputToken = new Token(data);
    } else {
        if (m_InputToken.getPayload() instanceof adams.data.instance.Instance) {
            instA = new adams.data.instance.Instance();
            instA.set(inst);
            m_OutputToken = new Token(instA);
        } else {
            m_OutputToken = new Token(inst);
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from w ww.j  a v a 2s .  co  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances dataOld;
    Instance instOld;
    Instances dataNew;
    Instance instNew;
    Attribute att;
    int i;
    StringBuilder order;
    List<Add> adds;
    Add add;
    int index;
    StringBuilder labels;
    int n;
    List<Filter> filters;
    Reorder reorder;

    result = null;

    if (m_OnTheFly && (m_Reference == null)) {
        result = setUpReference();
        if (result != null)
            return result;
    }

    dataNew = null;
    instNew = null;

    // get input data
    if (m_InputToken.getPayload() instanceof Instance) {
        instOld = (Instance) m_InputToken.getPayload();
        dataOld = instOld.dataset();
    } else {
        instOld = null;
        dataOld = (Instances) m_InputToken.getPayload();
    }

    // do we need to initialize filter?
    if (m_InitializeOnce || (m_Reorder == null)) {
        // check incoming data
        if (!m_Lenient) {
            for (i = 0; i < m_Reference.numAttributes(); i++) {
                att = m_Reference.attribute(i);
                if (dataOld.attribute(att.name()) == null) {
                    if (result == null)
                        result = "Missing attribute(s) in incoming data: " + att.name();
                    else
                        result += ", " + att.name();
                }
            }
            if (result != null)
                getLogger().severe(result);
        }

        if (result == null) {
            try {
                // determine indices
                order = new StringBuilder();
                adds = new ArrayList<Add>();
                for (i = 0; i < m_Reference.numAttributes(); i++) {
                    att = m_Reference.attribute(i);
                    if (dataOld.attribute(att.name()) == null) {
                        index = dataOld.numAttributes() + adds.size();
                        add = new Add();
                        add.setAttributeIndex("last");
                        add.setAttributeName(att.name());
                        add.setAttributeType(new SelectedTag(att.type(), Add.TAGS_TYPE));
                        if (att.isNominal()) {
                            labels = new StringBuilder();
                            for (n = 0; n < att.numValues(); n++) {
                                if (labels.length() > 0)
                                    labels.append(",");
                                labels.append(att.value(n));
                            }
                            add.setNominalLabels(labels.toString());
                        }
                        adds.add(add);
                    } else {
                        index = dataOld.attribute(att.name()).index();
                    }
                    if (order.length() > 0)
                        order.append(",");
                    order.append((index + 1));
                }

                // build reorder filter
                reorder = new Reorder();
                reorder.setAttributeIndices(order.toString());

                // build multifilter
                filters = new ArrayList<Filter>();
                filters.addAll(adds);
                filters.add(reorder);
                m_Reorder = new MultiFilter();
                m_Reorder.setFilters(filters.toArray(new Filter[filters.size()]));

                // initialize filter
                m_Reorder.setInputFormat(dataOld);
            } catch (Exception e) {
                result = handleException("Failed to initialize reorder filter!", e);
            }
        }
    }

    // reorder data
    if (result == null) {
        try {
            if (instOld != null) {
                m_Reorder.input(instOld);
                m_Reorder.batchFinished();
                instNew = m_Reorder.output();
                if (m_KeepRelationName)
                    instNew.dataset().setRelationName(dataOld.relationName());
            } else {
                dataNew = Filter.useFilter(dataOld, m_Reorder);
                if (m_KeepRelationName)
                    dataNew.setRelationName(dataOld.relationName());
            }
        } catch (Exception e) {
            result = handleException("Failed to reorder data!", e);
            instNew = null;
            dataNew = null;
        }
    }

    if (instNew != null)
        m_OutputToken = new Token(instNew);
    else if (dataNew != null)
        m_OutputToken = new Token(dataNew);

    return result;
}

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

License:Open Source License

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

    result = null;

    inst = null;
    data = null;
    filteredInst = null;
    filteredData = null;
    filter = (weka.filters.Filter) m_Filter;
    if (m_InputToken.getPayload() instanceof weka.core.Instance)
        inst = (weka.core.Instance) m_InputToken.getPayload();
    else if (m_InputToken.getPayload() instanceof weka.core.Instances)
        data = (weka.core.Instances) m_InputToken.getPayload();
    else
        inst = ((adams.data.instance.Instance) m_InputToken.getPayload()).toInstance();
    if (data == null)
        data = inst.dataset();

    try {
        // initialize filter?
        if (!m_Initialized) {
            result = setUpContainers(filter);
            if (result == null)
                result = updateObject(filter);
            filter.setInputFormat(new weka.core.Instances(data, 0));
        }

        if (result == null) {
            // filter data
            relation = data.relationName();
            if (inst == null) {
                filteredData = Filter.useFilter(data, filter);
                if (m_KeepRelationName)
                    filteredData.setRelationName(relation);
            } else {
                filter.input(inst);
                filter.batchFinished();
                filteredInst = filter.output();
                if (m_KeepRelationName)
                    filteredInst.dataset().setRelationName(relation);
            }

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

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

    return result;
}

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

License:Open Source License

/**
 * Launches the functionality of the menu item.
 *///from www  .  j a va  2s .c  o  m
@Override
public void launch() {
    File file;
    if (m_Parameters.length == 0) {
        // choose file
        int retVal = m_FileChooser.showOpenDialog(null);
        if (retVal != JFileChooser.APPROVE_OPTION)
            return;
        file = m_FileChooser.getSelectedFile();
    } else {
        file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile();
        m_FileChooser.setSelectedFile(file);
    }

    // create plot
    Instances result;
    try {
        result = m_FileChooser.getLoader().getDataSet();
    } catch (Exception e) {
        GUIHelper.showErrorMessage(getOwner(),
                "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e));
        return;
    }
    result.setClassIndex(result.numAttributes() - 1);
    ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
    PlotData2D plot = new PlotData2D(result);
    plot.setPlotName(result.relationName());
    plot.m_displayAllPoints = true;
    boolean[] connectPoints = new boolean[result.numInstances()];
    for (int cp = 1; cp < connectPoints.length; cp++)
        connectPoints[cp] = true;
    try {
        plot.setConnectPoints(connectPoints);
        vmc.addPlot(plot);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e));
        return;
    }

    ChildFrame frame = createChildFrame(vmc, GUIHelper.getDefaultDialogDimension());
    frame.setTitle(frame.getTitle() + " - " + file);
}