Save lives, businesses and more with Anomaly Detector 📈
Tags: azure, cognitiveservices, inclusion, aiforgood
Published: 2020-04-21 12:04:42

Around 2.5 years back, one of the data centers in the Hospital of Cairns, Queensland, Australia <a href="https://www.datacenterdynamics.com/en/news/data-center-battery-incident-causes-fire-in-australian-hospital/">caught up the fire</a> because of the faulty battery. This year, UK's Air Accidents Investigation Branch <a href="https://assets.publishing.service.gov.uk/media/5e7241a486650c7270546a05/Guimbal_Cabri_G2_G-CILR_04-20.pdf">concluded</a> that the fire that <a href="https://aviation-safety.net/wikibase/201438">crashed</a> Guimbal Cabri G2 helicopter was because of the electrical short circuit. Last week, Americans have <a href="https://www.cnbc.com/2020/04/15/americans-have-lost-13point4-million-to-fraud-linked-to-covid-19.html">lost $13.4 million</a> to fraud linked to Covid-19. All of this could have been saved if we had foreseen these issues before they actually occurred. How? Let's find out...

<!--more-->

Before we actually get into the solution, let's understand the common problem in all of these situations. The common problem was <strong><em>something did not work as expected </em></strong><strong><em>due to which it caused the accident / damage. </em></strong>Now the question is, is it really possible to identify something before hand which is not working as expected? If it is then how do we find it?

The answer of this question is: Yes, it's possible with <strong>Anomaly Detection</strong>.
<h2>Anomaly Detection</h2>
<a href="https://towardsdatascience.com/anomaly-detection-for-dummies-15f148e559c1">A</a><a href="https://towardsdatascience.com/anomaly-detection-for-dummies-15f148e559c1">nomaly detection</a> is the process of identifying unexpected items or events (referred to as anomalies) in your data sets, which differ from the normal behaviour.

Anomaly detection is widely used with the time-series data to identify anomalies in real-time systems (such as fraud detection systems, critical health monitoring system, event-detection systems) using different machine learning algorithms and techniques. However, you, as a developer of any stack may not want to learn about all the underlying algorithms, techniques and mathematics behind it yet you want to implement this in your systems so that you can be notified timely about anything that may be super important for business. <em><strong>That's one of my motivations to write this post. </strong></em>I want to bring AI to the reach of every developer like you!

<img class="aligncenter size-full wp-image-30905" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/AnomalyDetector-Banner.png" alt="Anomaly Detector Service" width="1640" height="624" />

As I discussed above, Anomaly Detection is a whole topic that requires a good and thorough understanding to implement but with the advent of Azure Cognitive Services, Microsoft has democratized the machine learning <a href="https://techcommunity.microsoft.com/t5/ai-customer-engineering-team/introducing-azure-anomaly-detector-api/ba-p/490162">underlying technical implementation and algorithms</a> for developers like us and exposed some API endpoints so that you can easily infuse AI capabilities into your solutions, with no or little knowledge of AI / machine learning.
<blockquote>In my <a href="https://www.arafattehsin.com/ai-for-every-developer-immersive-reader-cognitive-service/">first post</a> of this series, I have already given a brief overview of Cognitive Services and their need in today's world.</blockquote>
<h2>Anomaly Detector API <sup>Preview</sup></h2>
<a href="https://azure.microsoft.com/en-us/services/cognitive-services/anomaly-detector/">Anomaly Detector</a> API allows you to foresee the abnormalities in your time series data with the help of machine learning algorithms irrespective of the industry, scenario or your data volume.

This cognitive service will determine the boundaries for anomaly detection, expected values and other useful features which we're going to discuss now.
<h3>Highlights</h3>
This service is available as a client SDK for C#, Go, Node and Python and if your favorite language is not listed here then it's also available as a RESTful API (to support my folks who do not have SDK support, <em><strong>I will be using this one in my use-case</strong></em>).

Currently, Anomaly Detector service offers <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/anomaly-detector/how-to/identify-anomalies#anomaly-detection-modes">two types of detection</a>;
<h4>Batch Detection</h4>
This type of detection internally generates a model using an entire series, each point is detected with the same model. With this method, points before and after a certain point are used to determine whether it is an anomaly. The entire detection can give the user an overall status of the time series.
<h4>Real-time / Streaming Detection</h4>
With this method, only historical points are used to determine whether the target point is an anomaly. The latest point detecting matches the scenario of real-time monitoring of business metrics. <em><strong>This is the method we're going to use in our demo.</strong></em>

To sum it up, with the current version of Anomaly Detector, business customers can discover incidents and establish a logic flow for root cause analysis. It also provides a detection for the change point to discover the changes in time series. I'd highly encourage you to <a href="https://westus2.dev.cognitive.microsoft.com/docs/services/AnomalyDetector/operations/post-timeseries-last-detect">read</a> about the overall schema for request and response for better understanding. We will only be focusing on main topics here as required by our demo.
<blockquote>There's a chance that you may not understand something from this post, please feel free to comment, reach out to me on twitter or LinkedIn and I will be happy to help you with this.</blockquote>
As a part of <strong>AI For Every Developer </strong>series, I always want to cover a different tech stack in my each post and this time, I will be covering the Azure Logic Apps to achieve our business goal.
<h3>Automated Clinical Labs</h3>
As I have written above, there can be enormous use-cases from fraud detection to the stock market analysis and so on, but I am going to cover something related to the Clinical Labs (for a change 😊). We've picked up the case for maintaining the temperature of  this lab. So you can imagine that we've got sensors installed that record the temperature of the lab every hour (it can be every 30 minutes or less as well, if you wish) - the IoT can be managed by the Azure IoT Central.

<img class="aligncenter size-full wp-image-30897" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/Temperature-Anomaly.gif" alt="Anomaly Detector - Temperature" width="1162" height="448" />

As soon as the app records the temperature, it sends the telemetry (in JSON format) to the Azure Logic Apps (<em>can't it be an Azure Function? Yes, it could be and this is an example and the motive of this post is to show how can you utilize the Anomaly Detector</em>).

<img class="aligncenter wp-image-30898" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/HTTP-Request.jpg" alt="HTTP Request" width="603" height="440" />

As soon as the Azure Logic App receives the telemetry, it then records it into the Cosmos DB and right after that, it also gets <em><strong>all the records</strong></em><em> </em>from the database (including the latest one) and then it it sent to the Anomaly Detector service.

You may ask why it does not just send the latest telemetry to the service since the model may have already been created? So, currently the service creates the model on the fly and that's why we always have to send the data (trust me, it won't take a lot of data / time as it's JSON).

We can then call our Anomaly Detector service like this;

<img class="aligncenter wp-image-30899" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/HTTP-API-Call.png" alt="API Call" width="483" height="399" />

Upon the response of the Anomaly Detector service, if <strong>IsAnomaly</strong> property is <strong>true</strong> then an email is sent to the Lab's Manager to either <strong>Escalate it</strong> to the support staff or <b>Pause </b>the tests / processes which are being executed.

<img class="aligncenter wp-image-30900" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/Send-Email.jpg" alt="Send Email Logic Apps" width="479" height="418" />

<img class="aligncenter wp-image-30901" src="https://www.arafattehsin.com/wp-content/uploads/2020/04/Email.jpg" alt="Azure Logic Apps Email Action" width="402" height="231" />

To extend this example further, we can also implement the response of the email action and then send the notification to the Support Staff or open up a case in Dynamics 365 but I'll just stop it here!

This is super simple API but it truly holds a powerful implementation behind the scenes and I am sure you can use this service for several use-cases.

Until next time.