Ejemplo n.º 1
0
 public SrCnnEntireModeler(double threshold, double sensitivity, SrCnnDetectMode detectMode)
 {
     _threshold    = threshold;
     _sensitivity  = sensitivity;
     _detectMode   = detectMode;
     _predictArray = new double[_lookaheadWindowSize + 1];
 }
Ejemplo n.º 2
0
        public SrCnnEntireAnomalyDetector(IHostEnvironment env, IDataView input, string inputColumnName, string outputColumnName, double threshold, int batchSize, double sensitivity, SrCnnDetectMode detectMode)
            : base(env, nameof(SrCnnEntireAnomalyDetector), input)
        {
            Host.CheckValue(inputColumnName, nameof(inputColumnName));
            _inputColumnName = inputColumnName;

            Host.CheckUserArg(batchSize == -1 || batchSize >= MinBatchSize, nameof(batchSize), "BatchSize must be -1 or no less than 12.");
            _batchSize = batchSize;

            Host.CheckUserArg(threshold >= 0 && threshold <= 1, nameof(threshold), "Must be in [0,1].");
            Host.CheckUserArg(detectMode == SrCnnDetectMode.AnomalyOnly ||
                              detectMode == SrCnnDetectMode.AnomalyAndExpectedValue ||
                              detectMode == SrCnnDetectMode.AnomalyAndMargin, nameof(detectMode), "Invalid detectMode");

            Host.CheckUserArg(sensitivity >= 0 && sensitivity <= 100, nameof(sensitivity), "Must be in [0,100].");
            _outputLength = _outputLengthArray[(int)detectMode];
            _threshold    = threshold;
            _sensitivity  = sensitivity;
            _detectMode   = detectMode;

            _bindings = new Bindings(input.Schema, inputColumnName, outputColumnName, new VectorDataViewType(NumberDataViewType.Double, _outputLength));
        }
Ejemplo n.º 3
0
            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 void TestSrCnnBatchAnomalyDetector(
            [CombinatorialValues(SrCnnDetectMode.AnomalyOnly, SrCnnDetectMode.AnomalyAndExpectedValue, SrCnnDetectMode.AnomalyAndMargin)] SrCnnDetectMode mode,
            [CombinatorialValues(true, false)] bool loadDataFromFile,
            [CombinatorialValues(-1, 24, 26, 512)] int batchSize)
        {
            var       ml = new MLContext(1);
            IDataView dataView;

            if (loadDataFromFile)
            {
                var dataPath = GetDataPath("Timeseries", "anomaly_detection.csv");

                // Load data from file into the dataView
                dataView = ml.Data.LoadFromTextFile <TimeSeriesDataDouble>(dataPath, hasHeader: true);
            }
            else
            {
                // Generate sample series data with an anomaly
                var data = new List <TimeSeriesDataDouble>();
                for (int index = 0; index < 20; index++)
                {
                    data.Add(new TimeSeriesDataDouble {
                        Value = 5
                    });
                }
                data.Add(new TimeSeriesDataDouble {
                    Value = 10
                });
                for (int index = 0; index < 5; index++)
                {
                    data.Add(new TimeSeriesDataDouble {
                        Value = 5
                    });
                }

                // Convert data to IDataView.
                dataView = ml.Data.LoadFromEnumerable(data);
            }

            // Setup the detection arguments
            string outputColumnName = nameof(SrCnnAnomalyDetection.Prediction);
            string inputColumnName  = nameof(TimeSeriesDataDouble.Value);

            // Do batch anomaly detection
            var outputDataView = ml.AnomalyDetection.DetectEntireAnomalyBySrCnn(dataView, outputColumnName, inputColumnName,
                                                                                threshold: 0.35, batchSize: batchSize, sensitivity: 90.0, mode);

            // Getting the data of the newly created column as an IEnumerable of
            // SrCnnAnomalyDetection.
            var predictionColumn = ml.Data.CreateEnumerable <SrCnnAnomalyDetection>(
                outputDataView, reuseRowObject: false);

            int k = 0;

            foreach (var prediction in predictionColumn)
            {
                switch (mode)
                {
                case SrCnnDetectMode.AnomalyOnly:
                    Assert.Equal(3, prediction.Prediction.Length);
                    if (k == 20)
                    {
                        Assert.Equal(1, prediction.Prediction[0]);
                    }
                    else
                    {
                        Assert.Equal(0, prediction.Prediction[0]);
                    }
                    break;

                case SrCnnDetectMode.AnomalyAndExpectedValue:
                    Assert.Equal(4, prediction.Prediction.Length);
                    if (k == 20)
                    {
                        Assert.Equal(1, prediction.Prediction[0]);
                        Assert.Equal(5.00, prediction.Prediction[3], 2);
                    }
                    else
                    {
                        Assert.Equal(0, prediction.Prediction[0]);
                    }
                    break;

                case SrCnnDetectMode.AnomalyAndMargin:
                    Assert.Equal(7, prediction.Prediction.Length);
                    if (k == 20)
                    {
                        Assert.Equal(1, prediction.Prediction[0]);
                        Assert.Equal(5.00, prediction.Prediction[3], 2);
                        Assert.Equal(5.00, prediction.Prediction[4], 2);
                        Assert.Equal(5.01, prediction.Prediction[5], 2);
                        Assert.Equal(4.99, prediction.Prediction[6], 2);
                    }
                    else
                    {
                        Assert.Equal(0, prediction.Prediction[0]);
                    }
                    break;
                }
                k += 1;
            }
        }
