http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/Separation.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/Separation.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/Separation.java index 25534a6..1e3072b 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/Separation.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/Separation.java @@ -31,88 +31,88 @@ import java.util.List; public class Separation extends MeasureCollection { - public Separation() { - super(); + public Separation() { + super(); + } + + @Override + protected String[] getNames() { + return new String[] { "BSS", "BSS-GT", "BSS-Ratio" }; + } + + // @Override + public void evaluateClusteringSamoa(Clustering clustering, + Clustering trueClustering, ArrayList<Instance> points) + throws Exception { + + double BSS_GT = 1.0; + double BSS; + int dimension = points.get(0).numAttributes() - 1; + SphereCluster sc = new SphereCluster(points, dimension); + + // DO INTERNAL EVALUATION + // clustering.getClustering().get(0).getCenter(); + + BSS = getBSS(clustering, sc.getCenter()); + + if (trueClustering != null) { + List<Instance> listInstances = new ArrayList<>(); + for (Cluster c : trueClustering.getClustering()) { + DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); + listInstances.add(inst); + } + SphereCluster gt = new SphereCluster(listInstances, dimension); + BSS_GT = getBSS(trueClustering, gt.getCenter()); } - @Override - protected String[] getNames() { - return new String[]{"BSS", "BSS-GT", "BSS-Ratio"}; + addValue("BSS", BSS); + addValue("BSS-GT", BSS_GT); + addValue("BSS-Ratio", BSS / BSS_GT); + + } + + private double getBSS(Clustering clustering, double[] mean) { + double bss = 0.0; + for (int i = 0; i < clustering.size(); i++) { + double weight = clustering.get(i).getWeight(); + double sum = 0.0; + for (int j = 0; j < mean.length; j++) { + sum += Math.pow((mean[j] - clustering.get(i).getCenter()[j]), 2); + } + bss += weight * sum; } - //@Override - public void evaluateClusteringSamoa(Clustering clustering, - Clustering trueClustering, ArrayList<Instance> points) - throws Exception { - - double BSS_GT = 1.0; - double BSS; - int dimension = points.get(0).numAttributes() - 1; - SphereCluster sc = new SphereCluster(points, dimension); - - // DO INTERNAL EVALUATION - //clustering.getClustering().get(0).getCenter(); - - BSS = getBSS(clustering, sc.getCenter()); - - if (trueClustering != null) { - List<Instance> listInstances = new ArrayList<>(); - for (Cluster c : trueClustering.getClustering()) { - DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); - listInstances.add(inst); - } - SphereCluster gt = new SphereCluster(listInstances, dimension); - BSS_GT = getBSS(trueClustering, gt.getCenter()); - } - - addValue("BSS", BSS); - addValue("BSS-GT", BSS_GT); - addValue("BSS-Ratio", BSS / BSS_GT); - - } - - private double getBSS(Clustering clustering, double[] mean) { - double bss = 0.0; - for (int i = 0; i < clustering.size(); i++) { - double weight = clustering.get(i).getWeight(); - double sum = 0.0; - for (int j = 0; j < mean.length; j++) { - sum += Math.pow((mean[j] - clustering.get(i).getCenter()[j]), 2); - } - bss += weight * sum; - } - - return bss; + return bss; + } + + @Override + protected void evaluateClustering(Clustering clustering, + Clustering trueClustering, ArrayList<DataPoint> points) + throws Exception { + double BSS_GT = 1.0; + double BSS; + int dimension = points.get(0).numAttributes() - 1; + SphereCluster sc = new SphereCluster(points, dimension); + + // DO INTERNAL EVALUATION + // clustering.getClustering().get(0).getCenter(); + + BSS = getBSS(clustering, sc.getCenter()); + + if (trueClustering != null) { + String s = ""; + List<Instance> listInstances = new ArrayList<>(); + for (Cluster c : trueClustering.getClustering()) { + DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); + listInstances.add(inst); + s += " " + c.getWeight(); + } + SphereCluster gt = new SphereCluster(listInstances, dimension); + BSS_GT = getBSS(trueClustering, gt.getCenter()); } - @Override - protected void evaluateClustering(Clustering clustering, - Clustering trueClustering, ArrayList<DataPoint> points) - throws Exception { - double BSS_GT = 1.0; - double BSS; - int dimension = points.get(0).numAttributes() - 1; - SphereCluster sc = new SphereCluster(points, dimension); - - // DO INTERNAL EVALUATION - //clustering.getClustering().get(0).getCenter(); - - BSS = getBSS(clustering, sc.getCenter()); - - if (trueClustering != null) { - String s = ""; - List<Instance> listInstances = new ArrayList<>(); - for (Cluster c : trueClustering.getClustering()) { - DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); - listInstances.add(inst); - s += " " + c.getWeight(); - } - SphereCluster gt = new SphereCluster(listInstances, dimension); - BSS_GT = getBSS(trueClustering, gt.getCenter()); - } - - addValue("BSS", BSS); - addValue("BSS-GT", BSS_GT); - addValue("BSS-Ratio", BSS / BSS_GT); - } + addValue("BSS", BSS); + addValue("BSS-GT", BSS_GT); + addValue("BSS-Ratio", BSS / BSS_GT); + } }
http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/SilhouetteCoefficient.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/SilhouetteCoefficient.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/SilhouetteCoefficient.java index 6dee336..3740910 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/SilhouetteCoefficient.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/SilhouetteCoefficient.java @@ -20,118 +20,116 @@ package com.yahoo.labs.samoa.evaluation.measures; * #L% */ - import com.yahoo.labs.samoa.moa.cluster.Cluster; import com.yahoo.labs.samoa.moa.cluster.Clustering; import com.yahoo.labs.samoa.moa.evaluation.MeasureCollection; import com.yahoo.labs.samoa.moa.core.DataPoint; import java.util.ArrayList; - -public class SilhouetteCoefficient extends MeasureCollection{ - private double pointInclusionProbThreshold = 0.8; - - public SilhouetteCoefficient() { - super(); +public class SilhouetteCoefficient extends MeasureCollection { + private double pointInclusionProbThreshold = 0.8; + + public SilhouetteCoefficient() { + super(); + } + + @Override + protected boolean[] getDefaultEnabled() { + return new boolean[] { false }; + } + + @Override + public String[] getNames() { + return new String[] { "SilhCoeff" }; + } + + public void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) { + int numFCluster = clustering.size(); + + double[][] pointInclusionProbFC = new double[points.size()][numFCluster]; + for (int p = 0; p < points.size(); p++) { + DataPoint point = points.get(p); + for (int fc = 0; fc < numFCluster; fc++) { + Cluster cl = clustering.get(fc); + pointInclusionProbFC[p][fc] = cl.getInclusionProbability(point); + } } - @Override - protected boolean[] getDefaultEnabled() { - return new boolean[]{false}; - } - - @Override - public String[] getNames() { - return new String[]{"SilhCoeff"}; - } - - public void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) { - int numFCluster = clustering.size(); - - double [][] pointInclusionProbFC = new double[points.size()][numFCluster]; - for (int p = 0; p < points.size(); p++) { - DataPoint point = points.get(p); - for (int fc = 0; fc < numFCluster; fc++) { - Cluster cl = clustering.get(fc); - pointInclusionProbFC[p][fc] = cl.getInclusionProbability(point); - } + double silhCoeff = 0.0; + int totalCount = 0; + for (int p = 0; p < points.size(); p++) { + DataPoint point = points.get(p); + ArrayList<Integer> ownClusters = new ArrayList<>(); + for (int fc = 0; fc < numFCluster; fc++) { + if (pointInclusionProbFC[p][fc] > pointInclusionProbThreshold) { + ownClusters.add(fc); } - - double silhCoeff = 0.0; - int totalCount = 0; - for (int p = 0; p < points.size(); p++) { - DataPoint point = points.get(p); - ArrayList<Integer> ownClusters = new ArrayList<>(); + } + + if (ownClusters.size() > 0) { + double[] distanceByClusters = new double[numFCluster]; + int[] countsByClusters = new int[numFCluster]; + // calculate averageDistance of p to all cluster + for (int p1 = 0; p1 < points.size(); p1++) { + DataPoint point1 = points.get(p1); + if (p1 != p && point1.classValue() != -1) { for (int fc = 0; fc < numFCluster; fc++) { - if(pointInclusionProbFC[p][fc] > pointInclusionProbThreshold){ - ownClusters.add(fc); - } + if (pointInclusionProbFC[p1][fc] > pointInclusionProbThreshold) { + double distance = distance(point, point1); + distanceByClusters[fc] += distance; + countsByClusters[fc]++; + } } + } + } - if(ownClusters.size() > 0){ - double[] distanceByClusters = new double[numFCluster]; - int[] countsByClusters = new int[numFCluster]; - //calculate averageDistance of p to all cluster - for (int p1 = 0; p1 < points.size(); p1++) { - DataPoint point1 = points.get(p1); - if(p1!= p && point1.classValue() != -1){ - for (int fc = 0; fc < numFCluster; fc++) { - if(pointInclusionProbFC[p1][fc] > pointInclusionProbThreshold){ - double distance = distance(point, point1); - distanceByClusters[fc]+=distance; - countsByClusters[fc]++; - } - } - } - } - - //find closest OWN cluster as clusters might overlap - double minAvgDistanceOwn = Double.MAX_VALUE; - int minOwnIndex = -1; - for (int fc : ownClusters) { - double normDist = distanceByClusters[fc]/(double)countsByClusters[fc]; - if(normDist < minAvgDistanceOwn){// && pointInclusionProbFC[p][fc] > pointInclusionProbThreshold){ - minAvgDistanceOwn = normDist; - minOwnIndex = fc; - } - } - - - //find closest other (or other own) cluster - double minAvgDistanceOther = Double.MAX_VALUE; - for (int fc = 0; fc < numFCluster; fc++) { - if(fc != minOwnIndex){ - double normDist = distanceByClusters[fc]/(double)countsByClusters[fc]; - if(normDist < minAvgDistanceOther){ - minAvgDistanceOther = normDist; - } - } - } - - double silhP = (minAvgDistanceOther-minAvgDistanceOwn)/Math.max(minAvgDistanceOther, minAvgDistanceOwn); - point.setMeasureValue("SC - own", minAvgDistanceOwn); - point.setMeasureValue("SC - other", minAvgDistanceOther); - point.setMeasureValue("SC", silhP); + // find closest OWN cluster as clusters might overlap + double minAvgDistanceOwn = Double.MAX_VALUE; + int minOwnIndex = -1; + for (int fc : ownClusters) { + double normDist = distanceByClusters[fc] / (double) countsByClusters[fc]; + if (normDist < minAvgDistanceOwn) {// && pointInclusionProbFC[p][fc] > + // pointInclusionProbThreshold){ + minAvgDistanceOwn = normDist; + minOwnIndex = fc; + } + } - silhCoeff+=silhP; - totalCount++; - //System.out.println(point.getTimestamp()+" Silh "+silhP+" / "+avgDistanceOwn+" "+minAvgDistanceOther+" (C"+minIndex+")"); + // find closest other (or other own) cluster + double minAvgDistanceOther = Double.MAX_VALUE; + for (int fc = 0; fc < numFCluster; fc++) { + if (fc != minOwnIndex) { + double normDist = distanceByClusters[fc] / (double) countsByClusters[fc]; + if (normDist < minAvgDistanceOther) { + minAvgDistanceOther = normDist; } + } } - if(totalCount>0) - silhCoeff/=(double)totalCount; - //normalize from -1, 1 to 0,1 - silhCoeff = (silhCoeff+1)/2.0; - addValue(0,silhCoeff); - } - private double distance(DataPoint inst1, DataPoint inst2){ - double distance = 0.0; - int numDims = inst1.numAttributes(); - for (int i = 0; i < numDims; i++) { - double d = inst1.value(i) - inst2.value(i); - distance += d * d; - } - return Math.sqrt(distance); + double silhP = (minAvgDistanceOther - minAvgDistanceOwn) / Math.max(minAvgDistanceOther, minAvgDistanceOwn); + point.setMeasureValue("SC - own", minAvgDistanceOwn); + point.setMeasureValue("SC - other", minAvgDistanceOther); + point.setMeasureValue("SC", silhP); + + silhCoeff += silhP; + totalCount++; + // System.out.println(point.getTimestamp()+" Silh "+silhP+" / "+avgDistanceOwn+" "+minAvgDistanceOther+" (C"+minIndex+")"); + } + } + if (totalCount > 0) + silhCoeff /= (double) totalCount; + // normalize from -1, 1 to 0,1 + silhCoeff = (silhCoeff + 1) / 2.0; + addValue(0, silhCoeff); + } + + private double distance(DataPoint inst1, DataPoint inst2) { + double distance = 0.0; + int numDims = inst1.numAttributes(); + for (int i = 0; i < numDims; i++) { + double d = inst1.value(i) - inst2.value(i); + distance += d * d; } + return Math.sqrt(distance); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/StatisticalCollection.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/StatisticalCollection.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/StatisticalCollection.java index 6fc7adc..9b5f866 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/StatisticalCollection.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/evaluation/measures/StatisticalCollection.java @@ -28,159 +28,160 @@ import com.yahoo.labs.samoa.moa.core.DataPoint; import com.yahoo.labs.samoa.moa.evaluation.MeasureCollection; import com.yahoo.labs.samoa.moa.evaluation.MembershipMatrix; -public class StatisticalCollection extends MeasureCollection{ - private boolean debug = false; - - @Override - protected String[] getNames() { - //String[] names = {"van Dongen","Rand statistic", "C Index"}; - return new String[]{"van Dongen","Rand statistic"}; +public class StatisticalCollection extends MeasureCollection { + private boolean debug = false; + + @Override + protected String[] getNames() { + // String[] names = {"van Dongen","Rand statistic", "C Index"}; + return new String[] { "van Dongen", "Rand statistic" }; + } + + @Override + protected boolean[] getDefaultEnabled() { + return new boolean[] { false, false }; + } + + @Override + public void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) + throws Exception { + + MembershipMatrix mm = new MembershipMatrix(clustering, points); + int numClasses = mm.getNumClasses(); + int numCluster = clustering.size() + 1; + int n = mm.getTotalEntries(); + + double dongenMaxFC = 0; + double dongenMaxSumFC = 0; + for (int i = 0; i < numCluster; i++) { + double max = 0; + for (int j = 0; j < numClasses; j++) { + if (mm.getClusterClassWeight(i, j) > max) + max = mm.getClusterClassWeight(i, j); + } + dongenMaxFC += max; + if (mm.getClusterSum(i) > dongenMaxSumFC) + dongenMaxSumFC = mm.getClusterSum(i); } - @Override - protected boolean[] getDefaultEnabled() { - return new boolean[]{false, false}; + double dongenMaxHC = 0; + double dongenMaxSumHC = 0; + for (int j = 0; j < numClasses; j++) { + double max = 0; + for (int i = 0; i < numCluster; i++) { + if (mm.getClusterClassWeight(i, j) > max) + max = mm.getClusterClassWeight(i, j); + } + dongenMaxHC += max; + if (mm.getClassSum(j) > dongenMaxSumHC) + dongenMaxSumHC = mm.getClassSum(j); } - @Override - public void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) throws Exception { - - - MembershipMatrix mm = new MembershipMatrix(clustering, points); - int numClasses = mm.getNumClasses(); - int numCluster = clustering.size()+1; - int n = mm.getTotalEntries(); - - double dongenMaxFC = 0; - double dongenMaxSumFC = 0; - for (int i = 0; i < numCluster; i++){ - double max = 0; - for (int j = 0; j < numClasses; j++) { - if(mm.getClusterClassWeight(i, j)>max) max = mm.getClusterClassWeight(i, j); - } - dongenMaxFC+=max; - if(mm.getClusterSum(i)>dongenMaxSumFC) dongenMaxSumFC = mm.getClusterSum(i); - } - - double dongenMaxHC = 0; - double dongenMaxSumHC = 0; - for (int j = 0; j < numClasses; j++) { - double max = 0; - for (int i = 0; i < numCluster; i++){ - if(mm.getClusterClassWeight(i, j)>max) max = mm.getClusterClassWeight(i, j); - } - dongenMaxHC+=max; - if(mm.getClassSum(j)>dongenMaxSumHC) dongenMaxSumHC = mm.getClassSum(j); - } - - double dongen = (dongenMaxFC + dongenMaxHC)/(2*n); - //normalized dongen - //double dongen = 1-(2*n - dongenMaxFC - dongenMaxHC)/(2*n - dongenMaxSumFC - dongenMaxSumHC); - if(debug) - System.out.println("Dongen HC:"+dongenMaxHC+" FC:"+dongenMaxFC+" Total:"+dongen+" n "+n); - - addValue("van Dongen", dongen); - - - //Rand index - //http://www.cais.ntu.edu.sg/~qihe/menu4.html - double m1 = 0; - for (int j = 0; j < numClasses; j++) { - double v = mm.getClassSum(j); - m1+= v*(v-1)/2.0; - } - double m2 = 0; - for (int i = 0; i < numCluster; i++){ - double v = mm.getClusterSum(i); - m2+= v*(v-1)/2.0; - } - - double m = 0; - for (int i = 0; i < numCluster; i++){ - for (int j = 0; j < numClasses; j++) { - double v = mm.getClusterClassWeight(i, j); - m+= v*(v-1)/2.0; - } - } - double M = n*(n-1)/2.0; - double rand = (M - m1 - m2 +2*m)/M; - //normalized rand - //double rand = (m - m1*m2/M)/(m1/2.0 + m2/2.0 - m1*m2/M); - - addValue("Rand statistic", rand); - - - //addValue("C Index",cindex(clustering, points)); + double dongen = (dongenMaxFC + dongenMaxHC) / (2 * n); + // normalized dongen + // double dongen = 1-(2*n - dongenMaxFC - dongenMaxHC)/(2*n - dongenMaxSumFC + // - dongenMaxSumHC); + if (debug) + System.out.println("Dongen HC:" + dongenMaxHC + " FC:" + dongenMaxFC + " Total:" + dongen + " n " + n); + + addValue("van Dongen", dongen); + + // Rand index + // http://www.cais.ntu.edu.sg/~qihe/menu4.html + double m1 = 0; + for (int j = 0; j < numClasses; j++) { + double v = mm.getClassSum(j); + m1 += v * (v - 1) / 2.0; + } + double m2 = 0; + for (int i = 0; i < numCluster; i++) { + double v = mm.getClusterSum(i); + m2 += v * (v - 1) / 2.0; } + double m = 0; + for (int i = 0; i < numCluster; i++) { + for (int j = 0; j < numClasses; j++) { + double v = mm.getClusterClassWeight(i, j); + m += v * (v - 1) / 2.0; + } + } + double M = n * (n - 1) / 2.0; + double rand = (M - m1 - m2 + 2 * m) / M; + // normalized rand + // double rand = (m - m1*m2/M)/(m1/2.0 + m2/2.0 - m1*m2/M); + + addValue("Rand statistic", rand); + + // addValue("C Index",cindex(clustering, points)); + } + + public double cindex(Clustering clustering, ArrayList<DataPoint> points) { + int numClusters = clustering.size(); + double withinClustersDistance = 0; + int numDistancesWithin = 0; + double numDistances = 0; + + // double[] withinClusters = new double[numClusters]; + double[] minWithinClusters = new double[numClusters]; + double[] maxWithinClusters = new double[numClusters]; + ArrayList<Integer>[] pointsInClusters = new ArrayList[numClusters]; + for (int c = 0; c < numClusters; c++) { + pointsInClusters[c] = new ArrayList<>(); + minWithinClusters[c] = Double.MAX_VALUE; + maxWithinClusters[c] = Double.MIN_VALUE; + } - - public double cindex(Clustering clustering, ArrayList<DataPoint> points){ - int numClusters = clustering.size(); - double withinClustersDistance = 0; - int numDistancesWithin = 0; - double numDistances = 0; - - //double[] withinClusters = new double[numClusters]; - double[] minWithinClusters = new double[numClusters]; - double[] maxWithinClusters = new double[numClusters]; - ArrayList<Integer>[] pointsInClusters = new ArrayList[numClusters]; - for (int c = 0; c < numClusters; c++) { - pointsInClusters[c] = new ArrayList<>(); - minWithinClusters[c] = Double.MAX_VALUE; - maxWithinClusters[c] = Double.MIN_VALUE; - } - - for (int p = 0; p < points.size(); p++) { - for (int c = 0; c < clustering.size(); c++) { - if(clustering.get(c).getInclusionProbability(points.get(p)) > 0.8){ - pointsInClusters[c].add(p); - numDistances++; - } - } - } - - //calc within cluster distances + min and max values - for (int c = 0; c < numClusters; c++) { - int numDistancesInC = 0; - ArrayList<Integer> pointsInC = pointsInClusters[c]; - for (int p = 0; p < pointsInC.size(); p++) { - DataPoint point = points.get(pointsInC.get(p)); - for (int p1 = p+1; p1 < pointsInC.size(); p1++) { - numDistancesWithin++; - numDistancesInC++; - DataPoint point1 = points.get(pointsInC.get(p1)); - double dist = point.getDistance(point1); - withinClustersDistance+=dist; - if(minWithinClusters[c] > dist) minWithinClusters[c] = dist; - if(maxWithinClusters[c] < dist) maxWithinClusters[c] = dist; - } - } - } - - double minWithin = Double.MAX_VALUE; - double maxWithin = Double.MIN_VALUE; - for (int c = 0; c < numClusters; c++) { - if(minWithinClusters[c] < minWithin) - minWithin = minWithinClusters[c]; - if(maxWithinClusters[c] > maxWithin) - maxWithin = maxWithinClusters[c]; + for (int p = 0; p < points.size(); p++) { + for (int c = 0; c < clustering.size(); c++) { + if (clustering.get(c).getInclusionProbability(points.get(p)) > 0.8) { + pointsInClusters[c].add(p); + numDistances++; } + } + } - double cindex = 0; - if(numDistancesWithin != 0){ - double meanWithinClustersDistance = withinClustersDistance/numDistancesWithin; - cindex = (meanWithinClustersDistance - minWithin)/(maxWithin-minWithin); + // calc within cluster distances + min and max values + for (int c = 0; c < numClusters; c++) { + int numDistancesInC = 0; + ArrayList<Integer> pointsInC = pointsInClusters[c]; + for (int p = 0; p < pointsInC.size(); p++) { + DataPoint point = points.get(pointsInC.get(p)); + for (int p1 = p + 1; p1 < pointsInC.size(); p1++) { + numDistancesWithin++; + numDistancesInC++; + DataPoint point1 = points.get(pointsInC.get(p1)); + double dist = point.getDistance(point1); + withinClustersDistance += dist; + if (minWithinClusters[c] > dist) + minWithinClusters[c] = dist; + if (maxWithinClusters[c] < dist) + maxWithinClusters[c] = dist; } + } + } + double minWithin = Double.MAX_VALUE; + double maxWithin = Double.MIN_VALUE; + for (int c = 0; c < numClusters; c++) { + if (minWithinClusters[c] < minWithin) + minWithin = minWithinClusters[c]; + if (maxWithinClusters[c] > maxWithin) + maxWithin = maxWithinClusters[c]; + } - if(debug){ - System.out.println("Min:"+Arrays.toString(minWithinClusters)); - System.out.println("Max:"+Arrays.toString(maxWithinClusters)); - System.out.println("totalWithin:"+numDistancesWithin); - } - return cindex; + double cindex = 0; + if (numDistancesWithin != 0) { + double meanWithinClustersDistance = withinClustersDistance / numDistancesWithin; + cindex = (meanWithinClustersDistance - minWithin) / (maxWithin - minWithin); } + if (debug) { + System.out.println("Min:" + Arrays.toString(minWithinClusters)); + System.out.println("Max:" + Arrays.toString(maxWithinClusters)); + System.out.println("totalWithin:" + numDistancesWithin); + } + return cindex; + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldContentEvent.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldContentEvent.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldContentEvent.java index 5e86cb0..82052fd 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldContentEvent.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldContentEvent.java @@ -27,43 +27,43 @@ import com.yahoo.labs.samoa.core.ContentEvent; */ public class HelloWorldContentEvent implements ContentEvent { - private static final long serialVersionUID = -2406968925730298156L; - private final boolean isLastEvent; - private final int helloWorldData; + private static final long serialVersionUID = -2406968925730298156L; + private final boolean isLastEvent; + private final int helloWorldData; - public HelloWorldContentEvent(int helloWorldData, boolean isLastEvent) { - this.isLastEvent = isLastEvent; - this.helloWorldData = helloWorldData; - } - - /* - * No-argument constructor for Kryo - */ - public HelloWorldContentEvent() { - this(0,false); - } + public HelloWorldContentEvent(int helloWorldData, boolean isLastEvent) { + this.isLastEvent = isLastEvent; + this.helloWorldData = helloWorldData; + } - @Override - public String getKey() { - return null; - } + /* + * No-argument constructor for Kryo + */ + public HelloWorldContentEvent() { + this(0, false); + } - @Override - public void setKey(String str) { - // do nothing, it's key-less content event - } + @Override + public String getKey() { + return null; + } - @Override - public boolean isLastEvent() { - return isLastEvent; - } + @Override + public void setKey(String str) { + // do nothing, it's key-less content event + } - public int getHelloWorldData() { - return helloWorldData; - } + @Override + public boolean isLastEvent() { + return isLastEvent; + } - @Override - public String toString() { - return "HelloWorldContentEvent [helloWorldData=" + helloWorldData + "]"; - } + public int getHelloWorldData() { + return helloWorldData; + } + + @Override + public String toString() { + return "HelloWorldContentEvent [helloWorldData=" + helloWorldData + "]"; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldDestinationProcessor.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldDestinationProcessor.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldDestinationProcessor.java index e22c0fe..3d8aac7 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldDestinationProcessor.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldDestinationProcessor.java @@ -24,26 +24,27 @@ import com.yahoo.labs.samoa.core.ContentEvent; import com.yahoo.labs.samoa.core.Processor; /** - * Example {@link Processor} that simply prints the received events to standard output. + * Example {@link Processor} that simply prints the received events to standard + * output. */ public class HelloWorldDestinationProcessor implements Processor { - private static final long serialVersionUID = -6042613438148776446L; - private int processorId; + private static final long serialVersionUID = -6042613438148776446L; + private int processorId; - @Override - public boolean process(ContentEvent event) { - System.out.println(processorId + ": " + event); - return true; - } + @Override + public boolean process(ContentEvent event) { + System.out.println(processorId + ": " + event); + return true; + } - @Override - public void onCreate(int id) { - this.processorId = id; - } + @Override + public void onCreate(int id) { + this.processorId = id; + } - @Override - public Processor newProcessor(Processor p) { - return new HelloWorldDestinationProcessor(); - } + @Override + public Processor newProcessor(Processor p) { + return new HelloWorldDestinationProcessor(); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldSourceProcessor.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldSourceProcessor.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldSourceProcessor.java index a37201f..1f4517d 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldSourceProcessor.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldSourceProcessor.java @@ -31,45 +31,45 @@ import com.yahoo.labs.samoa.core.Processor; */ public class HelloWorldSourceProcessor implements EntranceProcessor { - private static final long serialVersionUID = 6212296305865604747L; - private Random rnd; - private final long maxInst; - private long count; + private static final long serialVersionUID = 6212296305865604747L; + private Random rnd; + private final long maxInst; + private long count; - public HelloWorldSourceProcessor(long maxInst) { - this.maxInst = maxInst; - } + public HelloWorldSourceProcessor(long maxInst) { + this.maxInst = maxInst; + } - @Override - public boolean process(ContentEvent event) { - // do nothing, API will be refined further - return false; - } + @Override + public boolean process(ContentEvent event) { + // do nothing, API will be refined further + return false; + } - @Override - public void onCreate(int id) { - rnd = new Random(id); - } + @Override + public void onCreate(int id) { + rnd = new Random(id); + } - @Override - public Processor newProcessor(Processor p) { - HelloWorldSourceProcessor hwsp = (HelloWorldSourceProcessor) p; - return new HelloWorldSourceProcessor(hwsp.maxInst); - } + @Override + public Processor newProcessor(Processor p) { + HelloWorldSourceProcessor hwsp = (HelloWorldSourceProcessor) p; + return new HelloWorldSourceProcessor(hwsp.maxInst); + } - @Override - public boolean isFinished() { - return count >= maxInst; - } - - @Override - public boolean hasNext() { - return count < maxInst; - } + @Override + public boolean isFinished() { + return count >= maxInst; + } - @Override - public ContentEvent nextEvent() { - count++; - return new HelloWorldContentEvent(rnd.nextInt(), false); - } + @Override + public boolean hasNext() { + return count < maxInst; + } + + @Override + public ContentEvent nextEvent() { + count++; + return new HelloWorldContentEvent(rnd.nextInt(), false); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldTask.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldTask.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldTask.java index e6658f1..2c8e36c 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldTask.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/examples/HelloWorldTask.java @@ -36,63 +36,67 @@ import com.yahoo.labs.samoa.topology.Topology; import com.yahoo.labs.samoa.topology.TopologyBuilder; /** - * Example {@link Task} in SAMOA. This task simply sends events from a source {@link HelloWorldSourceProcessor} to a destination - * {@link HelloWorldDestinationProcessor}. The events are random integers generated by the source and encapsulated in a {@link HelloWorldContentEvent}. The - * destination prints the content of the event to standard output, prepended by the processor id. + * Example {@link Task} in SAMOA. This task simply sends events from a source + * {@link HelloWorldSourceProcessor} to a destination + * {@link HelloWorldDestinationProcessor}. The events are random integers + * generated by the source and encapsulated in a {@link HelloWorldContentEvent}. + * The destination prints the content of the event to standard output, prepended + * by the processor id. * - * The task has 2 main options: the number of events the source will generate (-i) and the parallelism level of the destination (-p). + * The task has 2 main options: the number of events the source will generate + * (-i) and the parallelism level of the destination (-p). */ public class HelloWorldTask implements Task, Configurable { - private static final long serialVersionUID = -5134935141154021352L; - private static Logger logger = LoggerFactory.getLogger(HelloWorldTask.class); - - /** The topology builder for the task. */ - private TopologyBuilder builder; - /** The topology that will be created for the task */ - private Topology helloWorldTopology; - - public IntOption instanceLimitOption = new IntOption("instanceLimit", 'i', - "Maximum number of instances to generate (-1 = no limit).", 1000000, -1, Integer.MAX_VALUE); - - public IntOption helloWorldParallelismOption = new IntOption("parallelismOption", 'p', - "Number of destination Processors", 1, 1, Integer.MAX_VALUE); - - public StringOption evaluationNameOption = new StringOption("evaluationName", 'n', - "Identifier of the evaluation", "HelloWorldTask" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())); - - @Override - public void init() { - // create source EntranceProcessor - /* The event source for the topology. Implements EntranceProcessor */ - HelloWorldSourceProcessor sourceProcessor = new HelloWorldSourceProcessor(instanceLimitOption.getValue()); - builder.addEntranceProcessor(sourceProcessor); - - // create Stream - Stream stream = builder.createStream(sourceProcessor); - - // create destination Processor - /* The event sink for the topology. Implements Processor */ - HelloWorldDestinationProcessor destProcessor = new HelloWorldDestinationProcessor(); - builder.addProcessor(destProcessor, helloWorldParallelismOption.getValue()); - builder.connectInputShuffleStream(stream, destProcessor); - - // build the topology - helloWorldTopology = builder.build(); - logger.debug("Successfully built the topology"); - } - - @Override - public Topology getTopology() { - return helloWorldTopology; - } - - @Override - public void setFactory(ComponentFactory factory) { - // will be removed when dynamic binding is implemented - builder = new TopologyBuilder(factory); - logger.debug("Successfully instantiating TopologyBuilder"); - builder.initTopology(evaluationNameOption.getValue()); - logger.debug("Successfully initializing SAMOA topology with name {}", evaluationNameOption.getValue()); - } + private static final long serialVersionUID = -5134935141154021352L; + private static Logger logger = LoggerFactory.getLogger(HelloWorldTask.class); + + /** The topology builder for the task. */ + private TopologyBuilder builder; + /** The topology that will be created for the task */ + private Topology helloWorldTopology; + + public IntOption instanceLimitOption = new IntOption("instanceLimit", 'i', + "Maximum number of instances to generate (-1 = no limit).", 1000000, -1, Integer.MAX_VALUE); + + public IntOption helloWorldParallelismOption = new IntOption("parallelismOption", 'p', + "Number of destination Processors", 1, 1, Integer.MAX_VALUE); + + public StringOption evaluationNameOption = new StringOption("evaluationName", 'n', + "Identifier of the evaluation", "HelloWorldTask" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())); + + @Override + public void init() { + // create source EntranceProcessor + /* The event source for the topology. Implements EntranceProcessor */ + HelloWorldSourceProcessor sourceProcessor = new HelloWorldSourceProcessor(instanceLimitOption.getValue()); + builder.addEntranceProcessor(sourceProcessor); + + // create Stream + Stream stream = builder.createStream(sourceProcessor); + + // create destination Processor + /* The event sink for the topology. Implements Processor */ + HelloWorldDestinationProcessor destProcessor = new HelloWorldDestinationProcessor(); + builder.addProcessor(destProcessor, helloWorldParallelismOption.getValue()); + builder.connectInputShuffleStream(stream, destProcessor); + + // build the topology + helloWorldTopology = builder.build(); + logger.debug("Successfully built the topology"); + } + + @Override + public Topology getTopology() { + return helloWorldTopology; + } + + @Override + public void setFactory(ComponentFactory factory) { + // will be removed when dynamic binding is implemented + builder = new TopologyBuilder(factory); + logger.debug("Successfully instantiating TopologyBuilder"); + builder.initTopology(evaluationNameOption.getValue()); + logger.debug("Successfully initializing SAMOA topology with name {}", evaluationNameOption.getValue()); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/AdaptiveLearner.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/AdaptiveLearner.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/AdaptiveLearner.java index 0986253..e465b7d 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/AdaptiveLearner.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/AdaptiveLearner.java @@ -24,30 +24,30 @@ package com.yahoo.labs.samoa.learners; * License */ - import com.yahoo.labs.samoa.moa.classifiers.core.driftdetection.ChangeDetector; import com.yahoo.labs.samoa.topology.Stream; /** - * The Interface Adaptive Learner. - * Initializing Classifier should initalize PI to connect the Classifier with the input stream - * and initialize result stream so that other PI can connect to the classification result of this classifier + * The Interface Adaptive Learner. Initializing Classifier should initalize PI + * to connect the Classifier with the input stream and initialize result stream + * so that other PI can connect to the classification result of this classifier */ public interface AdaptiveLearner { - /** - * Gets the change detector item. - * - * @return the change detector item - */ - public ChangeDetector getChangeDetector(); - - /** - * Sets the change detector item. - * - * @param cd the change detector item - */ - public void setChangeDetector(ChangeDetector cd); - + /** + * Gets the change detector item. + * + * @return the change detector item + */ + public ChangeDetector getChangeDetector(); + + /** + * Sets the change detector item. + * + * @param cd + * the change detector item + */ + public void setChangeDetector(ChangeDetector cd); + } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstanceContentEvent.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstanceContentEvent.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstanceContentEvent.java index 91b1b7b..fd25736 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstanceContentEvent.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstanceContentEvent.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.learners; /* @@ -29,8 +28,8 @@ import com.yahoo.labs.samoa.core.ContentEvent; import com.yahoo.labs.samoa.core.SerializableInstance; import net.jcip.annotations.Immutable; import com.yahoo.labs.samoa.instances.Instance; -//import weka.core.Instance; +//import weka.core.Instance; /** * The Class InstanceEvent. @@ -38,170 +37,178 @@ import com.yahoo.labs.samoa.instances.Instance; @Immutable final public class InstanceContentEvent implements ContentEvent { - /** + /** * */ - private static final long serialVersionUID = -8620668863064613845L; - private long instanceIndex; - private int classifierIndex; - private int evaluationIndex; - private SerializableInstance instance; - private boolean isTraining; - private boolean isTesting; - private boolean isLast = false; - - public InstanceContentEvent() { - - } - - /** - * Instantiates a new instance event. - * - * @param index the index - * @param instance the instance - * @param isTraining the is training - */ - public InstanceContentEvent(long index, Instance instance, - boolean isTraining, boolean isTesting) { - if (instance != null) { - this.instance = new SerializableInstance(instance); - } - this.instanceIndex = index; - this.isTraining = isTraining; - this.isTesting = isTesting; - } - - /** - * Gets the single instance of InstanceEvent. - * - * @return the instance. - */ - public Instance getInstance() { - return instance; - } - - /** - * Gets the instance index. - * - * @return the index of the data vector. - */ - public long getInstanceIndex() { - return instanceIndex; - } - - /** - * Gets the class id. - * - * @return the true class of the vector. - */ - public int getClassId() { - // return classId; - return (int) instance.classValue(); - } - - /** - * Checks if is training. - * - * @return true if this is training data. - */ - public boolean isTraining() { - return isTraining; - } - - /** - * Set training flag. - * - * @param training flag. - */ - public void setTraining(boolean training) { - this.isTraining = training; - } - - /** - * Checks if is testing. - * - * @return true if this is testing data. - */ - public boolean isTesting(){ - return isTesting; - } - - /** - * Set testing flag. - * - * @param testing flag. - */ - public void setTesting(boolean testing) { - this.isTesting = testing; - } - - /** - * Gets the classifier index. - * - * @return the classifier index - */ - public int getClassifierIndex() { - return classifierIndex; - } - - /** - * Sets the classifier index. - * - * @param classifierIndex the new classifier index - */ - public void setClassifierIndex(int classifierIndex) { - this.classifierIndex = classifierIndex; - } - - /** - * Gets the evaluation index. - * - * @return the evaluation index - */ - public int getEvaluationIndex() { - return evaluationIndex; - } - - /** - * Sets the evaluation index. - * - * @param evaluationIndex the new evaluation index - */ - public void setEvaluationIndex(int evaluationIndex) { - this.evaluationIndex = evaluationIndex; - } + private static final long serialVersionUID = -8620668863064613845L; + private long instanceIndex; + private int classifierIndex; + private int evaluationIndex; + private SerializableInstance instance; + private boolean isTraining; + private boolean isTesting; + private boolean isLast = false; + + public InstanceContentEvent() { + + } + + /** + * Instantiates a new instance event. + * + * @param index + * the index + * @param instance + * the instance + * @param isTraining + * the is training + */ + public InstanceContentEvent(long index, Instance instance, + boolean isTraining, boolean isTesting) { + if (instance != null) { + this.instance = new SerializableInstance(instance); + } + this.instanceIndex = index; + this.isTraining = isTraining; + this.isTesting = isTesting; + } + + /** + * Gets the single instance of InstanceEvent. + * + * @return the instance. + */ + public Instance getInstance() { + return instance; + } + + /** + * Gets the instance index. + * + * @return the index of the data vector. + */ + public long getInstanceIndex() { + return instanceIndex; + } + + /** + * Gets the class id. + * + * @return the true class of the vector. + */ + public int getClassId() { + // return classId; + return (int) instance.classValue(); + } + + /** + * Checks if is training. + * + * @return true if this is training data. + */ + public boolean isTraining() { + return isTraining; + } + + /** + * Set training flag. + * + * @param training + * flag. + */ + public void setTraining(boolean training) { + this.isTraining = training; + } + + /** + * Checks if is testing. + * + * @return true if this is testing data. + */ + public boolean isTesting() { + return isTesting; + } + + /** + * Set testing flag. + * + * @param testing + * flag. + */ + public void setTesting(boolean testing) { + this.isTesting = testing; + } + + /** + * Gets the classifier index. + * + * @return the classifier index + */ + public int getClassifierIndex() { + return classifierIndex; + } + + /** + * Sets the classifier index. + * + * @param classifierIndex + * the new classifier index + */ + public void setClassifierIndex(int classifierIndex) { + this.classifierIndex = classifierIndex; + } + + /** + * Gets the evaluation index. + * + * @return the evaluation index + */ + public int getEvaluationIndex() { + return evaluationIndex; + } + + /** + * Sets the evaluation index. + * + * @param evaluationIndex + * the new evaluation index + */ + public void setEvaluationIndex(int evaluationIndex) { + this.evaluationIndex = evaluationIndex; + } + + /* + * (non-Javadoc) + * + * @see samoa.core.ContentEvent#getKey(int) + */ + public String getKey(int key) { + if (key == 0) + return Long.toString(this.getEvaluationIndex()); + else + return Long.toString(10000 + * this.getEvaluationIndex() + + this.getClassifierIndex()); + } + + @Override + public String getKey() { + // System.out.println("InstanceContentEvent "+Long.toString(this.instanceIndex)); + return Long.toString(this.getClassifierIndex()); + } + + @Override + public void setKey(String str) { + this.instanceIndex = Long.parseLong(str); + } + + @Override + public boolean isLastEvent() { + return isLast; + } + + public void setLast(boolean isLast) { + this.isLast = isLast; + } - /* (non-Javadoc) - * @see samoa.core.ContentEvent#getKey(int) - */ - public String getKey(int key) { - if (key == 0) - return Long.toString(this.getEvaluationIndex()); - else return Long.toString(10000 - * this.getEvaluationIndex() - + this.getClassifierIndex()); - } - - @Override - public String getKey() { - //System.out.println("InstanceContentEvent "+Long.toString(this.instanceIndex)); - return Long.toString(this.getClassifierIndex()); - } - - @Override - public void setKey(String str) { - this.instanceIndex = Long.parseLong(str); - } - - @Override - public boolean isLastEvent() { - return isLast; - } - - public void setLast(boolean isLast) { - this.isLast = isLast; - } - - - } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstancesContentEvent.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstancesContentEvent.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstancesContentEvent.java index ff005b6..ce5937a 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstancesContentEvent.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/InstancesContentEvent.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.learners; /* @@ -31,8 +30,8 @@ import net.jcip.annotations.Immutable; import com.yahoo.labs.samoa.instances.Instance; import java.util.LinkedList; import java.util.List; -//import weka.core.Instance; +//import weka.core.Instance; /** * The Class InstanceEvent. @@ -40,154 +39,161 @@ import java.util.List; @Immutable final public class InstancesContentEvent implements ContentEvent { - /** + /** * */ - private static final long serialVersionUID = -8620668863064613845L; - private long instanceIndex; - private int classifierIndex; - private int evaluationIndex; - //private SerializableInstance instance; - private boolean isTraining; - private boolean isTesting; - private boolean isLast = false; - - public InstancesContentEvent() { - - } - - /** - * Instantiates a new instance event. - * - * @param index the index - * @param instance the instance - * @param isTraining the is training - */ - public InstancesContentEvent(long index,// Instance instance, - boolean isTraining, boolean isTesting) { - /*if (instance != null) { - this.instance = new SerializableInstance(instance); - }*/ - this.instanceIndex = index; - this.isTraining = isTraining; - this.isTesting = isTesting; - } - - public InstancesContentEvent(InstanceContentEvent event){ - this.instanceIndex = event.getInstanceIndex(); - this.isTraining = event.isTraining(); - this.isTesting = event.isTesting(); - } - - protected List<Instance> instanceList = new LinkedList<Instance>(); - - public void add(Instance instance){ - instanceList.add(new SerializableInstance(instance)); - } - - /** - * Gets the single instance of InstanceEvent. - * - * @return the instance. - */ - public Instance[] getInstances() { - return instanceList.toArray(new Instance[instanceList.size()]); - } - - /** - * Gets the instance index. - * - * @return the index of the data vector. - */ - public long getInstanceIndex() { - return instanceIndex; - } - - /** - * Checks if is training. - * - * @return true if this is training data. - */ - public boolean isTraining() { - return isTraining; - } - - /** - * Checks if is testing. - * - * @return true if this is testing data. - */ - public boolean isTesting(){ - return isTesting; - } - - /** - * Gets the classifier index. - * - * @return the classifier index - */ - public int getClassifierIndex() { - return classifierIndex; - } - - /** - * Sets the classifier index. - * - * @param classifierIndex the new classifier index - */ - public void setClassifierIndex(int classifierIndex) { - this.classifierIndex = classifierIndex; - } - - /** - * Gets the evaluation index. - * - * @return the evaluation index - */ - public int getEvaluationIndex() { - return evaluationIndex; - } - - /** - * Sets the evaluation index. - * - * @param evaluationIndex the new evaluation index - */ - public void setEvaluationIndex(int evaluationIndex) { - this.evaluationIndex = evaluationIndex; - } + private static final long serialVersionUID = -8620668863064613845L; + private long instanceIndex; + private int classifierIndex; + private int evaluationIndex; + // private SerializableInstance instance; + private boolean isTraining; + private boolean isTesting; + private boolean isLast = false; + + public InstancesContentEvent() { + + } + + /** + * Instantiates a new instance event. + * + * @param index + * the index + * @param instance + * the instance + * @param isTraining + * the is training + */ + public InstancesContentEvent(long index,// Instance instance, + boolean isTraining, boolean isTesting) { + /* + * if (instance != null) { this.instance = new + * SerializableInstance(instance); } + */ + this.instanceIndex = index; + this.isTraining = isTraining; + this.isTesting = isTesting; + } + + public InstancesContentEvent(InstanceContentEvent event) { + this.instanceIndex = event.getInstanceIndex(); + this.isTraining = event.isTraining(); + this.isTesting = event.isTesting(); + } + + protected List<Instance> instanceList = new LinkedList<Instance>(); + + public void add(Instance instance) { + instanceList.add(new SerializableInstance(instance)); + } + + /** + * Gets the single instance of InstanceEvent. + * + * @return the instance. + */ + public Instance[] getInstances() { + return instanceList.toArray(new Instance[instanceList.size()]); + } + + /** + * Gets the instance index. + * + * @return the index of the data vector. + */ + public long getInstanceIndex() { + return instanceIndex; + } + + /** + * Checks if is training. + * + * @return true if this is training data. + */ + public boolean isTraining() { + return isTraining; + } + + /** + * Checks if is testing. + * + * @return true if this is testing data. + */ + public boolean isTesting() { + return isTesting; + } + + /** + * Gets the classifier index. + * + * @return the classifier index + */ + public int getClassifierIndex() { + return classifierIndex; + } + + /** + * Sets the classifier index. + * + * @param classifierIndex + * the new classifier index + */ + public void setClassifierIndex(int classifierIndex) { + this.classifierIndex = classifierIndex; + } + + /** + * Gets the evaluation index. + * + * @return the evaluation index + */ + public int getEvaluationIndex() { + return evaluationIndex; + } + + /** + * Sets the evaluation index. + * + * @param evaluationIndex + * the new evaluation index + */ + public void setEvaluationIndex(int evaluationIndex) { + this.evaluationIndex = evaluationIndex; + } + + /* + * (non-Javadoc) + * + * @see samoa.core.ContentEvent#getKey(int) + */ + public String getKey(int key) { + if (key == 0) + return Long.toString(this.getEvaluationIndex()); + else + return Long.toString(10000 + * this.getEvaluationIndex() + + this.getClassifierIndex()); + } + + @Override + public String getKey() { + // System.out.println("InstanceContentEvent "+Long.toString(this.instanceIndex)); + return Long.toString(this.getClassifierIndex()); + } + + @Override + public void setKey(String str) { + this.instanceIndex = Long.parseLong(str); + } + + @Override + public boolean isLastEvent() { + return isLast; + } + + public void setLast(boolean isLast) { + this.isLast = isLast; + } - /* (non-Javadoc) - * @see samoa.core.ContentEvent#getKey(int) - */ - public String getKey(int key) { - if (key == 0) - return Long.toString(this.getEvaluationIndex()); - else return Long.toString(10000 - * this.getEvaluationIndex() - + this.getClassifierIndex()); - } - - @Override - public String getKey() { - //System.out.println("InstanceContentEvent "+Long.toString(this.instanceIndex)); - return Long.toString(this.getClassifierIndex()); - } - - @Override - public void setKey(String str) { - this.instanceIndex = Long.parseLong(str); - } - - @Override - public boolean isLastEvent() { - return isLast; - } - - public void setLast(boolean isLast) { - this.isLast = isLast; - } - - - } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/Learner.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/Learner.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/Learner.java index 993ca47..636b023 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/Learner.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/Learner.java @@ -29,34 +29,36 @@ import java.io.Serializable; import java.util.Set; /** - * The Interface Classifier. - * Initializing Classifier should initalize PI to connect the Classifier with the input stream - * and initialize result stream so that other PI can connect to the classification result of this classifier + * The Interface Classifier. Initializing Classifier should initalize PI to + * connect the Classifier with the input stream and initialize result stream so + * that other PI can connect to the classification result of this classifier */ -public interface Learner extends Serializable{ - - /** - * Inits the Learner object. - * - * @param topologyBuilder the topology builder - * @param dataset the dataset - * @param parallelism the parallelism - */ - public void init(TopologyBuilder topologyBuilder, Instances dataset, int parallelism); - - /** - * Gets the input processing item. - * - * @return the input processing item - */ - public Processor getInputProcessor(); - - - /** - * Gets the result streams - * - * @return the set of result streams - */ - public Set<Stream> getResultStreams(); +public interface Learner extends Serializable { + + /** + * Inits the Learner object. + * + * @param topologyBuilder + * the topology builder + * @param dataset + * the dataset + * @param parallelism + * the parallelism + */ + public void init(TopologyBuilder topologyBuilder, Instances dataset, int parallelism); + + /** + * Gets the input processing item. + * + * @return the input processing item + */ + public Processor getInputProcessor(); + + /** + * Gets the result streams + * + * @return the set of result streams + */ + public Set<Stream> getResultStreams(); } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/ResultContentEvent.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/ResultContentEvent.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/ResultContentEvent.java index 0879872..cb1e317 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/ResultContentEvent.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/ResultContentEvent.java @@ -28,185 +28,186 @@ import com.yahoo.labs.samoa.instances.Instance; * License */ - /** * The Class ResultEvent. */ final public class ResultContentEvent implements ContentEvent { - /** - * - */ - private static final long serialVersionUID = -2650420235386873306L; - private long instanceIndex; - private int classifierIndex; - private int evaluationIndex; - private SerializableInstance instance; - - private int classId; - private double[] classVotes; - - private final boolean isLast; - - public ResultContentEvent(){ - this.isLast = false; - } - - - public ResultContentEvent(boolean isLast) { - this.isLast = isLast; - } - - /** - * Instantiates a new result event. - * - * @param instanceIndex - * the instance index - * @param instance - * the instance - * @param classId - * the class id - * @param classVotes - * the class votes - */ - public ResultContentEvent(long instanceIndex, Instance instance, int classId, - double[] classVotes, boolean isLast) { - if(instance != null){ - this.instance = new SerializableInstance(instance); - } - this.instanceIndex = instanceIndex; - this.classId = classId; - this.classVotes = classVotes; - this.isLast = isLast; - } - - /** - * Gets the single instance of ResultEvent. - * - * @return single instance of ResultEvent - */ - public SerializableInstance getInstance() { - return instance; - } - - /** - * Sets the instance. - * - * @param instance - * the new instance - */ - public void setInstance(SerializableInstance instance) { - this.instance = instance; - } - - /** - * Gets the num classes. - * - * @return the num classes - */ - public int getNumClasses() { // To remove - return instance.numClasses(); - } - - /** - * Gets the instance index. - * - * @return the index of the data vector. - */ - public long getInstanceIndex() { - return instanceIndex; - } - - /** - * Gets the class id. - * - * @return the true class of the vector. - */ - public int getClassId() { // To remove - return classId;// (int) instance.classValue();//classId; - } - - /** - * Gets the class votes. - * - * @return the class votes - */ - public double[] getClassVotes() { - return classVotes; - } - - /** - * Sets the class votes. - * - * @param classVotes - * the new class votes - */ - public void setClassVotes(double[] classVotes) { - this.classVotes = classVotes; - } - - /** - * Gets the classifier index. - * - * @return the classifier index - */ - public int getClassifierIndex() { - return classifierIndex; - } - - /** - * Sets the classifier index. - * - * @param classifierIndex - * the new classifier index - */ - public void setClassifierIndex(int classifierIndex) { - this.classifierIndex = classifierIndex; - } - - /** - * Gets the evaluation index. + /** * - * @return the evaluation index - */ - public int getEvaluationIndex() { - return evaluationIndex; - } - - /** - * Sets the evaluation index. - * - * @param evaluationIndex - * the new evaluation index - */ - public void setEvaluationIndex(int evaluationIndex) { - this.evaluationIndex = evaluationIndex; - } - - /* (non-Javadoc) - * @see samoa.core.ContentEvent#getKey(int) */ - //@Override - public String getKey(int key) { - if (key == 0) - return Long.toString(this.getEvaluationIndex()); - else return Long.toString(this.getEvaluationIndex() - + 1000 * this.getInstanceIndex()); - } - - @Override - public String getKey() { - return Long.toString(this.getEvaluationIndex()%100); - } - - @Override - public void setKey(String str) { - this.evaluationIndex = Integer.parseInt(str); - } - - @Override - public boolean isLastEvent() { - return isLast; - } + private static final long serialVersionUID = -2650420235386873306L; + private long instanceIndex; + private int classifierIndex; + private int evaluationIndex; + private SerializableInstance instance; + + private int classId; + private double[] classVotes; + + private final boolean isLast; + + public ResultContentEvent() { + this.isLast = false; + } + + public ResultContentEvent(boolean isLast) { + this.isLast = isLast; + } + + /** + * Instantiates a new result event. + * + * @param instanceIndex + * the instance index + * @param instance + * the instance + * @param classId + * the class id + * @param classVotes + * the class votes + */ + public ResultContentEvent(long instanceIndex, Instance instance, int classId, + double[] classVotes, boolean isLast) { + if (instance != null) { + this.instance = new SerializableInstance(instance); + } + this.instanceIndex = instanceIndex; + this.classId = classId; + this.classVotes = classVotes; + this.isLast = isLast; + } + + /** + * Gets the single instance of ResultEvent. + * + * @return single instance of ResultEvent + */ + public SerializableInstance getInstance() { + return instance; + } + + /** + * Sets the instance. + * + * @param instance + * the new instance + */ + public void setInstance(SerializableInstance instance) { + this.instance = instance; + } + + /** + * Gets the num classes. + * + * @return the num classes + */ + public int getNumClasses() { // To remove + return instance.numClasses(); + } + + /** + * Gets the instance index. + * + * @return the index of the data vector. + */ + public long getInstanceIndex() { + return instanceIndex; + } + + /** + * Gets the class id. + * + * @return the true class of the vector. + */ + public int getClassId() { // To remove + return classId;// (int) instance.classValue();//classId; + } + + /** + * Gets the class votes. + * + * @return the class votes + */ + public double[] getClassVotes() { + return classVotes; + } + + /** + * Sets the class votes. + * + * @param classVotes + * the new class votes + */ + public void setClassVotes(double[] classVotes) { + this.classVotes = classVotes; + } + + /** + * Gets the classifier index. + * + * @return the classifier index + */ + public int getClassifierIndex() { + return classifierIndex; + } + + /** + * Sets the classifier index. + * + * @param classifierIndex + * the new classifier index + */ + public void setClassifierIndex(int classifierIndex) { + this.classifierIndex = classifierIndex; + } + + /** + * Gets the evaluation index. + * + * @return the evaluation index + */ + public int getEvaluationIndex() { + return evaluationIndex; + } + + /** + * Sets the evaluation index. + * + * @param evaluationIndex + * the new evaluation index + */ + public void setEvaluationIndex(int evaluationIndex) { + this.evaluationIndex = evaluationIndex; + } + + /* + * (non-Javadoc) + * + * @see samoa.core.ContentEvent#getKey(int) + */ + // @Override + public String getKey(int key) { + if (key == 0) + return Long.toString(this.getEvaluationIndex()); + else + return Long.toString(this.getEvaluationIndex() + + 1000 * this.getInstanceIndex()); + } + + @Override + public String getKey() { + return Long.toString(this.getEvaluationIndex() % 100); + } + + @Override + public void setKey(String str) { + this.evaluationIndex = Integer.parseInt(str); + } + + @Override + public boolean isLastEvent() { + return isLast; + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearner.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearner.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearner.java index b5c30db..80ddbd2 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearner.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearner.java @@ -32,47 +32,47 @@ import com.yahoo.labs.samoa.instances.Instances; * @author abifet */ public interface LocalLearner extends Serializable { - - /** - * Creates a new learner object. - * - * @return the learner - */ - LocalLearner create(); - - /** - * Predicts the class memberships for a given instance. If an instance is - * unclassified, the returned array elements must be all zero. - * - * @param inst - * the instance to be classified - * @return an array containing the estimated membership probabilities of the - * test instance in each class - */ - double[] getVotesForInstance(Instance inst); - /** - * Resets this classifier. It must be similar to starting a new classifier - * from scratch. - * - */ - void resetLearning(); + /** + * Creates a new learner object. + * + * @return the learner + */ + LocalLearner create(); - /** - * Trains this classifier incrementally using the given instance. - * - * @param inst - * the instance to be used for training - */ - void trainOnInstance(Instance inst); - - /** - * Sets where to obtain the information of attributes of Instances - * - * @param dataset - * the dataset that contains the information - */ - @Deprecated - public void setDataset(Instances dataset); + /** + * Predicts the class memberships for a given instance. If an instance is + * unclassified, the returned array elements must be all zero. + * + * @param inst + * the instance to be classified + * @return an array containing the estimated membership probabilities of the + * test instance in each class + */ + double[] getVotesForInstance(Instance inst); + + /** + * Resets this classifier. It must be similar to starting a new classifier + * from scratch. + * + */ + void resetLearning(); + + /** + * Trains this classifier incrementally using the given instance. + * + * @param inst + * the instance to be used for training + */ + void trainOnInstance(Instance inst); + + /** + * Sets where to obtain the information of attributes of Instances + * + * @param dataset + * the dataset that contains the information + */ + @Deprecated + public void setDataset(Instances dataset); } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearnerProcessor.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearnerProcessor.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearnerProcessor.java index ae897f0..978a839 100755 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearnerProcessor.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/learners/classifiers/LocalLearnerProcessor.java @@ -42,176 +42,184 @@ import static com.yahoo.labs.samoa.moa.core.Utils.maxIndex; */ final public class LocalLearnerProcessor implements Processor { - /** + /** * */ - private static final long serialVersionUID = -1577910988699148691L; - - private static final Logger logger = LoggerFactory.getLogger(LocalLearnerProcessor.class); - - private LocalLearner model; - private Stream outputStream; - private int modelId; - private long instancesCount = 0; - - /** - * Sets the learner. - * - * @param model the model to set - */ - public void setLearner(LocalLearner model) { - this.model = model; - } - - /** - * Gets the learner. - * - * @return the model - */ - public LocalLearner getLearner() { - return model; - } - - /** - * Set the output streams. - * - * @param outputStream the new output stream - */ - public void setOutputStream(Stream outputStream) { - this.outputStream = outputStream; - } - - /** - * Gets the output stream. - * - * @return the output stream - */ - public Stream getOutputStream() { - return outputStream; - } - - /** - * Gets the instances count. - * - * @return number of observation vectors used in training iteration. - */ - public long getInstancesCount() { - return instancesCount; - } - - /** - * Update stats. - * - * @param event the event - */ - private void updateStats(InstanceContentEvent event) { - Instance inst = event.getInstance(); - this.model.trainOnInstance(inst); - this.instancesCount++; - if (this.changeDetector != null) { - boolean correctlyClassifies = this.correctlyClassifies(inst); - double oldEstimation = this.changeDetector.getEstimation(); - this.changeDetector.input(correctlyClassifies ? 0 : 1); - if (this.changeDetector.getChange() && this.changeDetector.getEstimation() > oldEstimation) { - //Start a new classifier - this.model.resetLearning(); - this.changeDetector.resetLearning(); - } - } - } - - /** - * Gets whether this classifier correctly classifies an instance. Uses - * getVotesForInstance to obtain the prediction and the instance to obtain - * its true class. - * - * - * @param inst the instance to be classified - * @return true if the instance is correctly classified - */ - private boolean correctlyClassifies(Instance inst) { - return maxIndex(model.getVotesForInstance(inst)) == (int) inst.classValue(); - } - - /** The test. */ - protected int test; //to delete - - /** - * On event. - * - * @param event the event - * @return true, if successful - */ - @Override - public boolean process(ContentEvent event) { - - InstanceContentEvent inEvent = (InstanceContentEvent) event; - Instance instance = inEvent.getInstance(); - - if (inEvent.getInstanceIndex() < 0) { - //end learning - ResultContentEvent outContentEvent = new ResultContentEvent(-1, instance, 0, - new double[0], inEvent.isLastEvent()); - outContentEvent.setClassifierIndex(this.modelId); - outContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); - outputStream.put(outContentEvent); - return false; - } - - if (inEvent.isTesting()){ - double[] dist = model.getVotesForInstance(instance); - ResultContentEvent outContentEvent = new ResultContentEvent(inEvent.getInstanceIndex(), - instance, inEvent.getClassId(), dist, inEvent.isLastEvent()); - outContentEvent.setClassifierIndex(this.modelId); - outContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); - logger.trace(inEvent.getInstanceIndex() + " {} {}", modelId, dist); - outputStream.put(outContentEvent); - } - - if (inEvent.isTraining()) { - updateStats(inEvent); - } - return false; - } - - /* (non-Javadoc) - * @see samoa.core.Processor#onCreate(int) - */ - @Override - public void onCreate(int id) { - this.modelId = id; - model = model.create(); - } - - /* (non-Javadoc) - * @see samoa.core.Processor#newProcessor(samoa.core.Processor) - */ - @Override - public Processor newProcessor(Processor sourceProcessor) { - LocalLearnerProcessor newProcessor = new LocalLearnerProcessor(); - LocalLearnerProcessor originProcessor = (LocalLearnerProcessor) sourceProcessor; - - if (originProcessor.getLearner() != null){ - newProcessor.setLearner(originProcessor.getLearner().create()); - } - - if (originProcessor.getChangeDetector() != null){ - newProcessor.setChangeDetector(originProcessor.getChangeDetector()); - } - - newProcessor.setOutputStream(originProcessor.getOutputStream()); - return newProcessor; - } - - protected ChangeDetector changeDetector; - - public ChangeDetector getChangeDetector() { - return this.changeDetector; - } - - public void setChangeDetector(ChangeDetector cd) { - this.changeDetector = cd; - } - + private static final long serialVersionUID = -1577910988699148691L; + + private static final Logger logger = LoggerFactory.getLogger(LocalLearnerProcessor.class); + + private LocalLearner model; + private Stream outputStream; + private int modelId; + private long instancesCount = 0; + + /** + * Sets the learner. + * + * @param model + * the model to set + */ + public void setLearner(LocalLearner model) { + this.model = model; + } + + /** + * Gets the learner. + * + * @return the model + */ + public LocalLearner getLearner() { + return model; + } + + /** + * Set the output streams. + * + * @param outputStream + * the new output stream + */ + public void setOutputStream(Stream outputStream) { + this.outputStream = outputStream; + } + + /** + * Gets the output stream. + * + * @return the output stream + */ + public Stream getOutputStream() { + return outputStream; + } + + /** + * Gets the instances count. + * + * @return number of observation vectors used in training iteration. + */ + public long getInstancesCount() { + return instancesCount; + } + + /** + * Update stats. + * + * @param event + * the event + */ + private void updateStats(InstanceContentEvent event) { + Instance inst = event.getInstance(); + this.model.trainOnInstance(inst); + this.instancesCount++; + if (this.changeDetector != null) { + boolean correctlyClassifies = this.correctlyClassifies(inst); + double oldEstimation = this.changeDetector.getEstimation(); + this.changeDetector.input(correctlyClassifies ? 0 : 1); + if (this.changeDetector.getChange() && this.changeDetector.getEstimation() > oldEstimation) { + // Start a new classifier + this.model.resetLearning(); + this.changeDetector.resetLearning(); + } + } + } + + /** + * Gets whether this classifier correctly classifies an instance. Uses + * getVotesForInstance to obtain the prediction and the instance to obtain its + * true class. + * + * + * @param inst + * the instance to be classified + * @return true if the instance is correctly classified + */ + private boolean correctlyClassifies(Instance inst) { + return maxIndex(model.getVotesForInstance(inst)) == (int) inst.classValue(); + } + + /** The test. */ + protected int test; // to delete + + /** + * On event. + * + * @param event + * the event + * @return true, if successful + */ + @Override + public boolean process(ContentEvent event) { + + InstanceContentEvent inEvent = (InstanceContentEvent) event; + Instance instance = inEvent.getInstance(); + + if (inEvent.getInstanceIndex() < 0) { + // end learning + ResultContentEvent outContentEvent = new ResultContentEvent(-1, instance, 0, + new double[0], inEvent.isLastEvent()); + outContentEvent.setClassifierIndex(this.modelId); + outContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); + outputStream.put(outContentEvent); + return false; + } + + if (inEvent.isTesting()) { + double[] dist = model.getVotesForInstance(instance); + ResultContentEvent outContentEvent = new ResultContentEvent(inEvent.getInstanceIndex(), + instance, inEvent.getClassId(), dist, inEvent.isLastEvent()); + outContentEvent.setClassifierIndex(this.modelId); + outContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); + logger.trace(inEvent.getInstanceIndex() + " {} {}", modelId, dist); + outputStream.put(outContentEvent); + } + + if (inEvent.isTraining()) { + updateStats(inEvent); + } + return false; + } + + /* + * (non-Javadoc) + * + * @see samoa.core.Processor#onCreate(int) + */ + @Override + public void onCreate(int id) { + this.modelId = id; + model = model.create(); + } + + /* + * (non-Javadoc) + * + * @see samoa.core.Processor#newProcessor(samoa.core.Processor) + */ + @Override + public Processor newProcessor(Processor sourceProcessor) { + LocalLearnerProcessor newProcessor = new LocalLearnerProcessor(); + LocalLearnerProcessor originProcessor = (LocalLearnerProcessor) sourceProcessor; + + if (originProcessor.getLearner() != null) { + newProcessor.setLearner(originProcessor.getLearner().create()); + } + + if (originProcessor.getChangeDetector() != null) { + newProcessor.setChangeDetector(originProcessor.getChangeDetector()); + } + + newProcessor.setOutputStream(originProcessor.getOutputStream()); + return newProcessor; + } + + protected ChangeDetector changeDetector; + + public ChangeDetector getChangeDetector() { + return this.changeDetector; + } + + public void setChangeDetector(ChangeDetector cd) { + this.changeDetector = cd; + } }
