use of com.amazon.randomcutforest.state.tree.CompactRandomCutTreeContext 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.state.tree.CompactRandomCutTreeContext 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);
}
Aggregations