Ejemplo n.º 5
0
 public Batch(int batchSize, int outputLength, double threshold, double sensitivity, SrCnnDetectMode detectMode)
 {
     _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);
 }
        /// <summary>
        /// Create <see cref="SrCnnEntireAnomalyDetector"/>, which detects timeseries anomalies for entire input using SRCNN algorithm.
        /// </summary>
        /// <param name="catalog">The AnomalyDetectionCatalog.</param>
        /// <param name="input">Input DataView.</param>
        /// <param name="outputColumnName">Name of the column resulting from data processing of <paramref name="inputColumnName"/>.
        /// The column data is a vector of <see cref="System.Double"/>. The length of this vector varies depending on <paramref name="detectMode"/>.</param>
        /// <param name="inputColumnName">Name of column to process. The column data must be <see cref="System.Double"/>.</param>
        /// <param name="threshold">The threshold to determine an anomaly. An anomaly is detected when the calculated SR raw score for a given point is more than the set threshold. This threshold must  fall between [0,1], and its default value is 0.3.</param>
        /// <param name="batchSize">Divide the input data into batches to fit srcnn model.
        /// When set to -1, use the whole input to fit model instead of batch by batch, when set to a positive integer, use this number as batch size.
        /// Must be -1 or a positive integer no less than 12. Default value is 1024.</param>
        /// <param name="sensitivity">Sensitivity of boundaries, only useful when srCnnDetectMode is AnomalyAndMargin. Must be in [0,100]. Default value is 99.</param>
        /// <param name="detectMode">An enum type of <see cref="SrCnnDetectMode"/>.
        /// When set to AnomalyOnly, the output vector would be a 3-element Double vector of (IsAnomaly, RawScore, Mag).
        /// When set to AnomalyAndExpectedValue, the output vector would be a 4-element Double vector of (IsAnomaly, RawScore, Mag, ExpectedValue).
        /// When set to AnomalyAndMargin, the output vector would be a 7-element Double vector of (IsAnomaly, AnomalyScore, Mag, ExpectedValue, BoundaryUnit, UpperBoundary, LowerBoundary).
        /// The RawScore is output by SR to determine whether a point is an anomaly or not, under AnomalyAndMargin mode, when a point is an anomaly, an AnomalyScore will be calculated according to sensitivity setting.
        /// Default value is AnomalyOnly.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!code-csharp[DetectEntireAnomalyBySrCnn](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectEntireAnomalyBySrCnn.cs)]
        /// ]]>
        /// </format>
        /// </example>
        public static IDataView DetectEntireAnomalyBySrCnn(this AnomalyDetectionCatalog catalog, IDataView input, string outputColumnName, string inputColumnName,
                                                           double threshold = 0.3, int batchSize = 1024, double sensitivity = 99, SrCnnDetectMode detectMode = SrCnnDetectMode.AnomalyOnly)
        {
            var options = new SrCnnEntireAnomalyDetectorOptions()
            {
                Threshold   = threshold,
                BatchSize   = batchSize,
                Sensitivity = sensitivity,
                DetectMode  = detectMode,
            };

            return(DetectEntireAnomalyBySrCnn(catalog, input, outputColumnName, inputColumnName, options));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Create <see cref="SrCnnEntireAnomalyDetector"/>, which detects timeseries anomalies for entire input using SRCNN algorithm.
 /// </summary>
 /// <param name="catalog">The AnomalyDetectionCatalog.</param>
 /// <param name="input">Input DataView.</param>
 /// <param name="outputColumnName">Name of the column resulting from data processing of <paramref name="inputColumnName"/>.
 /// The column data is a vector of <see cref="System.Double"/>. The length of this vector varies depending on <paramref name="detectMode"/>.</param>
 /// <param name="inputColumnName">Name of column to process. The column data must be <see cref="System.Double"/>.</param>
 /// <param name="threshold">The threshold to determine an anomaly. An anomaly is detected when the calculated SR raw score for a given point is more than the set threshold. This threshold must  fall between [0,1], and its default value is 0.3.</param>
 /// <param name="batchSize">Divide the input data into batches to fit srcnn model.
 /// When set to -1, use the whole input to fit model instead of batch by batch, when set to a positive integer, use this number as batch size.
 /// Must be -1 or a positive integer no less than 12. Default value is 1024.</param>
 /// <param name="sensitivity">Sensitivity of boundaries, only useful when srCnnDetectMode is AnomalyAndMargin. Must be in [0,100]. Default value is 99.</param>
 /// <param name="detectMode">An enum type of <see cref="SrCnnDetectMode"/>.
 /// When set to AnomalyOnly, the output vector would be a 3-element Double vector of (IsAnomaly, RawScore, Mag).
 /// When set to AnomalyAndExpectedValue, the output vector would be a 4-element Double vector of (IsAnomaly, RawScore, Mag, ExpectedValue).
 /// When set to AnomalyAndMargin, the output vector would be a 7-element Double vector of (IsAnomaly, AnomalyScore, Mag, ExpectedValue, BoundaryUnit, UpperBoundary, LowerBoundary).
 /// The RawScore is output by SR to determine whether a point is an anomaly or not, under AnomalyAndMargin mode, when a point is an anomaly, an AnomalyScore will be calculated according to sensitivity setting.
 /// Default value is AnomalyOnly.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[DetectEntireAnomalyBySrCnn](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectEntireAnomalyBySrCnn.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static IDataView DetectEntireAnomalyBySrCnn(this AnomalyDetectionCatalog catalog, IDataView input, string outputColumnName, string inputColumnName,
                                                    double threshold = 0.3, int batchSize = 1024, double sensitivity = 99, SrCnnDetectMode detectMode = SrCnnDetectMode.AnomalyOnly)
 => new SrCnnEntireAnomalyDetector(CatalogUtils.GetEnvironment(catalog), input, inputColumnName, outputColumnName, threshold, batchSize, sensitivity, detectMode);