Search in sources :

Example 11 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class V2StateToV3ForestConverter method convert.

public RandomCutForest convert(RandomCutForestState v2State) {
    String version = v2State.getVersion();
    checkArgument(version.equals(V2_0) || version.equals(V2_1), "incorrect convertor");
    RandomCutForestMapper mapper = new RandomCutForestMapper();
    mapper.setCompressionEnabled(v2State.isCompressed());
    return mapper.toModel(v2State);
}
Also used : RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper)

Example 12 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class V1JsonToV3StateConverterTest method testMerge.

@ParameterizedTest
@MethodSource("args")
public void testMerge(V1JsonResource jsonResource, Precision precision) {
    String resource = jsonResource.getResource();
    try (InputStream is = V1JsonToV3StateConverterTest.class.getResourceAsStream(jsonResource.getResource());
        BufferedReader rr = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
        StringBuilder b = new StringBuilder();
        String line;
        while ((line = rr.readLine()) != null) {
            b.append(line);
        }
        String json = b.toString();
        int number = new Random().nextInt(10) + 1;
        int testNumberOfTrees = Math.min(100, 1 + new Random().nextInt(number * jsonResource.getNumberOfTrees() - 1));
        ArrayList<String> models = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            models.add(json);
        }
        RandomCutForestState state = converter.convert(models, testNumberOfTrees, precision).get();
        assertEquals(jsonResource.getDimensions(), state.getDimensions());
        assertEquals(testNumberOfTrees, state.getNumberOfTrees());
        assertEquals(jsonResource.getSampleSize(), state.getSampleSize());
        RandomCutForest forest = new RandomCutForestMapper().toModel(state, 0);
        assertEquals(jsonResource.getDimensions(), forest.getDimensions());
        assertEquals(testNumberOfTrees, forest.getNumberOfTrees());
        assertEquals(jsonResource.getSampleSize(), forest.getSampleSize());
        // perform a simple validation of the deserialized forest by update and scoring
        // with a few points
        Random random = new Random(0);
        for (int i = 0; i < 100; i++) {
            double[] point = getPoint(jsonResource.getDimensions(), random);
            double score = forest.getAnomalyScore(point);
            assertTrue(score > 0);
            forest.update(point);
        }
        int expectedSize = (int) Math.floor(1.0 * testNumberOfTrees * json.length() / (number * jsonResource.getNumberOfTrees()));
        String newString = new ObjectMapper().writeValueAsString(new RandomCutForestMapper().toState(forest));
        System.out.println(" Copied " + number + " times, old number of trees " + jsonResource.getNumberOfTrees() + ", new trees " + testNumberOfTrees + ", Expected Old size " + expectedSize + ", new Size " + newString.length());
    } catch (IOException e) {
        fail("Unable to load JSON resource");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ArrayList(java.util.ArrayList) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) IOException(java.io.IOException) Random(java.util.Random) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) BufferedReader(java.io.BufferedReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class RandomCutForestShingledFunctionalTest method testUpdate.

@Test
public void testUpdate() {
    int dimensions = 10;
    RandomCutForest forest = RandomCutForest.builder().numberOfTrees(100).compact(true).dimensions(dimensions).randomSeed(0).sampleSize(200).precision(Precision.FLOAT_32).build();
    double[][] trainingData = genShingledData(1000, dimensions, 0);
    double[][] testData = genShingledData(100, dimensions, 1);
    for (int i = 0; i < testData.length; i++) {
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(true);
        double score = forest.getAnomalyScore(testData[i]);
        forest.update(testData[i]);
        RandomCutForestState forestState = mapper.toState(forest);
        forest = mapper.toModel(forestState);
    }
}
Also used : RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class StateMapperShingledBenchmark method roundTripFromProtostuff.

@Benchmark
@OperationsPerInvocation(NUM_TEST_SAMPLES)
public byte[] roundTripFromProtostuff(BenchmarkState state, Blackhole blackhole) {
    bytes = state.protostuff;
    double[][] testData = state.testData;
    for (int i = 0; i < NUM_TEST_SAMPLES; i++) {
        Schema<RandomCutForestState> schema = RuntimeSchema.getSchema(RandomCutForestState.class);
        RandomCutForestState forestState = schema.newMessage();
        ProtostuffIOUtil.mergeFrom(bytes, forestState, schema);
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(state.saveTreeState);
        RandomCutForest forest = mapper.toModel(forestState);
        double score = forest.getAnomalyScore(testData[i]);
        blackhole.consume(score);
        forest.update(testData[i]);
        forestState = mapper.toState(forest);
        LinkedBuffer buffer = LinkedBuffer.allocate(512);
        try {
            bytes = ProtostuffIOUtil.toByteArray(forestState, schema, buffer);
        } finally {
            buffer.clear();
        }
    }
    return bytes;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 15 with RandomCutForestMapper

use of com.amazon.randomcutforest.state.RandomCutForestMapper in project random-cut-forest-by-aws by aws.

the class StateMapperShingledBenchmark method roundTripFromJson.

@Benchmark
@OperationsPerInvocation(NUM_TEST_SAMPLES)
public String roundTripFromJson(BenchmarkState state, Blackhole blackhole) throws JsonProcessingException {
    String json = state.json;
    double[][] testData = state.testData;
    for (int i = 0; i < NUM_TEST_SAMPLES; i++) {
        ObjectMapper jsonMapper = new ObjectMapper();
        RandomCutForestState forestState = jsonMapper.readValue(json, RandomCutForestState.class);
        RandomCutForestMapper mapper = new RandomCutForestMapper();
        mapper.setSaveExecutorContextEnabled(true);
        mapper.setSaveTreeStateEnabled(state.saveTreeState);
        RandomCutForest forest = mapper.toModel(forestState);
        double score = forest.getAnomalyScore(testData[i]);
        blackhole.consume(score);
        forest.update(testData[i]);
        json = jsonMapper.writeValueAsString(mapper.toState(forest));
    }
    bytes = json.getBytes();
    return json;
}
Also used : RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForestState(com.amazon.randomcutforest.state.RandomCutForestState) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Aggregations

RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)21 RandomCutForestState (com.amazon.randomcutforest.state.RandomCutForestState)15 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)10 Precision (com.amazon.randomcutforest.config.Precision)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 OperationsPerInvocation (org.openjdk.jmh.annotations.OperationsPerInvocation)6 NormalMixtureTestData (com.amazon.randomcutforest.testutils.NormalMixtureTestData)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 LinkedBuffer (io.protostuff.LinkedBuffer)5 Random (java.util.Random)5 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IRCFComputeDescriptor (com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor)2 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)2 Preprocessor (com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor)2 PreprocessorMapper (com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper)2 BasicThresholderMapper (com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper)2 DiVectorMapper (com.amazon.randomcutforest.state.returntypes.DiVectorMapper)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2