Demand Curve Quick Start Guide

The Demand Curve Quick Start Guide demonstrates how to use Wallaroo to chart a demand curve based on submitted data. This example uses a model plus preprocess and postprocessing steps.

This tutorial and the assets can be downloaded as part of the Wallaroo Tutorials repository.

Demand Curve Pipeline Tutorial

This worksheet demonstrates a Wallaroo pipeline with data preprocessing, a model, and data postprocessing.

The model is a “demand curve” that predicts the expected number of units of a product that will be sold to a customer as a function of unit price and facts about the customer. Such models can be used for price optimization or sales volume forecasting. This is purely a “toy” demonstration, but is useful for detailing the process of working with models and pipelines.

Data preprocessing is required to create the features used by the model. Simple postprocessing prevents nonsensical estimates (e.g. negative units sold).

Prerequisites

  • An installed Wallaroo instance.
  • The following Python libraries installed:
    • os
    • wallaroo: The Wallaroo SDK. Included with the Wallaroo JupyterHub service by default.
import json
import wallaroo
from wallaroo.object import EntityNotFoundError
import pandas
import numpy
import conversion
from wallaroo.object import EntityNotFoundError
import pyarrow as pa

# used to display dataframe information without truncating
from IPython.display import display
import pandas as pd
pd.set_option('display.max_colwidth', None)

# ignoring warnings for demonstration
import warnings
warnings.filterwarnings('ignore')

Connect to the Wallaroo Instance

The first step is to connect to Wallaroo through the Wallaroo client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.

This is accomplished using the wallaroo.Client() command, which provides a URL to grant the SDK permission to your specific Wallaroo environment. When displayed, enter the URL into a browser and confirm permissions. Store the connection into a variable that can be referenced later.

If logging into the Wallaroo instance through the internal JupyterHub service, use wl = wallaroo.Client(). For more information on Wallaroo Client settings, see the Client Connection guide.

# Login through local Wallaroo instance

wl = wallaroo.Client()

Now that the Wallaroo client has been initialized, we can create the workspace and call it demandcurveworkspace, then set it as our current workspace. We’ll also create our pipeline so it’s ready when we add our models to it.

We’ll set some variables and methods to create our workspace, pipelines and models. Note that as of the July 2022 release of Wallaroo, workspace names must be unique. Pipelines with the same name will be created as a new version when built.

workspace_name = 'demandcurveworkspace'
pipeline_name = 'demandcurvepipeline'
model_name = 'demandcurvemodel'
model_file_name = './models/demand_curve_v1.onnx'
workspace = wl.get_workspace(name=workspace_name, create_if_not_exist=True)

wl.set_current_workspace(workspace)

demandcurve_pipeline = wl.build_pipeline(pipeline_name)
demandcurve_pipeline
namedemandcurvepipeline
created2024-04-16 19:38:36.714333+00:00
last_updated2024-04-16 19:38:36.714333+00:00
deployed(none)
archNone
accelNone
tags
versions79b65e34-675d-4bdc-b764-1299f30fa26f
steps
publishedFalse

With our workspace established, we’ll upload three models:

  • ./models/preprocess_dc_byop.zip: A preprocess model step that formats the data into a tensor that the model can inference from.
  • ./models/demand_curve_v1.onnx: Our demand_curve model. We’ll store the upload configuration into demand_curve_model.
  • ./models/postprocess_dc_byop.zip: A postprocess model step that will zero out any negative values and return the output variable as “prediction”.

Note that the order we upload our models isn’t important - we’ll be establishing the actual process of moving data from one model to the next when we set up our pipeline.

demand_curve_model = wl.upload_model(model_name, 
                                     model_file_name, 
                                     framework=wallaroo.framework.Framework.ONNX
                                     ).configure(tensor_fields=["tensor"])
input_schema = pa.schema([
    pa.field('Date', pa.string()),
    pa.field('cust_known', pa.bool_()),
    pa.field('StockCode', pa.int32()),
    pa.field('UnitPrice', pa.float32()),
    pa.field('UnitsSold', pa.int32())
])

output_schema = pa.schema([
    pa.field('tensor', pa.list_(pa.float64()))
])

preprocess_step = wl.upload_model('curve-preprocess', 
                                  './models/preprocess_dc_byop.zip', 
                                  framework=wallaroo.framework.Framework.CUSTOM, 
                                  input_schema=input_schema, 
                                  output_schema=output_schema)
Waiting for model loading - this will take up to 10.0min.
Model is pending loading to a container runtime..
Model is attempting loading to a container runtime...........successful

Ready

input_schema = pa.schema([
    pa.field('variable', pa.list_(pa.float64()))
])

output_schema = pa.schema([
    pa.field('prediction', pa.list_(pa.float64()))
])

postprocess_step = wl.upload_model('curve-postprocess', 
                                   './models/postprocess_dc_byop.zip', 
                                   framework=wallaroo.framework.Framework.CUSTOM, 
                                   input_schema=input_schema, 
                                   output_schema=output_schema)
Waiting for model loading - this will take up to 10.0min.
Model is pending loading to a container runtime..
Model is attempting loading to a container runtime..........successful

