1
0
Форкнуть 0

changing demo in light of consumption changes

This commit is contained in:
Alex Wang 2015-07-13 17:56:21 -07:00
Родитель fd57a76cf0
Коммит 5ca7b1c627
6 изменённых файлов: 43 добавлений и 24 удалений

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

@ -90,7 +90,7 @@ predictTitanic(1, "male", "20", "2", "0", "8.50")
# Publish the function
# Go to https://metaanalytics001.cloudapp.net/Home/ViewWorkspace/bbc91d900c3546b695d6507867fc72ae?#Workspace/WebServiceGroups/listWebServiceGroups
# to see published function
TitanicService <- publishWebService("predictTitanic", "TitanicDemo7-10", list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="double"), list("survProb"="double"), wsID, wsAuth)
TitanicService <- publishWebService("predictTitanic", "TitanicDemo", list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="float"), list("survProb"="float"), wsID, wsAuth)
# Currently response is a list of two things:
# new web service details, default endpoint details (using discovery functions)
@ -107,9 +107,12 @@ endpoints <- TitanicService[[2]]
# Consume the new webservice
# First, consume with inputs as a list
# Slow initially as it makes the connection
response <- consumeLists(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list("1", "male", "20", "1", "0", "8.50"), list("1", "female", "20", "1", "0", "8.50"))
response <- consumeDataTable(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list("1", "male", "20", "1", "0", "8.50"), list("1", "female", "20", "1", "0", "8.50"))
# Subsequent calls are faster as connection is left open
response2 <- consumeLists(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list("2", "male", "50", "1", "0", "8.50"), list("2", "female", "50", "1", "0", "8.50"))
response2 <- consumeDataTable(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list("2", "male", "50", "1", "0", "8.50"), list("2", "female", "50", "1", "0", "8.50"))
response3 <- consumeDataTable(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list(1, "male", "20", "2", "0", 8.50))
# consume with inputs as dataframe
# creating test data.frame

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

@ -1,14 +1,14 @@
{
"left" : {
"panelheight" : 1078,
"splitterpos" : 423,
"panelheight" : 958,
"splitterpos" : 375,
"topwindowstate" : "NORMAL",
"windowheight" : 1116
"windowheight" : 996
},
"right" : {
"panelheight" : 1078,
"splitterpos" : 680,
"panelheight" : 958,
"splitterpos" : 604,
"topwindowstate" : "NORMAL",
"windowheight" : 1116
"windowheight" : 996
}
}

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

@ -98,11 +98,15 @@ consumeDataTable <- function(api_key, requestURL, columnNames, ..., globalParam=
#store arguments as mega list of lists
valuesList <- lapply(X=list(...), function(x) x)
#make api call with components of payload
result <- callDTAPI(api_key, requestURL, columnNames, valuesList, globalParam, retryDelay)
resultStored <- jsonlite::fromJSON(result)
resultList = resultStored$Results$output1$value$Values
resultDF <- data.frame(resultList[,(ncol(resultList))])
colnames(resultDF) = "Scored probabilities"
results <- callDTAPI(api_key, requestURL, columnNames, valuesList, globalParam, retryDelay)
results <- jsonlite::fromJSON(results)
resultValues = results$Results$output1$value$Values
# Previous lines were commented out, would not return correctly if there were multiple return values
#resultDF <- data.frame(resultList[,(ncol(resultList))])
#colnames(resultDF) = "Scored probabilities"
resultDF <- data.frame(resultValues$Values)
colnames(resultDF) <- resultValues$ColumnNames
return(resultDF)
}
@ -112,7 +116,7 @@ consumeDataTable <- function(api_key, requestURL, columnNames, ..., globalParam=
#' @param api key must be entered as the first parameter, and must be a string
#' @param requestURL must be entered as the third parameter, and must be a string
#' @param columnNames entered as a list
#' @param ... each parameter must be a request in the format of a list that contains a row of values corresponsing to the column names provided
#' @param ... each parameter must be a request in the format of a list that contains a row of values corresponding to the column names provided
#' @param globalParam global parameters entered as a string, default value is ""
#' @param retryDelay the time in seconds to delay before retrying in case of a server error, default value of 0.3 seconds
#' @return results in a list of lists, with the scored probability at the end of each list
@ -268,7 +272,6 @@ callDTAPI <- function(api_key, requestURL, columnNames, values, globalParam, re
}
return(formatresult)
}
#' This function is a helper that takes in an API key, values in the key value format and column names to pass to the API and the request URL (OData Endpoint Address).
#' It then obtains a response from Azure Machine Learning Studio and returns a response to the consumeFile function.
@ -299,6 +302,7 @@ callAPI <- function(api_key, requestURL, keyvalues, globalParam, retryDelay) {
,GlobalParameters = globalParam
)
body = enc2utf8((rjson::toJSON(req)))
print(body)
#make call to API after constructing request payload
@ -338,6 +342,7 @@ callAPI <- function(api_key, requestURL, keyvalues, globalParam, retryDelay) {
}
discoverSchema <- function(wkID, token, schemes = "https", host = "requestresponse001.cloudapp.net:443", api_version = "2.0") {
# swagger document:
# schemes://hostbasepath/"swagger.json?api-version=2.0"

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

@ -30,9 +30,7 @@ getFramework <- function(tUrl, authToken) {
httpheader=c('Authorization' = auth, 'Content-Type' = "application/json", 'Accept' = "application/json"),
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE,
ssl.verifyhost = FALSE # REMOVE FOR PRODUCTION
)
verbose = TRUE)
# Print results
return(RJSONIO::fromJSON(h$value()))

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

