Search in sources :

Example 1 with ArrayOfDoublesSketchIterator

use of com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator in project sketches-pig by DataSketches.

the class ArrayOfDoublesSketchStats method sketchToSummaryStatistics.

/**
 * Convert sketch to summary statistics.
 *
 * @param sketch ArrayOfDoublesSketch to convert to summary statistics.
 * @return An array of SummaryStatistics.
 */
static SummaryStatistics[] sketchToSummaryStatistics(final ArrayOfDoublesSketch sketch) {
    final SummaryStatistics[] summaryStatistics = new SummaryStatistics[sketch.getNumValues()];
    for (int i = 0; i < sketch.getNumValues(); i++) {
        summaryStatistics[i] = new SummaryStatistics();
    }
    final ArrayOfDoublesSketchIterator it = sketch.iterator();
    while (it.next()) {
        final double[] values = it.getValues();
        for (int i = 0; i < it.getValues().length; i++) {
            summaryStatistics[i].addValue(values[i]);
        }
    }
    return summaryStatistics;
}
Also used : ArrayOfDoublesSketchIterator(com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics)

Example 2 with ArrayOfDoublesSketchIterator

use of com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator in project sketches-pig by DataSketches.

the class ArrayOfDoublesSketchToEstimates method exec.

@Override
public Tuple exec(final Tuple input) throws IOException {
    if ((input == null) || (input.size() == 0)) {
        return null;
    }
    final DataByteArray dba = (DataByteArray) input.get(0);
    final ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.wrapSketch(Memory.wrap(dba.get()));
    final double[] estimates = new double[sketch.getNumValues() + 1];
    estimates[0] = sketch.getEstimate();
    if (sketch.getRetainedEntries() > 0) {
        // remove unnecessary check when version of sketches-core > 0.4.0
        final ArrayOfDoublesSketchIterator it = sketch.iterator();
        while (it.next()) {
            final double[] values = it.getValues();
            for (int i = 0; i < sketch.getNumValues(); i++) {
                estimates[i + 1] += values[i];
            }
        }
        for (int i = 0; i < sketch.getNumValues(); i++) {
            estimates[i + 1] /= sketch.getTheta();
        }
    }
    return Util.doubleArrayToTuple(estimates);
}
Also used : ArrayOfDoublesSketchIterator(com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator) DataByteArray(org.apache.pig.data.DataByteArray) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch)

Example 3 with ArrayOfDoublesSketchIterator

use of com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator in project sketches-pig by DataSketches.

the class ArrayOfDoublesSketchToQuantilesSketch method exec.

@Override
public DataByteArray exec(final Tuple input) throws IOException {
    if ((input == null) || (input.size() == 0)) {
        return null;
    }
    final DataByteArray dba = (DataByteArray) input.get(0);
    final ArrayOfDoublesSketch sketch = ArrayOfDoublesSketches.wrapSketch(Memory.wrap(dba.get()));
    int column = 1;
    if (input.size() > 1) {
        column = (int) input.get(1);
        if ((column < 1) || (column > sketch.getNumValues())) {
            throw new IllegalArgumentException("Column number out of range. The given sketch has " + sketch.getNumValues() + " columns");
        }
    }
    final DoublesSketchBuilder builder = DoublesSketch.builder();
    if (k > 0) {
        builder.setK(k);
    }
    final UpdateDoublesSketch qs = builder.build();
    final ArrayOfDoublesSketchIterator it = sketch.iterator();
    while (it.next()) {
        qs.update(it.getValues()[column - 1]);
    }
    return new DataByteArray(qs.compact().toByteArray());
}
Also used : ArrayOfDoublesSketchIterator(com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator) UpdateDoublesSketch(com.yahoo.sketches.quantiles.UpdateDoublesSketch) DoublesSketchBuilder(com.yahoo.sketches.quantiles.DoublesSketchBuilder) DataByteArray(org.apache.pig.data.DataByteArray) ArrayOfDoublesSketch(com.yahoo.sketches.tuple.ArrayOfDoublesSketch)

Aggregations

ArrayOfDoublesSketchIterator (com.yahoo.sketches.tuple.ArrayOfDoublesSketchIterator)3 ArrayOfDoublesSketch (com.yahoo.sketches.tuple.ArrayOfDoublesSketch)2 DataByteArray (org.apache.pig.data.DataByteArray)2 DoublesSketchBuilder (com.yahoo.sketches.quantiles.DoublesSketchBuilder)1 UpdateDoublesSketch (com.yahoo.sketches.quantiles.UpdateDoublesSketch)1 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)1