Enframe — Building Your App. by Jamie Green
By: Opex Analytics
Tags: Artificial Intelligence, Software Engineering, Deployment, Data Science, Data

Enframe — Building Your App

by Jamie Green

Edit on Aug 17, 2020: Note that while the Enframe product is the same, the name has changed to App Studio OpenX, or OpenX for short, and is part of the llama.ai platform.

You might have read about our deployment product, Enframe, in a prior blog post. If not, give it a quick read and then come right back (we’ll wait!).

Ready now? Great! Let’s take a quick tour through the development process of Enframe.

Starting Files

An Enframe app consists of four folders: Inputs , Outputs , Scripts , and Config . Let’s use a classic scikit-learn dataset and sample task that’s familiar to most data scientists: the Iris clustering problem.

Inputs

The Inputs folder will contain a .csv file for each of your intended input tables. Each .csv just needs to have a row of column names. As long as that requirement is satisfied, Enframe will automatically convert those files and columns into a PostgreSQL database schema. You can also include data in the file, which will be loaded into your app by default, or leave it blank to add your own data later.

For our Iris app, we’ll call our input table “Iris”, so we’ll call the file iris.csv .

Outputs

Like Inputs, you’ll just have a .csv file that represents your output table schema. For this app, our output table will just be the same as the “Iris” input table but with an extra column for the cluster assignments. We’ll call this file iris_clusters.csv .

Config

To start, your Config folder will be empty. This is used for uploading existing applications and adding custom features. The Enframe app creation process will help you configure your app and generate the config files automatically.

Scripts

This is where your code lives. Typically you’ll want to call your main file execute.py (or execute.R for the R-inclined), to distinguish it from any other files you might want to include.

We can skip some of the specifics of the Iris clustering code, but let’s look at the code that’s specifically required for Enframe.

First, we’ll import the class for connecting to Enframe via the Enframe Connector API and instantiate an object of that class:

from enConn import DatabaseClient ### READ IN DATA ### db = DatabaseClient()

Next let’s read the data and parameters from PostgreSQL using the API:

# read in iris table from database

iris = db.read_table(table='iris') # read parameters from database

parameter_df = db.read_table(table='parameters') # get the 'Number of Clusters' parameter from parameter dataframe

parameter_dict = {

row['displayname']: row['value'] for ind, row in

parameter_df.iterrows()

}

clusters = int(parameter_dict['Number of Clusters'])

We can then run our clustering model using the scikit-learn KMeans implementation of the k-means algorithm. Note that we can use the clusters parameter as the n_clusters argument for the KMeans function:

### CLUSTERING MODEL ###

import pandas as pd

from sklearn.preprocessing import scale

from sklearn.cluster import KMeans iris_clusters = iris # standardize data

clust_data = pd.DataFrame(scale(iris.iloc[:, 0:4]), index=iris.index, columns=iris.iloc[:, 0:4].columns) # run K-means for optimal size

kmeans = KMeans(n_clusters=clusters, random_state=0, max_iter=1000,

n_init=100).fit(clust_data) # attach the clusterid column back to original data

iris_clusters[“cluster_id”] = kmeans.labels_

iris_clusters = iris_clusters[[“sepal_length”, “sepal_width”, “petal_length”, “petal_width”, “species”, “cluster_id”]]

Lastly, we’ll write our results to the database, overwriting any old data from a previous analysis:

### WRITE DATA TO DATABASE ###

db.write_table_from_dataframe(table_name=’iris_clusters’,

dataframe=iris_clusters, append_records=False, is_new=False)

Configure Your App with Enframe’s UI

Now that we have the four required folders, we can start configuring our app. The first thing we need to do is name our app and upload the aforementioned folders:

Next, we can customize the app’s display name and description:

After that, we can further configure our Inputs, Outputs, and Parameters.

Inputs

Enframe has already taken our iris.csv file and loaded it into this section. But maybe we want to change a table or column’s display name (e.g., use capitalization and spaces instead of underscores for display purposes). We can use the Edit Column menu to make these changes. (Note that Enframe automatically detects data type based on the file you used in your input.):

Outputs

It’s a cinch to make changes to the outputs as well. Let’s say we want to change the data type for Cluster ID to type Text to treat them as categories. Just navigate to Edit Columns and switch up the data type with a simple click:

Parameters

Let’s make a parameter called “Number of Clusters” that requires a Numeric input from the user:

Script/Visualizations

Enframe will automatically recognize a file called execute.py as the main script and choose python3 as the language. If you called your file something else, or want to change which script gets run, you can set that here. Note that you can also choose any of the other execute commands listed here:

Lastly, if we wanted to customize the app’s visuals, this is the stage in which we could do so. There are many options available: we can change the names of the tabs, modify the colors and/or theme, add other configuration options, and more. Let’s switch to a custom theme, then we’re all set to deploy:

And we’re done! Our Input Data page has our Iris data, the Parameters page allows us to control the number of clusters and run the script, and the Output Data is populated with our analyzed data. We can also add a Tableau dashboard to visualize those clusters, but we’ll save the how-to for another day.