Search in sources :

Example 1 with Preprocessor

use of com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor in project random-cut-forest-by-aws by aws.

the class ThresholdedRandomCutForestMapper method toState.

@Override
public ThresholdedRandomCutForestState toState(ThresholdedRandomCutForest model) {
    ThresholdedRandomCutForestState state = new ThresholdedRandomCutForestState();
    RandomCutForestMapper randomCutForestMapper = new RandomCutForestMapper();
    randomCutForestMapper.setPartialTreeStateEnabled(true);
    randomCutForestMapper.setSaveTreeStateEnabled(true);
    randomCutForestMapper.setCompressionEnabled(true);
    randomCutForestMapper.setSaveCoordinatorStateEnabled(true);
    randomCutForestMapper.setSaveExecutorContextEnabled(true);
    state.setForestState(randomCutForestMapper.toState(model.getForest()));
    BasicThresholderMapper thresholderMapper = new BasicThresholderMapper();
    state.setThresholderState(thresholderMapper.toState(model.getThresholder()));
    PreprocessorMapper preprocessorMapper = new PreprocessorMapper();
    state.setPreprocessorStates(new PreprocessorState[] { preprocessorMapper.toState((Preprocessor) model.getPreprocessor()) });
    state.setTriggerFactor(model.getPredictorCorrector().getTriggerFactor());
    state.setIgnoreSimilar(model.getPredictorCorrector().isIgnoreSimilar());
    state.setIgnoreSimilarFactor(model.getPredictorCorrector().getIgnoreSimilarFactor());
    state.setNumberOfAttributors(model.getPredictorCorrector().getNumberOfAttributors());
    state.setForestMode(model.getForestMode().name());
    state.setTransformMethod(model.getTransformMethod().name());
    IRCFComputeDescriptor descriptor = model.getLastAnomalyDescriptor();
    state.setLastAnomalyTimeStamp(descriptor.getInternalTimeStamp());
    state.setLastAnomalyScore(descriptor.getRCFScore());
    state.setLastAnomalyAttribution(new DiVectorMapper().toState(descriptor.getAttribution()));
    state.setLastAnomalyPoint(descriptor.getRCFPoint());
    state.setLastExpectedPoint(descriptor.getExpectedRCFPoint());
    state.setLastRelativeIndex(descriptor.getRelativeIndex());
    return state;
}
Also used : IRCFComputeDescriptor(com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor) BasicThresholderMapper(com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) DiVectorMapper(com.amazon.randomcutforest.state.returntypes.DiVectorMapper) Preprocessor(com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor) PreprocessorMapper(com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper)

Example 2 with Preprocessor

use of com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor in project random-cut-forest-by-aws by aws.

the class PreprocessorMapper method toModel.

@Override
public Preprocessor toModel(PreprocessorState state, long seed) {
    DeviationMapper deviationMapper = new DeviationMapper();
    Deviation timeStampDeviation = deviationMapper.toModel(state.getTimeStampDeviationState());
    Deviation dataQuality = deviationMapper.toModel(state.getDataQualityState());
    Deviation[] deviations = null;
    if (state.getDeviationStates() != null) {
        deviations = new Deviation[state.getDeviationStates().length];
        for (int i = 0; i < state.getDeviationStates().length; i++) {
            deviations[i] = deviationMapper.toModel(state.getDeviationStates()[i]);
        }
    }
    Preprocessor.Builder<?> preprocessorBuilder = new Preprocessor.Builder<>().forestMode(ForestMode.valueOf(state.getForestMode())).shingleSize(state.getShingleSize()).dimensions(state.getDimensions()).normalizeTime(state.isNormalizeTime()).imputationMethod(ImputationMethod.valueOf(state.getImputationMethod())).fillValues(state.getDefaultFill()).inputLength(state.getInputLength()).weights(state.getWeights()).transformMethod(TransformMethod.valueOf(state.getTransformMethod())).startNormalization(state.getStartNormalization()).useImputedFraction(state.getUseImputedFraction()).timeDeviation(timeStampDeviation).dataQuality(dataQuality);
    if (deviations != null) {
        preprocessorBuilder.deviations(deviations);
    }
    Preprocessor preprocessor = preprocessorBuilder.build();
    preprocessor.setInitialValues(state.getInitialValues());
    preprocessor.setInitialTimeStamps(state.getInitialTimeStamps());
    preprocessor.setClipFactor(state.getClipFactor());
    preprocessor.setValuesSeen(state.getValuesSeen());
    preprocessor.setInternalTimeStamp(state.getInternalTimeStamp());
    preprocessor.setLastShingledInput(state.getLastShingledInput());
    preprocessor.setLastShingledPoint(state.getLastShingledPoint());
    preprocessor.setPreviousTimeStamps(state.getPreviousTimeStamps());
    preprocessor.setNormalizeTime(state.isNormalizeTime());
    return preprocessor;
}
Also used : DeviationMapper(com.amazon.randomcutforest.parkservices.state.statistics.DeviationMapper) Preprocessor(com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor) Deviation(com.amazon.randomcutforest.parkservices.statistics.Deviation)

