README.org 42 KB
Newer Older
Maude Le Jeune's avatar
Maude Le Jeune committed
1
#+TITLE: The Pipelet Readme v1.1
Maude Le Jeune's avatar
Maude Le Jeune committed
2 3
#+STYLE: <link rel="stylesheet" type="text/css" href="org.css" />

Maude Le Jeune's avatar
Maude Le Jeune committed
4 5
Pipelet is a free framework allowing for the creation, execution and
browsing of scientific data processing pipelines. It provides:
6

Marc Betoule's avatar
Marc Betoule committed
7 8 9 10 11
+ easy chaining of interdependent elementary tasks,
+ web access to data products,
+ branch handling,
+ automated distribution of computational tasks.

12 13 14
Both engine and web interface are written in Python. As Pipelet is all
about chaining processing written in Python or using Python as a glue
language, prior knowledge of this language is required.
Marc Betoule's avatar
Marc Betoule committed
15 16 17 18 19

* Tutorial
** Introduction
*** Why using pipelines

20 21
The pipeline mechanism allows to apply a sequence of processing
steps to some data, in a way that the input of each process is the
Marc Betoule's avatar
Marc Betoule committed
22 23 24 25 26 27 28
output of the previous one. Making visible these different processing
steps, in the right order, is essential in data analysis to keep track
of what you did, and make sure that the whole processing remains
consistent.

*** How it works

Maude Le Jeune's avatar
Maude Le Jeune committed
29
Pipelet is based on the possibility to save on disk every intermediate
30
input or output of a pipeline, which is usually not a strong
Maude Le Jeune's avatar
Maude Le Jeune committed
31
constraint but offers a lot of benefits. It means that you can stop
32
the processing whenever you want, and start it again without
Marc Betoule's avatar
Marc Betoule committed
33 34 35 36 37
recomputing the whole thing: you just take the last products you have
on disk, and continue the processing where it stopped. This logic is
interesting when the computation cost is higher than the cost of disk
space required by intermediate products.

Maude Le Jeune's avatar
readme  
Maude Le Jeune committed
38 39 40 41 42 43 44 45
In addition, the Pipelet engine has been designed to
process *data* *sets*. It takes advantage of the parallelisation
opportunity that comes with data which share the same structure (data
arrays), to dispatch the computational tasks on parallel architecture.
The data dependency scheme is also used to save CPU time, and allows
to handle very big data sets processing.


Marc Betoule's avatar
Marc Betoule committed
46 47
*** The Pipelet functionalities

Maude Le Jeune's avatar
Maude Le Jeune committed
48 49
Pipelet is a free framework which helps you : 
+ to write and manipulate pipelines with any dependency scheme, 
Maude Le Jeune's avatar
readme  
Maude Le Jeune committed
50 51 52
+ to keep track of what processing has been applied to your data and perform comparisons,
+ to carry pipelines source code from development to production and
  adapt to different hardware and software architectures.
Marc Betoule's avatar
Marc Betoule committed
53

Maude Le Jeune's avatar
Maude Le Jeune committed
54

55
*** What's new in v1.1
56 57 58 59 60 61 62 63 64 65 66 67

- Speed improvement during execution and navigation to handle pipeline of 100 thousand tasks.
- Task repository versionning to manage =group_by= directive which uses different parent tasks list.
- New =glob_seg= type utility to search data files from parent task
  only + improvement of I/O and parameters utilities. See [[*The%20segment%20environment][The segment
  environment]].
- Improvement of external dependencies management : the depend
  directive induces a copy of external dependencies, the version
  number (together with RCS revision if exist) of the imported modules
  are output.
- Pickle file render available from the Web interface 

68

Marc Betoule's avatar
Marc Betoule committed
69
** Getting started
Maude Le Jeune's avatar
Maude Le Jeune committed
70 71 72 73 74 75
*** Pipelet installation 
**** Dependencies 

+ Running the Pipelet engine requires Python >= 2.6.

+ The web interface of Pipelet requires the installation of the
76
  cherrypy3 Python module (on Debian: aptitude install python-cherrypy3).
Maude Le Jeune's avatar
Maude Le Jeune committed
77

78 79 80 81 82
+ Although default Python installation provides the sqlite3 module,
  you may not be able to use it. In that case, you can manually
  install the pysqlite2 module. 


83
You may find useful to install some generic scientific tools that nicely interact with Pipelet: 
Maude Le Jeune's avatar
Maude Le Jeune committed
84 85 86
+ numpy
+ matplotlib
+ latex 
Marc Betoule's avatar
Marc Betoule committed
87

88 89


Maude Le Jeune's avatar
Maude Le Jeune committed
90
**** Getting Pipelet
Marc Betoule's avatar
Marc Betoule committed
91

Maude Le Jeune's avatar
readme  
Maude Le Jeune committed
92 93 94 95 96 97 98 99 100 101 102
***** Software status

The first version of the software is currently in the process of
stabilisation.  The Pipelet engine has now reached the level of
desired sophistication.  On the other hand, the user interface has
been developped in a minimalist way. It includes the main
functionalities but with a design which could and will be more user
friendly. 


***** Getting last pipelet version
Marc Betoule's avatar
Marc Betoule committed
103

Maude Le Jeune's avatar
Maude Le Jeune committed
104
=git clone git://gitorious.org/pipelet/pipelet.git=
Marc Betoule's avatar
Marc Betoule committed
105

Maude Le Jeune's avatar
Maude Le Jeune committed
106
**** Installing Pipelet
Marc Betoule's avatar
Marc Betoule committed
107

Marc Betoule's avatar
Marc Betoule committed
108
=sudo python setup.py install=
Marc Betoule's avatar
Marc Betoule committed
109

