{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# CAPSUL: chain algorithms in pipelines" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "
Capsul is a simple and efficient Python tool that aims to organize a set of processings.\n", "It is accessible to everybody, and is reusable in various contexts.\n", "The project is hosted on github: https://github.com/populse/capsul.
\n", "\n", "Documentation: http://populse.github.io/capsul\n", "
\n", "\n", "The following examples are using CAPSUL, and PyQt (or PySide). To get the GUI running in a non-blocking way, the IPython notebook should be started with the option --gui=qt:\n", "
ipython notebook --gui=qt\n", "Otherwise calls to the Qt loop will be blocking until windows are closed at each demo step.\n", "\n", "\n", "
\n", "Definitions\n", "\n", "
\n", "First check\n", "\n", "In order to test if capsul is installed on your machine, you can ask the the Capsul version:\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "# just to ensure compatibility of this notebook with python 2 and 3\n", "from __future__ import print_function\n", "# the following to avoid display when this notebook is converted to sphinx doc\n", "import os\n", "if os.environ.get('ALLOW_GUI', 'TRUE') in ('FALSE', '0'):\n", " use_gui = False\n", "else:\n", " %gui qt4\n", " use_gui = True\n", " from soma.qt_gui import qt_backend\n", " qt_backend.set_qt_backend()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import capsul\n", "print(capsul.__version__)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "This is a pure Python function with an XML Process description in the @xml_process decorator. Inside <process>
and </process>
elements, each input parameters are described as well as the returned value. The parameters are typed and a description is asked in order to generate proper tooltips or documentations. See XML specifications for more information.
We can now create a Process from this Python function:
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "from capsul.api import get_process_instance\n", "\n", "process = get_process_instance('__main__.add')\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "We can set some input parameters and execute the process:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "process.a = 40\n", "process.b = 2\n", "process()\n", "print(process.a, '+', process.b, '=', process.addition)\n" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "A pipeline uses processes, or sub-pipelines, in order to define a full processing chain, with links between building blocks. A pipeline may be defined either using the Python API, as a Pipeline subclass, or using a XML definition file.\n", "
\n", "\n", "StudyConfig is a placeholder for study-wide parameters, settings, paths and so on. It is a modular configuration tool, which has modules to configure some external software.\n", "
\n", "\n", "Note how the output \"image_out5\" depends on the proc_select2 switch value:
\n", "While \"image_out1\" is fixed via the FOM completion, its value \"back-propagates\" to both \"proc1.output_image\" and \"proc2.output_image\". For \"image_out5\" the FOM does not impose its value, it is deduced from either \"proc3.output_image\" (in turn set via the global \"image_out3\") or \"proc4.output_image\", depending on the proc_select2 swtch value.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "mp.proc_select2 = \"proc3\"\n", "print(\"switch proc_select2 value:\", mp.proc_select2)\n", "print(\"output image_out5:\", mp.image_out5)\n", "mp.proc_select2 = \"proc4\"\n", "print(\"switch proc_select2 value:\", mp.proc_select2)\n", "print(\"output image_out5:\", mp.image_out5)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "\n", "\n", "\n", "## Now Cleanup the files we have created..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "import shutil\n", "shutil.rmtree('/tmp/capsul_demo')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython3", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }