use of com.amazon.randomcutforest.executor.SamplerPlusTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestTest method testGetApproximateAnomalyAttribution.
@Test
public void testGetApproximateAnomalyAttribution() {
float[] point = { 1.2f, -3.4f };
DiVector zero = new DiVector(dimensions);
DiVector result = forest.getApproximateAnomalyAttribution(point);
assertFalse(forest.isOutputReady());
assertArrayEquals(zero.high, result.high, EPSILON);
assertArrayEquals(zero.low, result.low, EPSILON);
doReturn(true).when(forest).isOutputReady();
ConvergingAccumulator<DiVector> accumulator = new OneSidedConvergingDiVectorAccumulator(dimensions, RandomCutForest.DEFAULT_APPROXIMATE_ANOMALY_SCORE_HIGH_IS_CRITICAL, RandomCutForest.DEFAULT_APPROXIMATE_DYNAMIC_SCORE_PRECISION, RandomCutForest.DEFAULT_APPROXIMATE_DYNAMIC_SCORE_MIN_VALUES_ACCEPTED, numberOfTrees);
for (int i = 0; i < numberOfTrees; i++) {
SamplerPlusTree<Integer, float[]> component = (SamplerPlusTree<Integer, float[]>) components.get(i);
ITree<Integer, float[]> tree = component.getTree();
DiVector treeResult = new DiVector(dimensions);
for (int j = 0; j < dimensions; j++) {
treeResult.high[j] = Math.random();
treeResult.low[j] = Math.random();
}
when(tree.traverse(aryEq(point), any(VisitorFactory.class))).thenReturn(treeResult);
when(tree.getMass()).thenReturn(256);
if (!accumulator.isConverged()) {
accumulator.accept(treeResult);
}
}
DiVector expectedResult = accumulator.getAccumulatedValue().scale(1.0 / accumulator.getValuesAccepted());
result = forest.getApproximateAnomalyAttribution(point);
assertArrayEquals(expectedResult.high, result.high, EPSILON);
assertArrayEquals(expectedResult.low, result.low, EPSILON);
}
use of com.amazon.randomcutforest.executor.SamplerPlusTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestTest method testGetSimpleDensity.
@Test
public void testGetSimpleDensity() {
float[] point = { 12.3f, -45.6f };
DensityOutput zero = new DensityOutput(dimensions, sampleSize);
assertFalse(forest.samplersFull());
DensityOutput result = forest.getSimpleDensity(point);
assertEquals(zero.getDensity(), result.getDensity(), EPSILON);
doReturn(true).when(forest).samplersFull();
List<InterpolationMeasure> intermediateResults = new ArrayList<>();
for (int i = 0; i < numberOfTrees; i++) {
InterpolationMeasure treeResult = new InterpolationMeasure(dimensions, sampleSize);
for (int j = 0; j < dimensions; j++) {
treeResult.measure.high[j] = Math.random();
treeResult.measure.low[j] = Math.random();
treeResult.distances.high[j] = Math.random();
treeResult.distances.low[j] = Math.random();
treeResult.probMass.high[j] = Math.random();
treeResult.probMass.low[j] = Math.random();
}
SamplerPlusTree<Integer, float[]> component = (SamplerPlusTree<Integer, float[]>) components.get(i);
ITree<Integer, float[]> tree = component.getTree();
when(tree.traverse(aryEq(point), any(VisitorFactory.class))).thenReturn(treeResult);
intermediateResults.add(treeResult);
}
Collector<InterpolationMeasure, ?, InterpolationMeasure> collector = InterpolationMeasure.collector(dimensions, sampleSize, numberOfTrees);
DensityOutput expectedResult = new DensityOutput(intermediateResults.stream().collect(collector));
result = forest.getSimpleDensity(point);
assertEquals(expectedResult.getDensity(), result.getDensity(), EPSILON);
}
use of com.amazon.randomcutforest.executor.SamplerPlusTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestTest method testGetAnomalyAttribution.
@Test
public void testGetAnomalyAttribution() {
float[] point = { 1.2f, -3.4f };
assertFalse(forest.isOutputReady());
DiVector zero = new DiVector(dimensions);
DiVector result = forest.getAnomalyAttribution(point);
assertArrayEquals(zero.high, result.high);
assertArrayEquals(zero.low, result.low);
doReturn(true).when(forest).isOutputReady();
DiVector expectedResult = new DiVector(dimensions);
for (int i = 0; i < numberOfTrees; i++) {
DiVector treeResult = new DiVector(dimensions);
for (int j = 0; j < dimensions; j++) {
treeResult.high[j] = Math.random();
treeResult.low[j] = Math.random();
}
SamplerPlusTree<Integer, float[]> component = (SamplerPlusTree<Integer, float[]>) components.get(i);
ITree<Integer, float[]> tree = component.getTree();
when(tree.traverse(aryEq(point), any(VisitorFactory.class))).thenReturn(treeResult);
when(tree.getMass()).thenReturn(256);
DiVector.addToLeft(expectedResult, treeResult);
}
expectedResult = expectedResult.scale(1.0 / numberOfTrees);
result = forest.getAnomalyAttribution(point);
assertArrayEquals(expectedResult.high, result.high, EPSILON);
assertArrayEquals(expectedResult.low, result.low, EPSILON);
}
use of com.amazon.randomcutforest.executor.SamplerPlusTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestMapper method singlePrecisionForest.
public RandomCutForest singlePrecisionForest(RandomCutForest.Builder<?> builder, RandomCutForestState state, IPointStore<float[]> extPointStore, List<ITree<Integer, float[]>> extTrees, List<IStreamSampler<Integer>> extSamplers) {
checkArgument(builder != null, "builder cannot be null");
checkArgument(extTrees == null || extTrees.size() == state.getNumberOfTrees(), "incorrect number of trees");
checkArgument(extSamplers == null || extSamplers.size() == state.getNumberOfTrees(), "incorrect number of samplers");
checkArgument(extSamplers != null | state.isSaveSamplerStateEnabled(), " need samplers ");
checkArgument(extPointStore != null || state.isSaveCoordinatorStateEnabled(), " need coordinator state ");
Random random = builder.getRandom();
ComponentList<Integer, float[]> components = new ComponentList<>();
CompactRandomCutTreeContext context = new CompactRandomCutTreeContext();
IPointStore<float[]> pointStore = (extPointStore == null) ? new PointStoreMapper().toModel(state.getPointStoreState()) : extPointStore;
PointStoreCoordinator<float[]> coordinator = new PointStoreCoordinator<>(pointStore);
coordinator.setTotalUpdates(state.getTotalUpdates());
context.setPointStore(pointStore);
context.setMaxSize(state.getSampleSize());
RandomCutTreeMapper treeMapper = new RandomCutTreeMapper();
List<CompactRandomCutTreeState> treeStates = state.isSaveTreeStateEnabled() ? state.getCompactRandomCutTreeStates() : null;
CompactSamplerMapper samplerMapper = new CompactSamplerMapper();
List<CompactSamplerState> samplerStates = state.isSaveSamplerStateEnabled() ? state.getCompactSamplerStates() : null;
for (int i = 0; i < state.getNumberOfTrees(); i++) {
IStreamSampler<Integer> sampler = (extSamplers != null) ? extSamplers.get(i) : samplerMapper.toModel(samplerStates.get(i), random.nextLong());
ITree<Integer, float[]> tree;
if (extTrees != null) {
tree = extTrees.get(i);
} else if (treeStates != null) {
tree = treeMapper.toModel(treeStates.get(i), context, random.nextLong());
sampler.getSample().forEach(s -> tree.addPoint(s.getValue(), s.getSequenceIndex()));
tree.setConfig(Config.BOUNDING_BOX_CACHE_FRACTION, treeStates.get(i).getBoundingBoxCacheFraction());
} else {
// using boundingBoxCahce for the new tree
tree = new RandomCutTree.Builder().capacity(state.getSampleSize()).randomSeed(random.nextLong()).pointStoreView(pointStore).boundingBoxCacheFraction(state.getBoundingBoxCacheFraction()).centerOfMassEnabled(state.isCenterOfMassEnabled()).storeSequenceIndexesEnabled(state.isStoreSequenceIndexesEnabled()).build();
sampler.getSample().forEach(s -> tree.addPoint(s.getValue(), s.getSequenceIndex()));
}
components.add(new SamplerPlusTree<>(sampler, tree));
}
builder.precision(Precision.FLOAT_32);
return new RandomCutForest(builder, coordinator, components, random);
}
use of com.amazon.randomcutforest.executor.SamplerPlusTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestMapper method toState.
/**
* Create a {@link RandomCutForestState} object representing the state of the
* given forest. If the forest is compact and the {@code saveTreeState} flag is
* set to true, then structure of the trees in the forest will be included in
* the state object. If the flag is set to false, then the state object will
* only contain the sampler data for each tree. If the
* {@code saveExecutorContext} is true, then the executor context will be
* included in the state object.
*
* @param forest A Random Cut Forest whose state we want to capture.
* @return a {@link RandomCutForestState} object representing the state of the
* given forest.
* @throws IllegalArgumentException if the {@code saveTreeState} flag is true
* and the forest is not compact.
*/
@Override
public RandomCutForestState toState(RandomCutForest forest) {
if (saveTreeStateEnabled) {
checkArgument(forest.isCompact(), "tree state cannot be saved for noncompact forests");
}
RandomCutForestState state = new RandomCutForestState();
state.setNumberOfTrees(forest.getNumberOfTrees());
state.setDimensions(forest.getDimensions());
state.setTimeDecay(forest.getTimeDecay());
state.setSampleSize(forest.getSampleSize());
state.setShingleSize(forest.getShingleSize());
state.setCenterOfMassEnabled(forest.isCenterOfMassEnabled());
state.setOutputAfter(forest.getOutputAfter());
state.setStoreSequenceIndexesEnabled(forest.isStoreSequenceIndexesEnabled());
state.setTotalUpdates(forest.getTotalUpdates());
state.setCompact(forest.isCompact());
state.setInternalShinglingEnabled(forest.isInternalShinglingEnabled());
state.setBoundingBoxCacheFraction(forest.getBoundingBoxCacheFraction());
state.setSaveSamplerStateEnabled(saveSamplerStateEnabled);
state.setSaveTreeStateEnabled(saveTreeStateEnabled);
state.setSaveCoordinatorStateEnabled(saveCoordinatorStateEnabled);
state.setPrecision(forest.getPrecision().name());
state.setCompressed(compressionEnabled);
state.setPartialTreeState(partialTreeStateEnabled);
if (saveExecutorContextEnabled) {
ExecutionContext executionContext = new ExecutionContext();
executionContext.setParallelExecutionEnabled(forest.isParallelExecutionEnabled());
executionContext.setThreadPoolSize(forest.getThreadPoolSize());
state.setExecutionContext(executionContext);
}
if (saveCoordinatorStateEnabled) {
PointStoreCoordinator<?> pointStoreCoordinator = (PointStoreCoordinator<?>) forest.getUpdateCoordinator();
PointStoreMapper mapper = new PointStoreMapper();
mapper.setCompressionEnabled(compressionEnabled);
mapper.setNumberOfTrees(forest.getNumberOfTrees());
PointStoreState pointStoreState = mapper.toState((PointStore) pointStoreCoordinator.getStore());
state.setPointStoreState(pointStoreState);
}
List<CompactSamplerState> samplerStates = null;
if (saveSamplerStateEnabled) {
samplerStates = new ArrayList<>();
}
List<ITree<Integer, ?>> trees = null;
if (saveTreeStateEnabled) {
trees = new ArrayList<>();
}
CompactSamplerMapper samplerMapper = new CompactSamplerMapper();
samplerMapper.setCompressionEnabled(compressionEnabled);
for (IComponentModel<?, ?> component : forest.getComponents()) {
SamplerPlusTree<Integer, ?> samplerPlusTree = (SamplerPlusTree<Integer, ?>) component;
CompactSampler sampler = (CompactSampler) samplerPlusTree.getSampler();
if (samplerStates != null) {
samplerStates.add(samplerMapper.toState(sampler));
}
if (trees != null) {
trees.add(samplerPlusTree.getTree());
}
}
state.setCompactSamplerStates(samplerStates);
if (trees != null) {
RandomCutTreeMapper treeMapper = new RandomCutTreeMapper();
List<CompactRandomCutTreeState> treeStates = trees.stream().map(t -> treeMapper.toState((RandomCutTree) t)).collect(Collectors.toList());
state.setCompactRandomCutTreeStates(treeStates);
}
return state;
}
Aggregations