Example 3 with Preprocessor

use of com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor in project random-cut-forest-by-aws by aws.

the class ThresholdedRandomCutForestMapper method toModel.

@Override
public ThresholdedRandomCutForest toModel(ThresholdedRandomCutForestState state, long seed) {
    RandomCutForestMapper randomCutForestMapper = new RandomCutForestMapper();
    BasicThresholderMapper thresholderMapper = new BasicThresholderMapper();
    PreprocessorMapper preprocessorMapper = new PreprocessorMapper();
    RandomCutForest forest = randomCutForestMapper.toModel(state.getForestState());
    BasicThresholder thresholder = thresholderMapper.toModel(state.getThresholderState());
    Preprocessor preprocessor = preprocessorMapper.toModel(state.getPreprocessorStates()[0]);
    ForestMode forestMode = ForestMode.valueOf(state.getForestMode());
    TransformMethod transformMethod = TransformMethod.valueOf(state.getTransformMethod());
    RCFComputeDescriptor descriptor = new RCFComputeDescriptor(null, 0L);
    descriptor.setRCFScore(state.getLastAnomalyScore());
    descriptor.setInternalTimeStamp(state.getLastAnomalyTimeStamp());
    descriptor.setAttribution(new DiVectorMapper().toModel(state.getLastAnomalyAttribution()));
    descriptor.setRCFPoint(state.getLastAnomalyPoint());
    descriptor.setExpectedRCFPoint(state.getLastExpectedPoint());
    descriptor.setRelativeIndex(state.getLastRelativeIndex());
    descriptor.setForestMode(forestMode);
    descriptor.setTransformMethod(transformMethod);
    descriptor.setImputationMethod(ImputationMethod.valueOf(state.getPreprocessorStates()[0].getImputationMethod()));
    PredictorCorrector predictorCorrector = new PredictorCorrector(thresholder);
    predictorCorrector.setIgnoreSimilar(state.isIgnoreSimilar());
    predictorCorrector.setIgnoreSimilarFactor(state.getIgnoreSimilarFactor());
    predictorCorrector.setTriggerFactor(state.getTriggerFactor());
    predictorCorrector.setNumberOfAttributors(state.getNumberOfAttributors());
    return new ThresholdedRandomCutForest(forestMode, transformMethod, forest, predictorCorrector, preprocessor, descriptor);
}
Also used : ForestMode(com.amazon.randomcutforest.config.ForestMode) BasicThresholderMapper(com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper) PredictorCorrector(com.amazon.randomcutforest.parkservices.PredictorCorrector) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest) DiVectorMapper(com.amazon.randomcutforest.state.returntypes.DiVectorMapper) Preprocessor(com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor) PreprocessorMapper(com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper) TransformMethod(com.amazon.randomcutforest.config.TransformMethod) IRCFComputeDescriptor(com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor) RCFComputeDescriptor(com.amazon.randomcutforest.parkservices.RCFComputeDescriptor) BasicThresholder(com.amazon.randomcutforest.parkservices.threshold.BasicThresholder) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)

