-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathIidAnomalyDetectionBase.cs
74 lines (61 loc) · 2.84 KB
/
IidAnomalyDetectionBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.Internal.Utilities;
using Microsoft.ML.Runtime.Model;
namespace Microsoft.ML.Runtime.TimeSeriesProcessing
{
/// <summary>
/// This transform computes the p-values and martingale scores for a supposedly i.i.d input sequence of floats. In other words, it assumes
/// the input sequence represents the raw anomaly score which might have been computed via another process.
/// </summary>
public abstract class IidAnomalyDetectionBase : SequentialAnomalyDetectionTransformBase<Single, IidAnomalyDetectionBase.State>
{
public IidAnomalyDetectionBase(ArgumentsBase args, string name, IHostEnvironment env)
: base(args, name, env)
{
InitialWindowSize = 0;
}
public IidAnomalyDetectionBase(IHostEnvironment env, ModelLoadContext ctx, string name)
: base(env, ctx, name)
{
Host.CheckDecode(InitialWindowSize == 0);
}
public override Schema GetOutputSchema(Schema inputSchema)
{
Host.CheckValue(inputSchema, nameof(inputSchema));
if (!inputSchema.TryGetColumnIndex(InputColumnName, out var col))
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", InputColumnName);
var colType = inputSchema.GetColumnType(col);
if (colType != NumberType.R4)
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", InputColumnName, NumberType.R4.ToString(), colType.ToString());
return Transform(new EmptyDataView(Host, inputSchema)).Schema;
}
public override void Save(ModelSaveContext ctx)
{
ctx.CheckAtModel();
Host.Assert(InitialWindowSize == 0);
// *** Binary format ***
// <base>
base.Save(ctx);
}
public sealed class State : AnomalyDetectionStateBase
{
protected override void LearnStateFromDataCore(FixedSizeQueue<Single> data)
{
// This method is empty because there is no need for initial tuning for this transform.
}
protected override void InitializeAnomalyDetector()
{
// This method is empty because there is no need for any extra initialization for this transform.
}
protected override double ComputeRawAnomalyScore(ref Single input, FixedSizeQueue<Single> windowedBuffer, long iteration)
{
// This transform treats the input sequenence as the raw anomaly score.
return (double)input;
}
}
}
}