Page 39 - MSDN Magazine, November 2017
P. 39
service you need to run the experiment and then press the Deploy Web Service icon on the bottom pane of Machine Learning Studio (see the highlighted item in Figure 8). If you don’t add Web service input and output modules to the experiment, this pane will show Set Up Web Service. If you click it, Web service input and output modules will be added to the experiment, and the button label will change to Deploy Web Service.
When the Web service deployment completes, you’ll be redirected to the Web service dashboard, shown in Figure 9. The dashboard presents a summary of the published Web service, along with the API key and instructions on how to send requests and handle responses (API Help Page). Specifically, after clicking the request/ response hyperlink, a Web service URL and a detailed request and response structure in JSON format will be presented.
To proceed further, I store the API key and create JSON-to-C# mappings using the JSONUtils service (jsonutils.com). I then save the resulting classes in corresponding files, AnomalyDetectionRequest.cs and AnomalyDetectionResponse.cs, in the AnomalyDetection sub- folder. Their structure is similar—both files contain classes, which as usual are composed mostly of the auto-implemented properties. AnomalyDetectionRequest and AnomalyDetectionResponse rep- resent corresponding JSON objects being transmitted between a client and a Web service. As an example, the definition of the AnomalyDetectionRequest class and dependent objects is given in Figure 10. Note that to convert a collection of brightness data points to an input accepted by the Machine Learning Studio Web service (two dimensional array of strings), I use a helper class, ConversionHelper. The latter, which full definition is in the companion code, has two public methods. They either convert the collection of brightness data points to string[,] (Brightness- DataToStringTable) or vice versa (AnomalyDetectionResponse- ToBrignthessData).
Once the JSON-to-C# object mapping is established, I can write the actual Web service client. To that end I first install the Microsoft.AspNet.WebApi.Client NuGet package and then use it to define the AnomalyDetectionClient class (see the corresponding file in the companion code). This class has three private fields: baseAddress, apiKey and httpClient. The first field stores the URL of the Machine Learning Studio Web service, while the second contains the API key. These two values are used to instantiate the HttpClient class (from the installed NuGet package):
public AnomalyDetectionClient() {
httpClient = new HttpClient() {
BaseAddress = new Uri(baseAddress), };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
}
After creating the client, I can start sending requests to the Machine Learning Studio Web service with the AnomalyDetec- tionClient.DetectAnomalyAsync method from Figure 11. This method accepts a collection of brightness data points, representing test data. This test data replaces the CSV file I used previously for experimenting and is used to instantiate AnomalyDetection- Request. An instance of this class is later posted to the Web service msdnmagazine.com
Figure 8 The Azure Machine Learning Studio Action Pane
Figure 9 The Web Service Dashboard
for analysis with the PostAsJsonAsync extension method. The resulting JSON response is converted to the AnomalyDetection- Response class instance, which is finally returned by the Detect- AnomalyAsync function. I also look for any errors and eventually throw an exception if appropriate.
Figure 10 A Definition of the AnomalyDetectionRequest Class and Dependent Objects
public class AnomalyDetectionRequest {
public Inputs Inputs { get; set; }
public GlobalParameters GlobalParameters { get; set; }
public AnomalyDetectionRequest( IList<BrightnessDataPoint> brightnessData)
{
Inputs = new Inputs() {
Data = new Data() {
ColumnNames = new string[] {
"Time",
"Brightness" },
Values = ConversionHelper. BrightnessDataToStringTable(brightnessData)
} };
} }
public class Inputs {
public Data Data { get; set; } }
public class Data {
public string[] ColumnNames { get; set; }
public string[,] Values { get; set; } }
public class GlobalParameters { }
November 2017 35