This commit is contained in:
Martin Pernollet 2015-04-19 20:54:10 +02:00
parent 11d7c14ce8
commit 42564abfe7
4 changed files with 86 additions and 118 deletions

View File

@ -1,56 +1,89 @@
package org.jzy3d.maths; package org.jzy3d.maths;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.SortedSet; import java.util.Map.Entry;
import java.util.TreeSet;
public class Histogram<X,V> { public class Histogram {
Map<X,V> data; protected Range[] ranges ;
protected Map<Range,Integer> data;
public Histogram(){ public Histogram(float min, float max, int bins){
data = new HashMap<X,V>(); initBins(min, max, bins);
} }
public static void main(String[] args) { private void initBins(float min, float max, int bins) {
Histogram<Integer, Integer> h = new Histogram<Integer, Integer>(); data = new HashMap<Range,Integer>(bins);
h.put(20, 3); ranges = new Range[bins];
h.put(30, 5);
h.put(40, 15);
h.put(50, 4);
h.console();
float min = 0;
float max = 1;
int bins = 20;
Histogram<Range, Float> h2 = new Histogram<Range, Float>(); float step = (max-min)/bins;
float rmin = min;
for (int i = 0; i < bins - 1; i++) {
ranges[i] = new Range(rmin, rmin + step);
data.put(ranges[i], 0);
rmin = rmin + step;
}
ranges[bins-1] = new Range(rmin, max);
data.put(ranges[bins-1], 0);
} }
public void put(X x,V value){ public void add(List<Float> values){
data.put(x, value); for(float v : values){
add(v);
}
} }
private void console(){ public void add(float value){
SortedSet<X> xs = new TreeSet<X>(data.keySet()); for(Entry<Range,Integer> e: data.entrySet()){
for(X x : xs){ Range r = e.getKey();
V value = data.get(x); if(r.isIn(value, true, true)){
consoleKeyValue(x, value); e.setValue(e.getValue()+1);
return;
}
}
illegalValueException(value);
}
private void illegalValueException(float value) {
StringBuilder sb = new StringBuilder();
String m = "value could not be added to any pre-configured bin. "
+ "Are you adding a value out of the min-max range you used to build "
+ Histogram.class.getSimpleName() + "?";
sb.append(m + "\n");
sb.append("min:" + ranges[0].getMin() + "\n");
sb.append("max:" + ranges[ranges.length-1].getMax() + "\n");
sb.append("value:" + value + "\n");
throw new IllegalArgumentException(sb.toString());
}
public Range[] ranges(){
return ranges;
}
public int getCount(int bin) {
return data.get(ranges[bin]);
}
public void setCount(int bin, int value) {
data.put(ranges[bin], value);
}
public void console() {
for (int i = 0; i < ranges.length; i++) {
System.out.println(ranges[i] + " : " + data.get(ranges[0]));
} }
} }
private void consoleKeyValue(X x, V value) {
consoleKey(x); public int computeMaxCount() {
consoleValue(value); int max = Integer.MIN_VALUE;
System.out.println(); for(Entry<Range,Integer> e : data.entrySet()){
int v = e.getValue();
if(v>max)
max = v;
}
return max;
} }
}
private void consoleKey(X key) {
System.out.print(key + " : ");
}
private void consoleValue(V value) {
System.out.print(value);
}
}

View File

@ -1,70 +0,0 @@
package org.jzy3d.maths;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class HistogramRange {
protected Range[] ranges ;
protected Map<Range,Integer> data;
public HistogramRange(float min, float max, int bins){
initBins(min, max, bins);
}
private void initBins(float min, float max, int bins) {
data = new HashMap<Range,Integer>(bins);
ranges = new Range[bins];
float step = (max-min)/bins;
float rmin = min;
for (int i = 0; i < bins - 1; i++) {
ranges[i] = new Range(rmin, rmin + step);
data.put(ranges[i], 0);
rmin = rmin + step;
}
ranges[bins-1] = new Range(rmin, max);
data.put(ranges[bins-1], 0);
}
public void add(float value){
for(Entry<Range,Integer> e: data.entrySet()){
Range r = e.getKey();
if(r.isIn(value)){
e.setValue(e.getValue()+1);
return;
}
}
throw new IllegalArgumentException("value could not be added to any pre-configured bin. Are you adding a value out of the min-max range you used to build " + HistogramRange.class.getSimpleName() + "?");
}
public Range[] ranges(){
return ranges;
}
public int getCount(int bin) {
return data.get(ranges[bin]);
}
public void setCount(int bin, int value) {
data.put(ranges[bin], value);
}
public void console() {
for (int i = 0; i < ranges.length; i++) {
System.out.println(ranges[i] + " : " + data.get(ranges[0]));
}
}
public int computeMaxCount() {
int max = Integer.MIN_VALUE;
for(Entry<Range,Integer> e : data.entrySet()){
int v = e.getValue();
if(v>max)
max = v;
}
return max;
}
}

View File

@ -52,13 +52,18 @@ public class Scale {
} }
public boolean isIn(float value){ public boolean isIn(float value){
if(value<=min) return isIn(value, false, true);
}
public boolean isIn(float value, boolean includingMin, boolean includingMax){
if(includingMin?value<min:value<=min)
return false; return false;
if(value>max) if(includingMax?value>max:value>=max)
return false; return false;
return true; return true;
} }
public boolean valid() { public boolean valid() {
if (min <= max) if (min <= max)
return true; return true;

View File

@ -3,7 +3,7 @@ package org.jzy3d.plot2d.primitives;
import org.jzy3d.chart.Chart; import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color; import org.jzy3d.colors.Color;
import org.jzy3d.maths.Coord3d; import org.jzy3d.maths.Coord3d;
import org.jzy3d.maths.HistogramRange; import org.jzy3d.maths.Histogram;
import org.jzy3d.maths.Range; import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.primitives.AbstractComposite; import org.jzy3d.plot3d.primitives.AbstractComposite;
import org.jzy3d.plot3d.primitives.Point; import org.jzy3d.plot3d.primitives.Point;
@ -12,10 +12,10 @@ import org.jzy3d.plot3d.primitives.axes.layout.IAxeLayout;
import org.jzy3d.plot3d.primitives.axes.layout.providers.StaticTickProvider; import org.jzy3d.plot3d.primitives.axes.layout.providers.StaticTickProvider;
public class Histogram2d { public class Histogram2d {
protected HistogramRange model; protected Histogram model;
protected AbstractComposite drawable; protected AbstractComposite drawable;
public Histogram2d(HistogramRange model) { public Histogram2d(Histogram model) {
setModel(model); setModel(model);
} }
@ -34,12 +34,12 @@ public class Histogram2d {
layout(chart); layout(chart);
} }
public void setModel(HistogramRange model) { public void setModel(Histogram model) {
this.model = model; this.model = model;
this.drawable = buildDrawable(model); this.drawable = buildDrawable(model);
} }
public HistogramRange getModel() { public Histogram getModel() {
return model; return model;
} }
@ -47,7 +47,7 @@ public class Histogram2d {
return drawable; return drawable;
} }
protected AbstractComposite buildDrawable(HistogramRange model){ protected AbstractComposite buildDrawable(Histogram model){
AbstractComposite c = new AbstractComposite() { AbstractComposite c = new AbstractComposite() {
}; };
float z= 0; float z= 0;