1
0
Форкнуть 0

Fixed samples and in the processing of editing consumption documentation

This commit is contained in:
Alex Wang 2015-08-05 17:40:05 -07:00
Родитель 419ecb59aa
Коммит 972e49f0be
18 изменённых файлов: 92 добавлений и 92 удалений

Просмотреть файл

@ -36,7 +36,7 @@ msftWebService <- publishWebService("MSFTpredict", "MSFTdemo", list("close"="flo
msftEndpoints <- msftWebService[[2]]
#
msftConsumeSingleRows <- consumeDataTable(msftEndpoints[[1]]["PrimaryKey"], msftEndpoints[[1]]$ApiLocation, list("close", "volume"), list(25, 300), list(30, 100))
msftConsumeSingleRows <- consumeLists(msftEndpoints[[1]]["PrimaryKey"], msftEndpoints[[1]]$ApiLocation, list("close", "volume"), list(25, 300), list(30, 100))
msftDF <- data.frame("close"=c(107,208,300), "volume"=c(400,569,665))
msftConsumeDF <- consumeDataframe(msftWebService[[2]][[1]]$PrimaryKey, msftWebService[[2]][[1]]$ApiLocation, msftDF)

Просмотреть файл

@ -37,7 +37,7 @@ bikeCount <- publishWebService("predictBikeCount", "bikeCount", list("dateTime"=
# Consume web service
endpoints <- bikeCount[[2]]
response <- consumeDataTable(endpoints[[1]]["PrimaryKey"], endpoints[[1]]["ApiLocation"],
response <- consumeDataLists(endpoints[[1]]["PrimaryKey"], endpoints[[1]]["ApiLocation"],
list("dateTime", "month", "hr", "hldy", "wkdy", "weather",
"temp", "hum", "wndspd", "casual", "registered", "cnt",
"isWorking", "mthCnt", "dayWeek", "wrkTime", "xformHr"),

Просмотреть файл

@ -25,6 +25,6 @@ onlineCluster <- publishWebService("getCluster", "kMeansCancer", list("age"="int
"Irradiat"="int"), list("cluster"="int"), wsID, wsAuth)
# Consume web service
endpoints <- onlineCluster[[2]]
responseDF <- consumeDataTable(endpoints[[1]]$PrimaryKey, endpoints[[1]]$ApiLocation,
responseDF <- consumeDataLists(endpoints[[1]]$PrimaryKey, endpoints[[1]]$ApiLocation,
list("age", "mp", "tSize", "invNodes", "nodeCaps", "DegMalig", "Breast", "BreastQuad", "Irradiat"),
list(7, 2, 3, 4, 2, 3, 2, 1, 1))

Просмотреть файл

@ -29,7 +29,7 @@ irisWebService <- publishWebService("predictClass", "TestIris", list("sepalLengt
irisEndpoints <- irisWebService[[2]]
#Consume single request
irisConsumeSingleRows <- consumeDataTable(irisEndpoints[[1]]$PrimaryKey, irisEndpoints[[1]]$ApiLocation, list("sepalLength", "sepalWidth", "petalLength", "petalWidth"), list(5, 5, 4, 3), list(4.5, 6.5, 4.5, 2))
irisConsumeSingleRows <- consumeLists(irisEndpoints[[1]]$PrimaryKey, irisEndpoints[[1]]$ApiLocation, list("sepalLength", "sepalWidth", "petalLength", "petalWidth"), list(5, 5, 4, 3), list(4.5, 6.5, 4.5, 2))
#Consume data frame
irisDF <- data.frame("sepalLength"=c(1,2,3), "sepalWidth"=c(4,5,6), "petalLength"=c(7,8,9), "petalWidth"=c(10,11,12))

Просмотреть файл

@ -76,7 +76,7 @@ titanicWebService <- publishWebService("predictTitanicSurvival", "TestTitanic",
titanicEndpoints <- titanicWebService[[2]]
#May take long since servers are getting warmed up
titanicConsumeSingleRows <- consumeDataTable(titanicEndpoints[[1]]["PrimaryKey"], titanicEndpoints[[1]]$ApiLocation, list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list(1, "male", 20, 2, 0, 8.50), list(1, "female", 20, 1, 0, 8.50))
titanicConsumeSingleRows <- consumeLists(titanicEndpoints[[1]]["PrimaryKey"], titanicEndpoints[[1]]$ApiLocation, list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list(1, "male", 20, 2, 0, 8.50), list(1, "female", 20, 1, 0, 8.50))
# consume with inputs as dataframe
# creating test data.frame

Просмотреть файл

@ -1,6 +1,3 @@
wrapper <- "inputDF <- maml.mapInputPort(1)\r\noutputDF <- matrix(ncol = %s, nrow = nrow(inputDF))\r\ncolnames(outputDF) <- list(%s)\r\noutputDF <- data.frame(outputDF)\r\nfor (file in list.files(\"src\")) {\r\n if (file == \"%s\") {\r\n load(\"src/%s\")\r\n for (item in names(dependencies)) {\r\n assign(item, dependencies[[item]])\r\n }\r\n }\r\n else {\r\n if (!(file %%in%% installed.packages()[,\"Package\"])) {\r\n install.packages(paste(\"src\", file, sep=\"/\"), lib=\".\", repos=NULL, verbose=TRUE)\r\n }\r\n library(strsplit(file, \"\\\\.\")[[1]][[1]], character.only=TRUE)\r\n }\r\n}\r\naction <- %s\r\nfor (i in 1:nrow(inputDF)) {\r\n outputDF[i,] <- do.call(\"action\", as.list(inputDF[i,]))\r\n}\r\nmaml.mapOutputPort(\"outputDF\")"
#############################################################
#' @title Get function source code as a string
#' @description
#' This is a helper function that will convert a function's source code to a string
#' @export internal
@ -510,3 +507,6 @@ devtools::document()
nchars("C:/Users/t-alewa/Documents/Azure-MachineLearning-ClientLibrary-R/maml/man")
nchar("C:/Users/t-alewa/Documents/Azure-MachineLearning-ClientLibrary-R/maml/man")
nchar("C:/Users/t-alewa/Documents/Azure-MachineLearning-ClientLibrary-R/maml/man/getFunctionString.R")
library(maml)
?config
??config

Просмотреть файл

@ -30,6 +30,7 @@ C%3A%2FUsers%2Ft-alewa%2FDownloads%2FconsumeRRS.R="AF6892D8"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Fman%2FconsumeSingleRows.Rd="5CEB561E"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Fman%2FgetFramework.Rd="D791A6BB"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Fman%2FgetWebServices.Rd="42ED6261"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Fman%2Fmaml-package.Rd="5313FC18"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Fman%2FpackDependencies.Rd="CC7977E2"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Ftests%2Ftestthat%2FtestConsume.R="CBF4468"
~%2FAzure-MachineLearning-ClientLibrary-R%2Fmaml%2Ftests%2Ftestthat%2FtestDiscover.R="4BC6E8CA"

Просмотреть файл

@ -7,7 +7,7 @@ Authors@R: c(person("AzureML", "Maintainer", email = "AzureMLForCRAN@microsoft.c
person("Brianna", "Gerads", role = "aut"),
person("Ritika", "Ravichandra", role = "aut"),
person("Alex", "Wang", role = "aut"))
Maintainer: AzureML Maintainer <AzureMLForCRAN@microsoft.com>
Maintainer: Raymond Laghaeian <AzureMLForCRAN@microsoft.com>
Description: Provides an interface with Microsoft Azure to easily publish,
consume, and discover models as a web service.
License: MIT + file LICENSE

Просмотреть файл

@ -1,14 +1,15 @@
################################################################################################################################
#' This function discovers using the workspace ID and web service ID, information specific to the consumption functions
#' @title Discover function schema
#' Discover web service schema
#'
#' Discover the expected input to a web service specified by a web service ID ng the workspace ID and web service ID, information specific to the consumption functions
#'
#' @export
#' @param helpURL help page URL
#'
#' @param helpURL URL of the help page of the web service
#' @param scheme the URI scheme
#' @param host optional parameter that defaults to ussouthcentral.services.azureml.net
#' @param api_version that defaults to 2.0
#' @param api_version defaults to 2.0
#' @return List containing the request URL of the webservice, column names of the data, sample input as well as the input schema
################################################################################################################################
#'
discoverSchema <- function(helpURL, scheme = "https", host = "ussouthcentral.services.azureml.net", api_version = "2.0") {
endpointId = getDetailsFromUrl(helpURL)[[1]]
workspaceId = getDetailsFromUrl(helpURL)[[2]]
@ -102,21 +103,20 @@ discoverSchema <- function(helpURL, scheme = "https", host = "ussouthcentral.ser
return (list("requestUrl" = requestUrl, "columnNames" = columnNames, "sampleInput" = inputExample, "inputSchema" = inputSchema))
}
################################################################################################################################
#' This function takes in an API key, file name and the request URL as compulsory parameters.
#' It scores a file and returns results in a data frame.
#' It calls a helper function that sends requests to the server in the key-value format.
#' It processes requests in batches and stores the responses in order of batches in an array. It returns the results in a data frame, and stores the results in a text file.
#' @param apiKey entered as a string
#' @param requestUrl entered as a string or discovered through the discover schema method
#' @param inFileName the name of the file scored as a string
#' Use a web service to score a file
#'
#' Read in a csv and score it in batches using a Microsoft Azure Machine Learning Web Service. The results are stored in a new csv, default named "results.csv"
#' @param apiKey primary access key as a string
#' @param requestUrl API URL
#' @param inFileName the name of the file to be scored as a string
#' @param globalParam global parameters entered as a list, default value is an empty list
#' @param outputFileName the name of the file to output results to, entered as a string, default value is "results.csv"
#' @param batchSize batch size of each batch, default value is 250
#' @param batchSize batch size of each batch, default value is 300
#' @param retryDelay the time in seconds to delay before retrying in case of a server error, default value is 0.3 seconds
#' @return returnDataFrame data frame containing results returned from web service call
################################################################################################################################
#'
#' @family consume
#'
consumeFile <- function(apiKey, requestUrl, inFileName, globalParam = setNames(list(), character(0)), outputFileName = "results.csv", batchSize = 300, retryDelay = 0.3) {
#Stops users if they miss out mandatory fields
if (missing(apiKey)) {
@ -177,19 +177,20 @@ consumeFile <- function(apiKey, requestUrl, inFileName, globalParam = setNames(l
return (returnDataFrame)
}
################################################################################################################################
#' This function takes in an API key, request URL, requests as lists in the key value format as compulsory parameters.
#' It scores requests with lists entered in the key-value format and returns results in a data frame.
#' It calls a helper function that sends requests to the server in the key value format.
#' It processes requests in batches and stores the responses in order of batches in an array. It returns the results in a data frame.
#' @param apiKey entered as a string
#' @param requestUrl request URL entered as a string or discovered through the discover schema method
#' @param ... variable number of requests entered as lists in the key-value format
#' Use a web service to score data in list format
#'
#' Score data represented as lists, where each list represents one parameter of the web service
#'
#' @param apiKey primary access key as a string
#' @param requestUrl API URL
#' @param globalParam global parameters entered as a list, default value is an empty list
#' @param batchSize batch size of each batch, default value is 300
#' @param retryDelay the time in seconds to delay before retrying in case of a server error, default value is 0.3 seconds
#' ... variable number of requests entered as lists in key-value format
#' @return returnDataFrame data frame containing results returned from web service call
################################################################################################################################
#'
#' @family consume
# @example
consumeLists <- function(apiKey, requestUrl, ..., globalParam = setNames(list(), character(0)), retryDelay = 0.3) {
#Stops users if they miss out mandatory fields
@ -217,20 +218,19 @@ consumeLists <- function(apiKey, requestUrl, ..., globalParam = setNames(list(),
return(resultDataFrame)
}
################################################################################################################################
#' This function takes in an API key, data frame and the request URL as compulsory parameters.
#' It scores a data frame and returns results in a data frame.
#' It calls a helper function that sends requests to the server in the key-value format.
#' It processes requests in batches and stores the responses in order of batches in an array.
#' @param apiKey entered as a string
#' @param requestUrl entered as a string or discovered through the discover schema method
#' Use a web service to score a data frame
#'
#' @param apiKey primary access key as a string
#' @param requestUrl API URL
#' @param scoreDataFrame the data frame to be scored
#' @param scoreDataFrame the data frame to be scored
#' @param globalParam global parameters entered as a list, default value is an empty list
#' @param batchSize batch size of each batch, default value is 250
#' @param batchSize batch size of each batch, default value is 300
#' @param retryDelay the time in seconds to delay before retrying in case of a server error, default value is 0.3 seconds
#' @return returnDataFrame data frame containing results returned from web service call
################################################################################################################################
#'
#' @family consume
#'
consumeDataframe <- function(apiKey, requestUrl, scoreDataFrame, globalParam=setNames(list(), character(0)), batchSize = 300, retryDelay = 0.3) {
#Stops users if they miss out mandatory fields

Просмотреть файл

@ -1,32 +1,20 @@
# API URLs ----------------------------------------------------------------
wsURL = "https://management-tm.azureml.net/workspaces/%s/webservices"
wsURLdet = "https://management-tm.azureml.net/workspaces/%s/webservices/%s"
epURL = "https://management-tm.azureml.net/workspaces/%s/webservices/%s/endpoints"
epURLdet = "https://management-tm.azureml.net/workspaces/%s/webservices/%s/endpoints/%s"
# remove in real version
testURL = "https://hiteshsm.cloudapp.net/workspaces/%s/webservices/%s/endpoints"
internalURL = "https://management.azureml-int.net/workspaces/%s/webservices/%s/endpoints"
internalURL = "https://management.azureml-int.net"
prodURL = "https://management-tm.azureml.net"
# Functions ---------------------------------------------------------------
#' Abstraction for making GET requests.
#'
#' Framework for making GET requests to the Azure management APIs.
#'
#' @param tUrl API URL
#' @param authToken authentication token
#' @param tUrl the API URL
#' @param authToken the authentication token
#'
#' @return the response as a named list
#'
#' @family discovery
#' @keywords Internal
#' @keywords internal
getFramework <- function(tUrl, authToken) {
# Collectors for API response
h = RCurl::basicTextGatherer()
@ -104,7 +92,7 @@ getWebServices <- function(wkID, authToken, url=prodURL) {
#' @export
#'
#' @inheritParams getWebServices
#' @param wsID webservice ID
#' @param wsID the web service ID
#'
#' @return Returns a list with the following fields:
#'

Просмотреть файл

@ -1,21 +1,27 @@
#' Allows you to discover, publish and consume Azure ML web services
#'
#' The discovery code allows the user to retrieve a list of the web services available in their workspace given that they provide the workspace ID and the authorization token (both of which can be found in settings on the AzureML webpage). A user can also get detailed information about a specific web service, retrieve its endpoints, and the details of a specific endpoint.
#'
#' Publish, discover, and consumer Microsoft Azure Web Services
#'
#' This package implements an interface with Microsoft Azure Machine Learning, allowing users to publish a function, e.g. a prediction using a trained machine learning model, as a web service, to be shared with users on Visual Studio, Excel, etc., or consumed within R itself. Users are also available to access their preexisting web services and API endpoints available to their workspace to consume within R. All functions require a workspace ID (WsID) and authorization token (authToken) that can be found on the settings tab of studio.azureml.net, but users can make a temporary free account at \url{https://azure.microsoft.com/en-us/pricing/free-trial/}. For more information, check out this page: \url{https://github.com/Azure/Azure-MachineLearning-ClientLibrary-R}.
#'
#' 1. Discovery
#'
#'
#' \itemize{
#' \item Get endpoints: \code{\link{getEndpoints}}
#' \item Get web services: \code{\link{getWebServices}}
#' \item Get web service details \code{\link{getWSDetails}}
#' \item Get endpoints: \code{\link{getEndpoints}}
#' \item Get endpoint details: \code{\link{getEPDetails}}
#' }
#'
#'
#' 2. Publish
#'
#' blah
#'
#'
#' \itemize{
#' \item Publish a new web service: \code{\link{publishWebService}}
#' \item Update an existing web service: \code{\link{updateWebService}}
#' }
#'
#' 3. Consume
#'
#'
#' blah
#'
#'
#' @name maml-package
#' @aliases maml
#' @docType package

Просмотреть файл

@ -1,10 +1,12 @@
# String constants --------------------------------------------------------
publishURL <- "https://management.azureml.net/workspaces/%s/webservices/%s"
wrapper <- "inputDF <- maml.mapInputPort(1)\r\noutputDF <- matrix(ncol = %s, nrow = nrow(inputDF))\r\ncolnames(outputDF) <- list(%s)\r\noutputDF <- data.frame(outputDF)\r\nfor (file in list.files(\"src\")) {\r\n if (file == \"%s\") {\r\n load(\"src/%s\")\r\n for (item in names(dependencies)) {\r\n assign(item, dependencies[[item]])\r\n }\r\n }\r\n else {\r\n if (!(file %%in%% installed.packages()[,\"Package\"])) {\r\n install.packages(paste(\"src\", file, sep=\"/\"), lib=\".\", repos=NULL, verbose=TRUE)\r\n }\r\n library(strsplit(file, \"\\\\.\")[[1]][[1]], character.only=TRUE)\r\n }\r\n}\r\naction <- %s\r\nfor (i in 1:nrow(inputDF)) {\r\n outputDF[i,] <- do.call(\"action\", as.list(inputDF[i,]))\r\n}\r\nmaml.mapOutputPort(\"outputDF\")"
# Functions ---------------------------------------------------------------
#' Get function source code
#'
#' Returns the source code of a function as a string
@ -83,7 +85,7 @@ getFunctionString <- function (x)
#'
#' Find a function's in-memory and package dependencies, and turn them into a base-64 encoded zip file. This string is used in the publish API call to upload dependencies to the server.
#'
#' @param functionName function to package dependencies from
#' @param functionName function to package dependencies from
#' @return list containing the guid for the rdta file and the encoded zip
#'
#' @keywords internal
@ -344,9 +346,7 @@ publishWebService <- function(functionName, serviceName, inputSchema, outputSche
}
# convert the payload to JSON as expected by API
# TODO: consolidate json packages, i.e. use only one if possible
body = RJSONIO::toJSON(req)
#print(sprintf(wrapper, length(outputSchema), paste(sprintf("\"%s\"", names(outputSchema)), collapse=","), zipString[[1]], zipString[[1]], paste(getFunctionString(functionName))))
# Response gatherer
h = RCurl::basicTextGatherer()
@ -367,7 +367,6 @@ publishWebService <- function(functionName, serviceName, inputSchema, outputSche
newService <- RJSONIO::fromJSON(h$value())
# Use discovery functions to get endpoints for immediate use
# NOTE: switch from internal URL for production
endpoints <- getEndpoints(wkID, authToken, newService["Id"])
# currently returning list of webservice details (as a list) and endpoint details (as a list) in that order
@ -378,7 +377,7 @@ publishWebService <- function(functionName, serviceName, inputSchema, outputSche
#' Update a Published Web Service
#'
#' This function updates published code given a valid workspace ID and authentication token. The function expects the function name, service id, and the input and output schemas from the user. The user can expect a list of the web service details, the default endpoint details and the consumption function and use this information to access the published function.
#' Update a web service, i.e. change the underlying R code that the service will run when called.
#'
#' @export
#'
@ -389,7 +388,7 @@ publishWebService <- function(functionName, serviceName, inputSchema, outputSche
#' @family publish
#'
# @examples
# TitanicService <- updateWebService("predictTitanic", "TitanicDemo", list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="float"), list("survProb"="float"), wsID, wsAuth)
# TitanicService <- updateWebService("predictTitanic", "TitanicDemo", "wsID of list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="float"), list("survProb"="float"), wsID, wsAuth)
updateWebService <- function(functionName, serviceName, wsID, inputSchema, outputSchema, wkID, authToken) {
# Make sure schema inputted matches function signature

Просмотреть файл

@ -11,7 +11,7 @@ getEPDetails(wkID, authToken, wsID, epName, url = prodURL)
\item{authToken}{primary authorization token}
\item{wsID}{webservice ID}
\item{wsID}{the web service ID}
\item{epName}{endpoint name}

Просмотреть файл

@ -11,7 +11,7 @@ getEndpoints(wkID, authToken, wsID, url = prodURL)
\item{authToken}{primary authorization token}
\item{wsID}{webservice ID}
\item{wsID}{the web service ID}
\item{url}{the API url to make the call to, by default hits the Azure management API}
}

Просмотреть файл

@ -7,9 +7,9 @@
getFramework(tUrl, authToken)
}
\arguments{
\item{tUrl}{API URL}
\item{tUrl}{the API URL}
\item{authToken}{authentication token}
\item{authToken}{the authentication token}
}
\value{
the response as a named list
@ -22,5 +22,5 @@ Other discovery: \code{\link{getEPDetails}};
\code{\link{getEndpoints}}; \code{\link{getWSDetails}};
\code{\link{getWebServices}}
}
\keyword{Internal}
\keyword{internal}

Просмотреть файл

@ -11,7 +11,7 @@ getWSDetails(wkID, authToken, wsID, url = prodURL)
\item{authToken}{primary authorization token}
\item{wsID}{webservice ID}
\item{wsID}{the web service ID}
\item{url}{the API url to make the call to, by default hits the Azure management API}
}

Просмотреть файл

@ -4,20 +4,26 @@
\name{maml-package}
\alias{maml}
\alias{maml-package}
\title{Allows you to discover, publish and consume Azure ML web services}
\title{Publish, discover, and consumer Microsoft Azure Web Services}
\description{
The discovery code allows the user to retrieve a list of the web services available in their workspace given that they provide the workspace ID and the authorization token (both of which can be found in settings on the AzureML webpage). A user can also get detailed information about a specific web service, retrieve its endpoints, and the details of a specific endpoint.
This package implements an interface with Microsoft Azure Machine Learning, allowing users to publish a function, e.g. a prediction using a trained machine learning model, as a web service, to be shared with users on Visual Studio, Excel, etc., or consumed within R itself. Users are also available to access their preexisting web services and API endpoints available to their workspace to consume within R. All functions require a workspace ID (WsID) and authorization token (authToken) that can be found on the settings tab of studio.azureml.net, but users can make a temporary free account [here](https://azure.microsoft.com/en-us/pricing/free-trial/). For more information, check out the (GitHub repo)[https://github.com/Azure/Azure-MachineLearning-ClientLibrary-R].
}
\details{
1. Discovery
\itemize{
\item Get endpoints: \code{\link{getEndpoints}}
\item Get web services: \code{\link{getWebServices}}
\item Get web service details \code{\link{getWSDetails}}
\item Get endpoints: \code{\link{getEndpoints}}
\item Get endpoint details: \code{\link{getEPDetails}}
}
2. Publish
blah
\itemize{
\item Publish a new web service: \code{\link{publishWebService}}
\item Update an existing web service: \code{\link{updateWebService}}
}
3. Consume

Просмотреть файл

@ -26,7 +26,7 @@ updateWebService(functionName, serviceName, wsID, inputSchema, outputSchema,
List of webservice details, default endpoint details, and the consumption function
}
\description{
This function updates published code given a valid workspace ID and authentication token. The function expects the function name, service id, and the input and output schemas from the user. The user can expect a list of the web service details, the default endpoint details and the consumption function and use this information to access the published function.
Update a web service, i.e. change the underlying R code that the service will run when called.
}
\seealso{
Other publish: \code{\link{publishWebService}}