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());
}
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);
}
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());
}
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]);
}
}
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);
}
Aggregations