BookmarkSubscribeRSS Feed

Calling Python package in Git from SAS Intelligent Decisioning

Started ‎02-28-2024 by
Modified ‎03-04-2024 by
Views 610

or How to best manage large Python sources in SAS Intelligent Decisioning

Decision Flow in Intelligent DecisioningDecision Flow in Intelligent Decisioning

 

SAS Intelligent Decisioning (ID) is great to design and build decisions. It is a great tool to operationalize Analytics. In a graphical (low code no code) manner you can easily design, build, test and deploy decision flows. However sometimes it may be necessary to add some code to a decision flow. In SAS Intelligent Decisioning you can do this by adding SAS DS2 or Python code.

 

The code is stored in code files in ID. Python code files are meant for manageable Python scripts that are not too large. If we want to execute Python scripts which have many lines of code and are broken up into several functions it may be better to develop, test and maintain this code in a dedicated Python environment. The code should then be loaded into SAS Intelligent Decisioning and should have a defined API which is called from within a Python code file in ID.

 

Long Python script in SAS Intelligent Decisioning. Python scrip with package loaded from Git.
02.jpg

03.jpg

In this blog I’m going to show how we can write and version a Python module in a Python development environment and execute the module as part of a decision flow in SAS Intelligent Decisioning.

 

Python development

In general Python developers can build and test their Python module in an environment of their choice, for example VS Code. The development environment should meet the programmer’s needs where they feel comfortable and where they are most productive in.

 

For our scenario we are going to build a simple “Hello Word” program using VS Code to edit & test the code...

Edit & test code in VS Code.Edit & test code in VS Code.

 

...and to version it in GitHub.

Version code in GitHub from VS  Code.Version code in GitHub from VS Code.

The Python module needs to be versioned in GitHub/GitLab. The development environment should be connected to Git either directly via the code editor (Git-plugin) or indirectly through a Git desktop or Web UI. The project in Git needs be configured to create a Python package as this will allow us to load the package into SAS Intelligent Decisioning.

 

Release in GitHub.Release in GitHub.

When the Python code is written, tested and ready to be used we build a release in Git which we are going to reference from SAS Intelligent Decisioning.

 

Creating a release also means we have a dedicated commit point which is ready to be used and is not going to change going forward. Installing the Python code from a release (tarball or zip file) also means we don’t need Git for the installation, as it is not available in the Viya installation.

 

 

 

 

 

 

 

 

 

 

Using the Python code in SAS Intelligent Decisioning

 

Publishing Destination

When a decision flow is created, we need to publish it to an environment from where we are going to execute it. An Administrator can setup different publishing destinations which then can be used in SAS Intelligent Decisioning.

 

When we publish to a Container Publishing Destination  we build a SAS Container Runtime (SCR). SCR is a lightweight Open Container Initiative (OCI) compliant container that scores SAS models and decisions. These containers can be used in your cloud environment as standalone scoring engines or incorporated into an execution pipeline.

 

When we publish a decision flow to a Container Publishing Destination, we can dynamically install Python packages which are added to the container’s Python environment when it is build. We are going to use this feature to install our Python package that resides in Git.

 

Note: To install a Python package from Git you need to have Viya 2023.12 or later.

 

Using Python code from Git repository

When we open a new Python code file in SAS Intelligent Decisioning there are three docstrings at the top of the code file. One docstring is for “DependentPackages” where we can state Python packages that we want to be installed in the container runtime when it is built.

 

To install a package from Git we copy the URL of the release archive file from Git, and generate a personal access token in GitHub/GitLab. In the DependentPackages docstring we paste the Git release archive URL together with the leading Git access token.

Python code file referencing Python package in GitHub.Python code file referencing Python package in GitHub.

 There are different ways of calling a release from Git:

  • Tag: Call a specific release via its tag (as we did above):
    • GitHub: https://<token>@<github-repository>/archive/refs/tags/<tag>.tar.gz
    • GitLab: https:// oauth2:< token >@<github-repository>/archive/refs/tags/<tag>.tar.gz
  • Latest Release: Always trigger the latest release when publishing the decision flow to the Container Publishing Destination:
    • GitHub: https://< token >@<github-repository>/archive/refs/heads/main.tar.gz
    • GitLab: https:// oauth2:< token >@<github-repository>/archive/refs/heads/main.tar.gz
  • Snap-Shot Number: To ensure we always use the same physical git archive; we can use the release snap-shot number. Releases could be deleted and re-build using the same tag. Using the snap-shot number will ensure that we always get the same code as the snap-shot number will change if the release gets re-build.
    • GitHub: https://< token >@<github-repository>/archive/<snap-shop number>.tar.gz
    • GitLab: https:// oauth2:< token >@<github-repository>/archive/<snap-shop number>.tar.gz

Release Snap-Shot Number.Release Snap-Shot Number.

 

When we have put the release URL in the docstring, we can reference the python package by its name via the import statement like any other Python package. In our case the package is called helloworld. We import the package and then call its function hw() from inside the code file’s execute() function.

Calling Python package from within ID Python code file.Calling Python package from within ID Python code file.

We call the entry point of the Python package from the python code file in ID. In terms of the decision flow the Python package is a “black box”. We call it with its input parameters and receive the defeined return parameters. The way it works internally is maintained and managed by the Python developers in the Python development environment.

 

When we have written the Python code file we can add it to the decision flow in Intelligent Decisioning.

Python code file added to decision flow.Python code file added to decision flow.

 

Testing

As the Python package is installed in SCR when the container is built, it is not available in SAS Intelligent Decisioning’s Python environment and therefore we can’t test the decision flow in ID via the Scoring/Test or Scoring/Scenarios tab. In order to test the decision flow, we need to publish it first.

Publish decision flow to container destination.Publish decision flow to container destination.

 

When the decision flow is published, go to the Scoring/Publishing Validation tab. In there, click on the test name for the published decision flow and set the table with the input data for the test. The test table column names and the input parameter names need to match in order to run test the decision flow.

Validate published decision flow.Validate published decision flow.

When the test is finished, we can click on the results icon to check the output from the decision flow.

Decision flow test run result.Decision flow test run result.

When the test result is as expected, the decision flow is ready to be used.

 

Note: To test a published decision flow from within SAS Intelligent Decisioning the publishing destination needs to be configured respectively.

 

Conclusion

SAS Intelligent Decisioning works great with open source like Python. The Python editor in the Python code file in ID is good to develop and test small to medium Python scripts. If you envisage to execute Python scripts in ID with many lines of code, you should consider an external Python development environment and call the Python code as described in this blog. This will help you to maintain the Python code in an appropriate way and to create a better manageable decision flow after all.

 

 

Version history
Last update:
‎03-04-2024 01:11 PM
Updated by:
Contributors

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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

Article Tags