[SciPy-dev] Re: Octave array formatting examples

Fernando Pérez fperez at pizero.colorado.edu
Mon Feb 18 18:39:30 CST 2002


> I think there is an easier way. The str() representation of arrays is
> done by a module, which can be overloaded or replaced. There was once
> somebody who has done this and also implemented the nice feature to
> only show array up to some size. New users of NumPy had very often the
> suprise, that the building of the output for big arrays take such a
> long time, that it looks like the system is hanging. (The output is
> not streaming).

Well, I actually think the amount of real work is the same whether
implemented via __str__ or via a display hook. Since having a
user-modifyable display hook is in general a good idea for a flexible
system, I went ahead and put in the necessary things for that to be possible.

If you grab the latest IPython from
http://www-hep.colorado.edu/~fperez/ipython/ and install it, you'll get a
system which knows about Numeric Arrays for printing, and has a @format
command a la Octave. You need to start it with the 'math' profile, via
'ipython -p math' for this to be enabled.

Now, the functionality is currently empty, but what's left to be done is 100%
IPython-independent. Here's what you get now:

In [1]: i=identity(3)

In [2]: i
Out[2]= NumPy array, format: long
[[1 0 0]
 [0 1 0]
 [0 0 1]]

In [3]: x=[0,1,2]

In [4]: x
Out[4]=
[0, 1, 2]

In [5]: format short

In [6]: format long e
Invalid format: long e
Valid formats: ['long', 'short']


As you see, it currently only prints a little message telling you it knows you
gave it a numpy array, and the format command doesn't really do anyhting. But
the hooks are in place. The function that needs to be filled in is (defined in
the IPython/Extensions/numeric_formats.py file):

def num_display(self,arg):
    """Display method for printing which treats Numeric arrays specially.
    """

    # Non-numpy variables are printed using the system default
    if type(arg) != ArrayType:
        self._display(arg)
        return
    # Otherwise, we do work.
    format = __IP.runtime_rc.numarray_print_format
    print 'NumPy array, format:',format

    # ***Here is where all the printing logic needs to be implemented***
    print arg # nothing yet :)

Right now it just does a 'print arg' and nothing else. But maybe someone else
wants to tackle it :) As I said, all the IPython-specific work is finished.

I had a quick look at the ArrayPrinter file in Numeric, and there seems to be
a lot in there. Now I have to go back to working on other things, but maybe
that could be a good starting point of code for this.

Well, this is my 'proof-of-concept' code. I think at least the architecture is
flexible enough to support what Eric wants. But writing all the fancy
octave-like output is actually a fair bit of work, so that will wait. Plus,
it's curses-based (it computes window width for splitting things), so we'd
need to decide whether making curses a mandatory dependency (which kills
Windows platforms) is a good idea.

Cheers,

f.




More information about the Scipy-dev mailing list