[Matrix-SIG] Limiting size of arrays printed by Numeric

Roger Burnham rburnham@cri-inc.com
Fri, 23 Jul 1999 14:29:56 -800


On 23 Jul 99, at 14:46, Joe Van Andel mused:

> I'm writing a radar data processing system that uses fairly large
> arrays.  (360 x 900).  When I have a list or dictionary that contains
> such an array, I frequently want to see just the size and type of the
> array,
>  without getting all the content printed by the default repr() function. 
> Otherwise, using Numeric Python interactively is rather awkward, since
> you never know when you may end up accidently printing out some enormous
> (Megabytes!) amount of data.
> 
> 
>  Has anyone over-ridden the repr() function to have thresholds that
> would print "small" arrays as usual, but would print large arrays as
> something like:
> 
> array(500x300, FLOAT)
> 
> How do I over-ride the built in function?  Once I over-ride it, how do I
> access the original built-in function?
> 
> Thanks much, I appreciate your help.

Joe,

This is taken from a windows specific app that gives the user the choice of 
printing the arrary or not.  Should give you the idea...

_opts = {
    'maxLineWidth': 132,
    'precision': 4,
    'suppressSmall': 1,
    'maxElements': 1024,
   }

def _checkSize(a):
    '''Helper to ensure that one does not accidentally try to print a
    huge array (kiss the process good-bye if you do!).
    '''
    size = 1
    for dimSize in a.shape:
        size = size * dimSize
    if size > _opts['maxElements']:
        msg = '''
You have asked to print a very large array.

This array contains %d elements and may take a very
long time and use a lot of memory to actually convert
to a string.

Do you really wish to convert this array to a string?

''' % size
        ans = win32ui.MessageBox(msg, 'Really print the array?',
                                 (win32con.MB_YESNO|
                                  win32con.MB_ICONQUESTION|
                                  win32con.MB_SYSTEMMODAL))
        return (ans == win32con.IDYES)
    else:
        return 1
    
def my_array_repr(a,max_line_width=None, precision=None, suppress_small=None):
    '''Replacement for Numeric.array_repr to check for too large of a string
    conversion, and a way for the user to change the conversion parameters.
    '''
    if max_line_width == None:
        max_line_width = _opts['maxLineWidth']
    if precision == None:
        precision = _opts['precision']
    if suppress_small == None:
        suppress_small = _opts['suppressSmall']
    if _checkSize(a):
        return array2string(a, max_line_width, precision,
                            suppress_small, ', ', 1)
    else:
        return '<array_repr cancelled>'

def my_array_str(a, max_line_width=None, precision=None, suppress_small=None):
    '''Replacement for Numeric.array_str to check for too large of a string
    conversion, and a way for the user to change the conversion parameters.
    '''
    if max_line_width == None:
        max_line_width = _opts['maxLineWidth']
    if precision == None:
        precision = _opts['precision']
    if suppress_small == None:
        suppress_small = _opts['suppressSmall']
    if _checkSize(a):
        return array2string(a, max_line_width, precision,
                            suppress_small, ' ', 0)
    else:
        return '<array_str cancelled>'

# Replace Numeric.array_repr and Numeric.array_str with our "friendly"
# versions.

import Numeric
Numeric.array_repr = my_array_repr
Numeric.array_str = my_array_str

# Do the same for the multiarray module.

import multiarray
multiarray.set_string_function(my_array_str, 0)
multiarray.set_string_function(my_array_repr, 1)

# Clean up the namespace...

del Numeric
del multiarray


Cheers,

Roger Burnham   
Cambridge Research & Instrumentation   
rburnham@cri-inc.com   
http://www.cri-inc.com/   
http://starship.python.net/crew/roger/   
PGP Key: http://www.nai.com/default_pgp.asp   
PGP Fingerprint: 5372 729A 9557 5F36 177F  084A 6C64 BE27 0BC4 CF2D