public void TestSrCnnAnomalyDetectorWithSeasonalAnomalyData( [CombinatorialValues(SrCnnDeseasonalityMode.Stl, SrCnnDeseasonalityMode.Mean, SrCnnDeseasonalityMode.Median)] SrCnnDeseasonalityMode mode ) { var ml = new MLContext(1); IDataView dataView; var dataPath = GetDataPath("Timeseries", "period_anomaly.csv"); // Load data from file into the dataView dataView = ml.Data.LoadFromTextFile <TimeSeriesDataDouble>(dataPath, hasHeader: true); // Setup the detection arguments string outputColumnName = nameof(SrCnnAnomalyDetection.Prediction); string inputColumnName = nameof(TimeSeriesDataDouble.Value); // Do batch anomaly detection var options = new SrCnnEntireAnomalyDetectorOptions() { Threshold = 0.23, BatchSize = -1, Sensitivity = 53.0, DetectMode = SrCnnDetectMode.AnomalyAndMargin, Period = 288, DeseasonalityMode = mode }; var outputDataView = ml.AnomalyDetection.DetectEntireAnomalyBySrCnn(dataView, outputColumnName, inputColumnName, options); // Getting the data of the newly created column as an IEnumerable of SrCnnAnomalyDetection. var predictionColumn = ml.Data.CreateEnumerable <SrCnnAnomalyDetection>( outputDataView, reuseRowObject: false); var anomalyStartIndex = 2988; var anomalyEndIndex = 3095; int k = 0; foreach (var prediction in predictionColumn) { Assert.Equal(7, prediction.Prediction.Length); if (anomalyStartIndex <= k && k <= anomalyEndIndex) { Assert.Equal(1, prediction.Prediction[0]); } else { Assert.Equal(0, prediction.Prediction[0]); } ++k; } }
public SrCnnEntireModeler(double threshold, double sensitivity, SrCnnDetectMode detectMode, int period, SrCnnDeseasonalityMode deseasonalityMode) { _threshold = threshold; _sensitivity = sensitivity; _detectMode = detectMode; _period = period; _predictArray = new double[_lookaheadWindowSize + 1]; switch (deseasonalityMode) { case SrCnnDeseasonalityMode.Stl: _deseasonalityFunction = new StlDeseasonality(); break; case SrCnnDeseasonalityMode.Mean: _deseasonalityFunction = new MeanDeseasonality(); break; default: Contracts.Assert(deseasonalityMode == SrCnnDeseasonalityMode.Median); _deseasonalityFunction = new MedianDeseasonality(); break; } }
public Batch(int batchSize, int outputLength, double threshold, double sensitivity, SrCnnDetectMode detectMode, int period, SrCnnDeseasonalityMode deseasonalityMode) { _batchSize = batchSize; _outputLength = outputLength; if (batchSize == -1) { _previousBatch = new List <double>(); _batch = new List <double>(); } else { _previousBatch = new List <double>(batchSize); _batch = new List <double>(batchSize); } _modeler = new SrCnnEntireModeler(threshold, sensitivity, detectMode, period, deseasonalityMode); }