110
*** Running a simple test pipeline
Marc Betoule's avatar
Marc Betoule committed
111 112 113

1. Run the test pipeline

Maude Le Jeune's avatar
Maude Le Jeune committed
114
   =cd test/first_test=
Marc Betoule's avatar
Marc Betoule committed
115

Maude Le Jeune's avatar
Maude Le Jeune committed
116
   =python main.py=
Marc Betoule's avatar
Marc Betoule committed
117 118 119

2. Add this pipeline to the web interface

Maude Le Jeune's avatar
Maude Le Jeune committed
120
   =pipeweb track test ./.sqlstatus=
Marc Betoule's avatar
Marc Betoule committed
121

122
3. Set up an account in the access control list and launch the web server
Marc Betoule's avatar
Marc Betoule committed
123

Maude Le Jeune's avatar
Maude Le Jeune committed
124
   =pipeutils -a username -l 2 .sqlstatus=
Marc Betoule's avatar
Marc Betoule committed
125

Maude Le Jeune's avatar
Maude Le Jeune committed
126
   =pipeweb start=
Marc Betoule's avatar
Marc Betoule committed
127 128 129 130

4. You should be able to browse the result on the web page
   http://localhost:8080

131 132
*** Getting a new pipe framework

Maude Le Jeune's avatar
Maude Le Jeune committed
133
To get a new pipeline framework, with example main and segment scripts : 
134

Marc Betoule's avatar
Marc Betoule committed
135
=pipeutils -c pipename=
136

Maude Le Jeune's avatar
Maude Le Jeune committed
137 138 139
This command ends up with the creation of directory named pipename wich contains: 
+ a main script (named main.py) providing functionnalities to execute
  your pipeline in various modes (debug, parallel, batch mode, ...)
140
+ an example of segment script (=default.py=) which illustrates
Maude Le Jeune's avatar
Maude Le Jeune committed
141
  the pipelet utilities with comments. 
142

Maude Le Jeune's avatar
Maude Le Jeune committed
143
The next section describes those two files in more details. 
144

Maude Le Jeune's avatar
Maude Le Jeune committed
145
** Writing Pipes
Marc Betoule's avatar
Marc Betoule committed
146 147 148 149 150 151 152 153 154 155
*** Pipeline architecture

The definition of a data processing pipeline consists in :
+ a succession of python scripts, called segments, coding each step
  of the actual processing.
+ a main script that defines the dependency scheme between segments,
  and launch the processing.

