pythonnumpyrecarray

Inheriting from numpy.recarray, __unicode__ issue


I have made a subclass of a numpy.recarray. The purpose of the class is to provide pretty printing for record arrays while maintaining the record array functionality.

Here is the code:

import numpy as np
import re

class TableView(np.recarray):

    def __new__(cls,array):
        return np.asarray(array).view(cls)

    def __str__(self):
        return self.__unicode__().encode("UTF-8")

    def __repr__(self):
        return self.__unicode__().encode("UTF-8")

    def __unicode__(self):

        options = np.get_printoptions()
        nrows = len(self)

        print "unicode called"

        if nrows > 2*options['edgeitems']:
            if nrows > options['threshold']:
                nrows = 2*options['edgeitems'] + 1

        ncols = len(self.dtype)

        fields = self.dtype.names

        strdata = np.empty((nrows + 1,ncols),dtype='S32')
        strdata[0] = fields

        np_len = np.vectorize(lambda x: len(x))
        maxcolchars = np.empty(ncols,dtype='i4')

        for i, field in enumerate(fields):
            strdata[1:,i] = re.sub('[\[\]]','',np.array_str(self[field])).split()
            maxcolchars[i] = np.amax(np_len(strdata[:,i]))

        rowformat = ' '.join(["{:>%s}" % maxchars for maxchars in maxcolchars])
        formatrow = lambda row: (rowformat).format(*row)
        strdata = np.apply_along_axis(formatrow,1,strdata)

        return '\n'.join(strdata)

Here is how it prints:

In [3]: x = np.array([(22, 2, -1000000000.0, 2000.0), (22, 2, 400.0, 2000.0),
   ...:  (22, 2, 500.0, 2000.0), (44, 2, 800.0, 4000.0), (55, 5, 900.0, 5000.0),
   ...:  (55, 5, 1000.0, 5000.0), (55, 5, 8900.0, 5000.0),
   ...:  (55, 5, 11400.0, 5000.0), (33, 3, 14500.0, 3000.0),
   ...:  (33, 3, 40550.0, 3000.0), (33, 3, 40990.0, 3000.0),
   ...:  (33, 3, 44400.01213545, 3000.0)],  
   ...:           dtype=[('subcase', '<i4'), ('id', '<i4'), ('vonmises', '<f4'), ('maxprincipal', '<f4')])

In [6]: TableView(x)
unicode called
Out[6]: 
subcase id        vonmises maxprincipal
     22  2 -1.00000000e+09        2000.
     22  2  4.00000000e+02        2000.
     22  2  5.00000000e+02        2000.
     44  2  8.00000000e+02        4000.
     55  5  9.00000000e+02        5000.
     55  5  1.00000000e+03        5000.
     55  5  8.90000000e+03        5000.
     55  5  1.14000000e+04        5000.
     33  3  1.45000000e+04        3000.
     33  3  4.05500000e+04        3000.
     33  3  4.09900000e+04        3000.
     33  3  4.44000117e+04        3000.

But this does not work when i print only one row:

In [7]: TableView(x)[0]
Out[7]: (22, 2, -1000000000.0, 2000.0)

It works for multiple rows:

In [8]: TableView(x)[0:1]
unicode called
Out[8]: 
subcase id        vonmises maxprincipal
     22  2 -1.00000000e+09        2000.

Upon further investigation:

In [10]: type(TableView(x)[0])
Out[10]: numpy.record

In [11]: type(TableView(x)[0:1])
Out[11]: __main__.TableView

How can i make a numpy.record of TableView have the same unicode?


Solution

  • Your diagnosis is right. A single element of this class is a record, not an Tableview array.

    And indexing with a slice or list, [0:1] or [[0]], is the immediate solution.

    Trying to subclass np.record and changing the elements of the Tableview seems complicated.

    You might try customizing the __getitem__ method. This is called when you index the array.

    It ends with:

        else:
            # return a single element
            return obj
    

    A modified version might return a single element Tableview instead

            return Tableview([obj])
    

    But that might produce some sort of endless recursion, keeping you from accessing elements as regular records.

    Otherwise you might just want to live with the slice indexing.