-
Notifications
You must be signed in to change notification settings - Fork 23
/
part04.tex
475 lines (370 loc) · 12.4 KB
/
part04.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
\documentclass[english,serif,mathserif,xcolor=pdftex,dvipsnames,table]{beamer}
\usetheme{gc3}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{gc3}
\title[Introduction]{%
Customizing \\ session-based scripts
}
\author[R. Murri, S3IT UZH]{%
Riccardo Murri \texttt{<riccardo.murri@uzh.ch>}
\\[1ex]
\emph{S3IT: Services and Support for Science IT}
\\[1ex]
University of Zurich
}
\date{July~11--14, 2016}
\begin{document}
% title frame
\maketitle
\begin{frame}
\frametitle{Outline}
\begin{enumerate}
\item Customize positional argument processing
\item Customize option processing
\item More complex \texttt{new\_tasks()} implementations
\end{enumerate}
\end{frame}
\begin{frame}[fragile]
\frametitle{What is command-line argument processing?}
Problem:
\begin{sh}
$ python ex4a.py --foo=1 bar baz
\end{sh}%$
\+
How do we translate the above shell command invocation into a
Python procedure call \\ \texttt{ex4a('bar', 'baz', foo=1)}?
\+
In other words: how do we access the command-line arguments given
to the shell from Python code?
\end{frame}
\begin{frame}
\frametitle{Command-line argument processing in GC3Pie}
GC3Pie scripts use the standard Python
library~\href{https://docs.python.org/2/howto/argparse.html}{\texttt{argparse}}.
\+
However, differently from \texttt{argparse}, GC3Pie scripts use
separate ways to configure processing of options and positional
arguments.
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
Positional arguments are defined in a method called
\lstinline|setup_args()|; override it in derived classes to change
what arguments are accepted, their names and number.
\+
This is the default implementation:
\begin{python}
def setup_args(self):
self.add_param(
'args', nargs='*', metavar='INPUT',
help="Path to input file or directory.")
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
\begin{python}
def setup_args(self):
self.add_param(
~\HL{'args'}~, nargs='*', metavar='INPUT',
help="Path to input file or directory.")
\end{python}
\+
The value of this command-line argument will be recorded in the
variable \texttt{self.params.\HL{args}} in Python code.
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
\begin{python}
def setup_args(self):
self.add_param(
'args', ~\HL{nargs='*'}~, metavar='INPUT',
help="Path to input file or directory.")
\end{python}
\+
There are 0 or more arguments of this kind. \\
Makes \lstinline|self.params.args| into a Python list.
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
\begin{python}
def setup_args(self):
self.add_param('args', ~\HL{nargs=\ldots}~, ~{\em [\ldots]}~)
\end{python}
\+
Other possible values for the \texttt{nargs} parameter are:
\begin{itemize}
\item \texttt{nargs='+'} --- there are \emph{1 or more} arguments,
i.e., at least one argument is required.
\item \texttt{nargs=$N$} --- there are exactly $N > 1$ arguments of this kind.
\item \texttt{nargs=$1$} (default) --- one single argument of this
kind; \textbf{in this case \lstinline|self.params.args| is a
\emph{string}, not a list.}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
\begin{python}
def setup_args(self):
self.add_param(
'args', nargs='*', ~\HL{metavar='INPUT'}~,
help="Path to input file or directory.")
\end{python}
\+
The name of the command-line argument as displayed to users in
usage and help texts:
\begin{sh}
$ python solutions/ex2c.py --help
usage: ex2c [-h] [-V] [-v] [--config-files CONFIG_FILES] [-c NUM]
[-m GIGABYTES] [-r NAME] [-w DURATION] [-s PATH] [-u URL] [-N]
[-C NUM] [-J NUM] [-o DIRECTORY] [-l [STATES]]
~\HL{[INPUT [INPUT ...]]}~
\end{sh}%$
\+
If not given, defaults to the uppercased version of the ``internal'' name.
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
\begin{python}
def setup_args(self):
self.add_param(
'args', nargs='*', metavar='INPUT',
~\HL{help="Path to input file or directory.")}~
\end{python}
\+
Description of the command-line argument in \texttt{-{}-help} text.
\begin{sh}
$ python solutions/ex2c.py --help
usage: ex2c [-h] ~{\em [\ldots]}~
positional arguments:
~\HL{INPUT}~ ~\HL{Path to input file or directory.}~
\end{sh}%$
\end{frame}
\begin{frame}[fragile]
\frametitle{Positional argument processing}
Calls to \lstinline|self.add_param()| can be repeated to
parse many different command-line arguments in sequence:
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_args(self):
self.add_param('input', help="Input file")
self.add_param('radius', help="Convolution radius")
self.add_param('sigma', help="Threshold")
\end{python}
\+
With the above definition, the following command-line:
\begin{sh}
$ python example.py file.img 10 2.1
\end{sh}%$
generates the equivalent of the following Python code:
\begin{python}
self.params.input = 'example.py'
self.params.radius = '10' # it's a string!
self.params.sigma = '2.1' # this one too!
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Detour: From grayscale to colors}
\begin{sh}
$ convert gray-bfly.jpg \
( xc:blue xc:magenta xc:yellow +append ) \
-clut color-bfly.jpg
\end{sh}%$
\begin{tabular}{ccc}
{\includegraphics[width=0.4\linewidth]{fig/gray-butterfly.jpg}}
&
{\includegraphics[width=0.1\linewidth,totalheight=0.25\textheight]{fig/arrow.pdf}}
&
{\includegraphics[width=0.4\linewidth]{fig/rgb-butterfly.jpg}}
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\begin{exercise*}[4.A]
Write a \texttt{colorize.py} script to apply this colorization process to a set of grayscale images.
\+
The \texttt{colorize.py} script shall be invoked like this:
\begin{sh}
$ python colorize.py ~\em c1~ ~\em c2~ ~\em c3~ ~\em img1~ ~[\em img2 \ldots]~
\end{sh}%$
where \texttt{\em c1}, \texttt{\em c2}, \texttt{\em c3} are color
names and \texttt{\em img1}, \texttt{\em img2} are image files.
\+
Each image shall be processed in a separate colorization task.
\end{exercise*}
\end{frame}
\begin{frame}[fragile]
\frametitle{Argument types}
You can ask
\href{https://docs.python.org/2/howto/argparse.html}{\texttt{argparse}}
and GC3Pie to convert a command-line argument to a certain Python type.
\+ For example:
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_args(self):
# this argument is a string (default type)
self.add_param('input', type=str, help="...")
# the `radius` argument is an integer
self.add_param('radius', type=int, help="...")
# the `sigma` argument is a floating-point number
self.add_param('sigma', type=float, help="...")
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Argument types}
Declaring argument types makes for better usability: if an argument
does not match its type, the script exists immediately and the user
is notified with a clear error message.
\+
\begin{sh}
$ python downloads/argp.py foo.txt 123 x
usage: argparse [-h] ~\em [\ldots]~
input radius sigma
argp: error: argument sigma: invalid float value: 'x'
\end{sh}%$
\end{frame}
\begin{frame}[fragile]
\frametitle{Argument types}\small
In addition to the standard Python types, GC3Pie provides other
validation functions to ensure arguments meet commonly-found
conditions.
\begin{python}
from gc3libs.cmdline import \
existing_file, positive_int
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_args(self):
# reject non-existent input files outright
self.add_param('input', type=existing_file, ~\ldots~)
# force radius to be > 0
self.add_param('radius', type=positive_int, ~\ldots~)
# sigma is a floating-point number
self.add_param('sigma', type=float, ~\ldots~)
\end{python}
\begin{references}
\tiny\url{http://gc3pie.readthedocs.io/en/master/programmers/api/gc3libs/cmdline.html}
\end{references}
\end{frame}
\begin{frame}[fragile]
\frametitle{Command-line option processing}
Command-line options can be \textbf{added} to a session-based script
by overriding the \lstinline|setup_options()| method:
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_options(self):
self.add_param('--e-value', '-e', dest='e_value',
type=float, default=10.0,
help="Expectation value")
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Command-line option processing}
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_options(self):
self.add_param('--e-value', ~\HL{'-e'}~, dest='e_value',
type=float, default=10.0,
help="Expectation value")
\end{python}
\+
Aliases and abbreviations for options can be defined.
\end{frame}
\begin{frame}[fragile]
\frametitle{Command-line option processing}
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_options(self):
self.add_param('--e-value', '-e', ~\HL{dest='e\_value'}~,
type=float, default=10.0,
help="Expectation value")
\end{python}
\+
The value of this option will be stored in \lstinline|self.params.|\texttt{\HL{e\_value}}.
\end{frame}
\begin{frame}[fragile]
\frametitle{Command-line option processing}
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_options(self):
self.add_param('--e-value', '-e', dest='e_value',
~\HL{type=float}~, default=10.0,
help="Expectation value")
\end{python}
\+
Types work exactly as for positional arguments.
\end{frame}
\begin{frame}[fragile]
\frametitle{Command-line option processing}
\begin{python}
class AScript(SessionBasedScript):
# ~\em [\ldots]~
def setup_options(self):
self.add_param('--e-value', '-e', dest='e_value',
type=float, ~\HL{default=10.0}~,
help="Expectation value")
\end{python}
\+
If the option is not present on the command-line, the associated
Python variable (here,
\lstinline|self.params.|\texttt{\HL{e\_value}}) takes this value.
\end{frame}
\begin{frame}[fragile]
\frametitle{Detour: BLAST}
BLAST is a suite of programs to perform search and alignment of
nucleotides and proteins.
\+ One common use of BLAST is the following: given a file describing
a new organism, compare it one-to-one to a set of known organisms to
find similarities.
\+ The command-line invocation for one such comparison would look like this:
\begin{sh}
$ blastp -query new.faa -subject known.faa \
-evalue 1e-6 -outfmt 9
\end{sh}%$
\end{frame}
\begin{frame}[fragile]
\begin{exercise*}[4.B]\small
Write a \texttt{topblast.py} script to perform 1-1 BLAST comparisons.
\+
The \texttt{topblast.py} script shall be invoked like this:
\begin{sh}
$ python topblast.py [-e ~$T$~] [-m ~$F$~] \
new.faa k1.faa [k2.faa ...]
\end{sh}%$
where:
\begin{itemize}
\item Option \texttt{-e} (alias: \texttt{-{}-e-value}) takes a floating point threshold argument $T$;
\item Option \texttt{-m} (alias: \texttt{-{}-output-format}) takes a single-digit integer argument $F$;
\item Arguments \texttt{new.faa}, \texttt{k1.faa}, etc. are files.
\end{itemize}
\+ The script should generate and run comparisons between
\texttt{new.faa} and each of the {k$N$.faa}. Each 1-1 comparison
should run as a separate task. All of them share the same
settings for the \texttt{-evalue} and \texttt{-outfmt} options for
\texttt{blastp}.
\end{exercise*}
\end{frame}
\begin{frame}[fragile]
\begin{exercise*}[4.C] \emph{(Homework)}
Modify the \texttt{topblast.py} script that you've written in Exercise 4.B
to be invoked like this:
\begin{sh}
$ python topblast.py [-e ~$T$~] [-m ~$F$~] new.faa dir
\end{sh}%$
\+ Input files describing the ``known'' subjects should be found by
recursively scanning the given directory path.
\+ Bonus points if the modified script exists with a correct error
message in case \texttt{new.faa} is not an existing file, or
\texttt{dir} is not a valid directory path.
\end{exercise*}
\end{frame}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: