Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you capture the output of ipython's magic methods? (timeit)

I want to capture and plot the results from 5 or so timeit calls with logarithmically increasing sizes of N to show how methodX() scales with input.

So far I have tried:

output = %timeit -r 10 results = methodX(N) 

It does not work...

Can't find info in the docs either. I feel like you should be able to at least intercept the string that is printed. After that I can parse it to extract my info.

Has anyone done this or tried?

PS: this is in an ipython notebook if that makes a diff.

like image 253
Gus Avatar asked Jun 26 '13 02:06

Gus


People also ask

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

What is the input of a cell magic method?

Magic commands come in two flavors: line magics, which are denoted by a single % prefix and operate on a single line of input, and cell magics, which are denoted by a double %% prefix and operate on multiple lines of input.

What is a magic command used for in Jupyter?

Jupyter Notebook - Big Data Visualization Tool These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself. Magic commands act as convenient functions where Python syntax is not the most natural one.

What is Magic command in IPython?

IPython - Magic Commands. Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.

What does%timeit do in Python?

The %timeit magic runs the given code many times, then returns the speed of the fastest result. The %%timeit cell magic can be used to time blocks of code.

Can magic functions make arbitrary changes to input in Python?

They can in fact make arbitrary modifications to the input they receive, which need not even be a valid Python code at all. They receive the whole block as a single string. To know more about magic functions, the built-in magics and their docstrings, use the magic command.

How do I get the output of IPython?

IPython will run the given command using commands.getoutput (), and return the result formatted as a list (split on ‘n’). Since the output is _returned_, it will be stored in ipython’s regular output cache Out [N] and in the ‘_N’ automatic variables.


2 Answers

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:

In [1]: result = %timeit -o 1 + 2 Out[1]: 10000000 loops, best of 3: 23.2 ns per loop  In [2]: result.best Out[2]: 2.3192405700683594e-08 
like image 182
Iguananaut Avatar answered Sep 27 '22 17:09

Iguananaut


PS: this is in an ipython notebook if that makes a diff.

No it does not.

On dev there is te %%capture cell magic. The other way would be to modify the timeit magic to return value instead of printing, or use the timeit module itself. Patches welcomed.

like image 20
Matt Avatar answered Sep 27 '22 17:09

Matt