Example usage for weka.core Instance setDataset

List of usage examples for weka.core Instance setDataset

Introduction

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

Prototype

public void setDataset(Instances instances);

Source Link

Document

Sets the reference to the dataset.

Usage

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.DefaultInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    FastVector attributes = new FastVector(0);
    Instances meta = new Instances(name, attributes, 1);
    Instance data = new Instance(0);
    data.setDataset(meta);
    return data;/*from  w  w w.j  ava  2s .  c  o m*/
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.DoubleArrayInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    Instance ret = new Instance(0);
    ret.setDataset(new Instances(name, new FastVector(0), 1));
    double[] data = (double[]) value;
    if ((data != null) && (data.length > 0)) {
        FastVector attributes = new FastVector(data.length);
        ret = new Instance(data.length);
        for (int i = 0; i < attributes.size(); ++i) {
            attributes.addElement(new Attribute(name + ":" + i));
            ret.setValue(i, data[i]);/*w ww  .j a  v  a2s  . com*/
        }
        ret.setDataset(new Instances(name, attributes, 1));
    }
    return ret;
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.DoubleInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    Instance ret = new Instance(1);
    FastVector attributes = new FastVector();
    attributes.addElement(new Attribute(name));
    Instances meta = new Instances(name, attributes, 1);
    ret.setDataset(meta);
    ret.setValue(0, ((Double) value).doubleValue());
    return ret;/*from   ww  w.j  av  a  2s  . c  o  m*/
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.IntegerInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    Instance ret = new Instance(1);
    FastVector attributes = new FastVector();
    attributes.addElement(new Attribute(name));
    Instances meta = new Instances(name, attributes, 1);
    ret.setDataset(meta);
    ret.setValue(0, ((Integer) value).intValue());
    return ret;//from   w ww  .  ja  v  a  2  s . c  o  m
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.LongInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    Instance ret = new Instance(1);
    FastVector attributes = new FastVector();
    attributes.addElement(new Attribute(name));
    Instances meta = new Instances(name, attributes, 1);
    ret.setDataset(meta);
    ret.setValue(0, ((Long) value).longValue());
    return ret;//  w  w w .j  av a  2 s  . c  om
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.instanceFactories.StringInstanceFactory.java

License:Open Source License

