Skip to content

Commit

Permalink
version 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
pinduzera committed Jun 6, 2023
1 parent ebd3b08 commit 3e72322
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sasctl
Title: The sasctl package enables easy communication between the SAS Viya platform APIs and the R runtime
Version: 0.6.4.9000
Version: 0.7.0
Author: Eduardo Hellas
Authors@R: c(
person(given = "Eduardo",
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sasctl (development version)
# sasctl 0.7.0

* Fixed correct release retrieval when using `session` to connect to Viya 2020.x
* Added `codegen` function in experimental state. Works for simple `lm`, `glm` models and [tidymodels](https://www.tidymodels.org/) `workflow` with regression or classification model mode`
Expand Down
7 changes: 4 additions & 3 deletions R/codegen.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ codegen.workflow <- function(tm_workflow, path = "scoreCode.R", rds = "model.rds
output_list <- list(EM_CLASSIFICATION = predictions[[".pred"]],
EM_EVENTPROBABILITY = predictions[[".pred_<<referenceLevel>>"]],
EM_PROBABILITY = subset(predictions, select = -c(.pred))[boolClass],
EM_PROBABILITY = apply(subset(predictions, select = -c(.pred)), 1, max),
I_<<target>> = predictions[[".pred"]],
<<target>> = predictions[[".pred"]],
<<p_labels>>
Expand Down Expand Up @@ -237,9 +237,10 @@ codegen.workflow <- function(tm_workflow, path = "scoreCode.R", rds = "model.rds
if (!exists("sasctlRmodel"))
{
assign("sasctlRmodel", readRDS(file = paste(rdsPath, "<<rds>>", sep = "")), envir = .GlobalEnv)
<<target_labels_string>>
}
<<target_labels_string>>
data <- data.frame(<<paste(predictors," = ", predictors, collapse = ",\n ")>>)
predictions <- predict(sasctlRmodel, new_data = data, type = "<<response_type>>")
Expand Down
6 changes: 5 additions & 1 deletion R/json_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ write_in_out_json <- function(data, input = TRUE, path = './', noFile = FALSE){
if (is.factor(x)) as.character(x) else x }))

### variable lengths, 8 is default for numeric


size <- sapply(data,
FUN = function(x){
if (is.character(x)) max(sapply(x, nchar)) else {8}}
if (is.character(x)) max(sapply(x, nchar), na.rm = TRUE) else {8}}
)

size[sapply(size, function(x) is.na(x) | x == 0 )] <- 8

df <- data.frame(name = names(vars),
length = size,
type = vars,
Expand Down
52 changes: 41 additions & 11 deletions vignettes/xgb-tidymodel.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ knitr::opts_chunk$set(
)
```

R-sasctl has tools to help you create all the necessary files to upload an R model successfully to SAS Viya Model Manager and be able to take advantage of the many .
R-sasctl has tools to help you create all the necessary files to upload an R model successfully to SAS Viya Model Manager and be able to take advantage of its many features.

## Simple data preparation

Expand Down Expand Up @@ -126,6 +126,8 @@ Scoring codes are required to run R and Python models in SAS Model Manager, `sas

You will notice that the scoring code has many `EM_*` and `P_<<target>><<level>>` variables. They're not required, but it makes very consistent on how models made in SAS Viya UI are created. Making it easier to mix these models.


## Generating the Score code
```{r vars, echo=FALSE, include=FALSE}
used_vars <- paste0("`",colnames(xgb_fitted[["pre"]][["mold"]][["predictors"]]), "`", collapse = ", ")
expected_vars <- paste0("`",colnames(hmeq)[-1], "`", collapse = ", ")
Expand All @@ -139,24 +141,52 @@ We can use the `inputs` to generate alternate input variables. In this case it i
code <- codegen(xgb_fitted,
path = paste0(path, "scoreCode.R"),
inputs = colnames(hmeq)[-1],
referenceLevel = 1,
rds = modelrda)
code
```

Now we create some additional files which are required to configure SAS Model Manager when uploading the files. For the variables specifically, should match the inputs from the model and outputs (outputs may be needed to match what comes out the scoring code)
### Testing the scoring code

```{r test_code}
## getting the .rda Path to be called in the function
## this is simulating what Viya passes to R when calling it
rdsPath <- path
## Calling the generated code string as code to
## create the function locally
codeExpression <- str2expression(code)
eval(codeExpression)
## this is a helper to create the variables
## cat(paste0(colnames(hmeq)[-1], " = hmeq[, '", colnames(hmeq)[-1],"']", collapse = ",\n " ))
scoreRes <- scoreFunction(LOAN = hmeq[, 'LOAN'],
MORTDUE = hmeq[, 'MORTDUE'],
VALUE = hmeq[, 'VALUE'],
REASON = hmeq[, 'REASON'],
JOB = hmeq[, 'JOB'],
YOJ = hmeq[, 'YOJ'],
DEROG = hmeq[, 'DEROG'],
DELINQ = hmeq[, 'DELINQ'],
CLAGE = hmeq[, 'CLAGE'],
NINQ = hmeq[, 'NINQ'],
CLNO = hmeq[, 'CLNO'],
DEBTINC = hmeq[, 'DEBTINC'])
scoreRes <- as.data.frame(scoreRes)
head(scoreRes)
```

```{r creating_files5}
## note required to be correct values, just correct format
scoreddf$I_BAD <- as.character(scoreddf$BAD)
scoreddf$BAD <- as.character(scoreddf$BAD)
scoreddf$EM_EVENTPROBABILITY <- 0.1
scoreddf$EM_PROBABILITY <- 0.1
scoreddf$EM_CLASSIFICATION <- as.character(scoreddf$BAD)


write_in_out_json(hmeq[,-1], input = TRUE, path = path)
write_in_out_json(scoreddf[-3], input = FALSE, path = path)
Now we create some additional files which are required to configure SAS Model Manager when uploading the files. For the variables specifically, should match the inputs from the model and outputs. We can use the result from the scoring code test from the previous chunck. Otherwise you can create a `data.frame` with the expected outputs and pass it.

```{r creating_files5}
write_in_out_json(hmeq[,-1], input = TRUE, path = path)
write_in_out_json(scoreRes, input = FALSE, path = path)
write_fileMetadata_json(scoreCodeName = "scoreCode.R",
scoreResource = modelrda,
Expand Down

0 comments on commit 3e72322

Please sign in to comment.