Merge pull request #22 from metamx/histograms

HistogramVisual: add support for quantiles
This commit is contained in:
cheddar 2012-11-06 20:20:34 -08:00
commit 9c3b70068e
3 changed files with 15 additions and 15 deletions

View File

@ -132,10 +132,15 @@ public class Histogram
return buf.array(); return buf.array();
} }
/**
* Returns a visual representation of a histogram object.
* Initially returns an array of just the min. and max. values
* but can also support the addition of quantiles.
*/
public HistogramVisual asVisual() { public HistogramVisual asVisual() {
float[] visualCounts = new float[bins.length - 2]; float[] visualCounts = new float[bins.length - 2];
for(int i = 0; i < visualCounts.length; ++i) visualCounts[i] = (float)bins[i + 1]; for(int i = 0; i < visualCounts.length; ++i) visualCounts[i] = (float)bins[i + 1];
return new HistogramVisual(breaks, visualCounts, min, max); return new HistogramVisual(breaks, visualCounts, new float[]{min, max});
} }
public static Histogram fromBytes(byte[] bytes) { public static Histogram fromBytes(byte[] bytes) {

View File

@ -29,15 +29,14 @@ public class HistogramVisual
{ {
@JsonProperty final public double[] breaks; @JsonProperty final public double[] breaks;
@JsonProperty final public double[] counts; @JsonProperty final public double[] counts;
@JsonProperty final double min; // an array of the quantiles including the min. and max.
@JsonProperty final double max; @JsonProperty final public double[] quantiles;
@JsonCreator @JsonCreator
public HistogramVisual( public HistogramVisual(
@JsonProperty double[] breaks, @JsonProperty double[] breaks,
@JsonProperty double[] counts, @JsonProperty double[] counts,
@JsonProperty double min, @JsonProperty double[] quantiles
@JsonProperty double max
) )
{ {
Preconditions.checkArgument(breaks != null, "breaks must not be null"); Preconditions.checkArgument(breaks != null, "breaks must not be null");
@ -46,15 +45,13 @@ public class HistogramVisual
this.breaks = breaks; this.breaks = breaks;
this.counts = counts; this.counts = counts;
this.min = min; this.quantiles = quantiles;
this.max = max;
} }
public HistogramVisual( public HistogramVisual(
float[] breaks, float[] breaks,
float[] counts, float[] counts,
float min, float[] quantiles
float max
) )
{ {
Preconditions.checkArgument(breaks != null, "breaks must not be null"); Preconditions.checkArgument(breaks != null, "breaks must not be null");
@ -63,10 +60,10 @@ public class HistogramVisual
this.breaks = new double[breaks.length]; this.breaks = new double[breaks.length];
this.counts = new double[counts.length]; this.counts = new double[counts.length];
this.quantiles = new double[quantiles.length];
for(int i = 0; i < breaks.length; ++i) this.breaks[i] = breaks[i]; for(int i = 0; i < breaks.length; ++i) this.breaks[i] = breaks[i];
for(int i = 0; i < counts.length; ++i) this.counts[i] = counts[i]; for(int i = 0; i < counts.length; ++i) this.counts[i] = counts[i];
this.min = min; for(int i = 0; i < quantiles.length; ++i) this.quantiles[i] = quantiles[i];
this.max = max;
} }
@Override @Override
@ -75,8 +72,7 @@ public class HistogramVisual
return "HistogramVisual{" + return "HistogramVisual{" +
"counts=" + Arrays.toString(counts) + "counts=" + Arrays.toString(counts) +
", breaks=" + Arrays.toString(breaks) + ", breaks=" + Arrays.toString(breaks) +
", min=" + min + ", quantiles=" + Arrays.toString(quantiles) +
", max=" + max +
'}'; '}';
} }
} }

View File

@ -68,8 +68,7 @@ public class HistogramTest
Map<String,Object> expectedObj = Maps.newLinkedHashMap(); Map<String,Object> expectedObj = Maps.newLinkedHashMap();
expectedObj.put("breaks", Arrays.asList(visualBreaks)); expectedObj.put("breaks", Arrays.asList(visualBreaks));
expectedObj.put("counts", Arrays.asList(visualCounts)); expectedObj.put("counts", Arrays.asList(visualCounts));
expectedObj.put("min", -1.0); expectedObj.put("quantiles", Arrays.asList(new Double[]{-1.0, 1.0}));
expectedObj.put("max", 1.0);
Map<String,Object> obj = (Map<String, Object>)objectMapper.readValue(json, Object.class); Map<String,Object> obj = (Map<String, Object>)objectMapper.readValue(json, Object.class);
Assert.assertEquals(expectedObj, obj); Assert.assertEquals(expectedObj, obj);