User-Defined Functions (UDF)
    • Dark
      Light

    User-Defined Functions (UDF)

    • Dark
      Light

    Article summary

    Introduction

    This tutorial guides through the process of creating user-defined functions. These functions facilitate the development of custom business logic, which can be stored as an API. This API can then serve as a building block for an application or as a tool for integrating information with other enterprise systems.

    Understanding the Framework

    By understanding these two key concepts, you'll be better equipped to create and customize bespoke applications within the Sight Machine platform.

    Bespoke Analytics Framework

    • What is it?

      This framework provides a structured way to create custom applications within the Sight Machine platform. It uses a configuration-driven approach, meaning you define the application's structure and behavior through configuration files rather than writing extensive code.

    • Why is it important?

      The Bespoke Analytics Framework simplifies the process of building custom applications, making it more accessible to users with less technical expertise. It also ensures consistency and maintainability across different applications.

    User-Defined Function

    • What is it?

      UDFs are custom functions that you can write to perform specific calculations or data manipulations within your bespoke applications. They allow you to tailor the analytics to your exact needs.

    • Why is it important?

      UDFs provide flexibility and customization, enabling you to create bespoke applications that are tailored to your specific use cases and data requirements.

    Environments

    Having an understanding of the different environment application allows you to test and iterate on new applications without disrupting the live system. This helps to ensure the stability and reliability of your applications.

    • Test Environment: The development or testing environment is where you can create and experiment with new applications without affecting the production environment.

    • Live Environment: The production or live environment is where your live applications are deployed and used by end-users.

    Creating a User-Defined Function: Local Development

    Local Development involves developing UDFs on your own computer using a Jupyter Notebook. This approach gives you more flexibility and control over your development environment, but it also requires setting up additional tools and managing dependencies.

    ⚠️ Note: This workflow is recommended for Voltron 2.

    Creating from a Template

    Step 1: Open the Hamburger Menu in the upper right corner.

    Step 2: Select the Dev Panel option from the menu.

    Please note, this option is only accessible to users with a developer role or higher.

    Step 3: Locate and select the User Defined Notebook API. This is where the list of user-defined functions is housed.

    In this environment, no existing or saved user-defined functions are present. It provides instructions on how to access ODBC within the notebook and how to execute it.

    Step 4: Download the template file. This file, a Jupyter notebook, includes an example of creating a user-defined function.

    Downloading and Accessing the Notebook File

    This guide explains how to download a Jupyter notebook file, modify it, and upload it as a user-defined function.

    Step 1: Download the notebook file as a template.

    Step 2: Open the terminal and start your Jupyter notebook.

    Step 3: Navigate to the location of the downloaded file. For this example, the file is in the Downloads folder.

    Modifying the UDF Template

    Step 1: After opening the template, you will notice 3 distinct sections.

    Section 1: Initialize Parameters

    The first section initializes parameters and contains a parameter tag. These can be modified by passing in new values to the API, such as date parameters or different machines.  In the example below, a, b, and c are all parameters initialized in the first cell.

    Section 2: Business Logic

    The next section contains the business logic.  This is a simple example and in practice this can take up as many cells as is necessary.

    Section 3: API Response

    The final cell returns the output via the API response.

    Step 1: Modify the template as needed.

    Step 2: Execute the code to see the expected result. In this example, without passing any parameters, the API response should be 2.

    Step 3: Save your notebook.

    Uploading the Modified Notebook File

    Step 1: Return to the developer page and choose the saved file from the appropriate location.

    Step 2: Provide a name for the UDF. This will be the name of the API that's being called.

    Step 3: Provide a description for the function.

    Step 4: Select "upload". A confirmation message will appear indicating a successful upload.

    The new function is now visible, complete with its description and the time it was last saved, and ready to run!

    Creating a User-Defined Function: In-Platform Development

    In-platform development provides a convenient and streamlined way to create User-Defined Functions (UDFs) directly within the Sight Machine platform. This approach eliminates the need for setting up a local development environment and simplifies the process of creating and managing UDFs.

    By utilizing the in-platform development tools, you can efficiently write, test, and deploy UDFs without leaving the Sight Machine platform. This can significantly reduce development time and effort, allowing you to focus on creating powerful and customized analytics for your bespoke applications.

    ⚠️ Note: This workflow is recommended for Voltron 1.

    Creating from a MA Notebook

    Step 1: Open the Hamburger Menu in the upper right corner.

    Step 2: Select the Dev Panel option from the menu.

    Please note, this option is only accessible to users with a developer role or higher.

    Step 3: Locate and select the MA Notebooks link. This is where notebooks can be developed and saved as UDFs.

    Creating a New Notebook

    The MA Notebooks page displays a list of all created notebooks and their current status, a standard feature for Jupyter notebooks.

    To create a new notebook, follow these steps:

    Step 1: Select the New notebook option from the header.

    Image

    Step 2: Select Python (udf) as the notebook kernel.

    Image

    After selecting, the new notebook appears in a separate tab, offering a familiar notebook experience.

    Notebook Development and Testing

    As in the local development template example, your notebook should contain three distinct sections:

    Section 1: Initialize Parameters

    The first section initializes parameters and contains a parameter tag. These can be modified by passing in new values to the API, such as date parameters or different machines.  In the example below, ab, and c are all parameters initialized in the first cell. As an alternative to defining parameters in the first cell, you can tag any cell with the parameters tag.

    Section 2: Business Logic

    The next section contains the business logic.  This is a simple example and in practice this can take up as many cells as is necessary.

    Section 3: API Response

    The final cell returns the output via the API response. 

    Saving the Notebook as a UDF

    This tutorial outlines the process of saving a new user-defined function (UDF) within a notebook and uploading it to the UDF section.

    Step 1: Finalize the new user-defined function within the notebook and select the Save as UDF option within the notebook.

    Image

    Step 2: Assign a name to the UDF, such as "My Data Quality Test".

    Image

    Step 3: Input a description for the UDF.

    Image

    Step 4: Select upload. A confirmation message will appear upon successful upload.

    Image

    Step 5: The UDF has been saved and you can navigate to the User Defined Notebooks API section within the /dev page for confirmation.

    Image

    Image

    Running Your UDF

    1. After uploading, an api request can be POSTed to {environment_name}.sightmachine.io/v1/udf/task/async with the following body format (parameters are optional):

      {
          "name": <notebook name>,
          "parameters": {
       &nbs