<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 VisTrails API example
This notebook showcases the new API. Inlined are some comments and explanations.
Step1:  The new API is exposed under the top-level vistrails package. The moment you use one of the API functions, like load_vistrail(), it will create an application and load the same configuration that the VisTrails application uses (although it will automatically enable packages the moment you need them).
Step2:  This explicitely requests IPythonMode to be enabled on output modules, so that pipeline executions will put results on the notebook (similarly to %matplotlib inline for matplotlib plots).
Vistrails and Pipelines
You can get a Vistrail through load_vistrail().
Step3:  A Vistrail is a whole version tree, where each version is a different pipeline. From it we can get Pipelines, but it is also stateful (i.e. has a current version); this is useful for editing (creating new versions from the current one). It also provides the interface that Pipeline has, implicitely acting on the current_pipeline.
If GraphViz is available, Vistrail and Pipeline will be rendered in the IPython notebook.
Step4:  Packages
Only basic_modules (and abstractions?) are loaded on initialization, so that using the API stays fast. A package might be auto-enabled when it is requested, which is efficient and convenient.
Note that load_package() only accepts package identifiers.
Step5:  You can get Modules from the package using the dot or bracket syntax. These modules are "dangling" modules, not yet instanciated in a specific pipeline/vistrail.
These will be useful once editing pipelines is added to the API.
Step6:  Execution
In addition to executing a Pipeline or Vistrail, you can easily pass values in on InputPort modules (to use subworkflows as Python functions) and get results out (either on OutputPort modules or any port of any module).
Execution returns a Results object from which you can get all of this. In addition, output modules (such as matplotlib's MplFigureOutput) will output to the IPython notebook if possible.
Gets output
Step7:  This gets the value on any output port of any module (no need to insert OutputPort or GenericOutput modules, if you know how to find the module)
Step8:  This gets the value passed to an OutputPort module, using the OutputPort's name
Step9:  Sets inputs
Step10:  We need to provide value to this workflow, for its two InputPort modules. Input can be supplied to execute() in two ways
Step11:  Other example
Step12:  Note that if you print a File value, IPython will try to render it.
Step13:  Output mode
Step14:  This workflow uses MplFigureOutput, which outputs to the IPython notebook if available (and since the spreadsheet is not running).
Step15:  This one uses RichTextOutput
Step16:  TableOutput
Step17:  And vtkRendererOutput
Step19:  Pipeline manipulation
The API wrapper doesn't currently provide easier methods to manipulate pipelines. This is mainly because these operations need all of the VisTrails concepts anyway.
You can however call the controller methods on vistrail.controller directly, using modules obtained through load_package().
<ASSISTANT_TASK:>
Python Code:
import vistrails as vt
Explanation: VisTrails API example
This notebook showcases the new API. Inlined are some comments and explanations.
End of explanation
vt.ipython_mode(True)
Explanation: The new API is exposed under the top-level vistrails package. The moment you use one of the API functions, like load_vistrail(), it will create an application and load the same configuration that the VisTrails application uses (although it will automatically enable packages the moment you need them).
End of explanation
vistrail = vt.load_vistrail('simplemath.vt')
Explanation: This explicitely requests IPythonMode to be enabled on output modules, so that pipeline executions will put results on the notebook (similarly to %matplotlib inline for matplotlib plots).
Vistrails and Pipelines
You can get a Vistrail through load_vistrail().
End of explanation
vistrail
vistrail.select_latest_version()
vistrail
vistrail.get_pipeline(2)
Explanation: A Vistrail is a whole version tree, where each version is a different pipeline. From it we can get Pipelines, but it is also stateful (i.e. has a current version); this is useful for editing (creating new versions from the current one). It also provides the interface that Pipeline has, implicitely acting on the current_pipeline.
If GraphViz is available, Vistrail and Pipeline will be rendered in the IPython notebook.
End of explanation
tabledata = vt.load_package('org.vistrails.vistrails.tabledata')
tabledata
Explanation: Packages
Only basic_modules (and abstractions?) are loaded on initialization, so that using the API stays fast. A package might be auto-enabled when it is requested, which is efficient and convenient.
Note that load_package() only accepts package identifiers.
End of explanation
tabledata.convert
from vistrails.core.modules.module_registry import MissingModule
try:
    tabledata['convert']  # can't get namespaces this way, use a dot
except MissingModule:
    pass
else:
    assert False
tabledata.BuildTable, tabledata['BuildTable']
tabledata.read.CSVFile, tabledata['read|CSVFile']
Explanation: You can get Modules from the package using the dot or bracket syntax. These modules are "dangling" modules, not yet instanciated in a specific pipeline/vistrail.
These will be useful once editing pipelines is added to the API.
End of explanation
outputs = vt.load_vistrail('outputs.vt')
outputs.select_version(1)
outputs
# Errors
try:
    result = outputs.execute()
except vt.ExecutionErrors:
    traceback.print_exc()
else:
    assert False
# Results
outputs.select_latest_version()
result = outputs.execute()
result
outputs
outputs.current_pipeline
Explanation: Execution
In addition to executing a Pipeline or Vistrail, you can easily pass values in on InputPort modules (to use subworkflows as Python functions) and get results out (either on OutputPort modules or any port of any module).
Execution returns a Results object from which you can get all of this. In addition, output modules (such as matplotlib's MplFigureOutput) will output to the IPython notebook if possible.
Gets output
End of explanation
result.module_output(0)
Explanation: This gets the value on any output port of any module (no need to insert OutputPort or GenericOutput modules, if you know how to find the module):
End of explanation
result.output_port('msg')
Explanation: This gets the value passed to an OutputPort module, using the OutputPort's name:
End of explanation
pipeline = vistrail.current_pipeline
pipeline
in_a = pipeline.get_input('in_a')
assert (in_a == pipeline.get_module('First input')) is True
in_a
Explanation: Sets inputs
End of explanation
result = pipeline.execute(in_a == 2, in_b=4)
result.output_port('out_times'), result.output_port('out_plus')
Explanation: We need to provide value to this workflow, for its two InputPort modules. Input can be supplied to execute() in two ways:
* either by using module_obj == value, where module_obj is a module obtained from the pipeline, using get_input() or get_module();
* or by using module_name=value, where module_name is the name set on an InputPort module
Note that, to Python, module_obj is a variable and must be bound to a value (of type Module), whereas module_name is a keyword-parameter name.
End of explanation
im = vt.load_vistrail('imagemagick.vt')
im.select_version('read')
im
Explanation: Other example
End of explanation
im.execute().output_port('result')
im.select_version('blur')
im
im.execute().output_port('result')
im.select_version('edges')
im.execute().output_port('result')
Explanation: Note that if you print a File value, IPython will try to render it.
End of explanation
mpl = vt.load_vistrail('../matplotlib/pie_ex1.vt')
mpl.select_latest_version()
Explanation: Output mode
End of explanation
mpl.execute()
richtext = vt.load_vistrail('out_html.xml')
richtext.select_latest_version()
Explanation: This workflow uses MplFigureOutput, which outputs to the IPython notebook if available (and since the spreadsheet is not running).
End of explanation
richtext.execute()
tbl = vt.load_vistrail('table.xml')
tbl.select_latest_version()
Explanation: This one uses RichTextOutput:
End of explanation
tbl.execute()
render = vt.load_vistrail('brain_output.xml')
render.select_latest_version()
Explanation: TableOutput:
End of explanation
render.execute()
Explanation: And vtkRendererOutput:
End of explanation
import urllib2
basic = vt.load_package('org.vistrails.vistrails.basic')
pythoncalc = vt.load_package('org.vistrails.vistrails.pythoncalc')
new_vistrail = vt.Vistrail()
# Simple Integer constant
data = new_vistrail.controller.add_module_from_descriptor(basic.Integer.descriptor)
new_vistrail.controller.update_function(data, 'value', ['8'])
# PythonSource module
pythonblock = new_vistrail.controller.add_module_from_descriptor(basic.PythonSource.descriptor)
new_vistrail.controller.update_ports(pythonblock.id,
                                      [],
                                      [('output', 'computed_number', 'org.vistrails.vistrails.basic:Float')])
new_vistrail.controller.update_function(pythonblock, 'source', [urllib2.quote(r\
number = 211856436.75
while number > 10.0:
    number /= 7.0
computed_number = number
)])
# Python calc that multiply inputs
multiply = new_vistrail.controller.add_module_from_descriptor(pythoncalc.PythonCalc.descriptor)
new_vistrail.controller.update_function(multiply, 'op', ['*'])
# StandardOutput, here it will print to the notebook
output = new_vistrail.controller.add_module_from_descriptor(basic.StandardOutput.descriptor)
# Add connections
new_vistrail.controller.add_connection(data.id, 'value', multiply.id, 'value1')
new_vistrail.controller.add_connection(pythonblock.id, 'computed_number', multiply.id, 'value2')
new_vistrail.controller.add_connection(multiply.id, 'value', output.id, 'value')
new_vistrail.current_version
new_vistrail.current_pipeline
new_vistrail.execute()
Explanation: Pipeline manipulation
The API wrapper doesn't currently provide easier methods to manipulate pipelines. This is mainly because these operations need all of the VisTrails concepts anyway.
You can however call the controller methods on vistrail.controller directly, using modules obtained through load_package().
End of explanation
<END_TASK>