package stat; import java.awt.*; import java.io.*; import parameters.Parameter; import parameters.ParameterSet; import parameters.ParameterSetArray; /** Makes a histogram of the values of one of the Value Fields across all the parameter sets. This is not currently all that useful, since you have to set it up from within the code, but in the code you can set the number of bins, start bin, bin width, whether there is a bin that catches values not falling in the other bins, and the value field to make the histogram out of. */ public class ValueFieldHistogramStat extends Stat implements Runnable { int numBins = 20; boolean catchAllBins = false; float startValue = 0f, incrValue = 1f; int [] histo; String valueField = "Score"; public ValueFieldHistogramStat() { } public void calculateStat() { int i; if(paramSets == null) { System.out.println("No Cam loaded - ValueFieldHistogram did nothing"); return; } FileDialog fd = new FileDialog(new Frame(), "Save into file:", FileDialog.SAVE); fd.show(); if(fd.getFile() == null) return; try { PrintWriter ps = new PrintWriter(new FileOutputStream(fd.getDirectory() + fd.getFile())); prototype = paramSets.getPrototype(); if(prototype == null) prototype = paramSets.goToBeginning(); if(prototype == null) { ps.close(); return; } // Nothing in cam file if(catchAllBins) histo = new int[numBins + 2]; else histo = new int[numBins]; int pos = prototype.getFieldPos(valueField); ps.println("Histogram of " + valueField + " values for file " + fd.getFile()); // First save titles for each bin if(catchAllBins) ps.print("<" + startValue + "\t"); for(i = 0; i < numBins; i++) { ps.print((startValue + i * incrValue) + "-" + (startValue + (i+1) * incrValue) + "\t"); } if(catchAllBins) ps.print(">" + (startValue + i * incrValue)); ps.println(""); // Next go through all the param sets, and accumulate values ParameterSet set = paramSets.goToBeginning(); while(set != null) { float val = set.getFieldValue(pos); int bin = (int)((val - startValue) / incrValue); if(catchAllBins) { if(bin < 0) histo[0]++; else if(bin > numBins) histo[numBins]++; else histo[bin+1]++; } else { if(bin >= 0 && bin < numBins) histo[bin]++; } set = paramSets.getNextSet(); } if(catchAllBins) { for(i = 0; i < numBins + 2; i++) { ps.print(histo[i] + "\t"); } } else { for(i = 0; i < numBins; i++) { ps.print(histo[i] + "\t"); } } ps.println(); ps.close(); System.out.println("Saved histogram of " + valueField); } catch(Exception e) { System.out.println("Error in ValueFieldHistogram: " + e.toString()); } } }