Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anomoly/codegen #4

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -53,24 +53,33 @@ Call the client's `DetectEntireSeriesAsync` method with the `DetectRequest` obje
//detect
Console.WriteLine("Detecting anomalies in the entire time series.");

EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false);

if (result.IsAnomaly.Contains(true))
try
{
Console.WriteLine("An anomaly was detected at index:");
EntireDetectResponse result = await client.DetectEntireSeriesAsync(request).ConfigureAwait(false);

bool hasAnomaly = false;
for (int i = 0; i < request.Series.Count; ++i)
{
if (result.IsAnomaly[i])
{
Console.Write(i);
Console.Write(" ");
Console.WriteLine("An anomaly was detected at index: {0}.", i);
hasAnomaly = true;
}
}
Console.WriteLine();
if (!hasAnomaly)
{
Console.WriteLine("No anomalies detected in the series.");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine(String.Format("Entire detection failed: {0}", ex.Message));
throw;
}
else
catch (Exception ex)
{
Console.WriteLine(" No anomalies detected in the series.");
Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
throw;
}
```
To see the full example source files, see:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,28 @@ Call the client's `DetectLastPointAsync` method with the `DetectRequest` object
//detect
Console.WriteLine("Detecting the anomaly status of the latest point in the series.");

LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false);

if (result.IsAnomaly)
try
{
LastDetectResponse result = await client.DetectLastPointAsync(request).ConfigureAwait(false);

if (result.IsAnomaly)
{
Console.WriteLine("The latest point was detected as an anomaly.");
}
else
{
Console.WriteLine("The latest point was not detected as an anomaly.");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine("The latest point was detected as an anomaly.");
Console.WriteLine(String.Format("Last detection failed: {0}", ex.Message));
throw;
}
else
catch (Exception ex)
{
Console.WriteLine("The latest point was not detected as an anomaly.");
Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
throw;
}
```
To see the full example source files, see:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ You could add the data source, along with start time and end time to a `ModelInf
Call `TrainMultivariateModel` with the created data feed and extract the model ID from the response header. Afterwards, you can get the model info, including the model status, by calling `GetMultivariateModelAsync` with the model ID . Wait until the model status is ready.

```C# Snippet:TrainMultivariateModel
private async Task<Guid?> trainAsync(AnomalyDetectorClient client, string datasource, DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
private async Task<Guid?> TrainAsync(AnomalyDetectorClient client, string datasource, DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
{
try
{
Console.WriteLine("Training new model...");

int model_number = await getModelNumberAsync(client, false).ConfigureAwait(false);
int model_number = await GetModelNumberAsync(client, false).ConfigureAwait(false);
Console.WriteLine(String.Format("{0} available models before training.", model_number));

ModelInfo data_feed = new ModelInfo(datasource, start_time, end_time);
Expand Down Expand Up @@ -69,24 +69,24 @@ private async Task<Guid?> trainAsync(AnomalyDetectorClient client, string dataso
Console.WriteLine(String.Format("Request timeout after {0} tryouts", max_tryout));
}

model_number = await getModelNumberAsync(client).ConfigureAwait(false);
model_number = await GetModelNumberAsync(client).ConfigureAwait(false);
Console.WriteLine(String.Format("{0} available models after training.", model_number));
return trained_model_id;
}
catch (Exception e)
{
Console.WriteLine(String.Format("Train error. {0}", e.Message));
throw new Exception(e.Message);
throw;
}
}
```

## Detect anomalies

To detect anomalies using your newly trained model, create a private async Task named `detectAsync`. You will create a new `DetectionRequest`, pass that as a parameter to `DetectAnomalyAsync` and await the response. From the header of the response, you could extract the result ID. With the result ID, you could get the detection content and detection status by `GetDetectionResultAsync`. Return the detection content when the detection status is ready.
To detect anomalies using your newly trained model, create a private async Task named `DetectAsync`. You will create a new `DetectionRequest`, pass that as a parameter to `DetectAnomalyAsync` and await the response. From the header of the response, you could extract the result ID. With the result ID, you could get the detection content and detection status by `GetDetectionResultAsync`. Return the detection content when the detection status is ready.

```C# Snippet:DetectMultivariateAnomaly
private async Task<DetectionResult> detectAsync(AnomalyDetectorClient client, string datasource, Guid model_id,DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
private async Task<DetectionResult> DetectAsync(AnomalyDetectorClient client, string datasource, Guid model_id,DateTimeOffset start_time, DateTimeOffset end_time, int max_tryout = 500)
{
try
{
Expand Down Expand Up @@ -118,7 +118,48 @@ private async Task<DetectionResult> detectAsync(AnomalyDetectorClient client, st
catch (Exception e)
{
Console.WriteLine(String.Format("Detection error. {0}", e.Message));
throw new Exception(e.Message);
throw;
}
}
```

## Detect last anomalies

To detect anomalies using your newly trained model, create a private async Task named `DetectLastAsync`. You will create a new `LastDetectionRequest`, you could assign how many last points you want to detect in the request. Pass `LastDetectionRequest` as a parameter to `LastDetectAnomalyAsync` and await the response. Return the detection content when the detection status is ready.

```C# Snippet:DetectLastMultivariateAnomaly
private async Task<LastDetectionResult> DetectLastAsync(AnomalyDetectorClient client, Guid model_id)
{
Console.WriteLine("Start detect...");

List<VariableValues> variables = new List<VariableValues>();
variables.Add(new VariableValues("variables_name1", new[] { "2021-01-01 00:00:00", "2021-01-01 01:00:00", "2021-01-01 02:00:00" }, new[] { 0.0f, 0.0f, 0.0f }));
variables.Add(new VariableValues("variables_name2", new[] { "2021-01-01 00:00:00", "2021-01-01 01:00:00", "2021-01-01 02:00:00" }, new[] { 0.0f, 0.0f, 0.0f }));

LastDetectionRequest lastDetectionRequest = new LastDetectionRequest(variables, 1);

try
{
Response<LastDetectionResult> response = await client.LastDetectAnomalyAsync(model_id, lastDetectionRequest).ConfigureAwait(false);
if (response.GetRawResponse().Status == 200)
{
foreach (AnomalyState state in response.Value.Results)
{
Console.WriteLine(String.Format("timestamp: {}, isAnomaly: {}, score: {}.", state.Timestamp, state.Value.IsAnomaly, state.Value.Score));
}
}

return response;
}
catch (RequestFailedException ex)
{
Console.WriteLine(String.Format("Last detection failed: {0}", ex.Message));
throw;
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
throw;
}
}
```
Expand All @@ -128,7 +169,7 @@ private async Task<DetectionResult> detectAsync(AnomalyDetectorClient client, st
To export the model you trained previously, create a private async Task named `exportAysnc`. You will use `ExportModelAsync` and pass the model ID of the model you wish to export.

```C# Snippet:ExportMultivariateModel
private async Task exportAsync(AnomalyDetectorClient client, Guid model_id, string model_path = "model.zip")
private async Task ExportAsync(AnomalyDetectorClient client, Guid model_id, string model_path = "model.zip")
{
try
{
Expand All @@ -144,7 +185,7 @@ private async Task exportAsync(AnomalyDetectorClient client, Guid model_id, stri
catch (Exception e)
{
Console.WriteLine(String.Format("Export error. {0}", e.Message));
throw new Exception(e.Message);
throw;
}
}
```
Expand All @@ -154,13 +195,13 @@ private async Task exportAsync(AnomalyDetectorClient client, Guid model_id, stri
To delete a model that you have created previously use `DeleteMultivariateModelAsync` and pass the model ID of the model you wish to delete. You can check the number of models after deletion with `getModelNumberAsync`.

```C# Snippet:DeleteMultivariateModel
private async Task deleteAsync(AnomalyDetectorClient client, Guid model_id)
private async Task DeleteAsync(AnomalyDetectorClient client, Guid model_id)
{
await client.DeleteMultivariateModelAsync(model_id).ConfigureAwait(false);
int model_number = await getModelNumberAsync(client).ConfigureAwait(false);
int model_number = await GetModelNumberAsync(client).ConfigureAwait(false);
Console.WriteLine(String.Format("{0} available models after deletion.", model_number));
}
private async Task<int> getModelNumberAsync(AnomalyDetectorClient client, bool delete = false)
private async Task<int> GetModelNumberAsync(AnomalyDetectorClient client, bool delete = false)
{
int count = 0;
AsyncPageable<ModelSnapshot> model_list = client.ListMultivariateModelAsync(0, 10000);
Expand Down
Loading