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