Accessing Generated Objects

Executing a stream typically involves producing additional output objects. These additional objects might be a new model, or a piece of output that provides information to be used in subsequent executions.

In the example below, the druglearn.str stream is used again as the starting point for the stream. In this example, all nodes in the stream are executed and the results are stored in a list. The script then loops through the results, and any model outputs that result from the execution are saved as an IBM® SPSS® Modeler model (.gm) file, and the model is PMML exported.

import modeler.api

stream = modeler.script.stream()

# Set this to an existing folder on your system.
# Include a trailing directory separator
modelFolder = "C:/temp/models/"

# Execute the stream
models = []
stream.runAll(models)

# Save any models that were created
taskrunner = modeler.script.session().getTaskRunner()
for model in models:
    # If the stream execution built other outputs then ignore them
    if not(isinstance(model, modeler.api.ModelOutput)):
        continue

    label = model.getLabel()
    algorithm = model.getModelDetail().getAlgorithmName()

    # save each model...
    modelFile = modelFolder + label + algorithm + ".gm"
    taskrunner.saveModelToFile(model, modelFile)

    # ...and export each model PMML...
    modelFile = modelFolder + label + algorithm + ".xml"
    taskrunner.exportModelToFile(model, modelFile, modeler.api.FileFormat.XML)

The task runner class provides a convenient way running various common tasks. The methods that are available in this class are summarized in the following table.

Table 1. Methods of the task runner class for performing common tasks
Method Return type Description
t.createStream(name, autoConnect, autoManage) Stream Creates and returns a new stream. Note that code that must create streams privately without making them visible to the user should set the autoManage flag to False.
t.exportDocumentToFile(
documentOutput, filename, 
fileFormat)
Not applicable Exports the stream description to a file using the specified file format.
t.exportModelToFile(modelOutput, filename, fileFormat) Not applicable Exports the model to a file using the specified file format.
t.exportStreamToFile(stream, filename, fileFormat) Not applicable Exports the stream to a file using the specified file format.
t.insertNodeFromFile(filename, diagram) Node Reads and returns a node from the specified file, inserting it into the supplied diagram. Note that this can be used to read both Node and SuperNode objects.
t.openDocumentFromFile(filename, autoManage) DocumentOutput Reads and returns a document from the specified file.
t.openModelFromFile(filename, autoManage) ModelOutput Reads and returns a model from the specified file.
t.openStreamFromFile(filename, autoManage) Stream Reads and returns a stream from the specified file.
t.saveDocumentToFile(
documentOutput, filename)
Not applicable Saves the document to the specified file location.
t.saveModelToFile(modelOutput, filename) Not applicable Saves the model to the specified file location.
t.saveStreamToFile(stream, filename) Not applicable Saves the stream to the specified file location.