use of com.amazon.randomcutforest.tree.RandomCutTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestMapper method toModel.
/**
* Create a {@link RandomCutForest} instance from a
* {@link RandomCutForestState}. If the state contains tree states, then trees
* will be constructed from the tree state objects. Otherwise, empty trees are
* created and populated from the sampler data. The resulting forest should be
* equal in distribution to the forest that the state object was created from.
*
* @param state A Random Cut Forest state object.
* @param executionContext An executor context that will be used to initialize
* new executors in the Random Cut Forest. If this
* argument is null, then the mapper will look for an
* executor context in the state object.
* @param seed A random seed.
* @return A Random Cut Forest corresponding to the state object.
* @throws NullPointerException if both the {@code executorContext} method
* argument and the executor context field in the
* state object are null.
*/
public RandomCutForest toModel(RandomCutForestState state, ExecutionContext executionContext, long seed) {
ExecutionContext ec;
if (executionContext != null) {
ec = executionContext;
} else {
checkNotNull(state.getExecutionContext(), "The executor context in the state object is null, an executor context must be passed explicitly to toModel()");
ec = state.getExecutionContext();
}
RandomCutForest.Builder<?> builder = RandomCutForest.builder().numberOfTrees(state.getNumberOfTrees()).dimensions(state.getDimensions()).timeDecay(state.getTimeDecay()).sampleSize(state.getSampleSize()).centerOfMassEnabled(state.isCenterOfMassEnabled()).outputAfter(state.getOutputAfter()).parallelExecutionEnabled(ec.isParallelExecutionEnabled()).threadPoolSize(ec.getThreadPoolSize()).storeSequenceIndexesEnabled(state.isStoreSequenceIndexesEnabled()).shingleSize(state.getShingleSize()).boundingBoxCacheFraction(state.getBoundingBoxCacheFraction()).compact(state.isCompact()).internalShinglingEnabled(state.isInternalShinglingEnabled()).randomSeed(seed);
if (Precision.valueOf(state.getPrecision()) == Precision.FLOAT_32) {
return singlePrecisionForest(builder, state, null, null, null);
}
Random random = builder.getRandom();
PointStore pointStore = new PointStoreMapper().convertFromDouble(state.getPointStoreState());
ComponentList<Integer, float[]> components = new ComponentList<>();
PointStoreCoordinator<float[]> coordinator = new PointStoreCoordinator<>(pointStore);
coordinator.setTotalUpdates(state.getTotalUpdates());
CompactRandomCutTreeContext context = new CompactRandomCutTreeContext();
context.setPointStore(pointStore);
context.setMaxSize(state.getSampleSize());
checkArgument(state.isSaveSamplerStateEnabled(), " conversion cannot proceed without samplers");
List<CompactSamplerState> samplerStates = state.getCompactSamplerStates();
CompactSamplerMapper samplerMapper = new CompactSamplerMapper();
for (int i = 0; i < state.getNumberOfTrees(); i++) {
CompactSampler compactData = samplerMapper.toModel(samplerStates.get(i));
RandomCutTree tree = RandomCutTree.builder().capacity(state.getSampleSize()).pointStoreView(pointStore).storeSequenceIndexesEnabled(state.isStoreSequenceIndexesEnabled()).outputAfter(state.getOutputAfter()).centerOfMassEnabled(state.isCenterOfMassEnabled()).randomSeed(random.nextLong()).build();
CompactSampler sampler = CompactSampler.builder().capacity(state.getSampleSize()).timeDecay(state.getTimeDecay()).randomSeed(random.nextLong()).build();
sampler.setMaxSequenceIndex(compactData.getMaxSequenceIndex());
sampler.setMostRecentTimeDecayUpdate(compactData.getMostRecentTimeDecayUpdate());
for (Weighted<Integer> sample : compactData.getWeightedSample()) {
Integer reference = sample.getValue();
Integer newReference = tree.addPoint(reference, sample.getSequenceIndex());
if (newReference.intValue() != reference.intValue()) {
pointStore.incrementRefCount(newReference);
pointStore.decrementRefCount(reference);
}
sampler.addPoint(newReference, sample.getWeight(), sample.getSequenceIndex());
}
components.add(new SamplerPlusTree<>(sampler, tree));
}
return new RandomCutForest(builder, coordinator, components, random);
}
use of com.amazon.randomcutforest.tree.RandomCutTree 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;
}
use of com.amazon.randomcutforest.tree.RandomCutTree in project random-cut-forest-by-aws by aws.
the class RandomCutTreeMapper method toModel.
@Override
public RandomCutTree toModel(CompactRandomCutTreeState state, CompactRandomCutTreeContext context, long seed) {
AbstractNodeStoreMapper nodeStoreMapper = new AbstractNodeStoreMapper();
nodeStoreMapper.setRoot(state.getRoot());
AbstractNodeStore nodeStore = nodeStoreMapper.toModel(state.getNodeStoreState(), context);
int dimension = (state.getDimensions() != 0) ? state.getDimensions() : context.getPointStore().getDimensions();
// boundingBoxcache is not set deliberately;
// it should be set after the partial tree is complete
RandomCutTree tree = new RandomCutTree.Builder().dimension(dimension).storeSequenceIndexesEnabled(state.isStoreSequenceIndexesEnabled()).capacity(state.getMaxSize()).setRoot(state.getRoot()).randomSeed(state.getSeed()).pointStoreView(context.getPointStore()).nodeStore(nodeStore).centerOfMassEnabled(state.isCenterOfMassEnabled()).outputAfter(state.getOutputAfter()).build();
return tree;
}
use of com.amazon.randomcutforest.tree.RandomCutTree in project random-cut-forest-by-aws by aws.
the class RandomCutForestTest method setUp.
@BeforeEach
public void setUp() {
dimensions = 2;
sampleSize = 256;
numberOfTrees = 10;
components = new ComponentList<>();
for (int i = 0; i < numberOfTrees; i++) {
CompactSampler sampler = mock(CompactSampler.class);
when(sampler.getCapacity()).thenReturn(sampleSize);
RandomCutTree tree = mock(RandomCutTree.class);
components.add(spy(new SamplerPlusTree<>(sampler, tree)));
}
updateCoordinator = spy(new PointStoreCoordinator<>(new PointStore.Builder().dimensions(2).capacity(1).build()));
traversalExecutor = spy(new SequentialForestTraversalExecutor(components));
updateExecutor = spy(new SequentialForestUpdateExecutor<>(updateCoordinator, components));
RandomCutForest.Builder<?> builder = RandomCutForest.builder().dimensions(dimensions).numberOfTrees(numberOfTrees).sampleSize(sampleSize);
forest = spy(new RandomCutForest(builder, updateCoordinator, components, builder.getRandom()));
Whitebox.setInternalState(forest, "traversalExecutor", traversalExecutor);
Whitebox.setInternalState(forest, "updateExecutor", updateExecutor);
}
Aggregations