Ready

With our models uploaded, we’re going to create our own pipeline and give it three steps:

  • The preprocess step to put the data into a tensor format.
  • Then we apply the data to our demand_curve_model.
  • And finally, we prepare our data for output with the module_post.
# now make a pipeline
demandcurve_pipeline.undeploy()
demandcurve_pipeline.clear()
demandcurve_pipeline.add_model_step(preprocess_step)
demandcurve_pipeline.add_model_step(demand_curve_model)
demandcurve_pipeline.add_model_step(postprocess_step)
namedemandcurvepipeline
created2024-04-16 19:38:36.714333+00:00
last_updated2024-04-16 19:38:36.714333+00:00
deployed(none)
archNone
accelNone
tags
versions79b65e34-675d-4bdc-b764-1299f30fa26f
steps
publishedFalse

And with that - let’s deploy our model pipeline. This usually takes about 45 seconds for the deployment to finish.

deploy_config = wallaroo.DeploymentConfigBuilder().replica_count(1).cpus(1).memory("1Gi").build()
demandcurve_pipeline.deploy(deployment_config=deploy_config)
namedemandcurvepipeline
created2024-04-16 19:38:36.714333+00:00
last_updated2024-04-16 19:40:58.596389+00:00
deployedTrue
archx86
accelnone
tags
versions182cdcee-ef04-416e-a4dd-964e3b3a2e9c, 79b65e34-675d-4bdc-b764-1299f30fa26f
stepsdemandcurvemodel
publishedFalse

Everything is ready. Let’s feed our pipeline some data. We have some information prepared with the daily_purchasses.csv spreadsheet. We’ll start with just one row to make sure that everything is working correctly.

# read in some purchase data
purchases = pandas.read_csv('daily_purchases.csv')

# start with a one-row data frame for testing
subsamp_raw = purchases.iloc[0:1,: ]
subsamp_raw
Datecust_knownStockCodeUnitPriceUnitsSold
02010-12-01False219284.211
result = demandcurve_pipeline.infer(subsamp_raw)
display(result)
timein.Datein.StockCodein.UnitPricein.UnitsSoldin.cust_knownout.predictionanomaly.count
02024-04-16 19:41:44.7062010-12-01219284.211False[6.680255142999893]0

We can see from the out.prediction field that the demand curve has a predicted slope of 6.68 from our sample data. We can isolate that by specifying just the data output below.

display(result.loc[0, ['out.prediction']][0])
[6.680255142999893]

Bulk Inference

The initial test went perfectly. Now let’s throw some more data into our pipeline. We’ll draw 10 random rows from our spreadsheet, perform an inference from that, and then display the results and the logs showing the pipeline’s actions.

ix = numpy.random.choice(purchases.shape[0], size=10, replace=False)
converted = conversion.pandas_to_dict(purchases.iloc[ix,: ])
test_df = pd.DataFrame(converted['query'], columns=converted['colnames'])
display(test_df)

output = demandcurve_pipeline.infer(test_df)
display(output)
Datecust_knownStockCodeUnitPriceUnitsSold
02011-09-09False207122.084
12010-12-07True219281.9521
22011-10-11True232032.0840
32011-01-26False223854.131
42011-09-04True232022.0826
52011-02-20True219291.9510
62010-12-09False226635.061
72010-12-01False85099F4.211
82011-07-25False224114.133
92011-11-11True219281.79100
timein.Datein.StockCodein.UnitPricein.UnitsSoldin.cust_knownout.predictionanomaly.count
02024-04-16 19:41:56.2872011-09-09207122.084False[9.110871233285868]0
12024-04-16 19:41:56.2872010-12-07219281.9521True[40.57067616108544]0
22024-04-16 19:41:56.2872011-10-11232032.0840True[33.125327529877765]0
32024-04-16 19:41:56.2872011-01-26223854.131False[6.7715457962084376]0
42024-04-16 19:41:56.2872011-09-04232022.0826True[33.125327529877765]0
52024-04-16 19:41:56.2872011-02-20219291.9510True[40.57067616108544]0
62024-04-16 19:41:56.2872010-12-09226635.061False[5.71029113645628]0
72024-04-16 19:41:56.2872010-12-0185099F4.211False[6.680255142999893]0
82024-04-16 19:41:56.2872011-07-25224114.133False[6.7715457962084376]0
92024-04-16 19:41:56.2872011-11-11219281.79100True[49.73419582342663]0

Undeploy the Pipeline

Once we’ve finished with our demand curve demo, we’ll undeploy the pipeline and give the resources back to our Kubernetes cluster.

demandcurve_pipeline.undeploy()
namedemandcurvepipeline
created2024-04-16 19:38:36.714333+00:00
last_updated2024-04-16 19:40:58.596389+00:00
deployedFalse
archx86
accelnone
tags
versions182cdcee-ef04-416e-a4dd-964e3b3a2e9c, 79b65e34-675d-4bdc-b764-1299f30fa26f
stepsdemandcurvemodel
publishedFalse

Thank you for being a part of this demonstration. If you have additional questions, please feel free to contact us at Wallaroo.