|
| 1 | +/* |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.randomcutforest.parkservices; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 19 | + |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.time.temporal.ChronoUnit; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.TreeSet; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import com.amazon.randomcutforest.config.ForestMode; |
| 32 | +import com.amazon.randomcutforest.config.Precision; |
| 33 | +import com.amazon.randomcutforest.config.TransformMethod; |
| 34 | + |
| 35 | +public class IgnoreTest { |
| 36 | + @Test |
| 37 | + public void testAnomalies() { |
| 38 | + // Initialize the forest parameters |
| 39 | + int shingleSize = 8; |
| 40 | + int numberOfTrees = 50; |
| 41 | + int sampleSize = 256; |
| 42 | + Precision precision = Precision.FLOAT_32; |
| 43 | + int baseDimensions = 1; |
| 44 | + |
| 45 | + long count = 0; |
| 46 | + int dimensions = baseDimensions * shingleSize; |
| 47 | + |
| 48 | + // Build the ThresholdedRandomCutForest |
| 49 | + ThresholdedRandomCutForest forest = new ThresholdedRandomCutForest.Builder<>().compact(true) |
| 50 | + .dimensions(dimensions).randomSeed(0).numberOfTrees(numberOfTrees).shingleSize(shingleSize) |
| 51 | + .sampleSize(sampleSize).precision(precision).anomalyRate(0.01).forestMode(ForestMode.STREAMING_IMPUTE) |
| 52 | + .transformMethod(TransformMethod.NORMALIZE).autoAdjust(true) |
| 53 | + .ignoreNearExpectedFromAboveByRatio(new double[] { 0.1 }) |
| 54 | + .ignoreNearExpectedFromBelowByRatio(new double[] { 0.1 }).build(); |
| 55 | + |
| 56 | + // Generate the list of doubles |
| 57 | + List<Double> randomDoubles = generateUniformRandomDoubles(); |
| 58 | + |
| 59 | + // List to store detected anomaly indices |
| 60 | + List<Integer> anomalies = new ArrayList<>(); |
| 61 | + |
| 62 | + // Process each data point through the forest |
| 63 | + for (double val : randomDoubles) { |
| 64 | + double[] point = new double[] { val }; |
| 65 | + long newStamp = 100 * count; |
| 66 | + |
| 67 | + AnomalyDescriptor result = forest.process(point, newStamp); |
| 68 | + |
| 69 | + if (result.getAnomalyGrade() != 0) { |
| 70 | + anomalies.add((int) count); |
| 71 | + } |
| 72 | + ++count; |
| 73 | + } |
| 74 | + |
| 75 | + // Expected anomalies |
| 76 | + List<Integer> expectedAnomalies = Arrays.asList(273, 283, 505, 1323); |
| 77 | + |
| 78 | + System.out.println("Anomalies detected at indices: " + anomalies); |
| 79 | + |
| 80 | + // Verify that all expected anomalies are detected |
| 81 | + assertTrue(anomalies.containsAll(expectedAnomalies), |
| 82 | + "Anomalies detected do not contain all expected anomalies"); |
| 83 | + } |
| 84 | + |
| 85 | + public static List<Double> generateUniformRandomDoubles() { |
| 86 | + // Set fixed times for reproducibility |
| 87 | + LocalDateTime startTime = LocalDateTime.of(2020, 1, 1, 0, 0, 0); |
| 88 | + LocalDateTime endTime = LocalDateTime.of(2020, 1, 2, 0, 0, 0); |
| 89 | + long totalIntervals = ChronoUnit.MINUTES.between(startTime, endTime); |
| 90 | + |
| 91 | + // Generate timestamps (not used but kept for completeness) |
| 92 | + List<LocalDateTime> timestamps = new ArrayList<>(); |
| 93 | + for (int i = 0; i < totalIntervals; i++) { |
| 94 | + timestamps.add(startTime.plusMinutes(i)); |
| 95 | + } |
| 96 | + |
| 97 | + // Initialize variables |
| 98 | + Random random = new Random(0); // For reproducibility |
| 99 | + double level = 0; |
| 100 | + List<Double> logCounts = new ArrayList<>(); |
| 101 | + |
| 102 | + // Decide random change points where level will change |
| 103 | + int numChanges = random.nextInt(6) + 5; // Random number between 5 and 10 inclusive |
| 104 | + |
| 105 | + Set<Integer> changeIndicesSet = new TreeSet<>(); |
| 106 | + changeIndicesSet.add(0); // Ensure the first index is included |
| 107 | + |
| 108 | + while (changeIndicesSet.size() < numChanges) { |
| 109 | + int idx = random.nextInt((int) totalIntervals - 1) + 1; // Random index between 1 and totalIntervals -1 |
| 110 | + changeIndicesSet.add(idx); |
| 111 | + } |
| 112 | + |
| 113 | + List<Integer> changeIndices = new ArrayList<>(changeIndicesSet); |
| 114 | + |
| 115 | + // Generate levels at each change point |
| 116 | + List<Double> levels = new ArrayList<>(); |
| 117 | + for (int i = 0; i < changeIndices.size(); i++) { |
| 118 | + if (i == 0) { |
| 119 | + level = random.nextDouble() * 10; // Starting level between 0 and 10 |
| 120 | + } else { |
| 121 | + double increment = -2 + random.nextDouble() * 7; // Random increment between -2 and 5 |
| 122 | + level = Math.max(0, level + increment); |
| 123 | + } |
| 124 | + levels.add(level); |
| 125 | + } |
| 126 | + |
| 127 | + // Now generate logCounts for each timestamp with even smoother transitions |
| 128 | + int currentLevelIndex = 0; |
| 129 | + for (int idx = 0; idx < totalIntervals; idx++) { |
| 130 | + if (currentLevelIndex + 1 < changeIndices.size() && idx >= changeIndices.get(currentLevelIndex + 1)) { |
| 131 | + currentLevelIndex++; |
| 132 | + } |
| 133 | + level = levels.get(currentLevelIndex); |
| 134 | + double sineWave = Math.sin((idx % 300) * (Math.PI / 150)) * 0.05 * level; |
| 135 | + double noise = (-0.01 * level) + random.nextDouble() * (0.02 * level); // Noise between -0.01*level and |
| 136 | + // 0.01*level |
| 137 | + double count = Math.max(0, level + sineWave + noise); |
| 138 | + logCounts.add(count); |
| 139 | + } |
| 140 | + |
| 141 | + // Introduce controlled changes for anomaly detection testing |
| 142 | + for (int changeIdx : changeIndices) { |
| 143 | + if (changeIdx + 10 < totalIntervals) { |
| 144 | + logCounts.set(changeIdx + 5, logCounts.get(changeIdx + 5) * 1.05); // 5% increase |
| 145 | + logCounts.set(changeIdx + 10, logCounts.get(changeIdx + 10) * 1.10); // 10% increase |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Output the generated logCounts |
| 150 | + System.out.println("Generated logCounts of size: " + logCounts.size()); |
| 151 | + return logCounts; |
| 152 | + } |
| 153 | +} |
0 commit comments