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