BookmarkSubscribeRSS Feed

Open-source data visualization : Dash, SWAT and SASCTL

Started ‎10-19-2023 by
Modified ‎10-23-2023 by
Views 1,207

In the previous article of this series, I explained how you can load data into the CAS server and display these data into a Dash web application. If you are a data scientist, you may also like to use models and score data while using your web application. In this article, I will explain how you can score data using a model published to SAS Micro Analytics Server.

 

If you want to use the code from this article, you can download it from this GitHub repository.

The model

When scoring data, you first need to have a model to score data against it. For this example, I created a Gradient Boosting model to calculate the price of a car based on its characteristics. The model in this case has been created using SAS Visual Analytics. If you prefer, you can create the model in Python or R and you can use the sasctl package to register the model into SAS Viya. Once registered, the model can then be published to SAS Micro Analytics Server (MAS). This server is designed to execute model scoring in parallel at “atomic” level. By atomic level, I mean one observation at a time. This is ideal when you want to integrate the scoring in a web application as you can get information from the web application and score it based on those inputs. This is exactly what we want to achieve in this article!

 

When the model is published to MAS, it is then accessible using REST APIs. While it is totally fine to directly use the REST APIs as documented, you can also use sasctl package to call the MAS endpoints.

 

The SASCTL package for Python

If you are a data scientist developing models in Python and loading these models into SAS Model Manager, you may already know about the sasctl package for Python. The package was mainly designed to load open-source models into SAS Viya and to handle their deployment into production using the Python language and SAS Viya REST APIs. To serve that purpose, the developers had to create "services" which can be used for model management related tasks, but also for core tasks like handling files and folders as well as authentication. This means that if you are Python developer, this package reduces the time needed to develop a solution as many of the SAS Viya REST APIs endpoints are implemented. And if a specific endpoint is not available, the package also provides a technique to make direct calls to SAS Viya REST APIs which are not defined as a service.

 

Here is a list of the available services:

  • CASManagement
  • Concepts
  • DataSources
  • Files
  • Folders
  • MicroAnalyticScore
  • ModelManagement
  • ModelPublish
  • ModelRepository
  • Projects
  • Relationships
  • ReportImages
  • Reports
  • SASLogon
  • SentimentAnalysis
  • TextCategorization
  • TextParsing
 
All these services implement the basic CRUD operations: Create, Read, Update, Delete. Each service also implements more specific methods like listing the CAS libraries, executing scoring into Micro Analytics Server (MAS), analyzing sentiment, etc.

 

 

The application

In this article, we will not start from scratch. In the previous article, we had a working application and most of the code will still be valid for this article. We will basically add a functionality to the existing application.
 
The final application will look like this:
 
xab_1_DashSasctl_final.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

 
Compared to the previous application, we are adding this card on the right-hand side which displays the result of the model scoring. The input data for the model scoring comes from the selection done in the graph above. Technically it is scoring data similar to the ones in the card displayed on left-hand side. It would have been possible to mix the “real” data and the predicted one, but it is better to separate the two types of information to reduce confusion
 

The code

The code for this application is available in this repository in the file named: dash_sasctl.py. You can compare the content of that file with dash_swat.py if you want to see what was added.

 

In the top part of the code, the import statements have been added as well as the username and password variables. The app.layout has also been modified to introduce the placeholder for the new component.

 

xab_2_DashSasctl_modifications-1024x958.png

 

At the bottom of the file, you will find the callback function which is responsible for populating the score component.

 

xab_3_DashSasctl_callback-1024x982.png

 

The score_data function retrieves information about the selected model and make when the graph is clicked (lines 97 and 98). When done a dataframe containing only the filtered data is created (lines 99 and 100).

 

To execute the model, we need to pass data to it. The model requires all the information available in the dataframe except for Make, Model, Invoice, Origin, MSRP, Length which are excluded from the list created between lines 101 and 106.

 

The last part of the code (lines 107 and 108) is where sasctl is used to create a session and execute the scoring based on the inputs stored in the list variable. When the scoring result is received from MAS, an HTML card is created (lines 109 to 121).camta to display the make and model information in the card header. The card body contains the suggested price which is in fact the result of the model scoring.

 

The demo

 

 

Conclusion

In the previous article, you have seen the basics of building a Dash application which loads data into CAS and displays the loaded data into a graph. The application also displayed the detailed data when the user makes a selection into the graph.

 

In this article, you see also how you can use models deployed to MAS to enrich the application with scored data.

 

In less than 100 lines of python code, we have a working web application with interactions with SAS Viya (CAS and MAS). You can of course build much more complex applications using your preferred language: Python. As you have seen, we can integrate SWAT and SASCTL packages provided by SAS into your Python application. Using this approach, you can also integrate other packages and enrich your application with other functionalities based on your needs. You could for example use this approach to register your python models into SAS Viya, score some data and validate that the results of your model executed directly in python and into MAS give the same results. But this is up to you to imagine your next application!

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
‎10-23-2023 10:34 AM
Updated by:

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started