public RandomCutForest toModel()

in Java/core/src/main/java/com/amazon/randomcutforest/state/RandomCutForestMapper.java [211:271]


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