MOA 12.03
Real Time Analytics for Data Streams
MeasureCollection.java
Go to the documentation of this file.
00001 /*
00002  *    MeasureCollection.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen (moa@cs.rwth-aachen.de)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.evaluation;
00022 
00023 import java.util.ArrayList;
00024 import java.util.HashMap;
00025 import moa.AbstractMOAObject;
00026 import moa.cluster.Clustering;
00027 import moa.gui.visualization.DataPoint;
00028 
00029 public abstract class MeasureCollection extends AbstractMOAObject{
00030     private String[] names;
00031     private ArrayList<Double>[] values;
00032     private ArrayList<Double>[] sortedValues;
00033     private ArrayList<String> events;
00034     
00035     private double[] minValue;
00036     private double[] maxValue;
00037     private double[] sumValues;
00038     private boolean[] enabled;
00039     private boolean[] corrupted;
00040     private double time;
00041     private boolean debug = true;
00042     private MembershipMatrix mm = null;
00043 
00044     private HashMap<String, Integer> map;
00045 
00046     private int numMeasures = 0;
00047     
00048     
00049 
00050 
00051      public MeasureCollection() {
00052         names = getNames();
00053         numMeasures = names.length;
00054         map = new HashMap<String, Integer>(numMeasures);        
00055         for (int i = 0; i < names.length; i++) {
00056              map.put(names[i],i);
00057         }
00058         values = (ArrayList<Double>[]) new ArrayList[numMeasures];
00059         sortedValues = (ArrayList<Double>[]) new ArrayList[numMeasures];
00060         maxValue = new double[numMeasures];
00061         minValue = new double[numMeasures];
00062         sumValues = new double[numMeasures];
00063         corrupted = new boolean[numMeasures];
00064         enabled = getDefaultEnabled();
00065         time = 0;
00066         events = new ArrayList<String>();
00067 
00068         for (int i = 0; i < numMeasures; i++) {
00069                 values[i] = new ArrayList<Double>();
00070                 sortedValues[i] = new ArrayList<Double>();
00071                 maxValue[i] = Double.MIN_VALUE;
00072                 minValue[i] = Double.MAX_VALUE;
00073                 corrupted[i] = false;
00074                 sumValues[i] = 0.0;
00075         }
00076 
00077     }
00078 
00079     protected abstract String[] getNames();
00080 
00081      public void addValue(int index, double value){
00082          if(Double.isNaN(value)){
00083                  if(debug)
00084                          System.out.println("NaN for "+names[index]);
00085              corrupted[index] = true;
00086          }
00087          if(value < 0){
00088                  if(debug)
00089                          System.out.println("Negative value for "+names[index]);
00090          }
00091 
00092          values[index].add(value);
00093          sumValues[index]+=value;
00094          if(value < minValue[index]) minValue[index] = value;
00095          if(value > maxValue[index]) maxValue[index] = value;
00096      }
00097 
00098      protected void addValue(String name, double value){
00099         if(map.containsKey(name)){
00100             addValue(map.get(name),value);
00101         }
00102         else{
00103             System.out.println(name+" is not a valid measure key, no value added");
00104         }
00105      }
00106      
00107      //add an empty entry e.g. if evaluation crashed internally
00108      public void addEmptyValue(int index){
00109          values[index].add(Double.NaN);
00110          corrupted[index] = true;
00111      }
00112 
00113      public int getNumMeasures(){
00114          return numMeasures;
00115      }
00116 
00117      public String getName(int index){
00118         return names[index];
00119      }
00120 
00121      public double getMaxValue(int index){
00122          return maxValue[index];
00123      }
00124 
00125      public double getMinValue(int index){
00126          return minValue[index];
00127      }
00128 
00129      public double getLastValue(int index){
00130          if(values[index].size()<1) return Double.NaN;
00131          return values[index].get(values[index].size()-1);
00132      }
00133 
00134      public double getMean(int index){
00135          if(corrupted[index] || values[index].size()<1)
00136              return Double.NaN;
00137 
00138          return sumValues[index]/values[index].size();
00139      }
00140 
00141      private void updateSortedValues(int index){
00142          //naive implementation of insertion sort
00143          for (int i = sortedValues[index].size(); i < values[index].size(); i++) {
00144              double v = values[index].get(i);
00145              int insertIndex = 0;
00146              while(!sortedValues[index].isEmpty() && insertIndex < sortedValues[index].size() && v > sortedValues[index].get(insertIndex))
00147                  insertIndex++;
00148              sortedValues[index].add(insertIndex,v);
00149          }
00150 //         for (int i = 0; i < sortedValues[index].size(); i++) {
00151 //             System.out.print(sortedValues[index].get(i)+" ");
00152 //         }
00153 //         System.out.println();
00154      }
00155 
00156      public void clean(int index){
00157          sortedValues[index].clear();
00158      }
00159 
00160      public double getMedian(int index){
00161          updateSortedValues(index);
00162          int size = sortedValues[index].size();
00163 
00164          if(size > 0){
00165              if(size%2 == 1)
00166                  return sortedValues[index].get((int)(size/2));
00167              else
00168                  return (sortedValues[index].get((size-1)/2)+sortedValues[index].get((size-1)/2+1))/2.0;
00169          }
00170          return Double.NaN;
00171     }
00172 
00173      public double getLowerQuartile(int index){
00174          updateSortedValues(index);
00175          int size = sortedValues[index].size();
00176          if(size > 11){
00177              return sortedValues[index].get(Math.round(size*0.25f));
00178          }
00179          return Double.NaN;
00180      }
00181 
00182      public double getUpperQuartile(int index){
00183          updateSortedValues(index);
00184          int size = sortedValues[index].size();
00185          if(size > 11){
00186              return sortedValues[index].get(Math.round(size*0.75f-1));
00187          }
00188          return Double.NaN;
00189      }
00190 
00191 
00192      public int getNumberOfValues(int index){
00193          return values[index].size();
00194      }
00195 
00196      public double getValue(int index, int i){
00197          if(i>=values[index].size()) return Double.NaN;
00198          return values[index].get(i);
00199      }
00200 
00201      public ArrayList<Double> getAllValues(int index){
00202          return values[index];
00203      }
00204 
00205      public void setEnabled(int index, boolean value){
00206          enabled[index] = value;
00207      }
00208 
00209      public boolean isEnabled(int index){
00210          return enabled[index];
00211      }
00212 
00213      public double getMeanRunningTime(){
00214          if(values[0].size()!=0)
00215             return (time/10e5/values[0].size());
00216          else
00217              return 0;
00218      }
00219 
00220      protected boolean[] getDefaultEnabled(){
00221          boolean[] defaults = new boolean[numMeasures];
00222          for (int i = 0; i < defaults.length; i++) {
00223              defaults[i] = true;
00224          }
00225          return defaults;
00226      }
00227 
00228      protected abstract void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) throws Exception;
00229 
00230      /*
00231       * Evaluate Clustering
00232       *
00233       * return Time in milliseconds
00234       */
00235      public double evaluateClusteringPerformance(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) throws Exception{
00236         long start = System.nanoTime();
00237         evaluateClustering(clustering, trueClustering, points);
00238         long duration = System.nanoTime()-start;
00239         time+=duration;
00240         duration/=10e5;
00241         return duration;
00242      }
00243 
00244      public void getDescription(StringBuilder sb, int indent) {
00245 
00246     }
00247 
00248      public void addEventType(String type){
00249          events.add(type);
00250      }
00251      public String getEventType(int index){
00252          if(index < events.size())
00253                  return events.get(index);
00254          else
00255                  return null;
00256      }
00257 }
00258 
 All Classes Namespaces Files Functions Variables Enumerations