Search in sources :

Example 11 with INodeView

use of com.amazon.randomcutforest.tree.INodeView in project random-cut-forest-by-aws by aws.

the class ImputeVisitorTest method testAcceptLeafEquals.

@Test
public void testAcceptLeafEquals() {
    float[] point = { queryPoint[0], 2.0f, queryPoint[2] };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getLiftedLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafDepth = 100;
    int leafMass = 10;
    when(leafNode.getMass()).thenReturn(leafMass);
    visitor.acceptLeaf(leafNode, leafDepth);
    float[] expected = new float[] { -1.0f, 2.0f, 3.0f };
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    assertEquals(defaultScoreSeenFunction(leafDepth, leafMass), visitor.getAnomalyRank());
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 12 with INodeView

use of com.amazon.randomcutforest.tree.INodeView in project random-cut-forest-by-aws by aws.

the class NearNeighborVisitorTest method acceptLeafNear.

@Test
public void acceptLeafNear() {
    float[] leafPoint = new float[] { 8.8f, 9.9f, -5.5f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(Arrays.copyOf(leafPoint, leafPoint.length));
    when(leafNode.getLiftedLeafPoint()).thenReturn(Arrays.copyOf(leafPoint, leafPoint.length));
    HashMap<Long, Integer> sequenceIndexes = new HashMap<>();
    sequenceIndexes.put(1234L, 1);
    sequenceIndexes.put(5678L, 1);
    when(leafNode.getSequenceIndexes()).thenReturn(sequenceIndexes);
    int depth = 12;
    visitor.acceptLeaf(leafNode, depth);
    Optional<Neighbor> optional = visitor.getResult();
    assertTrue(optional.isPresent());
    Neighbor neighbor = optional.get();
    assertNotSame(leafPoint, neighbor.point);
    assertArrayEquals(toDoubleArray(leafPoint), neighbor.point);
    assertEquals(Math.sqrt(3 * 1.1 * 1.1), neighbor.distance, EPSILON);
    assertNotSame(leafNode.getSequenceIndexes(), neighbor.sequenceIndexes);
}
Also used : HashMap(java.util.HashMap) Neighbor(com.amazon.randomcutforest.returntypes.Neighbor) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 13 with INodeView

use of com.amazon.randomcutforest.tree.INodeView in project random-cut-forest-by-aws by aws.

the class NearNeighborVisitorTest method acceptLeafNearTimestampsDisabled.

@Test
public void acceptLeafNearTimestampsDisabled() {
    float[] leafPoint = new float[] { 8.8f, 9.9f, -5.5f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLiftedLeafPoint()).thenReturn(Arrays.copyOf(leafPoint, leafPoint.length));
    when(leafNode.getLeafPoint()).thenReturn(Arrays.copyOf(leafPoint, leafPoint.length));
    assertEquals(0, leafNode.getSequenceIndexes().size());
    int depth = 12;
    visitor.acceptLeaf(leafNode, depth);
    Optional<Neighbor> optional = visitor.getResult();
    assertTrue(optional.isPresent());
    Neighbor neighbor = optional.get();
    assertNotSame(leafPoint, neighbor.point);
    assertArrayEquals(toDoubleArray(leafPoint), neighbor.point);
    assertEquals(Math.sqrt(3 * 1.1 * 1.1), neighbor.distance, EPSILON);
    assertTrue(neighbor.sequenceIndexes.isEmpty());
}
Also used : Neighbor(com.amazon.randomcutforest.returntypes.Neighbor) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 14 with INodeView

use of com.amazon.randomcutforest.tree.INodeView in project random-cut-forest-by-aws by aws.

the class SimpleInterpolationVisitorTest method testAcceptEqualsLeafPoint.

@Test
public void testAcceptEqualsLeafPoint() {
    float[] pointToScore = { 0.0f, 0.0f };
    int sampleSize = 50;
    SimpleInterpolationVisitor visitor = new SimpleInterpolationVisitor(pointToScore, sampleSize, 1, false);
    float[] point = Arrays.copyOf(pointToScore, pointToScore.length);
    INodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(point);
    when(node.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    when(node.getMass()).thenReturn(1);
    int depth = 2;
    visitor.acceptLeaf(node, depth);
    InterpolationMeasure result = visitor.getResult();
    double[] expected = new double[point.length];
    Arrays.fill(expected, 0.5 * (1 + node.getMass()) / point.length);
    assertArrayEquals(expected, result.measure.high);
    assertArrayEquals(expected, result.measure.low);
    Arrays.fill(expected, 0.5 / point.length);
    assertArrayEquals(expected, result.probMass.high);
    assertArrayEquals(expected, result.probMass.low);
    Arrays.fill(expected, 0.0);
    assertArrayEquals(expected, result.distances.high);
    assertArrayEquals(expected, result.distances.low);
    depth--;
    float[] siblingPoint = { 1.0f, -2.0f };
    INodeView sibling = mock(NodeView.class);
    int siblingMass = 2;
    when(sibling.getMass()).thenReturn(siblingMass);
    INodeView parent = mock(NodeView.class);
    when(parent.getMass()).thenReturn(1 + siblingMass);
    BoundingBox boundingBox = new BoundingBox(point, siblingPoint);
    when(parent.getBoundingBox()).thenReturn(boundingBox);
    when(parent.getSiblingBoundingBox(any())).thenReturn(new BoundingBox(siblingPoint));
    visitor.accept(parent, depth);
    result = visitor.getResult();
    // compute using shadow box (sibling leaf node at {1.0, -2.0} and parent
    // bounding box
    double[] directionalDistance = { 0.0, 1.0, 2.0, 0.0 };
    double[] differenceInRange = { 0.0, 1.0, 2.0, 0.0 };
    double sumOfNewRange = 1.0 + 2.0;
    double[] probVector = Arrays.stream(differenceInRange).map(x -> x / sumOfNewRange).toArray();
    expected = new double[2 * pointToScore.length];
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i] * (1 + node.getMass() + parent.getMass());
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.measure.high[i]);
        assertEquals(expected[2 * i + 1], result.measure.low[i]);
    }
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.probMass.high[i]);
        assertEquals(expected[2 * i + 1], result.probMass.low[i]);
    }
    for (int i = 0; i < expected.length; i++) {
        expected[i] = probVector[i] * directionalDistance[i];
    }
    for (int i = 0; i < pointToScore.length; i++) {
        assertEquals(expected[2 * i], result.distances.high[i]);
        assertEquals(expected[2 * i + 1], result.distances.low[i]);
    }
}
Also used : Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Test(org.junit.jupiter.api.Test) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) Arrays(java.util.Arrays) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) INodeView(com.amazon.randomcutforest.tree.INodeView) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.when(org.mockito.Mockito.when) NodeView(com.amazon.randomcutforest.tree.NodeView) Mockito.mock(org.mockito.Mockito.mock) BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 15 with INodeView

