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();
}
/**
* 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() {
float[] visualCounts = new float[bins.length - 2];
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) {

View File

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

View File

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