Example 4 with Preprocessor

use of com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor in project random-cut-forest-by-aws by aws.

the class ThresholdedRandomCutForestTest method testConfigStandard.

@Test
public void testConfigStandard() {
    int sampleSize = 256;
    int baseDimensions = 2;
    int shingleSize = 4;
    int dimensions = baseDimensions * shingleSize;
    long seed = new Random().nextLong();
    // have to enable internal shingling or keep it unfixed
    assertThrows(IllegalArgumentException.class, () -> ThresholdedRandomCutForest.builder().compact(true).dimensions(dimensions).precision(Precision.FLOAT_32).randomSeed(seed).forestMode(ForestMode.STANDARD).useImputedFraction(0.5).internalShinglingEnabled(false).shingleSize(shingleSize).anomalyRate(0.01).build());
    assertDoesNotThrow(() -> {
        ThresholdedRandomCutForest.builder().compact(true).dimensions(dimensions).precision(Precision.FLOAT_32).randomSeed(seed).forestMode(ForestMode.STANDARD).internalShinglingEnabled(false).shingleSize(shingleSize).anomalyRate(0.01).build();
    });
    assertThrows(IllegalArgumentException.class, () -> {
        ThresholdedRandomCutForest forest = ThresholdedRandomCutForest.builder().compact(true).dimensions(dimensions).precision(Precision.FLOAT_32).randomSeed(seed).forestMode(ForestMode.STANDARD).shingleSize(shingleSize).anomalyRate(0.01).transformMethod(NORMALIZE).startNormalization(111).stopNormalization(100).build();
    });
    ThresholdedRandomCutForest forest = ThresholdedRandomCutForest.builder().compact(true).dimensions(dimensions).precision(Precision.FLOAT_32).randomSeed(seed).forestMode(ForestMode.STANDARD).shingleSize(shingleSize).anomalyRate(0.01).transformMethod(NORMALIZE).startNormalization(111).stopNormalization(111).build();
    // left to false
    assertFalse(forest.getForest().isInternalShinglingEnabled());
    assertEquals(((Preprocessor) forest.getPreprocessor()).getInitialValues().length, 111);
    assertEquals(((Preprocessor) forest.getPreprocessor()).getInitialTimeStamps().length, 111);
    assertEquals(((Preprocessor) forest.getPreprocessor()).getStopNormalization(), 111);
    assertEquals(((Preprocessor) forest.getPreprocessor()).getStartNormalization(), 111);
}
Also used : Random(java.util.Random) Preprocessor(com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Preprocessor (com.amazon.randomcutforest.parkservices.preprocessor.Preprocessor)4 IRCFComputeDescriptor (com.amazon.randomcutforest.parkservices.IRCFComputeDescriptor)2 PreprocessorMapper (com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorMapper)2 BasicThresholderMapper (com.amazon.randomcutforest.parkservices.state.threshold.BasicThresholderMapper)2 RandomCutForestMapper (com.amazon.randomcutforest.state.RandomCutForestMapper)2 DiVectorMapper (com.amazon.randomcutforest.state.returntypes.DiVectorMapper)2 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)1 ForestMode (com.amazon.randomcutforest.config.ForestMode)1 TransformMethod (com.amazon.randomcutforest.config.TransformMethod)1 PredictorCorrector (com.amazon.randomcutforest.parkservices.PredictorCorrector)1 RCFComputeDescriptor (com.amazon.randomcutforest.parkservices.RCFComputeDescriptor)1 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)1 DeviationMapper (com.amazon.randomcutforest.parkservices.state.statistics.DeviationMapper)1 Deviation (com.amazon.randomcutforest.parkservices.statistics.Deviation)1 BasicThresholder (com.amazon.randomcutforest.parkservices.threshold.BasicThresholder)1 Random (java.util.Random)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1