@ -3,7 +3,7 @@
################################################################
publishURL <- "https://management.azureml-int.net/workspaces/%s/webservices/%s" ## REMOVE SSL IGNORING FOR REAL VERSION ##
#publishURL <- "https://hiteshsm.cloudapp.net/workspaces/%s/webservices/%s" ## REMOVE SSL IGNORING FOR REAL VERSION ##
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\n for (i in 1:nrow(inputDF)) {\r\n outputDF[i,] <- do.call(\"action\", as.list(inputDF[i,]))\r\n }\r\nmaml.mapOutputPort(\"outputDF\")"
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\")"
################################################################
@ -74,7 +74,8 @@ getFunctionString <- function (x)
#don't show the full response!
#res
return(gsub("\n", "\r\n", gsub("\"", "\\\"", objs)))
# Might return multiple objects in a list, currently returning first object (BIG ASSUMPTION)
return(gsub("\n", "\r\n", gsub("\"", "\\\"", objs[1])))
}
@ -373,7 +374,8 @@ 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(body)
# Response gatherer
h = RCurl::basicTextGatherer()
h$reset()
@ -387,8 +389,7 @@ publishWebService <- function(functionName, serviceName, inputSchema, outputSche
'Content-Type' = 'application/json',
'Accept' = 'application/json'),
content = body,
writefunction = h$update,
ssl.verifyhost = FALSE) ### REMOVE THIS FOR THE REAL VERSION ###
writefunction = h$update)
# TODO: format output
newService <- RJSONIO::fromJSON(h$value())

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

@ -79,3 +79,15 @@ add2 <- function(x) {
print(toJSON(list("test")))
return(x+a[[1]])
}
testService <- publishWebService("add", "addTest", list("out"="float"), list("in"="float"), wsID, wsAuth)
testEndpoints <- testService[[2]]
response <- consumeDataTable(testEndpoints[[1]]["PrimaryKey"], paste(testEndpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("out"), list("1"), list("2"), list("3"))
testModel <- function(Pclass, Sex, Age, SibSp, Parch, Fare) {
class(GBM.model)
return(list(Pclass, Sex, Age, SibSp, Parch, Fare))
}
TitanicTest <- publishWebService("testModel", "TestingModel7-13", list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="float"), list("Pclass"="string", "Sex"="string", "Age"="int", "SibSp"="int", "Parch"="int", "Fare"="float"), wsID, wsAuth)
testEndpoints <- TitanicTest[[2]]
response3 <- consumeDataTable(endpoints[[1]]["PrimaryKey"], paste(endpoints[[1]]["ApiLocation"], "/execute?api-version=2.0&details=true",sep=""), list("Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"), list(1, "male", "20", "2", "0", 8.50))