@Override
public Instance transform(Object value, String name) {
    Instance ret = new Instance(1);
    FastVector string = new FastVector(1);
    string.addElement(value);//w w  w  . java  2 s . com
    FastVector attributes = new FastVector(1);
    attributes.addElement(new Attribute(name, string));
    Instances meta = new Instances(name, attributes, 1);
    ret.setDataset(meta);
    ret.setValue(0, (String) value);
    return ret;
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.InstanceManipulation.java

License:Open Source License

/**
 * Takes the contents of the Instance array and creates a new Instance object
 * whose attributes are the attributes of the Instance objects in sequence
 * backed by a new Dataset relfecting the new set of attributes.
 *   // w w  w  .j a  v  a  2  s  . co m
 * If there is any conflict of attribute names between the Instance objects,
 * they are duplicated in the Instance object (which may cause difficulties
 * for some machine learning algorithms.)  If this a problem, utilize 
 * normalizeFieldNames instead of concatenation.
 * 
 * If either array is null or if the length of the data and meta array do not
 * match, a new Instance object without attributes is created and returned.
 * 
 * @param data array of Instance objects 
 * @param meta array of Instances backing the data array
 * @return new Instance containing all the given data
 */
static public Instance instanceConcatenation(Instance[] data, Instances[] meta) {
    FastVector attributeVector = new FastVector();
    LinkedList<Double> values = new LinkedList<Double>();
    Instance ret = new Instance(0);
    ret.setDataset(new Instances("Default", new FastVector(), 0));
    if ((data != null) && (meta != null) && (data.length == meta.length) && (data.length > 0)) {
        for (int i = 0; i < data.length; ++i) {
            for (int j = 0; j < meta[i].numAttributes(); ++j) {
                attributeVector.addElement(meta[i].attribute(j));
                values.add(data[i].value(j));
            }
        }
        ret = new Instance(values.size());
        Iterator<Double> it = values.iterator();
        for (int i = 0; i < ret.numAttributes(); ++i) {
            ret.setValue(i, it.next());
        }
        ret.setDataset(new Instances(meta[0].relationName() + " Concatenated", attributeVector, 1));
    }
    return ret;
}

From source file:org.mcennis.graphrat.algorithm.transformToArray.DirectedTriplesHistogram.java

License:Open Source License

@Override
public void execute(Graph g) {

    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("Mode").get(), ".*", false);
    //        TreeSet<Actor> triplesBase = new TreeSet<Actor>();

    relation = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
    relation.buildQuery((String) parameter.get("Relation").get(), false);

    Iterator<Actor> iterator = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
    int nonZeroCount = 0;
    int actorCount = 0;
    TreeSet<Actor> triplesBase = new TreeSet<Actor>();
    while (iterator.hasNext()) {
        iterator.next();// ww w.  ja v  a2s . c  o  m
        triplesBase.add(iterator.next());
        actorCount++;
    }
    iterator = triplesBase.iterator();
    double[] tripleCount = new double[16];

    Arrays.fill(tripleCount, 0.0);
    if (actorCount > 0) {
        //            while (Actor first : triplesBase) {
        while (iterator.hasNext()) {
            Actor first = iterator.next();
            TreeSet<Actor> firstNeighbors = new TreeSet<Actor>();

            TreeSet<Actor> secondNeighbors = new TreeSet<Actor>();

            TreeSet<Actor> firstActorList = getLinks(g, first, first, firstNeighbors);

            TreeSet<Actor> a = new TreeSet<Actor>();
            a.add(first);
            TreeSet<Actor> b = new TreeSet<Actor>();

            TreeSet<Actor> c = new TreeSet<Actor>();

            for (Actor second : firstActorList) {

                if (second.compareTo(first) > 0) {
                    b.add(second);

                    // handle all fully linked triples

                    TreeSet<Actor> secondActorList = getLinks(g, second, second, null);

                    secondActorList.retainAll(firstActorList);

                    int firstSecond = getLinkType(g, a, b);

                    for (Actor third : secondActorList) {
                        c.add(third);
                        int firstThird = getLinkType(g, a, c);

                        int secondThird = getLinkType(g, b, c);

                        tripleCount[mapIndex(firstSecond, firstThird, secondThird)]++;

                        nonZeroCount++;
                        c.clear();
                    }

                    // handle all 2 sided triples

                    secondActorList = getLinks(g, second, first, secondNeighbors);

                    secondActorList.removeAll(firstActorList);

                    for (Actor third : secondActorList) {
                        c.add(third);
                        int firstThird = getLinkType(g, a, c);

                        int secondThird = getLinkType(g, b, c);

                        tripleCount[mapIndex(firstSecond, firstThird, secondThird)]++;

                        nonZeroCount++;
                        c.clear();
                    }

                    // handle all 1 sided triples

                    firstNeighbors.addAll(secondNeighbors);

                    tripleCount[mapIndex(firstSecond, 0, 0)] += actorCount - firstNeighbors.size();

                    nonZeroCount += firstNeighbors.size();
                    b.clear();
                }
                a.clear();
            }
        }

        tripleCount[0] = (actorCount * (actorCount - 1) * (actorCount - 2)) / 6 - nonZeroCount;

        FastVector base = new FastVector();

        for (int i = 0; i < tripleCount.length; ++i) {

            base.addElement(
                    new Attribute(g.getID() + (String) parameter.get("DestinationProperty").get() + ":" + i));

        }

        Instances meta = new Instances("Directed Triples", base, 1);

        Instance value = new Instance(tripleCount.length, tripleCount);

        value.setDataset(meta);

        Property property = PropertyFactory.newInstance().create(
                AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("DestinationProperty").get()),
                (String) parameter.get("DestinationProperty").getType(), Instance.class);

        try {

            property.add(value);

            g.add(property);

        } catch (InvalidObjectTypeException ex) {

            Logger.getLogger(DirectedTriplesHistogram.class.getName()).log(Level.SEVERE,
                    "Property class doesn't match double[]", ex);

        }

    } else {

        Logger.getLogger(DirectedTriplesHistogram.class.getName()).log(Level.WARNING, "No actors of mode '"
                + (String) parameter.get("Mode").get() + "' were found in graph '" + g.getID() + "'");

    }
}

From source file:org.mcennis.graphrat.algorithm.transformToArray.UndirectedTriplesHistogram.java

License:Open Source License

@Override

