diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/AdaptiveBagging.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/AdaptiveBagging.java index 4b2c5317..8f111eea 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/AdaptiveBagging.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/AdaptiveBagging.java @@ -66,9 +66,6 @@ public class AdaptiveBagging implements Learner, Configurable { /** The distributor processor. */ private BaggingDistributorProcessor distributorP; - /** The input streams for the ensemble, one per member. */ - private Stream[] ensembleStreams; - /** The result stream. */ protected Stream resultStream; @@ -116,7 +113,8 @@ protected void setLayout() { } } - ensembleStreams = new Stream[ensembleSize]; + /* The input streams for the ensemble, one per member. */ + Stream[] ensembleStreams = new Stream[ensembleSize]; for (int i = 0; i < ensembleSize; i++) { ensembleStreams[i] = builder.createStream(distributorP); builder.connectInputShuffleStream(ensembleStreams[i], ensemble[i].getInputProcessor()); // connect streams one-to-one with ensemble members (the type of connection does not matter) diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/Boosting.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/Boosting.java index 4971fbaa..2c82d557 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/Boosting.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ensemble/Boosting.java @@ -103,9 +103,6 @@ protected void setLayout() { Stream predictionStream = this.builder.createStream(distributorP); this.builder.connectInputKeyStream(predictionStream, classifier.getInputProcessor()); -// distributorP.setOutputStream(testingStream); -// distributorP.setPredictionStream(predictionStream); - // Addition to Bagging: stream to train /* The training stream. */ Stream trainingStream = this.builder.createStream(predictionCombinerP); diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/centralized/AMRulesRegressorProcessor.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/centralized/AMRulesRegressorProcessor.java index 55f00644..685e2f98 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/centralized/AMRulesRegressorProcessor.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/centralized/AMRulesRegressorProcessor.java @@ -163,7 +163,7 @@ private double[] getVotesForInstance(Instance instance) { int numberOfRulesCovering = 0; for (ActiveRule rule : ruleSet) { - if (rule.isCovering(instance) == true) { + if (rule.isCovering(instance)) { numberOfRulesCovering++; double[] vote = rule.getPrediction(instance); double error = rule.getCurrentError(); @@ -179,9 +179,8 @@ private double[] getVotesForInstance(Instance instance) { double error = defaultRule.getCurrentError(); errorWeightedVote.addVote(vote, error); } - double[] weightedVote = errorWeightedVote.computeWeightedVote(); - return weightedVote; + return errorWeightedVote.computeWeightedVote(); } public ErrorWeightedVote newErrorWeightedVote() { @@ -223,13 +222,13 @@ public void trainOnInstanceImpl(Instance instance) { Iterator ruleIterator = this.ruleSet.iterator(); while (ruleIterator.hasNext()) { ActiveRule rule = ruleIterator.next(); - if (rule.isCovering(instance) == true) { + if (rule.isCovering(instance)) { rulesCoveringInstance = true; - if (isAnomaly(instance, rule) == false) { + if (!isAnomaly(instance, rule)) { // Update Change Detection Tests double error = rule.computeError(instance); // Use adaptive mode error boolean changeDetected = ((RuleActiveRegressionNode) rule.getLearningNode()).updateChangeDetection(error); - if (changeDetected == true) { + if (changeDetected) { ruleIterator.remove(); } else { rule.updateStatistics(instance); @@ -245,10 +244,10 @@ public void trainOnInstanceImpl(Instance instance) { } } - if (rulesCoveringInstance == false) { + if (!rulesCoveringInstance) { defaultRule.updateStatistics(instance); if (defaultRule.getInstancesSeen() % this.gracePeriod == 0.0) { - if (defaultRule.tryToExpand(this.splitConfidence, this.tieThreshold) == true) { + if (defaultRule.tryToExpand(this.splitConfidence, this.tieThreshold)) { ActiveRule newDefaultRule = newRule(defaultRule.getRuleNumberID(), (RuleActiveRegressionNode) defaultRule.getLearningNode(), ((RuleActiveRegressionNode) defaultRule.getLearningNode()).getStatisticsOtherBranchSplit()); // other branch @@ -274,7 +273,7 @@ private boolean isAnomaly(Instance instance, ActiveRule rule) { // AMRUles is equipped with anomaly detection. If on, compute the anomaly // value. boolean isAnomaly = false; - if (this.noAnomalyDetection == false) { + if (!this.noAnomalyDetection) { if (rule.getInstancesSeen() >= this.anomalyNumInstThreshold) { isAnomaly = rule.isAnomaly(instance, this.univariateAnomalyprobabilityThreshold, @@ -308,19 +307,19 @@ private ActiveRule newRule(int ID, RuleActiveRegressionNode node, double[] stati } } } - if (statistics != null && ((RuleActiveRegressionNode) r.getLearningNode()).getTargetMean() != null) + if (statistics != null && (r.getLearningNode()).getTargetMean() != null) { double mean; if (statistics[0] > 0) { mean = statistics[1] / statistics[0]; - ((RuleActiveRegressionNode) r.getLearningNode()).getTargetMean().reset(mean, (long) statistics[0]); + (r.getLearningNode()).getTargetMean().reset(mean, (long) statistics[0]); } } return r; } private ActiveRule newRule(int ID) { - ActiveRule r = new ActiveRule.Builder(). + return new ActiveRule.Builder(). threshold(this.pageHinckleyThreshold). alpha(this.pageHinckleyAlpha). changeDetection(this.driftDetection). @@ -329,7 +328,6 @@ private ActiveRule newRule(int ID) { learningRatio(this.learningRatio). numericObserver(numericObserver). id(ID).build(); - return r; } /* @@ -342,7 +340,7 @@ public void onCreate(int id) { this.ruleNumberID = 0; this.defaultRule = newRule(++this.ruleNumberID); - this.ruleSet = new LinkedList(); + this.ruleSet = new LinkedList<>(); } /* diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/common/ActiveRule.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/common/ActiveRule.java index d233af02..871cdd79 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/common/ActiveRule.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/common/ActiveRule.java @@ -38,8 +38,6 @@ public class ActiveRule extends LearningRule { private static final long serialVersionUID = 1L; - private double[] statisticsOtherBranchSplit; - private Builder builder; private RuleActiveRegressionNode learningNode; @@ -89,10 +87,6 @@ public void setLearningNode(RuleRegressionNode learningNode) { this.learningNode = (RuleActiveRegressionNode) learningNode; } - public double[] statisticsOtherBranchSplit() { - return this.statisticsOtherBranchSplit; - } - public RuleSplitNode getLastUpdatedRuleSplitNode() { return this.lastUpdatedRuleSplitNode; } @@ -104,7 +98,6 @@ public static class Builder implements Serializable { private static final long serialVersionUID = 1712887264918475622L; protected boolean changeDetection; - protected boolean usePerceptron; protected double threshold; protected double alpha; protected int predictionFunction; @@ -115,8 +108,6 @@ public static class Builder implements Serializable { protected FIMTDDNumericAttributeClassLimitObserver numericObserver; - protected double lastTargetMean; - public int id; public Builder() { @@ -182,8 +173,7 @@ public ActiveRule build() { */ public boolean tryToExpand(double splitConfidence, double tieThreshold) { - boolean shouldSplit = this.learningNode.tryToExpand(splitConfidence, tieThreshold); - return shouldSplit; + return this.learningNode.tryToExpand(splitConfidence, tieThreshold); } diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRLearnerProcessor.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRLearnerProcessor.java index a7189452..06a319cd 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRLearnerProcessor.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRLearnerProcessor.java @@ -50,10 +50,7 @@ public class AMRLearnerProcessor implements Processor { */ private static final long serialVersionUID = -2302897295090248013L; - private static final Logger logger = - LoggerFactory.getLogger(AMRLearnerProcessor.class); - - private int processorId; + private static final Logger logger = LoggerFactory.getLogger(AMRLearnerProcessor.class); private transient List ruleSet; @@ -105,10 +102,10 @@ private void trainRuleOnInstance(int ruleID, Instance instance) { ActiveRule rule = ruleIterator.next(); if (rule.getRuleNumberID() == ruleID) { // Check (again) for coverage - if (rule.isCovering(instance) == true) { + if (rule.isCovering(instance)) { double error = rule.computeError(instance); // Use adaptive mode error boolean changeDetected = ((RuleActiveRegressionNode) rule.getLearningNode()).updateChangeDetection(error); - if (changeDetected == true) { + if (changeDetected) { ruleIterator.remove(); this.sendRemoveRuleEvent(ruleID); @@ -137,7 +134,7 @@ private boolean isAnomaly(Instance instance, LearningRule rule) { // AMRUles is equipped with anomaly detection. If on, compute the anomaly // value. boolean isAnomaly = false; - if (this.noAnomalyDetection == false) { + if (!this.noAnomalyDetection) { if (rule.getInstancesSeen() >= this.anomalyNumInstThreshold) { isAnomaly = rule.isAnomaly(instance, this.univariateAnomalyprobabilityThreshold, @@ -167,8 +164,7 @@ private boolean addRule(ActiveRule rule) { @Override public void onCreate(int id) { - this.processorId = id; - this.ruleSet = new LinkedList(); + this.ruleSet = new LinkedList<>(); } @Override diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRulesStatisticsProcessor.java b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRulesStatisticsProcessor.java index 86fae3c6..0b6fa80f 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRulesStatisticsProcessor.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/classifiers/rules/distributed/AMRulesStatisticsProcessor.java @@ -49,10 +49,7 @@ public class AMRulesStatisticsProcessor implements Processor { */ private static final long serialVersionUID = 5268933189695395573L; - private static final Logger logger = - LoggerFactory.getLogger(AMRulesStatisticsProcessor.class); - - private int processorId; + private static final Logger logger = LoggerFactory.getLogger(AMRulesStatisticsProcessor.class); private transient List ruleSet; @@ -98,10 +95,10 @@ private void trainRuleOnInstance(int ruleID, Instance instance) { if (rule.getRuleNumberID() == ruleID) { // Check (again) for coverage // Skip anomaly check as Aggregator's perceptron should be well-updated - if (rule.isCovering(instance) == true) { + if (rule.isCovering(instance)) { double error = rule.computeError(instance); // Use adaptive mode error boolean changeDetected = ((RuleActiveRegressionNode) rule.getLearningNode()).updateChangeDetection(error); - if (changeDetected == true) { + if (changeDetected) { ruleIterator.remove(); this.sendRemoveRuleEvent(ruleID); @@ -143,8 +140,7 @@ private boolean addRule(ActiveRule rule) { @Override public void onCreate(int id) { - this.processorId = id; - this.ruleSet = new LinkedList(); + this.ruleSet = new LinkedList<>(); } @Override diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/LocalClustererProcessor.java b/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/LocalClustererProcessor.java index 163184ff..c7bb1ad6 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/LocalClustererProcessor.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/LocalClustererProcessor.java @@ -45,8 +45,7 @@ final public class LocalClustererProcessor implements Processor { * */ private static final long serialVersionUID = -1577910988699148691L; - private static final Logger logger = LoggerFactory - .getLogger(LocalClustererProcessor.class); + private static final Logger logger = LoggerFactory.getLogger(LocalClustererProcessor.class); private LocalClustererAdapter model; private Stream outputStream; private int modelId; @@ -84,7 +83,7 @@ public LocalClustererAdapter getLearner() { * Set the output streams. * * @param outputStream - * the new output stream {@link PredictionCombinerPE}. + * the new output stream {@link Stream}. */ public void setOutputStream(Stream outputStream) { diff --git a/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/SingleLearner.java b/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/SingleLearner.java index f6173f67..e0fa0583 100644 --- a/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/SingleLearner.java +++ b/samoa-api/src/main/java/org/apache/samoa/learners/clusterers/SingleLearner.java @@ -69,7 +69,7 @@ public void init(TopologyBuilder builder, Instances dataset, int parallelism) { protected void setLayout() { learnerP = new LocalClustererProcessor(); - LocalClustererAdapter learner = (LocalClustererAdapter) this.learnerOption.getValue(); + LocalClustererAdapter learner = this.learnerOption.getValue(); learner.setDataset(this.dataset); learnerP.setLearner(learner); @@ -96,7 +96,6 @@ public Processor getInputProcessor() { */ @Override public Set getResultStreams() { - Set streams = ImmutableSet.of(this.resultStream); - return streams; + return ImmutableSet.of(this.resultStream); } } diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/cluster/Miniball.java b/samoa-api/src/main/java/org/apache/samoa/moa/cluster/Miniball.java index da2a0e58..3dcdc5f7 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/cluster/Miniball.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/cluster/Miniball.java @@ -20,7 +20,6 @@ * #L% */ -import com.dreizak.miniball.model.ArrayPointSet; import com.dreizak.miniball.model.PointSet; import java.util.ArrayList; import java.util.List; @@ -62,7 +61,7 @@ public class PointStorage implements PointSet { public PointStorage(int dimension) { this.dimension = dimension; - this.L = new ArrayList(); + this.L = new ArrayList<>(); } public void add(double[] array) { diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/AbstractClusterer.java b/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/AbstractClusterer.java index 02a5191e..da77d045 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/AbstractClusterer.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/AbstractClusterer.java @@ -20,6 +20,7 @@ * #L% */ +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Random; @@ -134,21 +135,19 @@ public void trainOnInstance(Instance inst) { } public Measurement[] getModelMeasurements() { - List measurementList = new LinkedList(); + List measurementList = new LinkedList<>(); measurementList.add(new Measurement("model training instances", trainingWeightSeenByModel())); measurementList.add(new Measurement("model serialized size (bytes)", measureByteSize())); Measurement[] modelMeasurements = getModelMeasurementsImpl(); if (modelMeasurements != null) { - for (Measurement measurement : modelMeasurements) { - measurementList.add(measurement); - } + Collections.addAll(measurementList, modelMeasurements); } // add average of sub-model measurements Clusterer[] subModels = getSubClusterers(); if ((subModels != null) && (subModels.length > 0)) { - List subMeasurements = new LinkedList(); + List subMeasurements = new LinkedList<>(); for (Clusterer subModel : subModels) { if (subModel != null) { subMeasurements.add(subModel.getModelMeasurements()); @@ -157,9 +156,7 @@ public Measurement[] getModelMeasurements() { Measurement[] avgMeasurements = Measurement .averageMeasurements(subMeasurements .toArray(new Measurement[subMeasurements.size()][])); - for (Measurement measurement : avgMeasurements) { - measurementList.add(measurement); - } + Collections.addAll(measurementList, avgMeasurements); } return measurementList.toArray(new Measurement[measurementList.size()]); } @@ -295,5 +292,5 @@ public boolean keepClassLabel() { public Clustering getMicroClusteringResult() { return null; - }; + } } diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/ClusterGenerator.java b/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/ClusterGenerator.java index b9f80b53..06c9cff5 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/ClusterGenerator.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/clusterers/ClusterGenerator.java @@ -58,7 +58,6 @@ public class ClusterGenerator extends AbstractClusterer { public FloatOption clusterAddOption = new FloatOption("clusterAdd", 'A', "Adds additional clusters.", 0, 0, 1); - private static double err_intervall_width = 0.0; private ArrayList points; private int instanceCounter; private int windowCounter; @@ -67,7 +66,7 @@ public class ClusterGenerator extends AbstractClusterer { @Override public void resetLearningImpl() { - points = new ArrayList(); + points = new ArrayList<>(); instanceCounter = 0; windowCounter = 0; random = new Random(227); @@ -169,8 +168,9 @@ private Clustering alterClustering(Clustering scclustering) { double radius = sourceCluster.getRadius(); // move cluster center + double err_interval_width = 0.0; if (errLevelPosition > 0) { - double errOffset = random.nextDouble() * err_intervall_width / 2.0; + double errOffset = random.nextDouble() * err_interval_width / 2.0; double errOffsetDirection = ((random.nextBoolean()) ? 1 : -1); double level = errLevelPosition + errOffsetDirection * errOffset; double[] vector = new double[center.length]; @@ -203,20 +203,15 @@ private Clustering alterClustering(Clustering scclustering) { } center = newCenter; for (int d = 0; d < center.length; d++) { - if (newCenter[d] >= 0 && newCenter[d] <= 1) { - } - else { - System.out - .println("This shouldnt have happend, Cluster center out of bounds:" + Arrays.toString(newCenter)); + if (newCenter[d] < 0 || newCenter[d] > 1) { + System.out.println("This shouldnt have happend, Cluster center out of bounds:" + Arrays.toString(newCenter)); } } - // System.out.println("new Center "+Arrays.toString(newCenter)); - } // alter radius if (errLevelRadiusDecrease > 0 || errLevelRadiusIncrease > 0) { - double errOffset = random.nextDouble() * err_intervall_width / 2.0; + double errOffset = random.nextDouble() * err_interval_width / 2.0; int errOffsetDirection = ((random.nextBoolean()) ? 1 : -1); if (errLevelRadiusDecrease > 0 && (errLevelRadiusIncrease == 0 || random.nextBoolean())) { @@ -289,9 +284,7 @@ private Clustering joinClusters(Clustering clustering) { for (int c1 = 0; c1 < clustering.size(); c1++) { SphereCluster sc1 = (SphereCluster) clustering.get(c1); double minDist = Double.MAX_VALUE; - double minOver = 1; - int maxindexCon = -1; - int maxindexOver = -1; + int maxIndexCon = -1; for (int c2 = 0; c2 < clustering.size(); c2++) { SphereCluster sc2 = (SphereCluster) clustering.get(c2); // double over = sc1.overlapRadiusDegree(sc2); @@ -303,14 +296,11 @@ private Clustering joinClusters(Clustering clustering) { double threshold = Math.min(sc1.getRadius(), sc2.getRadius()) * radiusFactor; if (dist > 0 && dist < minDist && dist < threshold) { minDist = dist; - maxindexCon = c2; + maxIndexCon = c2; } } - int maxindex = -1; - if (maxindexOver != -1) - maxindex = maxindexOver; - else - maxindex = maxindexCon; + int maxindex; + maxindex = maxIndexCon; if (maxindex != -1 && !merged[c1]) { merged[c1] = true; diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningCurve.java b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningCurve.java index 427e01d2..5b11af53 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningCurve.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningCurve.java @@ -38,9 +38,9 @@ public class LearningCurve extends AbstractMOAObject { private static final long serialVersionUID = 1L; - protected List measurementNames = new ArrayList(); + protected List measurementNames = new ArrayList<>(); - protected List measurementValues = new ArrayList(); + protected List measurementValues = new ArrayList<>(); public LearningCurve(String orderingMeasurementName) { this.measurementNames.add(orderingMeasurementName); diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningEvaluation.java b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningEvaluation.java index 993667d5..3389fda0 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningEvaluation.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/LearningEvaluation.java @@ -46,7 +46,7 @@ public LearningEvaluation(Measurement[] measurements) { public LearningEvaluation(Measurement[] evaluationMeasurements, LearningPerformanceEvaluator cpe, Learner model) { - List measurementList = new LinkedList(); + List measurementList = new LinkedList<>(); measurementList.addAll(Arrays.asList(evaluationMeasurements)); measurementList.addAll(Arrays.asList(cpe.getPerformanceMeasurements())); measurementList.addAll(Arrays.asList(model.getModelMeasurements())); diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/MembershipMatrix.java b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/MembershipMatrix.java index 4387c044..3510b4d7 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/MembershipMatrix.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/evaluation/MembershipMatrix.java @@ -35,7 +35,7 @@ public class MembershipMatrix { int total_entries; int class_distribution[]; int total_class_entries; - int initalBuildTimestamp = -1; + int initialBuildTimestamp = -1; public MembershipMatrix(Clustering foundClustering, ArrayList points) { classmap = Clustering.classValues(points); @@ -50,13 +50,13 @@ public MembershipMatrix(Clustering foundClustering, ArrayList points) class_sums = new int[numClasses]; total_entries = 0; total_class_entries = points.size(); - for (int p = 0; p < points.size(); p++) { - int worklabel = classmap.get((int) points.get(p).classValue()); + for (DataPoint point : points) { + int worklabel = classmap.get((int) point.classValue()); // real class distribution class_distribution[worklabel]++; boolean covered = false; for (int c = 0; c < numCluster - 1; c++) { - double prob = foundClustering.get(c).getInclusionProbability(points.get(p)); + double prob = foundClustering.get(c).getInclusionProbability(point); if (prob >= 1) { cluster_class_weights[c][worklabel]++; class_sums[worklabel]++; @@ -74,7 +74,7 @@ public MembershipMatrix(Clustering foundClustering, ArrayList points) } - initalBuildTimestamp = points.get(0).getTimestamp(); + initialBuildTimestamp = points.get(0).getTimestamp(); } public int getClusterClassWeight(int i, int j) { @@ -119,7 +119,7 @@ public boolean hasNoiseClass() { @Override public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append("Membership Matrix\n"); for (int i = 0; i < cluster_class_weights.length; i++) { for (int j = 0; j < cluster_class_weights[i].length; j++) { @@ -128,22 +128,22 @@ public String toString() { sb.append("| " + cluster_sums[i] + "\n"); } // sb.append("-----------\n"); - for (int i = 0; i < class_sums.length; i++) { - sb.append(class_sums[i] + "\t "); + for (int class_sum : class_sums) { + sb.append(class_sum + "\t "); } sb.append("| " + total_entries + "\n"); sb.append("Real class distribution \n"); - for (int i = 0; i < class_distribution.length; i++) { - sb.append(class_distribution[i] + "\t "); + for (int aClass_distribution : class_distribution) { + sb.append(aClass_distribution + "\t "); } sb.append("| " + total_class_entries + "\n"); return sb.toString(); } - public int getInitalBuildTimestamp() { - return initalBuildTimestamp; + public int getInitialBuildTimestamp() { + return initialBuildTimestamp; } } diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/streams/clustering/RandomRBFGeneratorEvents.java b/samoa-api/src/main/java/org/apache/samoa/moa/streams/clustering/RandomRBFGeneratorEvents.java index c83f688d..c5f2a670 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/streams/clustering/RandomRBFGeneratorEvents.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/streams/clustering/RandomRBFGeneratorEvents.java @@ -37,7 +37,6 @@ import org.apache.samoa.moa.cluster.SphereCluster; import org.apache.samoa.moa.core.AutoExpandVector; import org.apache.samoa.moa.core.DataPoint; -import org.apache.samoa.moa.core.FastVector; import org.apache.samoa.moa.core.InstanceExample; import org.apache.samoa.moa.core.ObjectRepository; import org.apache.samoa.moa.streams.InstanceStream; @@ -48,7 +47,7 @@ import com.github.javacliparser.IntOption; public class RandomRBFGeneratorEvents extends ClusteringStream { - private transient Vector listeners; + private transient Vector listeners; private static final long serialVersionUID = 1L; @@ -135,10 +134,10 @@ private class GeneratorCluster implements Serializable { int currentMovementSteps; boolean isSplitting = false; - LinkedList points = new LinkedList(); - ArrayList microClusters = new ArrayList(); - ArrayList> microClustersPoints = new ArrayList(); - ArrayList microClustersDecay = new ArrayList(); + LinkedList points = new LinkedList<>(); + ArrayList microClusters = new ArrayList<>(); + ArrayList> microClustersPoints = new ArrayList<>(); + ArrayList microClustersDecay = new ArrayList<>(); public GeneratorCluster(int label) { boolean outofbounds = true; @@ -230,8 +229,7 @@ private void addInstance(Instance instance) { int minMicroIndex = -1; double minHullDist = Double.MAX_VALUE; boolean inserted = false; - // we favour more recently build clusters so we can remove earlier cluster - // sooner + // we favour more recently build clusters so we can remove earlier cluster sooner for (int m = microClusters.size() - 1; m >= 0; m--) { SphereCluster micro = microClusters.get(m); double hulldist = micro.getCenterDistance(point) - micro.getRadius(); @@ -251,49 +249,25 @@ private void addInstance(Instance instance) { } } // Reseting index choice for alternative cluster building - int alt = 1; - if (alt == 1) - minMicroIndex = -1; if (!inserted) { // add to closest cluster and expand cluster - if (minMicroIndex != -1) { - microClustersPoints.get(minMicroIndex).add(point); - // we should keep the miniball instances and just check in - // new points instead of rebuilding the whole thing - SphereCluster s = new SphereCluster(microClustersPoints.get(minMicroIndex), numAttsOption.getValue()); - // check if current microcluster is bigger then generating cluster - if (s.getRadius() > generator.getRadius()) { - // remove previously added point - microClustersPoints.get(minMicroIndex).remove(microClustersPoints.get(minMicroIndex).size() - 1); - minMicroIndex = -1; - } - else { - microClusters.set(minMicroIndex, s); - microClustersDecay.set(minMicroIndex, numGeneratedInstances); - } - } // minMicroIndex might have been reset above // create new micro cluster - if (minMicroIndex == -1) { - ArrayList microPoints = new ArrayList(); - microPoints.add(point); - SphereCluster s; - if (alt == 0) - s = new SphereCluster(microPoints, numAttsOption.getValue()); - else - s = new SphereCluster(generator.getCenter(), generator.getRadius(), 1); - - microClusters.add(s); - microClustersPoints.add(microPoints); - microClustersDecay.add(numGeneratedInstances); - int id = 0; - while (id < kernels.size()) { - if (kernels.get(id) == this) - break; - id++; - } - s.setGroundTruth(id); + ArrayList microPoints = new ArrayList<>(); + microPoints.add(point); + SphereCluster s; + s = new SphereCluster(generator.getCenter(), generator.getRadius(), 1); + + microClusters.add(s); + microClustersPoints.add(microPoints); + microClustersDecay.add(numGeneratedInstances); + int id = 0; + while (id < kernels.size()) { + if (kernels.get(id) == this) + break; + id++; } + s.setGroundTruth(id); } } @@ -301,10 +275,7 @@ private void addInstance(Instance instance) { private void move() { if (currentMovementSteps < totalMovementSteps) { currentMovementSteps++; - if (moveVector == null) { - return; - } - else { + if (moveVector != null) { double[] center = generator.getCenter(); boolean outofbounds = true; while (outofbounds) { @@ -426,24 +397,17 @@ private String splitKernel() { double radius = kernelRadiiOption.getValue(); double avgWeight = 1.0 / numClusterOption.getValue(); double weight = avgWeight + avgWeight * densityRangeOption.getValue() * instanceRandom.nextDouble(); - SphereCluster spcluster = null; + SphereCluster spcluster; double[] center = generator.getCenter(); spcluster = new SphereCluster(center, radius, weight); - if (spcluster != null) { - GeneratorCluster gc = new GeneratorCluster(clusterIdCounter++, spcluster); - gc.isSplitting = true; - kernels.add(gc); - normalizeWeights(); - numActiveKernels++; - return "Split from Kernel " + generator.getId(); - } - else { - System.out.println("Tried to split new kernel from C" + generator.getId() + - ". Not enough room for new cluster, decrease average radii, number of clusters or enable overlap."); - return ""; - } + GeneratorCluster gc = new GeneratorCluster(clusterIdCounter++, spcluster); + gc.isSplitting = true; + kernels.add(gc); + normalizeWeights(); + numActiveKernels++; + return "Split from Kernel " + generator.getId(); } private String fadeOut() { @@ -493,18 +457,18 @@ public void restart() { numGeneratedInstances = 0; clusterIdCounter = 0; mergeClusterA = mergeClusterB = null; - kernels = new AutoExpandVector(); + kernels = new AutoExpandVector<>(); initKernels(); } protected void generateHeader() { // 2013/06/02: Noise label - ArrayList attributes = new ArrayList(); + ArrayList attributes = new ArrayList<>(); for (int i = 0; i < this.numAttsOption.getValue(); i++) { attributes.add(new Attribute("att" + (i + 1))); } - ArrayList classLabels = new ArrayList(); + ArrayList classLabels = new ArrayList<>(); for (int i = 0; i < this.numClusterOption.getValue(); i++) { classLabels.add("class" + (i + 1)); } @@ -531,7 +495,7 @@ public InstanceExample nextInstance() { // make room for the classlabel double[] values_new = new double[numAttsOption.getValue()]; // +1 - double[] values = null; + double[] values; int clusterChoice = -1; if (instanceRandom.nextDouble() > noiseLevelOption.getValue()) { @@ -539,7 +503,7 @@ public InstanceExample nextInstance() { values = kernels.get(clusterChoice).generator.sample(instanceRandom).toDoubleArray(); } else { - // get ranodm noise point + // get random noise point values = getNoisePoint(); } @@ -569,8 +533,8 @@ public InstanceExample nextInstance() { public Clustering getGeneratingClusters() { Clustering clustering = new Clustering(); - for (int c = 0; c < kernels.size(); c++) { - clustering.add(kernels.get(c).generator); + for (GeneratorCluster kernel : kernels) { + clustering.add(kernel.generator); } return clustering; } @@ -579,11 +543,11 @@ public Clustering getMicroClustering() { Clustering clustering = new Clustering(); int id = 0; - for (int c = 0; c < kernels.size(); c++) { - for (int m = 0; m < kernels.get(c).microClusters.size(); m++) { - kernels.get(c).microClusters.get(m).setId(id); - kernels.get(c).microClusters.get(m).setGroundTruth(kernels.get(c).generator.getId()); - clustering.add(kernels.get(c).microClusters.get(m)); + for (GeneratorCluster kernel : kernels) { + for (int m = 0; m < kernel.microClusters.size(); m++) { + kernel.microClusters.get(m).setId(id); + kernel.microClusters.get(m).setGroundTruth(kernel.generator.getId()); + clustering.add(kernel.microClusters.get(m)); id++; } } @@ -595,8 +559,8 @@ public Clustering getMicroClustering() { /**************************** EVENTS ******************************************/ private void eventScheduler() { - for (int i = 0; i < kernels.size(); i++) { - kernels.get(i).updateKernel(); + for (GeneratorCluster kernel : kernels) { + kernel.updateKernel(); } nextEventCounter--; @@ -604,8 +568,8 @@ private void eventScheduler() { // should this be randomized as well??? if (nextEventCounter % kernelMovePointFrequency == 0) { // move kernels - for (int i = 0; i < kernels.size(); i++) { - kernels.get(i).move(); + for (GeneratorCluster kernel : kernels) { + kernel.move(); // overlapControl(); } } @@ -715,8 +679,7 @@ private String fadeOut() { while (kernels.get(id).kill != -1) id = instanceRandom.nextInt(kernels.size()); - String message = kernels.get(id).fadeOut(); - return message; + return kernels.get(id).fadeOut(); } private String fadeIn() { @@ -782,9 +745,7 @@ private String splitKernel() { while (kernels.get(id).kill != -1) id = instanceRandom.nextInt(kernels.size()); - String message = kernels.get(id).splitKernel(); - - return message; + return kernels.get(id).splitKernel(); } private String mergeKernels(int steps) { @@ -796,10 +757,7 @@ private String mergeKernels(int steps) { // System.out.println("DisredDist:"+(2*diseredDist)); for (int i = 0; i < kernels.size(); i++) { for (int j = 0; j < i; j++) { - if (kernels.get(i).kill != -1 || kernels.get(j).kill != -1) { - continue; - } - else { + if (kernels.get(i).kill == -1 && kernels.get(j).kill == -1) { double kernelDist = kernels.get(i).generator.getCenterDistance(kernels.get(j).generator); double d = kernelDist - 2 * diseredDist; // System.out.println("Dist:"+i+" / "+j+" "+d); @@ -866,10 +824,10 @@ private double[] getNoisePoint() { incluster = false; if (!noiseInClusterOption.isSet() && counter > 0) { counter--; - for (int c = 0; c < kernels.size(); c++) { - for (int m = 0; m < kernels.get(c).microClusters.size(); m++) { + for (GeneratorCluster kernel : kernels) { + for (int m = 0; m < kernel.microClusters.size(); m++) { Instance inst = new DenseInstance(1, sample); - if (kernels.get(c).microClusters.get(m).getInclusionProbability(inst) > 0) { + if (kernel.microClusters.get(m).getInclusionProbability(inst) > 0) { incluster = true; break; } @@ -904,11 +862,11 @@ private int chooseWeightedElement() { private void normalizeWeights() { double sumWeights = 0.0; - for (int i = 0; i < kernels.size(); i++) { - sumWeights += kernels.get(i).generator.getWeight(); + for (GeneratorCluster kernel : kernels) { + sumWeights += kernel.generator.getWeight(); } - for (int i = 0; i < kernels.size(); i++) { - kernels.get(i).generator.setWeight(kernels.get(i).generator.getWeight() / sumWeights); + for (GeneratorCluster kernel : kernels) { + kernel.generator.setWeight(kernel.generator.getWeight() / sumWeights); } } @@ -919,14 +877,14 @@ private void normalizeWeights() { /** Add a listener */ synchronized public void addClusterChangeListener(ClusterEventListener l) { if (listeners == null) - listeners = new Vector(); + listeners = new Vector<>(); listeners.addElement(l); } /** Remove a listener */ synchronized public void removeClusterChangeListener(ClusterEventListener l) { if (listeners == null) - listeners = new Vector(); + listeners = new Vector<>(); listeners.removeElement(l); } @@ -940,9 +898,9 @@ protected void fireClusterChange(long timestamp, String type, String message) { // make a copy of the listener list in case // anyone adds/removes listeners - Vector targets; + Vector targets; synchronized (this) { - targets = (Vector) listeners.clone(); + targets = (Vector) listeners.clone(); } // walk through the listener list and diff --git a/samoa-api/src/main/java/org/apache/samoa/moa/streams/generators/HyperplaneGenerator.java b/samoa-api/src/main/java/org/apache/samoa/moa/streams/generators/HyperplaneGenerator.java index b05fa1b1..50b9acdd 100644 --- a/samoa-api/src/main/java/org/apache/samoa/moa/streams/generators/HyperplaneGenerator.java +++ b/samoa-api/src/main/java/org/apache/samoa/moa/streams/generators/HyperplaneGenerator.java @@ -83,8 +83,6 @@ public String getPurposeString() { protected int[] sigma; - public int numberInstance; - @Override protected void prepareForUseImpl(TaskMonitor monitor, ObjectRepository repository) { monitor.setCurrentActivity("Preparing hyperplane...", -1.0); @@ -160,7 +158,7 @@ public Example nextInstance() { private void addDrift() { for (int i = 0; i < this.numDriftAttsOption.getValue(); i++) { - this.weights[i] += (double) ((double) sigma[i]) * ((double) this.magChangeOption.getValue()); + this.weights[i] += ((double) sigma[i]) * this.magChangeOption.getValue(); if (// this.weights[i] >= 1.0 || this.weights[i] <= 0.0 || (1 + (this.instanceRandom.nextInt(100))) <= this.sigmaPercentageOption.getValue()) { this.sigma[i] *= -1; diff --git a/samoa-api/src/main/java/org/apache/samoa/streams/fs/HDFSFileStreamSource.java b/samoa-api/src/main/java/org/apache/samoa/streams/fs/HDFSFileStreamSource.java index 00abd1a6..bb678998 100644 --- a/samoa-api/src/main/java/org/apache/samoa/streams/fs/HDFSFileStreamSource.java +++ b/samoa-api/src/main/java/org/apache/samoa/streams/fs/HDFSFileStreamSource.java @@ -59,24 +59,24 @@ public void init(String path, String ext) { public void init(Configuration config, String path, String ext) { this.config = config; - this.filePaths = new ArrayList(); + this.filePaths = new ArrayList<>(); Path hdfsPath = new Path(path); FileSystem fs; try { fs = FileSystem.get(config); FileStatus fileStat = fs.getFileStatus(hdfsPath); if (fileStat.isDirectory()) { - Path filterPath = hdfsPath; + Path filterPath; if (ext != null) { - filterPath = new Path(path.toString(), "*." + ext); + filterPath = new Path(path, "*." + ext); } else { - filterPath = new Path(path.toString(), "*"); + filterPath = new Path(path, "*"); } FileStatus[] filesInDir = fs.globStatus(filterPath); - for (int i = 0; i < filesInDir.length; i++) { - if (filesInDir[i].isFile()) { - filePaths.add(filesInDir[i].getPath().toString()); + for (FileStatus aFilesInDir : filesInDir) { + if (aFilesInDir.isFile()) { + filePaths.add(aFilesInDir.getPath().toString()); } } } diff --git a/samoa-api/src/main/java/org/apache/samoa/streams/fs/LocalFileStreamSource.java b/samoa-api/src/main/java/org/apache/samoa/streams/fs/LocalFileStreamSource.java index 129d2470..addd2320 100644 --- a/samoa-api/src/main/java/org/apache/samoa/streams/fs/LocalFileStreamSource.java +++ b/samoa-api/src/main/java/org/apache/samoa/streams/fs/LocalFileStreamSource.java @@ -25,7 +25,6 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; -import java.nio.file.FileSystems; import java.util.ArrayList; import java.util.List; @@ -49,12 +48,12 @@ public LocalFileStreamSource() { } public void init(String path, String ext) { - this.filePaths = new ArrayList(); + this.filePaths = new ArrayList<>(); File fileAtPath = new File(path); if (fileAtPath.isDirectory()) { File[] filesInDir = fileAtPath.listFiles(new FileExtensionFilter(ext)); - for (int i = 0; i < filesInDir.length; i++) { - filePaths.add(filesInDir[i].getAbsolutePath()); + for (File aFilesInDir : filesInDir) { + filePaths.add(aFilesInDir.getAbsolutePath()); } } else { diff --git a/samoa-local/src/test/java/org/apache/samoa/AlgosTest.java b/samoa-local/src/test/java/org/apache/samoa/AlgosTest.java index 1f7be7ed..7828da04 100644 --- a/samoa-local/src/test/java/org/apache/samoa/AlgosTest.java +++ b/samoa-local/src/test/java/org/apache/samoa/AlgosTest.java @@ -20,7 +20,6 @@ * #L% */ -import org.apache.samoa.LocalDoTask; import org.junit.Test; public class AlgosTest {