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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class Histogram<X,V> {
Map<X,V> data;
import java.util.Map.Entry;
public Histogram(){
data = new HashMap<X,V>();
public class Histogram {
protected Range[] ranges ;
protected Map<Range,Integer> data;
public Histogram(float min, float max, int bins){
initBins(min, max, bins);
}
public static void main(String[] args) {
Histogram<Integer, Integer> h = new Histogram<Integer, Integer>();
h.put(20, 3);
h.put(30, 5);
h.put(40, 15);
h.put(50, 4);
h.console();
private void initBins(float min, float max, int bins) {
data = new HashMap<Range,Integer>(bins);
ranges = new Range[bins];
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){
data.put(x, value);
}
private void console(){
SortedSet<X> xs = new TreeSet<X>(data.keySet());
for(X x : xs){
V value = data.get(x);
consoleKeyValue(x, value);
public void add(List<Float> values){
for(float v : values){
add(v);
}
}
private void consoleKeyValue(X x, V value) {
consoleKey(x);
consoleValue(value);
System.out.println();
public void add(float value){
for(Entry<Range,Integer> e: data.entrySet()){
Range r = e.getKey();
if(r.isIn(value, true, true)){
e.setValue(e.getValue()+1);
return;
}
}
illegalValueException(value);
}
private void consoleKey(X key) {
System.out.print(key + " : ");
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());
}
private void consoleValue(V value) {
System.out.print(value);
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

@ -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){
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;
if(value>max)
if(includingMax?value>max:value>=max)
return false;
return true;
}
public boolean valid() {
if (min <= max)
return true;

View File

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