The dependencies between segments must form a directed acyclic
graph. This graph is described by a char string using a subset of the
156
graphviz dot language (http://www.graphviz.org). For example the string:
Marc Betoule's avatar
Marc Betoule committed
157

Marc Betoule's avatar
Marc Betoule committed
158
#+begin_src python
Marc Betoule's avatar
Marc Betoule committed
159
"""
Marc Betoule's avatar
Marc Betoule committed
160 161 162
a -> b -> d;
c -> d;
c -> e;
Marc Betoule's avatar
Marc Betoule committed
163
"""
Marc Betoule's avatar
Marc Betoule committed
164 165 166 167
#+end_src

defines a pipeline with 5 segments ={"a", "b", "c", "d", "e"}=. The
relation =a->b= ensures that the processing of the segment "a" will be
168
done before the processing of its 'child' segment =b=. Also the output
Marc Betoule's avatar
Marc Betoule committed
169 170 171
of =a= will be fed as input for =b=. In the given example, the node
=d= has two parents =b= and =c=. Both will be executed before =d=. As
their is no relation between =b= and =c= which of the two will be
Marc Betoule's avatar
Marc Betoule committed
172 173
executed first is not defined.

Marc Betoule's avatar
Marc Betoule committed
174 175 176
When executing the segment =seg=, the engine looks for a python script
named =seg.py=. If not found, it looks iteratively for script files
named =se.py= and =s.py=. This way, different segments of the pipeline
Maude Le Jeune's avatar
Maude Le Jeune committed
177
can share the same code, if they are given a name with a common root
178
(this mechanism is useful to write generic segment and is completed by
Maude Le Jeune's avatar
Maude Le Jeune committed
179
the hooking system, described in the advanced usage section). The code
Marc Betoule's avatar
Marc Betoule committed
180 181
is then executed in a specific namespace (see below [[*The%20segment%20environment][The execution
environment]]).
Maude Le Jeune's avatar
Maude Le Jeune committed
182

Marc Betoule's avatar
Marc Betoule committed
183 184
*** The Pipeline object

185
Practically, the creation of a Pipeline object requires 3 arguments:
Marc Betoule's avatar
Marc Betoule committed
186

Marc Betoule's avatar
Marc Betoule committed
187
#+begin_src python
188
from pipelet.pipeline import Pipeline
189
P = Pipeline(pipedot, code_dir="./", prefix="./")
Marc Betoule's avatar
Marc Betoule committed
190
#+end_src
Marc Betoule's avatar
Marc Betoule committed
191

Marc Betoule's avatar
Marc Betoule committed
192 193

- =pipedot= is the string description of the pipeline
194
- =code_dir= is the path where the segment scripts can be found
Marc Betoule's avatar
Marc Betoule committed
195
- =prefix=  is the path to the data repository (see below [[*Hierarchical%20data%20storage][Hierarchical data storage]])
Marc Betoule's avatar
Marc Betoule committed
196

197
It is possible to output the graphviz representation of the pipeline
198 199
(requires the installation of graphviz). First, save the graph string
into a .dot file with the pipelet function:
Marc Betoule's avatar
Marc Betoule committed
200 201

#+begin_src python
Maude Le Jeune's avatar
Maude Le Jeune committed
202
P.to_dot('pipeline.dot')
Marc Betoule's avatar
Marc Betoule committed
203
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
204 205 206

Then, convert it to an image file with the dot command: 

Marc Betoule's avatar
Marc Betoule committed
207
=dot -Tpng -o pipeline.png pipeline.dot=
Maude Le Jeune's avatar
Maude Le Jeune committed
208

209
*** Dependencies between segments and data parallelism
Marc Betoule's avatar
Marc Betoule committed
210

Marc Betoule's avatar
Marc Betoule committed
211 212 213 214 215
The modification of the code of one segment will trigger its
recalculation and the recalculation of all the segments which
depend on it.

The output of a segment is a list of python objects. If a segment as
Maude Le Jeune's avatar
Maude Le Jeune committed
216 217 218 219 220 221 222 223 224 225
no particular output this list can be empty and do not need to be
specified. Elements of the list are allowed to be any kind of
pickleable python objects. However, a good practice is to fill the
list with the minimal set of characteristics relevant to describe the
output of the segment and to defer the storage of the data to
appropriate structures and file formats. For example, a segment which
performs computation on large images could virtually pass the results
of its computation to the following segment using the output list. It
is a better practice to store the resulting image in a dedicated file
and to pass in the list only the information allowing a non ambiguous
226
identification of this file (like its name or part of it).
Maude Le Jeune's avatar
Maude Le Jeune committed
227 228 229 230

The input of a child segment is taken in a set build from the output
lists of its parents. The content of the input set is actually tunable
using the multiplex directive (see below). However the simplest and
231
default behavior of the engine is to form the Cartesian product of
Maude Le Jeune's avatar
Maude Le Jeune committed
232 233
the output list of its parent.

234
To illustrate this behavior let us consider the following pipeline,
Maude Le Jeune's avatar
Maude Le Jeune committed
235 236
build from three segments:

Marc Betoule's avatar
Marc Betoule committed
237 238 239 240 241 242
#+begin_src python
"""
knights -> melt;
quality -> melt;
"""
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
243 244 245 246

and assume that the respective output lists of segments knights and
quality are:

Marc Betoule's avatar
Marc Betoule committed
247 248 249
#+begin_src python
["Lancelot", "Galahad"]
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
250
and:
Marc Betoule's avatar
Marc Betoule committed
251 252 253
#+begin_src python
['the Brave', 'the Pure']
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
254

255
The Cartesian product of the previous set is:
Marc Betoule's avatar
Marc Betoule committed
256
#+begin_src python
257
 [('Lancelot','the Brave'), ('Lancelot','the Pure'), ('Galahad','the Brave'), ('Galahad','the
Maude Le Jeune's avatar
Maude Le Jeune committed
258
Pure')]
Marc Betoule's avatar
Marc Betoule committed
259
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
260

Marc Betoule's avatar
Marc Betoule committed
261
Four instances of segment =melt= will thus be run, each one receiving
Maude Le Jeune's avatar
Maude Le Jeune committed
262 263 264
as input one of the four 2-tuples.

At the end of the execution of all the instances of a segment, their
Marc Betoule's avatar
Marc Betoule committed
265
output lists are concatenated. If the action of segment =melt= is to
Maude Le Jeune's avatar
Maude Le Jeune committed
266
concatenate the two strings he receives separated by a space, the
Marc Betoule's avatar
Marc Betoule committed
267
final output set of segment =melt= will be: 
Maude Le Jeune's avatar
Maude Le Jeune committed
268

Marc Betoule's avatar
Marc Betoule committed
269 270 271
#+begin_src python
[('Lancelot the Brave'), ('Lancelot the Pure'), ('Galahad the Brave'), ('Galahad the Pure')].
#+end_src
Marc Betoule's avatar
Marc Betoule committed
272

Marc Betoule's avatar
Marc Betoule committed
273
This default behavior can be altered by specifying a =#multiplex=
Marc Betoule's avatar
Marc Betoule committed
274 275
directive in the commentary of the segment code. See section [[*Multiplex%20directive][Multiplex
directive]] for more details.
276

277 278 279 280
As the segment execution order is not uniquely determined by the pipe
scheme (several path may exists), it is not possible to retrieve an
ordered input tuple. To overcome this issue, segment inputs are
dictionaries, with keywords matching parent segment names.  In the
Marc Betoule's avatar
Marc Betoule committed
281
above example, one can read =melt= inputs using:
282

Marc Betoule's avatar
Marc Betoule committed
283
#+begin_src python
284 285
k = seg_input["knights"]
q = seg_input["quality"]
Marc Betoule's avatar
Marc Betoule committed
286
#+end_src
287

288 289 290 291 292 293
One can also use dedicated segment routines: 
#+begin_src python
k = get_input("knights")
q = get_input("quality")
#+end_src

Marc Betoule's avatar
Marc Betoule committed
294
See section [[*The%20segment%20environment]['The segment environment']] for more details.
295

Marc Betoule's avatar
Marc Betoule committed
296
*** Orphan segments
Marc Betoule's avatar
Marc Betoule committed
297

298 299 300 301 302
By default, orphan segments (segments without parents) have no input
argument (an empty list), and therefore are executed once without
input. The possibility is offered to feed input to an orphan segment
by pushing a list into the output set of an hypothetic ('phantom')
parent. If P is an instance of the pipeline object, this is done by:
303

Marc Betoule's avatar
Marc Betoule committed
304
#+begin_src python
305
P.push (segname=[1,2,3])
Marc Betoule's avatar
Marc Betoule committed
306
#+end_src
307 308

From the segment environment, inputs can be retrieve from the
309
dedicated routine: 
310

Marc Betoule's avatar
Marc Betoule committed
311
#+begin_src python
312
id = get_input()
Marc Betoule's avatar
Marc Betoule committed
313
#+end_src
314

315 316 317 318 319 320 321
In this scheme, it is important to uniquely identify the child tasks
of the orphan segment by setting a dedicated output.

#+begin_src python
seg_output = id
#+end_src

Marc Betoule's avatar
Marc Betoule committed
322
See section [[*The%20segment%20environment][The segment environment]] for more details.
323

Marc Betoule's avatar
Marc Betoule committed
324 325
*** Hierarchical data storage

326 327 328 329 330 331
The framework provides versioning of your data and easy access through
the web interface. It also keep track of the code, of the execution
logs, and various meta-data of the processing. Of course, you remain
able to bypass the hierarchical storage and store your actual data
elsewhere, but you will loose the benefit of automated versioning
which proves to be quite convenient.
Marc Betoule's avatar
Marc Betoule committed
332 333

The storage is organized as follows:
334 335 336

- all pipeline instances are stored below a root which corresponds to
  the prefix parameter of the Pipeline object. 
Marc Betoule's avatar
Marc Betoule committed
337
      =/prefix/=
338
- all segment meta data are stored below a root which name corresponds
339
  to a unique hash computed on the segment code and its dependencies.
Marc Betoule's avatar
Marc Betoule committed
340
      =/prefix/segname_YFLJ65/=
341 342 343 344 345 346 347
- Segment's meta data are: 
  - a copy of the segment python script
  - a copy of all segment hook scripts
  - a parameter file (.args) which contains segment parameters value
  - a meta data file (.meta) which contains some extra meta data
- all segment instances data and meta data are stored in a specific subdirectory
  which name corresponds to a string representation of its input
348 349
  prefix by its identifier number
  	=/prefix/segname_YFLJ65/data/1_a/=
350
- if there is a single segment instance, then data are stored in
Marc Betoule's avatar
Marc Betoule committed
351
       =/prefix/segname_YFLJ65/data/=
352 353
- If a segment has at least one parent, its root will be located below
  one of its parent's one : 
Marc Betoule's avatar
Marc Betoule committed
354
       =/prefix/segname_YFLJ65/segname2_PLMBH9/=
355
- etc...
Maude Le Jeune's avatar
Maude Le Jeune committed
356
  
357 358 359 360 361 362
While the hierarchical storage makes easy the storing and handling of
many different data with different versions, it can make the manual
navigation in the data less convenient. Here comes the role of the [[*Browsing%20Pipes][web
interface]] (among other advantages, like distant access to the data,
tagging...).

Marc Betoule's avatar
Marc Betoule committed
363 364 365 366 367
*** The segment environment

The segment code is executed in a specific environment that provides:

1. access to the segment input and output
368 369
   - =get_input(seg)=: return the input coming from segment seg. If no
     segment specified, take the first. This utility replaces the
370
     =seg_input= variable which type could vary as described below.
371

372
   - =seg_input=:  this variable is a dictionary containing the input of the segment.
Marc Betoule's avatar
Marc Betoule committed
373
     In the general case, =seg_input= is a python dictionary which
374
     contains as many keywords as parent segments. In the case of orphan
Marc Betoule's avatar
Marc Betoule committed
375 376
     segment, the keyword used is suffixed by the =phantom= word. 
     One exception to this is coming from the use of the =group_by=
377
     directive, which alters the origin of the inputs. In this case,
378
     =seg_input= contains the resulting class elements. 
379
     
380 381
   - =set_output(o)=: set the segment output as a list. If =o= is not a
     list, set a list of one element =[o]=. 
382

Marc Betoule's avatar
Marc Betoule committed
383
   - =seg_output=: this variable has to be a list. 
Marc Betoule's avatar
Marc Betoule committed
384

385
2. Functionalities to use the automated hierarchical data storage system.
386 387
   - =get_data_fn(basename, seg)=: complete the filename with the path to
     the working directory of the segment (default is the current segment). 
388 389 390 391 392 393 394 395
   - =glob_parent(regexp, segs)=: Return the list of filename matching
        the pattern y in the data directory of direct parent tasks. It
        is possible to search only in a specific segment list segs.
   - =glob_seg(seg, regexp)=: Return the list of filename matching the
        pattern y in the data directory of parent segment x (all task
        directories are searched, independantly of whether the file
        comes from a task related to the current task). Its usage
        should be limited as it:
396 397 398
        - potentially breaks the dependancy scheme.
        - may hurt performances as all task directories of the segment
          x will be searched.
Marc Betoule's avatar
Marc Betoule committed
399
   - =get_tmp_fn()=: return a temporary filename.
Marc Betoule's avatar
Marc Betoule committed
400

401
3. Functionalities to use the automated parameters handling
402 403
   - =save_param(lst)=: the listed parameters will be saved in a dedicated file. 
   - =expose(lst)=: the listed parameters will be exposed from the web interface
Maude Le Jeune's avatar
Maude Le Jeune committed
404
   - =load_param(seg, globals(), lst)=: retrieve parameters from the meta data.
405 406

4. Various convenient functionalities
407
   - =save_products(filename, globals(), lst_par)=: use pickle to save a
408
     part of the current namespace.
409
   - =load_products(filename, globals(), lst_par)=: update the namespace by
Marc Betoule's avatar
Marc Betoule committed
410
     unpickling requested object from the file.
Marc Betoule's avatar
Marc Betoule committed
411 412 413
   - =logged_subprocess(lst_args)=: execute a subprocess and log its
     output in =processname.log= and =processname.err=.
   - =logger= is a standard =logging.Logger= object that can be used to
Betoule Marc's avatar
Betoule Marc committed
414
     log the processing
Marc Betoule's avatar
Marc Betoule committed
415

416
5. Hooking support 
Marc Betoule's avatar
Marc Betoule committed
417 418
   Pipelet enables you to write reusable generic
   segments by providing a hooking system via the hook function.
Marc Betoule's avatar
Marc Betoule committed
419 420 421
   =hook(hookname, globals())=: execute Python script =segname_hookname.py= and update the namespace.
   See the section [[*The%20hooking%20system][Hooking system]] for more details.

Marc Betoule's avatar
Marc Betoule committed
422

423
*** Writing a first pipeline
Marc Betoule's avatar
Marc Betoule committed
424

425 426 427
We are now in the position to write a complete simple pipeline. Let us
consider the knights example and write the beginning of the main file
=main.py= describing the pipeline:
Maude Le Jeune's avatar
Maude Le Jeune committed
428

Marc Betoule's avatar
Marc Betoule committed
429
#+begin_src python
430 431 432 433 434 435 436 437
  from pipelet.pipeline import Pipeline
  
  pipedot = """
  knights -> melt;
  quality -> melt;
  """
  
  P = Pipeline(pipedot, code_dir='./',prefix='./')  
Marc Betoule's avatar
Marc Betoule committed
438
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
439

440 441 442
Now, we create the 3 segment files =knights.py=, =quality.py= and
=melt.py=. The only action we expect from segment knights is simply to
provide a list of knights. Its code is very simple:
Marc Betoule's avatar
Marc Betoule committed
443
#+begin_src python
444
  set_output(["Lancelot", "Galahad")
Marc Betoule's avatar
Marc Betoule committed
445
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
446

447
Same thing for the segment quality:
Marc Betoule's avatar
Marc Betoule committed
448
#+begin_src python
449
  set_output (['the Brave', 'the Pure'] )
Marc Betoule's avatar
Marc Betoule committed
450
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
451

452 453
As explained, the segment melt will be executed four times. We expect
from it to concatenate its input and write the result into a file, so the code is:
Marc Betoule's avatar
Marc Betoule committed
454
#+begin_src python
455
  knight, quality = get_input('knights'), get_input('quality')
456 457 458
  f = open(get_data_fn('result.txt'), 'w')
  f.write(knight + ' ' + quality+'\n')
  f.close()  
Marc Betoule's avatar
Marc Betoule committed
459
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
460

461 462
We need to complete the main file so that it takes care of the
execution ([[*Running%20Pipes][see Running Pipes for more explainations]]):
Marc Betoule's avatar
Marc Betoule committed
463
#+begin_src python
464 465 466 467 468 469 470 471 472 473
  from pipelet.pipeline import Pipeline
  from pipelet.launchers import launch_interactive
  pipedot = """
  knights -> melt;
  quality -> melt;
  """
  
  P = Pipeline(pipedot, code_dir='./',prefix='./')
  w,t = launch_interactive(P)
  w.run()
Marc Betoule's avatar
Marc Betoule committed
474
#+end_src
Maude Le Jeune's avatar
Maude Le Jeune committed
475

476 477 478 479 480 481 482 483
The execution of the main file will run this simple example in the
'interactive' mode provided for debugging purposes. You may add a
knight in the list to see only the required part recomputed. More
complete examples are described in the [[*The%20example%20pipelines][example pipelines]] section. The
two remaining sections of the tutorial explain how to use execution
mode that enable to exploitation of data parallelism (in this case
running the four independent instances of the melt segment in
parallel), and how to provide web access to the results.
Maude Le Jeune's avatar
Maude Le Jeune committed
484

485

Marc Betoule's avatar
Marc Betoule committed
486
** Running Pipes
487
   
488 489
*** The sample main file

490
A sample main file is made available when creating a new Pipelet
491 492
framework. It is copied from the reference file: 

Marc Betoule's avatar
Marc Betoule committed
493
=pipelet/pipelet/static/main.py=
494 495 496 497 498 499 500

This script illustrates various ways of running pipes. It describes
the different parameters, and also, how to write a
main python script can be used as any binary from the command line
(including options parsing). 

*** Common options
501
    Some options are common to each running modes.
502 503 504 505
**** log level

The logging system is handle by the python logging facility module. 
This module defines the following log levels : 
Marc Betoule's avatar
Marc Betoule committed
506 507 508 509 510
+ =DEBUG=
+ =INFO=
+ =WARNING=
+ =ERROR=
+ =CRITICAL=
511

512
All logging messages are saved in the different Pipelet log files,
513 514 515 516 517
available from the web interface (rotating file logging).  It is also
possible to print those messages on the standard output (stream
logging), by setting the desired log level in the launchers options:
For example: 

Marc Betoule's avatar
Marc Betoule committed
518
#+begin_src python
519 520
import logging
launch_process(P, N,log_level=logging.DEBUG)
Marc Betoule's avatar
Marc Betoule committed
521
#+end_src
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

If set to 0, stream logging will be disable. 

**** matplotlib

The matplotlib documentation says: 

"Many users report initial problems trying to use maptlotlib in web
application servers, because by default matplotlib ships configured to
work with a graphical user interface which may require an X11
connection. Since many barebones application servers do not have X11
enabled, you may get errors if you don’t configure matplotlib for use
in these environments. Most importantly, you need to decide what kinds
of images you want to generate (PNG, PDF, SVG) and configure the
appropriate default backend. For 99% of users, this will be the Agg
backend, which uses the C++ antigrain rendering engine to make nice
PNGs. The Agg backend is also configured to recognize requests to
generate other output formats (PDF, PS, EPS, SVG). The easiest way to
configure matplotlib to use Agg is to call:

Marc Betoule's avatar
Marc Betoule committed
542
=matplotlib.use('Agg')=
543

544 545 546 547
The =matplotlib= and =matplotlib_interactive= options turn the
matplotlib backend to Agg in order to allow the execution in
non-interactive environment. The two options affects independently the
non interactive execution mode and the interactive mode.
548

Marc Betoule's avatar
Marc Betoule committed
549
Those two options are set to =True= by default in the sample main
550 551 552
script. Setting them to False deactivate this behavior for pipelines
that make no use of matplotlib (and prevents the raise of an exception
if matplotlib is not even available).
553

Marc Betoule's avatar
Marc Betoule committed
554
*** The interactive mode
Marc Betoule's avatar
Marc Betoule committed
555
This mode has been designed to ease debugging. If =P= is an instance of
556
the pipeline object, the syntax reads :
Marc Betoule's avatar
Marc Betoule committed
557

Marc Betoule's avatar
Marc Betoule committed
558
#+begin_src python
Marc Betoule's avatar
Marc Betoule committed
559 560 561
from pipelet.launchers import launch_interactive
w, t = launch_interactive(P)
w.run()
Marc Betoule's avatar
Marc Betoule committed
562
#+end_src
Marc Betoule's avatar
Marc Betoule committed
563 564 565 566

In this mode, each tasks will be computed in a sequential way. 
Do not hesitate to invoque the Python debugger from IPython : %pdb

Maude Le Jeune's avatar
Maude Le Jeune committed
567
To use the interactive mode, run: 
Marc Betoule's avatar
Marc Betoule committed
568
=main.py -d=
569

Marc Betoule's avatar
Marc Betoule committed
570
*** The process mode
571 572 573 574
In this mode, one can run simultaneous tasks (if the pipe scheme
allows to do so). 
The number of subprocess is set by the N parameter : 

Marc Betoule's avatar
Marc Betoule committed
575
#+begin_src python
576 577
from pipelet.launchers import launch_process
launch_process(P, N)
Marc Betoule's avatar
Marc Betoule committed
578
#+end_src
579

Maude Le Jeune's avatar
Maude Le Jeune committed
580
To use the process mode, run: 
Marc Betoule's avatar
Marc Betoule committed
581
=main.py=
Maude Le Jeune's avatar
Maude Le Jeune committed
582
or
Marc Betoule's avatar
Marc Betoule committed
583
=main.py -p 4=
Maude Le Jeune's avatar
Maude Le Jeune committed
584

Marc Betoule's avatar
Marc Betoule committed
585
*** The batch mode
586 587 588
In this mode, one can submit some batch jobs to execute the tasks. 
The number of job is set by the N parameter : 

Marc Betoule's avatar
Marc Betoule committed
589
#+begin_src python
590 591
from pipelet.launchers import launch_pbs
launch_pbs(P, N , address=(os.environ['HOST'],50000))
Marc Betoule's avatar
Marc Betoule committed
592
#+end_src
Marc Betoule's avatar
Marc Betoule committed
593

594 595 596 597 598 599 600 601 602 603 604
It is possible to specify some job submission options like: 
+ job name 
+ job header: this string is prepend to the PBS job scripts. You may
  want to add some environment specific paths. Log and error files are
  automatically handled by the pipelet engine, and made available from
  the web interface. 
+ cpu time: syntax is: "hh:mm:ss"

The 'server' option can be disable to add some workers to an existing
scheduler.

Maude Le Jeune's avatar
Maude Le Jeune committed
605
To use the batch mode, run: 
Marc Betoule's avatar
Marc Betoule committed
606
=main.py -b=
Maude Le Jeune's avatar
Maude Le Jeune committed
607 608 609

to start the server, and: 

Marc Betoule's avatar
Marc Betoule committed
610
=main.py -a 4=
611

Maude Le Jeune's avatar
Maude Le Jeune committed
612
to add 4 workers. 
Maude Le Jeune's avatar
Maude Le Jeune committed
613

614

Marc Betoule's avatar
Marc Betoule committed
615
** Browsing Pipes
616 617 618 619 620
*** The pipelet webserver and ACL

The pipelet webserver allows the browsing of multiple pipelines. 
Each pipeline has to be register using : 

Marc Betoule's avatar
Marc Betoule committed
621
=pipeweb track <shortname> sqlfile=
622

623 624
To remove a pipeline from the tracked list, use : 

Marc Betoule's avatar
Marc Betoule committed
625
=pipeweb untrack <shortname>=
626

627 628 629 630
As the pipeline browsing implies a disk parsing, some basic security
has to be set also. All users have to be register with a specific access
level (1 for read-only access, and 2 for write access).  

Marc Betoule's avatar
Marc Betoule committed
631
=pipeutils -a <username> -l 2 sqlfile=
Marc Betoule's avatar
Marc Betoule committed
632

633 634
To remove a user from the user list: 

Marc Betoule's avatar
Marc Betoule committed
635
=pipeutils -d <username> sqlfile=
636

637 638
Start the web server using : 

Marc Betoule's avatar
Marc Betoule committed
639
=pipeweb start=
640 641

Then the web application will be available on the web page http://localhost:8080
Marc Betoule's avatar
Marc Betoule committed
642

643 644
To stop the web server : 

Marc Betoule's avatar
Marc Betoule committed
645
=pipeweb stop=
646

Marc Betoule's avatar
Marc Betoule committed
647
*** The web application
648

649
In order to ease the comparison of different processing, the web
650 651 652 653
interface displays various views of the pipeline data : 

**** The index page 

654
The index page displays a tree view of all pipeline instances. Each
655 656 657 658 659 660
segment may be expand or reduce via the +/- buttons.  

The parameters used in each segments are resumed and displayed with
the date of execution and the number of related tasks order by
status. 

661
A check-box allows to performed operation on multiple segments :
662 663 664
  - deletion : to clean unwanted data
  - tag : to tag remarkable data

665
The filter panel allows to display the segment instances with respect to 2
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
criterions :
  - tag
  - date of execution

**** The code page

Each segment names is a link to its code page. From this page the user
can view all python scripts code which have been applied to the data.

The tree view is reduced to the current segment and its related
parents.

The root path corresponding to the data storage is also displayed.


**** The product page

The number of related tasks, order by status, is a link to the product
pages, where the data can be directly displayed (if images, or text
files) or downloaded. 
From this page it is also possible to delete a specific product and
its dependencies. 


**** The log page

692
The log page can be acceded via the log button of the filter panel.
693 694 695 696
Logs are ordered by date. 



697 698
** The example pipelines
*** fft
Marc Betoule's avatar
Marc Betoule committed
699

700 701 702 703 704
**** Highlights

This example illustrates a very simple image processing use.
The problematic is the following : one wants to apply a Gaussian
filter in Fourier domain on several 2D images. 
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
The pipe scheme is: 

#+begin_src python
pipedot = """
mkgauss->convol;
fftimg->convol;
"""
#+end_src

where segment 'mkgauss' computes the Gaussian filter, 'fftimg' computes the
Fourier transforms of the input images, and 'convol' performs the
filtering in Fourier domain, and the inverse transform of the filtered
images. 

#+begin_src python
P = pipeline.Pipeline(pipedot, code_dir=op.abspath('./'), prefix=op.abspath('./'))
P.to_dot('pipeline.dot')
#+end_src

The pipe scheme is output as a .dot file, that can be converted to an
image file with the command line: 

=dot -Tpng -o pipeline.png pipeline.dot=

To apply this filter to several images (in our case 4 input images),
the pipe data parallelism is used. 
From the main script, a 4-element list is pushed to the =fftimg=
segment. 

#+begin_src python
P.push(fftimg=[1,2,3,4]) 
#+end_src

At execution, 4 instances of the =fftimg= segment will be
created, and each of them outputs one element of this list : 

#+begin_src python
743 744
img = get_input() #(fftimg.py - line 15)
set_output (img)  #(fftimg.py - line 38)
745 746 747 748 749 750 751 752 753 754 755 756 757
#+end_src

On the other side, a single instance of the =mkgauss= segment will be
executed, as there is one filter to apply. 

The last segment =convol=, which depends on the two others, will be
executed with a number of instances that is the Cartesian product of
its 4+1 inputs (ie 4 instances)

The instance identifier which is set by the =fftimg= output, can be
retrieve with the following instruction: 

#+begin_src python
758
img = get_input('fftimg')   #(convol.py - line 12)
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
#+end_src

**** Running the pipe

Follow the same procedure than for the first example pipeline, to run
this pipe and browse the result. 


*** cmb
**** Running the pipe

This CMB pipeline depends on two external python modules: 
+ healpy   :  http://code.google.com/p/healpy/
+ spherelib:  http://gitorious.org/spherelib


**** Problematic

This example illustrates a very simple CMB data processing use.  

The problematic is the following : one wants to characterize the
inverse noise weighting spectral estimator (as applied to the WMAP 1yr
data). A first demo pipeline is built to check that the algorithm
has correctly been implemented. Then, Monte Carlo simulations are used
to compute error bars estimates. 

**** A design pipeline

The design pipe scheme is: 

#+begin_src python
pipe_dot = """ 
cmb->clcmb->clplot;
noise->clcmb;
noise->clnoise->clplot;
"""
#+end_src

where: 
+ =cmb=: generate a CMB map from LCDM power spectrum. 
+ =noise=: compute the mode coupling matrix from the input hit-count map
+ =clnoise=: compute the empirical noise power spectrum from a noise
  realization. 
+ =clcmb=: generate two noise realizations, add them to the CMB map, to compute a
  first cross spectrum estimator. Then weighting mask and mode
  coupling matrix are applied to get the inverse noise weighting
  estimator
+ =clplot=: make a plot to compare pure cross spectrum vs inverse noise
  weighting estimators. 

As the two first orphan segments depends on a single shared parameter
which is the map resolution nside, this argument is pushed from the
main script. 

Another input argument of the cmb segment, is its simulation identifier,
which will be used for latter Monte Carlo. In order to push two
inputs to a single segment instance, we use python tuple data type.

#+begin_src python
P.push(cmb=[(nside, 1)])
P.push(noise=[nside])
#+end_src

From the segment, those inputs are retrieved with : 

#+begin_src python
825 826
(nside,sim_id) = get_input() ##(cmb.py line 14)
nside  = seg_input()         ##(noise.py line 15)
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
#+end_src

The last segment produces a plot in which we compare: 
+ the input LCDM power spectrum
+ the binned cross spectrum of the noisy CMB maps
+ the binned cross spectrum of which we applied hitcount weight and
  mode coupling matrix. 
+ the noise power spectrum computed by clnoise segment. 

In this plot we check that both estimators are corrects, and that the
noise level is the expected one.

**** From the design pipeline to Monte Carlo

As a second step, Monte Carlo simulations are used to compute error
bars. 

The =clnoise= segment is no longer needed, so that the new pipe scheme
reads : 

#+begin_src python
pipe_dot = """ 
cmb->clcmb->clplot;
noise->clcmb;
"""
#+end_src

We now use the native data parallelization scheme of the pipe to build
many instances of the =cmb= and =clcmb= segments. 

#+begin_src python
cmbin = []
for sim_id in [1,2,3,4,5,6]:
    cmbin.append((nside, sim_id))
P.push(cmb=cmbin)
#+end_src


* Advanced usage
This section describe more complicated (and useful) features and
requires good familiarity with the concept introduced in the previous section.
868
** Multiplex directive
Marc Betoule's avatar
Marc Betoule committed
869
   
870

Marc Betoule's avatar
Marc Betoule committed
871
The default behavior can be altered by specifying a =#multiplex=
872 873 874 875 876
directive in the commentary of the segment code. If several multiplex
directives are present in the segment code the last one is retained.

The multiplex directive can be one of: 

Marc Betoule's avatar
Marc Betoule committed
877
+ =#multiplex cross_prod= : default behavior, return the Cartesian
878
  product. 
Marc Betoule's avatar
Marc Betoule committed
879
+ =#multiplex union= : make the union of the inputs
880

Marc Betoule's avatar
Marc Betoule committed
881
Moreover the =#multiplex cross_prod= directive admits filtering and
882 883
grouping by class similarly to SQL requests:

Marc Betoule's avatar
Marc Betoule committed
884
#+begin_src python
885
#multiplex cross_prod where "condition" group_by "class_function"
Marc Betoule's avatar
Marc Betoule committed
886
#+end_src
887

Marc Betoule's avatar
Marc Betoule committed
888
=condition= and =class_function= are python code evaluated for each element
889 890
of the product set. 

Marc Betoule's avatar
Marc Betoule committed
891 892
The argument of =where= is a condition. The element will be part of the
input set only if it evaluates to =True=.
893

Marc Betoule's avatar
Marc Betoule committed
894
The =group_by= directive groups elements into class according to the
895 896 897 898 899 900 901 902 903
result of the evaluation of the given class function. The input set
contains all the resulting class. For example, if the function is a
constant, the input set will contain only one element: the class
containing all elements.

During the evaluation, the values of the tuple elements are accessible
as variable wearing the name of the corresponding parents.


Marc Betoule's avatar
Marc Betoule committed
904 905
Given the Cartesian product set:
#+begin_src python
906
 [('Lancelot','the Brave'), ('Lancelot','the Pure'), ('Galahad','the Brave'), ('Galahad','the
Marc Betoule's avatar
Marc Betoule committed
907 908 909 910 911
Pure')]
#+end_src

one can use :
#+begin_src python
912
#multiplex cross_prod where "quality=='the Brave'" 
Marc Betoule's avatar
Marc Betoule committed
913 914 915 916 917
#+end_src
to get 2 instances of the following segment (=melt=) running on: 
#+begin_src python
('Lancelot','the Brave'), ('Galahad','the Brave')
#+end_src
918

Marc Betoule's avatar
Marc Betoule committed
919
#+begin_src python
920
#multiplex cross_prod group_by "knights"
Marc Betoule's avatar
Marc Betoule committed
921 922 923 924 925
#+end_src
to get 2 instances of the =melt= segment running on:
#+begin_src python
('Lancelot'), ('Galahad')
#+end_src
926

Marc Betoule's avatar
Marc Betoule committed
927
#+begin_src python
928
#multiplex cross_prod group_by "0"
Marc Betoule's avatar
Marc Betoule committed
929 930
#+end_src
to get 1 instance of the =melt= segment running on: (0)
931

Marc Betoule's avatar
Marc Betoule committed
932
Note that to make use of =group_by=, elements of the output set have to be
933 934 935 936
hashable.

Another caution on the use of group: segment input data type is no
longer a dictionary in those cases as the original tuple is
Marc Betoule's avatar
Marc Betoule committed
937
lost and replaced by the result of the class function.
938

Marc Betoule's avatar
Marc Betoule committed
939
See section [[*The%20segment%20environment][The segment environment]] for more details.
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

** Depend directive

As explained in the introduction section, Pipelet offers the
possibility to spare CPU time by saving intermediate products on disk.
We call intermediate products the input/output data files of the
different segments.  

Each segment repository is identified by a unique key which depends
on: 
- the segment processing code and parameters (segment and hook
  scripts)
- the input data (identified from the key of the parent segments)

Every change made on a segment (new parameter or new parent) will then
give a different key, and tell the Pipelet engine to compute a new
segment instance.

It is possible to add some external dependencies to the key
computation using the depend directive: 

Marc Betoule's avatar
Marc Betoule committed
961
#+begin_src python
962
#depend file1 file2
Marc Betoule's avatar
Marc Betoule committed
963
#+end_src
964 965 966 967 968 969 970 971 972

At the very beginning of the pipeline execution, all dependencies will
be stored, to prevent any change (code edition) between the key
computation and actual processing.

Note that this mechanism works only for segment and hook
scripts. External dependencies are also read as the beginning of the
pipeline execution, but only used for the key computation.

Marc Betoule's avatar
Marc Betoule committed
973 974
** Database reconstruction

975 976 977
In case of unfortunate lost of the pipeline sql data base, it is
possible to reconstruct it from the disk : 

Marc Betoule's avatar
Marc Betoule committed
978
#+begin_src python
979 980
import pipelet
pipelet.utils.rebuild_db_from_disk (prefix, sqlfile)
Marc Betoule's avatar
Marc Betoule committed
981
#+end_src
Marc Betoule's avatar
Marc Betoule committed
982

983 984 985
All information will be retrieve, but with new identifiers. 

** The hooking system
986

987
As described in the 'segment environment' section, Pipelet supports
988 989 990 991 992 993
an hooking system which allows the use of generic processing code, and
code sectioning.

Let's consider a set of instructions that have to be systematically
applied at the end of a segment (post processing), one can put those
instruction in the separate script file named for example
Marc Betoule's avatar
Marc Betoule committed
994
=segname_postproc.py= and calls the hook function: 
995

Marc Betoule's avatar
Marc Betoule committed
996
#+begin_src python
997
hook('postproc', globals()) 
Marc Betoule's avatar
Marc Betoule committed
998
#+end_src
999

1000
A specific dictionary can be passed to the hook script to avoid
1001 1002
confusion. 

Marc Betoule's avatar
Marc Betoule committed
1003
The hook scripts are included into the hash key computation.
1004

1005 1006