Search in sources :

Example 6 with BoundingBox

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

the class ImputeVisitorTest method testMerge.

@Test
public void testMerge() {
    float[] otherPoint = new float[] { 99, 100, 101 };
    ImputeVisitor other = new ImputeVisitor(otherPoint, 0, new int[0]);
    // set other.rank to a small value
    NodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(new float[] { 0, 0, 0 });
    when(node.getLiftedLeafPoint()).thenReturn(new float[] { 0, 0, 0 });
    when(node.getBoundingBox()).thenReturn(new BoundingBox(new float[] { 0, 0, 0 }));
    other.acceptLeaf(node, 99);
    assertTrue(other.getAnomalyRank() < visitor.getAnomalyRank());
    other.combine(visitor);
    assertArrayEquals(otherPoint, other.getResult().leafPoint);
    visitor.combine(other);
    assertArrayEquals(otherPoint, visitor.getResult().leafPoint);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) NodeView(com.amazon.randomcutforest.tree.NodeView) Test(org.junit.jupiter.api.Test)

Example 7 with BoundingBox

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

the class ImputeVisitorTest method testAcceptLeafEqualsZeroDepth.

@Test
public void testAcceptLeafEqualsZeroDepth() {
    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 = 0;
    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(0.0, visitor.getAnomalyRank());
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 8 with BoundingBox

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

the class ImputeVisitorTest method testAccept.

@Test
public void testAccept() {
    float[] point = { queryPoint[0], 2.0f, -111.11f };
    INodeView node = mock(NodeView.class);
    when(node.getLeafPoint()).thenReturn(point);
    when(node.getLiftedLeafPoint()).thenReturn(point);
    when(node.getBoundingBox()).thenReturn(new BoundingBox(point, point));
    int depth = 100;
    int leafMass = 10;
    when(node.getMass()).thenReturn(leafMass);
    visitor.acceptLeaf(node, depth);
    float[] expected = new float[] { -1.0f, 2.0f, 3.0f };
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    assertEquals(defaultScoreUnseenFunction(depth, leafMass), visitor.getAnomalyRank());
    depth--;
    IBoundingBoxView boundingBox = node.getBoundingBox().getMergedBox(new float[] { 99.0f, 4.0f, -19.0f });
    when(node.getBoundingBox()).thenReturn(boundingBox);
    when(node.probailityOfSeparation(any())).thenReturn(CommonUtils.getProbabilityOfSeparation(boundingBox, expected));
    when(node.getMass()).thenReturn(leafMass + 2);
    double oldRank = visitor.getAnomalyRank();
    visitor.accept(node, depth);
    assertArrayEquals(expected, visitor.getResult().leafPoint);
    double p = CommonUtils.getProbabilityOfSeparation(boundingBox, expected);
    double expectedRank = p * defaultScoreUnseenFunction(depth, node.getMass()) + (1 - p) * oldRank;
    assertEquals(expectedRank, visitor.getAnomalyRank(), EPSILON);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Example 9 with BoundingBox

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

the class ImputeVisitorTest method testAcceptLeafNotEquals.

@Test
public void testAcceptLeafNotEquals() {
    float[] point = { queryPoint[0], 2.0f, -111.11f };
    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(defaultScoreUnseenFunction(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 10 with BoundingBox

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

the class AnomalyScoreVisitorTest method testAcceptLeafNotEquals.

@Test
public void testAcceptLeafNotEquals() {
    float[] point = new float[] { 1.0f, 2.0f, 3.0f };
    float[] anotherPoint = new float[] { 4.0f, 5.0f, 6.0f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(anotherPoint);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(anotherPoint, anotherPoint));
    int leafDepth = 100;
    AnomalyScoreVisitor visitor = new AnomalyScoreVisitor(point, 2);
    visitor.acceptLeaf(leafNode, leafDepth);
    double expectedScore = 1.0 / (leafDepth + 1);
    assertThat(visitor.getResult(), closeTo(CommonUtils.defaultScalarNormalizerFunction(expectedScore, 2), EPSILON));
    assertFalse(visitor.pointInsideBox);
}
Also used : BoundingBox(com.amazon.randomcutforest.tree.BoundingBox) INodeView(com.amazon.randomcutforest.tree.INodeView) Test(org.junit.jupiter.api.Test)

Aggregations

BoundingBox (com.amazon.randomcutforest.tree.BoundingBox)18 Test (org.junit.jupiter.api.Test)18 INodeView (com.amazon.randomcutforest.tree.INodeView)16 NodeView (com.amazon.randomcutforest.tree.NodeView)6 InterpolationMeasure (com.amazon.randomcutforest.returntypes.InterpolationMeasure)4 IBoundingBoxView (com.amazon.randomcutforest.tree.IBoundingBoxView)4 DiVector (com.amazon.randomcutforest.returntypes.DiVector)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