[IPython-dev] More %magics - numpy print options

Robert Kern robert.kern@gmail....
Fri Jan 23 00:48:19 CST 2009


I've made a couple more %magics, this time to manage a stack of numpy's print options. If 
you've ever run into numpy's 1000-element threshold for summarization or the small number 
of digits and just wanted to change them temporarily, I think you'll appreciate this.

   http://www.enthought.com/~rkern/cgi-bin/hgwebdir.cgi/ipython-magics/

In [11]: %push_print?
Type:             Magic function
Base Class:       <type 'instancemethod'>
Namespace:        IPython internal
File:             /Users/rkern/.ipython/mymagics.py
Definition:       %push_print(self, arg)
Docstring:
     Set numpy array printing options by pushing onto a stack.

     %push_print [-p PRECISION] [-t THRESHOLD] [-e EDGEITEMS] [-l LINEWIDTH]
                       [-s] [-S] [-n NANSTR] [-i INFSTR] [-q]

     optional arguments:
       -p PRECISION, --precision PRECISION
                             Number of digits of precision for floating point
                             output.
       -t THRESHOLD, --threshold THRESHOLD
                             Total number of array elements which trigger
                             summarization rather than a full repr. 0 disables
                             thresholding.
       -e EDGEITEMS, --edgeitems EDGEITEMS
                             Number of array items in summary at beginning and end
                             of each dimension.
       -l LINEWIDTH, --linewidth LINEWIDTH
                             The number of characters per line for the purpose of
                             inserting line breaks.
       -s, --suppress        Suppress the printing of small floating point values.
       -S, --no-suppress     Do not suppress the printing of small floating point
                             values.
       -n NANSTR, --nanstr NANSTR
                             String representation of floating point not-a-number.
       -i INFSTR, --infstr INFSTR
                             String representation of floating point infinity.
       -q, --quiet           Do not print the new settings.

In [12]: from numpy import *

In [13]: x = linspace(0, 1, 10)

In [14]: x
Out[14]:
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])

In [15]: %push_print -p 16
Precision:  16
Threshold:  1000
Edge items: 3
Line width: 75
Suppress:   False
NaN:        NaN
Inf:        Inf

In [16]: x
Out[16]:
array([ 0.                ,  0.1111111111111111,  0.2222222222222222,
         0.3333333333333333,  0.4444444444444444,  0.5555555555555556,
         0.6666666666666666,  0.7777777777777777,  0.8888888888888888,  1.                ])

In [17]: %pop_print
Precision:  8
Threshold:  1000
Edge items: 3
Line width: 75
Suppress:   False
NaN:        NaN
Inf:        Inf

In [18]: x
Out[18]:
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-dev mailing list