Search in sources :

Example 1 with PointStoreState

use of com.amazon.randomcutforest.state.store.PointStoreState 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;
}
Also used : CommonUtils.checkNotNull(com.amazon.randomcutforest.CommonUtils.checkNotNull) Setter(lombok.Setter) Getter(lombok.Getter) Precision(com.amazon.randomcutforest.config.Precision) CompactSampler(com.amazon.randomcutforest.sampler.CompactSampler) Random(java.util.Random) SamplerPlusTree(com.amazon.randomcutforest.executor.SamplerPlusTree) RandomCutTree(com.amazon.randomcutforest.tree.RandomCutTree) ArrayList(java.util.ArrayList) PointStore(com.amazon.randomcutforest.store.PointStore) Weighted(com.amazon.randomcutforest.sampler.Weighted) Config(com.amazon.randomcutforest.config.Config) PointStoreMapper(com.amazon.randomcutforest.state.store.PointStoreMapper) IPointStore(com.amazon.randomcutforest.store.IPointStore) ComponentList(com.amazon.randomcutforest.ComponentList) PointStoreCoordinator(com.amazon.randomcutforest.executor.PointStoreCoordinator) IComponentModel(com.amazon.randomcutforest.IComponentModel) CompactRandomCutTreeContext(com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext) CompactSamplerState(com.amazon.randomcutforest.state.sampler.CompactSamplerState) CommonUtils.checkArgument(com.amazon.randomcutforest.CommonUtils.checkArgument) PointStoreState(com.amazon.randomcutforest.state.store.PointStoreState) Collectors(java.util.stream.Collectors) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ITree(com.amazon.randomcutforest.tree.ITree) List(java.util.List) RandomCutTreeMapper(com.amazon.randomcutforest.state.tree.RandomCutTreeMapper) CompactRandomCutTreeState(com.amazon.randomcutforest.state.tree.CompactRandomCutTreeState) CompactSamplerMapper(com.amazon.randomcutforest.state.sampler.CompactSamplerMapper) IStreamSampler(com.amazon.randomcutforest.sampler.IStreamSampler) CompactSampler(com.amazon.randomcutforest.sampler.CompactSampler) CompactSamplerMapper(com.amazon.randomcutforest.state.sampler.CompactSamplerMapper) PointStoreState(com.amazon.randomcutforest.state.store.PointStoreState) ITree(com.amazon.randomcutforest.tree.ITree) CompactRandomCutTreeState(com.amazon.randomcutforest.state.tree.CompactRandomCutTreeState) PointStoreCoordinator(com.amazon.randomcutforest.executor.PointStoreCoordinator) RandomCutTreeMapper(com.amazon.randomcutforest.state.tree.RandomCutTreeMapper) PointStoreMapper(com.amazon.randomcutforest.state.store.PointStoreMapper) CompactSamplerState(com.amazon.randomcutforest.state.sampler.CompactSamplerState) SamplerPlusTree(com.amazon.randomcutforest.executor.SamplerPlusTree)

Aggregations

CommonUtils.checkArgument (com.amazon.randomcutforest.CommonUtils.checkArgument)1 CommonUtils.checkNotNull (com.amazon.randomcutforest.CommonUtils.checkNotNull)1 ComponentList (com.amazon.randomcutforest.ComponentList)1 IComponentModel (com.amazon.randomcutforest.IComponentModel)1 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)1 Config (com.amazon.randomcutforest.config.Config)1 Precision (com.amazon.randomcutforest.config.Precision)1 PointStoreCoordinator (com.amazon.randomcutforest.executor.PointStoreCoordinator)1 SamplerPlusTree (com.amazon.randomcutforest.executor.SamplerPlusTree)1 CompactSampler (com.amazon.randomcutforest.sampler.CompactSampler)1 IStreamSampler (com.amazon.randomcutforest.sampler.IStreamSampler)1 Weighted (com.amazon.randomcutforest.sampler.Weighted)1 CompactSamplerMapper (com.amazon.randomcutforest.state.sampler.CompactSamplerMapper)1 CompactSamplerState (com.amazon.randomcutforest.state.sampler.CompactSamplerState)1 PointStoreMapper (com.amazon.randomcutforest.state.store.PointStoreMapper)1 PointStoreState (com.amazon.randomcutforest.state.store.PointStoreState)1 CompactRandomCutTreeContext (com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext)1 CompactRandomCutTreeState (com.amazon.randomcutforest.state.tree.CompactRandomCutTreeState)1 RandomCutTreeMapper (com.amazon.randomcutforest.state.tree.RandomCutTreeMapper)1 IPointStore (com.amazon.randomcutforest.store.IPointStore)1