Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using Microsoft.ML;
using Microsoft.ML.TimeSeries;

namespace Samples.Dynamic
{
public static class LocalizeRootCauseMultipleDimensions
{
private static string AGG_SYMBOL = "##SUM##";

public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var mlContext = new MLContext();

// Create an root cause localization input instance.
DateTime timestamp = GetTimestamp();
var data = new RootCauseLocalizationInput(timestamp, GetAnomalyDimension(), new List<MetricSlice>() { new MetricSlice(timestamp, GetPoints()) }, AggregateType.Sum, AGG_SYMBOL);

// Get the root cause localization result.
RootCauses prediction = mlContext.AnomalyDetection.LocalizeRootCauses(data);

// Print the localization result.
int count = 0;
foreach (RootCauseItem item in prediction.Causes[0].Items)
{
count++;
Console.WriteLine($"Root cause item #{count} ...");
Console.WriteLine($"Score: {item.Score}, Path: {String.Join(" ",item.Path)}, Direction: {item.Direction}, Dimension:{String.Join(" ", item.Dimension)}");
}

//Item #1 ...
//Score: 0.26670448876705927, Path: DataCenter, Direction: Up, Dimension:[Country, UK] [DeviceType, ##SUM##] [DataCenter, DC1]
}

private static List<Point> GetPoints()
{
List<Point> points = new List<Point>();

Dictionary<string, Object> dic1 = new Dictionary<string, Object>();
dic1.Add("Country", "UK");
dic1.Add("DeviceType", "Laptop");
dic1.Add("DataCenter", "DC1");
points.Add(new Point(200, 100, true, dic1));

Dictionary<string, Object> dic2 = new Dictionary<string, Object>();
dic2.Add("Country", "UK");
dic2.Add("DeviceType", "Mobile");
dic2.Add("DataCenter", "DC1");
points.Add(new Point(1000, 100, true, dic2));

Dictionary<string, Object> dic3 = new Dictionary<string, Object>();
dic3.Add("Country", "UK");
dic3.Add("DeviceType", AGG_SYMBOL);
dic3.Add("DataCenter", "DC1");
points.Add(new Point(1200, 200, true, dic3));

Dictionary<string, Object> dic4 = new Dictionary<string, Object>();
dic4.Add("Country", "UK");
dic4.Add("DeviceType", "Laptop");
dic4.Add("DataCenter", "DC2");
points.Add(new Point(100, 100, false, dic4));

Dictionary<string, Object> dic5 = new Dictionary<string, Object>();
dic5.Add("Country", "UK");
dic5.Add("DeviceType", "Mobile");
dic5.Add("DataCenter", "DC2");
points.Add(new Point(200, 200, false, dic5));

Dictionary<string, Object> dic6 = new Dictionary<string, Object>();
dic6.Add("Country", "UK");
dic6.Add("DeviceType", AGG_SYMBOL);
dic6.Add("DataCenter", "DC2");
points.Add(new Point(300, 300, false, dic6));

Dictionary<string, Object> dic7 = new Dictionary<string, Object>();
dic7.Add("Country", "UK");
dic7.Add("DeviceType", AGG_SYMBOL);
dic7.Add("DataCenter", AGG_SYMBOL);
points.Add(new Point(1500, 500, true, dic7));

Dictionary<string, Object> dic8 = new Dictionary<string, Object>();
dic8.Add("Country", "UK");
dic8.Add("DeviceType", "Laptop");
dic8.Add("DataCenter", AGG_SYMBOL);
points.Add(new Point(300, 200, true, dic8));

Dictionary<string, Object> dic9 = new Dictionary<string, Object>();
dic9.Add("Country", "UK");
dic9.Add("DeviceType", "Mobile");
dic9.Add("DataCenter", AGG_SYMBOL);
points.Add(new Point(1200, 300, true, dic9));

return points;
}

private static Dictionary<string, Object> GetAnomalyDimension()
{
Dictionary<string, Object> dim = new Dictionary<string, Object>();
dim.Add("Country", "UK");
dim.Add("DeviceType", AGG_SYMBOL);
dim.Add("DataCenter", AGG_SYMBOL);

return dim;
}

private static DateTime GetTimestamp()
{
return new DateTime(2020, 3, 23, 0, 0, 0);
}
}
}
20 changes: 12 additions & 8 deletions docs/samples/Microsoft.ML.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using Samples.Dynamic;
Comment thread
klausmh marked this conversation as resolved.

namespace Microsoft.ML.Samples
{
public static class Program
{
public static void Main(string[] args) => RunAll();
public static void Main(string[] args) => RunAll("LocalizeRootCauseMultipleDimensions");
Comment thread
klausmh marked this conversation as resolved.
Outdated

internal static void RunAll()
internal static void RunAll(string pattern = null)
{
int samples = 0;
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var sample = type.GetMethod("Example", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

if (sample != null)
if (pattern?.Equals(type.Name) ?? false)
{
Console.WriteLine(type.Name);
sample.Invoke(null, null);
samples++;
var sample = type.GetMethod("Example", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

if (sample != null)
{
Console.WriteLine(type.Name);
sample.Invoke(null, null);
samples++;
}
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/Microsoft.ML.TimeSeries/ExtensionsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Reflection;
using Microsoft.ML.Data;
Comment thread
klausmh marked this conversation as resolved.
using Microsoft.ML.Runtime;
Expand Down Expand Up @@ -164,6 +165,24 @@ public static SrCnnAnomalyEstimator DetectAnomalyBySrCnn(this TransformsCatalog
/// </format>
/// </example>
public static RootCause LocalizeRootCause(this AnomalyDetectionCatalog catalog, RootCauseLocalizationInput src, double beta = 0.5)
{
return LocalizeRootCauses(catalog, src, beta).Causes.FirstOrDefault();
Comment thread
klausmh marked this conversation as resolved.
Outdated
}

/// <summary>
/// Create <see cref="RootCause"/>, which localizes root causes using decision tree algorithm.
Comment thread
klausmh marked this conversation as resolved.
Outdated
/// </summary>
/// <param name="catalog">The anomaly detection catalog.</param>
/// <param name="src">Root cause's input. The data is an instance of <see cref="Microsoft.ML.TimeSeries.RootCauseLocalizationInput"/>.</param>
/// <param name="beta">Beta is a weight parameter for user to choose. It is used when score is calculated for each root cause item. The range of beta should be in [0,1]. For a larger beta, root cause point which has a large difference between value and expected value will get a high score. On the contrary, for a small beta, root cause items which has a high relative change will get a high score.</param>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
/// [!code-csharp[LocalizeRootCause](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/LocalizeRootCauseMultipleDimensions.cs)]
/// ]]>
/// </format>
/// </example>
public static RootCauses LocalizeRootCauses(this AnomalyDetectionCatalog catalog, RootCauseLocalizationInput src, double beta = 0.5)
{
IHostEnvironment host = CatalogUtils.GetEnvironment(catalog);

Expand All @@ -175,8 +194,7 @@ public static RootCause LocalizeRootCause(this AnomalyDetectionCatalog catalog,

Comment thread
klausmh marked this conversation as resolved.
//find out the root cause
RootCauseAnalyzer analyzer = new RootCauseAnalyzer(src, beta);
RootCause dst = analyzer.Analyze();
return dst;
return analyzer.AnalyzeAll();
}

private static void CheckRootCauseInput(IHostEnvironment host, RootCauseLocalizationInput src)
Expand Down
Loading