Parameters
Parameters provide a useful way of passing values at runtime, rather than hard coding them
directly in a script. Parameters and their values are defined in the same way as for flows; that is,
as entries in the parameters table of a flow, or as parameters on the command line. The
Stream
class implements a set of functions defined by the
ParameterProvider
object as shown in the following table. Session
provides a getParameters()
call which returns an object that defines those
functions.
Method | Return type | Description |
---|---|---|
p.parameterIterator() |
Iterator | Returns an iterator of parameter names for this object. |
p.getParameterDefinition( parameterName) |
ParameterDefinition |
Returns the parameter definition for the parameter with the specified name, or
None if no such parameter exists in this provider. The result may be a snapshot of
the definition at the time the method was called and need not reflect any subsequent modifications
made to the parameter through this provider. |
p.getParameterLabel(parameterName) |
string | Returns the label of the named parameter, or None if no such
parameter exists. |
p.setParameterLabel(parameterName, label) |
Not applicable | Sets the label of the named parameter. |
p.getParameterStorage( parameterName) |
ParameterStorage |
Returns the storage of the named parameter, or None if no
such parameter exists. |
p.setParameterStorage( parameterName, storage) |
Not applicable | Sets the storage of the named parameter. |
p.getParameterType(parameterName) |
ParameterType |
Returns the type of the named parameter, or None if no such
parameter exists. |
p.setParameterType(parameterName, type) |
Not applicable | Sets the type of the named parameter. |
p.getParameterValue(parameterName) |
Object | Returns the value of the named parameter, or None if no such
parameter exists. |
p.setParameterValue(parameterName, value) |
Not applicable | Sets the value of the named parameter. |
In the following example, the script aggregates some Telco data to find which region has the lowest average income data. A flow parameter is then set with this region. That flow parameter is then used in a Select node to exclude that region from the data, before a churn model is built on the remainder.
The example is artificial because the script generates the Select node itself and could therefore have generated the correct value directly into the Select node expression. However, flows are typically pre-built, so setting parameters in this way provides a useful example.
The first part of this example script creates the flow parameter that will contain the region with the lowest average income. The script also creates the nodes in the aggregation branch and the model building branch, and connects them together.
import modeler.api
stream = modeler.script.stream()
# Initialize a flow parameter
stream.setParameterStorage("LowestRegion", modeler.api.ParameterStorage.INTEGER)
# First create the aggregation branch to compute the average income per region
sourcenode = stream.findByID("idGXVBG5FBZH")
aggregatenode = modeler.script.stream().createAt("aggregate", "Aggregate", 294, 142)
aggregatenode.setPropertyValue("keys", ["region"])
aggregatenode.setKeyedPropertyValue("aggregates", "income", ["Mean"])
tablenode = modeler.script.stream().createAt("table", "Table", 462, 142)
stream.link(sourcenode, aggregatenode)
stream.link(aggregatenode, tablenode)
selectnode = stream.createAt("select", "Select", 210, 232)
selectnode.setPropertyValue("mode", "Discard")
# Reference the flow parameter in the selection
selectnode.setPropertyValue("condition", "'region' = '$P-LowestRegion'")
typenode = stream.createAt("type", "Type", 366, 232)
typenode.setKeyedPropertyValue("direction", "Drug", "Target")
c50node = stream.createAt("c50", "C5.0", 534, 232)
stream.link(sourcenode, selectnode)
stream.link(selectnode, typenode)
stream.link(typenode, c50node)
The example script creates the following flow.
