PRESENTATION - Ask The Expert - How Do I Integrate SAS Viya and Open Source
PRESENTATION - Ask The Expert - How Do I Integrate SAS Viya and Open Source
Open Source
Ask the Expert
Melodie Rush
Customer Success Principal Data Scientist
Connect with me:
LinkedIn: https://www.linkedin.com/in/melodierush
Twitter: @Melodie_Rush
Programming Interfaces
API Interfaces
• Python or R code
• Downloads a sample of data
to CAS controller
• Runs code natively in Python
or R installed on the CAS
controller
• Brings results back to CAS for
comparison in Model Studio
1. Precursor code
3. USER CODE
#-------------------------------------------------------------------------------
# Language: PYTHON node’s working directory
#-------------------------------------------------------------------------------
# Variable declarations
dm_nodedir = '/xxx/xxx/xxx/xxx'
dm_dec_target = 'BAD'
dm_partitionvar = '_PartInd_'
dm_partition_train_val = 1
dm_class_input = ["IMP_DELINQ","IMP_DEROG","IMP_JOB","IMP_NINQ","IMP_REASON"]
dm_interval_input = ["IMP_CLAGE","IMP_CLNO","IMP_DEBTINC","IMP_MORTDUE","IMP_VALUE","IMP_YOJ" ,"LOAN"]
dm_input = dm_class_input + dm_interval_input
# Variable declarations
dm_dec_target <- 'BAD'
dm_partitionvar <- '_PartInd_'
dm_partition_train_val <- 1
#-------------------------------------------------------------------------------
# Generate data frame: Y
#-------------------------------------------------------------------------------
import pandas as pd
dm_inputdf = pd.read_csv(dm_nodedir + '/node_data.csv')
#-------------------------------------------------------------------------------
# Generate data frame: Y
#-------------------------------------------------------------------------------
dm_inputdf <- read.csv(file='node_data.csv', stringsAsFactors=TRUE, check.names=FALSE)
#-------------------------------------------------------------------------------
# Generate data frame: Y; Supervised model
#-------------------------------------------------------------------------------
dm_scoreddf.to_csv(path_or_buf=dm_nodedir + '/node_scored.csv', encoding='utf-8', index=False,
float_format='%g')
#-------------------------------------------------------------------------------
# Generate data frame: Y; Supervised model
#-------------------------------------------------------------------------------
write.csv(dm_scoreddf, file='node_scored.csv', row.names=FALSE, fileEncoding='UTF-8')
To add Python
code to every
Open Source Code
node use Project
Settings → Node
Configuration
This is the error returned by SAS and it is not descriptive. Clicking it will move the log to where the error
occurred.
Copyr i ght © SAS I nsti tute I nc . Al l r i ghts reser ved.
Debugging in Model Studio
Scroll up to find the output returned by python
This will tell you which line of code encountered the error and what the error was – in this case an
indentation error.
Copyr i ght © SAS I nsti tute I nc . Al l r i ghts reser ved.
Summary
Support Register,
Be part of an Publish or
Ensemble Download score
code or score API
SAS Scripting Wrapper for Analytics Transfer SWAT for Python GitHub
(SWAT) allows you to access data stored on SWAT for R GitHub
Viya and use Viya-enabled procedures from
Viya Programming Guide
Python and R.
Open to SAS®
Controller
APIs
df = s.CASTable(‘hmeq’)
df.head(10)
CAS Action
[table.fetch]
table.name = “hmeq”
from = 1 to = 10
sas.decisionTree.forestTrain(
table = dict(),
target = string,
inputs = value_list,
nominals = value_list,
ntree = int32,
casOut = dict()
)
Copyr i ght © SAS I nsti tute I nc . Al l r i ghts reser ved.
R SWAT example
Print 5 rows
Register
Validate
Deploy
Monitor
• Automatically generate
executable scoring code for
Python-based models
• Validate model is executable,
using representative test data set
• View output table, code, and log
• Publish Validation ensures model
is executing correctly in
production environment
sasctl
translation
An API is the messenger that takes a request, tells a system what you
want to do and then returns the response back to you.
• A RESTful API is an application program interface (API) that uses HTTP
requests to GET, PUT, POST and DELETE data. An API for a website is
code that allows two software programs to communicate with each
other.
• “REST stands for REpresentational State Transfer”
• “API means Application Programming Interface”
APIs for application developers APIs for analysts and data scientist
and admins • Designed for data scientist,
• designed for enterprise programmers and administrators
application developers who need to interact with CAS
• intend to build on the work of directly
model builders and data • Used to executing CAS actions,
scientists, to deliver apps based managing CAS sessions,
on SAS Viya technology monitoring the system and
inspecting the CAS grid
developer.SAS.com
Copyr i ght © SAS I nsti tute I nc . Al l r i ghts reser ved.
REST APIs
Scoring API
• Python objects enable you to embed and import Python functions into SAS
programs
• The Python code is not converted to SAS code. Instead, the Python code runs in
the Python interpreter of your choice and returns the results to SAS.
• With a small Python code modification, you can run your Python functions from
SAS and easily program in both languages at the same time.
• SUBMIT INTO - Write a block of Python code into the Python object
• APPEND – write a single line of Python code into the Python object
• PUBLISH – taking the source code in our Python object and submitting
it to the Python interpreter
• CALL – accessing the Python function in SAS Data Step or PROC FCMP
• CLEAR – Python object is cleaned of all knowledge of any previous
Python source code
• Allows you to pass a Python or R executable file to the Java class SASJavaExec
• SASJavaExec runs the Python or R script with its command line argument and
passes any output or reports any errors back to the SAS Viya log
• Useful if
– Need to submit a Python or R script to a compute server or remote environment
– Want to include DLPy models in a pipeline
Using Python with Model Studio for SAS Visual Data Mining
and Machine Learning – Paper
Incorporating Python into your SAS Visual Data Mining and
Machine Learning Pipeline - Video
Copyr i ght © SAS I nsti tute I nc . Al l r i ghts reser ved.
Base SAS Java Object
Executes a Python or R file
/* Execute Python script */
data _null_;
length rtn_val 8;
declare javaobj
j("com.sas.analytics.datamining.servertier.SASPythonExec",
"&dm_nodedir&dm_dsep.dm_srcfile.py");
j.callVoidMethod("setOutputFile",
"&dm_nodedir&dm_dsep&lang._output.txt");
j.callIntMethod("executeProcess", rtn_val);
j.delete();
call symput('javaobj_rtnval', rtn_val);
run;
sas.com
FREE Training
Learn from home – free for 30 days. Get software labs to practice and online support if needed.
Newsletters
Get the latest SAS news plus tips, tricks and more.
Users Groups
Meet local SAS users, network and exchange ideas – virtually.
SAS Profile
If you haven’t already done so, create your SAS Profile to access free training, SAS Support Communities, technical support, software
downloads, newsletters and more.