def last_detect_sample(endpoint, key, request):
    print('Sample of detecting whether the latest point in series is anomaly.')

    client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(key))
    response = client.last_detect(request)
    if response.is_anomaly:
        print('The latest point is detected as anomaly.')
    else:
        print('The latest point is not detected as anomaly.')
Esempio n. 2
0
def last_detect(subscription_key):
    print("Sample of detecting whether the latest point in series is anomaly.")
    # Add your Azure Anomaly Detector subscription key to your environment variables.
    endpoint = os.environ.get["ANOMALY_DETECTOR_ENDPOINT"]
    
    try:
        client = AnomalyDetectorClient(
            endpoint, CognitiveServicesCredentials(subscription_key))
        request = get_request()
        response = client.last_detect(request)
        if response.is_anomaly:
            print("The latest point is detected as anomaly.")
        else:
            print("The latest point is not detected as anomaly.")
    except Exception as e:
        if isinstance(e, APIErrorException):
            print("Error code: {}".format(e.error.code))
            print("Error message: {}".format(e.error.message))
        else:
            print(e)
def last_detect(subscription_key):
    print("Sample of detecting whether the latest point in series is anomaly.")

    endpoint = "https://{}.api.cognitive.microsoft.com".format(
        ANOMALYDETECTOR_LOCATION)
    try:
        client = AnomalyDetectorClient(
            endpoint, CognitiveServicesCredentials(subscription_key))
        request = get_request()
        response = client.last_detect(request)
        if response.is_anomaly:
            print("The latest point is detected as anomaly.")
        else:
            print("The latest point is not detected as anomaly.")
    except Exception as e:
        if isinstance(e, APIErrorException):
            print("Error code: {}".format(e.error.code))
            print("Error message: {}".format(e.error.message))
        else:
            print(e)
def last_detect_sample(endpoint, key):
    print('Sample of detecting whether the latest point in series is anomaly.')

    client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(key))

    series = [
        Point(timestamp=datetime(1962, 1, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 2, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 3, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 4, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 5, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 6, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 7, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 8, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 9, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 10, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 11, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1962, 12, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 1, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 2, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 3, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 4, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 5, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 6, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 7, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 8, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 9, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 10, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 11, 1, tzinfo=timezone.utc), value=1),
        Point(timestamp=datetime(1963, 12, 1, tzinfo=timezone.utc), value=0),
    ]

    data = Request(series=series,
                   granularity=Granularity.monthly,
                   sensitivity=95,
                   max_anomaly_ratio=0.25)
    response = client.last_detect(data)
    if response.is_anomaly:
        print('The latest point is detected as anomaly.')
    else:
        print('The latest point is not detected as anomaly.')
        print(e)

if True in response.is_anomaly:
    print('An anomaly was detected at index:')
    for i in range(len(response.is_anomaly)):
        if response.is_anomaly[i]:
            print(i)
else:
    print('No anomalies were detected in the time series.')
# </detectAnomaliesBatch>

# Detect the anomaly status of the latest data point

# <latestPointDetection>
print('Detecting the anomaly status of the latest data point.')

try:
    response = client.last_detect(request)
except Exception as e:
    if isinstance(e, APIErrorException):
        print('Error code: {}'.format(e.error.code),
            'Error message: {}'.format(e.error.message))
    else:
        print(e)

if response.is_anomaly:
    print('The latest point is detected as anomaly.')
else:
    print('The latest point is not detected as anomaly.')
# </latestPointDetection>