Find max non-infinity element in pytables CArray

654 views Asked by At

This must be easy, but I'm very new to pytables. My application has dataset sizes so large they cannot be held in memory, thus I use PyTable CArrays. However, I need to find the maximum element in an array that is not infinity. Naively in numpy I'd do this:

max_element = numpy.max(array[array != numpy.inf])

Obviously that won't work in PyTables without introducing a whole array into memory. I could loop through the CArray in windows that fit in memory, but it'd be surprising to me if there weren't a max/min reduction operation. Is there an elegant mechanism to get the conditional maximum element of that array?

1

There are 1 answers

0
Anthony Scopatz On BEST ANSWER

If your CArray is one dimensional, it is probably easier to stick it in a single-column Table. Then you have access to the where() method and can easily evaluate expressions like the following.

from itertools import imap
max(imap(lamdba r: r['col'], tab.where('col != np.inf')))

This works because where() never reads in all the data at once and returns an iterator, which is handed off to map, which is handed off to max. Note that in Python 3, you don't need to import imap() and imap() becomes just the builtin map().

Not using a table means that you need to use the Expr class and do more of the wiring yourself.