Skip to content
Open
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 @@ -76,7 +76,8 @@
"source": [
"def detect(endpoint, apikey, request_data):\n",
" headers = {'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': apikey}\n",
" response = requests.post(endpoint, data=json.dumps(request_data), headers=headers)\n",
" endpoint_uri = f\"https://{endpoint}\"\n",
" response = requests.post(endpoint_uri, data=json.dumps(request_data), headers=headers)\n",
" if response.status_code == 200:\n",
" return json.loads(response.content.decode(\"utf-8\"))\n",
" else:\n",
Expand All @@ -92,9 +93,9 @@
"source": [
"def build_figure(sample_data, sensitivity):\n",
" sample_data['sensitivity'] = sensitivity\n",
" result = detect(endpoint, apikey, sample_data)\n",
" columns = {'expectedValues': result['expectedValues'], 'isAnomaly': result['isAnomaly'], 'isNegativeAnomaly': result['isNegativeAnomaly'],\n",
" 'isPositiveAnomaly': result['isPositiveAnomaly'], 'upperMargins': result['upperMargins'], 'lowerMargins': result['lowerMargins'],\n",
" result = detect(endpoint_latest, apikey, sample_data)\n",
" columns = {'expectedValue': result['expectedValue'], 'isAnomaly': result['isAnomaly'], 'isNegativeAnomaly': result['isNegativeAnomaly'],\n",
" 'isPositiveAnomaly': result['isPositiveAnomaly'], 'upperMargin': result['upperMargin'], 'lowerMargin': result['lowerMargin'],\n",
" 'timestamp': [parser.parse(x['timestamp']) for x in sample_data['series']], \n",
" 'value': [x['value'] for x in sample_data['series']]}\n",
" response = pd.DataFrame(data=columns)\n",
Expand All @@ -106,19 +107,19 @@
" anomaly_indexes = []\n",
" p = figure(x_axis_type='datetime', title=\"Batch Anomaly Detection ({0} Sensitvity)\".format(sensitivity), width=800, height=600)\n",
" for anom in response['isAnomaly']:\n",
" if anom == True and (values[index] > response.iloc[index]['expectedValues'] + response.iloc[index]['upperMargins'] or \n",
" values[index] < response.iloc[index]['expectedValues'] - response.iloc[index]['lowerMargins']):\n",
" if anom == True and (values[index] > response.iloc[index]['expectedValue'] + response.iloc[index]['upperMargin'] or \n",
" values[index] < response.iloc[index]['expectedValue'] - response.iloc[index]['lowerMargin']):\n",
" anomalies.append(values[index])\n",
" anomaly_labels.append(label[index])\n",
" anomaly_indexes.append(index)\n",
" index = index+1\n",
" upperband = response['expectedValues'] + response['upperMargins']\n",
" lowerband = response['expectedValues'] -response['lowerMargins']\n",
" upperband = response['expectedValue'] + response['upperMargin']\n",
" lowerband = response['expectedValue'] -response['lowerMargin']\n",
" band_x = np.append(label, label[::-1])\n",
" band_y = np.append(lowerband, upperband[::-1])\n",
" boundary = p.patch(band_x, band_y, color=Blues4[2], fill_alpha=0.5, line_width=1, legend='Boundary')\n",
" p.line(label, values, legend='Value', color=\"#2222aa\", line_width=1)\n",
" p.line(label, response['expectedValues'], legend='ExpectedValue', line_width=1, line_dash=\"dotdash\", line_color='olivedrab')\n",
" p.line(label, response['expectedValue'], legend='ExpectedValue', line_width=1, line_dash=\"dotdash\", line_color='olivedrab')\n",
" anom_source = ColumnDataSource(dict(x=anomaly_labels, y=anomalies))\n",
" anoms = p.circle('x', 'y', size=5, color='tomato', source=anom_source)\n",
" p.legend.border_line_width = 1\n",
Expand Down Expand Up @@ -149,7 +150,7 @@
"outputs": [],
"source": [
"# Hourly Sample\n",
"sample_data = json.load(open('univariate_sample_hourly.json'))\n",
"sample_data = json.load(open('../SDK Sample/univariate_sample_hourly.json'))\n",
"sample_data['granularity'] = 'hourly'\n",
"sample_data['period'] = 24\n",
"# 95 sensitivity\n",
Expand Down Expand Up @@ -190,7 +191,7 @@
"outputs": [],
"source": [
"#daily sample\n",
"sample_data = json.load(open('univariate_sample_daily.json'))\n",
"sample_data = json.load(open('../SDK Sample/univariate_sample_daily.json'))\n",
"sample_data['granularity'] = 'daily'\n",
"# 95 sensitivity\n",
"build_figure(sample_data,95)"
Expand Down Expand Up @@ -238,4 +239,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"source": [
"def detect(endpoint, apikey, request_data):\n",
" headers = {'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': apikey}\n",
" response = requests.post(endpoint, data=json.dumps(request_data), headers=headers)\n",
" endpoint_uri = f\"https://{endpoint}\"\n",
" response = requests.post(endpoint_uri, data=json.dumps(request_data), headers=headers)\n",
" if response.status_code == 200:\n",
" return json.loads(response.content.decode(\"utf-8\"))\n",
" else:\n",
Expand Down Expand Up @@ -117,9 +118,9 @@
" lowerband = response['expectedValues'] -response['lowerMargins']\n",
" band_x = np.append(label, label[::-1])\n",
" band_y = np.append(lowerband, upperband[::-1])\n",
" boundary = p.patch(band_x, band_y, color=Blues4[2], fill_alpha=0.5, line_width=1, legend='Boundary')\n",
" p.line(label, values, legend='value', color=\"#2222aa\", line_width=1)\n",
" p.line(label, response['expectedValues'], legend='expectedValue', line_width=1, line_dash=\"dotdash\", line_color='olivedrab')\n",
" boundary = p.patch(band_x, band_y, color=Blues4[2], fill_alpha=0.5, line_width=1, legend_label='Boundary')\n",
" p.line(label, values, legend_label='value', color=\"#2222aa\", line_width=1)\n",
" p.line(label, response['expectedValues'], legend_label='expectedValue', line_width=1, line_dash=\"dotdash\", line_color='olivedrab')\n",
" anom_source = ColumnDataSource(dict(x=anomaly_labels, y=anomalies))\n",
" anoms = p.circle('x', 'y', size=5, color='tomato', source=anom_source)\n",
" p.legend.border_line_width = 1\n",
Expand Down Expand Up @@ -148,7 +149,7 @@
"outputs": [],
"source": [
"def detect_anomaly(sensitivity):\n",
" sample_data = json.load(open('univariate_sample_daily.json'))\n",
" sample_data = json.load(open('../SDK Sample/univariate_sample_daily.json'))\n",
" points = sample_data['series']\n",
" skip_point = 29\n",
" result = {'expectedValues': [None]*len(points), 'upperMargins': [None]*len(points), \n",
Expand Down Expand Up @@ -217,4 +218,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}