Example usage for weka.core Attribute DATE

List of usage examples for weka.core Attribute DATE

Introduction

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

Prototype

int DATE

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

Click Source Link

Document

Constant set for attributes with date values.

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//from  w  w  w .j  a va2  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.data.conversion.SpreadSheetToWekaInstances.java

License:Open Source License

/**
 * Performs the actual conversion.//from w  ww  .j a  va 2  s  . c o m
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    Instances result;
    SpreadSheet sheet;
    DenseInstance inst;
    ArrayList<Attribute> atts;
    HashSet<String> unique;
    ArrayList<String> labels;
    Row row;
    Cell cell;
    int i;
    int n;
    double[] values;
    Collection<ContentType> types;
    ContentType type;
    boolean added;
    int[] classIndices;

    sheet = (SpreadSheet) m_Input;

    // create header
    atts = new ArrayList<>();
    for (i = 0; i < sheet.getColumnCount(); i++) {
        added = false;
        types = sheet.getContentTypes(i);
        if (types.contains(ContentType.DOUBLE))
            types.remove(ContentType.LONG);
        if (types.contains(ContentType.LONG)) {
            types.add(ContentType.DOUBLE);
            types.remove(ContentType.LONG);
        }

        if (types.size() == 1) {
            type = (ContentType) types.toArray()[0];
            if (type == ContentType.DOUBLE) {
                atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent()));
                added = true;
            } else if (type == ContentType.DATE) {
                atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(),
                        Constants.TIMESTAMP_FORMAT));
                added = true;
            } else if (type == ContentType.TIME) {
                atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIME_FORMAT));
                added = true;
            }
        }

        if (!added) {
            unique = new HashSet<>();
            for (n = 0; n < sheet.getRowCount(); n++) {
                row = sheet.getRow(n);
                cell = row.getCell(i);
                if ((cell != null) && !cell.isMissing())
                    unique.add(cell.getContent());
            }
            if ((unique.size() > m_MaxLabels) || (m_MaxLabels < 1)) {
                atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), (FastVector) null));
            } else {
                labels = new ArrayList<>(unique);
                Collections.sort(labels);
                atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), labels));
            }
        }
    }
    result = new Instances(Environment.getInstance().getProject(), atts, sheet.getRowCount());
    if (sheet.hasName())
        result.setRelationName(sheet.getName());

    // add data
    for (n = 0; n < sheet.getRowCount(); n++) {
        row = sheet.getRow(n);
        values = new double[result.numAttributes()];
        for (i = 0; i < result.numAttributes(); i++) {
            cell = row.getCell(i);
            values[i] = weka.core.Utils.missingValue();
            if ((cell != null) && !cell.isMissing()) {
                if (result.attribute(i).type() == Attribute.DATE) {
                    if (cell.isTime())
                        values[i] = cell.toTime().getTime();
                    else
                        values[i] = cell.toDate().getTime();
                } else if (result.attribute(i).isNumeric()) {
                    values[i] = Utils.toDouble(cell.getContent());
                } else if (result.attribute(i).isString()) {
                    values[i] = result.attribute(i).addStringValue(cell.getContent());
                } else {
                    values[i] = result.attribute(i).indexOfValue(cell.getContent());
                }
            }
        }
        inst = new DenseInstance(1.0, values);
        result.add(inst);
    }

    if (sheet instanceof Dataset) {
        classIndices = ((Dataset) sheet).getClassAttributeIndices();
        if (classIndices.length > 0)
            result.setClassIndex(classIndices[0]);
    }

    return result;
}

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

License:Open Source License

/**
 * Performs the actual conversion.//  ww w .j a  v  a  2s .  c om
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    SpreadSheet result;
    Instances data;
    Row row;
    int i;
    int n;
    String str;

    data = (Instances) m_Input;

    // special case for InstancesViews
    if (m_SpreadSheetType instanceof InstancesView) {
        result = new InstancesView((Instances) m_Input);
        return result;
    }

    // create header
    result = m_SpreadSheetType.newInstance();
    result.setDataRowClass(m_DataRowType.getClass());
    row = result.getHeaderRow();
    for (n = 0; n < data.numAttributes(); n++)
        row.addCell("" + n).setContent(data.attribute(n).name());
    if (result instanceof Dataset) {
        if (data.classIndex() != -1)
            ((Dataset) result).setClassAttribute(data.classIndex(), true);
    }

    // fill spreadsheet
    for (i = 0; i < data.numInstances(); i++) {
        row = result.addRow("" + i);

        for (n = 0; n < data.numAttributes(); n++) {
            if (data.instance(i).isMissing(n))
                continue;
            if (data.attribute(n).type() == Attribute.DATE) {
                row.addCell("" + n).setContent(new DateTimeMsec(new Date((long) data.instance(i).value(n))));
            } else if (data.attribute(n).type() == Attribute.NUMERIC) {
                row.addCell("" + n).setContent(data.instance(i).value(n));
            } else {
                str = data.instance(i).stringValue(n);
                if (str.equals(SpreadSheet.MISSING_VALUE))
                    row.addCell("" + n).setContentAsString("'" + str + "'");
                else
                    row.addCell("" + n).setContentAsString(str);
            }
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item./*from   www  . j ava  2s .  c  om*/
 *
 * @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.//  www .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.WekaInstancesMerge.java

License:Open Source License

/**
 * Merges the datasets based on the collected IDs.
 *
 * @param orig   the original datasets/* w  w  w.  ja v a  2 s.com*/
 * @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  2s . co m*/
 *
 * @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.  j  a  v a  2s  .  com*/
 *
 * @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
 *//*w w w .j  av a2 s . 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  . j  a  v  a2s  .  co 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 "???";
        }
    }
}