Capture the result of an IPython magic function
jupyter magic commands cheat sheet
usageerror: line magic function `%%capture` not found.
usageerror: cell magic `%%` not found.
how to run unix commands in jupyter notebook
I'm trying to capture the resulting object of IPython Notebook magic function. Specifically
So the following code...
import time def say_hello(n): time.sleep(n) print "hello" t = %timeit say_hello(5)
Prints to stdout:
1 loops, best of 3: 5 s per loop
However, I'd like to capture the result of
%timeit say_hello(5) in the variable
A resulting object called
TimeitResult is generated by
%timeit, but I can't figure out how to access it from within a Notebook.
I'd like a cleaner solution than having to manually capture stdout using
sys.stdout tricks (this code will be part of a presentation so I'm trying to keep it as straight forward as possible). Anyone have any ideas?
In the source file you linked to, the docstring shows the options for running the timeit magic function; one of which is returning an object result:
-o: return a TimeitResult that can be stored in a variable to inspect the result in more details.
So, if you run
obj = %timeit -o somefunc()
obj will reference the result object that was returned (hint: use tab completion on the object, that will show you the attributes it has).
Built-in magic commands — IPython 7.16.1 documentation, Aliases have lower precedence than magic functions and Python normal Shell capture - run shell command and capture output (DEPRECATED use !) This duplicate question Capture the result of an IPython magic function has an answer demonstrating that this has since been implemented. Calling the %timeit magic with the -o option like: %timeit -o <statement> returns a TimeitResult object which is a simple object with all information about the %timeit run as attributes. For example:
An example of consuming the TimeItResult output:
myarray = (3,2,1) sorttime = %timeit -n1 -r3 -o myarray.sort() print(sorttime.best)
Look here for the other TimeItResult attributes: https://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.execution.html
IPython reference — IPython 3.2.1 documentation, system) to get information about any particular magic function you are interested in. example (the %sx magic runs a shell command and captures the output). This magic function restricts a floating point result to specified digits after decimal. %pwd. This magic function returns the present working directory. %pylab. This function populates current IPython session with matplotlib, and numpy libraries. %recall. When executed without any parameter, this function executes previous command.
Complementing @dsemi's answer: Use
-o to save the
timeit result into a variable, e.g.:
obj = %timeit -o somefunc()
The docstring documentation of the
TimeitResult when using tab completion shows the available attributes:
Object returned by the timeit magic with info about the run. Contains the following attributes : loops: (int) number of loops done per measurement repeat: (int) number of times the measurement has been repeated best: (float) best execution time / number all_runs: (list of float) execution time of each run (in s) compile_time: (float) time of statement compilation (s)
Module: core.magics.execution — IPython 2.4.2-maint documentation, core.magics.execution �. Implementation of execution-related magic functions. CapturedIO object with stdout/err attributes for the text of the captured output. I get the same result on Ipython 3.1 and python 3.4. The %%capture function changed a bit from 1.x to 3.x. Seems the 3.x documentation claims that it should capture stdout/stderr and the ipython rich display system. %%capture grabs all output types, not just stdout/stderr, so you can do plots and use IPython's display system inside %%capture
%%capture cell magic does not suppress automatic output � Issue , I get the same result on Ipython 3.1 and python 3.4. The %%capture function changed a bit from 1.x to 3.x. Seems the 3.x documentation claims� Magic ¶ class IPython.Magic.Magic(shell)¶ Magic functions for InteractiveShell. Shell functions which can be reached as %function_name. All magic functions should accept a string, which they can parse for their own needs. This can make some functions easier to type, eg %cd ../ vs. %cd(“../”) ALL definitions MUST begin with the prefix
iPython - Magic Function, iPython magic function can be called with a command line style syntax. Articles Related Type More Manual capture of command output and magic output� Magic functions are one of the IPython’s cool features. The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features. There are two kinds of magics, line-oriented and cell-oriented. Line magics
IPython - Magic Commands, IPython - Magic Commands - Magic commands or magic functions are one of the Magic commands act as convenient functions where Python syntax is not the using Jupyter notebook, %matplotlib inline directive displays plot output in the� The %run magic command allows you to run any python script and load all of its data directly into the interactive namespace. Since the file is re-read from disk each time, changes you make to it are reflected immediately (unlike imported modules, which have to be specifically reloaded). IPython also includes dreload, a recursive reload function.
- Do you want the string of the output, or the TimeitResult object?
- possible duplicate of Can you capture the output of ipython's magic methods? (timeit)
- Awesome, was exactly what I was looking for!
- Is there any documentation of the
TimeitResultobject so that I don't have to use the tab completion trick and figure out all attributes myself?
- Couldn't find any official docs, but the docstring in the source shows all of the attributes.