Show More
@@ -1,1093 +1,1093 b'' | |||
|
1 | 1 | ================= |
|
2 | 2 | IPython reference |
|
3 | 3 | ================= |
|
4 | 4 | |
|
5 | 5 | .. _command_line_options: |
|
6 | 6 | |
|
7 | 7 | Command-line usage |
|
8 | 8 | ================== |
|
9 | 9 | |
|
10 | 10 | You start IPython with the command:: |
|
11 | 11 | |
|
12 | 12 | $ ipython [options] files |
|
13 | 13 | |
|
14 | 14 | If invoked with no options, it executes all the files listed in sequence |
|
15 | 15 | and drops you into the interpreter while still acknowledging any options |
|
16 | 16 | you may have set in your ipython_config.py. This behavior is different from |
|
17 | 17 | standard Python, which when called as python -i will only execute one |
|
18 | 18 | file and ignore your configuration setup. |
|
19 | 19 | |
|
20 | 20 | Please note that some of the configuration options are not available at |
|
21 | 21 | the command line, simply because they are not practical here. Look into |
|
22 | 22 | your configuration files for details on those. There are separate configuration |
|
23 | 23 | files for each profile, and the files look like :file:`ipython_config.py` or |
|
24 | 24 | :file:`ipython_config_{frontendname}.py`. Profile directories look like |
|
25 | 25 | :file:`profile_{profilename}` and are typically installed in the :envvar:`IPYTHONDIR` directory, |
|
26 | 26 | which defaults to :file:`$HOME/.ipython`. For Windows users, :envvar:`HOME` |
|
27 | 27 | resolves to :file:`C:\\Users\\{YourUserName}` in most instances. |
|
28 | 28 | |
|
29 | 29 | Command-line Options |
|
30 | 30 | -------------------- |
|
31 | 31 | |
|
32 | 32 | To see the options IPython accepts, use ``ipython --help`` (and you probably |
|
33 | 33 | should run the output through a pager such as ``ipython --help | less`` for |
|
34 | 34 | more convenient reading). This shows all the options that have a single-word |
|
35 | 35 | alias to control them, but IPython lets you configure all of its objects from |
|
36 | 36 | the command-line by passing the full class name and a corresponding value; type |
|
37 | 37 | ``ipython --help-all`` to see this full list. For example:: |
|
38 | 38 | |
|
39 | 39 | ipython --matplotlib qt |
|
40 | 40 | |
|
41 | 41 | is equivalent to:: |
|
42 | 42 | |
|
43 | 43 | ipython --TerminalIPythonApp.matplotlib='qt' |
|
44 | 44 | |
|
45 | 45 | Note that in the second form, you *must* use the equal sign, as the expression |
|
46 | 46 | is evaluated as an actual Python assignment. While in the above example the |
|
47 | 47 | short form is more convenient, only the most common options have a short form, |
|
48 | 48 | while any configurable variable in IPython can be set at the command-line by |
|
49 | 49 | using the long form. This long form is the same syntax used in the |
|
50 | 50 | configuration files, if you want to set these options permanently. |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | Interactive use |
|
54 | 54 | =============== |
|
55 | 55 | |
|
56 | 56 | IPython is meant to work as a drop-in replacement for the standard interactive |
|
57 | 57 | interpreter. As such, any code which is valid python should execute normally |
|
58 | 58 | under IPython (cases where this is not true should be reported as bugs). It |
|
59 | 59 | does, however, offer many features which are not available at a standard python |
|
60 | 60 | prompt. What follows is a list of these. |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | Caution for Windows users |
|
64 | 64 | ------------------------- |
|
65 | 65 | |
|
66 | 66 | Windows, unfortunately, uses the '\\' character as a path separator. This is a |
|
67 | 67 | terrible choice, because '\\' also represents the escape character in most |
|
68 | 68 | modern programming languages, including Python. For this reason, using '/' |
|
69 | 69 | character is recommended if you have problems with ``\``. However, in Windows |
|
70 | 70 | commands '/' flags options, so you can not use it for the root directory. This |
|
71 | 71 | means that paths beginning at the root must be typed in a contrived manner |
|
72 | 72 | like: ``%copy \opt/foo/bar.txt \tmp`` |
|
73 | 73 | |
|
74 | 74 | .. _magic: |
|
75 | 75 | |
|
76 | 76 | Magic command system |
|
77 | 77 | -------------------- |
|
78 | 78 | |
|
79 | 79 | IPython will treat any line whose first character is a % as a special |
|
80 | 80 | call to a 'magic' function. These allow you to control the behavior of |
|
81 | 81 | IPython itself, plus a lot of system-type features. They are all |
|
82 | 82 | prefixed with a % character, but parameters are given without |
|
83 | 83 | parentheses or quotes. |
|
84 | 84 | |
|
85 | 85 | Lines that begin with ``%%`` signal a *cell magic*: they take as arguments not |
|
86 | 86 | only the rest of the current line, but all lines below them as well, in the |
|
87 | 87 | current execution block. Cell magics can in fact make arbitrary modifications |
|
88 | 88 | to the input they receive, which need not even be valid Python code at all. |
|
89 | 89 | They receive the whole block as a single string. |
|
90 | 90 | |
|
91 | 91 | As a line magic example, the ``%cd`` magic works just like the OS command of |
|
92 | 92 | the same name:: |
|
93 | 93 | |
|
94 | 94 | In [8]: %cd |
|
95 | 95 | /home/fperez |
|
96 | 96 | |
|
97 | 97 | The following uses the builtin ``timeit`` in cell mode:: |
|
98 | 98 | |
|
99 | 99 | In [10]: %%timeit x = range(10000) |
|
100 | 100 | ...: min(x) |
|
101 | 101 | ...: max(x) |
|
102 | 102 | ...: |
|
103 | 103 | 1000 loops, best of 3: 438 us per loop |
|
104 | 104 | |
|
105 | 105 | In this case, ``x = range(10000)`` is called as the line argument, and the |
|
106 | 106 | block with ``min(x)`` and ``max(x)`` is called as the cell body. The |
|
107 | 107 | ``timeit`` magic receives both. |
|
108 | 108 | |
|
109 | 109 | If you have 'automagic' enabled (as it by default), you don't need to type in |
|
110 | 110 | the single ``%`` explicitly for line magics; IPython will scan its internal |
|
111 | 111 | list of magic functions and call one if it exists. With automagic on you can |
|
112 | 112 | then just type ``cd mydir`` to go to directory 'mydir':: |
|
113 | 113 | |
|
114 | 114 | In [9]: cd mydir |
|
115 | 115 | /home/fperez/mydir |
|
116 | 116 | |
|
117 | 117 | Note that cell magics *always* require an explicit ``%%`` prefix, automagic |
|
118 | 118 | calling only works for line magics. |
|
119 | 119 | |
|
120 | 120 | The automagic system has the lowest possible precedence in name searches, so |
|
121 | 121 | you can freely use variables with the same names as magic commands. If a magic |
|
122 | 122 | command is 'shadowed' by a variable, you will need the explicit ``%`` prefix to |
|
123 | 123 | use it: |
|
124 | 124 | |
|
125 | 125 | .. sourcecode:: ipython |
|
126 | 126 | |
|
127 | 127 | In [1]: cd ipython # %cd is called by automagic |
|
128 | 128 | /home/fperez/ipython |
|
129 | 129 | |
|
130 | 130 | In [2]: cd=1 # now cd is just a variable |
|
131 | 131 | |
|
132 | 132 | In [3]: cd .. # and doesn't work as a function anymore |
|
133 | 133 | File "<ipython-input-3-9fedb3aff56c>", line 1 |
|
134 | 134 | cd .. |
|
135 | 135 | ^ |
|
136 | 136 | SyntaxError: invalid syntax |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | In [4]: %cd .. # but %cd always works |
|
140 | 140 | /home/fperez |
|
141 | 141 | |
|
142 | 142 | In [5]: del cd # if you remove the cd variable, automagic works again |
|
143 | 143 | |
|
144 | 144 | In [6]: cd ipython |
|
145 | 145 | |
|
146 | 146 | /home/fperez/ipython |
|
147 | 147 | |
|
148 | 148 | .. _defining_magics: |
|
149 | 149 | |
|
150 | 150 | Defining your own magics |
|
151 | 151 | ++++++++++++++++++++++++ |
|
152 | 152 | |
|
153 | 153 | There are two main ways to define your own magic functions: from standalone |
|
154 | 154 | functions and by inheriting from a base class provided by IPython: |
|
155 | 155 | :class:`IPython.core.magic.Magics`. Below we show code you can place in a file |
|
156 | 156 | that you load from your configuration, such as any file in the ``startup`` |
|
157 | 157 | subdirectory of your default IPython profile. |
|
158 | 158 | |
|
159 | 159 | First, let us see the simplest case. The following shows how to create a line |
|
160 | 160 | magic, a cell one and one that works in both modes, using just plain functions: |
|
161 | 161 | |
|
162 | 162 | .. sourcecode:: python |
|
163 | 163 | |
|
164 | 164 | from IPython.core.magic import (register_line_magic, register_cell_magic, |
|
165 | 165 | register_line_cell_magic) |
|
166 | 166 | |
|
167 | 167 | @register_line_magic |
|
168 | 168 | def lmagic(line): |
|
169 | 169 | "my line magic" |
|
170 | 170 | return line |
|
171 | 171 | |
|
172 | 172 | @register_cell_magic |
|
173 | 173 | def cmagic(line, cell): |
|
174 | 174 | "my cell magic" |
|
175 | 175 | return line, cell |
|
176 | 176 | |
|
177 | 177 | @register_line_cell_magic |
|
178 | 178 | def lcmagic(line, cell=None): |
|
179 | 179 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
180 | 180 | if cell is None: |
|
181 | 181 | print("Called as line magic") |
|
182 | 182 | return line |
|
183 | 183 | else: |
|
184 | 184 | print("Called as cell magic") |
|
185 | 185 | return line, cell |
|
186 | 186 | |
|
187 | 187 | # We delete these to avoid name conflicts for automagic to work |
|
188 | 188 | del lmagic, lcmagic |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | You can also create magics of all three kinds by inheriting from the |
|
192 | 192 | :class:`IPython.core.magic.Magics` class. This lets you create magics that can |
|
193 | 193 | potentially hold state in between calls, and that have full access to the main |
|
194 | 194 | IPython object: |
|
195 | 195 | |
|
196 | 196 | .. sourcecode:: python |
|
197 | 197 | |
|
198 | 198 | # This code can be put in any Python module, it does not require IPython |
|
199 | 199 | # itself to be running already. It only creates the magics subclass but |
|
200 | 200 | # doesn't instantiate it yet. |
|
201 | 201 | from __future__ import print_function |
|
202 | 202 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
203 | 203 | cell_magic, line_cell_magic) |
|
204 | 204 | |
|
205 | 205 | # The class MUST call this class decorator at creation time |
|
206 | 206 | @magics_class |
|
207 | 207 | class MyMagics(Magics): |
|
208 | 208 | |
|
209 | 209 | @line_magic |
|
210 | 210 | def lmagic(self, line): |
|
211 | 211 | "my line magic" |
|
212 | 212 | print("Full access to the main IPython object:", self.shell) |
|
213 | 213 | print("Variables in the user namespace:", list(self.shell.user_ns.keys())) |
|
214 | 214 | return line |
|
215 | 215 | |
|
216 | 216 | @cell_magic |
|
217 | 217 | def cmagic(self, line, cell): |
|
218 | 218 | "my cell magic" |
|
219 | 219 | return line, cell |
|
220 | 220 | |
|
221 | 221 | @line_cell_magic |
|
222 | 222 | def lcmagic(self, line, cell=None): |
|
223 | 223 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
224 | 224 | if cell is None: |
|
225 | 225 | print("Called as line magic") |
|
226 | 226 | return line |
|
227 | 227 | else: |
|
228 | 228 | print("Called as cell magic") |
|
229 | 229 | return line, cell |
|
230 | 230 | |
|
231 | 231 | |
|
232 | 232 | # In order to actually use these magics, you must register them with a |
|
233 | 233 | # running IPython. This code must be placed in a file that is loaded once |
|
234 | 234 | # IPython is up and running: |
|
235 | 235 | ip = get_ipython() |
|
236 | 236 | # You can register the class itself without instantiating it. IPython will |
|
237 | 237 | # call the default constructor on it. |
|
238 | 238 | ip.register_magics(MyMagics) |
|
239 | 239 | |
|
240 | 240 | If you want to create a class with a different constructor that holds |
|
241 | 241 | additional state, then you should always call the parent constructor and |
|
242 | 242 | instantiate the class yourself before registration: |
|
243 | 243 | |
|
244 | 244 | .. sourcecode:: python |
|
245 | 245 | |
|
246 | 246 | @magics_class |
|
247 | 247 | class StatefulMagics(Magics): |
|
248 | 248 | "Magics that hold additional state" |
|
249 | 249 | |
|
250 | 250 | def __init__(self, shell, data): |
|
251 | 251 | # You must call the parent constructor |
|
252 | 252 | super(StatefulMagics, self).__init__(shell) |
|
253 | 253 | self.data = data |
|
254 | 254 | |
|
255 | 255 | # etc... |
|
256 | 256 | |
|
257 | 257 | # This class must then be registered with a manually created instance, |
|
258 | 258 | # since its constructor has different arguments from the default: |
|
259 | 259 | ip = get_ipython() |
|
260 | 260 | magics = StatefulMagics(ip, some_data) |
|
261 | 261 | ip.register_magics(magics) |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | In earlier versions, IPython had an API for the creation of line magics (cell |
|
265 | 265 | magics did not exist at the time) that required you to create functions with a |
|
266 | 266 | method-looking signature and to manually pass both the function and the name. |
|
267 | 267 | While this API is no longer recommended, it remains indefinitely supported for |
|
268 | 268 | backwards compatibility purposes. With the old API, you'd create a magic as |
|
269 | 269 | follows: |
|
270 | 270 | |
|
271 | 271 | .. sourcecode:: python |
|
272 | 272 | |
|
273 | 273 | def func(self, line): |
|
274 | 274 | print("Line magic called with line:", line) |
|
275 | 275 | print("IPython object:", self.shell) |
|
276 | 276 | |
|
277 | 277 | ip = get_ipython() |
|
278 | 278 | # Declare this function as the magic %mycommand |
|
279 | 279 | ip.define_magic('mycommand', func) |
|
280 | 280 | |
|
281 | 281 | Type ``%magic`` for more information, including a list of all available magic |
|
282 | 282 | functions at any time and their docstrings. You can also type |
|
283 | 283 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for |
|
284 | 284 | information on the '?' system) to get information about any particular magic |
|
285 | 285 | function you are interested in. |
|
286 | 286 | |
|
287 | 287 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
288 | 288 | docstrings of all currently available magic commands. |
|
289 | 289 | |
|
290 | 290 | |
|
291 | 291 | Access to the standard Python help |
|
292 | 292 | ---------------------------------- |
|
293 | 293 | |
|
294 | 294 | Simply type ``help()`` to access Python's standard help system. You can |
|
295 | 295 | also type ``help(object)`` for information about a given object, or |
|
296 | 296 | ``help('keyword')`` for information on a keyword. You may need to configure your |
|
297 | 297 | PYTHONDOCS environment variable for this feature to work correctly. |
|
298 | 298 | |
|
299 | 299 | .. _dynamic_object_info: |
|
300 | 300 | |
|
301 | 301 | Dynamic object information |
|
302 | 302 | -------------------------- |
|
303 | 303 | |
|
304 | 304 | Typing ``?word`` or ``word?`` prints detailed information about an object. If |
|
305 | 305 | certain strings in the object are too long (e.g. function signatures) they get |
|
306 | 306 | snipped in the center for brevity. This system gives access variable types and |
|
307 | 307 | values, docstrings, function prototypes and other useful information. |
|
308 | 308 | |
|
309 | 309 | If the information will not fit in the terminal, it is displayed in a pager |
|
310 | 310 | (``less`` if available, otherwise a basic internal pager). |
|
311 | 311 | |
|
312 | 312 | Typing ``??word`` or ``word??`` gives access to the full information, including |
|
313 | 313 | the source code where possible. Long strings are not snipped. |
|
314 | 314 | |
|
315 | 315 | The following magic functions are particularly useful for gathering |
|
316 | 316 | information about your working environment. You can get more details by |
|
317 | 317 | typing ``%magic`` or querying them individually (``%function_name?``); |
|
318 | 318 | this is just a summary: |
|
319 | 319 | |
|
320 | 320 | * **%pdoc <object>**: Print (or run through a pager if too long) the |
|
321 | 321 | docstring for an object. If the given object is a class, it will |
|
322 | 322 | print both the class and the constructor docstrings. |
|
323 | 323 | * **%pdef <object>**: Print the call signature for any callable |
|
324 | 324 | object. If the object is a class, print the constructor information. |
|
325 | 325 | * **%psource <object>**: Print (or run through a pager if too long) |
|
326 | 326 | the source code for an object. |
|
327 | 327 | * **%pfile <object>**: Show the entire source file where an object was |
|
328 | 328 | defined via a pager, opening it at the line where the object |
|
329 | 329 | definition begins. |
|
330 | 330 | * **%who/%whos**: These functions give information about identifiers |
|
331 | 331 | you have defined interactively (not things you loaded or defined |
|
332 | 332 | in your configuration files). %who just prints a list of |
|
333 | 333 | identifiers and %whos prints a table with some basic details about |
|
334 | 334 | each identifier. |
|
335 | 335 | |
|
336 | 336 | Note that the dynamic object information functions (?/??, ``%pdoc``, |
|
337 | 337 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as |
|
338 | 338 | directly on variables. For example, after doing ``import os``, you can use |
|
339 | 339 | ``os.path.abspath??``. |
|
340 | 340 | |
|
341 | 341 | .. _readline: |
|
342 | 342 | |
|
343 | 343 | Readline-based features |
|
344 | 344 | ----------------------- |
|
345 | 345 | |
|
346 | 346 | These features require the GNU readline library, so they won't work if your |
|
347 | 347 | Python installation lacks readline support. We will first describe the default |
|
348 | 348 | behavior IPython uses, and then how to change it to suit your preferences. |
|
349 | 349 | |
|
350 | 350 | |
|
351 | 351 | Command line completion |
|
352 | 352 | +++++++++++++++++++++++ |
|
353 | 353 | |
|
354 | 354 | At any time, hitting TAB will complete any available python commands or |
|
355 | 355 | variable names, and show you a list of the possible completions if |
|
356 | 356 | there's no unambiguous one. It will also complete filenames in the |
|
357 | 357 | current directory if no python names match what you've typed so far. |
|
358 | 358 | |
|
359 | 359 | |
|
360 | 360 | Search command history |
|
361 | 361 | ++++++++++++++++++++++ |
|
362 | 362 | |
|
363 | 363 | IPython provides two ways for searching through previous input and thus |
|
364 | 364 | reduce the need for repetitive typing: |
|
365 | 365 | |
|
366 | 366 | 1. Start typing, and then use the up and down arrow keys (or :kbd:`Ctrl-p` |
|
367 | 367 | and :kbd:`Ctrl-n`) to search through only the history items that match |
|
368 | 368 | what you've typed so far. |
|
369 | 369 | 2. Hit :kbd:`Ctrl-r`: to open a search prompt. Begin typing and the system |
|
370 | 370 | searches your history for lines that contain what you've typed so |
|
371 | 371 | far, completing as much as it can. |
|
372 | 372 | |
|
373 | 373 | IPython will save your input history when it leaves and reload it next |
|
374 | 374 | time you restart it. By default, the history file is named |
|
375 | 375 | :file:`.ipython/profile_{name}/history.sqlite`. |
|
376 | 376 | |
|
377 | 377 | Autoindent |
|
378 | 378 | ++++++++++ |
|
379 | 379 | |
|
380 | 380 | IPython can recognize lines ending in ':' and indent the next line, |
|
381 | 381 | while also un-indenting automatically after 'raise' or 'return'. |
|
382 | 382 | |
|
383 | 383 | This feature uses the readline library, so it will honor your |
|
384 | 384 | :file:`~/.inputrc` configuration (or whatever file your :envvar:`INPUTRC` environment variable points |
|
385 | 385 | to). Adding the following lines to your :file:`.inputrc` file can make |
|
386 | 386 | indenting/unindenting more convenient (M-i indents, M-u unindents):: |
|
387 | 387 | |
|
388 | 388 | # if you don't already have a ~/.inputrc file, you need this include: |
|
389 | 389 | $include /etc/inputrc |
|
390 | 390 | |
|
391 | 391 | $if Python |
|
392 | 392 | "\M-i": " " |
|
393 | 393 | "\M-u": "\d\d\d\d" |
|
394 | 394 | $endif |
|
395 | 395 | |
|
396 | 396 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
397 | 397 | |
|
398 | 398 | .. warning:: |
|
399 | 399 | |
|
400 | 400 | Setting the above indents will cause problems with unicode text entry in |
|
401 | 401 | the terminal. |
|
402 | 402 | |
|
403 | 403 | .. warning:: |
|
404 | 404 | |
|
405 | 405 | Autoindent is ON by default, but it can cause problems with the pasting of |
|
406 | 406 | multi-line indented code (the pasted code gets re-indented on each line). A |
|
407 | 407 | magic function %autoindent allows you to toggle it on/off at runtime. You |
|
408 | 408 | can also disable it permanently on in your :file:`ipython_config.py` file |
|
409 | 409 | (set TerminalInteractiveShell.autoindent=False). |
|
410 | 410 | |
|
411 | 411 | If you want to paste multiple lines in the terminal, it is recommended that |
|
412 | 412 | you use ``%paste``. |
|
413 | 413 | |
|
414 | 414 | |
|
415 | 415 | Customizing readline behavior |
|
416 | 416 | +++++++++++++++++++++++++++++ |
|
417 | 417 | |
|
418 | 418 | All these features are based on the GNU readline library, which has an |
|
419 | 419 | extremely customizable interface. Normally, readline is configured via a |
|
420 | 420 | :file:`.inputrc` file. IPython respects this, and you can also customise readline |
|
421 | 421 | by setting the following :doc:`configuration </config/intro>` options: |
|
422 | 422 | |
|
423 | 423 | * ``InteractiveShell.readline_parse_and_bind``: this holds a list of strings to be executed |
|
424 | 424 | via a readline.parse_and_bind() command. The syntax for valid commands |
|
425 | 425 | of this kind can be found by reading the documentation for the GNU |
|
426 | 426 | readline library, as these commands are of the kind which readline |
|
427 | 427 | accepts in its configuration file. |
|
428 | 428 | * ``InteractiveShell.readline_remove_delims``: a string of characters to be removed |
|
429 | 429 | from the default word-delimiters list used by readline, so that |
|
430 | 430 | completions may be performed on strings which contain them. Do not |
|
431 | 431 | change the default value unless you know what you're doing. |
|
432 | 432 | |
|
433 | 433 | You will find the default values in your configuration file. |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | Session logging and restoring |
|
437 | 437 | ----------------------------- |
|
438 | 438 | |
|
439 | 439 | You can log all input from a session either by starting IPython with the |
|
440 | 440 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) |
|
441 | 441 | or by activating the logging at any moment with the magic function %logstart. |
|
442 | 442 | |
|
443 | 443 | Log files can later be reloaded by running them as scripts and IPython |
|
444 | 444 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
445 | 445 | restoring the state of a previous session. This feature is not quite |
|
446 | 446 | perfect, but can still be useful in many cases. |
|
447 | 447 | |
|
448 | 448 | The log files can also be used as a way to have a permanent record of |
|
449 | 449 | any code you wrote while experimenting. Log files are regular text files |
|
450 | 450 | which you can later open in your favorite text editor to extract code or |
|
451 | 451 | to 'clean them up' before using them to replay a session. |
|
452 | 452 | |
|
453 | 453 | The `%logstart` function for activating logging in mid-session is used as |
|
454 | 454 | follows:: |
|
455 | 455 | |
|
456 | 456 | %logstart [log_name [log_mode]] |
|
457 | 457 | |
|
458 | 458 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
459 | 459 | current working directory, in 'rotate' mode (see below). |
|
460 | 460 | |
|
461 | 461 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
462 | 462 | history up to that point and then continues logging. |
|
463 | 463 | |
|
464 | 464 | %logstart takes a second optional parameter: logging mode. This can be |
|
465 | 465 | one of (note that the modes are given unquoted): |
|
466 | 466 | |
|
467 | 467 | * [over:] overwrite existing log_name. |
|
468 | 468 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
469 | 469 | * [append:] well, that says it. |
|
470 | 470 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
471 | 471 | |
|
472 | 472 | The %logoff and %logon functions allow you to temporarily stop and |
|
473 | 473 | resume logging to a file which had previously been started with |
|
474 | 474 | %logstart. They will fail (with an explanation) if you try to use them |
|
475 | 475 | before logging has been started. |
|
476 | 476 | |
|
477 | 477 | .. _system_shell_access: |
|
478 | 478 | |
|
479 | 479 | System shell access |
|
480 | 480 | ------------------- |
|
481 | 481 | |
|
482 | 482 | Any input line beginning with a ! character is passed verbatim (minus |
|
483 | 483 | the !, of course) to the underlying operating system. For example, |
|
484 | 484 | typing ``!ls`` will run 'ls' in the current directory. |
|
485 | 485 | |
|
486 | 486 | Manual capture of command output |
|
487 | 487 | -------------------------------- |
|
488 | 488 | |
|
489 | 489 | You can assign the result of a system command to a Python variable with the |
|
490 | 490 | syntax ``myfiles = !ls``. This gets machine readable output from stdout |
|
491 | 491 | (e.g. without colours), and splits on newlines. To explicitly get this sort of |
|
492 | 492 | output without assigning to a variable, use two exclamation marks (``!!ls``) or |
|
493 | 493 | the ``%sx`` magic command. |
|
494 | 494 | |
|
495 | 495 | The captured list has some convenience features. ``myfiles.n`` or ``myfiles.s`` |
|
496 | 496 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` |
|
497 | 497 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. |
|
498 | 498 | See :ref:`string_lists` for details. |
|
499 | 499 | |
|
500 | 500 | IPython also allows you to expand the value of python variables when |
|
501 | 501 | making system calls. Wrap variables or expressions in {braces}:: |
|
502 | 502 | |
|
503 | 503 | In [1]: pyvar = 'Hello world' |
|
504 | 504 | In [2]: !echo "A python variable: {pyvar}" |
|
505 | 505 | A python variable: Hello world |
|
506 | 506 | In [3]: import math |
|
507 | 507 | In [4]: x = 8 |
|
508 | 508 | In [5]: !echo {math.factorial(x)} |
|
509 | 509 | 40320 |
|
510 | 510 | |
|
511 | 511 | For simple cases, you can alternatively prepend $ to a variable name:: |
|
512 | 512 | |
|
513 | 513 | In [6]: !echo $sys.argv |
|
514 | 514 | [/home/fperez/usr/bin/ipython] |
|
515 | 515 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ |
|
516 | 516 | A system variable: /home/fperez |
|
517 | 517 | |
|
518 | 518 | System command aliases |
|
519 | 519 | ---------------------- |
|
520 | 520 | |
|
521 | 521 | The %alias magic function allows you to define magic functions which are in fact |
|
522 | 522 | system shell commands. These aliases can have parameters. |
|
523 | 523 | |
|
524 | 524 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' |
|
525 | 525 | |
|
526 | 526 | Then, typing ``alias_name params`` will execute the system command 'cmd |
|
527 | 527 | params' (from your underlying operating system). |
|
528 | 528 | |
|
529 | 529 | You can also define aliases with parameters using %s specifiers (one per |
|
530 | 530 | parameter). The following example defines the parts function as an |
|
531 | 531 | alias to the command 'echo first %s second %s' where each %s will be |
|
532 | 532 | replaced by a positional parameter to the call to %parts:: |
|
533 | 533 | |
|
534 | 534 | In [1]: %alias parts echo first %s second %s |
|
535 | 535 | In [2]: parts A B |
|
536 | 536 | first A second B |
|
537 | 537 | In [3]: parts A |
|
538 | 538 | ERROR: Alias <parts> requires 2 arguments, 1 given. |
|
539 | 539 | |
|
540 | 540 | If called with no parameters, %alias prints the table of currently |
|
541 | 541 | defined aliases. |
|
542 | 542 | |
|
543 | 543 | The %rehashx magic allows you to load your entire $PATH as |
|
544 | 544 | ipython aliases. See its docstring for further details. |
|
545 | 545 | |
|
546 | 546 | |
|
547 | 547 | .. _dreload: |
|
548 | 548 | |
|
549 | 549 | Recursive reload |
|
550 | 550 | ---------------- |
|
551 | 551 | |
|
552 | 552 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a |
|
553 | 553 | module: changes made to any of its dependencies will be reloaded without |
|
554 | 554 | having to exit. To start using it, do:: |
|
555 | 555 | |
|
556 | 556 | from IPython.lib.deepreload import reload as dreload |
|
557 | 557 | |
|
558 | 558 | |
|
559 | 559 | Verbose and colored exception traceback printouts |
|
560 | 560 | ------------------------------------------------- |
|
561 | 561 | |
|
562 | 562 | IPython provides the option to see very detailed exception tracebacks, |
|
563 | 563 | which can be especially useful when debugging large programs. You can |
|
564 | 564 | run any Python file with the %run function to benefit from these |
|
565 | 565 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
566 | 566 | be colored (if your terminal supports it) which makes them much easier |
|
567 | 567 | to parse visually. |
|
568 | 568 | |
|
569 | 569 | See the magic xmode and colors functions for details. |
|
570 | 570 | |
|
571 | 571 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
572 | 572 | module, now part of the standard Python library. |
|
573 | 573 | |
|
574 | 574 | |
|
575 | 575 | .. _input_caching: |
|
576 | 576 | |
|
577 | 577 | Input caching system |
|
578 | 578 | -------------------- |
|
579 | 579 | |
|
580 | 580 | IPython offers numbered prompts (In/Out) with input and output caching |
|
581 | 581 | (also referred to as 'input history'). All input is saved and can be |
|
582 | 582 | retrieved as variables (besides the usual arrow key recall), in |
|
583 | 583 | addition to the %rep magic command that brings a history entry |
|
584 | 584 | up for editing on the next command line. |
|
585 | 585 | |
|
586 | 586 | The following variables always exist: |
|
587 | 587 | |
|
588 | 588 | * _i, _ii, _iii: store previous, next previous and next-next previous inputs. |
|
589 | 589 | * In, _ih : a list of all inputs; _ih[n] is the input from line n. If you |
|
590 | 590 | overwrite In with a variable of your own, you can remake the assignment to the |
|
591 | 591 | internal list with a simple ``In=_ih``. |
|
592 | 592 | |
|
593 | 593 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
594 | 594 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. |
|
595 | 595 | |
|
596 | 596 | For example, what you typed at prompt 14 is available as ``_i14``, ``_ih[14]`` |
|
597 | 597 | and ``In[14]``. |
|
598 | 598 | |
|
599 | 599 | This allows you to easily cut and paste multi line interactive prompts |
|
600 | 600 | by printing them out: they print like a clean string, without prompt |
|
601 | 601 | characters. You can also manipulate them like regular variables (they |
|
602 | 602 | are strings), modify or exec them. |
|
603 | 603 | |
|
604 | 604 | You can also re-execute multiple lines of input easily by using the |
|
605 | 605 | magic %rerun or %macro functions. The macro system also allows you to re-execute |
|
606 | 606 | previous lines which include magic function calls (which require special |
|
607 | 607 | processing). Type %macro? for more details on the macro system. |
|
608 | 608 | |
|
609 | 609 | A history function %hist allows you to see any part of your input |
|
610 | 610 | history by printing a range of the _i variables. |
|
611 | 611 | |
|
612 | 612 | You can also search ('grep') through your history by typing |
|
613 | 613 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, |
|
614 | 614 | etc. You can bring history entries listed by '%hist -g' up for editing |
|
615 | 615 | with the %recall command, or run them immediately with %rerun. |
|
616 | 616 | |
|
617 | 617 | .. _output_caching: |
|
618 | 618 | |
|
619 | 619 | Output caching system |
|
620 | 620 | --------------------- |
|
621 | 621 | |
|
622 | 622 | For output that is returned from actions, a system similar to the input |
|
623 | 623 | cache exists but using _ instead of _i. Only actions that produce a |
|
624 | 624 | result (NOT assignments, for example) are cached. If you are familiar |
|
625 | 625 | with Mathematica, IPython's _ variables behave exactly like |
|
626 | 626 | Mathematica's % variables. |
|
627 | 627 | |
|
628 | 628 | The following variables always exist: |
|
629 | 629 | |
|
630 | 630 | * [_] (a single underscore): stores previous output, like Python's |
|
631 | 631 | default interpreter. |
|
632 | 632 | * [__] (two underscores): next previous. |
|
633 | 633 | * [___] (three underscores): next-next previous. |
|
634 | 634 | |
|
635 | 635 | Additionally, global variables named _<n> are dynamically created (<n> |
|
636 | 636 | being the prompt counter), such that the result of output <n> is always |
|
637 | 637 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
638 | 638 | ``_21``). |
|
639 | 639 | |
|
640 | 640 | These variables are also stored in a global dictionary (not a |
|
641 | 641 | list, since it only has entries for lines which returned a result) |
|
642 | 642 | available under the names _oh and Out (similar to _ih and In). So the |
|
643 | 643 | output from line 12 can be obtained as ``_12``, ``Out[12]`` or ``_oh[12]``. If you |
|
644 | 644 | accidentally overwrite the Out variable you can recover it by typing |
|
645 | 645 | ``Out=_oh`` at the prompt. |
|
646 | 646 | |
|
647 | 647 | This system obviously can potentially put heavy memory demands on your |
|
648 | 648 | system, since it prevents Python's garbage collector from removing any |
|
649 | 649 | previously computed results. You can control how many results are kept |
|
650 | 650 | in memory with the configuration option ``InteractiveShell.cache_size``. |
|
651 | 651 | If you set it to 0, output caching is disabled. You can also use the ``%reset`` |
|
652 | 652 | and ``%xdel`` magics to clear large items from memory. |
|
653 | 653 | |
|
654 | 654 | Directory history |
|
655 | 655 | ----------------- |
|
656 | 656 | |
|
657 | 657 | Your history of visited directories is kept in the global list _dh, and |
|
658 | 658 | the magic %cd command can be used to go to any entry in that list. The |
|
659 | 659 | %dhist command allows you to view this history. Do ``cd -<TAB>`` to |
|
660 | 660 | conveniently view the directory history. |
|
661 | 661 | |
|
662 | 662 | |
|
663 | 663 | Automatic parentheses and quotes |
|
664 | 664 | -------------------------------- |
|
665 | 665 | |
|
666 | 666 | These features were adapted from Nathan Gray's LazyPython. They are |
|
667 | 667 | meant to allow less typing for common situations. |
|
668 | 668 | |
|
669 | 669 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
670 | 670 | (notice the commas between the arguments):: |
|
671 | 671 | |
|
672 | 672 | In [1]: callable_ob arg1, arg2, arg3 |
|
673 | 673 | ------> callable_ob(arg1, arg2, arg3) |
|
674 | 674 | |
|
675 | 675 | .. note:: |
|
676 | 676 | This feature is disabled by default. To enable it, use the ``%autocall`` |
|
677 | 677 | magic command. The commands below with special prefixes will always work, |
|
678 | 678 | however. |
|
679 | 679 | |
|
680 | 680 | You can force automatic parentheses by using '/' as the first character |
|
681 | 681 | of a line. For example:: |
|
682 | 682 | |
|
683 | 683 | In [2]: /globals # becomes 'globals()' |
|
684 | 684 | |
|
685 | 685 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
686 | 686 | |
|
687 | 687 | In [3]: print /globals # syntax error |
|
688 | 688 | |
|
689 | 689 | In most cases the automatic algorithm should work, so you should rarely |
|
690 | 690 | need to explicitly invoke /. One notable exception is if you are trying |
|
691 | 691 | to call a function with a list of tuples as arguments (the parenthesis |
|
692 | 692 | will confuse IPython):: |
|
693 | 693 | |
|
694 | 694 | In [4]: zip (1,2,3),(4,5,6) # won't work |
|
695 | 695 | |
|
696 | 696 | but this will work:: |
|
697 | 697 | |
|
698 | 698 | In [5]: /zip (1,2,3),(4,5,6) |
|
699 | 699 | ------> zip ((1,2,3),(4,5,6)) |
|
700 | 700 | Out[5]: [(1, 4), (2, 5), (3, 6)] |
|
701 | 701 | |
|
702 | 702 | IPython tells you that it has altered your command line by displaying |
|
703 | 703 | the new command line preceded by ``--->``. |
|
704 | 704 | |
|
705 | 705 | You can force automatic quoting of a function's arguments by using ``,`` |
|
706 | 706 | or ``;`` as the first character of a line. For example:: |
|
707 | 707 | |
|
708 | 708 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
709 | 709 | |
|
710 | 710 | If you use ';' the whole argument is quoted as a single string, while ',' splits |
|
711 | 711 | on whitespace:: |
|
712 | 712 | |
|
713 | 713 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
714 | 714 | |
|
715 | 715 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
716 | 716 | |
|
717 | 717 | Note that the ',' or ';' MUST be the first character on the line! This |
|
718 | 718 | won't work:: |
|
719 | 719 | |
|
720 | 720 | In [4]: x = ,my_function /home/me # syntax error |
|
721 | 721 | |
|
722 | 722 | IPython as your default Python environment |
|
723 | 723 | ========================================== |
|
724 | 724 | |
|
725 | 725 | Python honors the environment variable :envvar:`PYTHONSTARTUP` and will |
|
726 | 726 | execute at startup the file referenced by this variable. If you put the |
|
727 | 727 | following code at the end of that file, then IPython will be your working |
|
728 | 728 | environment anytime you start Python:: |
|
729 | 729 | |
|
730 | 730 | import os, IPython |
|
731 | 731 | os.environ['PYTHONSTARTUP'] = '' # Prevent running this again |
|
732 | 732 | IPython.start_ipython() |
|
733 | 733 | raise SystemExit |
|
734 | 734 | |
|
735 | 735 | The ``raise SystemExit`` is needed to exit Python when |
|
736 | 736 | it finishes, otherwise you'll be back at the normal Python ``>>>`` |
|
737 | 737 | prompt. |
|
738 | 738 | |
|
739 | 739 | This is probably useful to developers who manage multiple Python |
|
740 | 740 | versions and don't want to have correspondingly multiple IPython |
|
741 | 741 | versions. Note that in this mode, there is no way to pass IPython any |
|
742 | 742 | command-line options, as those are trapped first by Python itself. |
|
743 | 743 | |
|
744 | 744 | .. _Embedding: |
|
745 | 745 | |
|
746 | 746 | Embedding IPython |
|
747 | 747 | ================= |
|
748 | 748 | |
|
749 | 749 | You can start a regular IPython session with |
|
750 | 750 | |
|
751 | 751 | .. sourcecode:: python |
|
752 | 752 | |
|
753 | 753 | import IPython |
|
754 | 754 | IPython.start_ipython(argv=[]) |
|
755 | 755 | |
|
756 | 756 | at any point in your program. This will load IPython configuration, |
|
757 | 757 | startup files, and everything, just as if it were a normal IPython session. |
|
758 | 758 | |
|
759 | 759 | It is also possible to embed an IPython shell in a namespace in your Python code. |
|
760 | 760 | This allows you to evaluate dynamically the state of your code, |
|
761 | 761 | operate with your variables, analyze them, etc. Note however that |
|
762 | 762 | any changes you make to values while in the shell do not propagate back |
|
763 | 763 | to the running code, so it is safe to modify your values because you |
|
764 | 764 | won't break your code in bizarre ways by doing so. |
|
765 | 765 | |
|
766 | 766 | .. note:: |
|
767 | 767 | |
|
768 | 768 | At present, embedding IPython cannot be done from inside IPython. |
|
769 | 769 | Run the code samples below outside IPython. |
|
770 | 770 | |
|
771 | 771 | This feature allows you to easily have a fully functional python |
|
772 | 772 | environment for doing object introspection anywhere in your code with a |
|
773 | 773 | simple function call. In some cases a simple print statement is enough, |
|
774 | 774 | but if you need to do more detailed analysis of a code fragment this |
|
775 | 775 | feature can be very valuable. |
|
776 | 776 | |
|
777 | 777 | It can also be useful in scientific computing situations where it is |
|
778 | 778 | common to need to do some automatic, computationally intensive part and |
|
779 | 779 | then stop to look at data, plots, etc. |
|
780 | 780 | Opening an IPython instance will give you full access to your data and |
|
781 | 781 | functions, and you can resume program execution once you are done with |
|
782 | 782 | the interactive part (perhaps to stop again later, as many times as |
|
783 | 783 | needed). |
|
784 | 784 | |
|
785 | 785 | The following code snippet is the bare minimum you need to include in |
|
786 | 786 | your Python programs for this to work (detailed examples follow later):: |
|
787 | 787 | |
|
788 | 788 | from IPython import embed |
|
789 | 789 | |
|
790 | 790 | embed() # this call anywhere in your program will start IPython |
|
791 | 791 | |
|
792 | 792 | You can also embed an IPython *kernel*, for use with qtconsole, etc. via |
|
793 | 793 | ``IPython.embed_kernel()``. This should function work the same way, but you can |
|
794 | 794 | connect an external frontend (``ipython qtconsole`` or ``ipython console``), |
|
795 | 795 | rather than interacting with it in the terminal. |
|
796 | 796 | |
|
797 | 797 | You can run embedded instances even in code which is itself being run at |
|
798 | 798 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
799 | 799 | to get lost as to where you are (in your top-level IPython or in your |
|
800 | 800 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
801 | 801 | to something different for the embedded instances. The code examples |
|
802 | 802 | below illustrate this. |
|
803 | 803 | |
|
804 | 804 | You can also have multiple IPython instances in your program and open |
|
805 | 805 | them separately, for example with different options for data |
|
806 | 806 | presentation. If you close and open the same instance multiple times, |
|
807 | 807 | its prompt counters simply continue from each execution to the next. |
|
808 | 808 | |
|
809 | 809 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` |
|
810 | 810 | module for more details on the use of this system. |
|
811 | 811 | |
|
812 | 812 | The following sample file illustrating how to use the embedding |
|
813 |
functionality is provided in the examples directory as e |
|
|
813 | functionality is provided in the examples directory as embed_class_long.py. | |
|
814 | 814 | It should be fairly self-explanatory: |
|
815 | 815 | |
|
816 |
.. literalinclude:: ../../../examples/ |
|
|
816 | .. literalinclude:: ../../../examples/Embedding/embed_class_long.py | |
|
817 | 817 | :language: python |
|
818 | 818 | |
|
819 | 819 | Once you understand how the system functions, you can use the following |
|
820 | 820 | code fragments in your programs which are ready for cut and paste: |
|
821 | 821 | |
|
822 |
.. literalinclude:: ../../../examples/ |
|
|
822 | .. literalinclude:: ../../../examples/Embedding/embed_class_short.py | |
|
823 | 823 | :language: python |
|
824 | 824 | |
|
825 | 825 | Using the Python debugger (pdb) |
|
826 | 826 | =============================== |
|
827 | 827 | |
|
828 | 828 | Running entire programs via pdb |
|
829 | 829 | ------------------------------- |
|
830 | 830 | |
|
831 | 831 | pdb, the Python debugger, is a powerful interactive debugger which |
|
832 | 832 | allows you to step through code, set breakpoints, watch variables, |
|
833 | 833 | etc. IPython makes it very easy to start any script under the control |
|
834 | 834 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
835 | 835 | function or not. For this, simply type ``%run -d myscript`` at an |
|
836 | 836 | IPython prompt. See the %run command's documentation for more details, including |
|
837 | 837 | how to control where pdb will stop execution first. |
|
838 | 838 | |
|
839 | 839 | For more information on the use of the pdb debugger, see :ref:`debugger-commands` |
|
840 | 840 | in the Python documentation. |
|
841 | 841 | |
|
842 | 842 | |
|
843 | 843 | Post-mortem debugging |
|
844 | 844 | --------------------- |
|
845 | 845 | |
|
846 | 846 | Going into a debugger when an exception occurs can be |
|
847 | 847 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
848 | 848 | opens up at the point in your code which triggered the exception, and |
|
849 | 849 | while your program is at this point 'dead', all the data is still |
|
850 | 850 | available and you can walk up and down the stack frame and understand |
|
851 | 851 | the origin of the problem. |
|
852 | 852 | |
|
853 | 853 | You can use the ``%debug`` magic after an exception has occurred to start |
|
854 | 854 | post-mortem debugging. IPython can also call debugger every time your code |
|
855 | 855 | triggers an uncaught exception. This feature can be toggled with the %pdb magic |
|
856 | 856 | command, or you can start IPython with the ``--pdb`` option. |
|
857 | 857 | |
|
858 | 858 | For a post-mortem debugger in your programs outside IPython, |
|
859 | 859 | put the following lines toward the top of your 'main' routine:: |
|
860 | 860 | |
|
861 | 861 | import sys |
|
862 | 862 | from IPython.core import ultratb |
|
863 | 863 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
864 | 864 | color_scheme='Linux', call_pdb=1) |
|
865 | 865 | |
|
866 | 866 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
867 | 867 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
868 | 868 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
869 | 869 | options which can be set in IPython with ``--colors`` and ``--xmode``. |
|
870 | 870 | |
|
871 | 871 | This will give any of your programs detailed, colored tracebacks with |
|
872 | 872 | automatic invocation of pdb. |
|
873 | 873 | |
|
874 | 874 | .. _pasting_with_prompts: |
|
875 | 875 | |
|
876 | 876 | Pasting of code starting with Python or IPython prompts |
|
877 | 877 | ======================================================= |
|
878 | 878 | |
|
879 | 879 | IPython is smart enough to filter out input prompts, be they plain Python ones |
|
880 | 880 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and ``...:``). You can |
|
881 | 881 | therefore copy and paste from existing interactive sessions without worry. |
|
882 | 882 | |
|
883 | 883 | The following is a 'screenshot' of how things work, copying an example from the |
|
884 | 884 | standard Python tutorial:: |
|
885 | 885 | |
|
886 | 886 | In [1]: >>> # Fibonacci series: |
|
887 | 887 | |
|
888 | 888 | In [2]: ... # the sum of two elements defines the next |
|
889 | 889 | |
|
890 | 890 | In [3]: ... a, b = 0, 1 |
|
891 | 891 | |
|
892 | 892 | In [4]: >>> while b < 10: |
|
893 | 893 | ...: ... print(b) |
|
894 | 894 | ...: ... a, b = b, a+b |
|
895 | 895 | ...: |
|
896 | 896 | 1 |
|
897 | 897 | 1 |
|
898 | 898 | 2 |
|
899 | 899 | 3 |
|
900 | 900 | 5 |
|
901 | 901 | 8 |
|
902 | 902 | |
|
903 | 903 | And pasting from IPython sessions works equally well:: |
|
904 | 904 | |
|
905 | 905 | In [1]: In [5]: def f(x): |
|
906 | 906 | ...: ...: "A simple function" |
|
907 | 907 | ...: ...: return x**2 |
|
908 | 908 | ...: ...: |
|
909 | 909 | |
|
910 | 910 | In [2]: f(3) |
|
911 | 911 | Out[2]: 9 |
|
912 | 912 | |
|
913 | 913 | .. _gui_support: |
|
914 | 914 | |
|
915 | 915 | GUI event loop support |
|
916 | 916 | ====================== |
|
917 | 917 | |
|
918 | 918 | .. versionadded:: 0.11 |
|
919 | 919 | The ``%gui`` magic and :mod:`IPython.lib.inputhook`. |
|
920 | 920 | |
|
921 | 921 | IPython has excellent support for working interactively with Graphical User |
|
922 | 922 | Interface (GUI) toolkits, such as wxPython, PyQt4/PySide, PyGTK and Tk. This is |
|
923 | 923 | implemented using Python's builtin ``PyOSInputHook`` hook. This implementation |
|
924 | 924 | is extremely robust compared to our previous thread-based version. The |
|
925 | 925 | advantages of this are: |
|
926 | 926 | |
|
927 | 927 | * GUIs can be enabled and disabled dynamically at runtime. |
|
928 | 928 | * The active GUI can be switched dynamically at runtime. |
|
929 | 929 | * In some cases, multiple GUIs can run simultaneously with no problems. |
|
930 | 930 | * There is a developer API in :mod:`IPython.lib.inputhook` for customizing |
|
931 | 931 | all of these things. |
|
932 | 932 | |
|
933 | 933 | For users, enabling GUI event loop integration is simple. You simple use the |
|
934 | 934 | ``%gui`` magic as follows:: |
|
935 | 935 | |
|
936 | 936 | %gui [GUINAME] |
|
937 | 937 | |
|
938 | 938 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
939 | 939 | arguments are ``wx``, ``qt``, ``gtk`` and ``tk``. |
|
940 | 940 | |
|
941 | 941 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
942 | 942 | object, do:: |
|
943 | 943 | |
|
944 | 944 | %gui wx |
|
945 | 945 | |
|
946 | 946 | You can also start IPython with an event loop set up using the :option:`--gui` |
|
947 | 947 | flag:: |
|
948 | 948 | |
|
949 | 949 | $ ipython --gui=qt |
|
950 | 950 | |
|
951 | 951 | For information on IPython's matplotlib_ integration (and the ``matplotlib`` |
|
952 | 952 | mode) see :ref:`this section <matplotlib_support>`. |
|
953 | 953 | |
|
954 | 954 | For developers that want to use IPython's GUI event loop integration in the |
|
955 | 955 | form of a library, these capabilities are exposed in library form in the |
|
956 | 956 | :mod:`IPython.lib.inputhook` and :mod:`IPython.lib.guisupport` modules. |
|
957 | 957 | Interested developers should see the module docstrings for more information, |
|
958 | 958 | but there are a few points that should be mentioned here. |
|
959 | 959 | |
|
960 | 960 | First, the ``PyOSInputHook`` approach only works in command line settings |
|
961 | 961 | where readline is activated. The integration with various eventloops |
|
962 | 962 | is handled somewhat differently (and more simply) when using the standalone |
|
963 | 963 | kernel, as in the qtconsole and notebook. |
|
964 | 964 | |
|
965 | 965 | Second, when using the ``PyOSInputHook`` approach, a GUI application should |
|
966 | 966 | *not* start its event loop. Instead all of this is handled by the |
|
967 | 967 | ``PyOSInputHook``. This means that applications that are meant to be used both |
|
968 | 968 | in IPython and as standalone apps need to have special code to detects how the |
|
969 | 969 | application is being run. We highly recommend using IPython's support for this. |
|
970 | 970 | Since the details vary slightly between toolkits, we point you to the various |
|
971 | 971 | examples in our source directory :file:`examples/lib` that demonstrate |
|
972 | 972 | these capabilities. |
|
973 | 973 | |
|
974 | 974 | Third, unlike previous versions of IPython, we no longer "hijack" (replace |
|
975 | 975 | them with no-ops) the event loops. This is done to allow applications that |
|
976 | 976 | actually need to run the real event loops to do so. This is often needed to |
|
977 | 977 | process pending events at critical points. |
|
978 | 978 | |
|
979 | 979 | Finally, we also have a number of examples in our source directory |
|
980 | 980 | :file:`examples/lib` that demonstrate these capabilities. |
|
981 | 981 | |
|
982 | 982 | PyQt and PySide |
|
983 | 983 | --------------- |
|
984 | 984 | |
|
985 | 985 | .. attempt at explanation of the complete mess that is Qt support |
|
986 | 986 | |
|
987 | 987 | When you use ``--gui=qt`` or ``--matplotlib=qt``, IPython can work with either |
|
988 | 988 | PyQt4 or PySide. There are three options for configuration here, because |
|
989 | 989 | PyQt4 has two APIs for QString and QVariant - v1, which is the default on |
|
990 | 990 | Python 2, and the more natural v2, which is the only API supported by PySide. |
|
991 | 991 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole |
|
992 | 992 | uses v2, but you can still use any interface in your code, since the |
|
993 | 993 | Qt frontend is in a different process. |
|
994 | 994 | |
|
995 | 995 | The default will be to import PyQt4 without configuration of the APIs, thus |
|
996 | 996 | matching what most applications would expect. It will fall back of PySide if |
|
997 | 997 | PyQt4 is unavailable. |
|
998 | 998 | |
|
999 | 999 | If specified, IPython will respect the environment variable ``QT_API`` used |
|
1000 | 1000 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires |
|
1001 | 1001 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, |
|
1002 | 1002 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for |
|
1003 | 1003 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. |
|
1004 | 1004 | |
|
1005 | 1005 | If you launch IPython in matplotlib mode with ``ipython --matplotlib=qt``, |
|
1006 | 1006 | then IPython will ask matplotlib which Qt library to use (only if QT_API is |
|
1007 | 1007 | *not set*), via the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or |
|
1008 | 1008 | older, then IPython will always use PyQt4 without setting the v2 APIs, since |
|
1009 | 1009 | neither v2 PyQt nor PySide work. |
|
1010 | 1010 | |
|
1011 | 1011 | .. warning:: |
|
1012 | 1012 | |
|
1013 | 1013 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set |
|
1014 | 1014 | to work with IPython's qt integration, because otherwise PyQt4 will be |
|
1015 | 1015 | loaded in an incompatible mode. |
|
1016 | 1016 | |
|
1017 | 1017 | It also means that you must *not* have ``QT_API`` set if you want to |
|
1018 | 1018 | use ``--gui=qt`` with code that requires PyQt4 API v1. |
|
1019 | 1019 | |
|
1020 | 1020 | |
|
1021 | 1021 | .. _matplotlib_support: |
|
1022 | 1022 | |
|
1023 | 1023 | Plotting with matplotlib |
|
1024 | 1024 | ======================== |
|
1025 | 1025 | |
|
1026 | 1026 | matplotlib_ provides high quality 2D and 3D plotting for Python. matplotlib_ |
|
1027 | 1027 | can produce plots on screen using a variety of GUI toolkits, including Tk, |
|
1028 | 1028 | PyGTK, PyQt4 and wxPython. It also provides a number of commands useful for |
|
1029 | 1029 | scientific computing, all with a syntax compatible with that of the popular |
|
1030 | 1030 | Matlab program. |
|
1031 | 1031 | |
|
1032 | 1032 | To start IPython with matplotlib support, use the ``--matplotlib`` switch. If |
|
1033 | 1033 | IPython is already running, you can run the ``%matplotlib`` magic. If no |
|
1034 | 1034 | arguments are given, IPython will automatically detect your choice of |
|
1035 | 1035 | matplotlib backend. You can also request a specific backend with |
|
1036 | 1036 | ``%matplotlib backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', |
|
1037 | 1037 | 'gtk', 'osx'. In the web notebook and Qt console, 'inline' is also a valid |
|
1038 | 1038 | backend value, which produces static figures inlined inside the application |
|
1039 | 1039 | window instead of matplotlib's interactive figures that live in separate |
|
1040 | 1040 | windows. |
|
1041 | 1041 | |
|
1042 | 1042 | .. _interactive_demos: |
|
1043 | 1043 | |
|
1044 | 1044 | Interactive demos with IPython |
|
1045 | 1045 | ============================== |
|
1046 | 1046 | |
|
1047 | 1047 | IPython ships with a basic system for running scripts interactively in |
|
1048 | 1048 | sections, useful when presenting code to audiences. A few tags embedded |
|
1049 | 1049 | in comments (so that the script remains valid Python code) divide a file |
|
1050 | 1050 | into separate blocks, and the demo can be run one block at a time, with |
|
1051 | 1051 | IPython printing (with syntax highlighting) the block before executing |
|
1052 | 1052 | it, and returning to the interactive prompt after each block. The |
|
1053 | 1053 | interactive namespace is updated after each block is run with the |
|
1054 | 1054 | contents of the demo's namespace. |
|
1055 | 1055 | |
|
1056 | 1056 | This allows you to show a piece of code, run it and then execute |
|
1057 | 1057 | interactively commands based on the variables just created. Once you |
|
1058 | 1058 | want to continue, you simply execute the next block of the demo. The |
|
1059 | 1059 | following listing shows the markup necessary for dividing a script into |
|
1060 | 1060 | sections for execution as a demo: |
|
1061 | 1061 | |
|
1062 |
.. literalinclude:: ../../../examples/l |
|
|
1062 | .. literalinclude:: ../../../examples/IPython Kernel/example-demo.py | |
|
1063 | 1063 | :language: python |
|
1064 | 1064 | |
|
1065 | 1065 | In order to run a file as a demo, you must first make a Demo object out |
|
1066 | 1066 | of it. If the file is named myscript.py, the following code will make a |
|
1067 | 1067 | demo:: |
|
1068 | 1068 | |
|
1069 | 1069 | from IPython.lib.demo import Demo |
|
1070 | 1070 | |
|
1071 | 1071 | mydemo = Demo('myscript.py') |
|
1072 | 1072 | |
|
1073 | 1073 | This creates the mydemo object, whose blocks you run one at a time by |
|
1074 | 1074 | simply calling the object with no arguments. Then call it to run each step |
|
1075 | 1075 | of the demo:: |
|
1076 | 1076 | |
|
1077 | 1077 | mydemo() |
|
1078 | 1078 | |
|
1079 | 1079 | Demo objects can be |
|
1080 | 1080 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1081 | 1081 | last block, etc. See the :mod:`IPython.lib.demo` module and the |
|
1082 | 1082 | :class:`~IPython.lib.demo.Demo` class for details. |
|
1083 | 1083 | |
|
1084 | 1084 | Limitations: These demos are limited to |
|
1085 | 1085 | fairly simple uses. In particular, you cannot break up sections within |
|
1086 | 1086 | indented code (loops, if statements, function definitions, etc.) |
|
1087 | 1087 | Supporting something like this would basically require tracking the |
|
1088 | 1088 | internal execution state of the Python interpreter, so only top-level |
|
1089 | 1089 | divisions are allowed. If you want to be able to open an IPython |
|
1090 | 1090 | instance at an arbitrary point in a program, you can use IPython's |
|
1091 | 1091 | :ref:`embedding facilities <Embedding>`. |
|
1092 | 1092 | |
|
1093 | 1093 | .. include:: ../links.txt |
@@ -1,150 +1,150 b'' | |||
|
1 | 1 | .. _parallel_asyncresult: |
|
2 | 2 | |
|
3 | 3 | ====================== |
|
4 | 4 | The AsyncResult object |
|
5 | 5 | ====================== |
|
6 | 6 | |
|
7 | 7 | In non-blocking mode, :meth:`apply` submits the command to be executed and |
|
8 | 8 | then returns a :class:`~.AsyncResult` object immediately. The |
|
9 | 9 | AsyncResult object gives you a way of getting a result at a later |
|
10 | 10 | time through its :meth:`get` method, but it also collects metadata |
|
11 | 11 | on execution. |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | Beyond multiprocessing's AsyncResult |
|
15 | 15 | ==================================== |
|
16 | 16 | |
|
17 | 17 | .. Note:: |
|
18 | 18 | |
|
19 | 19 | The :class:`~.AsyncResult` object provides a superset of the interface in |
|
20 | 20 | :py:class:`multiprocessing.pool.AsyncResult`. See the |
|
21 | 21 | `official Python documentation <http://docs.python.org/library/multiprocessing#multiprocessing.pool.AsyncResult>`_ |
|
22 | 22 | for more on the basics of this interface. |
|
23 | 23 | |
|
24 | 24 | Our AsyncResult objects add a number of convenient features for working with |
|
25 | 25 | parallel results, beyond what is provided by the original AsyncResult. |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | get_dict |
|
29 | 29 | -------- |
|
30 | 30 | |
|
31 | 31 | First, is :meth:`.AsyncResult.get_dict`, which pulls results as a dictionary |
|
32 | 32 | keyed by engine_id, rather than a flat list. This is useful for quickly |
|
33 | 33 | coordinating or distributing information about all of the engines. |
|
34 | 34 | |
|
35 | 35 | As an example, here is a quick call that gives every engine a dict showing |
|
36 | 36 | the PID of every other engine: |
|
37 | 37 | |
|
38 | 38 | .. sourcecode:: ipython |
|
39 | 39 | |
|
40 | 40 | In [10]: ar = rc[:].apply_async(os.getpid) |
|
41 | 41 | In [11]: pids = ar.get_dict() |
|
42 | 42 | In [12]: rc[:]['pid_map'] = pids |
|
43 | 43 | |
|
44 | 44 | This trick is particularly useful when setting up inter-engine communication, |
|
45 | 45 | as in IPython's :file:`examples/parallel/interengine` examples. |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | Metadata |
|
49 | 49 | ======== |
|
50 | 50 | |
|
51 | 51 | IPython.parallel tracks some metadata about the tasks, which is stored |
|
52 | 52 | in the :attr:`.Client.metadata` dict. The AsyncResult object gives you an |
|
53 | 53 | interface for this information as well, including timestamps stdout/err, |
|
54 | 54 | and engine IDs. |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | Timing |
|
58 | 58 | ------ |
|
59 | 59 | |
|
60 | 60 | IPython tracks various timestamps as :py:class:`.datetime` objects, |
|
61 | 61 | and the AsyncResult object has a few properties that turn these into useful |
|
62 | 62 | times (in seconds as floats). |
|
63 | 63 | |
|
64 | 64 | For use while the tasks are still pending: |
|
65 | 65 | |
|
66 | 66 | * :attr:`ar.elapsed` is just the elapsed seconds since submission, for use |
|
67 | 67 | before the AsyncResult is complete. |
|
68 | 68 | * :attr:`ar.progress` is the number of tasks that have completed. Fractional progress |
|
69 | 69 | would be:: |
|
70 | 70 | |
|
71 | 71 | 1.0 * ar.progress / len(ar) |
|
72 | 72 | |
|
73 | 73 | * :meth:`AsyncResult.wait_interactive` will wait for the result to finish, but |
|
74 | 74 | print out status updates on progress and elapsed time while it waits. |
|
75 | 75 | |
|
76 | 76 | For use after the tasks are done: |
|
77 | 77 | |
|
78 | 78 | * :attr:`ar.serial_time` is the sum of the computation time of all of the tasks |
|
79 | 79 | done in parallel. |
|
80 | 80 | * :attr:`ar.wall_time` is the time between the first task submitted and last result |
|
81 | 81 | received. This is the actual cost of computation, including IPython overhead. |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | .. note:: |
|
85 | 85 | |
|
86 | 86 | wall_time is only precise if the Client is waiting for results when |
|
87 | 87 | the task finished, because the `received` timestamp is made when the result is |
|
88 | 88 | unpacked by the Client, triggered by the :meth:`~Client.spin` call. If you |
|
89 | 89 | are doing work in the Client, and not waiting/spinning, then `received` might |
|
90 | 90 | be artificially high. |
|
91 | 91 | |
|
92 | 92 | An often interesting metric is the time it actually cost to do the work in parallel |
|
93 | 93 | relative to the serial computation, and this can be given simply with |
|
94 | 94 | |
|
95 | 95 | .. sourcecode:: python |
|
96 | 96 | |
|
97 | 97 | speedup = ar.serial_time / ar.wall_time |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | Map results are iterable! |
|
101 | 101 | ========================= |
|
102 | 102 | |
|
103 | 103 | When an AsyncResult object has multiple results (e.g. the :class:`~AsyncMapResult` |
|
104 | 104 | object), you can actually iterate through results themselves, and act on them as they arrive: |
|
105 | 105 | |
|
106 |
.. literalinclude:: ../../../examples/ |
|
|
106 | .. literalinclude:: ../../../examples/Parallel Computing/itermapresult.py | |
|
107 | 107 | :language: python |
|
108 | 108 | :lines: 20-67 |
|
109 | 109 | |
|
110 | 110 | That is to say, if you treat an AsyncMapResult as if it were a list of your actual |
|
111 | 111 | results, it should behave as you would expect, with the only difference being |
|
112 | 112 | that you can start iterating through the results before they have even been computed. |
|
113 | 113 | |
|
114 | 114 | This lets you do a dumb version of map/reduce with the builtin Python functions, |
|
115 | 115 | and the only difference between doing this locally and doing it remotely in parallel |
|
116 | 116 | is using the asynchronous view.map instead of the builtin map. |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | Here is a simple one-line RMS (root-mean-square) implemented with Python's builtin map/reduce. |
|
120 | 120 | |
|
121 | 121 | .. sourcecode:: ipython |
|
122 | 122 | |
|
123 | 123 | In [38]: X = np.linspace(0,100) |
|
124 | 124 | |
|
125 | 125 | In [39]: from math import sqrt |
|
126 | 126 | |
|
127 | 127 | In [40]: add = lambda a,b: a+b |
|
128 | 128 | |
|
129 | 129 | In [41]: sq = lambda x: x*x |
|
130 | 130 | |
|
131 | 131 | In [42]: sqrt(reduce(add, map(sq, X)) / len(X)) |
|
132 | 132 | Out[42]: 58.028845747399714 |
|
133 | 133 | |
|
134 | 134 | In [43]: sqrt(reduce(add, view.map(sq, X)) / len(X)) |
|
135 | 135 | Out[43]: 58.028845747399714 |
|
136 | 136 | |
|
137 | 137 | To break that down: |
|
138 | 138 | |
|
139 | 139 | 1. ``map(sq, X)`` Compute the square of each element in the list (locally, or in parallel) |
|
140 | 140 | 2. ``reduce(add, sqX) / len(X)`` compute the mean by summing over the list (or AsyncMapResult) |
|
141 | 141 | and dividing by the size |
|
142 | 142 | 3. take the square root of the resulting number |
|
143 | 143 | |
|
144 | 144 | .. seealso:: |
|
145 | 145 | |
|
146 | 146 | When AsyncResult or the AsyncMapResult don't provide what you need (for instance, |
|
147 | 147 | handling individual results as they arrive, but with metadata), you can always |
|
148 | 148 | just split the original result's ``msg_ids`` attribute, and handle them as you like. |
|
149 | 149 | |
|
150 | 150 | For an example of this, see :file:`examples/parallel/customresult.py` |
@@ -1,177 +1,177 b'' | |||
|
1 | 1 | .. _dag_dependencies: |
|
2 | 2 | |
|
3 | 3 | ================ |
|
4 | 4 | DAG Dependencies |
|
5 | 5 | ================ |
|
6 | 6 | |
|
7 | 7 | Often, parallel workflow is described in terms of a `Directed Acyclic Graph |
|
8 | 8 | <http://en.wikipedia.org/wiki/Directed_acyclic_graph>`_ or DAG. A popular library |
|
9 | 9 | for working with Graphs is NetworkX_. Here, we will walk through a demo mapping |
|
10 | 10 | a nx DAG to task dependencies. |
|
11 | 11 | |
|
12 | 12 | The full script that runs this demo can be found in |
|
13 | 13 | :file:`examples/parallel/dagdeps.py`. |
|
14 | 14 | |
|
15 | 15 | Why are DAGs good for task dependencies? |
|
16 | 16 | ---------------------------------------- |
|
17 | 17 | |
|
18 | 18 | The 'G' in DAG is 'Graph'. A Graph is a collection of **nodes** and **edges** that connect |
|
19 | 19 | the nodes. For our purposes, each node would be a task, and each edge would be a |
|
20 | 20 | dependency. The 'D' in DAG stands for 'Directed'. This means that each edge has a |
|
21 | 21 | direction associated with it. So we can interpret the edge (a,b) as meaning that b depends |
|
22 | 22 | on a, whereas the edge (b,a) would mean a depends on b. The 'A' is 'Acyclic', meaning that |
|
23 | 23 | there must not be any closed loops in the graph. This is important for dependencies, |
|
24 | 24 | because if a loop were closed, then a task could ultimately depend on itself, and never be |
|
25 | 25 | able to run. If your workflow can be described as a DAG, then it is impossible for your |
|
26 | 26 | dependencies to cause a deadlock. |
|
27 | 27 | |
|
28 | 28 | A Sample DAG |
|
29 | 29 | ------------ |
|
30 | 30 | |
|
31 | 31 | Here, we have a very simple 5-node DAG: |
|
32 | 32 | |
|
33 | 33 | .. figure:: figs/simpledag.* |
|
34 | 34 | :width: 600px |
|
35 | 35 | |
|
36 | 36 | With NetworkX, an arrow is just a fattened bit on the edge. Here, we can see that task 0 |
|
37 | 37 | depends on nothing, and can run immediately. 1 and 2 depend on 0; 3 depends on |
|
38 | 38 | 1 and 2; and 4 depends only on 1. |
|
39 | 39 | |
|
40 | 40 | A possible sequence of events for this workflow: |
|
41 | 41 | |
|
42 | 42 | 0. Task 0 can run right away |
|
43 | 43 | 1. 0 finishes, so 1,2 can start |
|
44 | 44 | 2. 1 finishes, 3 is still waiting on 2, but 4 can start right away |
|
45 | 45 | 3. 2 finishes, and 3 can finally start |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | Further, taking failures into account, assuming all dependencies are run with the default |
|
49 | 49 | `success=True,failure=False`, the following cases would occur for each node's failure: |
|
50 | 50 | |
|
51 | 51 | 0. fails: all other tasks fail as Impossible |
|
52 | 52 | 1. 2 can still succeed, but 3,4 are unreachable |
|
53 | 53 | 2. 3 becomes unreachable, but 4 is unaffected |
|
54 | 54 | 3. and 4. are terminal, and can have no effect on other nodes |
|
55 | 55 | |
|
56 | 56 | The code to generate the simple DAG: |
|
57 | 57 | |
|
58 | 58 | .. sourcecode:: python |
|
59 | 59 | |
|
60 | 60 | import networkx as nx |
|
61 | 61 | |
|
62 | 62 | G = nx.DiGraph() |
|
63 | 63 | |
|
64 | 64 | # add 5 nodes, labeled 0-4: |
|
65 | 65 | map(G.add_node, range(5)) |
|
66 | 66 | # 1,2 depend on 0: |
|
67 | 67 | G.add_edge(0,1) |
|
68 | 68 | G.add_edge(0,2) |
|
69 | 69 | # 3 depends on 1,2 |
|
70 | 70 | G.add_edge(1,3) |
|
71 | 71 | G.add_edge(2,3) |
|
72 | 72 | # 4 depends on 1 |
|
73 | 73 | G.add_edge(1,4) |
|
74 | 74 | |
|
75 | 75 | # now draw the graph: |
|
76 | 76 | pos = { 0 : (0,0), 1 : (1,1), 2 : (-1,1), |
|
77 | 77 | 3 : (0,2), 4 : (2,2)} |
|
78 | 78 | nx.draw(G, pos, edge_color='r') |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | For demonstration purposes, we have a function that generates a random DAG with a given |
|
82 | 82 | number of nodes and edges. |
|
83 | 83 | |
|
84 |
.. literalinclude:: ../../../examples/ |
|
|
84 | .. literalinclude:: ../../../examples/Parallel Computing/dagdeps.py | |
|
85 | 85 | :language: python |
|
86 | 86 | :lines: 20-36 |
|
87 | 87 | |
|
88 | 88 | So first, we start with a graph of 32 nodes, with 128 edges: |
|
89 | 89 | |
|
90 | 90 | .. sourcecode:: ipython |
|
91 | 91 | |
|
92 | 92 | In [2]: G = random_dag(32,128) |
|
93 | 93 | |
|
94 | 94 | Now, we need to build our dict of jobs corresponding to the nodes on the graph: |
|
95 | 95 | |
|
96 | 96 | .. sourcecode:: ipython |
|
97 | 97 | |
|
98 | 98 | In [3]: jobs = {} |
|
99 | 99 | |
|
100 | 100 | # in reality, each job would presumably be different |
|
101 | 101 | # randomwait is just a function that sleeps for a random interval |
|
102 | 102 | In [4]: for node in G: |
|
103 | 103 | ...: jobs[node] = randomwait |
|
104 | 104 | |
|
105 | 105 | Once we have a dict of jobs matching the nodes on the graph, we can start submitting jobs, |
|
106 | 106 | and linking up the dependencies. Since we don't know a job's msg_id until it is submitted, |
|
107 | 107 | which is necessary for building dependencies, it is critical that we don't submit any jobs |
|
108 | 108 | before other jobs it may depend on. Fortunately, NetworkX provides a |
|
109 | 109 | :meth:`topological_sort` method which ensures exactly this. It presents an iterable, that |
|
110 | 110 | guarantees that when you arrive at a node, you have already visited all the nodes it |
|
111 | 111 | on which it depends: |
|
112 | 112 | |
|
113 | 113 | .. sourcecode:: ipython |
|
114 | 114 | |
|
115 | 115 | In [5]: rc = Client() |
|
116 | 116 | In [5]: view = rc.load_balanced_view() |
|
117 | 117 | |
|
118 | 118 | In [6]: results = {} |
|
119 | 119 | |
|
120 | 120 | In [7]: for node in nx.topological_sort(G): |
|
121 | 121 | ...: # get list of AsyncResult objects from nodes |
|
122 | 122 | ...: # leading into this one as dependencies |
|
123 | 123 | ...: deps = [ results[n] for n in G.predecessors(node) ] |
|
124 | 124 | ...: # submit and store AsyncResult object |
|
125 | 125 | ...: with view.temp_flags(after=deps, block=False): |
|
126 | 126 | ...: results[node] = view.apply_with_flags(jobs[node]) |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | Now that we have submitted all the jobs, we can wait for the results: |
|
130 | 130 | |
|
131 | 131 | .. sourcecode:: ipython |
|
132 | 132 | |
|
133 | 133 | In [8]: view.wait(results.values()) |
|
134 | 134 | |
|
135 | 135 | Now, at least we know that all the jobs ran and did not fail (``r.get()`` would have |
|
136 | 136 | raised an error if a task failed). But we don't know that the ordering was properly |
|
137 | 137 | respected. For this, we can use the :attr:`metadata` attribute of each AsyncResult. |
|
138 | 138 | |
|
139 | 139 | These objects store a variety of metadata about each task, including various timestamps. |
|
140 | 140 | We can validate that the dependencies were respected by checking that each task was |
|
141 | 141 | started after all of its predecessors were completed: |
|
142 | 142 | |
|
143 |
.. literalinclude:: ../../../examples/ |
|
|
143 | .. literalinclude:: ../../../examples/Parallel Computing/dagdeps.py | |
|
144 | 144 | :language: python |
|
145 | 145 | :lines: 64-70 |
|
146 | 146 | |
|
147 | 147 | We can also validate the graph visually. By drawing the graph with each node's x-position |
|
148 | 148 | as its start time, all arrows must be pointing to the right if dependencies were respected. |
|
149 | 149 | For spreading, the y-position will be the runtime of the task, so long tasks |
|
150 | 150 | will be at the top, and quick, small tasks will be at the bottom. |
|
151 | 151 | |
|
152 | 152 | .. sourcecode:: ipython |
|
153 | 153 | |
|
154 | 154 | In [10]: from matplotlib.dates import date2num |
|
155 | 155 | |
|
156 | 156 | In [11]: from matplotlib.cm import gist_rainbow |
|
157 | 157 | |
|
158 | 158 | In [12]: pos = {}; colors = {} |
|
159 | 159 | |
|
160 | 160 | In [12]: for node in G: |
|
161 | 161 | ....: md = results[node].metadata |
|
162 | 162 | ....: start = date2num(md.started) |
|
163 | 163 | ....: runtime = date2num(md.completed) - start |
|
164 | 164 | ....: pos[node] = (start, runtime) |
|
165 | 165 | ....: colors[node] = md.engine_id |
|
166 | 166 | |
|
167 | 167 | In [13]: nx.draw(G, pos, node_list=colors.keys(), node_color=colors.values(), |
|
168 | 168 | ....: cmap=gist_rainbow) |
|
169 | 169 | |
|
170 | 170 | .. figure:: figs/dagdeps.* |
|
171 | 171 | :width: 600px |
|
172 | 172 | |
|
173 | 173 | Time started on x, runtime on y, and color-coded by engine-id (in this case there |
|
174 | 174 | were four engines). Edges denote dependencies. |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | .. _NetworkX: http://networkx.lanl.gov/ |
@@ -1,208 +1,208 b'' | |||
|
1 | 1 | .. _parallel_examples: |
|
2 | 2 | |
|
3 | 3 | ================= |
|
4 | 4 | Parallel examples |
|
5 | 5 | ================= |
|
6 | 6 | |
|
7 | 7 | In this section we describe two more involved examples of using an IPython |
|
8 | 8 | cluster to perform a parallel computation. We will be doing some plotting, |
|
9 | 9 | so we start IPython with matplotlib integration by typing:: |
|
10 | 10 | |
|
11 | 11 | ipython --matplotlib |
|
12 | 12 | |
|
13 | 13 | at the system command line. |
|
14 | 14 | Or you can enable matplotlib integration at any point with: |
|
15 | 15 | |
|
16 | 16 | .. sourcecode:: ipython |
|
17 | 17 | |
|
18 | 18 | In [1]: %matplotlib |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | 150 million digits of pi |
|
22 | 22 | ======================== |
|
23 | 23 | |
|
24 | 24 | In this example we would like to study the distribution of digits in the |
|
25 | 25 | number pi (in base 10). While it is not known if pi is a normal number (a |
|
26 | 26 | number is normal in base 10 if 0-9 occur with equal likelihood) numerical |
|
27 | 27 | investigations suggest that it is. We will begin with a serial calculation on |
|
28 | 28 | 10,000 digits of pi and then perform a parallel calculation involving 150 |
|
29 | 29 | million digits. |
|
30 | 30 | |
|
31 | 31 | In both the serial and parallel calculation we will be using functions defined |
|
32 | 32 | in the :file:`pidigits.py` file, which is available in the |
|
33 | 33 | :file:`examples/parallel` directory of the IPython source distribution. |
|
34 | 34 | These functions provide basic facilities for working with the digits of pi and |
|
35 | 35 | can be loaded into IPython by putting :file:`pidigits.py` in your current |
|
36 | 36 | working directory and then doing: |
|
37 | 37 | |
|
38 | 38 | .. sourcecode:: ipython |
|
39 | 39 | |
|
40 | 40 | In [1]: run pidigits.py |
|
41 | 41 | |
|
42 | 42 | Serial calculation |
|
43 | 43 | ------------------ |
|
44 | 44 | |
|
45 | 45 | For the serial calculation, we will use `SymPy <http://www.sympy.org>`_ to |
|
46 | 46 | calculate 10,000 digits of pi and then look at the frequencies of the digits |
|
47 | 47 | 0-9. Out of 10,000 digits, we expect each digit to occur 1,000 times. While |
|
48 | 48 | SymPy is capable of calculating many more digits of pi, our purpose here is to |
|
49 | 49 | set the stage for the much larger parallel calculation. |
|
50 | 50 | |
|
51 | 51 | In this example, we use two functions from :file:`pidigits.py`: |
|
52 | 52 | :func:`one_digit_freqs` (which calculates how many times each digit occurs) |
|
53 | 53 | and :func:`plot_one_digit_freqs` (which uses Matplotlib to plot the result). |
|
54 | 54 | Here is an interactive IPython session that uses these functions with |
|
55 | 55 | SymPy: |
|
56 | 56 | |
|
57 | 57 | .. sourcecode:: ipython |
|
58 | 58 | |
|
59 | 59 | In [7]: import sympy |
|
60 | 60 | |
|
61 | 61 | In [8]: pi = sympy.pi.evalf(40) |
|
62 | 62 | |
|
63 | 63 | In [9]: pi |
|
64 | 64 | Out[9]: 3.141592653589793238462643383279502884197 |
|
65 | 65 | |
|
66 | 66 | In [10]: pi = sympy.pi.evalf(10000) |
|
67 | 67 | |
|
68 | 68 | In [11]: digits = (d for d in str(pi)[2:]) # create a sequence of digits |
|
69 | 69 | |
|
70 | 70 | In [13]: freqs = one_digit_freqs(digits) |
|
71 | 71 | |
|
72 | 72 | In [14]: plot_one_digit_freqs(freqs) |
|
73 | 73 | Out[14]: [<matplotlib.lines.Line2D object at 0x18a55290>] |
|
74 | 74 | |
|
75 | 75 | The resulting plot of the single digit counts shows that each digit occurs |
|
76 | 76 | approximately 1,000 times, but that with only 10,000 digits the |
|
77 | 77 | statistical fluctuations are still rather large: |
|
78 | 78 | |
|
79 | 79 | .. image:: figs/single_digits.* |
|
80 | 80 | |
|
81 | 81 | It is clear that to reduce the relative fluctuations in the counts, we need |
|
82 | 82 | to look at many more digits of pi. That brings us to the parallel calculation. |
|
83 | 83 | |
|
84 | 84 | Parallel calculation |
|
85 | 85 | -------------------- |
|
86 | 86 | |
|
87 | 87 | Calculating many digits of pi is a challenging computational problem in itself. |
|
88 | 88 | Because we want to focus on the distribution of digits in this example, we |
|
89 | 89 | will use pre-computed digit of pi from the website of Professor Yasumasa |
|
90 | 90 | Kanada at the University of Tokyo (http://www.super-computing.org). These |
|
91 | 91 | digits come in a set of text files (ftp://pi.super-computing.org/.2/pi200m/) |
|
92 | 92 | that each have 10 million digits of pi. |
|
93 | 93 | |
|
94 | 94 | For the parallel calculation, we have copied these files to the local hard |
|
95 | 95 | drives of the compute nodes. A total of 15 of these files will be used, for a |
|
96 | 96 | total of 150 million digits of pi. To make things a little more interesting we |
|
97 | 97 | will calculate the frequencies of all 2 digits sequences (00-99) and then plot |
|
98 | 98 | the result using a 2D matrix in Matplotlib. |
|
99 | 99 | |
|
100 | 100 | The overall idea of the calculation is simple: each IPython engine will |
|
101 | 101 | compute the two digit counts for the digits in a single file. Then in a final |
|
102 | 102 | step the counts from each engine will be added up. To perform this |
|
103 | 103 | calculation, we will need two top-level functions from :file:`pidigits.py`: |
|
104 | 104 | |
|
105 |
.. literalinclude:: ../../../examples/ |
|
|
105 | .. literalinclude:: ../../../examples/Parallel Computing/pi/pidigits.py | |
|
106 | 106 | :language: python |
|
107 | 107 | :lines: 47-62 |
|
108 | 108 | |
|
109 | 109 | We will also use the :func:`plot_two_digit_freqs` function to plot the |
|
110 | 110 | results. The code to run this calculation in parallel is contained in |
|
111 | 111 | :file:`examples/parallel/parallelpi.py`. This code can be run in parallel |
|
112 | 112 | using IPython by following these steps: |
|
113 | 113 | |
|
114 | 114 | 1. Use :command:`ipcluster` to start 15 engines. We used 16 cores of an SGE linux |
|
115 | 115 | cluster (1 controller + 15 engines). |
|
116 | 116 | 2. With the file :file:`parallelpi.py` in your current working directory, open |
|
117 | 117 | up IPython, enable matplotlib, and type ``run parallelpi.py``. This will download |
|
118 | 118 | the pi files via ftp the first time you run it, if they are not |
|
119 | 119 | present in the Engines' working directory. |
|
120 | 120 | |
|
121 | 121 | When run on our 16 cores, we observe a speedup of 14.2x. This is slightly |
|
122 | 122 | less than linear scaling (16x) because the controller is also running on one of |
|
123 | 123 | the cores. |
|
124 | 124 | |
|
125 | 125 | To emphasize the interactive nature of IPython, we now show how the |
|
126 | 126 | calculation can also be run by simply typing the commands from |
|
127 | 127 | :file:`parallelpi.py` interactively into IPython: |
|
128 | 128 | |
|
129 | 129 | .. sourcecode:: ipython |
|
130 | 130 | |
|
131 | 131 | In [1]: from IPython.parallel import Client |
|
132 | 132 | |
|
133 | 133 | # The Client allows us to use the engines interactively. |
|
134 | 134 | # We simply pass Client the name of the cluster profile we |
|
135 | 135 | # are using. |
|
136 | 136 | In [2]: c = Client(profile='mycluster') |
|
137 | 137 | In [3]: v = c[:] |
|
138 | 138 | |
|
139 | 139 | In [3]: c.ids |
|
140 | 140 | Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
|
141 | 141 | |
|
142 | 142 | In [4]: run pidigits.py |
|
143 | 143 | |
|
144 | 144 | In [5]: filestring = 'pi200m.ascii.%(i)02dof20' |
|
145 | 145 | |
|
146 | 146 | # Create the list of files to process. |
|
147 | 147 | In [6]: files = [filestring % {'i':i} for i in range(1,16)] |
|
148 | 148 | |
|
149 | 149 | In [7]: files |
|
150 | 150 | Out[7]: |
|
151 | 151 | ['pi200m.ascii.01of20', |
|
152 | 152 | 'pi200m.ascii.02of20', |
|
153 | 153 | 'pi200m.ascii.03of20', |
|
154 | 154 | 'pi200m.ascii.04of20', |
|
155 | 155 | 'pi200m.ascii.05of20', |
|
156 | 156 | 'pi200m.ascii.06of20', |
|
157 | 157 | 'pi200m.ascii.07of20', |
|
158 | 158 | 'pi200m.ascii.08of20', |
|
159 | 159 | 'pi200m.ascii.09of20', |
|
160 | 160 | 'pi200m.ascii.10of20', |
|
161 | 161 | 'pi200m.ascii.11of20', |
|
162 | 162 | 'pi200m.ascii.12of20', |
|
163 | 163 | 'pi200m.ascii.13of20', |
|
164 | 164 | 'pi200m.ascii.14of20', |
|
165 | 165 | 'pi200m.ascii.15of20'] |
|
166 | 166 | |
|
167 | 167 | # download the data files if they don't already exist: |
|
168 | 168 | In [8]: v.map(fetch_pi_file, files) |
|
169 | 169 | |
|
170 | 170 | # This is the parallel calculation using the Client.map method |
|
171 | 171 | # which applies compute_two_digit_freqs to each file in files in parallel. |
|
172 | 172 | In [9]: freqs_all = v.map(compute_two_digit_freqs, files) |
|
173 | 173 | |
|
174 | 174 | # Add up the frequencies from each engine. |
|
175 | 175 | In [10]: freqs = reduce_freqs(freqs_all) |
|
176 | 176 | |
|
177 | 177 | In [11]: plot_two_digit_freqs(freqs) |
|
178 | 178 | Out[11]: <matplotlib.image.AxesImage object at 0x18beb110> |
|
179 | 179 | |
|
180 | 180 | In [12]: plt.title('2 digit counts of 150m digits of pi') |
|
181 | 181 | Out[12]: <matplotlib.text.Text object at 0x18d1f9b0> |
|
182 | 182 | |
|
183 | 183 | The resulting plot generated by Matplotlib is shown below. The colors indicate |
|
184 | 184 | which two digit sequences are more (red) or less (blue) likely to occur in the |
|
185 | 185 | first 150 million digits of pi. We clearly see that the sequence "41" is |
|
186 | 186 | most likely and that "06" and "07" are least likely. Further analysis would |
|
187 | 187 | show that the relative size of the statistical fluctuations have decreased |
|
188 | 188 | compared to the 10,000 digit calculation. |
|
189 | 189 | |
|
190 | 190 | .. image:: figs/two_digit_counts.* |
|
191 | 191 | |
|
192 | 192 | Conclusion |
|
193 | 193 | ========== |
|
194 | 194 | |
|
195 | 195 | To conclude these examples, we summarize the key features of IPython's |
|
196 | 196 | parallel architecture that have been demonstrated: |
|
197 | 197 | |
|
198 | 198 | * Serial code can be parallelized often with only a few extra lines of code. |
|
199 | 199 | We have used the :class:`DirectView` and :class:`LoadBalancedView` classes |
|
200 | 200 | for this purpose. |
|
201 | 201 | * The resulting parallel code can be run without ever leaving the IPython's |
|
202 | 202 | interactive shell. |
|
203 | 203 | * Any data computed in parallel can be explored interactively through |
|
204 | 204 | visualization or further numerical calculations. |
|
205 | 205 | * We have run these examples on a cluster running RHEL 5 and Sun GridEngine. |
|
206 | 206 | IPython's built in support for SGE (and other batch systems) makes it easy |
|
207 | 207 | to get started with IPython's parallel capabilities. |
|
208 | 208 |
General Comments 0
You need to be logged in to leave comments.
Login now