Search in sources :

Example 1 with IStreamSampler

use of com.amazon.randomcutforest.sampler.IStreamSampler 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);
}
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) RandomCutTree(com.amazon.randomcutforest.tree.RandomCutTree) CompactRandomCutTreeContext(com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext) CompactSamplerMapper(com.amazon.randomcutforest.state.sampler.CompactSamplerMapper) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ComponentList(com.amazon.randomcutforest.ComponentList) 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) Random(java.util.Random)

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