use of com.amazon.randomcutforest.tree.INodeView in project random-cut-forest-by-aws by aws.

the class SimpleInterpolationVisitorTest method testAcceptLeafEquals.

@Test
public void testAcceptLeafEquals() {
    float[] point = { 1.0f, 2.0f, 3.0f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(point);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int leafDepth = 100;
    int leafMass = 10;
    when(leafNode.getMass()).thenReturn(leafMass);
    int sampleSize = 21;
    SimpleInterpolationVisitor visitor = new SimpleInterpolationVisitor(point, sampleSize, 1, false);
    visitor.acceptLeaf(leafNode, leafDepth);
    InterpolationMeasure result = visitor.getResult();
    double[] expected = new double[point.length];
    Arrays.fill(expected, 0.5 * (1 + leafMass) / point.length);
    assertArrayEquals(expected, result.measure.high);
    assertArrayEquals(expected, result.measure.low);
    Arrays.fill(expected, 0.5 / point.length);
    assertArrayEquals(expected, result.probMass.high);
    assertArrayEquals(expected, result.probMass.low);
    Arrays.fill(expected, 0.0);
    assertArrayEquals(expected, result.distances.high);
    assertArrayEquals(expected, result.distances.low);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Aggregations

INodeView (com.amazon.randomcutforest.tree.INodeView)17 Test (org.junit.jupiter.api.Test)17 BoundingBox (com.amazon.randomcutforest.tree.BoundingBox)14 InterpolationMeasure (com.amazon.randomcutforest.returntypes.InterpolationMeasure)4 NodeView (com.amazon.randomcutforest.tree.NodeView)4 DiVector (com.amazon.randomcutforest.returntypes.DiVector)3 Neighbor (com.amazon.randomcutforest.returntypes.Neighbor)3 Arrays (java.util.Arrays)3 Assertions.assertArrayEquals (org.junit.jupiter.api.Assertions.assertArrayEquals)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 IBoundingBoxView (com.amazon.randomcutforest.tree.IBoundingBoxView)2 HashMap (java.util.HashMap)2