Search in sources :

Example 6 with InterpolationMeasure

use of com.amazon.randomcutforest.returntypes.InterpolationMeasure 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)

Example 7 with InterpolationMeasure

use of com.amazon.randomcutforest.returntypes.InterpolationMeasure in project random-cut-forest-by-aws by aws.

the class SimpleInterpolationVisitorTest method testAcceptLeafNotEquals.

@Test
public void testAcceptLeafNotEquals() {
    float[] point = { 1.0f, 9.0f, 4.0f };
    float[] anotherPoint = { 4.0f, 5.0f, 6.0f };
    INodeView leafNode = mock(NodeView.class);
    when(leafNode.getLeafPoint()).thenReturn(anotherPoint);
    when(leafNode.getBoundingBox()).thenReturn(new BoundingBox(anotherPoint, anotherPoint));
    when(leafNode.getMass()).thenReturn(4);
    int leafDepth = 100;
    int sampleSize = 99;
    SimpleInterpolationVisitor visitor = new SimpleInterpolationVisitor(point, sampleSize, 1, false);
    visitor.acceptLeaf(leafNode, leafDepth);
    InterpolationMeasure result = visitor.getResult();
    double expectedSumOfNewRange = 3.0 + 4.0 + 2.0;
    double[] expectedDifferenceInRangeVector = { 0.0, 3.0, 4.0, 0.0, 0.0, 2.0 };
    double[] expectedProbVector = Arrays.stream(expectedDifferenceInRangeVector).map(x -> x / expectedSumOfNewRange).toArray();
    double[] expectedmeasure = Arrays.stream(expectedProbVector).toArray();
    double[] expectedDistances = new double[2 * point.length];
    for (int i = 0; i < 2 * point.length; i++) {
        expectedDistances[i] = expectedProbVector[i] * expectedDifferenceInRangeVector[i];
    }
    for (int i = 0; i < 2 * point.length; i++) {
        expectedmeasure[i] = expectedmeasure[i] * 5;
    }
    for (int i = 0; i < point.length; i++) {
        assertEquals(expectedProbVector[2 * i], result.probMass.high[i]);
        assertEquals(expectedProbVector[2 * i + 1], result.probMass.low[i]);
        assertEquals(expectedmeasure[2 * i], result.measure.high[i]);
        assertEquals(expectedmeasure[2 * i + 1], result.measure.low[i]);
        assertEquals(expectedDistances[2 * i], result.distances.high[i]);
        assertEquals(expectedDistances[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)

Aggregations

InterpolationMeasure (com.amazon.randomcutforest.returntypes.InterpolationMeasure)7 Test (org.junit.jupiter.api.Test)6 BoundingBox (com.amazon.randomcutforest.tree.BoundingBox)4 INodeView (com.amazon.randomcutforest.tree.INodeView)4 NodeView (com.amazon.randomcutforest.tree.NodeView)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 DensityOutput (com.amazon.randomcutforest.returntypes.DensityOutput)2 SamplerPlusTree (com.amazon.randomcutforest.executor.SamplerPlusTree)1 SimpleInterpolationVisitor (com.amazon.randomcutforest.interpolation.SimpleInterpolationVisitor)1 ArrayList (java.util.ArrayList)1