public void execute(Graph g) {

    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("Mode").get(), ".*", false);
    //        TreeSet<Actor> triplesBase = new TreeSet<Actor>();

    relation = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
    relation.buildQuery((String) parameter.get("Relation").get(), false);

    Iterator<Actor> iterator = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
    int nonZeroCount = 0;
    int actorCount = 0;
    TreeSet<Actor> triplesBase = new TreeSet<Actor>();
    while (iterator.hasNext()) {
        iterator.next();/*ww w . ja  va 2s . c o m*/
        triplesBase.add(iterator.next());
        actorCount++;
    }
    iterator = triplesBase.iterator();
    if (iterator.hasNext()) {

        double[] tripleCount = new double[4];

        Arrays.fill(tripleCount, 0.0);

        while (iterator.hasNext()) {
            Actor first = iterator.next();
            TreeSet<Actor> firstNeighbors = new TreeSet<Actor>();
            TreeSet<Actor> a = new TreeSet<Actor>();
            a.add(first);
            TreeSet<Actor> b = new TreeSet<Actor>();

            TreeSet<Actor> secondNeighbors = new TreeSet<Actor>();

            TreeSet<Actor> firstActorList = getLinks(g, a, a, firstNeighbors);

            for (Actor second : firstActorList) {

                if (second.compareTo(first) > 0) {
                    b.add(second);

                    // handle all fully linked triples

                    TreeSet<Actor> secondActorList = getLinks(g, b, b, null);

                    TreeSet<Actor> fullyLinked = new TreeSet<Actor>();

                    fullyLinked.addAll(firstActorList);

                    fullyLinked.retainAll(secondActorList);

                    tripleCount[3] += fullyLinked.size();

                    // handle all 2 sided triples

                    secondActorList = getLinks(g, b, a, secondNeighbors);

                    TreeSet<Actor> twoSided = new TreeSet<Actor>();

                    twoSided.addAll(secondActorList);

                    twoSided.removeAll(firstActorList);

                    tripleCount[2] += twoSided.size();

                    // handle all 1 sided triples

                    firstNeighbors.addAll(secondNeighbors);

                    tripleCount[1] += actorCount - firstNeighbors.size();
                    b.clear();
                }

            }
            a.clear();
        }

        tripleCount[0] = (actorCount * (actorCount - 1) * (actorCount - 2)) / 6 - tripleCount[1]
                - tripleCount[2] - tripleCount[3];

        FastVector base = new FastVector();

        for (int i = 0; i < tripleCount.length; ++i) {

            base.addElement(new Attribute(
                    AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("DestinationProperty").get())
                            + ":" + i));

        }

        Instances meta = new Instances("Undirected Triples", base, 1);

        Instance value = new Instance(tripleCount.length, tripleCount);

        value.setDataset(meta);
        Property property = PropertyFactory.newInstance().create("BasicProperty",
                AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("DestinationProperty").get()),
                Instance.class);

        try {

            property.add(value);

            g.add(property);

        } catch (InvalidObjectTypeException ex) {

            Logger.getLogger(UndirectedTriplesHistogram.class.getName()).log(Level.SEVERE,
                    "Property class does not match double[]", ex);

        }

    } else {

        Logger.getLogger(UndirectedTriplesHistogram.class.getName()).log(Level.SEVERE, "No actors of mode '"
                + (String) parameter.get("Mode").get() + "' were found in graph '" + g.getID() + "'");

    }

}

From source file:org.mcennis.graphrat.dataAquisition.ReadAudioFiles.java

License:Open Source License

protected void createSong(AggregatorContainer container, String ID) {

    // create Instance

    double[][] rawValues = container.getResults();

    int total = 0;

    for (int i = 0; i < rawValues.length; ++i) {

        if (rawValues[i] != null) {

            total += rawValues[i].length;

        }//from   w  ww . j av  a 2 s  . com

    }

    double[] results = new double[total + 1];

    int count = 0;

    for (int i = 0; i < rawValues.length; ++i) {

        if (rawValues[i] != null) {

            for (int j = 0; j < rawValues[i].length; ++j) {

                results[count++] = rawValues[i][j];

            }

        }

    }

    results[results.length - 1] = Double.NaN;

    Instance instance = new Instance(results.length, results);

    instance.setDataset(globalInstances);

    //attach instance to new actor.

    Actor song = ActorFactory.newInstance().create((String) properties.get("Mode").get(), ID);
    Property prop = PropertyFactory.newInstance().create(
            (String) PropertyFactory.newInstance().getClassParameter().getValue().iterator().next(),
            (String) properties.get("PropertyID").get(), Instances.class);

    try {

        prop.add(instance);

    } catch (InvalidObjectTypeException ex) {

        Logger.getLogger(ReadAudioFiles.class.getName()).log(Level.SEVERE,
                "Property class does not match weka.core.Instance", ex);

    }

    song.add(prop);

    graph.add(song);

}