Search in sources :

Example 1 with Intersection

use of org.apache.datasketches.tuple.Intersection in project sketches-core by DataSketches.

the class AdoubleIntersectionTest method checkExactIntersectionWithThetaDisjoint.

@Test
public void checkExactIntersectionWithThetaDisjoint() {
    final UpdateSketch thSkA = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    final UpdateSketch thSkB = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    int key = 0;
    for (int i = 0; i < 32; i++) {
        thSkA.update(key++);
    }
    for (int i = 0; i < 32; i++) {
        thSkB.update(key++);
    }
    final DoubleSummary dsum = new DoubleSummaryFactory(mode).newSummary();
    final Intersection<DoubleSummary> intersection = new Intersection<>(new DoubleSummarySetOperations(mode, mode));
    CompactSketch<DoubleSummary> result;
    intersection.intersect(thSkA, dsum);
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
    // an intersection with no entries must survive more updates
    intersection.intersect(thSkA, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
    intersection.reset();
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 2 with Intersection

use of org.apache.datasketches.tuple.Intersection in project sketches-core by DataSketches.

the class BoundsOnRatiosInTupleSketchedSetsTest method checkNormalReturns2.

@Test
public void checkNormalReturns2() {
    // tuple, theta
    // 4K
    final UpdatableSketch<Double, DoubleSummary> skA = tupleBldr.build();
    final UpdateSketch skC = thetaBldr.build();
    final int uA = 10000;
    final int uC = 100000;
    for (int i = 0; i < uA; i++) {
        skA.update(i, constSummary);
    }
    for (int i = 0; i < uC; i++) {
        skC.update(i + (uA / 2));
    }
    final Intersection<DoubleSummary> inter = new Intersection<>(dsso);
    inter.intersect(skA);
    inter.intersect(skC, factory.newSummary());
    final Sketch<DoubleSummary> skB = inter.getResult();
    double est = BoundsOnRatiosInTupleSketchedSets.getEstimateOfBoverA(skA, skB);
    double lb = BoundsOnRatiosInTupleSketchedSets.getLowerBoundForBoverA(skA, skB);
    double ub = BoundsOnRatiosInTupleSketchedSets.getUpperBoundForBoverA(skA, skB);
    assertTrue(ub > est);
    assertTrue(est > lb);
    assertEquals(est, 0.5, .03);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
    // skA is now empty
    skA.reset();
    est = BoundsOnRatiosInTupleSketchedSets.getEstimateOfBoverA(skA, skB);
    lb = BoundsOnRatiosInTupleSketchedSets.getLowerBoundForBoverA(skA, skB);
    ub = BoundsOnRatiosInTupleSketchedSets.getUpperBoundForBoverA(skA, skB);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
    // Now both are empty
    skC.reset();
    est = BoundsOnRatiosInTupleSketchedSets.getEstimateOfBoverA(skA, skC);
    lb = BoundsOnRatiosInTupleSketchedSets.getLowerBoundForBoverA(skA, skC);
    ub = BoundsOnRatiosInTupleSketchedSets.getUpperBoundForBoverA(skA, skC);
    println("ub : " + ub);
    println("est: " + est);
    println("lb : " + lb);
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) DoubleSummary(org.apache.datasketches.tuple.adouble.DoubleSummary) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 3 with Intersection

use of org.apache.datasketches.tuple.Intersection in project sketches-core by DataSketches.

the class AdoubleIntersectionTest method checkEstimatingIntersectionWithThetaOverlapping.

@Test
public void checkEstimatingIntersectionWithThetaOverlapping() {
    final UpdateSketch thSkA = new UpdateSketchBuilder().setLogNominalEntries(4).build();
    final UpdateSketch thSkB = new UpdateSketchBuilder().setLogNominalEntries(10).build();
    // dense mode, low theta
    for (int i = 0; i < 64; i++) {
        thSkA.update(i);
    }
    // exact overlapping
    for (int i = 32; i < 96; i++) {
        thSkB.update(i);
    }
    final DoubleSummary dsum = new DoubleSummaryFactory(mode).newSummary();
    final Intersection<DoubleSummary> intersection = new Intersection<>(new DoubleSummarySetOperations(mode, mode));
    CompactSketch<DoubleSummary> result;
    intersection.intersect(thSkA, dsum);
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 14);
    thSkB.reset();
    // exact, disjoint
    for (int i = 100; i < 164; i++) {
        thSkB.update(i);
    }
    // remove existing entries
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
    intersection.intersect(thSkB, dsum);
    result = intersection.getResult();
    Assert.assertEquals(result.getRetainedEntries(), 0);
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 4 with Intersection

use of org.apache.datasketches.tuple.Intersection in project sketches-core by DataSketches.

the class AdoubleIntersectionTest method checkExactIntersectionWithTheta.

@Test
public void checkExactIntersectionWithTheta() {
    final UpdateSketch thSkNull = null;
    final UpdateSketch thSkEmpty = new UpdateSketchBuilder().build();
    final UpdateSketch thSk10 = new UpdateSketchBuilder().build();
    final UpdateSketch thSk15 = new UpdateSketchBuilder().build();
    for (int i = 0; i < 10; i++) {
        thSk10.update(i);
    }
    // overlap = 5
    for (int i = 0; i < 10; i++) {
        thSk15.update(i + 5);
    }
    DoubleSummary dsum = new DoubleSummaryFactory(mode).newSummary();
    final Intersection<DoubleSummary> intersection = new Intersection<>(new DoubleSummarySetOperations(mode, mode));
    CompactSketch<DoubleSummary> result;
    try {
        intersection.getResult();
        fail();
    }// OK.
     catch (final SketchesStateException e) {
    }
    try {
        intersection.intersect(thSkNull, dsum);
        fail();
    }// OK
     catch (final SketchesArgumentException e) {
    }
    intersection.intersect(thSkEmpty, dsum);
    result = intersection.getResult();
    // Empty after empty first call
    Assert.assertTrue(result.isEmpty());
    intersection.reset();
    intersection.intersect(thSk10, dsum);
    result = intersection.getResult();
    // Returns valid first call
    Assert.assertEquals(result.getEstimate(), 10.0);
    intersection.reset();
    // Valid first call
    intersection.intersect(thSk10, dsum);
    intersection.intersect(thSkEmpty, dsum);
    result = intersection.getResult();
    // Returns Empty after empty second call
    Assert.assertTrue(result.isEmpty());
    intersection.reset();
    intersection.intersect(thSk10, dsum);
    intersection.intersect(thSk15, dsum);
    result = intersection.getResult();
    // Returns intersection
    Assert.assertEquals(result.getEstimate(), 5.0);
    intersection.reset();
    dsum = null;
    try {
        intersection.intersect(thSk10, dsum);
        fail();
    } catch (final SketchesArgumentException e) {
    }
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) UpdateSketchBuilder(org.apache.datasketches.theta.UpdateSketchBuilder) SketchesStateException(org.apache.datasketches.SketchesStateException) UpdateSketch(org.apache.datasketches.theta.UpdateSketch) Test(org.testng.annotations.Test)

Example 5 with Intersection

use of org.apache.datasketches.tuple.Intersection in project sketches-core by DataSketches.

the class ArrayOfStringsSketchTest method checkSketch.

@SuppressWarnings("deprecation")
@Test
public void checkSketch() {
    ArrayOfStringsSketch sketch1 = new ArrayOfStringsSketch();
    String[][] strArrArr = { { "a", "b" }, { "c", "d" }, { "e", "f" } };
    int len = strArrArr.length;
    for (int i = 0; i < len; i++) {
        sketch1.update(strArrArr[i], strArrArr[i]);
    }
    // insert duplicate
    sketch1.update(strArrArr[0], strArrArr[0]);
    // printSummaries(sketch1.iterator());
    byte[] array = sketch1.toByteArray();
    WritableMemory wmem = WritableMemory.writableWrap(array);
    ArrayOfStringsSketch sketch2 = new ArrayOfStringsSketch(wmem);
    // printSummaries(sketch2.iterator());
    checkSummaries(sketch2, sketch2);
    String[] strArr3 = { "g", "h" };
    sketch2.update(strArr3, strArr3);
    Union<ArrayOfStringsSummary> union = new Union<>(new ArrayOfStringsSummarySetOperations());
    union.union(sketch1);
    union.union(sketch2);
    CompactSketch<ArrayOfStringsSummary> csk = union.getResult();
    // printSummaries(csk.iterator());
    assertEquals(csk.getRetainedEntries(), 4);
    Intersection<ArrayOfStringsSummary> inter = new Intersection<>(new ArrayOfStringsSummarySetOperations());
    inter.intersect(sketch1);
    inter.intersect(sketch2);
    csk = inter.getResult();
    assertEquals(csk.getRetainedEntries(), 3);
    AnotB<ArrayOfStringsSummary> aNotB = new AnotB<>();
    aNotB.setA(sketch2);
    aNotB.notB(sketch1);
    csk = aNotB.getResult(true);
    assertEquals(csk.getRetainedEntries(), 1);
}
Also used : Intersection(org.apache.datasketches.tuple.Intersection) Union(org.apache.datasketches.tuple.Union) AnotB(org.apache.datasketches.tuple.AnotB) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

Intersection (org.apache.datasketches.tuple.Intersection)5 Test (org.testng.annotations.Test)5 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)4 UpdateSketchBuilder (org.apache.datasketches.theta.UpdateSketchBuilder)3 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)1 SketchesStateException (org.apache.datasketches.SketchesStateException)1 WritableMemory (org.apache.datasketches.memory.WritableMemory)1 AnotB (org.apache.datasketches.tuple.AnotB)1 Union (org.apache.datasketches.tuple.Union)1 DoubleSummary (org.apache.datasketches.tuple.adouble.DoubleSummary)1