/doc/tutorials/simple_tutorial/step1_follows.rst

https://code.google.com/p/ruffus/ · ReStructuredText · 139 lines · 84 code · 55 blank · 0 comment · 0 complexity · 61862eb0845cf7a84f86aa6647ca90c1 MD5 · raw file

  1. .. include:: ../../global.inc
  2. .. _Simple_Tutorial_1st_step:
  3. ###################################################################
  4. Step 1: An introduction to Ruffus pipelines
  5. ###################################################################
  6. * :ref:`Simple tutorial overview <Simple_Tutorial>`
  7. Computational pipelines often become quite simple
  8. if we breakdown the process into simple stages.
  9. .. note::
  10. Ruffus refers to each stage of your pipeline as a :term:`task`.
  11. | Let us start with the usual "Hello World".
  12. | We have the following two python functions which
  13. we would like to turn into an automatic pipeline:
  14. .. image:: ../../images/simple_tutorial_hello_world.png
  15. .. ::
  16. ::
  17. def first_task():
  18. print "Hello "
  19. def second_task():
  20. print "world"
  21. The simplest **Ruffus** pipeline would look like this:
  22. .. image:: ../../images/simple_tutorial_intro_follows.png
  23. .. ::
  24. ::
  25. from ruffus import *
  26. def first_task():
  27. print "Hello "
  28. @follows(first_task)
  29. def second_task():
  30. print "world"
  31. pipeline_run([second_task])
  32. The functions which do the actual work of each stage of the pipeline remain unchanged.
  33. The role of **Ruffus** is to make sure these functions are called in the right order,
  34. with the right parameters, running in parallel using multiprocessing if desired.
  35. There are three simple parts to building a **ruffus** pipeline
  36. #. importing ruffus
  37. #. "Decorating" functions which are part of the pipeline
  38. #. Running the pipeline!
  39. .. index::
  40. pair: decorators; Tutorial
  41. ****************************
  42. "Decorating" functions
  43. ****************************
  44. You need to tag or :term:`decorator` existing code to tell **Ruffus** that they are part
  45. of the pipeline.
  46. .. note::
  47. :term:`decorator`\ s are ways to tag or mark out functions.
  48. They start with a ``@`` prefix and take a number of parameters in parenthesis.
  49. .. image:: ../../images/simple_tutorial_decorator_syntax.png
  50. The **ruffus** decorator :ref:`@follows <decorators.follows>` makes sure that
  51. ``second_task`` follows ``first_task``.
  52. | Multiple :term:`decorator`\ s can be used for each :term:`task` function to add functionality
  53. to *Ruffus* pipeline functions.
  54. | However, the decorated python functions can still be
  55. called normally, outside of *Ruffus*.
  56. | *Ruffus* :term:`decorator`\ s can be added to (stacked on top of) any function in any order.
  57. * :ref:`More on @follows in the in the Ruffus `Manual <manual.follows>`
  58. * :ref:`@follows syntax in detail <decorators.follows>`
  59. .. index::
  60. pair: pipeline_run; Tutorial
  61. ****************************
  62. Running the pipeline
  63. ****************************
  64. We run the pipeline by specifying the **last** stage (:term:`task` function) of your pipeline.
  65. Ruffus will know what other functions this depends on, following the appropriate chain of
  66. dependencies automatically, making sure that the entire pipeline is up-to-date.
  67. Because ``second_task`` depends on ``first_task``, both functions are executed in order.
  68. ::
  69. >>> pipeline_run([second_task], verbose = 1)
  70. Ruffus by default prints out the ``verbose`` progress through the pipelined code,
  71. interleaved with the **Hello** printed by ``first_task`` and **World** printed
  72. by ``second_task``.
  73. .. image:: ../../images/simple_tutorial_hello_world_output.png
  74. .. ::
  75. ::
  76. >>> pipeline_run([second_task], verbose = 1)
  77. Start Task = first_task
  78. Hello
  79. Job completed
  80. Completed Task = first_task
  81. Start Task = second_task
  82. world
  83. Job completed
  84. Completed Task = second_task